2
0

background-v01.html 5.4 KB

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