background-v01.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Fundamentals</title>
  8. <link href="resources/threejs-tutorials.css" rel="stylesheet" />
  9. <style>
  10. html, body {
  11. margin: 0;
  12. height: 100%;
  13. }
  14. canvas {
  15. width: 100%;
  16. height: 100%;
  17. display: block;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <canvas id="c"></canvas>
  23. </body>
  24. <script type="module">
  25. import * as THREE from '../../build/three.module.js';
  26. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  27. import {GLTFLoader} from '../../examples/jsm/loaders/GLTFLoader.js';
  28. function main() {
  29. const canvas = document.querySelector('#c');
  30. const renderer = new THREE.WebGLRenderer({canvas});
  31. const scene = new THREE.Scene();
  32. const aspect = 2; // the canvas default
  33. const fov = 35;
  34. const near = 0.1;
  35. const far = 5000;
  36. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  37. camera.position.x = -0.;
  38. camera.position.y = 350;
  39. camera.position.z = 40.;
  40. const useFog = true;
  41. const useOrbitCamera = false;
  42. const showHelpers = false;
  43. const camSpeed = 0.2;
  44. if (useOrbitCamera) {
  45. const controls = new OrbitControls(camera, canvas);
  46. controls.target.set(0, 100.01, 0.2);
  47. controls.update();
  48. }
  49. renderer.outputEncoding = THREE.GammaEncoding;
  50. renderer.shadowMap.enabled = true;
  51. const hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 0.6);
  52. hemiLight.color.setHSL(0.6, 1, 0.6);
  53. hemiLight.groundColor.setHSL(0.095, 1, 0.75);
  54. hemiLight.position.set(0, 50, 0);
  55. scene.add(hemiLight);
  56. if (showHelpers) {
  57. const hemiLightHelper = new THREE.HemisphereLightHelper(hemiLight, 10);
  58. scene.add(hemiLightHelper);
  59. }
  60. const dirLight = new THREE.DirectionalLight(0xffffff, 1);
  61. dirLight.color.setHSL(0.1, 1, 0.95);
  62. dirLight.position.set(-300, 220, 245);
  63. scene.add(dirLight);
  64. dirLight.castShadow = true;
  65. dirLight.shadow.mapSize.width = 2048;
  66. dirLight.shadow.mapSize.height = 2048;
  67. const d = 350;
  68. dirLight.shadow.camera.left = -d;
  69. dirLight.shadow.camera.right = d;
  70. dirLight.shadow.camera.top = d;
  71. dirLight.shadow.camera.bottom = -d;
  72. dirLight.shadow.camera.near = 100;
  73. dirLight.shadow.camera.far = 950;
  74. dirLight.shadow.bias = -0.005;
  75. if (showHelpers) {
  76. const dirLightHeper = new THREE.DirectionalLightHelper(dirLight, 10);
  77. scene.add(dirLightHeper);
  78. }
  79. const loader = new GLTFLoader();
  80. const camRadius = 600;
  81. const camHeight = 160;
  82. const camTarget = [0, 30, 0];
  83. const fogNear = 1350;
  84. const fogFar = 1500;
  85. loader.load('resources/models/mountain_landscape/scene.gltf', (gltf) => {
  86. gltf.scene.traverse((child) => {
  87. if ( child.isMesh ) {
  88. child.castShadow = true;
  89. child.receiveShadow = true;
  90. }
  91. });
  92. scene.add(gltf.scene);
  93. });
  94. window.s = scene;
  95. if (useFog) {
  96. const vertexShader = `
  97. varying vec3 vWorldPosition;
  98. void main() {
  99. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  100. vWorldPosition = worldPosition.xyz;
  101. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  102. }
  103. `;
  104. const fragmentShader = `
  105. uniform vec3 topColor;
  106. uniform vec3 bottomColor;
  107. uniform float offset;
  108. uniform float exponent;
  109. varying vec3 vWorldPosition;
  110. void main() {
  111. float h = normalize( vWorldPosition + offset ).y;
  112. gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( max( h , 0.0), exponent ), 0.0 ) ), 1.0 );
  113. }
  114. `;
  115. const uniforms = {
  116. topColor: { value: new THREE.Color(0x88AABB) },
  117. bottomColor: { value: new THREE.Color(0xEFCB7F) },
  118. offset: { value: 730 },
  119. exponent: { value: 0.3 },
  120. };
  121. uniforms.topColor.value.copy(hemiLight.color);
  122. scene.fog = new THREE.Fog(scene.background, fogNear, fogFar);
  123. scene.fog.color.copy(uniforms.bottomColor.value);
  124. const skyGeo = new THREE.SphereGeometry(4000, 32, 15);
  125. const skyMat = new THREE.ShaderMaterial( { vertexShader: vertexShader, fragmentShader: fragmentShader, uniforms: uniforms, side: THREE.BackSide } );
  126. const sky = new THREE.Mesh( skyGeo, skyMat );
  127. scene.add(sky);
  128. }
  129. function resizeRendererToDisplaySize(renderer) {
  130. const canvas = renderer.domElement;
  131. const width = canvas.clientWidth;
  132. const height = canvas.clientHeight;
  133. if (width === canvas.width && height === canvas.height) {
  134. return false;
  135. }
  136. renderer.setSize(width, height, false);
  137. return true;
  138. }
  139. function render(time) {
  140. time *= 0.001;
  141. time += 80;
  142. if (resizeRendererToDisplaySize(renderer)) {
  143. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  144. camera.updateProjectionMatrix();
  145. }
  146. if (!useOrbitCamera) {
  147. const angle = Math.sin(time * camSpeed) + Math.PI * .75;
  148. camera.position.set(Math.cos(angle) * camRadius, camHeight, Math.sin(angle) * camRadius);
  149. camera.lookAt(...camTarget);
  150. }
  151. renderer.render(scene, camera);
  152. requestAnimationFrame(render);
  153. }
  154. requestAnimationFrame(render);
  155. }
  156. main();
  157. </script>
  158. </html>