RoomEnvironment.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * https://github.com/google/model-viewer/blob/master/packages/model-viewer/src/three-components/EnvironmentScene.ts
  3. */
  4. import {
  5. BackSide,
  6. BoxGeometry,
  7. Mesh,
  8. MeshBasicMaterial,
  9. MeshStandardMaterial,
  10. PointLight,
  11. Scene,
  12. } from 'three';
  13. class RoomEnvironment extends Scene {
  14. constructor( renderer = null ) {
  15. super();
  16. const geometry = new BoxGeometry();
  17. geometry.deleteAttribute( 'uv' );
  18. const roomMaterial = new MeshStandardMaterial( { side: BackSide } );
  19. const boxMaterial = new MeshStandardMaterial();
  20. let intensity = 5;
  21. if ( renderer !== null && renderer._useLegacyLights === false ) intensity = 900;
  22. const mainLight = new PointLight( 0xffffff, intensity, 28, 2 );
  23. mainLight.position.set( 0.418, 16.199, 0.300 );
  24. this.add( mainLight );
  25. const room = new Mesh( geometry, roomMaterial );
  26. room.position.set( - 0.757, 13.219, 0.717 );
  27. room.scale.set( 31.713, 28.305, 28.591 );
  28. this.add( room );
  29. const box1 = new Mesh( geometry, boxMaterial );
  30. box1.position.set( - 10.906, 2.009, 1.846 );
  31. box1.rotation.set( 0, - 0.195, 0 );
  32. box1.scale.set( 2.328, 7.905, 4.651 );
  33. this.add( box1 );
  34. const box2 = new Mesh( geometry, boxMaterial );
  35. box2.position.set( - 5.607, - 0.754, - 0.758 );
  36. box2.rotation.set( 0, 0.994, 0 );
  37. box2.scale.set( 1.970, 1.534, 3.955 );
  38. this.add( box2 );
  39. const box3 = new Mesh( geometry, boxMaterial );
  40. box3.position.set( 6.167, 0.857, 7.803 );
  41. box3.rotation.set( 0, 0.561, 0 );
  42. box3.scale.set( 3.927, 6.285, 3.687 );
  43. this.add( box3 );
  44. const box4 = new Mesh( geometry, boxMaterial );
  45. box4.position.set( - 2.017, 0.018, 6.124 );
  46. box4.rotation.set( 0, 0.333, 0 );
  47. box4.scale.set( 2.002, 4.566, 2.064 );
  48. this.add( box4 );
  49. const box5 = new Mesh( geometry, boxMaterial );
  50. box5.position.set( 2.291, - 0.756, - 2.621 );
  51. box5.rotation.set( 0, - 0.286, 0 );
  52. box5.scale.set( 1.546, 1.552, 1.496 );
  53. this.add( box5 );
  54. const box6 = new Mesh( geometry, boxMaterial );
  55. box6.position.set( - 2.193, - 0.369, - 5.547 );
  56. box6.rotation.set( 0, 0.516, 0 );
  57. box6.scale.set( 3.875, 3.487, 2.986 );
  58. this.add( box6 );
  59. // -x right
  60. const light1 = new Mesh( geometry, createAreaLightMaterial( 50 ) );
  61. light1.position.set( - 16.116, 14.37, 8.208 );
  62. light1.scale.set( 0.1, 2.428, 2.739 );
  63. this.add( light1 );
  64. // -x left
  65. const light2 = new Mesh( geometry, createAreaLightMaterial( 50 ) );
  66. light2.position.set( - 16.109, 18.021, - 8.207 );
  67. light2.scale.set( 0.1, 2.425, 2.751 );
  68. this.add( light2 );
  69. // +x
  70. const light3 = new Mesh( geometry, createAreaLightMaterial( 17 ) );
  71. light3.position.set( 14.904, 12.198, - 1.832 );
  72. light3.scale.set( 0.15, 4.265, 6.331 );
  73. this.add( light3 );
  74. // +z
  75. const light4 = new Mesh( geometry, createAreaLightMaterial( 43 ) );
  76. light4.position.set( - 0.462, 8.89, 14.520 );
  77. light4.scale.set( 4.38, 5.441, 0.088 );
  78. this.add( light4 );
  79. // -z
  80. const light5 = new Mesh( geometry, createAreaLightMaterial( 20 ) );
  81. light5.position.set( 3.235, 11.486, - 12.541 );
  82. light5.scale.set( 2.5, 2.0, 0.1 );
  83. this.add( light5 );
  84. // +y
  85. const light6 = new Mesh( geometry, createAreaLightMaterial( 100 ) );
  86. light6.position.set( 0.0, 20.0, 0.0 );
  87. light6.scale.set( 1.0, 0.1, 1.0 );
  88. this.add( light6 );
  89. }
  90. dispose() {
  91. const resources = new Set();
  92. this.traverse( ( object ) => {
  93. if ( object.isMesh ) {
  94. resources.add( object.geometry );
  95. resources.add( object.material );
  96. }
  97. } );
  98. for ( const resource of resources ) {
  99. resource.dispose();
  100. }
  101. }
  102. }
  103. function createAreaLightMaterial( intensity ) {
  104. const material = new MeshBasicMaterial();
  105. material.color.setScalar( intensity );
  106. return material;
  107. }
  108. export { RoomEnvironment };