Getting world position from vertex shader
See original GitHub issueHello,
I’m trying to implement a custom pass in a postprocessing pipeline, but I can’t find a way to get the world positions of my scene.
I use this very simple shader as a base, which works on threejs default composer : https://jsfiddle.net/4nzkwesg/1/
vert :
varying vec3 vColor;
void main()
{
vec4 worldPosition = modelMatrix * vec4(position, 1.0);
vColor = normalize(abs(worldPosition.xyz));
gl_Position = projectionMatrix * viewMatrix * worldPosition;
}
frag :
precision mediump float;
varying vec3 vColor;
void main()
{
gl_FragColor = vec4(vColor, 1.0);
}
But I can’t make it work on postprocessing because (it seems) I get screen space positions :
I’m calling this pass just after the renderPass.
What would be the correct way to port this shader to postprocessing ? Should I pass the geometries as attributes somewhere ? Or is there another way to access it from the vertex shader ?
Thank you very much.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How do you get the world position of a vertex in a shader?
I am trying to get the world position of a vertex into my fragment shader but the _Object2World translation doesn't appear to be...
Read more >get vertex world position in glsl - Stack Overflow
You should be passing the World Matrix into the shader and multiplying the vertex by that if you wish to get the position...
Read more >Get world-position in Vertex shader
I'm wondering how I can get the final position of a vertex. I use glTranslate in my render code, and I'm not getting...
Read more >how to get the world position in your fragment shader : r/godot
A quick tip: Multiplying by a matrix is quite slow, so you should do this calculation in the Vertex shader and pass it...
Read more >Cg Programming/Unity/Shading in World Space - Wikibooks
In this tutorial we will look at a shader that changes the fragment color depending on its position in the world. The concept...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you very much, it works perfectly ! I now understand better how the pipeline works and the difference between effects and passes. Blend function seems to be very powerful too !
Thanks again for your time, this is really appreciated 😃
For that you’d make an Effect. For example: https://codesandbox.io/s/gallant-dew-9zog6?runonclick=1&file=/src/FogEffect.js
The strategy is this: render the scene colors (RenderPass) and the world position data (WorldPositionPass) into two separate buffers and then use those buffers in a fullscreen shader to render the desired effect. The scene colors are already available to Effect shaders via the
inputColor
argument, but you can just render the fog factor out and then use the blend function mechanism to handle blending.An optimal pipeline would make use of multiple render targets (MRT) to render the geometry buffers but three.js doesn’t support that yet.