TA159

Notas, resueltos y trabajos practicos de la materia Sistemas Gráficos
Index Commits Files Refs Submodules README LICENSE
tp/src/tunnel.js (1486B)
   1 import * as THREE from 'three';
   2 
   3 export function generateTunnelGeometry(
   4     tunnelHeight = 20, tunnelWidth = 14,
   5     tunnelWallThickness = 0.5, tunnelLen = 26) {
   6 
   7     const path = new THREE.Path();
   8     path.moveTo(-tunnelWidth/2, 0);
   9     path.lineTo(-tunnelWidth/2, tunnelHeight*1/3);
  10     path.moveTo(-tunnelWidth/2, tunnelHeight*1/3);
  11     path.quadraticCurveTo(0, tunnelHeight, tunnelWidth/2, tunnelHeight*1/3);
  12     path.moveTo(tunnelWidth/2, 0);
  13     path.lineTo(tunnelWidth/2, 0);
  14 
  15 
  16     // cerramos la curva con otra de la misma forma con una diferencia de
  17     // `tunnelWallThickness`
  18     path.lineTo(tunnelWidth/2-tunnelWallThickness, 0);
  19     path.moveTo(tunnelWidth/2-tunnelWallThickness, 0);
  20 
  21     path.lineTo(tunnelWidth/2-tunnelWallThickness, tunnelHeight*1/3);
  22     path.moveTo(tunnelWidth/2-tunnelWallThickness, tunnelHeight*1/3);
  23 
  24     path.quadraticCurveTo(
  25         0, tunnelHeight-(tunnelWallThickness*2),
  26         -tunnelWidth/2+tunnelWallThickness, tunnelHeight*1/3);
  27 
  28     path.lineTo(-tunnelWidth/2+tunnelWallThickness, 0);
  29     path.moveTo(-tunnelWidth/2+tunnelWallThickness, 0);
  30 
  31     path.lineTo(-tunnelWidth/2, 0);
  32     path.moveTo(-tunnelWidth/2, 0);
  33 
  34     const points = path.getPoints();
  35     const shape = new THREE.Shape(points);
  36 
  37     const extrudeSettings = {
  38         curveSegments: 24,
  39         steps: 50,
  40         depth: tunnelLen,
  41     };
  42 
  43     const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
  44 
  45     // el `1` en `x` es porque por algun motivo queda descentrado con respecto
  46     // a la vía del tren
  47     geometry.translate(1, 0, -tunnelLen/2);
  48     return geometry;
  49 }