background.html 6.2 KB

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