background.html 6.4 KB

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