threejs-cameras.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import * as THREE from 'three';
  2. import {threejsLessonUtils} from './threejs-lesson-utils.js';
  3. {
  4. function addShape(color, geometry) {
  5. const material = new THREE.MeshPhongMaterial({color});
  6. return new THREE.Mesh(geometry, material);
  7. }
  8. threejsLessonUtils.addDiagrams({
  9. shapeCube: {
  10. create() {
  11. const width = 8;
  12. const height = 8;
  13. const depth = 8;
  14. return addShape('hsl(150,100%,40%)', new THREE.BoxGeometry(width, height, depth));
  15. },
  16. },
  17. shapeCone: {
  18. create() {
  19. const radius = 6;
  20. const height = 8;
  21. const segments = 24;
  22. return addShape('hsl(160,100%,40%)', new THREE.ConeGeometry(radius, height, segments));
  23. },
  24. },
  25. shapeCylinder: {
  26. create() {
  27. const radiusTop = 4;
  28. const radiusBottom = 4;
  29. const height = 8;
  30. const radialSegments = 24;
  31. return addShape('hsl(170,100%,40%)', new THREE.CylinderGeometry(radiusTop, radiusBottom, height, radialSegments));
  32. },
  33. },
  34. shapeSphere: {
  35. create() {
  36. const radius = 5;
  37. const widthSegments = 24;
  38. const heightSegments = 16;
  39. return addShape('hsl(180,100%,40%)', new THREE.SphereGeometry(radius, widthSegments, heightSegments));
  40. },
  41. },
  42. shapeFrustum: {
  43. create() {
  44. const width = 8;
  45. const height = 8;
  46. const depth = 8;
  47. const geometry = new THREE.BoxGeometry(width, height, depth);
  48. const perspMat = new THREE.Matrix4();
  49. perspMat.makePerspective(-3, 3, -3, 3, 4, 12);
  50. const inMat = new THREE.Matrix4();
  51. inMat.makeTranslation(0, 0, 8);
  52. const mat = new THREE.Matrix4();
  53. mat.multiply(perspMat);
  54. mat.multiply(inMat);
  55. geometry.applyMatrix4(mat);
  56. geometry.computeBoundingBox();
  57. geometry.center();
  58. geometry.scale(3, 3, 3);
  59. geometry.rotateY(Math.PI);
  60. geometry.computeVertexNormals();
  61. return addShape('hsl(190,100%,40%)', geometry);
  62. },
  63. },
  64. });
  65. }