background.html 6.3 KB

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