threejs-lights-directional-w-helper.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 - Lights - Directional w/Helper</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. 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.rotation.x = Math.PI * -.5;
  59. scene.add(mesh);
  60. }
  61. {
  62. const cubeSize = 4;
  63. const cubeGeo = new THREE.BoxBufferGeometry(cubeSize, cubeSize, cubeSize);
  64. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  65. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  66. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  67. scene.add(mesh);
  68. }
  69. {
  70. const sphereRadius = 3;
  71. const sphereWidthDivisions = 32;
  72. const sphereHeightDivisions = 16;
  73. const sphereGeo = new THREE.SphereBufferGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  74. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  75. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  76. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  77. scene.add(mesh);
  78. }
  79. class ColorGUIHelper {
  80. constructor(object, prop) {
  81. this.object = object;
  82. this.prop = prop;
  83. }
  84. get value() {
  85. return `#${this.object[this.prop].getHexString()}`;
  86. }
  87. set value(hexString) {
  88. this.object[this.prop].set(hexString);
  89. }
  90. }
  91. function makeXYZGUI(gui, vector3, name, onChangeFn) {
  92. const folder = gui.addFolder(name);
  93. folder.add(vector3, 'x', -10, 10).onChange(onChangeFn);
  94. folder.add(vector3, 'y', 0, 10).onChange(onChangeFn);
  95. folder.add(vector3, 'z', -10, 10).onChange(onChangeFn);
  96. folder.open();
  97. }
  98. {
  99. const color = 0xFFFFFF;
  100. const intensity = 1;
  101. const light = new THREE.DirectionalLight(color, intensity);
  102. light.position.set(0, 10, 0);
  103. light.target.position.set(-5, 0, 0);
  104. scene.add(light);
  105. scene.add(light.target);
  106. const helper = new THREE.DirectionalLightHelper(light);
  107. scene.add(helper);
  108. function updateLight() {
  109. light.target.updateMatrixWorld();
  110. helper.update();
  111. }
  112. updateLight();
  113. const gui = new dat.GUI();
  114. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  115. gui.add(light, 'intensity', 0, 2, 0.01);
  116. makeXYZGUI(gui, light.position, 'position', updateLight);
  117. makeXYZGUI(gui, light.target.position, 'target', updateLight);
  118. }
  119. function resizeRendererToDisplaySize(renderer) {
  120. const canvas = renderer.domElement;
  121. const width = canvas.clientWidth;
  122. const height = canvas.clientHeight;
  123. const needResize = canvas.width !== width || canvas.height !== height;
  124. if (needResize) {
  125. renderer.setSize(width, height, false);
  126. }
  127. return needResize;
  128. }
  129. function render() {
  130. if (resizeRendererToDisplaySize(renderer)) {
  131. const canvas = renderer.domElement;
  132. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  133. camera.updateProjectionMatrix();
  134. }
  135. renderer.render(scene, camera);
  136. requestAnimationFrame(render);
  137. }
  138. requestAnimationFrame(render);
  139. }
  140. main();
  141. </script>
  142. </html>