gulpfile.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. 'use strict';
  2. var fs = require( 'fs' );
  3. var gulp = require( 'gulp' );
  4. var gutil = require( 'gulp-util' );
  5. var obj_verify = {
  6. vertices: [],
  7. normals: [],
  8. uvs: [],
  9. facesV: [],
  10. facesVn: [],
  11. facesVt: [],
  12. materials: []
  13. };
  14. obj_verify.vertices.push( [ -1, 1, 1 ] );
  15. obj_verify.vertices.push( [ -1, -1, 1 ] );
  16. obj_verify.vertices.push( [ 1, -1, 1 ] );
  17. obj_verify.vertices.push( [ 1, 1, 1 ] );
  18. obj_verify.vertices.push( [ -1, 1, -1 ] );
  19. obj_verify.vertices.push( [ -1, -1, -1 ] );
  20. obj_verify.vertices.push( [ 1, -1, -1 ] );
  21. obj_verify.vertices.push( [ 1, 1, -1 ] );
  22. obj_verify.normals.push( [ 0, 0, 1 ] );
  23. obj_verify.normals.push( [ 0, 0, -1 ] );
  24. obj_verify.normals.push( [ 0, 1, 0 ] );
  25. obj_verify.normals.push( [ 0, -1, 0 ] );
  26. obj_verify.normals.push( [ 1, 0, 0 ] );
  27. obj_verify.normals.push( [ -1, 0, 0 ] );
  28. obj_verify.uvs.push( [ 0, 1 ] );
  29. obj_verify.uvs.push( [ 1, 1 ] );
  30. obj_verify.uvs.push( [ 1, 0 ] );
  31. obj_verify.uvs.push( [ 0, 0 ] );
  32. obj_verify.facesV.push( [ 1, 2, 3, 4 ] );
  33. obj_verify.facesV.push( [ 8, 7, 6, 5 ] );
  34. obj_verify.facesV.push( [ 4, 3, 7, 8 ] );
  35. obj_verify.facesV.push( [ 5, 1, 4, 8 ] );
  36. obj_verify.facesV.push( [ 5, 6, 2, 1 ] );
  37. obj_verify.facesV.push( [ 2, 6, 7, 3 ] );
  38. obj_verify.facesVn.push( [ 1, 1, 1, 1 ] );
  39. obj_verify.facesVn.push( [ 2, 2, 2, 2 ] );
  40. obj_verify.facesVn.push( [ 5, 5, 5, 5 ] );
  41. obj_verify.facesVn.push( [ 3, 3, 3, 3 ] );
  42. obj_verify.facesVn.push( [ 6, 6, 6, 6 ] );
  43. obj_verify.facesVn.push( [ 4, 4, 4, 4 ] );
  44. obj_verify.facesVt.push( [ 1, 2, 3, 4 ] );
  45. obj_verify.facesVt.push( [ 1, 2, 3, 4 ] );
  46. obj_verify.facesVt.push( [ 1, 2, 3, 4 ] );
  47. obj_verify.facesVt.push( [ 1, 2, 3, 4 ] );
  48. obj_verify.facesVt.push( [ 1, 2, 3, 4 ] );
  49. obj_verify.facesVt.push( [ 1, 2, 3, 4 ] );
  50. obj_verify.materials.push( 'usemtl red' );
  51. obj_verify.materials.push( 'usemtl blue' );
  52. obj_verify.materials.push( 'usemtl green' );
  53. obj_verify.materials.push( 'usemtl lightblue' );
  54. obj_verify.materials.push( 'usemtl orange' );
  55. obj_verify.materials.push( 'usemtl purple' );
  56. function vobjCreateVertices( factor, offsets ) {
  57. var output = '\n';
  58. for ( var x, y, z, i = 0, v = obj_verify.vertices, length = v.length; i < length; i++ ) {
  59. x = v[ i ][ 0 ] * factor + offsets[ 0 ];
  60. y = v[ i ][ 1 ] * factor + offsets[ 1 ];
  61. z = v[ i ][ 2 ] * factor + offsets[ 2 ];
  62. output += 'v ' + x + ' ' + y + ' ' + z + '\n';
  63. }
  64. return output;
  65. };
  66. function vobjCreateUvs() {
  67. var output = '\n';
  68. for ( var x, y, z, i = 0, vn = obj_verify.uvs, length = vn.length; i < length; i++ ) {
  69. x = vn[ i ][ 0 ];
  70. y = vn[ i ][ 1 ];
  71. output += 'vt ' + x + ' ' + y + '\n';
  72. }
  73. return output;
  74. };
  75. function vobjCreateNormals() {
  76. var output = '\n';
  77. for ( var x, y, z, i = 0, vn = obj_verify.normals, length = vn.length; i < length; i++ ) {
  78. x = vn[ i ][ 0 ];
  79. y = vn[ i ][ 1 ];
  80. z = vn[ i ][ 2 ];
  81. output += 'vn ' + x + ' ' + y + ' ' + z + '\n';
  82. }
  83. return output;
  84. };
  85. function vobjCreateCubeV( offsets, groups, usemtls ) {
  86. var output = '\n';
  87. if ( groups === null || groups.length === 0 ) groups = [ null, null, null, null, null, null ];
  88. if ( usemtls === null || usemtls.length === 0 ) usemtls = [ null, null, null, null, null, null ];
  89. for ( var group, usemtl, f0, f1, f2, f3, i = 0, facesV = obj_verify.facesV, length = facesV.length; i < length; i++ ) {
  90. f0 = facesV[ i ][ 0 ] + offsets[ 0 ];
  91. f1 = facesV[ i ][ 1 ] + offsets[ 0 ];
  92. f2 = facesV[ i ][ 2 ] + offsets[ 0 ];
  93. f3 = facesV[ i ][ 3 ] + offsets[ 0 ];
  94. group = groups[ i ];
  95. usemtl = usemtls[ i ];
  96. if ( group ) output += 'g ' + group + '\n';
  97. if ( usemtl ) output += 'usemtl ' + usemtl + '\n';
  98. output += 'f ' + f0 + ' ' + f1 + ' ' + f2 + ' ' + f3 + '\n';
  99. }
  100. return output;
  101. };
  102. function vobjCreateCubeVVn( offsets, groups, usemtls ) {
  103. var output = '\n';
  104. if ( groups === null || groups.length === 0 ) groups = [ null, null, null, null, null, null ];
  105. if ( usemtls === null || usemtls.length === 0 ) usemtls = [ null, null, null, null, null, null ];
  106. for ( var group, usemtl, f0, f1, f2, f3, i = 0, facesV = obj_verify.facesV, facesVn = obj_verify.facesVn; i < 6; i++ ) {
  107. f0 = facesV[ i ][ 0 ] + offsets[ 0 ] + '//' + ( facesVn[ i ][ 0 ] + offsets[ 1 ] );
  108. f1 = facesV[ i ][ 1 ] + offsets[ 0 ] + '//' + ( facesVn[ i ][ 1 ] + offsets[ 1 ] );
  109. f2 = facesV[ i ][ 2 ] + offsets[ 0 ] + '//' + ( facesVn[ i ][ 2 ] + offsets[ 1 ] );
  110. f3 = facesV[ i ][ 3 ] + offsets[ 0 ] + '//' + ( facesVn[ i ][ 3 ] + offsets[ 1 ] );
  111. group = groups[ i ];
  112. usemtl = usemtls[ i ];
  113. if ( group ) output += 'g ' + group + '\n';
  114. if ( usemtl ) output += 'usemtl ' + usemtl + '\n';
  115. output += 'f ' + f0 + ' ' + f1 + ' ' + f2 + ' ' + f3 + '\n';
  116. }
  117. return output;
  118. };
  119. function vobjCreateCubeVVt( offsets, groups, usemtls ) {
  120. var output = '\n';
  121. if ( groups === null || groups.length === 0 ) groups = [ null, null, null, null, null, null ];
  122. if ( usemtls === null || usemtls.length === 0 ) usemtls = [ null, null, null, null, null, null ];
  123. for ( var group, usemtl, f0, f1, f2, f3, i = 0, facesV = obj_verify.facesV, facesVt = obj_verify.facesVt; i < 6; i++ ) {
  124. f0 = facesV[ i ][ 0 ] + offsets[ 0 ] + '/' + ( facesVt[ i ][ 0 ] + offsets[ 1 ] );
  125. f1 = facesV[ i ][ 1 ] + offsets[ 0 ] + '/' + ( facesVt[ i ][ 1 ] + offsets[ 1 ] );
  126. f2 = facesV[ i ][ 2 ] + offsets[ 0 ] + '/' + ( facesVt[ i ][ 2 ] + offsets[ 1 ] );
  127. f3 = facesV[ i ][ 3 ] + offsets[ 0 ] + '/' + ( facesVt[ i ][ 3 ] + offsets[ 1 ] );
  128. group = groups[ i ];
  129. usemtl = usemtls[ i ];
  130. if ( group ) output += 'g ' + group + '\n';
  131. if ( usemtl ) output += 'usemtl ' + usemtl + '\n';
  132. output += 'f ' + f0 + ' ' + f1 + ' ' + f2 + ' ' + f3 + '\n';
  133. }
  134. return output;
  135. };
  136. function vobjCreateCubeVVnVt( offsets, groups, usemtls ) {
  137. var output = '\n';
  138. if ( groups === null || groups.length === 0 ) groups = [ null, null, null, null, null, null ];
  139. if ( usemtls === null || usemtls.length === 0 ) usemtls = [ null, null, null, null, null, null ];
  140. for ( var group, usemtl, f0, f1, f2, f3, i = 0, facesV = obj_verify.facesV, facesVt = obj_verify.facesVt, facesVn = obj_verify.facesVn; i < 6; i++ ) {
  141. f0 = facesV[ i ][ 0 ] + offsets[ 0 ] + '/' + ( facesVt[ i ][ 0 ] + offsets[ 1 ] ) + '/' + ( facesVn[ i ][ 0 ] + offsets[ 2 ] );
  142. f1 = facesV[ i ][ 1 ] + offsets[ 0 ] + '/' + ( facesVt[ i ][ 1 ] + offsets[ 1 ] ) + '/' + ( facesVn[ i ][ 1 ] + offsets[ 2 ] );
  143. f2 = facesV[ i ][ 2 ] + offsets[ 0 ] + '/' + ( facesVt[ i ][ 2 ] + offsets[ 1 ] ) + '/' + ( facesVn[ i ][ 2 ] + offsets[ 2 ] );
  144. f3 = facesV[ i ][ 3 ] + offsets[ 0 ] + '/' + ( facesVt[ i ][ 3 ] + offsets[ 1 ] ) + '/' + ( facesVn[ i ][ 3 ] + offsets[ 2 ] );
  145. group = groups[ i ];
  146. usemtl = usemtls[ i ];
  147. if ( group ) output += 'g ' + group + '\n';
  148. if ( usemtl ) output += 'usemtl ' + usemtl + '\n';
  149. output += 'f ' + f0 + ' ' + f1 + ' ' + f2 + ' ' + f3 + '\n';
  150. }
  151. return output;
  152. };
  153. gulp.task( 'default', function( cb ){
  154. gutil.log( 'Building: verify.obj' );
  155. var offsets = [ 0, 0, 0 ];
  156. var pos = [ -150, 50, 0 ];
  157. fs.writeFileSync( './verify.obj', '# Verification OBJ created with gulp\n\n' );
  158. fs.appendFileSync( './verify.obj', 'mtllib verify.mtl\n\n# Cube no materials. Translated x:' + pos[ 0 ] );
  159. fs.appendFileSync( './verify.obj', vobjCreateVertices( 10, pos ) );
  160. fs.appendFileSync( './verify.obj', vobjCreateCubeV( offsets, null, null ) );
  161. pos[ 0 ] += 50;
  162. offsets[ 0 ] += 8;
  163. fs.appendFileSync( './verify.obj', '\n\n# Cube with two materials. Translated x:' + pos[ 0 ] );
  164. fs.appendFileSync( './verify.obj', vobjCreateVertices( 10, pos ) );
  165. fs.appendFileSync( './verify.obj', vobjCreateCubeV( offsets, null, [ 'orange', null, null, 'purple', null, null ] ) );
  166. pos[ 0 ] += 50;
  167. offsets[ 0 ] += 8;
  168. fs.appendFileSync( './verify.obj', '\n\n# Cube with normals no materials. Translated x:' + pos[ 0 ] );
  169. fs.appendFileSync( './verify.obj', vobjCreateVertices( 10, pos ) );
  170. fs.appendFileSync( './verify.obj', vobjCreateNormals() );
  171. fs.appendFileSync( './verify.obj', vobjCreateCubeVVn( offsets, [ 'cube3', null, null, null, null, null ], [ 'lightblue', null, null, null, null, null ] ) );
  172. pos[ 0 ] += 50;
  173. offsets[ 0 ] += 8;
  174. fs.appendFileSync( './verify.obj', '\n\n# Cube with uvs and red material. Translated x:' + pos[ 0 ] );
  175. fs.appendFileSync( './verify.obj', vobjCreateVertices( 10, pos ) );
  176. fs.appendFileSync( './verify.obj', vobjCreateUvs() );
  177. fs.appendFileSync( './verify.obj', vobjCreateCubeVVt( offsets, null, [ 'red', null, null, null, null, null ] ) );
  178. pos[ 0 ] += 50;
  179. offsets[ 0 ] += 8;
  180. fs.appendFileSync( './verify.obj', '\n\n# cube with uvs and normals and material. Translated x' + pos[ 0 ] );
  181. fs.appendFileSync( './verify.obj', vobjCreateVertices( 10, pos ) );
  182. fs.appendFileSync( './verify.obj', vobjCreateCubeVVnVt( offsets, [], [ 'red', null, null, 'blue', null, 'green' ] ) );
  183. pos[ 0 ] += 50;
  184. offsets[ 0 ] += 8;
  185. offsets[ 1 ] += 6;
  186. fs.appendFileSync( './verify.obj', '\n\n# cube with uvs and normals and two materials and group for every quad. Translated x:' + pos[ 0 ] );
  187. fs.appendFileSync( './verify.obj', vobjCreateVertices( 10, pos ) );
  188. fs.appendFileSync( './verify.obj', vobjCreateNormals() );
  189. fs.appendFileSync( './verify.obj', vobjCreateCubeVVnVt( [ -9, offsets[ 1 ], offsets[ 2 ] ],
  190. [ 'cube6a', 'cube6b', 'cube6c', 'cube6d', 'cube6e', 'cube6f' ],
  191. [ 'green', null, null, 'orange', null, null ] ) );
  192. pos[ 0 ] += 50;
  193. offsets[ 0 ] += 8;
  194. fs.appendFileSync( './verify.obj', '\n\n# cube with uvs and normals and six materials and six groups, one for every quad. Translated x:' + pos[ 0 ] );
  195. fs.appendFileSync( './verify.obj', vobjCreateVertices( 10, pos ) );
  196. fs.appendFileSync( './verify.obj', vobjCreateCubeVVnVt( offsets,
  197. [ 'cube6a', 'cube6b', 'cube6c', 'cube6d', 'cube6e', 'cube6f' ],
  198. [ 'red', 'blue', 'green', 'lightblue', 'orange', 'purple' ] ) );
  199. pos[ 0 ] += 50;
  200. offsets[ 0 ] += 8;
  201. fs.appendFileSync( './verify.obj', '\n\n# Point. Translated x:' + pos[ 0 ] );
  202. fs.appendFileSync( './verify.obj', vobjCreateVertices( 10, pos ) );
  203. fs.appendFileSync( './verify.obj', '\np -8 -7 -6 -5 -4 -3 -2 -1\n' );
  204. pos[ 0 ] += 50;
  205. offsets[ 0 ] += 8;
  206. fs.appendFileSync( './verify.obj', '\n\n# Line. Translated x:' + pos[ 0 ] );
  207. fs.appendFileSync( './verify.obj', vobjCreateVertices( 10, pos ) );
  208. fs.appendFileSync( './verify.obj', 'l -8 -7 -6 -5 -4 -3 -2 -1 -8 -5 -8 -4 -7 -6 -7 -3 -5 -1 -3 -2 -6 -2 -4 -1\n' );
  209. pos[ 0 ] += 50;
  210. offsets[ 0 ] += 8;
  211. fs.appendFileSync( './verify.obj', '\n\n# Line UV. Translated x:' + pos[ 0 ] );
  212. fs.appendFileSync( './verify.obj', vobjCreateVertices( 10, pos ) );
  213. fs.appendFileSync( './verify.obj', '\nl -8/-2 -7/-1 -6/-2 -5/-1\n' );
  214. });