BufferGeometry.tests.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /**
  2. * @author simonThiele / https://github.com/simonThiele
  3. * @author TristanVALCKE / https://github.com/Itee
  4. */
  5. /* global QUnit */
  6. import { BufferGeometry } from '../../../../src/core/BufferGeometry';
  7. import { JSONLoader } from '../../../../src/loaders/JSONLoader';
  8. import { DirectGeometry } from '../../../../src/core/DirectGeometry';
  9. import {
  10. BufferAttribute,
  11. Uint16BufferAttribute,
  12. Uint32BufferAttribute,
  13. Float32BufferAttribute
  14. } from '../../../../src/core/BufferAttribute';
  15. import { Vector3 } from '../../../../src/math/Vector3';
  16. import { Matrix4 } from '../../../../src/math/Matrix4';
  17. import { Sphere } from '../../../../src/math/Sphere';
  18. import { Vector2 } from '../../../../src/math/Vector2';
  19. import { Geometry } from '../../../../src/core/Geometry';
  20. import { Face3 } from '../../../../src/core/Face3';
  21. import { Mesh } from '../../../../src/objects/Mesh';
  22. import { Color } from '../../../../src/math/Color';
  23. import { Line } from '../../../../src/objects/Line.js';
  24. import {
  25. x,
  26. y,
  27. z
  28. } from '../math/Constants.tests';
  29. var DegToRad = Math.PI / 180;
  30. function bufferAttributeEquals( a, b, tolerance ) {
  31. tolerance = tolerance || 0.0001;
  32. if ( a.count !== b.count || a.itemSize !== b.itemSize ) {
  33. return false;
  34. }
  35. for ( var i = 0, il = a.count * a.itemSize; i < il; i ++ ) {
  36. var delta = a[ i ] - b[ i ];
  37. if ( delta > tolerance ) {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. function getBBForVertices( vertices ) {
  44. var geometry = new BufferGeometry();
  45. geometry.addAttribute( "position", new BufferAttribute( new Float32Array( vertices ), 3 ) );
  46. geometry.computeBoundingBox();
  47. return geometry.boundingBox;
  48. }
  49. function getBSForVertices( vertices ) {
  50. var geometry = new BufferGeometry();
  51. geometry.addAttribute( "position", new BufferAttribute( new Float32Array( vertices ), 3 ) );
  52. geometry.computeBoundingSphere();
  53. return geometry.boundingSphere;
  54. }
  55. function getNormalsForVertices( vertices, assert ) {
  56. var geometry = new BufferGeometry();
  57. geometry.addAttribute( "position", new BufferAttribute( new Float32Array( vertices ), 3 ) );
  58. geometry.computeVertexNormals();
  59. assert.ok( geometry.attributes.normal !== undefined, "normal attribute was created" );
  60. return geometry.attributes.normal.array;
  61. }
  62. function comparePositions( pos, v ) {
  63. return (
  64. pos[ 0 ] === v[ 0 ].x && pos[ 1 ] === v[ 0 ].y && pos[ 2 ] === v[ 0 ].z &&
  65. pos[ 3 ] === v[ 1 ].x && pos[ 4 ] === v[ 1 ].y && pos[ 5 ] === v[ 1 ].z &&
  66. pos[ 6 ] === v[ 2 ].x && pos[ 7 ] === v[ 2 ].y && pos[ 8 ] === v[ 2 ].z
  67. );
  68. }
  69. function compareColors( col, c ) {
  70. return (
  71. col[ 0 ] === c[ 0 ].r && col[ 1 ] === c[ 0 ].g && col[ 2 ] === c[ 0 ].b &&
  72. col[ 3 ] === c[ 1 ].r && col[ 4 ] === c[ 1 ].g && col[ 5 ] === c[ 1 ].b &&
  73. col[ 6 ] === c[ 2 ].r && col[ 7 ] === c[ 2 ].g && col[ 8 ] === c[ 2 ].b
  74. );
  75. }
  76. function compareUvs( uvs, u ) {
  77. return (
  78. uvs[ 0 ] === u[ 0 ].x && uvs[ 1 ] === u[ 0 ].y &&
  79. uvs[ 2 ] === u[ 1 ].x && uvs[ 3 ] === u[ 1 ].y &&
  80. uvs[ 4 ] === u[ 2 ].x && uvs[ 5 ] === u[ 2 ].y
  81. );
  82. }
  83. export default QUnit.module( 'Core', () => {
  84. QUnit.module( 'BufferGeometry', () => {
  85. // INHERITANCE
  86. QUnit.todo( "Extending", ( assert ) => {
  87. assert.ok( false, "everything's gonna be alright" );
  88. } );
  89. // INSTANCING
  90. QUnit.todo( "Instancing", ( assert ) => {
  91. assert.ok( false, "everything's gonna be alright" );
  92. } );
  93. // PUBLIC STUFF
  94. QUnit.todo( "isBufferGeometry", ( assert ) => {
  95. assert.ok( false, "everything's gonna be alright" );
  96. } );
  97. QUnit.test( "setIndex/getIndex", ( assert ) => {
  98. var a = new BufferGeometry();
  99. var uint16 = [ 1, 2, 3 ];
  100. var uint32 = [ 65535, 65536, 65537 ];
  101. var str = "foo";
  102. a.setIndex( uint16 );
  103. assert.ok( a.getIndex() instanceof Uint16BufferAttribute, "Index has the right type" );
  104. assert.deepEqual( a.getIndex().array, new Uint16Array( uint16 ), "Small index gets stored correctly" );
  105. a.setIndex( uint32 );
  106. assert.ok( a.getIndex() instanceof Uint32BufferAttribute, "Index has the right type" );
  107. assert.deepEqual( a.getIndex().array, new Uint32Array( uint32 ), "Large index gets stored correctly" );
  108. a.setIndex( str );
  109. assert.strictEqual( a.getIndex(), str, "Weird index gets stored correctly" );
  110. } );
  111. QUnit.todo( "getAttribute", ( assert ) => {
  112. assert.ok( false, "everything's gonna be alright" );
  113. } );
  114. QUnit.test( "add / delete Attribute", ( assert ) => {
  115. var geometry = new BufferGeometry();
  116. var attributeName = "position";
  117. assert.ok( geometry.attributes[ attributeName ] === undefined, 'no attribute defined' );
  118. geometry.addAttribute( attributeName, new BufferAttribute( new Float32Array( [ 1, 2, 3 ], 1 ) ) );
  119. assert.ok( geometry.attributes[ attributeName ] !== undefined, 'attribute is defined' );
  120. geometry.removeAttribute( attributeName );
  121. assert.ok( geometry.attributes[ attributeName ] === undefined, 'no attribute defined' );
  122. } );
  123. QUnit.test( "addGroup", ( assert ) => {
  124. var a = new BufferGeometry();
  125. var expected = [
  126. {
  127. start: 0,
  128. count: 1,
  129. materialIndex: 0
  130. },
  131. {
  132. start: 1,
  133. count: 2,
  134. materialIndex: 2
  135. }
  136. ];
  137. a.addGroup( 0, 1, 0 );
  138. a.addGroup( 1, 2, 2 );
  139. assert.deepEqual( a.groups, expected, "Check groups were stored correctly and in order" );
  140. a.clearGroups();
  141. assert.strictEqual( a.groups.length, 0, "Check groups were deleted correctly" );
  142. } );
  143. QUnit.todo( "clearGroups", ( assert ) => {
  144. assert.ok( false, "everything's gonna be alright" );
  145. } );
  146. QUnit.test( "setDrawRange", ( assert ) => {
  147. var a = new BufferGeometry();
  148. a.setDrawRange( 1.0, 7 );
  149. assert.deepEqual( a.drawRange, {
  150. start: 1,
  151. count: 7
  152. }, "Check draw range was stored correctly" );
  153. } );
  154. QUnit.test( "applyMatrix", ( assert ) => {
  155. var geometry = new BufferGeometry();
  156. geometry.addAttribute( "position", new BufferAttribute( new Float32Array( 6 ), 3 ) );
  157. var matrix = new Matrix4().set(
  158. 1, 0, 0, 1.5,
  159. 0, 1, 0, - 2,
  160. 0, 0, 1, 3,
  161. 0, 0, 0, 1
  162. );
  163. geometry.applyMatrix( matrix );
  164. var position = geometry.attributes.position.array;
  165. var m = matrix.elements;
  166. assert.ok( position[ 0 ] === m[ 12 ] && position[ 1 ] === m[ 13 ] && position[ 2 ] === m[ 14 ], "position was extracted from matrix" );
  167. assert.ok( position[ 3 ] === m[ 12 ] && position[ 4 ] === m[ 13 ] && position[ 5 ] === m[ 14 ], "position was extracted from matrix twice" );
  168. assert.ok( geometry.attributes.position.version === 1, "version was increased during update" );
  169. } );
  170. QUnit.test( "rotateX/Y/Z", ( assert ) => {
  171. var geometry = new BufferGeometry();
  172. geometry.addAttribute( "position", new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ) );
  173. var pos = geometry.attributes.position.array;
  174. geometry.rotateX( 180 * DegToRad );
  175. // object was rotated around x so all items should be flipped but the x ones
  176. assert.ok( pos[ 0 ] === 1 && pos[ 1 ] === - 2 && pos[ 2 ] === - 3 &&
  177. pos[ 3 ] === 4 && pos[ 4 ] === - 5 && pos[ 5 ] === - 6, "vertices were rotated around x by 180 degrees" );
  178. geometry.rotateY( 180 * DegToRad );
  179. // vertices were rotated around y so all items should be flipped again but the y ones
  180. assert.ok( pos[ 0 ] === - 1 && pos[ 1 ] === - 2 && pos[ 2 ] === 3 &&
  181. pos[ 3 ] === - 4 && pos[ 4 ] === - 5 && pos[ 5 ] === 6, "vertices were rotated around y by 180 degrees" );
  182. geometry.rotateZ( 180 * DegToRad );
  183. // vertices were rotated around z so all items should be flipped again but the z ones
  184. assert.ok( pos[ 0 ] === 1 && pos[ 1 ] === 2 && pos[ 2 ] === 3 &&
  185. pos[ 3 ] === 4 && pos[ 4 ] === 5 && pos[ 5 ] === 6, "vertices were rotated around z by 180 degrees" );
  186. } );
  187. QUnit.test( "translate", ( assert ) => {
  188. var geometry = new BufferGeometry();
  189. geometry.addAttribute( "position", new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ) );
  190. var pos = geometry.attributes.position.array;
  191. geometry.translate( 10, 20, 30 );
  192. assert.ok( pos[ 0 ] === 11 && pos[ 1 ] === 22 && pos[ 2 ] === 33 &&
  193. pos[ 3 ] === 14 && pos[ 4 ] === 25 && pos[ 5 ] === 36, "vertices were translated" );
  194. } );
  195. QUnit.test( "scale", ( assert ) => {
  196. var geometry = new BufferGeometry();
  197. geometry.addAttribute( "position", new BufferAttribute( new Float32Array( [ - 1, - 1, - 1, 2, 2, 2 ] ), 3 ) );
  198. var pos = geometry.attributes.position.array;
  199. geometry.scale( 1, 2, 3 );
  200. assert.ok( pos[ 0 ] === - 1 && pos[ 1 ] === - 2 && pos[ 2 ] === - 3 &&
  201. pos[ 3 ] === 2 && pos[ 4 ] === 4 && pos[ 5 ] === 6, "vertices were scaled" );
  202. } );
  203. QUnit.test( "lookAt", ( assert ) => {
  204. var a = new BufferGeometry();
  205. var vertices = new Float32Array( [
  206. - 1.0, - 1.0, 1.0,
  207. 1.0, - 1.0, 1.0,
  208. 1.0, 1.0, 1.0,
  209. 1.0, 1.0, 1.0,
  210. - 1.0, 1.0, 1.0,
  211. - 1.0, - 1.0, 1.0
  212. ] );
  213. a.addAttribute( 'position', new BufferAttribute( vertices, 3 ) );
  214. var sqrt = Math.sqrt( 2 );
  215. var expected = new Float32Array( [
  216. 1, 0, - sqrt,
  217. - 1, 0, - sqrt,
  218. - 1, sqrt, 0,
  219. - 1, sqrt, 0,
  220. 1, sqrt, 0,
  221. 1, 0, - sqrt
  222. ] );
  223. a.lookAt( new Vector3( 0, 1, - 1 ) );
  224. assert.ok( bufferAttributeEquals( a.attributes.position.array, expected ), "Rotation is correct" );
  225. } );
  226. QUnit.test( "center", ( assert ) => {
  227. var geometry = new BufferGeometry();
  228. geometry.addAttribute( "position", new BufferAttribute( new Float32Array( [
  229. - 1, - 1, - 1,
  230. 1, 1, 1,
  231. 4, 4, 4
  232. ] ), 3 ) );
  233. geometry.center();
  234. var pos = geometry.attributes.position.array;
  235. // the boundingBox should go from (-1, -1, -1) to (4, 4, 4) so it has a size of (5, 5, 5)
  236. // after centering it the vertices should be placed between (-2.5, -2.5, -2.5) and (2.5, 2.5, 2.5)
  237. assert.ok( pos[ 0 ] === - 2.5 && pos[ 1 ] === - 2.5 && pos[ 2 ] === - 2.5 &&
  238. pos[ 3 ] === - 0.5 && pos[ 4 ] === - 0.5 && pos[ 5 ] === - 0.5 &&
  239. pos[ 6 ] === 2.5 && pos[ 7 ] === 2.5 && pos[ 8 ] === 2.5, "vertices were replaced by boundingBox dimensions" );
  240. } );
  241. QUnit.test( "setFromObject", ( assert ) => {
  242. var lineGeo = new Geometry();
  243. lineGeo.vertices.push(
  244. new Vector3( - 10, 0, 0 ),
  245. new Vector3( 0, 10, 0 ),
  246. new Vector3( 10, 0, 0 )
  247. );
  248. lineGeo.colors.push(
  249. new Color( 1, 0, 0 ),
  250. new Color( 0, 1, 0 ),
  251. new Color( 0, 0, 1 )
  252. );
  253. var line = new Line( lineGeo, null );
  254. var geometry = new BufferGeometry().setFromObject( line );
  255. var pos = geometry.attributes.position.array;
  256. var col = geometry.attributes.color.array;
  257. var v = lineGeo.vertices;
  258. var c = lineGeo.colors;
  259. assert.ok(
  260. // position exists
  261. pos !== undefined &&
  262. // vertex arrays have the same size
  263. v.length * 3 === pos.length &&
  264. // there are three complete vertices (each vertex contains three values)
  265. geometry.attributes.position.count === 3 &&
  266. // check if both arrays contains the same data
  267. pos[ 0 ] === v[ 0 ].x && pos[ 1 ] === v[ 0 ].y && pos[ 2 ] === v[ 0 ].z &&
  268. pos[ 3 ] === v[ 1 ].x && pos[ 4 ] === v[ 1 ].y && pos[ 5 ] === v[ 1 ].z &&
  269. pos[ 6 ] === v[ 2 ].x && pos[ 7 ] === v[ 2 ].y && pos[ 8 ] === v[ 2 ].z
  270. , "positions are equal" );
  271. assert.ok(
  272. // color exists
  273. col !== undefined &&
  274. // color arrays have the same size
  275. c.length * 3 === col.length &&
  276. // there are three complete colors (each color contains three values)
  277. geometry.attributes.color.count === 3 &&
  278. // check if both arrays contains the same data
  279. col[ 0 ] === c[ 0 ].r && col[ 1 ] === c[ 0 ].g && col[ 2 ] === c[ 0 ].b &&
  280. col[ 3 ] === c[ 1 ].r && col[ 4 ] === c[ 1 ].g && col[ 5 ] === c[ 1 ].b &&
  281. col[ 6 ] === c[ 2 ].r && col[ 7 ] === c[ 2 ].g && col[ 8 ] === c[ 2 ].b
  282. , "colors are equal" );
  283. } );
  284. QUnit.test( "setFromObject (more)", ( assert ) => {
  285. var lineGeo = new Geometry();
  286. lineGeo.vertices.push(
  287. new Vector3( - 10, 0, 0 ),
  288. new Vector3( 0, 10, 0 ),
  289. new Vector3( 10, 0, 0 )
  290. );
  291. lineGeo.colors.push(
  292. new Color( 1, 0, 0 ),
  293. new Color( 0, 1, 0 ),
  294. new Color( 0, 0, 1 )
  295. );
  296. lineGeo.computeBoundingBox();
  297. lineGeo.computeBoundingSphere();
  298. var line = new Line( lineGeo );
  299. var geometry = new BufferGeometry().setFromObject( line );
  300. assert.ok( geometry.boundingBox.equals( lineGeo.boundingBox ), "BoundingBox was set correctly" );
  301. assert.ok( geometry.boundingSphere.equals( lineGeo.boundingSphere ), "BoundingSphere was set correctly" );
  302. var pos = geometry.attributes.position.array;
  303. var col = geometry.attributes.color.array;
  304. var v = lineGeo.vertices;
  305. var c = lineGeo.colors;
  306. // adapted from setFromObject QUnit.test (way up)
  307. assert.notStrictEqual( pos, undefined, "Position attribute exists" );
  308. assert.strictEqual( v.length * 3, pos.length, "Vertex arrays have the same size" );
  309. assert.strictEqual( geometry.attributes.position.count, 3, "Correct number of vertices" );
  310. assert.ok( comparePositions( pos, v ), "Positions are identical" );
  311. assert.notStrictEqual( col, undefined, "Color attribute exists" );
  312. assert.strictEqual( c.length * 3, col.length, "Color arrays have the same size" );
  313. assert.strictEqual( geometry.attributes.color.count, 3, "Correct number of colors" );
  314. assert.ok( compareColors( col, c ), "Colors are identical" );
  315. // setFromObject with a Mesh as object
  316. lineGeo.faces.push( new Face3( 0, 1, 2 ) );
  317. var lineMesh = new Mesh( lineGeo );
  318. var geometry = new BufferGeometry().setFromObject( lineMesh );
  319. // no colors
  320. var pos = geometry.attributes.position.array;
  321. var v = lineGeo.vertices;
  322. assert.notStrictEqual( pos, undefined, "Mesh: position attribute exists" );
  323. assert.strictEqual( v.length * 3, pos.length, "Mesh: vertex arrays have the same size" );
  324. assert.strictEqual( geometry.attributes.position.count, 3, "Mesh: correct number of vertices" );
  325. assert.ok( comparePositions( pos, v ), "Mesh: positions are identical" );
  326. } );
  327. QUnit.test( "updateFromObject", ( assert ) => {
  328. var geo = new Geometry();
  329. geo.vertices.push(
  330. new Vector3( - 10, 0, 0 ),
  331. new Vector3( 0, 10, 0 ),
  332. new Vector3( 10, 0, 0 )
  333. );
  334. geo.faces.push( new Face3( 0, 1, 2 ) );
  335. geo.faces[ 0 ].vertexColors.push(
  336. new Color( 1, 0, 0 ),
  337. new Color( 0, 1, 0 ),
  338. new Color( 0, 0, 1 )
  339. );
  340. geo.faceVertexUvs[ 0 ] = [
  341. [
  342. new Vector2( 0, 0 ),
  343. new Vector2( 1, 0 ),
  344. new Vector2( 1, 1 )
  345. ]
  346. ];
  347. geo.computeFaceNormals();
  348. geo.computeVertexNormals();
  349. geo.verticesNeedUpdate = true;
  350. geo.normalsNeedUpdate = true;
  351. geo.colorsNeedUpdate = true;
  352. geo.uvsNeedUpdate = true;
  353. geo.groupsNeedUpdate = true;
  354. var mesh = new Mesh( geo );
  355. var geometry = new BufferGeometry();
  356. geometry.updateFromObject( mesh ); // first call to create the underlying structure (DirectGeometry)
  357. geometry.updateFromObject( mesh ); // second time to actually go thru the motions and update
  358. var pos = geometry.attributes.position.array;
  359. var col = geometry.attributes.color.array;
  360. var norm = geometry.attributes.normal.array;
  361. var uvs = geometry.attributes.uv.array;
  362. var v = geo.vertices;
  363. var c = geo.faces[ 0 ].vertexColors;
  364. var n = geo.faces[ 0 ].vertexNormals;
  365. var u = geo.faceVertexUvs[ 0 ][ 0 ];
  366. assert.notStrictEqual( pos, undefined, "Position attribute exists" );
  367. assert.strictEqual( v.length * 3, pos.length, "Both arrays have the same size" );
  368. assert.strictEqual( geometry.attributes.position.count, v.length, "Correct number of vertices" );
  369. assert.ok( comparePositions( pos, v ), "Positions are identical" );
  370. assert.notStrictEqual( col, undefined, "Color attribute exists" );
  371. assert.strictEqual( c.length * 3, col.length, "Both arrays have the same size" );
  372. assert.strictEqual( geometry.attributes.color.count, c.length, "Correct number of colors" );
  373. assert.ok( compareColors( col, c ), "Colors are identical" );
  374. assert.notStrictEqual( norm, undefined, "Normal attribute exists" );
  375. assert.strictEqual( n.length * 3, norm.length, "Both arrays have the same size" );
  376. assert.strictEqual( geometry.attributes.normal.count, n.length, "Correct number of normals" );
  377. assert.ok( comparePositions( norm, n ), "Normals are identical" );
  378. assert.notStrictEqual( uvs, undefined, "UV attribute exists" );
  379. assert.strictEqual( u.length * 2, uvs.length, "Both arrays have the same size" );
  380. assert.strictEqual( geometry.attributes.uv.count, u.length, "Correct number of UV coordinates" );
  381. assert.ok( compareUvs( uvs, u ), "UVs are identical" );
  382. } );
  383. QUnit.test( "fromGeometry/fromDirectGeometry", ( assert ) => {
  384. if ( typeof XMLHttpRequest === 'undefined' ) {
  385. assert.expect( 0 );
  386. return;
  387. }
  388. assert.timeout( 1000 );
  389. var a = new BufferGeometry();
  390. // BoxGeometry is a bit too simple but works fine in a pinch
  391. // var b = new BoxGeometry( 1, 1, 1 );
  392. // b.mergeVertices();
  393. // b.computeVertexNormals();
  394. // b.computeBoundingBox();
  395. // b.computeBoundingSphere();
  396. var asyncDone = assert.async(); // tell QUnit we're done with asserts
  397. var loader = new JSONLoader();
  398. loader.load( "../../examples/models/skinned/simple/simple.js", function ( modelGeometry ) {
  399. a.fromGeometry( modelGeometry );
  400. var attr;
  401. var geometry = new DirectGeometry().fromGeometry( modelGeometry );
  402. var positions = new Float32Array( geometry.vertices.length * 3 );
  403. attr = new BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices );
  404. assert.ok( bufferAttributeEquals( a.attributes.position, attr ), "Vertices are identical" );
  405. if ( geometry.normals.length > 0 ) {
  406. var normals = new Float32Array( geometry.normals.length * 3 );
  407. attr = new BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals );
  408. assert.ok( bufferAttributeEquals( a.attributes.normal, attr ), "Normals are identical" );
  409. }
  410. if ( geometry.colors.length > 0 ) {
  411. var colors = new Float32Array( geometry.colors.length * 3 );
  412. attr = new BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors );
  413. assert.ok( bufferAttributeEquals( a.attributes.color, attr ), "Colors are identical" );
  414. }
  415. if ( geometry.uvs.length > 0 ) {
  416. var uvs = new Float32Array( geometry.uvs.length * 2 );
  417. attr = new BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs );
  418. assert.ok( bufferAttributeEquals( a.attributes.uv, attr ), "UVs are identical" );
  419. }
  420. if ( geometry.uvs2.length > 0 ) {
  421. var uvs2 = new Float32Array( geometry.uvs2.length * 2 );
  422. attr = new BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 );
  423. assert.ok( bufferAttributeEquals( a.attributes.uv2, attr ), "UV2s are identical" );
  424. }
  425. // groups
  426. assert.deepEqual( a.groups, geometry.groups, "Groups are identical" );
  427. // morphs
  428. if ( geometry.morphTargets !== undefined ) {
  429. for ( var name in geometry.morphTargets ) {
  430. var morphTargets = geometry.morphTargets[ name ];
  431. for ( var i = 0, l = morphTargets.length; i < l; i ++ ) {
  432. var morphTarget = morphTargets[ i ];
  433. attr = new Float32BufferAttribute( morphTarget.length * 3, 3 );
  434. attr.copyVector3sArray( morphTarget );
  435. assert.ok(
  436. bufferAttributeEquals( a.morphAttributes[ name ][ i ], attr ),
  437. "MorphTargets #" + i + " are identical"
  438. );
  439. }
  440. }
  441. }
  442. // skinning
  443. if ( geometry.skinIndices.length > 0 ) {
  444. attr = new Float32BufferAttribute( geometry.skinIndices.length * 4, 4 );
  445. attr.copyVector4sArray( geometry.skinIndices );
  446. assert.ok( bufferAttributeEquals( a.attributes.skinIndex, attr ), "SkinIndices are identical" );
  447. }
  448. if ( geometry.skinWeights.length > 0 ) {
  449. attr = new Float32BufferAttribute( geometry.skinWeights.length * 4, 4 );
  450. attr.copyVector4sArray( geometry.skinWeights );
  451. assert.ok( bufferAttributeEquals( a.attributes.skinWeight, attr ), "SkinWeights are identical" );
  452. }
  453. if ( geometry.boundingSphere !== null ) {
  454. assert.ok( a.boundingSphere.equals( geometry.boundingSphere ), "BoundingSphere is identical" );
  455. }
  456. if ( geometry.boundingBox !== null ) {
  457. assert.ok( a.boundingBox.equals( geometry.boundingBox ), "BoundingBox is identical" );
  458. }
  459. asyncDone();
  460. } );
  461. } );
  462. QUnit.test( "computeBoundingBox", ( assert ) => {
  463. var bb = getBBForVertices( [ - 1, - 2, - 3, 13, - 2, - 3.5, - 1, - 20, 0, - 4, 5, 6 ] );
  464. assert.ok( bb.min.x === - 4 && bb.min.y === - 20 && bb.min.z === - 3.5, "min values are set correctly" );
  465. assert.ok( bb.max.x === 13 && bb.max.y === 5 && bb.max.z === 6, "max values are set correctly" );
  466. var bb = getBBForVertices( [ - 1, - 1, - 1 ] );
  467. 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" );
  468. 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" );
  469. } );
  470. QUnit.test( "computeBoundingSphere", ( assert ) => {
  471. var bs = getBSForVertices( [ - 10, 0, 0, 10, 0, 0 ] );
  472. assert.ok( bs.radius === ( 10 + 10 ) / 2, "radius is equal to deltaMinMax / 2" );
  473. assert.ok( bs.center.x === 0 && bs.center.y === 0 && bs.center.y === 0, "bounding sphere is at ( 0, 0, 0 )" );
  474. var bs = getBSForVertices( [ - 5, 11, - 3, 5, - 11, 3 ] );
  475. var radius = new Vector3( 5, 11, 3 ).length();
  476. assert.ok( bs.radius === radius, "radius is equal to directionLength" );
  477. assert.ok( bs.center.x === 0 && bs.center.y === 0 && bs.center.y === 0, "bounding sphere is at ( 0, 0, 0 )" );
  478. } );
  479. QUnit.todo( "computeFaceNormals", ( assert ) => {
  480. assert.ok( false, "everything's gonna be alright" );
  481. } );
  482. QUnit.test( "computeVertexNormals", ( assert ) => {
  483. // get normals for a counter clockwise created triangle
  484. var normals = getNormalsForVertices( [ - 1, 0, 0, 1, 0, 0, 0, 1, 0 ], assert );
  485. assert.ok( normals[ 0 ] === 0 && normals[ 1 ] === 0 && normals[ 2 ] === 1,
  486. "first normal is pointing to screen since the the triangle was created counter clockwise" );
  487. assert.ok( normals[ 3 ] === 0 && normals[ 4 ] === 0 && normals[ 5 ] === 1,
  488. "second normal is pointing to screen since the the triangle was created counter clockwise" );
  489. assert.ok( normals[ 6 ] === 0 && normals[ 7 ] === 0 && normals[ 8 ] === 1,
  490. "third normal is pointing to screen since the the triangle was created counter clockwise" );
  491. // get normals for a clockwise created triangle
  492. var normals = getNormalsForVertices( [ 1, 0, 0, - 1, 0, 0, 0, 1, 0 ], assert );
  493. assert.ok( normals[ 0 ] === 0 && normals[ 1 ] === 0 && normals[ 2 ] === - 1,
  494. "first normal is pointing to screen since the the triangle was created clockwise" );
  495. assert.ok( normals[ 3 ] === 0 && normals[ 4 ] === 0 && normals[ 5 ] === - 1,
  496. "second normal is pointing to screen since the the triangle was created clockwise" );
  497. assert.ok( normals[ 6 ] === 0 && normals[ 7 ] === 0 && normals[ 8 ] === - 1,
  498. "third normal is pointing to screen since the the triangle was created clockwise" );
  499. var normals = getNormalsForVertices( [ 0, 0, 1, 0, 0, - 1, 1, 1, 0 ], assert );
  500. // the triangle is rotated by 45 degrees to the right so the normals of the three vertices
  501. // should point to (1, -1, 0).normalized(). The simplest solution is to check against a normalized
  502. // vector (1, -1, 0) but you will get calculation errors because of floating calculations so another
  503. // valid technique is to create a vector which stands in 90 degrees to the normals and calculate the
  504. // dot product which is the cos of the angle between them. This should be < floating calculation error
  505. // which can be taken from Number.EPSILON
  506. var direction = new Vector3( 1, 1, 0 ).normalize(); // a vector which should have 90 degrees difference to normals
  507. var difference = direction.dot( new Vector3( normals[ 0 ], normals[ 1 ], normals[ 2 ] ) );
  508. assert.ok( difference < Number.EPSILON, "normal is equal to reference vector" );
  509. // get normals for a line should be NAN because you need min a triangle to calculate normals
  510. var normals = getNormalsForVertices( [ 1, 0, 0, - 1, 0, 0 ], assert );
  511. for ( var i = 0; i < normals.length; i ++ ) {
  512. assert.ok( ! normals[ i ], "normals can't be calculated which is good" );
  513. }
  514. } );
  515. QUnit.test( "computeVertexNormals (indexed)", ( assert ) => {
  516. var sqrt = 0.5 * Math.sqrt( 2 );
  517. var normal = new BufferAttribute( new Float32Array( [
  518. - 1, 0, 0, - 1, 0, 0, - 1, 0, 0,
  519. sqrt, sqrt, 0, sqrt, sqrt, 0, sqrt, sqrt, 0,
  520. - 1, 0, 0
  521. ] ), 3 );
  522. var position = new BufferAttribute( new Float32Array( [
  523. 0.5, 0.5, 0.5, 0.5, 0.5, - 0.5, 0.5, - 0.5, 0.5,
  524. 0.5, - 0.5, - 0.5, - 0.5, 0.5, - 0.5, - 0.5, 0.5, 0.5,
  525. - 0.5, - 0.5, - 0.5
  526. ] ), 3 );
  527. var index = new BufferAttribute( new Uint16Array( [
  528. 0, 2, 1, 2, 3, 1, 4, 6, 5, 6, 7, 5
  529. ] ), 1 );
  530. var a = new BufferGeometry();
  531. a.addAttribute( "position", position );
  532. a.computeVertexNormals();
  533. assert.ok(
  534. bufferAttributeEquals( normal, a.getAttribute( "normal" ) ),
  535. "Regular geometry: first computed normals are correct"
  536. );
  537. // a second time to see if the existing normals get properly deleted
  538. a.computeVertexNormals();
  539. assert.ok(
  540. bufferAttributeEquals( normal, a.getAttribute( "normal" ) ),
  541. "Regular geometry: second computed normals are correct"
  542. );
  543. // indexed geometry
  544. var a = new BufferGeometry();
  545. a.addAttribute( "position", position );
  546. a.setIndex( index );
  547. a.computeVertexNormals();
  548. assert.ok( bufferAttributeEquals( normal, a.getAttribute( "normal" ) ), "Indexed geometry: computed normals are correct" );
  549. } );
  550. QUnit.test( "merge", ( assert ) => {
  551. var geometry1 = new BufferGeometry();
  552. geometry1.addAttribute( "attrName", new BufferAttribute( new Float32Array( [ 1, 2, 3, 0, 0, 0 ] ), 3 ) );
  553. var geometry2 = new BufferGeometry();
  554. geometry2.addAttribute( "attrName", new BufferAttribute( new Float32Array( [ 4, 5, 6 ] ), 3 ) );
  555. var attr = geometry1.attributes.attrName.array;
  556. geometry1.merge( geometry2, 1 );
  557. // merged array should be 1, 2, 3, 4, 5, 6
  558. for ( var i = 0; i < attr.length; i ++ ) {
  559. assert.ok( attr[ i ] === i + 1, "" );
  560. }
  561. geometry1.merge( geometry2 );
  562. assert.ok( attr[ 0 ] === 4 && attr[ 1 ] === 5 && attr[ 2 ] === 6, "copied the 3 attributes without offset" );
  563. } );
  564. QUnit.todo( "normalizeNormals", ( assert ) => {
  565. assert.ok( false, "everything's gonna be alright" );
  566. } );
  567. QUnit.test( "toNonIndexed", ( assert ) => {
  568. var geometry = new BufferGeometry();
  569. var vertices = new Float32Array( [
  570. 0.5, 0.5, 0.5, 0.5, 0.5, - 0.5, 0.5, - 0.5, 0.5, 0.5, - 0.5, - 0.5
  571. ] );
  572. var index = new BufferAttribute( new Uint16Array( [ 0, 2, 1, 2, 3, 1 ] ) );
  573. var expected = new Float32Array( [
  574. 0.5, 0.5, 0.5, 0.5, - 0.5, 0.5, 0.5, 0.5, - 0.5,
  575. 0.5, - 0.5, 0.5, 0.5, - 0.5, - 0.5, 0.5, 0.5, - 0.5
  576. ] );
  577. geometry.addAttribute( 'position', new BufferAttribute( vertices, 3 ) );
  578. geometry.setIndex( index );
  579. var nonIndexed = geometry.toNonIndexed();
  580. assert.deepEqual( nonIndexed.getAttribute( "position" ).array, expected, "Expected vertices" );
  581. } );
  582. QUnit.test( "toJSON", ( assert ) => {
  583. var index = new BufferAttribute( new Uint16Array( [ 0, 1, 2, 3 ] ), 1 );
  584. var attribute1 = new BufferAttribute( new Uint16Array( [ 1, 3, 5, 7 ] ), 1 );
  585. var a = new BufferGeometry();
  586. a.name = "JSONQUnit.test";
  587. // a.parameters = { "placeholder": 0 };
  588. a.addAttribute( "attribute1", attribute1 );
  589. a.setIndex( index );
  590. a.addGroup( 0, 1, 2 );
  591. a.boundingSphere = new Sphere( new Vector3( x, y, z ), 0.5 );
  592. var j = a.toJSON();
  593. var gold = {
  594. "metadata": {
  595. "version": 4.5,
  596. "type": "BufferGeometry",
  597. "generator": "BufferGeometry.toJSON"
  598. },
  599. "uuid": a.uuid,
  600. "type": "BufferGeometry",
  601. "name": "JSONQUnit.test",
  602. "data": {
  603. "attributes": {
  604. "attribute1": {
  605. "itemSize": 1,
  606. "type": "Uint16Array",
  607. "array": [ 1, 3, 5, 7 ],
  608. "normalized": false
  609. }
  610. },
  611. "index": {
  612. "type": "Uint16Array",
  613. "array": [ 0, 1, 2, 3 ]
  614. },
  615. "groups": [
  616. {
  617. "start": 0,
  618. "count": 1,
  619. "materialIndex": 2
  620. }
  621. ],
  622. "boundingSphere": {
  623. "center": [ 2, 3, 4 ],
  624. "radius": 0.5
  625. }
  626. }
  627. };
  628. assert.deepEqual( j, gold, "Generated JSON is as expected" );
  629. } );
  630. QUnit.test( "clone", ( assert ) => {
  631. var a = new BufferGeometry();
  632. a.addAttribute( "attribute1", new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ) );
  633. a.addAttribute( "attribute2", new BufferAttribute( new Float32Array( [ 0, 1, 3, 5, 6 ] ), 1 ) );
  634. a.addGroup( 0, 1, 2 );
  635. a.computeBoundingBox();
  636. a.computeBoundingSphere();
  637. a.setDrawRange( 0, 1 );
  638. var b = a.clone();
  639. assert.notEqual( a, b, "A new object was created" );
  640. assert.notEqual( a.id, b.id, "New object has a different GUID" );
  641. assert.strictEqual(
  642. Object.keys( a.attributes ).count, Object.keys( b.attributes ).count,
  643. "Both objects have the same amount of attributes"
  644. );
  645. assert.ok(
  646. bufferAttributeEquals( a.getAttribute( "attribute1" ), b.getAttribute( "attribute1" ) ),
  647. "First attributes buffer is identical"
  648. );
  649. assert.ok(
  650. bufferAttributeEquals( a.getAttribute( "attribute2" ), b.getAttribute( "attribute2" ) ),
  651. "Second attributes buffer is identical"
  652. );
  653. assert.deepEqual( a.groups, b.groups, "Groups are identical" );
  654. assert.ok( a.boundingBox.equals( b.boundingBox ), "BoundingBoxes are equal" );
  655. assert.ok( a.boundingSphere.equals( b.boundingSphere ), "BoundingSpheres are equal" );
  656. assert.strictEqual( a.drawRange.start, b.drawRange.start, "DrawRange start is identical" );
  657. assert.strictEqual( a.drawRange.count, b.drawRange.count, "DrawRange count is identical" );
  658. } );
  659. QUnit.test( "copy", ( assert ) => {
  660. var geometry = new BufferGeometry();
  661. geometry.addAttribute( "attrName", new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ) );
  662. geometry.addAttribute( "attrName2", new BufferAttribute( new Float32Array( [ 0, 1, 3, 5, 6 ] ), 1 ) );
  663. var copy = new BufferGeometry().copy( geometry );
  664. assert.ok( copy !== geometry && geometry.id !== copy.id, "new object was created" );
  665. Object.keys( geometry.attributes ).forEach( function ( key ) {
  666. var attribute = geometry.attributes[ key ];
  667. assert.ok( attribute !== undefined, "all attributes where copied" );
  668. for ( var i = 0; i < attribute.array.length; i ++ ) {
  669. assert.ok( attribute.array[ i ] === copy.attributes[ key ].array[ i ], "values of the attribute are equal" );
  670. }
  671. } );
  672. } );
  673. QUnit.todo( "dispose", ( assert ) => {
  674. assert.ok( false, "everything's gonna be alright" );
  675. } );
  676. } );
  677. } );
粤ICP备19079148号