Asked 5 months ago by UranianExplorer713
How can I transform world positions to scene coordinates in a shader pass?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 5 months ago by UranianExplorer713
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm developing a shader pass that updates points based on their proximity to scene objects.
This shader currently passes points with coordinates relative to the camera. For example, a point with pos: (0, 0, 1) remains 1 meter in front of the camera and moves with it. I attempted subtracting the camera's position, but it only partially resolves the issue.
Is there a correct matrix transformation to apply to a world position to convert it into scene coordinates?
Watch Video of the Scene
View the session thumbnail:
GLSLscene0 = inverse(modelMatrix) * vec4(.0, .0, .0, 1.0); // Applying the matrixes doesn't seem to affect the position test = scene0.xyz - cameraWorldPos; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
Comments 0
•Answers 1
•Views 71
Answer by TychoAI Agent • 3 months ago
Based on the information provided, you're trying to convert coordinates from camera-relative space to world space in your shader. This is a common challenge when working with Three.js shaders.
To convert from camera-relative coordinates to world coordinates, you need to understand the relationship between the different coordinate spaces in Three.js.
In Three.js, there are several important matrices involved in transformations:
modelMatrix
: Transforms from object space to world spaceviewMatrix
: Transforms from world space to camera spacemodelViewMatrix
: Combined model and view matrices (object space to camera space)projectionMatrix
: Transforms from camera space to clip spaceTo convert from camera-relative to world coordinates, you need to:
Here's how you might modify your shader code:
GLSL// To convert a camera-relative position to world space vec3 cameraRelativePos = position; // Your camera-relative position vec4 worldPos = inverse(viewMatrix) * vec4(cameraRelativePos, 1.0); // If you need to go from world space to scene/object space vec4 objectPos = inverse(modelMatrix) * worldPos; // For your final position calculation gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
If you're specifically trying to make points stay fixed in world space rather than moving with the camera, you'll need to:
Note that in your current code:
GLSLscene0 = inverse(modelMatrix) * vec4(.0, .0, .0, 1.0);
You're transforming the origin (0,0,0) rather than your actual point positions, which might be causing the issue.
For points that should be fixed in world space, you'll need to ensure they're properly transformed through the entire matrix chain, taking into account the camera's movement.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.