background.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.set( - 580, 55, 390 );
  46. const maxFovX = 40;
  47. const numBirds = 40;
  48. const minMax = 700;
  49. const birdSpeed = 100;
  50. const useFog = true;
  51. const useOrbitCamera = true;
  52. const showHelpers = false;
  53. if ( useOrbitCamera ) {
  54. const controls = new OrbitControls( camera, canvas );
  55. controls.target.set( 0, 0, 0 );
  56. controls.update();
  57. }
  58. renderer.shadowMap.enabled = true;
  59. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0xffffff, 2 );
  60. hemiLight.color.setHSL( 0.6, 1, 0.5 );
  61. hemiLight.groundColor.setHSL( 0.095, 1, 0.5 );
  62. hemiLight.position.set( 0, 50, 0 );
  63. scene.add( hemiLight );
  64. if ( showHelpers ) {
  65. const hemiLightHelper = new THREE.HemisphereLightHelper( hemiLight, 10 );
  66. scene.add( hemiLightHelper );
  67. }
  68. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  69. dirLight.color.setHSL( 0.1, 1, 0.95 );
  70. dirLight.position.set( - 300, 220, 245 );
  71. scene.add( dirLight );
  72. dirLight.castShadow = true;
  73. dirLight.shadow.mapSize.width = 2048;
  74. dirLight.shadow.mapSize.height = 2048;
  75. const d = 350;
  76. dirLight.shadow.camera.left = - d;
  77. dirLight.shadow.camera.right = d;
  78. dirLight.shadow.camera.top = d;
  79. dirLight.shadow.camera.bottom = - d;
  80. dirLight.shadow.camera.near = 100;
  81. dirLight.shadow.camera.far = 950;
  82. dirLight.shadow.bias = - 0.005;
  83. if ( showHelpers ) {
  84. const dirLightHeper = new THREE.DirectionalLightHelper( dirLight, 10 );
  85. scene.add( dirLightHeper );
  86. }
  87. const birds = [];
  88. const loader = new GLTFLoader();
  89. const fogNear = 1350;
  90. const fogFar = 1500;
  91. function rand( min, max ) {
  92. if ( min === undefined ) {
  93. min = 0;
  94. max = 1;
  95. } else if ( max === undefined ) {
  96. max = min;
  97. min = 0;
  98. }
  99. return min + Math.random() * ( max - min );
  100. }
  101. loader.load( 'resources/models/flamingo/Flamingo.glb', ( gltf ) => {
  102. const orig = gltf.scene.children[ 0 ];
  103. orig.castShadow = true;
  104. orig.receiveShadow = true;
  105. for ( let i = 0; i < numBirds; ++ i ) {
  106. const u = i / ( numBirds - 1 );
  107. const mesh = orig.clone();
  108. mesh.position.set(
  109. rand( - 150, 150 ),
  110. ( u * 2 - 1 ) * 200,
  111. ( minMax * 2 * i * 1.7 ) % ( minMax * 2 ) - minMax / 2,
  112. );
  113. scene.add( mesh );
  114. mesh.material = mesh.material.clone();
  115. mesh.material.color.setHSL( rand(), 1, 0.8 );
  116. const mixer = new THREE.AnimationMixer( mesh );
  117. mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 1 ).play();
  118. mixer.update( rand( 10 ) );
  119. mixer.timeScale = rand( 0.9, 1.1 );
  120. birds.push( {
  121. mixer,
  122. mesh,
  123. } );
  124. }
  125. } );
  126. window.s = scene;
  127. if ( useFog ) {
  128. const vertexShader = `
  129. varying vec3 vWorldPosition;
  130. void main() {
  131. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  132. vWorldPosition = worldPosition.xyz;
  133. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  134. }
  135. `;
  136. const fragmentShader = `
  137. uniform vec3 topColor;
  138. uniform vec3 bottomColor;
  139. uniform float offset;
  140. uniform float exponent;
  141. varying vec3 vWorldPosition;
  142. void main() {
  143. float h = normalize( vWorldPosition + offset ).y;
  144. gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( max( h , 0.0), exponent ), 0.0 ) ), 1.0 );
  145. }
  146. `;
  147. const uniforms = {
  148. topColor: { value: new THREE.Color( 0x88AABB ) },
  149. bottomColor: { value: new THREE.Color( 0xEFCB7F ) },
  150. offset: { value: 730 },
  151. exponent: { value: 0.3 },
  152. };
  153. uniforms.topColor.value.copy( hemiLight.color );
  154. scene.fog = new THREE.Fog( scene.background, fogNear, fogFar );
  155. scene.fog.color.copy( uniforms.bottomColor.value );
  156. const skyGeo = new THREE.SphereGeometry( 3000, 32, 15 );
  157. const skyMat = new THREE.ShaderMaterial( { vertexShader: vertexShader, fragmentShader: fragmentShader, uniforms: uniforms, side: THREE.BackSide } );
  158. const sky = new THREE.Mesh( skyGeo, skyMat );
  159. scene.add( sky );
  160. }
  161. function resizeRendererToDisplaySize( renderer ) {
  162. const canvas = renderer.domElement;
  163. const width = canvas.clientWidth;
  164. const height = canvas.clientHeight;
  165. if ( width === canvas.width && height === canvas.height ) {
  166. return false;
  167. }
  168. renderer.setSize( width, height, false );
  169. return true;
  170. }
  171. let then = 0;
  172. function render( now ) {
  173. now *= 0.001;
  174. const deltaTime = now - then;
  175. then = now;
  176. if ( resizeRendererToDisplaySize( renderer ) ) {
  177. const aspect = canvas.clientWidth / canvas.clientHeight;
  178. const fovX = THREE.MathUtils.radToDeg( 2 * Math.atan( Math.tan( THREE.MathUtils.degToRad( fov ) * 0.5 ) * aspect ) );
  179. const newFovY = THREE.MathUtils.radToDeg( 2 * Math.atan( Math.tan( THREE.MathUtils.degToRad( maxFovX ) * .5 ) / aspect ) );
  180. camera.fov = fovX > maxFovX ? newFovY : fov;
  181. camera.aspect = aspect;
  182. camera.updateProjectionMatrix();
  183. }
  184. for ( const { mesh, mixer } of birds ) {
  185. mixer.update( deltaTime );
  186. mesh.position.z = ( mesh.position.z + minMax + mixer.timeScale * birdSpeed * deltaTime ) % ( minMax * 2 ) - minMax;
  187. }
  188. renderer.render( scene, camera );
  189. requestAnimationFrame( render );
  190. }
  191. requestAnimationFrame( render );
  192. }
  193. main();
  194. </script>
  195. </html>