What is the difference between absoulut and normal worldpos in unity shader?
- User
What is the difference between absoulut and normal worldpos in unity shader?
- Assistant
What is the difference between absoulut and normal worldpos in unity shader?
In Unity Shader programming, the terms "absolute world position" and "normal world position" generally refer to different aspects of shader calculations, usually in the context of rendering object transformations and normals respectively. It's essential to understand how these values are computed and used in shaders. The distinction can especially be important depending on the context or the precise terms used (like if you meant something like worldPos vs. Absolute World Position etc.). Let's clarify these:
Absolute World Position: This generally refers to a position of a vertex or a point in the world space coordinates. World space positions are independent of the camera's position or orientation, meaning they represent the position of objects in the global context of the game world. In shaders, you can compute absolute world position using the object's local position and the model matrix (which transforms local coordinates to world coordinates):
float4 worldPosition = mul(unity_ObjectToWorld, v.vertex);
Here, unity_ObjectToWorld is a matrix that contains the transformation from the object's local space to world space, and v.vertex would be the vertex position in object/local space.
World Normal (or normal world position): When you refer to the "normal world position" in shaders, you are typically talking about the direction vector that is perpendicular (normal) to the surface of a mesh at a given point. In shaders, normals need to be transformed differently from positions because they should not be affected by translation transformations. They are usually transformed by the transpose of the inverse of the matrix that transforms the position. In Unity, this often involves the unity_WorldToObject matrix or explicitly using the UNITY_MATRIX_IT_MV (inverse transpose model-view matrix):
fixed3 worldNormal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));
In this case, v.normal is the vertex normal in object space, and the transformation aligns this normal with the world space, taking into account only rotation and scaling but not translation.
Absolute World Position is used for effects that depend on the exact location of a point in the world, such as world-space lighting, fog, shadow calculations, and certain types of procedural texturing or positioning of global effects.
World Normal is primarily used for lighting calculations where you need to know how light interacts with surfaces in a scene based on their orientation relative to light sources.
It's also worth noting that these computations can be relatively expensive, especially the normal transformation, as it involves matrix transformations. Optimizing shaders by carefully managing when and how transformations are applied (and reducing the number of calculations by reusing results when possible) is key to maintaining good performance in graphics-intensive applications.
Not yet included in a published release.