BufferGeometry.tests.js 20 KB

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