Controlled aera rendering via shader
Hi,
I have started using shader in Unity but with some difficulties. I am trying to render some GameObject (i will call CubeB) in a specific aera of my screen. I want them in front of all other GameObject. I want to control the size of this aera.
But first I try to render just a Color in this aera.
For that I set a Plane in my scene with a script and just a transform. The script picks a material with the shader, transform the plane and draw a Mesh. The plane is normalized and the Mesh is set at each frame. The size, position, rotation of the plane change every frame via the following script :
/// Origin of the player's head
[Tooltip("Transform of the player's head")]
public Transform HeadTransform;
/// Material used to play with the Zbuffer for the RA rendering
[Tooltip("Material used to play with the Zbuffer for the RA rendering.")]
[SerializeField]
private Material FadeMaterial;
private Material FadeMaterialInstance;
private Mesh PlaneMesh;
private Matrix4x4 local;
void Start()
{
PlaneMesh = new Mesh();
Vector3[] verts = new Vector3[]
{
new Vector3(-1, -1, 0),
new Vector3(-1, 1, 0),
new Vector3(1, 1, 0),
new Vector3(1, -1, 0)
};
int[] elts = new int[] { 0, 1, 2, 0, 2, 3 };
PlaneMesh.vertices = verts;
PlaneMesh.triangles = elts;
PlaneMesh.RecalculateBounds();
if(FadeMaterial != null)
FadeMaterialInstance = new Material(FadeMaterial);
}
void Update()
{
Matrix4x4 local = Matrix4x4.TRS(Vector3.forward * 0.33f, Quaternion.identity, Vector3.one); // Has to be in front of the clipping plan of the camera
Graphics.DrawMesh(PlaneMesh, HeadTransform.localToWorldMatrix*local, FadeMaterialInstance,0);
FadeMaterialInstance.SetPass(0);
}
}
Then in the shader attached to the Material FADEMATERIAL try to do what I want. But no color appears at my screen ... I don't understand.
The shader :
Shader "Custom/FadeBlack"
{
SubShader
{
Tags{ "Queue" = "Geometry+5" "IgnoreProjector" = "True" }
ZTest Always
Pass
{
ZWrite On
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform float _fovSize;
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
float4 uv : TEXCOORD0;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = v.vertex;
o.uv = v.vertex;
return o;
}
struct fragmentOutput
{
float zvalue : SV_Depth;
float4 color : SV_Target;
};
fragmentOutput frag(v2f i)
{
fragmentOutput o;
o.zvalue = 0.0;
o.color = (1,1,1,1);
_fovSize = 0.5;
if ((i.vertex.y <(-_fovSize) || i.vertex.y >(_fovSize)) ||
(i.vertex.x <(-_fovSize) || i.vertex.x >(_fovSize)))
{
o.zvalue = 1.0;
o.color = (0,0,0.5,1);
}
return o;
}
ENDCG
}
}
}
Any idea of what is it happening ?