threejs-lights.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. /* global THREE, threejsLessonUtils */
  3. {
  4. function makeCheckerTexture(repeats) {
  5. const data = new Uint8Array([
  6. 0x88, 0x88, 0x88, 0xCC, 0xCC, 0xCC,
  7. 0xCC, 0xCC, 0xCC, 0x88, 0x88, 0x88,
  8. ]);
  9. const width = 2;
  10. const height = 2;
  11. const texture = new THREE.DataTexture(data, width, height, THREE.RGBFormat);
  12. texture.needsUpdate = true;
  13. texture.wrapS = THREE.RepeatWrapping;
  14. texture.wrapT = THREE.RepeatWrapping;
  15. texture.repeat.set(repeats / 2, repeats / 2);
  16. return texture;
  17. }
  18. const makeScene = function() {
  19. const cubeSize = 4;
  20. const cubeGeo = new THREE.BoxBufferGeometry(cubeSize, cubeSize, cubeSize);
  21. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  22. const sphereRadius = 3;
  23. const sphereWidthDivisions = 32;
  24. const sphereHeightDivisions = 16;
  25. const sphereGeo = new THREE.SphereBufferGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  26. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  27. const planeSize = 40;
  28. const planeGeo = new THREE.PlaneBufferGeometry(planeSize, planeSize);
  29. const planeMat = new THREE.MeshPhongMaterial({
  30. map: makeCheckerTexture(planeSize),
  31. side: THREE.DoubleSide,
  32. });
  33. return function(renderInfo) {
  34. const {scene, camera, elem} = renderInfo;
  35. const controls = new THREE.OrbitControls(camera, elem);
  36. controls.enableDamping = true;
  37. controls.enablePanning = false;
  38. controls.enableKeys = 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. }