# Changing color of vertices that are in the camera frustrum (Shader)

Hello everyone. So I have just started working on shaders and I had this idea that I want to check what vertices is in the frustrum of the camera. My knowledge of shaders, matrixes etc comes from watching videos and reading posts so bear with me please haha (This is also my first post on any forum like this)
Outside of my shader I set the matrix of the camera. In the shader I then set the verex to the clip position, then I use a function to see if the vertice is in the frustrum and if it is I assign it to 1 or 0 that I later use to show the color;

The results i get is this:

Only covering the multiple vertices that are directly in the centre of the camera.

Tried working around with the CheckIfFrustrum function and change the return to:

return vertexCheck.y > 1  && vertexCheck.x > 1  && vertexCheck.z > 0;

Which makes it take up a quarter of the frustrum instead.

If i only check the x or the y it covers half of the camera vertically or horizontally.

return vertexCheck.x > 1  && vertexCheck.z > 0;

Now if I only use the x values for the check I get a weird effect of a line with some of the white color inbetween:

return vertexCheck.x < 1 && vertexCheck.x > -1  && vertexCheck.z > 0;

Any tips on what I can do to make it so that all the vertices in the camera frustrum creates one color?

Code for outside the shader for updating the camera:

void LateUpdate()
{
Matrix4x4 M = transform.GetComponent<Renderer>().localToWorldMatrix;
Matrix4x4 V = theCamera.worldToCameraMatrix;
Matrix4x4 P = theCamera.projectionMatrix;
Matrix4x4 MVP = P * V * M;
}

Function in Shader that checks if the vertice is in the cameras view:

bool CheckIfFrustrum(float4x4 matr, float4 point)
{
float4 VertexCheck = mul(matr, float4(point.x, point.y, point.z, 1));
return VertexCheck.x > -1 && VertexCheck.x < 1 && VertexCheck.y > -1 && VertexCheck.y < 1;
}

Interpolators vert (MeshData v)
{
Interpolators o;

o.vertex = UnityObjectToClipPos(v.vertex);
o.color = CheckIfFrustrum(MvpCamera, o.vertex) ? 1 : 0;

return o;
}

Thanks in advance for any help!!

I see two problems:

First, you appear to be transforming the vertex twice. UnityObjectToClipPos already multiplies with the MVP matrix. If you want that matrix, you don’t need to do any matrix multiplication yourself. If you want a different matrix (which seems to be the case), pass v.vertex to CheckIfFrustum, not o.vertex.

Second, you don’t seem to be doing a perspective divide. Add this line after the matrix multiplication:
VertexCheck /= VertexCheck.w;

In case you’re interested in the math behind it:
Transformation matrices usually can’t do perspective projection. You need homogenous coordinates to do it. What that means is that you extend the 3D space to a 4D one where each 3D point is represented by a line through the origin. The 3D point (x, y, z) is represented by any 4D point of the form (wx, wy, wz, w). The matrix multiplication just gives you one such point but you still have to divide by w to get (x, y, z, 1).

This is done automatically for the vertex position during rasterization but you have to do it manually when you use camera projection matrices anywhere else.

Thanks , that works like the way I wanted it to! When I tried it in the past it had my suspicions that it was enough doing it with the UnityObjectToClip so I had tried it like that before but I have never read that you need to do the perspective divide and after doing that it worked out perfectly!
Thank you so much :)I!