background-v01.html 5.3 KB

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