shadows-point-light.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 - Spot Light w/CameraHelper</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. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  36. import {GUI} from '../../examples/jsm/libs/lil-gui.module.min.js';
  37. function main() {
  38. const canvas = document.querySelector('#c');
  39. const renderer = new THREE.WebGLRenderer({canvas});
  40. renderer.shadowMap.enabled = true;
  41. const fov = 45;
  42. const aspect = 2; // the canvas default
  43. const near = 0.1;
  44. const far = 100;
  45. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  46. camera.position.set(0, 10, 20);
  47. const controls = new OrbitControls(camera, canvas);
  48. controls.target.set(0, 5, 0);
  49. controls.update();
  50. const scene = new THREE.Scene();
  51. scene.background = new THREE.Color('black');
  52. {
  53. const planeSize = 40;
  54. const loader = new THREE.TextureLoader();
  55. const texture = loader.load('resources/images/checker.png');
  56. texture.wrapS = THREE.RepeatWrapping;
  57. texture.wrapT = THREE.RepeatWrapping;
  58. texture.magFilter = THREE.NearestFilter;
  59. const repeats = planeSize / 2;
  60. texture.repeat.set(repeats, repeats);
  61. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  62. const planeMat = new THREE.MeshPhongMaterial({
  63. map: texture,
  64. side: THREE.DoubleSide,
  65. });
  66. const mesh = new THREE.Mesh(planeGeo, planeMat);
  67. mesh.receiveShadow = true;
  68. mesh.rotation.x = Math.PI * -.5;
  69. scene.add(mesh);
  70. }
  71. {
  72. const cubeSize = 4;
  73. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  74. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  75. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  76. mesh.castShadow = true;
  77. mesh.receiveShadow = true;
  78. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  79. scene.add(mesh);
  80. }
  81. {
  82. const cubeSize = 30;
  83. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  84. const cubeMat = new THREE.MeshPhongMaterial({
  85. color: '#CCC',
  86. side: THREE.BackSide,
  87. });
  88. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  89. mesh.receiveShadow = true;
  90. mesh.position.set(0, cubeSize / 2 - 0.1, 0);
  91. scene.add(mesh);
  92. }
  93. {
  94. const sphereRadius = 3;
  95. const sphereWidthDivisions = 32;
  96. const sphereHeightDivisions = 16;
  97. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  98. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  99. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  100. mesh.castShadow = true;
  101. mesh.receiveShadow = true;
  102. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  103. scene.add(mesh);
  104. }
  105. class ColorGUIHelper {
  106. constructor(object, prop) {
  107. this.object = object;
  108. this.prop = prop;
  109. }
  110. get value() {
  111. return `#${this.object[this.prop].getHexString()}`;
  112. }
  113. set value(hexString) {
  114. this.object[this.prop].set(hexString);
  115. }
  116. }
  117. function makeXYZGUI(gui, vector3, name, onChangeFn) {
  118. const folder = gui.addFolder(name);
  119. folder.add(vector3, 'x', -10, 10).onChange(onChangeFn);
  120. folder.add(vector3, 'y', 0, 10).onChange(onChangeFn);
  121. folder.add(vector3, 'z', -10, 10).onChange(onChangeFn);
  122. // folder.open();
  123. }
  124. {
  125. const color = 0xFFFFFF;
  126. const intensity = 1;
  127. const light = new THREE.PointLight(color, intensity);
  128. light.castShadow = true;
  129. light.position.set(0, 10, 0);
  130. scene.add(light);
  131. const helper = new THREE.PointLightHelper(light);
  132. scene.add(helper);
  133. function updateCamera() {
  134. }
  135. class MinMaxGUIHelper {
  136. constructor(obj, minProp, maxProp, minDif) {
  137. this.obj = obj;
  138. this.minProp = minProp;
  139. this.maxProp = maxProp;
  140. this.minDif = minDif;
  141. }
  142. get min() {
  143. return this.obj[this.minProp];
  144. }
  145. set min(v) {
  146. this.obj[this.minProp] = v;
  147. this.obj[this.maxProp] = Math.max(this.obj[this.maxProp], v + this.minDif);
  148. }
  149. get max() {
  150. return this.obj[this.maxProp];
  151. }
  152. set max(v) {
  153. this.obj[this.maxProp] = v;
  154. this.min = this.min; // this will call the min setter
  155. }
  156. }
  157. const gui = new GUI();
  158. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  159. gui.add(light, 'intensity', 0, 2, 0.01);
  160. gui.add(light, 'distance', 0, 40).onChange(updateCamera);
  161. {
  162. const folder = gui.addFolder('Shadow Camera');
  163. folder.open();
  164. const minMaxGUIHelper = new MinMaxGUIHelper(light.shadow.camera, 'near', 'far', 0.1);
  165. folder.add(minMaxGUIHelper, 'min', 0.1, 50, 0.1).name('near').onChange(updateCamera);
  166. folder.add(minMaxGUIHelper, 'max', 0.1, 50, 0.1).name('far').onChange(updateCamera);
  167. }
  168. makeXYZGUI(gui, light.position, 'position', updateCamera);
  169. }
  170. function resizeRendererToDisplaySize(renderer) {
  171. const canvas = renderer.domElement;
  172. const width = canvas.clientWidth;
  173. const height = canvas.clientHeight;
  174. const needResize = canvas.width !== width || canvas.height !== height;
  175. if (needResize) {
  176. renderer.setSize(width, height, false);
  177. }
  178. return needResize;
  179. }
  180. function render() {
  181. resizeRendererToDisplaySize(renderer);
  182. {
  183. const canvas = renderer.domElement;
  184. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  185. camera.updateProjectionMatrix();
  186. }
  187. renderer.render(scene, camera);
  188. requestAnimationFrame(render);
  189. }
  190. requestAnimationFrame(render);
  191. }
  192. main();
  193. </script>
  194. </html>