BufferGeometry.tests.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /* global QUnit */
  2. import { BufferGeometry } from '../../../../src/core/BufferGeometry.js';
  3. import {
  4. BufferAttribute,
  5. Float16BufferAttribute,
  6. Uint16BufferAttribute,
  7. Uint32BufferAttribute
  8. } from '../../../../src/core/BufferAttribute.js';
  9. import { Vector3 } from '../../../../src/math/Vector3.js';
  10. import { Matrix4 } from '../../../../src/math/Matrix4.js';
  11. import { Quaternion } from '../../../../src/math/Quaternion.js';
  12. import { Sphere } from '../../../../src/math/Sphere.js';
  13. import { x, y, z } from '../../utils/math-constants.js';
  14. import { EventDispatcher } from '../../../../src/core/EventDispatcher.js';
  15. import { toHalfFloat } from '../../../../src/extras/DataUtils.js';
  16. const DegToRad = Math.PI / 180;
  17. function bufferAttributeEquals( a, b, tolerance ) {
  18. tolerance = tolerance || 0.0001;
  19. if ( a.count !== b.count || a.itemSize !== b.itemSize ) {
  20. return false;
  21. }
  22. for ( let i = 0, il = a.count * a.itemSize; i < il; i ++ ) {
  23. const delta = Math.abs( a.array[ i ] - b.array[ i ] );
  24. if ( delta > tolerance ) {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. function getBBForVertices( vertices ) {
  31. const geometry = new BufferGeometry();
  32. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  33. geometry.computeBoundingBox();
  34. return geometry.boundingBox;
  35. }
  36. function getBSForVertices( vertices ) {
  37. const geometry = new BufferGeometry();
  38. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  39. geometry.computeBoundingSphere();
  40. return geometry.boundingSphere;
  41. }
  42. function getNormalsForVertices( vertices, assert ) {
  43. const geometry = new BufferGeometry();
  44. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  45. geometry.computeVertexNormals();
  46. assert.ok( geometry.attributes.normal !== undefined, 'normal attribute was created' );
  47. return geometry.attributes.normal.array;
  48. }
  49. export default QUnit.module( 'Core', () => {
  50. QUnit.module( 'BufferGeometry', () => {
  51. // INHERITANCE
  52. QUnit.test( 'Extending', ( assert ) => {
  53. const object = new BufferGeometry();
  54. assert.strictEqual(
  55. object instanceof EventDispatcher, true,
  56. 'BufferGeometry extends from EventDispatcher'
  57. );
  58. } );
  59. // INSTANCING
  60. QUnit.test( 'Instancing', ( assert ) => {
  61. const object = new BufferGeometry();
  62. assert.ok( object, 'Can instantiate a BufferGeometry.' );
  63. } );
  64. // PROPERTIES
  65. QUnit.todo( 'id', ( assert ) => {
  66. assert.ok( false, 'everything\'s gonna be alright' );
  67. } );
  68. QUnit.todo( 'uuid', ( assert ) => {
  69. assert.ok( false, 'everything\'s gonna be alright' );
  70. } );
  71. QUnit.todo( 'name', ( assert ) => {
  72. assert.ok( false, 'everything\'s gonna be alright' );
  73. } );
  74. QUnit.test( 'type', ( assert ) => {
  75. const object = new BufferGeometry();
  76. assert.ok(
  77. object.type === 'BufferGeometry',
  78. 'BufferGeometry.type should be BufferGeometry'
  79. );
  80. } );
  81. QUnit.todo( 'index', ( assert ) => {
  82. assert.ok( false, 'everything\'s gonna be alright' );
  83. } );
  84. QUnit.todo( 'attributes', ( assert ) => {
  85. assert.ok( false, 'everything\'s gonna be alright' );
  86. } );
  87. QUnit.todo( 'morphAttributes', ( assert ) => {
  88. assert.ok( false, 'everything\'s gonna be alright' );
  89. } );
  90. QUnit.todo( 'morphTargetsRelative', ( assert ) => {
  91. assert.ok( false, 'everything\'s gonna be alright' );
  92. } );
  93. QUnit.todo( 'groups', ( assert ) => {
  94. assert.ok( false, 'everything\'s gonna be alright' );
  95. } );
  96. QUnit.todo( 'boundingBox', ( assert ) => {
  97. assert.ok( false, 'everything\'s gonna be alright' );
  98. } );
  99. QUnit.todo( 'boundingSphere', ( assert ) => {
  100. assert.ok( false, 'everything\'s gonna be alright' );
  101. } );
  102. QUnit.todo( 'drawRange', ( assert ) => {
  103. assert.ok( false, 'everything\'s gonna be alright' );
  104. } );
  105. QUnit.todo( 'userData', ( assert ) => {
  106. assert.ok( false, 'everything\'s gonna be alright' );
  107. } );
  108. // PUBLIC
  109. QUnit.test( 'isBufferGeometry', ( assert ) => {
  110. const object = new BufferGeometry();
  111. assert.ok(
  112. object.isBufferGeometry,
  113. 'BufferGeometry.isBufferGeometry should be true'
  114. );
  115. } );
  116. QUnit.test( 'setIndex/getIndex', ( assert ) => {
  117. const a = new BufferGeometry();
  118. const uint16 = [ 1, 2, 3 ];
  119. const uint32 = [ 65535, 65536, 65537 ];
  120. const str = 'foo';
  121. a.setIndex( uint16 );
  122. assert.ok( a.getIndex() instanceof Uint16BufferAttribute, 'Index has the right type' );
  123. assert.deepEqual( a.getIndex().array, new Uint16Array( uint16 ), 'Small index gets stored correctly' );
  124. a.setIndex( uint32 );
  125. assert.ok( a.getIndex() instanceof Uint32BufferAttribute, 'Index has the right type' );
  126. assert.deepEqual( a.getIndex().array, new Uint32Array( uint32 ), 'Large index gets stored correctly' );
  127. a.setIndex( str );
  128. assert.strictEqual( a.getIndex(), str, 'Weird index gets stored correctly' );
  129. } );
  130. QUnit.todo( 'getAttribute', ( assert ) => {
  131. assert.ok( false, 'everything\'s gonna be alright' );
  132. } );
  133. QUnit.test( 'set / delete Attribute', ( assert ) => {
  134. const geometry = new BufferGeometry();
  135. const attributeName = 'position';
  136. assert.ok( geometry.attributes[ attributeName ] === undefined, 'no attribute defined' );
  137. geometry.setAttribute( attributeName, new BufferAttribute( new Float32Array( [ 1, 2, 3 ], 1 ) ) );
  138. assert.ok( geometry.attributes[ attributeName ] !== undefined, 'attribute is defined' );
  139. geometry.deleteAttribute( attributeName );
  140. assert.ok( geometry.attributes[ attributeName ] === undefined, 'no attribute defined' );
  141. } );
  142. QUnit.test( 'addGroup', ( assert ) => {
  143. const a = new BufferGeometry();
  144. const expected = [
  145. {
  146. start: 0,
  147. count: 1,
  148. materialIndex: 0
  149. },
  150. {
  151. start: 1,
  152. count: 2,
  153. materialIndex: 2
  154. }
  155. ];
  156. a.addGroup( 0, 1, 0 );
  157. a.addGroup( 1, 2, 2 );
  158. assert.deepEqual( a.groups, expected, 'Check groups were stored correctly and in order' );
  159. a.clearGroups();
  160. assert.strictEqual( a.groups.length, 0, 'Check groups were deleted correctly' );
  161. } );
  162. QUnit.todo( 'clearGroups', ( assert ) => {
  163. assert.ok( false, 'everything\'s gonna be alright' );
  164. } );
  165. QUnit.test( 'setDrawRange', ( assert ) => {
  166. const a = new BufferGeometry();
  167. a.setDrawRange( 1.0, 7 );
  168. assert.deepEqual( a.drawRange, {
  169. start: 1,
  170. count: 7
  171. }, 'Check draw range was stored correctly' );
  172. } );
  173. QUnit.test( 'applyMatrix4', ( assert ) => {
  174. const geometry = new BufferGeometry();
  175. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( 6 ), 3 ) );
  176. const matrix = new Matrix4().set(
  177. 1, 0, 0, 1.5,
  178. 0, 1, 0, - 2,
  179. 0, 0, 1, 3,
  180. 0, 0, 0, 1
  181. );
  182. geometry.applyMatrix4( matrix );
  183. const position = geometry.attributes.position.array;
  184. const m = matrix.elements;
  185. assert.ok( position[ 0 ] === m[ 12 ] && position[ 1 ] === m[ 13 ] && position[ 2 ] === m[ 14 ], 'position was extracted from matrix' );
  186. assert.ok( position[ 3 ] === m[ 12 ] && position[ 4 ] === m[ 13 ] && position[ 5 ] === m[ 14 ], 'position was extracted from matrix twice' );
  187. assert.ok( geometry.attributes.position.version === 1, 'version was increased during update' );
  188. } );
  189. QUnit.test( 'applyQuaternion', ( assert ) => {
  190. const geometry = new BufferGeometry();
  191. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ) );
  192. const q = new Quaternion( 0.5, 0.5, 0.5, 0.5 );
  193. geometry.applyQuaternion( q );
  194. const pos = geometry.attributes.position.array;
  195. // geometry was rotated around the (1, 1, 1) axis.
  196. assert.ok( pos[ 0 ] === 3 && pos[ 1 ] === 1 && pos[ 2 ] === 2 &&
  197. pos[ 3 ] === 6 && pos[ 4 ] === 4 && pos[ 5 ] === 5, 'vertices were rotated properly' );
  198. } );
  199. QUnit.test( 'rotateX/Y/Z', ( assert ) => {
  200. const geometry = new BufferGeometry();
  201. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ) );
  202. const pos = geometry.attributes.position.array;
  203. geometry.rotateX( 180 * DegToRad );
  204. // object was rotated around x so all items should be flipped but the x ones
  205. assert.ok( pos[ 0 ] === 1 && pos[ 1 ] === - 2 && pos[ 2 ] === - 3 &&
  206. pos[ 3 ] === 4 && pos[ 4 ] === - 5 && pos[ 5 ] === - 6, 'vertices were rotated around x by 180 degrees' );
  207. geometry.rotateY( 180 * DegToRad );
  208. // vertices were rotated around y so all items should be flipped again but the y ones
  209. assert.ok( pos[ 0 ] === - 1 && pos[ 1 ] === - 2 && pos[ 2 ] === 3 &&
  210. pos[ 3 ] === - 4 && pos[ 4 ] === - 5 && pos[ 5 ] === 6, 'vertices were rotated around y by 180 degrees' );
  211. geometry.rotateZ( 180 * DegToRad );
  212. // vertices were rotated around z so all items should be flipped again but the z ones
  213. assert.ok( pos[ 0 ] === 1 && pos[ 1 ] === 2 && pos[ 2 ] === 3 &&
  214. pos[ 3 ] === 4 && pos[ 4 ] === 5 && pos[ 5 ] === 6, 'vertices were rotated around z by 180 degrees' );
  215. } );
  216. QUnit.test( 'translate', ( assert ) => {
  217. const geometry = new BufferGeometry();
  218. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ) );
  219. const pos = geometry.attributes.position.array;
  220. geometry.translate( 10, 20, 30 );
  221. assert.ok( pos[ 0 ] === 11 && pos[ 1 ] === 22 && pos[ 2 ] === 33 &&
  222. pos[ 3 ] === 14 && pos[ 4 ] === 25 && pos[ 5 ] === 36, 'vertices were translated' );
  223. } );
  224. QUnit.test( 'scale', ( assert ) => {
  225. const geometry = new BufferGeometry();
  226. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( [ - 1, - 1, - 1, 2, 2, 2 ] ), 3 ) );
  227. const pos = geometry.attributes.position.array;
  228. geometry.scale( 1, 2, 3 );
  229. assert.ok( pos[ 0 ] === - 1 && pos[ 1 ] === - 2 && pos[ 2 ] === - 3 &&
  230. pos[ 3 ] === 2 && pos[ 4 ] === 4 && pos[ 5 ] === 6, 'vertices were scaled' );
  231. } );
  232. QUnit.test( 'lookAt', ( assert ) => {
  233. const a = new BufferGeometry();
  234. const vertices = new Float32Array( [
  235. - 1.0, - 1.0, 1.0,
  236. 1.0, - 1.0, 1.0,
  237. 1.0, 1.0, 1.0,
  238. 1.0, 1.0, 1.0,
  239. - 1.0, 1.0, 1.0,
  240. - 1.0, - 1.0, 1.0
  241. ] );
  242. a.setAttribute( 'position', new BufferAttribute( vertices, 3 ) );
  243. const sqrt = Math.sqrt( 2 );
  244. const expected = new BufferAttribute( new Float32Array( [
  245. 1, 0, - sqrt,
  246. - 1, 0, - sqrt,
  247. - 1, sqrt, 0,
  248. - 1, sqrt, 0,
  249. 1, sqrt, 0,
  250. 1, 0, - sqrt
  251. ] ), 3 );
  252. a.lookAt( new Vector3( 0, 1, - 1 ) );
  253. assert.ok( bufferAttributeEquals( a.attributes.position, expected ), 'Rotation is correct' );
  254. } );
  255. QUnit.test( 'center', ( assert ) => {
  256. const geometry = new BufferGeometry();
  257. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( [
  258. - 1, - 1, - 1,
  259. 1, 1, 1,
  260. 4, 4, 4
  261. ] ), 3 ) );
  262. geometry.center();
  263. const pos = geometry.attributes.position.array;
  264. // the boundingBox should go from (-1, -1, -1) to (4, 4, 4) so it has a size of (5, 5, 5)
  265. // after centering it the vertices should be placed between (-2.5, -2.5, -2.5) and (2.5, 2.5, 2.5)
  266. assert.ok( pos[ 0 ] === - 2.5 && pos[ 1 ] === - 2.5 && pos[ 2 ] === - 2.5 &&
  267. pos[ 3 ] === - 0.5 && pos[ 4 ] === - 0.5 && pos[ 5 ] === - 0.5 &&
  268. pos[ 6 ] === 2.5 && pos[ 7 ] === 2.5 && pos[ 8 ] === 2.5, 'vertices were replaced by boundingBox dimensions' );
  269. } );
  270. QUnit.todo( 'setFromPoints', ( assert ) => {
  271. assert.ok( false, 'everything\'s gonna be alright' );
  272. } );
  273. QUnit.test( 'computeBoundingBox', ( assert ) => {
  274. let bb = getBBForVertices( [ - 1, - 2, - 3, 13, - 2, - 3.5, - 1, - 20, 0, - 4, 5, 6 ] );
  275. assert.ok( bb.min.x === - 4 && bb.min.y === - 20 && bb.min.z === - 3.5, 'min values are set correctly' );
  276. assert.ok( bb.max.x === 13 && bb.max.y === 5 && bb.max.z === 6, 'max values are set correctly' );
  277. bb = getBBForVertices( [ - 1, - 1, - 1 ] );
  278. assert.ok( bb.min.x === bb.max.x && bb.min.y === bb.max.y && bb.min.z === bb.max.z, 'since there is only one vertex, max and min are equal' );
  279. assert.ok( bb.min.x === - 1 && bb.min.y === - 1 && bb.min.z === - 1, 'since there is only one vertex, min and max are this vertex' );
  280. } );
  281. QUnit.test( 'computeBoundingSphere', ( assert ) => {
  282. let bs = getBSForVertices( [ - 10, 0, 0, 10, 0, 0 ] );
  283. assert.ok( bs.radius === 10, 'radius is equal to deltaMinMax / 2' );
  284. assert.ok( bs.center.x === 0 && bs.center.y === 0 && bs.center.y === 0, 'bounding sphere is at ( 0, 0, 0 )' );
  285. bs = getBSForVertices( [ - 5, 11, - 3, 5, - 11, 3 ] );
  286. const radius = new Vector3( 5, 11, 3 ).length();
  287. assert.ok( bs.radius === radius, 'radius is equal to directionLength' );
  288. assert.ok( bs.center.x === 0 && bs.center.y === 0 && bs.center.y === 0, 'bounding sphere is at ( 0, 0, 0 )' );
  289. } );
  290. const toHalfFloatArray = ( f32Array ) => {
  291. const f16Array = new Uint16Array( f32Array.length );
  292. for ( let i = 0, n = f32Array.length; i < n; ++ i ) {
  293. f16Array[ i ] = toHalfFloat( f32Array[ i ] );
  294. }
  295. return f16Array;
  296. };
  297. QUnit.test( 'computeBoundingBox - Float16', ( assert ) => {
  298. const vertices = [ - 1, - 2, - 3, 13, - 2, - 3.5, - 1, - 20, 0, - 4, 5, 6 ];
  299. const geometry = new BufferGeometry();
  300. geometry.setAttribute( 'position', new Float16BufferAttribute( toHalfFloatArray( vertices ), 3 ) );
  301. geometry.computeBoundingBox();
  302. let bb = geometry.boundingBox;
  303. assert.ok( bb.min.x === - 4 && bb.min.y === - 20 && bb.min.z === - 3.5, 'min values are set correctly' );
  304. assert.ok( bb.max.x === 13 && bb.max.y === 5 && bb.max.z === 6, 'max values are set correctly' );
  305. bb = getBBForVertices( [ - 1, - 1, - 1 ] );
  306. assert.ok( bb.min.x === bb.max.x && bb.min.y === bb.max.y && bb.min.z === bb.max.z, 'since there is only one vertex, max and min are equal' );
  307. assert.ok( bb.min.x === - 1 && bb.min.y === - 1 && bb.min.z === - 1, 'since there is only one vertex, min and max are this vertex' );
  308. } );
  309. QUnit.test( 'computeBoundingSphere - Float16', ( assert ) => {
  310. const vertices = [ - 10, 0, 0, 10, 0, 0 ];
  311. const geometry = new BufferGeometry();
  312. geometry.setAttribute( 'position', new Float16BufferAttribute( toHalfFloatArray( vertices ), 3 ) );
  313. geometry.computeBoundingSphere();
  314. let bs = geometry.boundingSphere;
  315. assert.ok( bs.radius === 10, 'radius is equal to deltaMinMax / 2' );
  316. assert.ok( bs.center.x === 0 && bs.center.y === 0 && bs.center.y === 0, 'bounding sphere is at ( 0, 0, 0 )' );
  317. bs = getBSForVertices( [ - 5, 11, - 3, 5, - 11, 3 ] );
  318. const radius = new Vector3( 5, 11, 3 ).length();
  319. assert.ok( bs.radius === radius, 'radius is equal to directionLength' );
  320. assert.ok( bs.center.x === 0 && bs.center.y === 0 && bs.center.y === 0, 'bounding sphere is at ( 0, 0, 0 )' );
  321. } );
  322. QUnit.todo( 'computeTangents', ( assert ) => {
  323. assert.ok( false, 'everything\'s gonna be alright' );
  324. } );
  325. QUnit.test( 'computeVertexNormals', ( assert ) => {
  326. // get normals for a counter clockwise created triangle
  327. let normals = getNormalsForVertices( [ - 1, 0, 0, 1, 0, 0, 0, 1, 0 ], assert );
  328. assert.ok( normals[ 0 ] === 0 && normals[ 1 ] === 0 && normals[ 2 ] === 1,
  329. 'first normal is pointing to screen since the triangle was created counter clockwise' );
  330. assert.ok( normals[ 3 ] === 0 && normals[ 4 ] === 0 && normals[ 5 ] === 1,
  331. 'second normal is pointing to screen since the triangle was created counter clockwise' );
  332. assert.ok( normals[ 6 ] === 0 && normals[ 7 ] === 0 && normals[ 8 ] === 1,
  333. 'third normal is pointing to screen since the triangle was created counter clockwise' );
  334. // get normals for a clockwise created triangle
  335. normals = getNormalsForVertices( [ 1, 0, 0, - 1, 0, 0, 0, 1, 0 ], assert );
  336. assert.ok( normals[ 0 ] === 0 && normals[ 1 ] === 0 && normals[ 2 ] === - 1,
  337. 'first normal is pointing to screen since the triangle was created clockwise' );
  338. assert.ok( normals[ 3 ] === 0 && normals[ 4 ] === 0 && normals[ 5 ] === - 1,
  339. 'second normal is pointing to screen since the triangle was created clockwise' );
  340. assert.ok( normals[ 6 ] === 0 && normals[ 7 ] === 0 && normals[ 8 ] === - 1,
  341. 'third normal is pointing to screen since the triangle was created clockwise' );
  342. normals = getNormalsForVertices( [ 0, 0, 1, 0, 0, - 1, 1, 1, 0 ], assert );
  343. // the triangle is rotated by 45 degrees to the right so the normals of the three vertices
  344. // should point to (1, -1, 0).normalized(). The simplest solution is to check against a normalized
  345. // vector (1, -1, 0) but you will get calculation errors because of floating calculations so another
  346. // valid technique is to create a vector which stands in 90 degrees to the normals and calculate the
  347. // dot product which is the cos of the angle between them. This should be < floating calculation error
  348. // which can be taken from Number.EPSILON
  349. const direction = new Vector3( 1, 1, 0 ).normalize(); // a vector which should have 90 degrees difference to normals
  350. const difference = direction.dot( new Vector3( normals[ 0 ], normals[ 1 ], normals[ 2 ] ) );
  351. assert.ok( difference < Number.EPSILON, 'normal is equal to reference vector' );
  352. // get normals for a line should be NAN because you need min a triangle to calculate normals
  353. normals = getNormalsForVertices( [ 1, 0, 0, - 1, 0, 0 ], assert );
  354. for ( let i = 0; i < normals.length; i ++ ) {
  355. assert.ok( ! normals[ i ], 'normals can\'t be calculated which is good' );
  356. }
  357. } );
  358. QUnit.test( 'computeVertexNormals (indexed)', ( assert ) => {
  359. const sqrt = 0.5 * Math.sqrt( 2 );
  360. const normal = new BufferAttribute( new Float32Array( [
  361. - 1, 0, 0, - 1, 0, 0, - 1, 0, 0,
  362. sqrt, sqrt, 0, sqrt, sqrt, 0, sqrt, sqrt, 0
  363. ] ), 3 );
  364. const position = new BufferAttribute( new Float32Array( [
  365. 0.5, 0.5, 0.5, 0.5, 0.5, - 0.5, 0.5, - 0.5, 0.5,
  366. 0.5, - 0.5, - 0.5, - 0.5, 0.5, - 0.5, - 0.5, 0.5, 0.5
  367. ] ), 3 );
  368. // the index buffer defines the same two triangles but in counter-clockwise order,
  369. // which should result in flipped normals.
  370. const flippedNormals = normal.clone().applyMatrix4( new Matrix4().makeScale( - 1, - 1, - 1 ) );
  371. const index = new BufferAttribute( new Uint16Array( [
  372. 0, 2, 1, 3, 5, 4
  373. ] ), 1 );
  374. let a = new BufferGeometry();
  375. a.setAttribute( 'position', position );
  376. a.computeVertexNormals();
  377. assert.ok(
  378. bufferAttributeEquals( normal, a.getAttribute( 'normal' ) ),
  379. 'Regular geometry: first computed normals are correct'
  380. );
  381. // a second time to see if the existing normals get properly deleted
  382. a.computeVertexNormals();
  383. assert.ok(
  384. bufferAttributeEquals( normal, a.getAttribute( 'normal' ) ),
  385. 'Regular geometry: second computed normals are correct'
  386. );
  387. // indexed geometry
  388. a = new BufferGeometry();
  389. a.setAttribute( 'position', position );
  390. a.setIndex( index );
  391. a.computeVertexNormals();
  392. assert.ok( bufferAttributeEquals( flippedNormals, a.getAttribute( 'normal' ) ), 'Indexed geometry: computed normals are correct' );
  393. } );
  394. QUnit.todo( 'normalizeNormals', ( assert ) => {
  395. assert.ok( false, 'everything\'s gonna be alright' );
  396. } );
  397. QUnit.test( 'toNonIndexed', ( assert ) => {
  398. const geometry = new BufferGeometry();
  399. const vertices = new Float32Array( [
  400. 0.5, 0.5, 0.5, 0.5, 0.5, - 0.5, 0.5, - 0.5, 0.5, 0.5, - 0.5, - 0.5
  401. ] );
  402. const index = new BufferAttribute( new Uint16Array( [ 0, 2, 1, 2, 3, 1 ] ) );
  403. const expected = new Float32Array( [
  404. 0.5, 0.5, 0.5, 0.5, - 0.5, 0.5, 0.5, 0.5, - 0.5,
  405. 0.5, - 0.5, 0.5, 0.5, - 0.5, - 0.5, 0.5, 0.5, - 0.5
  406. ] );
  407. geometry.setAttribute( 'position', new BufferAttribute( vertices, 3 ) );
  408. geometry.setIndex( index );
  409. const nonIndexed = geometry.toNonIndexed();
  410. assert.deepEqual( nonIndexed.getAttribute( 'position' ).array, expected, 'Expected vertices' );
  411. } );
  412. QUnit.test( 'toJSON', ( assert ) => {
  413. const index = new BufferAttribute( new Uint16Array( [ 0, 1, 2, 3 ] ), 1 );
  414. const attribute1 = new BufferAttribute( new Uint16Array( [ 1, 3, 5, 7 ] ), 1 );
  415. attribute1.name = 'attribute1';
  416. const a = new BufferGeometry();
  417. a.name = 'JSONQUnit.test';
  418. // a.parameters = { "placeholder": 0 };
  419. a.setAttribute( 'attribute1', attribute1 );
  420. a.setIndex( index );
  421. a.addGroup( 0, 1, 2 );
  422. a.boundingSphere = new Sphere( new Vector3( x, y, z ), 0.5 );
  423. let j = a.toJSON();
  424. const gold = {
  425. 'metadata': {
  426. 'version': 4.6,
  427. 'type': 'BufferGeometry',
  428. 'generator': 'BufferGeometry.toJSON'
  429. },
  430. 'uuid': a.uuid,
  431. 'type': 'BufferGeometry',
  432. 'name': 'JSONQUnit.test',
  433. 'data': {
  434. 'attributes': {
  435. 'attribute1': {
  436. 'itemSize': 1,
  437. 'type': 'Uint16Array',
  438. 'array': [ 1, 3, 5, 7 ],
  439. 'normalized': false,
  440. 'name': 'attribute1'
  441. }
  442. },
  443. 'index': {
  444. 'type': 'Uint16Array',
  445. 'array': [ 0, 1, 2, 3 ]
  446. },
  447. 'groups': [
  448. {
  449. 'start': 0,
  450. 'count': 1,
  451. 'materialIndex': 2
  452. }
  453. ],
  454. 'boundingSphere': {
  455. 'center': [ 2, 3, 4 ],
  456. 'radius': 0.5
  457. }
  458. }
  459. };
  460. assert.deepEqual( j, gold, 'Generated JSON is as expected' );
  461. // add morphAttributes
  462. a.morphAttributes.attribute1 = [];
  463. a.morphAttributes.attribute1.push( attribute1.clone() );
  464. j = a.toJSON();
  465. gold.data.morphAttributes = {
  466. 'attribute1': [ {
  467. 'itemSize': 1,
  468. 'type': 'Uint16Array',
  469. 'array': [ 1, 3, 5, 7 ],
  470. 'normalized': false,
  471. 'name': 'attribute1'
  472. } ]
  473. };
  474. gold.data.morphTargetsRelative = false;
  475. assert.deepEqual( j, gold, 'Generated JSON with morphAttributes is as expected' );
  476. } );
  477. QUnit.test( 'clone', ( assert ) => {
  478. const a = new BufferGeometry();
  479. a.setAttribute( 'attribute1', new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ) );
  480. a.setAttribute( 'attribute2', new BufferAttribute( new Float32Array( [ 0, 1, 3, 5, 6 ] ), 1 ) );
  481. a.addGroup( 0, 1, 2 );
  482. a.computeBoundingBox();
  483. a.computeBoundingSphere();
  484. a.setDrawRange( 0, 1 );
  485. const b = a.clone();
  486. assert.notEqual( a, b, 'A new object was created' );
  487. assert.notEqual( a.id, b.id, 'New object has a different GUID' );
  488. assert.strictEqual(
  489. Object.keys( a.attributes ).count, Object.keys( b.attributes ).count,
  490. 'Both objects have the same amount of attributes'
  491. );
  492. assert.ok(
  493. bufferAttributeEquals( a.getAttribute( 'attribute1' ), b.getAttribute( 'attribute1' ) ),
  494. 'First attributes buffer is identical'
  495. );
  496. assert.ok(
  497. bufferAttributeEquals( a.getAttribute( 'attribute2' ), b.getAttribute( 'attribute2' ) ),
  498. 'Second attributes buffer is identical'
  499. );
  500. assert.deepEqual( a.groups, b.groups, 'Groups are identical' );
  501. assert.ok( a.boundingBox.equals( b.boundingBox ), 'BoundingBoxes are equal' );
  502. assert.ok( a.boundingSphere.equals( b.boundingSphere ), 'BoundingSpheres are equal' );
  503. assert.strictEqual( a.drawRange.start, b.drawRange.start, 'DrawRange start is identical' );
  504. assert.strictEqual( a.drawRange.count, b.drawRange.count, 'DrawRange count is identical' );
  505. } );
  506. QUnit.test( 'copy', ( assert ) => {
  507. const geometry = new BufferGeometry();
  508. geometry.setAttribute( 'attrName', new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ) );
  509. geometry.setAttribute( 'attrName2', new BufferAttribute( new Float32Array( [ 0, 1, 3, 5, 6 ] ), 1 ) );
  510. const copy = new BufferGeometry().copy( geometry );
  511. assert.ok( copy !== geometry && geometry.id !== copy.id, 'new object was created' );
  512. Object.keys( geometry.attributes ).forEach( function ( key ) {
  513. const attribute = geometry.attributes[ key ];
  514. assert.ok( attribute !== undefined, 'all attributes where copied' );
  515. for ( let i = 0; i < attribute.array.length; i ++ ) {
  516. assert.ok( attribute.array[ i ] === copy.attributes[ key ].array[ i ], 'values of the attribute are equal' );
  517. }
  518. } );
  519. } );
  520. QUnit.test( 'dispose', ( assert ) => {
  521. assert.expect( 0 );
  522. const object = new BufferGeometry();
  523. object.dispose();
  524. } );
  525. } );
  526. } );
粤ICP备19079148号