BufferGeometry.tests.js 29 KB

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