Vector3.tests.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /* global QUnit */
  2. import { Vector3 } from '../../../../src/math/Vector3.js';
  3. import { Vector4 } from '../../../../src/math/Vector4.js';
  4. import { Matrix3 } from '../../../../src/math/Matrix3.js';
  5. import { Matrix4 } from '../../../../src/math/Matrix4.js';
  6. import { Spherical } from '../../../../src/math/Spherical.js';
  7. import { Quaternion } from '../../../../src/math/Quaternion.js';
  8. import { Euler } from '../../../../src/math/Euler.js';
  9. import { Cylindrical } from '../../../../src/math/Cylindrical.js';
  10. import { BufferAttribute } from '../../../../src/core/BufferAttribute.js';
  11. import { PerspectiveCamera } from '../../../../src/cameras/PerspectiveCamera.js';
  12. import {
  13. x,
  14. y,
  15. z,
  16. eps
  17. } from '../../utils/math-constants.js';
  18. export default QUnit.module( 'Maths', () => {
  19. QUnit.module( 'Vector3', () => {
  20. // INSTANCING
  21. QUnit.test( 'Instancing', ( assert ) => {
  22. let a = new Vector3();
  23. assert.ok( a.x == 0, 'Passed!' );
  24. assert.ok( a.y == 0, 'Passed!' );
  25. assert.ok( a.z == 0, 'Passed!' );
  26. a = new Vector3( x, y, z );
  27. assert.ok( a.x === x, 'Passed!' );
  28. assert.ok( a.y === y, 'Passed!' );
  29. assert.ok( a.z === z, 'Passed!' );
  30. } );
  31. // PUBLIC STUFF
  32. QUnit.test( 'isVector3', ( assert ) => {
  33. const object = new Vector3();
  34. assert.ok( object.isVector3, 'Vector3.isVector3 should be true' );
  35. } );
  36. QUnit.test( 'set', ( assert ) => {
  37. const a = new Vector3();
  38. assert.ok( a.x == 0, 'Passed!' );
  39. assert.ok( a.y == 0, 'Passed!' );
  40. assert.ok( a.z == 0, 'Passed!' );
  41. a.set( x, y, z );
  42. assert.ok( a.x == x, 'Passed!' );
  43. assert.ok( a.y == y, 'Passed!' );
  44. assert.ok( a.z == z, 'Passed!' );
  45. } );
  46. QUnit.todo( 'setScalar', ( assert ) => {
  47. assert.ok( false, 'everything\'s gonna be alright' );
  48. } );
  49. QUnit.todo( 'setX', ( assert ) => {
  50. assert.ok( false, 'everything\'s gonna be alright' );
  51. } );
  52. QUnit.todo( 'setY', ( assert ) => {
  53. assert.ok( false, 'everything\'s gonna be alright' );
  54. } );
  55. QUnit.todo( 'setZ', ( assert ) => {
  56. assert.ok( false, 'everything\'s gonna be alright' );
  57. } );
  58. QUnit.todo( 'setComponent', ( assert ) => {
  59. assert.ok( false, 'everything\'s gonna be alright' );
  60. } );
  61. QUnit.todo( 'getComponent', ( assert ) => {
  62. assert.ok( false, 'everything\'s gonna be alright' );
  63. } );
  64. QUnit.todo( 'clone', ( assert ) => {
  65. assert.ok( false, 'everything\'s gonna be alright' );
  66. } );
  67. QUnit.test( 'copy', ( assert ) => {
  68. const a = new Vector3( x, y, z );
  69. const b = new Vector3().copy( a );
  70. assert.ok( b.x == x, 'Passed!' );
  71. assert.ok( b.y == y, 'Passed!' );
  72. assert.ok( b.z == z, 'Passed!' );
  73. // ensure that it is a true copy
  74. a.x = 0;
  75. a.y = - 1;
  76. a.z = - 2;
  77. assert.ok( b.x == x, 'Passed!' );
  78. assert.ok( b.y == y, 'Passed!' );
  79. assert.ok( b.z == z, 'Passed!' );
  80. } );
  81. QUnit.test( 'add', ( assert ) => {
  82. const a = new Vector3( x, y, z );
  83. const b = new Vector3( - x, - y, - z );
  84. a.add( b );
  85. assert.ok( a.x == 0, 'Passed!' );
  86. assert.ok( a.y == 0, 'Passed!' );
  87. assert.ok( a.z == 0, 'Passed!' );
  88. const c = new Vector3().addVectors( b, b );
  89. assert.ok( c.x == - 2 * x, 'Passed!' );
  90. assert.ok( c.y == - 2 * y, 'Passed!' );
  91. assert.ok( c.z == - 2 * z, 'Passed!' );
  92. } );
  93. QUnit.todo( 'addScalar', ( assert ) => {
  94. assert.ok( false, 'everything\'s gonna be alright' );
  95. } );
  96. QUnit.todo( 'addVectors', ( assert ) => {
  97. assert.ok( false, 'everything\'s gonna be alright' );
  98. } );
  99. QUnit.test( 'addScaledVector', ( assert ) => {
  100. const a = new Vector3( x, y, z );
  101. const b = new Vector3( 2, 3, 4 );
  102. const s = 3;
  103. a.addScaledVector( b, s );
  104. assert.strictEqual( a.x, x + b.x * s, 'Check x' );
  105. assert.strictEqual( a.y, y + b.y * s, 'Check y' );
  106. assert.strictEqual( a.z, z + b.z * s, 'Check z' );
  107. } );
  108. QUnit.test( 'sub', ( assert ) => {
  109. const a = new Vector3( x, y, z );
  110. const b = new Vector3( - x, - y, - z );
  111. a.sub( b );
  112. assert.ok( a.x == 2 * x, 'Passed!' );
  113. assert.ok( a.y == 2 * y, 'Passed!' );
  114. assert.ok( a.z == 2 * z, 'Passed!' );
  115. const c = new Vector3().subVectors( a, a );
  116. assert.ok( c.x == 0, 'Passed!' );
  117. assert.ok( c.y == 0, 'Passed!' );
  118. assert.ok( c.z == 0, 'Passed!' );
  119. } );
  120. QUnit.todo( 'subScalar', ( assert ) => {
  121. assert.ok( false, 'everything\'s gonna be alright' );
  122. } );
  123. QUnit.todo( 'subVectors', ( assert ) => {
  124. assert.ok( false, 'everything\'s gonna be alright' );
  125. } );
  126. QUnit.todo( 'multiply', ( assert ) => {
  127. assert.ok( false, 'everything\'s gonna be alright' );
  128. } );
  129. QUnit.todo( 'multiplyScalar', ( assert ) => {
  130. assert.ok( false, 'everything\'s gonna be alright' );
  131. } );
  132. QUnit.test( 'multiplyVectors', ( assert ) => {
  133. const a = new Vector3( x, y, z );
  134. const b = new Vector3( 2, 3, - 5 );
  135. const c = new Vector3().multiplyVectors( a, b );
  136. assert.strictEqual( c.x, x * 2, 'Check x' );
  137. assert.strictEqual( c.y, y * 3, 'Check y' );
  138. assert.strictEqual( c.z, z * - 5, 'Check z' );
  139. } );
  140. QUnit.test( 'applyEuler', ( assert ) => {
  141. const a = new Vector3( x, y, z );
  142. const euler = new Euler( 90, - 45, 0 );
  143. const expected = new Vector3( - 2.352970120501014, - 4.7441750936226645, 0.9779234597246458 );
  144. a.applyEuler( euler );
  145. assert.ok( Math.abs( a.x - expected.x ) <= eps, 'Check x' );
  146. assert.ok( Math.abs( a.y - expected.y ) <= eps, 'Check y' );
  147. assert.ok( Math.abs( a.z - expected.z ) <= eps, 'Check z' );
  148. } );
  149. QUnit.test( 'applyAxisAngle', ( assert ) => {
  150. const a = new Vector3( x, y, z );
  151. const axis = new Vector3( 0, 1, 0 );
  152. const angle = Math.PI / 4.0;
  153. const expected = new Vector3( 3 * Math.sqrt( 2 ), 3, Math.sqrt( 2 ) );
  154. a.applyAxisAngle( axis, angle );
  155. assert.ok( Math.abs( a.x - expected.x ) <= eps, 'Check x' );
  156. assert.ok( Math.abs( a.y - expected.y ) <= eps, 'Check y' );
  157. assert.ok( Math.abs( a.z - expected.z ) <= eps, 'Check z' );
  158. } );
  159. QUnit.test( 'applyMatrix3', ( assert ) => {
  160. const a = new Vector3( x, y, z );
  161. const m = new Matrix3().set( 2, 3, 5, 7, 11, 13, 17, 19, 23 );
  162. a.applyMatrix3( m );
  163. assert.strictEqual( a.x, 33, 'Check x' );
  164. assert.strictEqual( a.y, 99, 'Check y' );
  165. assert.strictEqual( a.z, 183, 'Check z' );
  166. } );
  167. QUnit.todo( 'applyNormalMatrix', ( assert ) => {
  168. // applyNormalMatrix( m )
  169. assert.ok( false, 'everything\'s gonna be alright' );
  170. } );
  171. QUnit.test( 'applyMatrix4', ( assert ) => {
  172. const a = new Vector3( x, y, z );
  173. const b = new Vector4( x, y, z, 1 );
  174. let m = new Matrix4().makeRotationX( Math.PI );
  175. a.applyMatrix4( m );
  176. b.applyMatrix4( m );
  177. assert.ok( a.x == b.x / b.w, 'Passed!' );
  178. assert.ok( a.y == b.y / b.w, 'Passed!' );
  179. assert.ok( a.z == b.z / b.w, 'Passed!' );
  180. m = new Matrix4().makeTranslation( 3, 2, 1 );
  181. a.applyMatrix4( m );
  182. b.applyMatrix4( m );
  183. assert.ok( a.x == b.x / b.w, 'Passed!' );
  184. assert.ok( a.y == b.y / b.w, 'Passed!' );
  185. assert.ok( a.z == b.z / b.w, 'Passed!' );
  186. m = new Matrix4().set(
  187. 1, 0, 0, 0,
  188. 0, 1, 0, 0,
  189. 0, 0, 1, 0,
  190. 0, 0, 1, 0
  191. );
  192. a.applyMatrix4( m );
  193. b.applyMatrix4( m );
  194. assert.ok( a.x == b.x / b.w, 'Passed!' );
  195. assert.ok( a.y == b.y / b.w, 'Passed!' );
  196. assert.ok( a.z == b.z / b.w, 'Passed!' );
  197. } );
  198. QUnit.test( 'applyQuaternion', ( assert ) => {
  199. const a = new Vector3( x, y, z );
  200. a.applyQuaternion( new Quaternion() );
  201. assert.strictEqual( a.x, x, 'Identity rotation: check x' );
  202. assert.strictEqual( a.y, y, 'Identity rotation: check y' );
  203. assert.strictEqual( a.z, z, 'Identity rotation: check z' );
  204. } );
  205. QUnit.todo( 'project', ( assert ) => {
  206. assert.ok( false, 'everything\'s gonna be alright' );
  207. } );
  208. QUnit.todo( 'unproject', ( assert ) => {
  209. assert.ok( false, 'everything\'s gonna be alright' );
  210. } );
  211. QUnit.test( 'transformDirection', ( assert ) => {
  212. const a = new Vector3( x, y, z );
  213. const m = new Matrix4();
  214. const transformed = new Vector3( 0.3713906763541037, 0.5570860145311556, 0.7427813527082074 );
  215. a.transformDirection( m );
  216. assert.ok( Math.abs( a.x - transformed.x ) <= eps, 'Check x' );
  217. assert.ok( Math.abs( a.y - transformed.y ) <= eps, 'Check y' );
  218. assert.ok( Math.abs( a.z - transformed.z ) <= eps, 'Check z' );
  219. } );
  220. QUnit.todo( 'divide', ( assert ) => {
  221. assert.ok( false, 'everything\'s gonna be alright' );
  222. } );
  223. QUnit.todo( 'divideScalar', ( assert ) => {
  224. assert.ok( false, 'everything\'s gonna be alright' );
  225. } );
  226. QUnit.todo( 'min', ( assert ) => {
  227. assert.ok( false, 'everything\'s gonna be alright' );
  228. } );
  229. QUnit.todo( 'max', ( assert ) => {
  230. assert.ok( false, 'everything\'s gonna be alright' );
  231. } );
  232. QUnit.todo( 'clamp', ( assert ) => {
  233. assert.ok( false, 'everything\'s gonna be alright' );
  234. } );
  235. QUnit.test( 'clampScalar', ( assert ) => {
  236. const a = new Vector3( - 0.01, 0.5, 1.5 );
  237. const clamped = new Vector3( 0.1, 0.5, 1.0 );
  238. a.clampScalar( 0.1, 1.0 );
  239. assert.ok( Math.abs( a.x - clamped.x ) <= 0.001, 'Check x' );
  240. assert.ok( Math.abs( a.y - clamped.y ) <= 0.001, 'Check y' );
  241. assert.ok( Math.abs( a.z - clamped.z ) <= 0.001, 'Check z' );
  242. } );
  243. QUnit.todo( 'clampLength', ( assert ) => {
  244. assert.ok( false, 'everything\'s gonna be alright' );
  245. } );
  246. QUnit.todo( 'floor', ( assert ) => {
  247. assert.ok( false, 'everything\'s gonna be alright' );
  248. } );
  249. QUnit.todo( 'ceil', ( assert ) => {
  250. assert.ok( false, 'everything\'s gonna be alright' );
  251. } );
  252. QUnit.todo( 'round', ( assert ) => {
  253. assert.ok( false, 'everything\'s gonna be alright' );
  254. } );
  255. QUnit.todo( 'roundToZero', ( assert ) => {
  256. assert.ok( false, 'everything\'s gonna be alright' );
  257. } );
  258. QUnit.test( 'negate', ( assert ) => {
  259. const a = new Vector3( x, y, z );
  260. a.negate();
  261. assert.ok( a.x == - x, 'Passed!' );
  262. assert.ok( a.y == - y, 'Passed!' );
  263. assert.ok( a.z == - z, 'Passed!' );
  264. } );
  265. QUnit.test( 'dot', ( assert ) => {
  266. const a = new Vector3( x, y, z );
  267. const b = new Vector3( - x, - y, - z );
  268. const c = new Vector3();
  269. let result = a.dot( b );
  270. assert.ok( result == ( - x * x - y * y - z * z ), 'Passed!' );
  271. result = a.dot( c );
  272. assert.ok( result == 0, 'Passed!' );
  273. } );
  274. QUnit.todo( 'lengthSq', ( assert ) => {
  275. assert.ok( false, 'everything\'s gonna be alright' );
  276. } );
  277. QUnit.todo( 'length', ( assert ) => {
  278. assert.ok( false, 'everything\'s gonna be alright' );
  279. } );
  280. QUnit.test( 'manhattanLength', ( assert ) => {
  281. const a = new Vector3( x, 0, 0 );
  282. const b = new Vector3( 0, - y, 0 );
  283. const c = new Vector3( 0, 0, z );
  284. const d = new Vector3();
  285. assert.ok( a.manhattanLength() == x, 'Positive x' );
  286. assert.ok( b.manhattanLength() == y, 'Negative y' );
  287. assert.ok( c.manhattanLength() == z, 'Positive z' );
  288. assert.ok( d.manhattanLength() == 0, 'Empty initialization' );
  289. a.set( x, y, z );
  290. assert.ok( a.manhattanLength() == Math.abs( x ) + Math.abs( y ) + Math.abs( z ), 'All components' );
  291. } );
  292. QUnit.test( 'normalize', ( assert ) => {
  293. const a = new Vector3( x, 0, 0 );
  294. const b = new Vector3( 0, - y, 0 );
  295. const c = new Vector3( 0, 0, z );
  296. a.normalize();
  297. assert.ok( a.length() == 1, 'Passed!' );
  298. assert.ok( a.x == 1, 'Passed!' );
  299. b.normalize();
  300. assert.ok( b.length() == 1, 'Passed!' );
  301. assert.ok( b.y == - 1, 'Passed!' );
  302. c.normalize();
  303. assert.ok( c.length() == 1, 'Passed!' );
  304. assert.ok( c.z == 1, 'Passed!' );
  305. } );
  306. QUnit.test( 'setLength', ( assert ) => {
  307. let a = new Vector3( x, 0, 0 );
  308. assert.ok( a.length() == x, 'Passed!' );
  309. a.setLength( y );
  310. assert.ok( a.length() == y, 'Passed!' );
  311. a = new Vector3( 0, 0, 0 );
  312. assert.ok( a.length() == 0, 'Passed!' );
  313. a.setLength( y );
  314. assert.ok( a.length() == 0, 'Passed!' );
  315. a.setLength();
  316. assert.ok( isNaN( a.length() ), 'Passed!' );
  317. } );
  318. QUnit.todo( 'lerp', ( assert ) => {
  319. assert.ok( false, 'everything\'s gonna be alright' );
  320. } );
  321. QUnit.todo( 'lerpVectors', ( assert ) => {
  322. assert.ok( false, 'everything\'s gonna be alright' );
  323. } );
  324. QUnit.test( 'cross', ( assert ) => {
  325. const a = new Vector3( x, y, z );
  326. const b = new Vector3( 2 * x, - y, 0.5 * z );
  327. const crossed = new Vector3( 18, 12, - 18 );
  328. a.cross( b );
  329. assert.ok( Math.abs( a.x - crossed.x ) <= eps, 'Check x' );
  330. assert.ok( Math.abs( a.y - crossed.y ) <= eps, 'Check y' );
  331. assert.ok( Math.abs( a.z - crossed.z ) <= eps, 'Check z' );
  332. } );
  333. QUnit.test( 'crossVectors', ( assert ) => {
  334. const a = new Vector3( x, y, z );
  335. const b = new Vector3( x, - y, z );
  336. const c = new Vector3();
  337. const crossed = new Vector3( 24, 0, - 12 );
  338. c.crossVectors( a, b );
  339. assert.ok( Math.abs( c.x - crossed.x ) <= eps, 'Check x' );
  340. assert.ok( Math.abs( c.y - crossed.y ) <= eps, 'Check y' );
  341. assert.ok( Math.abs( c.z - crossed.z ) <= eps, 'Check z' );
  342. } );
  343. QUnit.test( 'projectOnVector', ( assert ) => {
  344. const a = new Vector3( 1, 0, 0 );
  345. const b = new Vector3();
  346. const normal = new Vector3( 10, 0, 0 );
  347. assert.ok( b.copy( a ).projectOnVector( normal ).equals( new Vector3( 1, 0, 0 ) ), 'Passed!' );
  348. a.set( 0, 1, 0 );
  349. assert.ok( b.copy( a ).projectOnVector( normal ).equals( new Vector3( 0, 0, 0 ) ), 'Passed!' );
  350. a.set( 0, 0, - 1 );
  351. assert.ok( b.copy( a ).projectOnVector( normal ).equals( new Vector3( 0, 0, 0 ) ), 'Passed!' );
  352. a.set( - 1, 0, 0 );
  353. assert.ok( b.copy( a ).projectOnVector( normal ).equals( new Vector3( - 1, 0, 0 ) ), 'Passed!' );
  354. } );
  355. QUnit.test( 'projectOnPlane', ( assert ) => {
  356. const a = new Vector3( 1, 0, 0 );
  357. const b = new Vector3();
  358. const normal = new Vector3( 1, 0, 0 );
  359. assert.ok( b.copy( a ).projectOnPlane( normal ).equals( new Vector3( 0, 0, 0 ) ), 'Passed!' );
  360. a.set( 0, 1, 0 );
  361. assert.ok( b.copy( a ).projectOnPlane( normal ).equals( new Vector3( 0, 1, 0 ) ), 'Passed!' );
  362. a.set( 0, 0, - 1 );
  363. assert.ok( b.copy( a ).projectOnPlane( normal ).equals( new Vector3( 0, 0, - 1 ) ), 'Passed!' );
  364. a.set( - 1, 0, 0 );
  365. assert.ok( b.copy( a ).projectOnPlane( normal ).equals( new Vector3( 0, 0, 0 ) ), 'Passed!' );
  366. } );
  367. QUnit.test( 'reflect', ( assert ) => {
  368. const a = new Vector3();
  369. const normal = new Vector3( 0, 1, 0 );
  370. const b = new Vector3();
  371. a.set( 0, - 1, 0 );
  372. assert.ok( b.copy( a ).reflect( normal ).equals( new Vector3( 0, 1, 0 ) ), 'Passed!' );
  373. a.set( 1, - 1, 0 );
  374. assert.ok( b.copy( a ).reflect( normal ).equals( new Vector3( 1, 1, 0 ) ), 'Passed!' );
  375. a.set( 1, - 1, 0 );
  376. normal.set( 0, - 1, 0 );
  377. assert.ok( b.copy( a ).reflect( normal ).equals( new Vector3( 1, 1, 0 ) ), 'Passed!' );
  378. } );
  379. QUnit.test( 'angleTo', ( assert ) => {
  380. const a = new Vector3( 0, - 0.18851655680720186, 0.9820700116639124 );
  381. const b = new Vector3( 0, 0.18851655680720186, - 0.9820700116639124 );
  382. assert.equal( a.angleTo( a ), 0 );
  383. assert.equal( a.angleTo( b ), Math.PI );
  384. const x = new Vector3( 1, 0, 0 );
  385. const y = new Vector3( 0, 1, 0 );
  386. const z = new Vector3( 0, 0, 1 );
  387. assert.equal( x.angleTo( y ), Math.PI / 2 );
  388. assert.equal( x.angleTo( z ), Math.PI / 2 );
  389. assert.equal( z.angleTo( x ), Math.PI / 2 );
  390. assert.ok( Math.abs( x.angleTo( new Vector3( 1, 1, 0 ) ) - ( Math.PI / 4 ) ) < 0.0000001 );
  391. } );
  392. QUnit.todo( 'distanceTo', ( assert ) => {
  393. assert.ok( false, 'everything\'s gonna be alright' );
  394. } );
  395. QUnit.todo( 'distanceToSquared', ( assert ) => {
  396. assert.ok( false, 'everything\'s gonna be alright' );
  397. } );
  398. QUnit.todo( 'manhattanDistanceTo', ( assert ) => {
  399. assert.ok( false, 'everything\'s gonna be alright' );
  400. } );
  401. QUnit.test( 'setFromSpherical', ( assert ) => {
  402. const a = new Vector3();
  403. const phi = Math.acos( - 0.5 );
  404. const theta = Math.sqrt( Math.PI ) * phi;
  405. const sph = new Spherical( 10, phi, theta );
  406. const expected = new Vector3( - 4.677914006701843, - 5, - 7.288149322420796 );
  407. a.setFromSpherical( sph );
  408. assert.ok( Math.abs( a.x - expected.x ) <= eps, 'Check x' );
  409. assert.ok( Math.abs( a.y - expected.y ) <= eps, 'Check y' );
  410. assert.ok( Math.abs( a.z - expected.z ) <= eps, 'Check z' );
  411. } );
  412. QUnit.todo( 'setFromSphericalCoords', ( assert ) => {
  413. // setFromSphericalCoords( radius, phi, theta )
  414. assert.ok( false, 'everything\'s gonna be alright' );
  415. } );
  416. QUnit.test( 'setFromCylindrical', ( assert ) => {
  417. const a = new Vector3();
  418. const cyl = new Cylindrical( 10, Math.PI * 0.125, 20 );
  419. const expected = new Vector3( 3.826834323650898, 20, 9.238795325112868 );
  420. a.setFromCylindrical( cyl );
  421. assert.ok( Math.abs( a.x - expected.x ) <= eps, 'Check x' );
  422. assert.ok( Math.abs( a.y - expected.y ) <= eps, 'Check y' );
  423. assert.ok( Math.abs( a.z - expected.z ) <= eps, 'Check z' );
  424. } );
  425. QUnit.todo( 'setFromCylindricalCoords', ( assert ) => {
  426. // setFromCylindricalCoords( radius, theta, y )
  427. assert.ok( false, 'everything\'s gonna be alright' );
  428. } );
  429. QUnit.test( 'setFromMatrixPosition', ( assert ) => {
  430. const a = new Vector3();
  431. const m = new Matrix4().set( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 );
  432. a.setFromMatrixPosition( m );
  433. assert.strictEqual( a.x, 7, 'Check x' );
  434. assert.strictEqual( a.y, 19, 'Check y' );
  435. assert.strictEqual( a.z, 37, 'Check z' );
  436. } );
  437. QUnit.test( 'setFromMatrixScale', ( assert ) => {
  438. const a = new Vector3();
  439. const m = new Matrix4().set( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 );
  440. const expected = new Vector3( 25.573423705088842, 31.921779399024736, 35.70714214271425 );
  441. a.setFromMatrixScale( m );
  442. assert.ok( Math.abs( a.x - expected.x ) <= eps, 'Check x' );
  443. assert.ok( Math.abs( a.y - expected.y ) <= eps, 'Check y' );
  444. assert.ok( Math.abs( a.z - expected.z ) <= eps, 'Check z' );
  445. } );
  446. QUnit.test( 'setFromMatrixColumn', ( assert ) => {
  447. const a = new Vector3();
  448. const m = new Matrix4().set( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 );
  449. a.setFromMatrixColumn( m, 0 );
  450. assert.strictEqual( a.x, 2, 'Index 0: check x' );
  451. assert.strictEqual( a.y, 11, 'Index 0: check y' );
  452. assert.strictEqual( a.z, 23, 'Index 0: check z' );
  453. a.setFromMatrixColumn( m, 2 );
  454. assert.strictEqual( a.x, 5, 'Index 2: check x' );
  455. assert.strictEqual( a.y, 17, 'Index 2: check y' );
  456. assert.strictEqual( a.z, 31, 'Index 2: check z' );
  457. } );
  458. QUnit.todo( 'setFromMatrix3Column', ( assert ) => {
  459. // setFromMatrix3Column( mat3, index )
  460. assert.ok( false, 'everything\'s gonna be alright' );
  461. } );
  462. QUnit.todo( 'setFromEuler', ( assert ) => {
  463. // setFromEuler( e )
  464. assert.ok( false, 'everything\'s gonna be alright' );
  465. } );
  466. QUnit.test( 'equals', ( assert ) => {
  467. const a = new Vector3( x, 0, z );
  468. const b = new Vector3( 0, - y, 0 );
  469. assert.ok( a.x != b.x, 'Passed!' );
  470. assert.ok( a.y != b.y, 'Passed!' );
  471. assert.ok( a.z != b.z, 'Passed!' );
  472. assert.ok( ! a.equals( b ), 'Passed!' );
  473. assert.ok( ! b.equals( a ), 'Passed!' );
  474. a.copy( b );
  475. assert.ok( a.x == b.x, 'Passed!' );
  476. assert.ok( a.y == b.y, 'Passed!' );
  477. assert.ok( a.z == b.z, 'Passed!' );
  478. assert.ok( a.equals( b ), 'Passed!' );
  479. assert.ok( b.equals( a ), 'Passed!' );
  480. } );
  481. QUnit.test( 'fromArray', ( assert ) => {
  482. const a = new Vector3();
  483. const array = [ 1, 2, 3, 4, 5, 6 ];
  484. a.fromArray( array );
  485. assert.strictEqual( a.x, 1, 'No offset: check x' );
  486. assert.strictEqual( a.y, 2, 'No offset: check y' );
  487. assert.strictEqual( a.z, 3, 'No offset: check z' );
  488. a.fromArray( array, 3 );
  489. assert.strictEqual( a.x, 4, 'With offset: check x' );
  490. assert.strictEqual( a.y, 5, 'With offset: check y' );
  491. assert.strictEqual( a.z, 6, 'With offset: check z' );
  492. } );
  493. QUnit.test( 'toArray', ( assert ) => {
  494. const a = new Vector3( x, y, z );
  495. let array = a.toArray();
  496. assert.strictEqual( array[ 0 ], x, 'No array, no offset: check x' );
  497. assert.strictEqual( array[ 1 ], y, 'No array, no offset: check y' );
  498. assert.strictEqual( array[ 2 ], z, 'No array, no offset: check z' );
  499. array = [];
  500. a.toArray( array );
  501. assert.strictEqual( array[ 0 ], x, 'With array, no offset: check x' );
  502. assert.strictEqual( array[ 1 ], y, 'With array, no offset: check y' );
  503. assert.strictEqual( array[ 2 ], z, 'With array, no offset: check z' );
  504. array = [];
  505. a.toArray( array, 1 );
  506. assert.strictEqual( array[ 0 ], undefined, 'With array and offset: check [0]' );
  507. assert.strictEqual( array[ 1 ], x, 'With array and offset: check x' );
  508. assert.strictEqual( array[ 2 ], y, 'With array and offset: check y' );
  509. assert.strictEqual( array[ 3 ], z, 'With array and offset: check z' );
  510. } );
  511. QUnit.test( 'fromBufferAttribute', ( assert ) => {
  512. const a = new Vector3();
  513. const attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 );
  514. a.fromBufferAttribute( attr, 0 );
  515. assert.strictEqual( a.x, 1, 'Offset 0: check x' );
  516. assert.strictEqual( a.y, 2, 'Offset 0: check y' );
  517. assert.strictEqual( a.z, 3, 'Offset 0: check z' );
  518. a.fromBufferAttribute( attr, 1 );
  519. assert.strictEqual( a.x, 4, 'Offset 1: check x' );
  520. assert.strictEqual( a.y, 5, 'Offset 1: check y' );
  521. assert.strictEqual( a.z, 6, 'Offset 1: check z' );
  522. } );
  523. QUnit.todo( 'random', ( assert ) => {
  524. // random()
  525. assert.ok( false, 'everything\'s gonna be alright' );
  526. } );
  527. QUnit.test( 'randomDirection', ( assert ) => {
  528. const vec = new Vector3();
  529. vec.randomDirection();
  530. const zero = new Vector3();
  531. assert.notDeepEqual(
  532. vec,
  533. zero,
  534. 'randomizes at least one component of the vector'
  535. );
  536. assert.ok( ( 1 - vec.length() ) <= Number.EPSILON, 'produces a unit vector' );
  537. } );
  538. // TODO (Itee) refactor/split
  539. QUnit.test( 'setX,setY,setZ', ( assert ) => {
  540. const a = new Vector3();
  541. assert.ok( a.x == 0, 'Passed!' );
  542. assert.ok( a.y == 0, 'Passed!' );
  543. assert.ok( a.z == 0, 'Passed!' );
  544. a.setX( x );
  545. a.setY( y );
  546. a.setZ( z );
  547. assert.ok( a.x == x, 'Passed!' );
  548. assert.ok( a.y == y, 'Passed!' );
  549. assert.ok( a.z == z, 'Passed!' );
  550. } );
  551. QUnit.test( 'setComponent,getComponent', ( assert ) => {
  552. const a = new Vector3();
  553. assert.ok( a.x == 0, 'Passed!' );
  554. assert.ok( a.y == 0, 'Passed!' );
  555. assert.ok( a.z == 0, 'Passed!' );
  556. a.setComponent( 0, 1 );
  557. a.setComponent( 1, 2 );
  558. a.setComponent( 2, 3 );
  559. assert.ok( a.getComponent( 0 ) == 1, 'Passed!' );
  560. assert.ok( a.getComponent( 1 ) == 2, 'Passed!' );
  561. assert.ok( a.getComponent( 2 ) == 3, 'Passed!' );
  562. } );
  563. QUnit.test( 'setComponent/getComponent exceptions', ( assert ) => {
  564. const a = new Vector3();
  565. assert.throws(
  566. function () {
  567. a.setComponent( 3, 0 );
  568. },
  569. /index is out of range/,
  570. 'setComponent with an out of range index throws Error'
  571. );
  572. assert.throws(
  573. function () {
  574. a.getComponent( 3 );
  575. },
  576. /index is out of range/,
  577. 'getComponent with an out of range index throws Error'
  578. );
  579. } );
  580. QUnit.test( 'min/max/clamp', ( assert ) => {
  581. const a = new Vector3( x, y, z );
  582. const b = new Vector3( - x, - y, - z );
  583. const c = new Vector3();
  584. c.copy( a ).min( b );
  585. assert.ok( c.x == - x, 'Passed!' );
  586. assert.ok( c.y == - y, 'Passed!' );
  587. assert.ok( c.z == - z, 'Passed!' );
  588. c.copy( a ).max( b );
  589. assert.ok( c.x == x, 'Passed!' );
  590. assert.ok( c.y == y, 'Passed!' );
  591. assert.ok( c.z == z, 'Passed!' );
  592. c.set( - 2 * x, 2 * y, - 2 * z );
  593. c.clamp( b, a );
  594. assert.ok( c.x == - x, 'Passed!' );
  595. assert.ok( c.y == y, 'Passed!' );
  596. assert.ok( c.z == - z, 'Passed!' );
  597. } );
  598. QUnit.test( 'distanceTo/distanceToSquared', ( assert ) => {
  599. const a = new Vector3( x, 0, 0 );
  600. const b = new Vector3( 0, - y, 0 );
  601. const c = new Vector3( 0, 0, z );
  602. const d = new Vector3();
  603. assert.ok( a.distanceTo( d ) == x, 'Passed!' );
  604. assert.ok( a.distanceToSquared( d ) == x * x, 'Passed!' );
  605. assert.ok( b.distanceTo( d ) == y, 'Passed!' );
  606. assert.ok( b.distanceToSquared( d ) == y * y, 'Passed!' );
  607. assert.ok( c.distanceTo( d ) == z, 'Passed!' );
  608. assert.ok( c.distanceToSquared( d ) == z * z, 'Passed!' );
  609. } );
  610. QUnit.test( 'setScalar/addScalar/subScalar', ( assert ) => {
  611. const a = new Vector3();
  612. const s = 3;
  613. a.setScalar( s );
  614. assert.strictEqual( a.x, s, 'setScalar: check x' );
  615. assert.strictEqual( a.y, s, 'setScalar: check y' );
  616. assert.strictEqual( a.z, s, 'setScalar: check z' );
  617. a.addScalar( s );
  618. assert.strictEqual( a.x, 2 * s, 'addScalar: check x' );
  619. assert.strictEqual( a.y, 2 * s, 'addScalar: check y' );
  620. assert.strictEqual( a.z, 2 * s, 'addScalar: check z' );
  621. a.subScalar( 2 * s );
  622. assert.strictEqual( a.x, 0, 'subScalar: check x' );
  623. assert.strictEqual( a.y, 0, 'subScalar: check y' );
  624. assert.strictEqual( a.z, 0, 'subScalar: check z' );
  625. } );
  626. QUnit.test( 'multiply/divide', ( assert ) => {
  627. const a = new Vector3( x, y, z );
  628. const b = new Vector3( 2 * x, 2 * y, 2 * z );
  629. const c = new Vector3( 4 * x, 4 * y, 4 * z );
  630. a.multiply( b );
  631. assert.strictEqual( a.x, x * b.x, 'multiply: check x' );
  632. assert.strictEqual( a.y, y * b.y, 'multiply: check y' );
  633. assert.strictEqual( a.z, z * b.z, 'multiply: check z' );
  634. b.divide( c );
  635. assert.ok( Math.abs( b.x - 0.5 ) <= eps, 'divide: check z' );
  636. assert.ok( Math.abs( b.y - 0.5 ) <= eps, 'divide: check z' );
  637. assert.ok( Math.abs( b.z - 0.5 ) <= eps, 'divide: check z' );
  638. } );
  639. QUnit.test( 'multiply/divide', ( assert ) => {
  640. const a = new Vector3( x, y, z );
  641. const b = new Vector3( - x, - y, - z );
  642. a.multiplyScalar( - 2 );
  643. assert.ok( a.x == x * - 2, 'Passed!' );
  644. assert.ok( a.y == y * - 2, 'Passed!' );
  645. assert.ok( a.z == z * - 2, 'Passed!' );
  646. b.multiplyScalar( - 2 );
  647. assert.ok( b.x == 2 * x, 'Passed!' );
  648. assert.ok( b.y == 2 * y, 'Passed!' );
  649. assert.ok( b.z == 2 * z, 'Passed!' );
  650. a.divideScalar( - 2 );
  651. assert.ok( a.x == x, 'Passed!' );
  652. assert.ok( a.y == y, 'Passed!' );
  653. assert.ok( a.z == z, 'Passed!' );
  654. b.divideScalar( - 2 );
  655. assert.ok( b.x == - x, 'Passed!' );
  656. assert.ok( b.y == - y, 'Passed!' );
  657. assert.ok( b.z == - z, 'Passed!' );
  658. } );
  659. QUnit.test( 'project/unproject', ( assert ) => {
  660. const a = new Vector3( x, y, z );
  661. const camera = new PerspectiveCamera( 75, 16 / 9, 0.1, 300.0 );
  662. const projected = new Vector3( - 0.36653213611158914, - 0.9774190296309043, 1.0506835611870624 );
  663. a.project( camera );
  664. assert.ok( Math.abs( a.x - projected.x ) <= eps, 'project: check x' );
  665. assert.ok( Math.abs( a.y - projected.y ) <= eps, 'project: check y' );
  666. assert.ok( Math.abs( a.z - projected.z ) <= eps, 'project: check z' );
  667. a.unproject( camera );
  668. assert.ok( Math.abs( a.x - x ) <= eps, 'unproject: check x' );
  669. assert.ok( Math.abs( a.y - y ) <= eps, 'unproject: check y' );
  670. assert.ok( Math.abs( a.z - z ) <= eps, 'unproject: check z' );
  671. } );
  672. QUnit.test( 'length/lengthSq', ( assert ) => {
  673. const a = new Vector3( x, 0, 0 );
  674. const b = new Vector3( 0, - y, 0 );
  675. const c = new Vector3( 0, 0, z );
  676. const d = new Vector3();
  677. assert.ok( a.length() == x, 'Passed!' );
  678. assert.ok( a.lengthSq() == x * x, 'Passed!' );
  679. assert.ok( b.length() == y, 'Passed!' );
  680. assert.ok( b.lengthSq() == y * y, 'Passed!' );
  681. assert.ok( c.length() == z, 'Passed!' );
  682. assert.ok( c.lengthSq() == z * z, 'Passed!' );
  683. assert.ok( d.length() == 0, 'Passed!' );
  684. assert.ok( d.lengthSq() == 0, 'Passed!' );
  685. a.set( x, y, z );
  686. assert.ok( a.length() == Math.sqrt( x * x + y * y + z * z ), 'Passed!' );
  687. assert.ok( a.lengthSq() == ( x * x + y * y + z * z ), 'Passed!' );
  688. } );
  689. QUnit.test( 'lerp/clone', ( assert ) => {
  690. const a = new Vector3( x, 0, z );
  691. const b = new Vector3( 0, - y, 0 );
  692. assert.ok( a.lerp( a, 0 ).equals( a.lerp( a, 0.5 ) ), 'Passed!' );
  693. assert.ok( a.lerp( a, 0 ).equals( a.lerp( a, 1 ) ), 'Passed!' );
  694. assert.ok( a.clone().lerp( b, 0 ).equals( a ), 'Passed!' );
  695. assert.ok( a.clone().lerp( b, 0.5 ).x == x * 0.5, 'Passed!' );
  696. assert.ok( a.clone().lerp( b, 0.5 ).y == - y * 0.5, 'Passed!' );
  697. assert.ok( a.clone().lerp( b, 0.5 ).z == z * 0.5, 'Passed!' );
  698. assert.ok( a.clone().lerp( b, 1 ).equals( b ), 'Passed!' );
  699. } );
  700. // OTHERS
  701. QUnit.test( 'iterable', ( assert ) => {
  702. const v = new Vector3( 0, 0.5, 1 );
  703. const array = [ ...v ];
  704. assert.strictEqual( array[ 0 ], 0, 'Vector3 is iterable.' );
  705. assert.strictEqual( array[ 1 ], 0.5, 'Vector3 is iterable.' );
  706. assert.strictEqual( array[ 2 ], 1, 'Vector3 is iterable.' );
  707. } );
  708. } );
  709. } );
粤ICP备19079148号