background-v01.html 5.2 KB

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