DirectGeometry.tests.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * @author moraxy / https://github.com/moraxy
  3. * @author TristanVALCKE / https://github.com/Itee
  4. */
  5. /* global QUnit */
  6. import { DirectGeometry } from '../../../../src/core/DirectGeometry';
  7. import { JSONLoader } from '../../../../src/loaders/JSONLoader';
  8. import { Vector2 } from '../../../../src/math/Vector2';
  9. import { Face3 } from '../../../../src/core/Face3';
  10. import { Geometry } from '../../../../src/core/Geometry';
  11. export default QUnit.module( 'Core', () => {
  12. QUnit.module( 'DirectGeometry', () => {
  13. // INSTANCING
  14. QUnit.todo( "Instancing", ( assert ) => {
  15. assert.ok( false, "everything's gonna be alright" );
  16. } );
  17. // PUBLIC STUFF
  18. QUnit.test( "computeGroups", ( assert ) => {
  19. var a = new DirectGeometry();
  20. var b = new Geometry();
  21. var expected = [
  22. { start: 0, materialIndex: 0, count: 3 },
  23. { start: 3, materialIndex: 1, count: 3 },
  24. { start: 6, materialIndex: 2, count: 6 }
  25. ];
  26. // we only care for materialIndex
  27. b.faces.push(
  28. new Face3( 0, 0, 0, undefined, undefined, 0 ),
  29. new Face3( 0, 0, 0, undefined, undefined, 1 ),
  30. new Face3( 0, 0, 0, undefined, undefined, 2 ),
  31. new Face3( 0, 0, 0, undefined, undefined, 2 )
  32. );
  33. a.computeGroups( b );
  34. assert.deepEqual( a.groups, expected, "Groups are as expected" );
  35. } );
  36. QUnit.test( "fromGeometry", ( assert ) => {
  37. if ( typeof XMLHttpRequest === 'undefined' ) {
  38. assert.expect( 0 );
  39. return;
  40. }
  41. assert.timeout( 1000 );
  42. var a = new DirectGeometry();
  43. var asyncDone = assert.async(); // tell QUnit when we're done with async stuff
  44. var loader = new JSONLoader();
  45. loader.load( "../../examples/models/skinned/simple/simple.js", function ( geometry ) {
  46. a.fromGeometry( geometry );
  47. var tmp = new DirectGeometry();
  48. tmp.computeGroups( geometry );
  49. assert.deepEqual( a.groups, tmp.groups, "Check groups" );
  50. var morphTargets = geometry.morphTargets;
  51. var morphTargetsLength = morphTargets.length;
  52. var morphTargetsPosition;
  53. if ( morphTargetsLength > 0 ) {
  54. morphTargetsPosition = [];
  55. for ( var i = 0; i < morphTargetsLength; i ++ ) {
  56. morphTargetsPosition[ i ] = [];
  57. }
  58. morphTargets.position = morphTargetsPosition;
  59. }
  60. var morphNormals = geometry.morphNormals;
  61. var morphNormalsLength = morphNormals.length;
  62. var morphTargetsNormal;
  63. if ( morphNormalsLength > 0 ) {
  64. morphTargetsNormal = [];
  65. for ( var i = 0; i < morphNormalsLength; i ++ ) {
  66. morphTargetsNormal[ i ] = [];
  67. }
  68. morphTargets.normal = morphTargetsNormal;
  69. }
  70. var vertices = [];
  71. var normals = [];
  72. var colors = [];
  73. var uvs = [];
  74. var uvs2 = [];
  75. var skinIndices = [];
  76. var skinWeights = [];
  77. var hasFaceVertexUv = geometry.faceVertexUvs[ 0 ] && geometry.faceVertexUvs[ 0 ].length > 0;
  78. var hasFaceVertexUv2 = geometry.faceVertexUvs[ 1 ] && geometry.faceVertexUvs[ 1 ].length > 0;
  79. var hasSkinIndices = geometry.skinIndices.length === geometry.vertices.length;
  80. var hasSkinWeights = geometry.skinWeights.length === geometry.vertices.length;
  81. for ( var i = 0; i < geometry.faces.length; i ++ ) {
  82. var face = geometry.faces[ i ];
  83. vertices.push(
  84. geometry.vertices[ face.a ],
  85. geometry.vertices[ face.b ],
  86. geometry.vertices[ face.c ]
  87. );
  88. var vertexNormals = face.vertexNormals;
  89. if ( vertexNormals.length === 3 ) {
  90. normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] );
  91. } else {
  92. normals.push( face.normal, face.normal, face.normal );
  93. }
  94. var vertexColors = face.vertexColors;
  95. if ( vertexColors.length === 3 ) {
  96. colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] );
  97. } else {
  98. colors.push( face.color, face.color, face.color );
  99. }
  100. if ( hasFaceVertexUv === true ) {
  101. var vertexUvs = geometry.faceVertexUvs[ 0 ][ i ];
  102. if ( vertexUvs !== undefined ) {
  103. uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  104. } else {
  105. uvs.push( new Vector2(), new Vector2(), new Vector2() );
  106. }
  107. }
  108. if ( hasFaceVertexUv2 === true ) {
  109. var vertexUvs = geometry.faceVertexUvs[ 1 ][ i ];
  110. if ( vertexUvs !== undefined ) {
  111. uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  112. } else {
  113. uvs2.push( new Vector2(), new Vector2(), new Vector2() );
  114. }
  115. }
  116. // morphs
  117. for ( var j = 0; j < morphTargetsLength; j ++ ) {
  118. var morphTarget = morphTargets[ j ].vertices;
  119. morphTargetsPosition[ j ].push(
  120. morphTarget[ face.a ],
  121. morphTarget[ face.b ],
  122. morphTarget[ face.c ]
  123. );
  124. }
  125. for ( var j = 0; j < morphNormalsLength; j ++ ) {
  126. var morphNormal = morphNormals[ j ].vertexNormals[ i ];
  127. morphTargetsNormal[ j ].push( morphNormal.a, morphNormal.b, morphNormal.c );
  128. }
  129. // skins
  130. if ( hasSkinIndices ) {
  131. skinIndices.push(
  132. geometry.skinIndices[ face.a ],
  133. geometry.skinIndices[ face.b ],
  134. geometry.skinIndices[ face.c ]
  135. );
  136. }
  137. if ( hasSkinWeights ) {
  138. skinWeights.push(
  139. geometry.skinWeights[ face.a ],
  140. geometry.skinWeights[ face.b ],
  141. geometry.skinWeights[ face.c ]
  142. );
  143. }
  144. }
  145. assert.deepEqual( a.vertices, vertices, "Vertices are identical" );
  146. assert.deepEqual( a.normals, normals, "Normals are identical" );
  147. assert.deepEqual( a.colors, colors, "Colors are identical" );
  148. assert.deepEqual( a.uvs, uvs, "UV coordinates are identical" );
  149. assert.deepEqual( a.uvs2, uvs2, "UV(2) coordinates are identical" );
  150. assert.deepEqual( a.skinIndices, skinIndices, "SkinIndices are identical" );
  151. assert.deepEqual( a.skinWeights, skinWeights, "SkinWeights are identical" );
  152. assert.deepEqual( a.morphTargetsPosition, morphTargetsPosition, "MorphTargets (Position) are identical" );
  153. assert.deepEqual( a.morphTargetsNormal, morphTargetsNormal, "MorphTargets (Normals) are identical" );
  154. asyncDone();
  155. }, onProgress, onError );
  156. function onProgress() {}
  157. function onError( error ) {
  158. console.error( error );
  159. asyncDone();
  160. }
  161. } );
  162. } );
  163. } );