2
0

background-v01.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. <!-- Import maps polyfill -->
  25. <!-- Remove this when import maps will be widely supported -->
  26. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  27. <script type="importmap">
  28. {
  29. "imports": {
  30. "three": "../../build/three.module.js",
  31. "three/addons/": "../../examples/jsm/"
  32. }
  33. }
  34. </script>
  35. <script type="module">
  36. import * as THREE from 'three';
  37. import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
  38. import {GLTFLoader} from 'three/addons/loaders/GLTFLoader.js';
  39. function main() {
  40. const canvas = document.querySelector('#c');
  41. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  42. const scene = new THREE.Scene();
  43. const aspect = 2; // the canvas default
  44. const fov = 35;
  45. const near = 0.1;
  46. const far = 5000;
  47. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  48. camera.position.x = -0.;
  49. camera.position.y = 350;
  50. camera.position.z = 40.;
  51. const useFog = true;
  52. const useOrbitCamera = false;
  53. const showHelpers = false;
  54. const camSpeed = 0.2;
  55. if (useOrbitCamera) {
  56. const controls = new OrbitControls(camera, canvas);
  57. controls.target.set(0, 100.01, 0.2);
  58. controls.update();
  59. }
  60. renderer.outputEncoding = THREE.sRGBEncoding;
  61. renderer.shadowMap.enabled = true;
  62. const hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 0.6);
  63. hemiLight.color.setHSL(0.6, 1, 0.6);
  64. hemiLight.groundColor.setHSL(0.095, 1, 0.75);
  65. hemiLight.position.set(0, 50, 0);
  66. scene.add(hemiLight);
  67. if (showHelpers) {
  68. const hemiLightHelper = new THREE.HemisphereLightHelper(hemiLight, 10);
  69. scene.add(hemiLightHelper);
  70. }
  71. const dirLight = new THREE.DirectionalLight(0xffffff, 1);
  72. dirLight.color.setHSL(0.1, 1, 0.95);
  73. dirLight.position.set(-300, 220, 245);
  74. scene.add(dirLight);
  75. dirLight.castShadow = true;
  76. dirLight.shadow.mapSize.width = 2048;
  77. dirLight.shadow.mapSize.height = 2048;
  78. const d = 350;
  79. dirLight.shadow.camera.left = -d;
  80. dirLight.shadow.camera.right = d;
  81. dirLight.shadow.camera.top = d;
  82. dirLight.shadow.camera.bottom = -d;
  83. dirLight.shadow.camera.near = 100;
  84. dirLight.shadow.camera.far = 950;
  85. dirLight.shadow.bias = -0.005;
  86. if (showHelpers) {
  87. const dirLightHeper = new THREE.DirectionalLightHelper(dirLight, 10);
  88. scene.add(dirLightHeper);
  89. }
  90. const loader = new GLTFLoader();
  91. const camRadius = 600;
  92. const camHeight = 160;
  93. const camTarget = [0, 30, 0];
  94. const fogNear = 1350;
  95. const fogFar = 1500;
  96. loader.load('resources/models/mountain_landscape/scene.gltf', (gltf) => {
  97. gltf.scene.traverse((child) => {
  98. if ( child.isMesh ) {
  99. child.castShadow = true;
  100. child.receiveShadow = true;
  101. }
  102. });
  103. scene.add(gltf.scene);
  104. });
  105. window.s = scene;
  106. if (useFog) {
  107. const vertexShader = `
  108. varying vec3 vWorldPosition;
  109. void main() {
  110. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  111. vWorldPosition = worldPosition.xyz;
  112. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  113. }
  114. `;
  115. const fragmentShader = `
  116. uniform vec3 topColor;
  117. uniform vec3 bottomColor;
  118. uniform float offset;
  119. uniform float exponent;
  120. varying vec3 vWorldPosition;
  121. void main() {
  122. float h = normalize( vWorldPosition + offset ).y;
  123. gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( max( h , 0.0), exponent ), 0.0 ) ), 1.0 );
  124. }
  125. `;
  126. const uniforms = {
  127. topColor: { value: new THREE.Color(0x88AABB) },
  128. bottomColor: { value: new THREE.Color(0xEFCB7F) },
  129. offset: { value: 730 },
  130. exponent: { value: 0.3 },
  131. };
  132. uniforms.topColor.value.copy(hemiLight.color);
  133. scene.fog = new THREE.Fog(scene.background, fogNear, fogFar);
  134. scene.fog.color.copy(uniforms.bottomColor.value);
  135. const skyGeo = new THREE.SphereGeometry(4000, 32, 15);
  136. const skyMat = new THREE.ShaderMaterial( { vertexShader: vertexShader, fragmentShader: fragmentShader, uniforms: uniforms, side: THREE.BackSide } );
  137. const sky = new THREE.Mesh( skyGeo, skyMat );
  138. scene.add(sky);
  139. }
  140. function resizeRendererToDisplaySize(renderer) {
  141. const canvas = renderer.domElement;
  142. const width = canvas.clientWidth;
  143. const height = canvas.clientHeight;
  144. if (width === canvas.width && height === canvas.height) {
  145. return false;
  146. }
  147. renderer.setSize(width, height, false);
  148. return true;
  149. }
  150. function render(time) {
  151. time *= 0.001;
  152. time += 80;
  153. if (resizeRendererToDisplaySize(renderer)) {
  154. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  155. camera.updateProjectionMatrix();
  156. }
  157. if (!useOrbitCamera) {
  158. const angle = Math.sin(time * camSpeed) + Math.PI * .75;
  159. camera.position.set(Math.cos(angle) * camRadius, camHeight, Math.sin(angle) * camRadius);
  160. camera.lookAt(...camTarget);
  161. }
  162. renderer.render(scene, camera);
  163. requestAnimationFrame(render);
  164. }
  165. requestAnimationFrame(render);
  166. }
  167. main();
  168. </script>
  169. </html>