threejs-lights-hemisphere.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 - Hemisphere</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. {
  92. const skyColor = 0xB1E1FF; // light blue
  93. const groundColor = 0xB97A20; // brownish orange
  94. const intensity = 1;
  95. const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  96. scene.add(light);
  97. const gui = new dat.GUI();
  98. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('skyColor');
  99. gui.addColor(new ColorGUIHelper(light, 'groundColor'), 'value').name('groundColor');
  100. gui.add(light, 'intensity', 0, 2, 0.01);
  101. }
  102. function resizeRendererToDisplaySize(renderer) {
  103. const canvas = renderer.domElement;
  104. const width = canvas.clientWidth;
  105. const height = canvas.clientHeight;
  106. const needResize = canvas.width !== width || canvas.height !== height;
  107. if (needResize) {
  108. renderer.setSize(width, height, false);
  109. }
  110. return needResize;
  111. }
  112. function render() {
  113. if (resizeRendererToDisplaySize(renderer)) {
  114. const canvas = renderer.domElement;
  115. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  116. camera.updateProjectionMatrix();
  117. }
  118. renderer.render(scene, camera);
  119. requestAnimationFrame(render);
  120. }
  121. requestAnimationFrame(render);
  122. }
  123. main();
  124. </script>
  125. </html>