scene.tests.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
  2. export default QUnit.module( 'JSON4 Format', () => {
  3. QUnit.module( 'Scene', () => {
  4. QUnit.test( 'Scene type', ( assert ) => {
  5. const json = {
  6. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  7. object: {
  8. uuid: 'scene-1',
  9. type: 'Scene',
  10. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  11. layers: 1
  12. }
  13. };
  14. const loader = new ObjectLoader();
  15. const scene = loader.parse( json );
  16. assert.ok( scene.isScene, 'Is Scene' );
  17. assert.strictEqual( scene.type, 'Scene', 'Type is Scene' );
  18. } );
  19. QUnit.test( 'Background as Color (hex integer)', ( assert ) => {
  20. const json = {
  21. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  22. object: {
  23. uuid: 'scene-1',
  24. type: 'Scene',
  25. background: 8900331,
  26. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  27. layers: 1
  28. }
  29. };
  30. const loader = new ObjectLoader();
  31. const scene = loader.parse( json );
  32. assert.ok( scene.background.isColor, 'Background is Color' );
  33. assert.strictEqual( scene.background.getHex(), 0x87ceeb, 'Background color value' );
  34. } );
  35. QUnit.test( 'Background as Texture reference', ( assert ) => {
  36. const json = {
  37. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  38. images: [ {
  39. uuid: 'image-1',
  40. url: {
  41. data: [ 255, 0, 0, 255 ],
  42. width: 1,
  43. height: 1,
  44. type: 'Uint8Array'
  45. }
  46. } ],
  47. textures: [ {
  48. uuid: 'tex-1',
  49. image: 'image-1'
  50. } ],
  51. object: {
  52. uuid: 'scene-1',
  53. type: 'Scene',
  54. background: 'tex-1',
  55. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  56. layers: 1
  57. }
  58. };
  59. const loader = new ObjectLoader();
  60. const scene = loader.parse( json );
  61. assert.ok( scene.background.isTexture, 'Background is Texture' );
  62. } );
  63. QUnit.test( 'Environment texture', ( assert ) => {
  64. const json = {
  65. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  66. images: [ {
  67. uuid: 'image-1',
  68. url: {
  69. data: [ 128, 128, 128, 255 ],
  70. width: 1,
  71. height: 1,
  72. type: 'Uint8Array'
  73. }
  74. } ],
  75. textures: [ {
  76. uuid: 'env-tex-1',
  77. image: 'image-1'
  78. } ],
  79. object: {
  80. uuid: 'scene-1',
  81. type: 'Scene',
  82. environment: 'env-tex-1',
  83. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  84. layers: 1
  85. }
  86. };
  87. const loader = new ObjectLoader();
  88. const scene = loader.parse( json );
  89. assert.ok( scene.environment.isTexture, 'Environment is Texture' );
  90. } );
  91. QUnit.test( 'Fog - color, near, far', ( assert ) => {
  92. const json = {
  93. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  94. object: {
  95. uuid: 'scene-1',
  96. type: 'Scene',
  97. fog: {
  98. type: 'Fog',
  99. name: '',
  100. color: 16777215,
  101. near: 1,
  102. far: 100
  103. },
  104. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  105. layers: 1
  106. }
  107. };
  108. const loader = new ObjectLoader();
  109. const scene = loader.parse( json );
  110. assert.ok( scene.fog.isFog, 'Has Fog' );
  111. assert.strictEqual( scene.fog.color.getHex(), 0xffffff, 'Fog color' );
  112. assert.strictEqual( scene.fog.near, 1, 'Fog near' );
  113. assert.strictEqual( scene.fog.far, 100, 'Fog far' );
  114. } );
  115. QUnit.test( 'FogExp2 - color, density', ( assert ) => {
  116. const json = {
  117. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  118. object: {
  119. uuid: 'scene-1',
  120. type: 'Scene',
  121. fog: {
  122. type: 'FogExp2',
  123. name: '',
  124. color: 8421504,
  125. density: 0.02
  126. },
  127. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  128. layers: 1
  129. }
  130. };
  131. const loader = new ObjectLoader();
  132. const scene = loader.parse( json );
  133. assert.ok( scene.fog.isFogExp2, 'Has FogExp2' );
  134. assert.strictEqual( scene.fog.color.getHex(), 0x808080, 'FogExp2 color' );
  135. assert.strictEqual( scene.fog.density, 0.02, 'FogExp2 density' );
  136. } );
  137. QUnit.test( 'Fog with name', ( assert ) => {
  138. const json = {
  139. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  140. object: {
  141. uuid: 'scene-1',
  142. type: 'Scene',
  143. fog: {
  144. type: 'Fog',
  145. name: 'MyFog',
  146. color: 16777215,
  147. near: 10,
  148. far: 200
  149. },
  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 scene = loader.parse( json );
  156. assert.strictEqual( scene.fog.name, 'MyFog', 'Fog name' );
  157. } );
  158. QUnit.test( 'backgroundBlurriness', ( assert ) => {
  159. const json = {
  160. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  161. object: {
  162. uuid: 'scene-1',
  163. type: 'Scene',
  164. backgroundBlurriness: 0.5,
  165. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  166. layers: 1
  167. }
  168. };
  169. const loader = new ObjectLoader();
  170. const scene = loader.parse( json );
  171. assert.strictEqual( scene.backgroundBlurriness, 0.5, 'backgroundBlurriness' );
  172. } );
  173. QUnit.test( 'backgroundIntensity', ( assert ) => {
  174. const json = {
  175. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  176. object: {
  177. uuid: 'scene-1',
  178. type: 'Scene',
  179. backgroundIntensity: 0.8,
  180. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  181. layers: 1
  182. }
  183. };
  184. const loader = new ObjectLoader();
  185. const scene = loader.parse( json );
  186. assert.strictEqual( scene.backgroundIntensity, 0.8, 'backgroundIntensity' );
  187. } );
  188. QUnit.test( 'backgroundRotation', ( assert ) => {
  189. const json = {
  190. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  191. object: {
  192. uuid: 'scene-1',
  193. type: 'Scene',
  194. backgroundRotation: [ Math.PI / 4, 0, 0, 'XYZ' ],
  195. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  196. layers: 1
  197. }
  198. };
  199. const loader = new ObjectLoader();
  200. const scene = loader.parse( json );
  201. assert.ok( Math.abs( scene.backgroundRotation.x - Math.PI / 4 ) < 0.0001, 'backgroundRotation.x' );
  202. } );
  203. QUnit.test( 'environmentIntensity', ( assert ) => {
  204. const json = {
  205. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  206. object: {
  207. uuid: 'scene-1',
  208. type: 'Scene',
  209. environmentIntensity: 1.5,
  210. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  211. layers: 1
  212. }
  213. };
  214. const loader = new ObjectLoader();
  215. const scene = loader.parse( json );
  216. assert.strictEqual( scene.environmentIntensity, 1.5, 'environmentIntensity' );
  217. } );
  218. QUnit.test( 'environmentRotation', ( assert ) => {
  219. const json = {
  220. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  221. object: {
  222. uuid: 'scene-1',
  223. type: 'Scene',
  224. environmentRotation: [ 0, Math.PI / 2, 0, 'XYZ' ],
  225. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  226. layers: 1
  227. }
  228. };
  229. const loader = new ObjectLoader();
  230. const scene = loader.parse( json );
  231. assert.ok( Math.abs( scene.environmentRotation.y - Math.PI / 2 ) < 0.0001, 'environmentRotation.y' );
  232. } );
  233. QUnit.test( 'Scene with children', ( assert ) => {
  234. const json = {
  235. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  236. geometries: [ {
  237. uuid: 'geom-1',
  238. type: 'BoxGeometry',
  239. width: 1,
  240. height: 1,
  241. depth: 1
  242. } ],
  243. materials: [ {
  244. uuid: 'mat-1',
  245. type: 'MeshBasicMaterial',
  246. color: 16711680
  247. } ],
  248. object: {
  249. uuid: 'scene-1',
  250. type: 'Scene',
  251. background: 0,
  252. children: [
  253. {
  254. uuid: 'mesh-1',
  255. type: 'Mesh',
  256. geometry: 'geom-1',
  257. material: 'mat-1',
  258. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  259. layers: 1
  260. },
  261. {
  262. uuid: 'light-1',
  263. type: 'AmbientLight',
  264. color: 16777215,
  265. intensity: 0.5,
  266. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  267. layers: 1
  268. }
  269. ]
  270. }
  271. };
  272. const loader = new ObjectLoader();
  273. const scene = loader.parse( json );
  274. assert.strictEqual( scene.children.length, 2, 'Has 2 children' );
  275. assert.ok( scene.children[ 0 ].isMesh, 'First child is Mesh' );
  276. assert.ok( scene.children[ 1 ].isAmbientLight, 'Second child is AmbientLight' );
  277. } );
  278. } );
  279. } );
粤ICP备19079148号