1

In my ThreeJS WebGLRenderer scene, I am currently clipping pixels with a GLSL fragment (pixel) shader, according to their distance from the lower left-hand corner of the screen:

if (distance(vec2(gl_FragCoord.x, gl_FragCoord.y), vec2(0.0, 0.0)) > 42.0)
    discard;

This shader is used with a ThreeJS.ShaderMaterial instance, that ends up used with a PointCloud mesh, that ends up a particle system in the scene.

My question is, how do I determine the "window size" for this shader? I am happy to pass these pixel dimensions from ThreeJS to the shader, but do not know the best way to map a ThreeJS geometry & mesh to a GLSL window size. Passing in window.innerWidth and window.innerHeight does not work.

The goal is to clip fragments that are near the center of the physical screen:

if (distance(vec2(gl_FragCoord.x, gl_FragCoord.y), vec2(pixelWidth * 0.5, pixelHeight * 0.5)) > 42.0)
    discard;

How would I find pixelWidth and pixelHeight on the ThreeJS side?

1
  • 1
    attribute is inappropriate for the fragment shader. Something qualified as attribute is a vertex attribute. In a fragment shader, the equivalent of a per-vertex attribute is a varying. But I don't think you want that - you'd have to write this attribute once for every vertex. A uniform is probably what you want. Commented Feb 17, 2015 at 3:30

1 Answer 1

2

If you're rendering to the canvas you can get the actual size with

var width = renderer.context.drawingBufferWidth;
var height = renderer.context.drawingBufferHeight;

If you're rendering to a render target then

var width = renderTarget.width;
var height = renderTarget.height;

should work

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the help, @gman. And for addressing the actual question, rather than nitpicking. You got me in the right direction. In my case, I needed to do: var width = webGLRenderer.context.drawingBufferWidth; var height = webGLRenderer.context.drawingBufferHeight; And then pass those values into the shader. Thanks again!
Assuming you're talking about the comment on your question, it is strange to see you passing in pixelWidth and pixelHeight as attributes :D Unless you really meant to do that maybe you want to edit your question so others don't copy that example?
Good point. I removed attribute (or varying or uniform) references, since how state is passed from ThreeJS into shaders is not relevant to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.