threejs-fog.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. /* global THREE, threejsLessonUtils */
  3. {
  4. function fogExample(scene, fog) {
  5. scene.fog = fog;
  6. const width = 4;
  7. const height = 3;
  8. const depth = 10;
  9. const geometry = new THREE.BoxBufferGeometry(width, height, depth);
  10. const material = new THREE.MeshPhongMaterial({color: 'hsl(130,50%,50%)'});
  11. return new THREE.Mesh(geometry, material);
  12. }
  13. function houseScene(props, fogInHouse) {
  14. const {scene, camera} = props;
  15. camera.far = 200;
  16. const loader = new THREE.GLTFLoader();
  17. const settings = {
  18. shininess: 0,
  19. roughness: 1,
  20. metalness: 0,
  21. side: THREE.DoubleSide,
  22. };
  23. loader.load('../resources/models/simple_house_scene/scene.gltf', (gltf) => {
  24. const materials = new Set();
  25. gltf.scene.traverse((node) => {
  26. const material = node.material;
  27. if (material) {
  28. (Array.isArray(material) ? material : [material]).forEach((material) => {
  29. if (!materials.has(material)) {
  30. materials.add(material);
  31. for (const [key, value] of Object.entries(settings)) {
  32. if (material[key] !== undefined) {
  33. material[key] = value;
  34. }
  35. }
  36. if (!fogInHouse && material.name.startsWith('fogless')) {
  37. material.fog = false;
  38. }
  39. }
  40. });
  41. }
  42. });
  43. scene.add(gltf.scene);
  44. });
  45. camera.fov = 45;
  46. camera.position.set(0.4, 1, 1.7);
  47. camera.lookAt(1, 1, 0.7);
  48. const color = 0xFFFFFF;
  49. const near = 1.5;
  50. const far = 5;
  51. scene.fog = new THREE.Fog(color, near, far);
  52. const light = new THREE.PointLight(0xFFFFFF, 1);
  53. light.position.copy(camera.position);
  54. light.position.y += 0.2;
  55. scene.add(light);
  56. const target = [1, 1, 0.7];
  57. return {
  58. trackball: false,
  59. obj3D: new THREE.Object3D(),
  60. update: (time) => {
  61. camera.lookAt(target[0] + Math.sin(time * .25) * .5, target[1], target[2]);
  62. },
  63. };
  64. }
  65. threejsLessonUtils.addDiagrams({
  66. fog: {
  67. create(props) {
  68. const {scene} = props;
  69. const color = 0xFFFFFF;
  70. const near = 12;
  71. const far = 18;
  72. return fogExample(scene, new THREE.Fog(color, near, far));
  73. },
  74. },
  75. fogExp2: {
  76. create(props) {
  77. const {scene} = props;
  78. const color = 0xFFFFFF;
  79. const density = 0.1;
  80. return fogExample(scene, new THREE.FogExp2(color, density));
  81. },
  82. },
  83. fogBlueBackgroundRed: {
  84. create(props) {
  85. const {scene} = props;
  86. scene.background = new THREE.Color('#F00');
  87. const color = '#00F';
  88. const near = 12;
  89. const far = 18;
  90. return fogExample(scene, new THREE.Fog(color, near, far));
  91. },
  92. },
  93. fogBlueBackgroundBlue: {
  94. create(props) {
  95. const {scene} = props;
  96. scene.background = new THREE.Color('#00F');
  97. const color = '#00F';
  98. const near = 12;
  99. const far = 18;
  100. return fogExample(scene, new THREE.Fog(color, near, far));
  101. },
  102. },
  103. fogHouseAll: {
  104. create(props) {
  105. return houseScene(props, true);
  106. },
  107. },
  108. fogHouseInsideNoFog: {
  109. create(props) {
  110. return houseScene(props, false);
  111. },
  112. },
  113. });
  114. }