0
我挣扎着一个Object3D,它的子网格应该看着相机的位置。
如果相机距离“远”,它可以正常工作,但如果相机朝向物体移动,则无法正常工作。Three.js Object3d儿童lookAt相机的位置
如果相机位置接近物体位置,则第二个添加的平面会旋转,直到相机看到平面的边缘。
而且我知道为什么这种行为只出现在第二个添加的平面上,并且恰好相机接近物体位置。
这是我到目前为止。
创建对象:
var obj = new THREE.Object3D();
obj.position.set(x, y, z);
var Uniforms = {
texturePrimary: { type: "t", value: Texture },
textureColorGraph: { type: "t", value: ColorGraph },
time: { type: "f", value: 0 },
color: { type: "f", value: 0 }
};
obj.Uniforms = Uniforms;
obj.add(makeplane1(3.2, Uniforms));
obj.add(makeplane2(25, Uniforms));
obj.update = function(pos){
this.Uniforms.time.value = shaderTiming;
$.each(this.children, function(i,mesh){
if(mesh.name === "plane1" || mesh.name === "plane2"){
var vec = mesh.parent.worldToLocal(pos);
mesh.lookAt(vec);
}
});
};
function makePlane1(radius, uniforms){
var Geo = new THREE.PlaneGeometry(radius, radius);
var Material = new THREE.ShaderMaterial(
{
uniforms: uniforms,
vertexShader: shaders[1].vertex,
fragmentShader: shaders[1].fragment,
blending: THREE.AdditiveBlending,
transparent: true
};
var plane = new THREE.Mesh(Geo, Material);
plane.name = "plane1";
return plane;
);
function makePlane2(radius, uniforms){
var Geo = new THREE.PlaneGeometry(radius, radius);
var Material = new THREE.ShaderMaterial(
{
uniforms: uniforms,
vertexShader: shaders[2].vertex,
fragmentShader: shaders[2].fragment,
blending: THREE.AdditiveBlending,
transparent: true
};
);
var plane = new THREE.Mesh(Geo, Material);
plane.name = "plane2";
return plane;
}
我可以打电话给this.lookAt(pos)
在obj.update(pos)
旋转整个对象,但其他网格不应该转动这样,所以这是可悲的选项。
和简单的顶点着色器的两个平面:
varying vec2 vUv;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
vUv = uv;
}
然后我在animationloop拨打:
$.each(scene.children, function(i, obj){
if(obj.update !== undefined) {
shaderTiming = (time - startTime)/ 1000;
obj.update(camera.getWorldPosition());
}
});
编辑:我只注意到这种行为只是发生,如果对象的位置不是(0,0,0)
。如果是这样,它就像它应该在任何相机位置一样。
另外一个简单的距离计算,相机对象无法正常工作。
vec1 = this.position;
vec2 = camera.position;
var dist = Math.sqrt(Math.pow(vec1.x - vec2.x, 2) + Math.pow(vec1.y - vec2.y, 2) + Math.pow(vec1.z - vec2.z, 2));
感谢您的任何提示。
如果'.lookAt()'不支持翻译父(S)在一般情况下,为什么像它应该的行为首先添加飞机?但很高兴知道,谢谢! –
您的问题对我来说太复杂了。 – WestLangley
@WestLangley“lookAt()不支持具有旋转和/或翻译父对象的对象。”什么时候才能解决?有没有计划修复它? – trusktr