RoomEnvironment.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {
  2. BackSide,
  3. BoxGeometry,
  4. Mesh,
  5. MeshBasicMaterial,
  6. MeshStandardMaterial,
  7. PointLight,
  8. Scene,
  9. } from 'three';
  10. /**
  11. * This class represents a scene with a basic room setup that can be used as
  12. * input for {@link PMREMGenerator#fromScene}. The resulting PMREM represents the room's
  13. * lighting and can be used for Image Based Lighting by assigning it to {@link Scene#environment}
  14. * or directly as an environment map to PBR materials.
  15. *
  16. * The implementation is based on the [EnvironmentScene](https://github.com/google/model-viewer/blob/master/packages/model-viewer/src/three-components/EnvironmentScene.ts)
  17. * component from the `model-viewer` project.
  18. *
  19. * ```js
  20. * const environment = new RoomEnvironment();
  21. * const pmremGenerator = new THREE.PMREMGenerator( renderer );
  22. *
  23. * const envMap = pmremGenerator.fromScene( environment ).texture;
  24. * scene.environment = envMap;
  25. * ```
  26. *
  27. * @augments Scene
  28. * @three_import import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  29. */
  30. class RoomEnvironment extends Scene {
  31. constructor() {
  32. super();
  33. const geometry = new BoxGeometry();
  34. geometry.deleteAttribute( 'uv' );
  35. const roomMaterial = new MeshStandardMaterial( { side: BackSide } );
  36. const boxMaterial = new MeshStandardMaterial();
  37. const mainLight = new PointLight( 0xffffff, 900, 28, 2 );
  38. mainLight.position.set( 0.418, 16.199, 0.300 );
  39. this.add( mainLight );
  40. const room = new Mesh( geometry, roomMaterial );
  41. room.position.set( - 0.757, 13.219, 0.717 );
  42. room.scale.set( 31.713, 28.305, 28.591 );
  43. this.add( room );
  44. const box1 = new Mesh( geometry, boxMaterial );
  45. box1.position.set( - 10.906, 2.009, 1.846 );
  46. box1.rotation.set( 0, - 0.195, 0 );
  47. box1.scale.set( 2.328, 7.905, 4.651 );
  48. this.add( box1 );
  49. const box2 = new Mesh( geometry, boxMaterial );
  50. box2.position.set( - 5.607, - 0.754, - 0.758 );
  51. box2.rotation.set( 0, 0.994, 0 );
  52. box2.scale.set( 1.970, 1.534, 3.955 );
  53. this.add( box2 );
  54. const box3 = new Mesh( geometry, boxMaterial );
  55. box3.position.set( 6.167, 0.857, 7.803 );
  56. box3.rotation.set( 0, 0.561, 0 );
  57. box3.scale.set( 3.927, 6.285, 3.687 );
  58. this.add( box3 );
  59. const box4 = new Mesh( geometry, boxMaterial );
  60. box4.position.set( - 2.017, 0.018, 6.124 );
  61. box4.rotation.set( 0, 0.333, 0 );
  62. box4.scale.set( 2.002, 4.566, 2.064 );
  63. this.add( box4 );
  64. const box5 = new Mesh( geometry, boxMaterial );
  65. box5.position.set( 2.291, - 0.756, - 2.621 );
  66. box5.rotation.set( 0, - 0.286, 0 );
  67. box5.scale.set( 1.546, 1.552, 1.496 );
  68. this.add( box5 );
  69. const box6 = new Mesh( geometry, boxMaterial );
  70. box6.position.set( - 2.193, - 0.369, - 5.547 );
  71. box6.rotation.set( 0, 0.516, 0 );
  72. box6.scale.set( 3.875, 3.487, 2.986 );
  73. this.add( box6 );
  74. // -x right
  75. const light1 = new Mesh( geometry, createAreaLightMaterial( 50 ) );
  76. light1.position.set( - 16.116, 14.37, 8.208 );
  77. light1.scale.set( 0.1, 2.428, 2.739 );
  78. this.add( light1 );
  79. // -x left
  80. const light2 = new Mesh( geometry, createAreaLightMaterial( 50 ) );
  81. light2.position.set( - 16.109, 18.021, - 8.207 );
  82. light2.scale.set( 0.1, 2.425, 2.751 );
  83. this.add( light2 );
  84. // +x
  85. const light3 = new Mesh( geometry, createAreaLightMaterial( 17 ) );
  86. light3.position.set( 14.904, 12.198, - 1.832 );
  87. light3.scale.set( 0.15, 4.265, 6.331 );
  88. this.add( light3 );
  89. // +z
  90. const light4 = new Mesh( geometry, createAreaLightMaterial( 43 ) );
  91. light4.position.set( - 0.462, 8.89, 14.520 );
  92. light4.scale.set( 4.38, 5.441, 0.088 );
  93. this.add( light4 );
  94. // -z
  95. const light5 = new Mesh( geometry, createAreaLightMaterial( 20 ) );
  96. light5.position.set( 3.235, 11.486, - 12.541 );
  97. light5.scale.set( 2.5, 2.0, 0.1 );
  98. this.add( light5 );
  99. // +y
  100. const light6 = new Mesh( geometry, createAreaLightMaterial( 100 ) );
  101. light6.position.set( 0.0, 20.0, 0.0 );
  102. light6.scale.set( 1.0, 0.1, 1.0 );
  103. this.add( light6 );
  104. }
  105. /**
  106. * Frees internal resources. This method should be called
  107. * when the environment is no longer required.
  108. */
  109. dispose() {
  110. const resources = new Set();
  111. this.traverse( ( object ) => {
  112. if ( object.isMesh ) {
  113. resources.add( object.geometry );
  114. resources.add( object.material );
  115. }
  116. } );
  117. for ( const resource of resources ) {
  118. resource.dispose();
  119. }
  120. }
  121. }
  122. function createAreaLightMaterial( intensity ) {
  123. const material = new MeshBasicMaterial();
  124. material.color.setScalar( intensity );
  125. return material;
  126. }
  127. export { RoomEnvironment };
粤ICP备19079148号