Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user defined clip plane #647

Closed
laure opened this issue Oct 13, 2011 · 5 comments
Closed

user defined clip plane #647

laure opened this issue Oct 13, 2011 · 5 comments
Labels

Comments

@laure
Copy link

laure commented Oct 13, 2011

I'd like to use user defined clipping planes.
In openGL i would use:

glClipPlane(GL_CLIP_PLANE0, planeEq);
glEnable(GL_CLIP_PLANE0);

How can I do the same with webGL?

@mrdoob
Copy link
Owner

mrdoob commented Jan 20, 2012

You should be able to get the WebGL context with renderer.context.
From there it's up to you and the WebGL spec :)

@Slayvin
Copy link
Contributor

Slayvin commented Aug 20, 2013

I recently encountered the same problem while implementing reflective flat surfaces (aka 'mirrors' 😄 ) into three.js
As far as I know, you can not have user-defined clipping planes with WebGL, at least not with the 'simple' openGL way you described.
The trick is to use the "Oblique View Frustum" method, which is explained in this paper: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
The purpose of this method is to replace the near clip plane of your mirrored camera frustum by the plane defined by the reflective surface.
There is even some source code provided (in C++ though): http://www.terathon.com/code/oblique.html

I successfully ported that method in three.js and here is a sample scene to show this live: http://jsfiddle.net/slayvin/PT32b/

The only remaining issue is that the renderTarget is rendered a frame behind, and I have no clue why! This causes some flickering when the camera moves too fast and when the mirror is going through other objects.

Any idea on how to fix this? Do I need to implement a kind of multi-buffering method, or is it simply because I don't udpdate the camera matrix after modifying the frustum? If so, how can I do that?

@mrdoob
Copy link
Owner

mrdoob commented Aug 20, 2013

Mmmhh... I've tried setting scene.autoUpdate to false and call scene.updateMatrixWorld() by hand to make sure all the renderers had the same stuff but doesn't seem to solve anything... Neither calling mirror.updateMatrix() before extracting the rotation from there...

Pretty cool though! Maybe we could abstract this into it's own object.

@WestLangley
Copy link
Collaborator

Sweet.

I also tried making sure the relevant matrices were current. If you are worried about lag, you have to update them before you use them. Unfortunately, it did not seem to matter when zooming quickly...

mirror.updateMatrixWorld();

camera.updateMatrixWorld();

camera.matrixWorldInverse.getInverse( camera.matrixWorld );

@Slayvin
Copy link
Contributor

Slayvin commented Aug 21, 2013

Hey many thanks guys!!

Your suggestions made me reconsider the problem, and I noticed that there was no 'general' lag, but only flickering when the near clip plane changes (the mirror position compared to the mirrored camera position).

So, you were almost there @WestLangley: it's the matrices of the mirrored camera you need to update, not the camera itself.

Result: no more 'lag' : http://jsfiddle.net/slayvin/PT32b/2/ 😃

Maybe we could abstract this into it's own object.

Yes, I've thought about it. It would be great to have a new THREE.mirror() that handles everything. I'll try to code something...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants