Three js plane geometry disappears when rotating - javascript

I'm trying to create a very simple scene containing a triangular planar face continuously rotating about the x axis.
Here's the code creating the geometry object, as indicated in this previous SO question:
// create triangular plane geometry
var geometry_1 = new THREE.Geometry();
var v1 = new THREE.Vector3(0,0,0);
var v2 = new THREE.Vector3(3,0,0);
var v3 = new THREE.Vector3(0,3,0);
geometry_1.vertices.push(v1);
geometry_1.vertices.push(v2);
geometry_1.vertices.push(v3);
geometry_1.faces.push(new THREE.Face3(0, 1, 2));
The animation function renders the scene and adds a small rotation to the mesh:
function animate() {
requestAnimationFrame( animate );
mesh_1.rotation.x += 0.005;
renderer.render( scene, camera );
}
Everything works fine until the value of mesh.rotation.x goes into the [Math.PI, 2*Math.PI] interval, at which point it disappears for exactly half of the cycle. This JSFiddle replicates the behavior I'm observing.
This is not a light problem, as there are an ambient light and a directional light supposed to illuminate the mesh at all points of it revolution.
This should not be a material problem, as I did set its side property to THREE.DoubleSide and in fact in the interval mesh.rotation.x into [0, Math.PI] I already observe both faces.
I tried adding another face to the same geometry with geometry_1.faces.push(new THREE.Face3(0, 2, 1)); but that still didn't solve the problem.
Adding a second geometry with an opposite face geometry_2.faces.push(new THREE.Face3(0, 2, 1)); and having the mesh rotate negatively mesh_2.rotation.x -= 0.005; allows me to observe the desired result because the two geometries are now disappearing in opposite halves of the [0, 2*Math.PI] interval. This however is a hacky and not ideal solution.
So what is going on? How can I solve it? Thanks!

Documentation says:
OrthographicCamera( left, right, top, bottom, near, far )
so, set your camera like this:
camera = new THREE.OrthographicCamera(-FRUSTUM_SIDE/2, FRUSTUM_SIDE/2,
FRUSTUM_SIDE/2, -FRUSTUM_SIDE/2);
thus you'll have default near and far camera frustrum planes (0.1 and 2000).
Explanation: You set your cam at z-position, which is equal to FRUSTRUM_SIDE/2 and also you set your far camera frustrum plane with the same value. So you see everything between your cam position and the distance from it, which is FRUSTRUM_SIDE/2. In world coordinates, your far plane is at point (0, 0, 0). That's why your triangle disappears when it goes further then the distance of FRUSTRUM_SIDE/2 from your cam.

Extending the answer from #prisoner849, the problem shown in the JSFiddle has nothing to do with the geometry or the material of the mesh, but with the shape and extension of the frustum defined by the OrthographicCamera.
As explained nicely in this video tutorial and in the documentation the frustum of an OrthographicCamera is a rectangular parallelepiped defined by the values left, right, top, bottom, near, far:
The camera should effectively be thought of as being attached to the surface on the near side and facing towards negative values of the z axis.
Once the frustum's shape is defined with:
FRUSTUM_SIDE = 20;
camera = new THREE.OrthographicCamera(
-FRUSTUM_SIDE/2, FRUSTUM_SIDE/2,
FRUSTUM_SIDE/2, -FRUSTUM_SIDE/2,
-FRUSTUM_SIDE/2, FRUSTUM_SIDE/2);
we will be able to see in our image all the objects in the scene which are entirely contained in it.
However, after defining the frustum the position of the camera is changed: camera.position.z = FRUSTUM_SIDE/2; (line 24 of the fiddle). This effectively moves the whole frustum and not just the location of the image, so while previously any object in (0,0,0) was in the center of the frustum, now it will lie on the very far end plane of it.
The animation function:
function animate() {
requestAnimationFrame( animate );
mesh_1.rotation.x += 0.005;
renderer.render( scene, camera );
}
rotates the triangle towards the image plane, but for angles between [Math.PI, 2*Math.PI] the plane is effectively pointing outside of the frustum and thus becomes invisible.
Removing the camera.position.z = FRUSTUM_SIDE/2; line, or defining a different frustum, or moving the mesh position to the middle of the new frustum are all possible solutions. I.e. corrected fiddle.

Related

How to rotate a cube based on where Three.js camera is looking?

Is there a way to rotate a cube in Three.js based on the side of the cube the camera is facing?
I'm using the below code to change the orientation the cube rotates from based on the change in the vector returned by camera.getWorldDirection().
Is there a better way to do this? When I use my mouse to scroll around the cube to a different face than the one the camera originally looked at and hit 'w'(the key to move the cube face up), the face the camera originally looked at will move up instead of the face the camera is looking at.
if (camera.getWorldDirection(new THREE.Vector3()).x > 0.50) {
let rotation = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0,0,1),-rotationStep);
cubeQuaternion.multiplyQuaternions(rotation, cubeQuaternion); // rotate cube
}
else {
let rotation = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1,0,0), rotationStep);
cubeQuaternion.multiplyQuaternions(rotation, cubeQuaternion);
}

threejs How to rotate transformcontrol itself

I'd like to rotate transformcontrol itself when object which attach to transformcontrol is rotate.
Before rotate
After rotate
As image shown, before rotate, cylinder's top direction is z-axis of transformcontrol, however after rotate is not.
I'd like that the transformcontrol's z axis to be always in the upward direction of the cylinder.
TransformControls returns an object that you can modify directly.
const controls = new TransformControls(camera, renderer.domElement);
controls.setSpace('local');
controls.rotation.x = Math.PI / -2;
If you want to sync its rotation with your mesh, you can add it as a child of that mesh or use Euler#copy.
mesh.add(controls);
// or manually in a render loop, for example
scene.add(controls);
renderer.setAnimationLoop(() => {
controls.rotation.copy(mesh.rotation);
renderer.render(scene, camera);
});

Three.js rotate vector and screen edge detection

I have a vector that I'm trying to keep length but rotate 90deg on colliding with the screen edge, but it gives me a weird out effect… don't know what I can be doing worn but it happens when I try to apply a Matrix and a Euler transform… Also for the screen detection, what I have is okj for detecting on and off screen, but it would be handy to know if it's the bottom, top, righ or left edge… Any clues? Thanks!
var direction = new THREE.Vector3(-0.2, -0.2, 0);
var a = new THREE.Euler( (Math.PI/2), 0, 0, 'XYZ' );
direction.applyEuler(a);
For the collision; I'm using the following:
camera.updateMatrixWorld(); // make sure the camera matrix is updated
camera.matrixWorldInverse.getInverse( camera.matrixWorld );
cameraViewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
frustum.setFromMatrix( cameraViewProjectionMatrix );
console.log(frustum.intersectsObject(textMesh));
I removed the line to get the reversed camera projection matrix.
The screen edges effectively became an object "outside" my view frustum, with whom my objects can now actively collide. By being now a negative view frustum, everything but that which we see is a 'walled object' that confines the frustum itself.

Three.js - Create 3D text that's unaffected by zooming and panning

Three.js version: r79
Basically, I want to have a 3D object (a mesh created with THREE.TextGeometry) act like it's in 2D space but is always in the same exact place on the screen (never moves with the camera, no matter if I zoom or pan). Is there a way to do this?
I'm actually not quite sure how unless I make what I feel is a giant hack and update the coordinates of the text mesh every time there is a mouse scroll event or pan event.
One solution is to add the mesh as a child of the camera.
scene.add( camera ); // required, since the camera has a child
camera.add( mesh );
mesh.position.set( 0, 0, - 100 ); // or whatever
three.js r.79

ThreeJS: Using a Sprite with the OculusRiftEffect

I'm developing for the OculusRift using the OculusRiftEffect from https://github.com/mrdoob/three.js/blob/master/examples/js/effects/OculusRiftEffect.js and am using Sprites. The problem is the sprites don't appear in the correct position in each eye as in the screenshot. You can see the house sprite is in different positions in each eye and causes a 'double vision' effect in the oculus. While playing around with the code (have a demo plunker here) you can notice that near the edges of the screen the positioning is more accurate but I need it nearer the center of the screen where the positioning is off. I assume this has something to do with the shading/rendering in OculusRiftEffect but don't know enough about it to break it down, any direction would be appreciated, thanks!
Sample code:
var _scene, _camera, _renderer, _effect, _sprite;
function init() {
_scene = new THREE.Scene();
_camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, .1, 100000);
_camera.lookAt(new THREE.Vector3());
_renderer = new THREE.WebGLRenderer({
antialias: true,
canvas: document.getElementById('legit')
});
_renderer.setSize(window.innerWidth, window.innerHeight);
_effect = new THREE.OculusRiftEffect(_renderer, {
worldScale: 1000
});
_effect.setSize(window.innerWidth, window.innerHeight);
THREE.ImageUtils.crossOrigin = 'anonymous';
_sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: new THREE.Texture(document.getElementById('icon')),
color: 0xff0000
})
);
_sprite.scale.set(200, 200, 1);
_sprite.position.set(500, 800, 1);
_scene.add(_sprite);
_scene.add(new THREE.Mesh(
new THREE.SphereGeometry(3000, 64, 32),
new THREE.MeshBasicMaterial({
color: 0xffffff,
wireframe: true,
side: THREE.DoubleSide
})
));
animate();
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
_renderer.render(_scene, _camera);
_effect.render(_scene, _camera);
}
document.addEventListener('DOMContentLoaded', init);
I am not that familiar with the Oculus plugin but I think your understanding of how sprites work is wrong.
Sprite is a rendering technique - a renderable rasterized surface... It can still be anywhere in space, and space is a relative term.
Is your house aware that it's part of the HUD and not part of a particle system somewhere in the distance?
One way to achieve what you want would be to overlay a copy at equal distance from the two points that represent the center of each eye, all in screen space. I think that this would give the effect that you are looking for.
That's as far as the positioning goes. Orientation wise, i'm not sure if they are actually properly aligned in your image above but the fish-eye effect is kicking in.
My reading of Three.js sprites indicates that they are positioned using screen coordinates, not in-scene geometry. Because of this they're ignoring the per-eye projection matrix offset that is imposed on the scene. I don't believe they will function properly in combination with the Oculus Rift distortion effect because of this.
As pailhead and Jherico mentioned this is not possible with the Oculus plugin. A workaround I found effective is to simply use a PlaneGeometry and set it as a child of the camera. Set it to look at the camera's position and it will act just like a sprite and render correctly for the OculusRiftEffect. Basic example (assuming camera and scene):
var sprite = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
new THREE.MeshBasicMaterial({color: 0xffffff})
);
//assuming camera is at (0,0,0) we need to offset it
sprite.position.set(20, 20, -100);
sprite.lookAt(camera.position);
camera.add(sprite);
//normally camera doesnt need to be added
//but if it has child meshes it is necessary
scene.add(camera);
Since the plane is a child of the camera once its offset is positioned correctly it will follow the camera wherever it goes. The caveat is that it won't function exactly the same as a Sprite in that you can't move it independently around the scene but it's the closest solution I've found.

Categories