background.html 6.1 KB

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