Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three.js - Geometry on top of another

Is it posible in Three.js to have a mesh always rendered on top of the scene, even if it's position is behind all objects? I'm implementing a lasso selection with a mesh and I need to render the selecting box on top of the rest of the scene.

like image 719
Leprosy Avatar asked Nov 09 '12 13:11

Leprosy


People also ask

Can three Js do 2D?

The way that Three. js structures its rendering means that the learning curve won't be too steep. It organizes all of the renders you'll do, whether 3D or 2D, under a “Scene” container.

Can I use three Js with angular?

Now you need to integrate three. js with Angular by importing the three. js to the component. In this way you can render the 3D object, which is component.

Do you need math for three Js?

The course is beginner friendly You don't need to have done WebGL or Three.js before. This training is intended for absolute beginners and will explain the basics before tackling more advanced topics. You don't have to be good at mathematics.


3 Answers

Yes.

First do this:

renderer.autoClear = false;

Then create a second scene that contains just the objects you want to be on top. Then, in your render loop:

renderer.clear();                     // clear buffers
renderer.render( scene, camera );     // render scene 1
renderer.clearDepth();                // clear depth buffer
renderer.render( scene2, camera );    // render scene 2

EDIT: Another solution is to have just one scene, but use this pattern:

mesh.renderOrder = 999;
mesh.onBeforeRender = function( renderer ) { renderer.clearDepth(); };

If the mesh has a single material, it will render "on top".

like image 76
WestLangley Avatar answered Oct 24 '22 01:10

WestLangley


On top of setting object.renderOrder you have to set material.depthTest to false on the relevant objects.

var spriteMaterial = new THREE.SpriteMaterial( { map: texture1, depthTest: false} );

    this.context1 = context1;
    this.texture1 = texture1;

    var sprite1 = new THREE.Sprite( spriteMaterial );
    sprite1.scale.set(30,15,1);
    sprite1.center.x=0;
    sprite1.center.y=0;
    sprite1.position.set( 0, 0, 0 );
    this.scene.add( sprite1 );
like image 31
Zhong Ri. Avatar answered Oct 24 '22 02:10

Zhong Ri.


If you want render only one mesh on front you can also manage by setting depthTest for the material of that object to false:

var onTopMaterial = new THREE.MeshStandardMaterial({
  color: 'red',
  depthTest: false
});

mesh.material = onTopMaterial;

Check a demonstation in this fiddle


Note: Make sure you have renderer.sortObjects set to the default true value.

like image 2
Wilt Avatar answered Oct 24 '22 03:10

Wilt