threejs-lights.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import * as THREE from 'three';
  2. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  3. import {threejsLessonUtils} from './threejs-lesson-utils.js';
  4. {
  5. function makeCheckerTexture(repeats) {
  6. const data = new Uint8Array([
  7. 0x88, 0x88, 0x88, 0xCC, 0xCC, 0xCC,
  8. 0xCC, 0xCC, 0xCC, 0x88, 0x88, 0x88,
  9. ]);
  10. const width = 2;
  11. const height = 2;
  12. const texture = new THREE.DataTexture(data, width, height, THREE.RGBFormat);
  13. texture.needsUpdate = true;
  14. texture.wrapS = THREE.RepeatWrapping;
  15. texture.wrapT = THREE.RepeatWrapping;
  16. texture.repeat.set(repeats / 2, repeats / 2);
  17. return texture;
  18. }
  19. const makeScene = function() {
  20. const cubeSize = 4;
  21. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  22. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  23. const sphereRadius = 3;
  24. const sphereWidthDivisions = 32;
  25. const sphereHeightDivisions = 16;
  26. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  27. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  28. const planeSize = 40;
  29. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  30. const planeMat = new THREE.MeshPhongMaterial({
  31. map: makeCheckerTexture(planeSize),
  32. side: THREE.DoubleSide,
  33. });
  34. return function(renderInfo) {
  35. const {scene, camera, elem} = renderInfo;
  36. const controls = new OrbitControls(camera, elem);
  37. controls.enableDamping = true;
  38. controls.enablePanning = false;
  39. controls.enableKeys = false;
  40. scene.background = new THREE.Color('black');
  41. {
  42. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  43. mesh.position.set(cubeSize + 1, cubeSize / 2, -cubeSize - 1);
  44. scene.add(mesh);
  45. }
  46. {
  47. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  48. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, -sphereRadius + 1);
  49. scene.add(mesh);
  50. }
  51. {
  52. const mesh = new THREE.Mesh(planeGeo, planeMat);
  53. mesh.rotation.x = Math.PI * -.5;
  54. scene.add(mesh);
  55. }
  56. return {
  57. trackball: false,
  58. lights: false,
  59. update() {
  60. controls.update();
  61. },
  62. };
  63. };
  64. }();
  65. threejsLessonUtils.addDiagrams({
  66. directionalOnly: {
  67. create(props) {
  68. const {scene, renderInfo} = props;
  69. const result = makeScene(renderInfo);
  70. {
  71. const light = new THREE.DirectionalLight(0xFFFFFF, 1);
  72. light.position.set(5, 10, 0);
  73. scene.add(light);
  74. }
  75. {
  76. const light = new THREE.AmbientLight(0xFFFFFF, .6);
  77. scene.add(light);
  78. }
  79. return result;
  80. },
  81. },
  82. directionalPlusHemisphere: {
  83. create(props) {
  84. const {scene, renderInfo} = props;
  85. const result = makeScene(renderInfo);
  86. {
  87. const light = new THREE.DirectionalLight(0xFFFFFF, 1);
  88. light.position.set(5, 10, 0);
  89. scene.add(light);
  90. }
  91. {
  92. const skyColor = 0xB1E1FF; // light blue
  93. const groundColor = 0xB97A20; // brownish orange
  94. const intensity = .6;
  95. const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  96. scene.add(light);
  97. }
  98. return result;
  99. },
  100. },
  101. });
  102. }