【发布时间】:2015-01-17 15:31:31
【问题描述】:
我一直在尽最大努力获得 RiggedHand 网格呈现的位置,以便在 THREEJS 场景中准确地创建小球体。我正在尝试在给定指尖的位置执行此操作。 X 和 Y 似乎是正确的,但 Z 几乎总是关闭。更重要的是,它离中心越远,偏斜就越明显(即 X 和 Y 也偏离了)。
我能做的最好的事情就是使用以下代码找出“正确”坐标:
var handMesh = hand.data('riggedHand.mesh');
//This is the best I've gotten so far with the position of the fingertip
var position = handMesh.fingers[1].tip.getWorldPosition();
不太确定最好的方法是什么,但到目前为止我已经尝试了几种。转换hand.finger[x].tipPosition,使用interactionbox并乘以windowsize来尝试得到一个准确的位置。我开始怀疑这是否与我在 THREEJS 中使用 PerspectiveCamera 并且我需要以某种方式补偿它(?)这一事实有更多关系 - 真的不知道该怎么做,但希望有人能指出我在正确的方向。
使用上述代码的完整sn-p示例代码如下:
var renderer, scene, camera, riggedHandPlugin, fadingSpheres
var sphereTTL = 7;
function initScene() {
camera = new THREE.PerspectiveCamera(80, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 200;
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0xcccccc, 0.002);
var light = new THREE.DirectionalLight(0xffffff);
light.position.set(1, 1, 1);
scene.add(light);
// renderer
renderer = new THREE.WebGLRenderer({ antialias: false });
renderer.setSize(window.innerWidth, window.innerHeight);
container = document.getElementById('container');
container.appendChild(renderer.domElement);
fadingSpheres = [];
}
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
if (fadingSpheres) {
fadingSpheres.forEach(removeDeadSpheres);
}
}
function FadingSphere(position, size, meshColor) {
//Draw the sphere at the position of the indexfinger tip position
var geometry = new THREE.SphereGeometry(3, 8, 8);
var material = new THREE.MeshLambertMaterial({ color: meshColor});
var mesh = new THREE.Mesh(geometry, material);
mesh.material.ambient = mesh.material.color;
mesh.position.x = position.x;
mesh.position.y = position.y;
mesh.position.z = position.z;
this.sphere = mesh;
scene.add(this.sphere);
fadingSpheres.push(this);
this.ttl = sphereTTL;
this.updateToRemove = function () {
this.ttl--;
return (this.ttl <= 0);
}
}
function removeDeadSpheres(fadingSphere, number, array) {
if (fadingSphere) {
if (fadingSphere.updateToRemove()) {
scene.remove(fadingSphere.sphere);
var index = array.indexOf(fadingSphere);
array.splice(index, 1);
}
}
}
//Within the leap draw loop
//Leap loop to call drawing functions
Leap.loop(
function (frame) {
frame.hands.forEach(
function (hand) {
var handMesh = hand.data('riggedHand.mesh');
function createSphereAtFingerTip(fingerIndex, colorHex) {
new FadingSphere(handMesh.fingers[fingerIndex].tip.getWorldPosition(), 3, colorHex);
}
createSphereAtFingerTip(0, 0xF57E20) //Thumb
createSphereAtFingerTip(1, 0xFFCC00) //Index
createSphereAtFingerTip(2, 0xCCCC51) //Middle
createSphereAtFingerTip(3, 0x8FB258) //Ring
createSphereAtFingerTip(4, 0x336699) //pinky
}
)
}
)
.use('riggedHand')
.use('handEntry')
riggedHandPlugin = Leap.loopController.plugins.riggedHand;
initScene();
render();
将不胜感激任何可能有帮助的建议/建议。
【问题讨论】:
标签: 3d three.js coordinates leap-motion