threejs-shadows-fake.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 - Shadows - Fake</title>
  8. <style>
  9. html, body {
  10. margin: 0;
  11. height: 100%;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="module">
  24. import * as THREE from './resources/threejs/r127/build/three.module.js';
  25. function main() {
  26. const canvas = document.querySelector('#c');
  27. const renderer = new THREE.WebGLRenderer({canvas});
  28. renderer.physicallyCorrectLights = true;
  29. const fov = 45;
  30. const aspect = 2; // the canvas default
  31. const near = 0.1;
  32. const far = 100;
  33. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  34. camera.position.set(0, 10, 20);
  35. camera.lookAt(0, 0, 0);
  36. const scene = new THREE.Scene();
  37. scene.background = new THREE.Color('white');
  38. const loader = new THREE.TextureLoader();
  39. {
  40. const planeSize = 40;
  41. const texture = loader.load('resources/images/checker.png');
  42. texture.wrapS = THREE.RepeatWrapping;
  43. texture.wrapT = THREE.RepeatWrapping;
  44. texture.magFilter = THREE.NearestFilter;
  45. const repeats = planeSize / 2;
  46. texture.repeat.set(repeats, repeats);
  47. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  48. const planeMat = new THREE.MeshBasicMaterial({
  49. map: texture,
  50. side: THREE.DoubleSide,
  51. });
  52. planeMat.color.setRGB(1.5, 1.5, 1.5);
  53. const mesh = new THREE.Mesh(planeGeo, planeMat);
  54. mesh.rotation.x = Math.PI * -.5;
  55. scene.add(mesh);
  56. }
  57. const shadowTexture = loader.load('resources/images/roundshadow.png');
  58. const sphereShadowBases = [];
  59. {
  60. const sphereRadius = 1;
  61. const sphereWidthDivisions = 32;
  62. const sphereHeightDivisions = 16;
  63. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  64. const planeSize = 1;
  65. const shadowGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  66. const numSpheres = 15;
  67. for (let i = 0; i < numSpheres; ++i) {
  68. // make a base for the shadow and the sphere.
  69. // so they move together.
  70. const base = new THREE.Object3D();
  71. scene.add(base);
  72. // add the shadow to the base
  73. // note: we make a new material for each sphere
  74. // so we can set that sphere's material transparency
  75. // separately.
  76. const shadowMat = new THREE.MeshBasicMaterial({
  77. map: shadowTexture,
  78. transparent: true, // so we can see the ground
  79. depthWrite: false, // so we don't have to sort
  80. });
  81. const shadowMesh = new THREE.Mesh(shadowGeo, shadowMat);
  82. shadowMesh.position.y = 0.001; // so we're above the ground slightly
  83. shadowMesh.rotation.x = Math.PI * -.5;
  84. const shadowSize = sphereRadius * 4;
  85. shadowMesh.scale.set(shadowSize, shadowSize, shadowSize);
  86. base.add(shadowMesh);
  87. // add the sphere to the base
  88. const u = i / numSpheres;
  89. const sphereMat = new THREE.MeshPhongMaterial();
  90. sphereMat.color.setHSL(u, 1, .75);
  91. const sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
  92. sphereMesh.position.set(0, sphereRadius + 2, 0);
  93. base.add(sphereMesh);
  94. // remember all 3 plus the y position
  95. sphereShadowBases.push({base, sphereMesh, shadowMesh, y: sphereMesh.position.y});
  96. }
  97. }
  98. {
  99. const skyColor = 0xB1E1FF; // light blue
  100. const groundColor = 0xB97A20; // brownish orange
  101. const intensity = 2;
  102. const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  103. scene.add(light);
  104. }
  105. {
  106. const color = 0xFFFFFF;
  107. const intensity = 1;
  108. const light = new THREE.DirectionalLight(color, intensity);
  109. light.position.set(0, 10, 5);
  110. light.target.position.set(-5, 0, 0);
  111. scene.add(light);
  112. scene.add(light.target);
  113. }
  114. function resizeRendererToDisplaySize(renderer) {
  115. const canvas = renderer.domElement;
  116. const width = canvas.clientWidth;
  117. const height = canvas.clientHeight;
  118. const needResize = canvas.width !== width || canvas.height !== height;
  119. if (needResize) {
  120. renderer.setSize(width, height, false);
  121. }
  122. return needResize;
  123. }
  124. function render(time) {
  125. time *= 0.001; // convert to seconds
  126. resizeRendererToDisplaySize(renderer);
  127. {
  128. const canvas = renderer.domElement;
  129. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  130. camera.updateProjectionMatrix();
  131. }
  132. sphereShadowBases.forEach((sphereShadowBase, ndx) => {
  133. const {base, sphereMesh, shadowMesh, y} = sphereShadowBase;
  134. // u is a value that goes from 0 to 1 as we iterate the spheres
  135. const u = ndx / sphereShadowBases.length;
  136. // compute a position for there base. This will move
  137. // both the sphere and its shadow
  138. const speed = time * .2;
  139. const angle = speed + u * Math.PI * 2 * (ndx % 1 ? 1 : -1);
  140. const radius = Math.sin(speed - ndx) * 10;
  141. base.position.set(Math.cos(angle) * radius, 0, Math.sin(angle) * radius);
  142. // yOff is a value that goes from 0 to 1
  143. const yOff = Math.abs(Math.sin(time * 2 + ndx));
  144. // move the sphere up and down
  145. sphereMesh.position.y = y + THREE.MathUtils.lerp(-2, 2, yOff);
  146. // fade the shadow as the sphere goes up
  147. shadowMesh.material.opacity = THREE.MathUtils.lerp(1, .25, yOff);
  148. });
  149. renderer.render(scene, camera);
  150. requestAnimationFrame(render);
  151. }
  152. requestAnimationFrame(render);
  153. }
  154. main();
  155. </script>
  156. </html>