threejs-lights.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, 0xFF, 0xCC, 0xCC, 0xCC, 0xFF,
  8. 0xCC, 0xCC, 0xCC, 0xFF, 0x88, 0x88, 0x88, 0xFF
  9. ]);
  10. const width = 2;
  11. const height = 2;
  12. const texture = new THREE.DataTexture(data, width, height);
  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. scene.background = new THREE.Color('black');
  40. {
  41. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  42. mesh.position.set(cubeSize + 1, cubeSize / 2, -cubeSize - 1);
  43. scene.add(mesh);
  44. }
  45. {
  46. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  47. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, -sphereRadius + 1);
  48. scene.add(mesh);
  49. }
  50. {
  51. const mesh = new THREE.Mesh(planeGeo, planeMat);
  52. mesh.rotation.x = Math.PI * -.5;
  53. scene.add(mesh);
  54. }
  55. return {
  56. trackball: false,
  57. lights: false,
  58. update() {
  59. controls.update();
  60. },
  61. };
  62. };
  63. }();
  64. threejsLessonUtils.addDiagrams({
  65. directionalOnly: {
  66. create(props) {
  67. const {scene, renderInfo} = props;
  68. const result = makeScene(renderInfo);
  69. {
  70. const light = new THREE.DirectionalLight(0xFFFFFF, 1);
  71. light.position.set(5, 10, 0);
  72. scene.add(light);
  73. }
  74. {
  75. const light = new THREE.AmbientLight(0xFFFFFF, .6);
  76. scene.add(light);
  77. }
  78. return result;
  79. },
  80. },
  81. directionalPlusHemisphere: {
  82. create(props) {
  83. const {scene, renderInfo} = props;
  84. const result = makeScene(renderInfo);
  85. {
  86. const light = new THREE.DirectionalLight(0xFFFFFF, 1);
  87. light.position.set(5, 10, 0);
  88. scene.add(light);
  89. }
  90. {
  91. const skyColor = 0xB1E1FF; // light blue
  92. const groundColor = 0xB97A20; // brownish orange
  93. const intensity = .6;
  94. const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  95. scene.add(light);
  96. }
  97. return result;
  98. },
  99. },
  100. });
  101. }