1
0

morph.tests.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
  2. export default QUnit.module( 'JSON4 Format', () => {
  3. QUnit.module( 'Morph Targets', () => {
  4. QUnit.test( 'morphAttributes on geometry', ( assert ) => {
  5. const json = {
  6. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  7. geometries: [ {
  8. uuid: 'geom-1',
  9. type: 'BufferGeometry',
  10. data: {
  11. attributes: {
  12. position: {
  13. itemSize: 3,
  14. type: 'Float32Array',
  15. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
  16. normalized: false
  17. }
  18. },
  19. morphAttributes: {
  20. position: [
  21. {
  22. itemSize: 3,
  23. type: 'Float32Array',
  24. array: [ 0, 0, 0, 2, 0, 0, 0, 2, 0 ],
  25. normalized: false
  26. }
  27. ]
  28. }
  29. }
  30. } ],
  31. materials: [ {
  32. uuid: 'mat-1',
  33. type: 'MeshBasicMaterial',
  34. color: 16711680,
  35. morphTargets: true
  36. } ],
  37. object: {
  38. uuid: 'mesh-1',
  39. type: 'Mesh',
  40. geometry: 'geom-1',
  41. material: 'mat-1',
  42. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  43. layers: 1
  44. }
  45. };
  46. const loader = new ObjectLoader();
  47. const mesh = loader.parse( json );
  48. assert.ok( mesh.geometry.morphAttributes.position, 'Has morphAttributes.position' );
  49. assert.strictEqual( mesh.geometry.morphAttributes.position.length, 1, 'Has 1 morph target' );
  50. assert.strictEqual( mesh.geometry.morphAttributes.position[ 0 ].count, 3, 'Morph target has 3 vertices' );
  51. } );
  52. QUnit.test( 'morphTargetsRelative flag', ( assert ) => {
  53. const json = {
  54. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  55. geometries: [ {
  56. uuid: 'geom-1',
  57. type: 'BufferGeometry',
  58. data: {
  59. attributes: {
  60. position: {
  61. itemSize: 3,
  62. type: 'Float32Array',
  63. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
  64. normalized: false
  65. }
  66. },
  67. morphAttributes: {
  68. position: [
  69. {
  70. itemSize: 3,
  71. type: 'Float32Array',
  72. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
  73. normalized: false
  74. }
  75. ]
  76. },
  77. morphTargetsRelative: true
  78. }
  79. } ],
  80. materials: [ {
  81. uuid: 'mat-1',
  82. type: 'MeshBasicMaterial',
  83. color: 16711680
  84. } ],
  85. object: {
  86. uuid: 'mesh-1',
  87. type: 'Mesh',
  88. geometry: 'geom-1',
  89. material: 'mat-1',
  90. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  91. layers: 1
  92. }
  93. };
  94. const loader = new ObjectLoader();
  95. const mesh = loader.parse( json );
  96. assert.strictEqual( mesh.geometry.morphTargetsRelative, true, 'morphTargetsRelative is true' );
  97. } );
  98. QUnit.test( 'Multiple morph targets per attribute', ( assert ) => {
  99. const json = {
  100. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  101. geometries: [ {
  102. uuid: 'geom-1',
  103. type: 'BufferGeometry',
  104. data: {
  105. attributes: {
  106. position: {
  107. itemSize: 3,
  108. type: 'Float32Array',
  109. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
  110. normalized: false
  111. }
  112. },
  113. morphAttributes: {
  114. position: [
  115. {
  116. name: 'smile',
  117. itemSize: 3,
  118. type: 'Float32Array',
  119. array: [ 0, 0, 0, 1.5, 0, 0, 0, 1.5, 0 ],
  120. normalized: false
  121. },
  122. {
  123. name: 'frown',
  124. itemSize: 3,
  125. type: 'Float32Array',
  126. array: [ 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0 ],
  127. normalized: false
  128. },
  129. {
  130. name: 'blink',
  131. itemSize: 3,
  132. type: 'Float32Array',
  133. array: [ 0, - 0.1, 0, 1, - 0.1, 0, 0, 0.9, 0 ],
  134. normalized: false
  135. }
  136. ]
  137. }
  138. }
  139. } ],
  140. materials: [ {
  141. uuid: 'mat-1',
  142. type: 'MeshBasicMaterial',
  143. color: 16711680
  144. } ],
  145. object: {
  146. uuid: 'mesh-1',
  147. type: 'Mesh',
  148. geometry: 'geom-1',
  149. material: 'mat-1',
  150. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  151. layers: 1
  152. }
  153. };
  154. const loader = new ObjectLoader();
  155. const mesh = loader.parse( json );
  156. assert.strictEqual( mesh.geometry.morphAttributes.position.length, 3, 'Has 3 morph targets' );
  157. assert.strictEqual( mesh.geometry.morphAttributes.position[ 0 ].name, 'smile', 'First target name' );
  158. assert.strictEqual( mesh.geometry.morphAttributes.position[ 1 ].name, 'frown', 'Second target name' );
  159. assert.strictEqual( mesh.geometry.morphAttributes.position[ 2 ].name, 'blink', 'Third target name' );
  160. } );
  161. QUnit.test( 'Morph targets for normal attribute', ( assert ) => {
  162. const json = {
  163. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  164. geometries: [ {
  165. uuid: 'geom-1',
  166. type: 'BufferGeometry',
  167. data: {
  168. attributes: {
  169. position: {
  170. itemSize: 3,
  171. type: 'Float32Array',
  172. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
  173. normalized: false
  174. },
  175. normal: {
  176. itemSize: 3,
  177. type: 'Float32Array',
  178. array: [ 0, 0, 1, 0, 0, 1, 0, 0, 1 ],
  179. normalized: false
  180. }
  181. },
  182. morphAttributes: {
  183. position: [
  184. {
  185. itemSize: 3,
  186. type: 'Float32Array',
  187. array: [ 0, 0, 0.5, 1, 0, 0.5, 0, 1, 0.5 ],
  188. normalized: false
  189. }
  190. ],
  191. normal: [
  192. {
  193. itemSize: 3,
  194. type: 'Float32Array',
  195. array: [ 0.5, 0, 0.866, 0.5, 0, 0.866, 0.5, 0, 0.866 ],
  196. normalized: false
  197. }
  198. ]
  199. }
  200. }
  201. } ],
  202. materials: [ {
  203. uuid: 'mat-1',
  204. type: 'MeshStandardMaterial',
  205. color: 16711680,
  206. morphTargets: true,
  207. morphNormals: true
  208. } ],
  209. object: {
  210. uuid: 'mesh-1',
  211. type: 'Mesh',
  212. geometry: 'geom-1',
  213. material: 'mat-1',
  214. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  215. layers: 1
  216. }
  217. };
  218. const loader = new ObjectLoader();
  219. const mesh = loader.parse( json );
  220. assert.ok( mesh.geometry.morphAttributes.position, 'Has morphAttributes.position' );
  221. assert.ok( mesh.geometry.morphAttributes.normal, 'Has morphAttributes.normal' );
  222. assert.strictEqual( mesh.geometry.morphAttributes.normal.length, 1, 'Has 1 normal morph target' );
  223. } );
  224. QUnit.test( 'Morph targets for color attribute', ( assert ) => {
  225. const json = {
  226. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  227. geometries: [ {
  228. uuid: 'geom-1',
  229. type: 'BufferGeometry',
  230. data: {
  231. attributes: {
  232. position: {
  233. itemSize: 3,
  234. type: 'Float32Array',
  235. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
  236. normalized: false
  237. },
  238. color: {
  239. itemSize: 3,
  240. type: 'Float32Array',
  241. array: [ 1, 0, 0, 1, 0, 0, 1, 0, 0 ],
  242. normalized: false
  243. }
  244. },
  245. morphAttributes: {
  246. position: [
  247. {
  248. itemSize: 3,
  249. type: 'Float32Array',
  250. array: [ 0, 0, 0, 2, 0, 0, 0, 2, 0 ],
  251. normalized: false
  252. }
  253. ],
  254. color: [
  255. {
  256. itemSize: 3,
  257. type: 'Float32Array',
  258. array: [ 0, 1, 0, 0, 1, 0, 0, 1, 0 ],
  259. normalized: false
  260. }
  261. ]
  262. }
  263. }
  264. } ],
  265. materials: [ {
  266. uuid: 'mat-1',
  267. type: 'MeshBasicMaterial',
  268. color: 16777215,
  269. vertexColors: true
  270. } ],
  271. object: {
  272. uuid: 'mesh-1',
  273. type: 'Mesh',
  274. geometry: 'geom-1',
  275. material: 'mat-1',
  276. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  277. layers: 1
  278. }
  279. };
  280. const loader = new ObjectLoader();
  281. const mesh = loader.parse( json );
  282. assert.ok( mesh.geometry.morphAttributes.color, 'Has morphAttributes.color' );
  283. assert.strictEqual( mesh.geometry.morphAttributes.color.length, 1, 'Has 1 color morph target' );
  284. } );
  285. } );
  286. } );
粤ICP备19079148号