background-v01.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 './resources/threejs/r110/build/three.module.js';
  26. import {OrbitControls} from './resources/threejs/r110/examples/jsm/controls/OrbitControls.js';
  27. import {GLTFLoader} from './resources/threejs/r110/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.gammaInput = true;
  50. renderer.gammaOutput = true;
  51. renderer.shadowMap.enabled = true;
  52. const hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 0.6);
  53. hemiLight.color.setHSL(0.6, 1, 0.6);
  54. hemiLight.groundColor.setHSL(0.095, 1, 0.75);
  55. hemiLight.position.set(0, 50, 0);
  56. scene.add(hemiLight);
  57. if (showHelpers) {
  58. const hemiLightHelper = new THREE.HemisphereLightHelper(hemiLight, 10);
  59. scene.add(hemiLightHelper);
  60. }
  61. const dirLight = new THREE.DirectionalLight(0xffffff, 1);
  62. dirLight.color.setHSL(0.1, 1, 0.95);
  63. dirLight.position.set(-300, 220, 245);
  64. scene.add(dirLight);
  65. dirLight.castShadow = true;
  66. dirLight.shadow.mapSize.width = 2048;
  67. dirLight.shadow.mapSize.height = 2048;
  68. const d = 350;
  69. dirLight.shadow.camera.left = -d;
  70. dirLight.shadow.camera.right = d;
  71. dirLight.shadow.camera.top = d;
  72. dirLight.shadow.camera.bottom = -d;
  73. dirLight.shadow.camera.near = 100;
  74. dirLight.shadow.camera.far = 950;
  75. dirLight.shadow.bias = -0.005;
  76. if (showHelpers) {
  77. const dirLightHeper = new THREE.DirectionalLightHelper(dirLight, 10);
  78. scene.add(dirLightHeper);
  79. }
  80. const loader = new GLTFLoader();
  81. const camRadius = 600;
  82. const camHeight = 160;
  83. const camTarget = [0, 30, 0];
  84. const fogNear = 1350;
  85. const fogFar = 1500;
  86. loader.load('resources/models/mountain_landscape/scene.gltf', (gltf) => {
  87. gltf.scene.traverse((child) => {
  88. if ( child.isMesh ) {
  89. child.castShadow = true;
  90. child.receiveShadow = true;
  91. }
  92. });
  93. scene.add(gltf.scene);
  94. });
  95. window.s = scene;
  96. if (useFog) {
  97. const vertexShader = `
  98. varying vec3 vWorldPosition;
  99. void main() {
  100. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  101. vWorldPosition = worldPosition.xyz;
  102. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  103. }
  104. `;
  105. const fragmentShader = `
  106. uniform vec3 topColor;
  107. uniform vec3 bottomColor;
  108. uniform float offset;
  109. uniform float exponent;
  110. varying vec3 vWorldPosition;
  111. void main() {
  112. float h = normalize( vWorldPosition + offset ).y;
  113. gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( max( h , 0.0), exponent ), 0.0 ) ), 1.0 );
  114. }
  115. `;
  116. const uniforms = {
  117. topColor: { value: new THREE.Color(0x88AABB) },
  118. bottomColor: { value: new THREE.Color(0xEFCB7F) },
  119. offset: { value: 730 },
  120. exponent: { value: 0.3 },
  121. };
  122. uniforms.topColor.value.copy(hemiLight.color);
  123. scene.fog = new THREE.Fog(scene.background, fogNear, fogFar);
  124. scene.fog.color.copy(uniforms.bottomColor.value);
  125. const skyGeo = new THREE.SphereBufferGeometry(4000, 32, 15);
  126. const skyMat = new THREE.ShaderMaterial( { vertexShader: vertexShader, fragmentShader: fragmentShader, uniforms: uniforms, side: THREE.BackSide } );
  127. const sky = new THREE.Mesh( skyGeo, skyMat );
  128. scene.add(sky);
  129. }
  130. function resizeRendererToDisplaySize(renderer) {
  131. const canvas = renderer.domElement;
  132. const width = canvas.clientWidth;
  133. const height = canvas.clientHeight;
  134. if (width === canvas.width && height === canvas.height) {
  135. return false;
  136. }
  137. renderer.setSize(width, height, false);
  138. return true;
  139. }
  140. function render(time) {
  141. time *= 0.001;
  142. time += 80;
  143. if (resizeRendererToDisplaySize(renderer)) {
  144. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  145. camera.updateProjectionMatrix();
  146. }
  147. if (!useOrbitCamera) {
  148. const angle = Math.sin(time * camSpeed) + Math.PI * .75;
  149. camera.position.set(Math.cos(angle) * camRadius, camHeight, Math.sin(angle) * camRadius);
  150. camera.lookAt(...camTarget);
  151. }
  152. renderer.render(scene, camera);
  153. requestAnimationFrame(render);
  154. }
  155. requestAnimationFrame(render);
  156. }
  157. main();
  158. </script>
  159. </html>