Color Fragment Shader

Hi,
I am a complete noob when it comes to shaders, but let me briefly explain what I am trying to achieve.

I would like to create a Fragment Shader, to which I can pass selected Camera Projection Matrix and Color all pixels that are in the camera frustum to a given color.

I can pass Camera projection matrix to the shader but have no clue how to do this clip test.

How I can transform my input camera projection matrix to view space to clip space, so I can simply check if the space point is between -1 & 1, therefore, that pixel is in the camera view?

I need this for my real-time projection mapping application. Simply one camera (real world projector) looks at a given object and sets the Color.r to 1.0 to every pixel that is in this Camera View, then the second camera does exactly the same but changes the Color.g to 1.

I have already a shader that when Color is yellow (R = 1, G = 1, B=0) to fades the color on both projectors

So far I was using Unity Standard Assets Projector to project those colors and separate camera with render texture to do just that. The issue is that my Camera is using Off-Axis projection and the projector in unity doesn’t take projection matrix so I cannot match my camera lens shift with the virtual projector, so its inaccurate.

Some code snippets would be greatly appreciated. I am happy to share the final working project when it’s done, and I feel I am really close…

You could use a projector still, just pass a custom projection matrix to the material that you use in the shader instead. And make sure the projector’s frustum is larger than your custom one.

Hey I managed to get it working with help from my friend.
So it’s kind of working as expected apart from the fact it colors the object on both sides - also the back face (which is not seen by the camera but indeed is in the camera frustum) and object behind it instead of being shadowed by the object in front is colored as well which is not the behaviour I am looking for…

Is there a way I can for example use depth buffer to mask out and make it work more like a color projector and ignore everything that is behind?

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "Custom/MagicShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
        SubShader
    {
        Tags { "RenderType" = "Opaque" }

        Pass
        {
             CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            uniform float4x4 _OtherProjectionViewMatrix;
            uniform float4x4 _OtherProjectionViewMatrixTwo;

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float4 worldPos : TEXCOORD0;
            };

            v2f vert (appdata v)
            {
                v2f o;

                o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                o.vertex = mul(UNITY_MATRIX_VP, o.worldPos);

                return o;
            }
           
            float4 frag (v2f i) : SV_Target
            {
                float4 otherClipSpacePos = mul(_OtherProjectionViewMatrix, i.worldPos);

                otherClipSpacePos /= otherClipSpacePos.w; //perspective divide

                float4 otherClipSpacePosTwo = mul(_OtherProjectionViewMatrixTwo, i.worldPos);
                otherClipSpacePosTwo /= otherClipSpacePosTwo.w;

                bool outOfBounds = (otherClipSpacePos.x < -1.0f || otherClipSpacePos.x > 1.0f || otherClipSpacePos.y < -1.0f || otherClipSpacePos.y > 1.0f);

                bool outOfBoundsTwo = (otherClipSpacePosTwo.x < -1.0f || otherClipSpacePosTwo.x > 1.0f || otherClipSpacePosTwo.y < -1.0f || otherClipSpacePosTwo.y > 1.0f);

                float4 outColor;
                outColor.r = outOfBounds ? 0.0f : 1.0f;

                outColor.g = outOfBoundsTwo ? 0.0f : 1.0f;


                return outColor;
            }
            ENDCG
       

        }
    }
}

From my initial research, I don’t think Depth Texture is a way to go since its two dimensional and when an object is behind another object it’s depth will be still greater than 0.

Normals could probably solve the problem of not coloring the back faces but again an object behind will still be colored.

I am running out of ideas on how to solve this…

Depth textures are the way to go, but probably not the way you’re thinking about them. What you need are basically shadow maps as what you’re doing is effectively the same as a spotlight with a more complex projection matrix. That means you need to render out depth maps from the camera views into textures and pass those along to the shader to do comparisons against. That can solve some of the backface “lighting” as well, though a cell shading style lighting approach may also be warranted; do a dot product with the surface normal and “light” (camera) direction and either scale and clamp or use a step.