- Home /
How to convert a unity's transformation to vector4 to use in a shader?
I have a shader which takes a vector4 . This vector4 is a section plane with x, y, z and w . The shader clips the portion below this section plane. What I want to do is assign this section plane's value from the transformation of a unity plane. I understand that I have to get the plane's position and rotation in a c# script and assign it to the shader. The problem is that I don't know how this position and rotation can be converted to a vector4 form. I don't have much idea about transformation matrices and shaders. Thanks in advance..
[1]: /storage/temp/64054-section-plane.jpg
https://en.wikibooks.org/wiki/Cg_Program$$anonymous$$g/Unity
This will help. Its good.
Answer by Bunny83 · Feb 17, 2016 at 10:50 AM
It's not really clear what the shader expects. A plane is usually defined by a normal vector (usually called "a,b,c") and a distance from the origin (usually called "d").
If you use a Vector4 you can use the first 3 components (x,y,z) as normal vector and the 4th component (w) as distance. To convert a given normal and a point on the plane into that format you could use a method like this:
public static Vector4 CalcPlane(Vector3 aNormal, Vector3 aPoint)
{
aNormal.Normalize();
return new Vector4(aNormal.x, aNormal.y, aNormal.z, -Vector3.Dot(aNormal, aPoint));
}
However your screenshot shows the x,y and z parameters as "angle" which seems to make no sense. Expecially you would only need 2 angles to define the normal of a (mathematical) plane in space. However using angles would just be way more complicated. Also the values in your screenshot (-0.4, 0.6, 0) seem to represent a normalized vector just like you would expect.
Your answer
