question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting world position from vertex shader

See original GitHub issue

Hello,

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 : Screenshot 2021-04-13 at 15 00 26

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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
risqcommented, Apr 20, 2021

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 😃

0reactions
vanruesccommented, Apr 14, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found