UNPKG

trip.three

Version:
44 lines (35 loc) 1.37 kB
'use strict'; var THREE = require('three'); var decimalAdjust = require('./decimalAdjust'); module.exports = function (width, height, camera, position) { var mouseX = position.x / width * 2 - 1; var mouseY = -(position.y / height) * 2 + 1; if (camera instanceof THREE.PerspectiveCamera) { var vector = new THREE.Vector3(mouseX, mouseY, 0.5); return vector.unproject(camera); } else if (camera instanceof THREE.OrthographicCamera) { var _vector = new THREE.Vector3(mouseX, mouseY, -1); var worldPos = _vector.unproject(camera); var q = camera.quaternion; var x = decimalAdjust(q.x, -3); var y = decimalAdjust(q.y, -3); var z = decimalAdjust(q.z, -3); var w = decimalAdjust(q.w, -3); var xPos = x === 0.5 && y === 0.5 && z === 0.5 && w === 0.5; var xNeg = x === -0.5 && y === 0.5 && z === 0.5 && w === -0.5; var yPos = x === 0 && y === 0.707 && z === 0.707 && w === 0; var yNeg = x === 0.707 && y === 0 && z === 0 && w === 0.707; var zPos = x === 0 && y === 0 && z === 0 && w === 1; var zNeg = x === 0 && y === 1 && z === 0 && w === 0; if (xPos || xNeg) { worldPos.x = 0; } else if (yPos || yNeg) { worldPos.y = 0; } else if (zPos || zNeg) { worldPos.z = 0; } else { console.warn('unsupported ortho camera for snapping'); } return worldPos; } };