lights.tests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
  2. export default QUnit.module( 'JSON4 Format', () => {
  3. QUnit.module( 'Lights', () => {
  4. QUnit.test( 'AmbientLight - color, intensity', ( assert ) => {
  5. const json = {
  6. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  7. object: {
  8. uuid: 'light-1',
  9. type: 'AmbientLight',
  10. color: 16777215,
  11. intensity: 0.5,
  12. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  13. layers: 1
  14. }
  15. };
  16. const loader = new ObjectLoader();
  17. const light = loader.parse( json );
  18. assert.ok( light.isAmbientLight, 'Is AmbientLight' );
  19. assert.strictEqual( light.color.getHex(), 0xffffff, 'Color is white' );
  20. assert.strictEqual( light.intensity, 0.5, 'Intensity is 0.5' );
  21. } );
  22. QUnit.test( 'DirectionalLight - basic', ( assert ) => {
  23. const json = {
  24. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  25. object: {
  26. uuid: 'light-1',
  27. type: 'DirectionalLight',
  28. color: 16777215,
  29. intensity: 1,
  30. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 5, 10, 5, 1 ],
  31. layers: 1
  32. }
  33. };
  34. const loader = new ObjectLoader();
  35. const light = loader.parse( json );
  36. assert.ok( light.isDirectionalLight, 'Is DirectionalLight' );
  37. assert.strictEqual( light.color.getHex(), 0xffffff, 'Color is white' );
  38. assert.strictEqual( light.intensity, 1, 'Intensity is 1' );
  39. assert.strictEqual( light.position.x, 5, 'Position.x is 5' );
  40. } );
  41. QUnit.test( 'DirectionalLight with target', ( assert ) => {
  42. const json = {
  43. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  44. object: {
  45. uuid: 'scene-1',
  46. type: 'Scene',
  47. children: [
  48. {
  49. uuid: 'light-1',
  50. type: 'DirectionalLight',
  51. color: 16777215,
  52. intensity: 1,
  53. target: 'target-1',
  54. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 5, 10, 5, 1 ],
  55. layers: 1
  56. },
  57. {
  58. uuid: 'target-1',
  59. type: 'Object3D',
  60. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  61. layers: 1
  62. }
  63. ]
  64. }
  65. };
  66. const loader = new ObjectLoader();
  67. const scene = loader.parse( json );
  68. const light = scene.children[ 0 ];
  69. assert.ok( light.isDirectionalLight, 'Is DirectionalLight' );
  70. assert.strictEqual( light.target.uuid, 'target-1', 'Target is bound' );
  71. } );
  72. QUnit.test( 'DirectionalLight with shadow', ( assert ) => {
  73. const json = {
  74. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  75. object: {
  76. uuid: 'light-1',
  77. type: 'DirectionalLight',
  78. color: 16777215,
  79. intensity: 1,
  80. castShadow: true,
  81. shadow: {
  82. bias: 0.001,
  83. normalBias: 0.02,
  84. radius: 1,
  85. mapSize: [ 1024, 1024 ]
  86. },
  87. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  88. layers: 1
  89. }
  90. };
  91. const loader = new ObjectLoader();
  92. const light = loader.parse( json );
  93. assert.strictEqual( light.castShadow, true, 'CastShadow is true' );
  94. assert.strictEqual( light.shadow.bias, 0.001, 'Shadow bias' );
  95. assert.strictEqual( light.shadow.normalBias, 0.02, 'Shadow normalBias' );
  96. assert.strictEqual( light.shadow.radius, 1, 'Shadow radius' );
  97. assert.strictEqual( light.shadow.mapSize.x, 1024, 'Shadow mapSize.x' );
  98. assert.strictEqual( light.shadow.mapSize.y, 1024, 'Shadow mapSize.y' );
  99. } );
  100. QUnit.test( 'PointLight - distance, decay', ( assert ) => {
  101. const json = {
  102. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  103. object: {
  104. uuid: 'light-1',
  105. type: 'PointLight',
  106. color: 16776960,
  107. intensity: 2,
  108. distance: 50,
  109. decay: 2,
  110. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 5, 0, 1 ],
  111. layers: 1
  112. }
  113. };
  114. const loader = new ObjectLoader();
  115. const light = loader.parse( json );
  116. assert.ok( light.isPointLight, 'Is PointLight' );
  117. assert.strictEqual( light.color.getHex(), 0xffff00, 'Color is yellow' );
  118. assert.strictEqual( light.intensity, 2, 'Intensity is 2' );
  119. assert.strictEqual( light.distance, 50, 'Distance is 50' );
  120. assert.strictEqual( light.decay, 2, 'Decay is 2' );
  121. } );
  122. QUnit.test( 'PointLight with shadow', ( assert ) => {
  123. const json = {
  124. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  125. object: {
  126. uuid: 'light-1',
  127. type: 'PointLight',
  128. color: 16777215,
  129. intensity: 1,
  130. distance: 0,
  131. decay: 2,
  132. castShadow: true,
  133. shadow: {
  134. bias: - 0.005,
  135. normalBias: 0,
  136. radius: 2,
  137. mapSize: [ 512, 512 ]
  138. },
  139. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  140. layers: 1
  141. }
  142. };
  143. const loader = new ObjectLoader();
  144. const light = loader.parse( json );
  145. assert.strictEqual( light.castShadow, true, 'CastShadow is true' );
  146. assert.strictEqual( light.shadow.bias, - 0.005, 'Shadow bias' );
  147. assert.strictEqual( light.shadow.radius, 2, 'Shadow radius' );
  148. } );
  149. QUnit.test( 'SpotLight - angle, penumbra', ( assert ) => {
  150. const json = {
  151. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  152. object: {
  153. uuid: 'light-1',
  154. type: 'SpotLight',
  155. color: 16777215,
  156. intensity: 1,
  157. distance: 100,
  158. angle: Math.PI / 6,
  159. penumbra: 0.2,
  160. decay: 2,
  161. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 10, 0, 1 ],
  162. layers: 1
  163. }
  164. };
  165. const loader = new ObjectLoader();
  166. const light = loader.parse( json );
  167. assert.ok( light.isSpotLight, 'Is SpotLight' );
  168. assert.ok( Math.abs( light.angle - Math.PI / 6 ) < 0.0001, 'Angle is PI/6' );
  169. assert.strictEqual( light.penumbra, 0.2, 'Penumbra is 0.2' );
  170. assert.strictEqual( light.distance, 100, 'Distance is 100' );
  171. assert.strictEqual( light.decay, 2, 'Decay is 2' );
  172. } );
  173. QUnit.test( 'SpotLight with shadow', ( assert ) => {
  174. const json = {
  175. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  176. object: {
  177. uuid: 'light-1',
  178. type: 'SpotLight',
  179. color: 16777215,
  180. intensity: 1,
  181. distance: 0,
  182. angle: Math.PI / 4,
  183. penumbra: 0.1,
  184. decay: 2,
  185. castShadow: true,
  186. shadow: {
  187. bias: 0,
  188. normalBias: 0,
  189. radius: 1,
  190. mapSize: [ 2048, 2048 ]
  191. },
  192. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  193. layers: 1
  194. }
  195. };
  196. const loader = new ObjectLoader();
  197. const light = loader.parse( json );
  198. assert.strictEqual( light.castShadow, true, 'CastShadow is true' );
  199. assert.strictEqual( light.shadow.mapSize.x, 2048, 'Shadow mapSize.x' );
  200. } );
  201. QUnit.test( 'HemisphereLight - groundColor', ( assert ) => {
  202. const json = {
  203. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  204. object: {
  205. uuid: 'light-1',
  206. type: 'HemisphereLight',
  207. color: 8900331,
  208. groundColor: 4473924,
  209. intensity: 0.6,
  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 light = loader.parse( json );
  216. assert.ok( light.isHemisphereLight, 'Is HemisphereLight' );
  217. assert.strictEqual( light.color.getHex(), 0x87ceeb, 'Sky color' );
  218. assert.strictEqual( light.groundColor.getHex(), 0x444444, 'Ground color' );
  219. assert.strictEqual( light.intensity, 0.6, 'Intensity is 0.6' );
  220. } );
  221. QUnit.test( 'RectAreaLight - width, height', ( assert ) => {
  222. const json = {
  223. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  224. object: {
  225. uuid: 'light-1',
  226. type: 'RectAreaLight',
  227. color: 16777215,
  228. intensity: 5,
  229. width: 10,
  230. height: 10,
  231. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 5, 0, 1 ],
  232. layers: 1
  233. }
  234. };
  235. const loader = new ObjectLoader();
  236. const light = loader.parse( json );
  237. assert.ok( light.isRectAreaLight, 'Is RectAreaLight' );
  238. assert.strictEqual( light.width, 10, 'Width is 10' );
  239. assert.strictEqual( light.height, 10, 'Height is 10' );
  240. assert.strictEqual( light.intensity, 5, 'Intensity is 5' );
  241. } );
  242. QUnit.test( 'LightProbe - spherical harmonics', ( assert ) => {
  243. const json = {
  244. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  245. object: {
  246. uuid: 'light-1',
  247. type: 'LightProbe',
  248. intensity: 1,
  249. sh: [
  250. 0.5, 0.5, 0.5,
  251. 0.1, 0.1, 0.1,
  252. 0.2, 0.2, 0.2,
  253. 0.05, 0.05, 0.05,
  254. 0.05, 0.05, 0.05,
  255. 0.05, 0.05, 0.05,
  256. 0.05, 0.05, 0.05,
  257. 0.05, 0.05, 0.05,
  258. 0.05, 0.05, 0.05
  259. ],
  260. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  261. layers: 1
  262. }
  263. };
  264. const loader = new ObjectLoader();
  265. const light = loader.parse( json );
  266. assert.ok( light.isLightProbe, 'Is LightProbe' );
  267. assert.ok( light.sh.isSphericalHarmonics3, 'Has SphericalHarmonics3' );
  268. assert.strictEqual( light.sh.coefficients[ 0 ].x, 0.5, 'SH coefficient' );
  269. } );
  270. QUnit.test( 'Shadow camera properties', ( assert ) => {
  271. const json = {
  272. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  273. object: {
  274. uuid: 'light-1',
  275. type: 'DirectionalLight',
  276. color: 16777215,
  277. intensity: 1,
  278. castShadow: true,
  279. shadow: {
  280. bias: 0,
  281. normalBias: 0,
  282. radius: 1,
  283. mapSize: [ 1024, 1024 ],
  284. camera: {
  285. uuid: 'cam-1',
  286. type: 'OrthographicCamera',
  287. left: - 10,
  288. right: 10,
  289. top: 10,
  290. bottom: - 10,
  291. near: 0.5,
  292. far: 500,
  293. zoom: 1,
  294. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  295. layers: 1
  296. }
  297. },
  298. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  299. layers: 1
  300. }
  301. };
  302. const loader = new ObjectLoader();
  303. const light = loader.parse( json );
  304. assert.ok( light.shadow.camera.isOrthographicCamera, 'Shadow camera is OrthographicCamera' );
  305. assert.strictEqual( light.shadow.camera.left, - 10, 'Shadow camera left' );
  306. assert.strictEqual( light.shadow.camera.right, 10, 'Shadow camera right' );
  307. assert.strictEqual( light.shadow.camera.near, 0.5, 'Shadow camera near' );
  308. assert.strictEqual( light.shadow.camera.far, 500, 'Shadow camera far' );
  309. } );
  310. QUnit.test( 'Shadow intensity', ( assert ) => {
  311. const json = {
  312. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  313. object: {
  314. uuid: 'light-1',
  315. type: 'DirectionalLight',
  316. color: 16777215,
  317. intensity: 1,
  318. castShadow: true,
  319. shadow: {
  320. intensity: 0.5,
  321. bias: 0,
  322. normalBias: 0,
  323. radius: 1,
  324. mapSize: [ 1024, 1024 ]
  325. },
  326. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  327. layers: 1
  328. }
  329. };
  330. const loader = new ObjectLoader();
  331. const light = loader.parse( json );
  332. assert.strictEqual( light.shadow.intensity, 0.5, 'Shadow intensity' );
  333. } );
  334. } );
  335. } );
粤ICP备19079148号