threejs-shadows-point-light.html 5.9 KB

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