After some testing i have a sample shader up and running. I have not had much Z buffer problems, but I have not tested with very detailed geometry yet. It works good but I only made a box test working with and AABB axis aligned bounding box. Anyone have any idea on an approach where I can test for a rotated box?
Here is my shader code. I transform the vertex data to world space, do my vertex movement and then tranform back to object space before i finish the shader.
Shader"Custom/BoxShader" {
Properties {
_UL (“Upperleft”, vector) = (0., 0., 0., 1.0)
_LR (“Lowerright”, vector) = (0., 0., 0., 1.0)
_MainTex (“Base (RGB)”, 2D) = “white” {}
}
SubShader {
Pass {
CGPROGRAM
#pragmavertexvert
#pragmafragmentfrag
#include “UnityCG.cginc”
uniformsampler2D_MainTex;
uniformfloat_Cutoff;
float4_UL;
float4_LR;
structvertexInput {
float4vertex : POSITION;
float4texcoord : TEXCOORD0;
};
structvertexOutput {
float4pos : SV_POSITION;
float4tex : TEXCOORD0;
};
vertexOutputvert(vertexInputinput) {
//transformintoworlspace
float4v = input.vertex;
float4world_space_vertex = mul( _Object2World, v );
if ( world_space_vertex.x > _UL.x) { world_space_vertex.x = _UL.x; }
if ( world_space_vertex.x < _LR.x){ world_space_vertex.x = _LR.x; }
if ( world_space_vertex.y > _UL.y) { world_space_vertex.y = _UL.y; }
if ( world_space_vertex.y < _LR.y){ world_space_vertex.y = _LR.y; }
if ( world_space_vertex.z > _UL.z) { world_space_vertex.z = _UL.z; }
if ( world_space_vertex.z < _LR.z){ world_space_vertex.z = _LR.z; }
//transformbackintolocalspace
v = mul( _World2Object, world_space_vertex );
vertexOutputoutput;
output.pos = mul (UNITY_MATRIX_MVP, v);
output.tex = input.texcoord;
returnoutput;
}
fixed4_Color;
float4frag(vertexOutputinput) : COLOR
{
float4textureColor = tex2D(_MainTex, input.tex.xy);
if (textureColor.a < _Cutoff)
{
discard;
}
returntextureColor;
}
ENDCG
}
}
}