threejs-lights-point.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 - Point</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.PointLight(color, intensity);
  102. light.position.set(0, 10, 0);
  103. scene.add(light);
  104. const helper = new THREE.PointLightHelper(light);
  105. scene.add(helper);
  106. function updateLight() {
  107. helper.update();
  108. }
  109. const gui = new dat.GUI();
  110. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  111. gui.add(light, 'intensity', 0, 2, 0.01);
  112. gui.add(light, 'distance', 0, 40).onChange(updateLight);
  113. makeXYZGUI(gui, light.position, 'position');
  114. }
  115. function resizeRendererToDisplaySize(renderer) {
  116. const canvas = renderer.domElement;
  117. const width = canvas.clientWidth;
  118. const height = canvas.clientHeight;
  119. const needResize = canvas.width !== width || canvas.height !== height;
  120. if (needResize) {
  121. renderer.setSize(width, height, false);
  122. }
  123. return needResize;
  124. }
  125. function render() {
  126. if (resizeRendererToDisplaySize(renderer)) {
  127. const canvas = renderer.domElement;
  128. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  129. camera.updateProjectionMatrix();
  130. }
  131. renderer.render(scene, camera);
  132. requestAnimationFrame(render);
  133. }
  134. requestAnimationFrame(render);
  135. }
  136. main();
  137. </script>
  138. </html>