threejs-shadows-point-light.html 5.9 KB

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