mathhelpers.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import * as THREE from 'three'
  2. export function getForwardVector(object3d) {
  3. const up = object3d.up.clone()
  4. const forwardVector = new THREE.Vector3(0,0,0)
  5. forwardVector.setFromMatrixColumn(object3d.matrix, 0)
  6. forwardVector.crossVectors(up.multiplyScalar(-1), forwardVector)
  7. return forwardVector.normalize()
  8. }
  9. export function getRightVector(object3d) {
  10. const up = object3d.up.clone()
  11. const rightVector = new THREE.Vector3(0,0,0)
  12. rightVector.setFromMatrixColumn(object3d.matrix, 0)
  13. rightVector.crossVectors(up.multiplyScalar(-1), rightVector)
  14. rightVector.crossVectors(object3d.up, rightVector)
  15. return rightVector.normalize()
  16. }
  17. export function drawDebugLine(game, startPoint, endPoint, color = 0xff0000, delay = 0) {
  18. const points = []
  19. points.push(startPoint)
  20. points.push(endPoint)
  21. const geometry = new THREE.BufferGeometry().setFromPoints( points );
  22. const material = new THREE.LineBasicMaterial( { color } );
  23. const line = new THREE.Line( geometry, material )
  24. game.scene.add(line)
  25. if(delay > 0) {
  26. setTimeout(() => {
  27. game.scene.remove(line)
  28. }, delay)
  29. }
  30. }