threejs-fog.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. /* global 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 = 30;
  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. obj3D: new THREE.Object3D(),
  59. update: (time) => {
  60. camera.lookAt(target[0] + Math.sin(time * .25) * .5, target[1], target[2]);
  61. },
  62. };
  63. }
  64. threejsLessonUtils.addDiagrams({
  65. fog: {
  66. create(props) {
  67. const {scene} = props;
  68. const color = 0xFFFFFF;
  69. const near = 12;
  70. const far = 18;
  71. return fogExample(scene, new THREE.Fog(color, near, far));
  72. },
  73. },
  74. fogExp2: {
  75. create(props) {
  76. const {scene} = props;
  77. const color = 0xFFFFFF;
  78. const density = 0.1;
  79. return fogExample(scene, new THREE.FogExp2(color, density));
  80. },
  81. },
  82. fogBlueBackgroundRed: {
  83. create(props) {
  84. const {scene} = props;
  85. scene.background = new THREE.Color('#F00');
  86. const color = '#00F';
  87. const near = 12;
  88. const far = 18;
  89. return fogExample(scene, new THREE.Fog(color, near, far));
  90. },
  91. },
  92. fogBlueBackgroundBlue: {
  93. create(props) {
  94. const {scene} = props;
  95. scene.background = new THREE.Color('#00F');
  96. const color = '#00F';
  97. const near = 12;
  98. const far = 18;
  99. return fogExample(scene, new THREE.Fog(color, near, far));
  100. },
  101. },
  102. fogHouseAll: {
  103. create(props) {
  104. return houseScene(props, true);
  105. },
  106. },
  107. fogHouseInsideNoFog: {
  108. create(props) {
  109. return houseScene(props, false);
  110. },
  111. },
  112. });
  113. }