I've been having a bit tricky time trying to get a virtual craniotomy to work out. I've been trying to modify one of the hololens sdk vert/frag shaders to achieve this effect, but unfortunately for me my talents lay elsewhere :/ I found and modified a surface shader to achieve a similar effect I want... however I would like to keep my current vert/frag shader. Picture of what I would like to happen. The approach I've been trying is to raycast and apply a clip to every pixel if falls within a certain radius of the raycast impact, via the following code added I've been testing out in its own shader. Code (CSharp): Shader "Custom/Cranio" { Properties{ _Color("Color", Color) = (1,1,1,1) _ObjPos("ObjPos", Vector) = (0,0,0) _ObjRad("Craniotomy Size", Float) = 0.001 } SubShader{ pass{ Tags { "Queue" = "Geometry" } LOD 150 CGPROGRAM #pragma vertex vert #pragma fragment frag float4 _ObjPos; //World Position of Raycast float _ObjRad; //Radius to Check float4 _Color; int check; struct a2v { float4 pos : POSITION; }; struct v2f { float4 pos : SV_POSITION; }; v2f vert(a2v input) { v2f output; //Change Local Vertex Position to World Apace and Find Dist Value float4 WorldPosition = mul(unity_ObjectToWorld, input.pos); float4 Delta = WorldPosition - _ObjPos; float Dist = length(Delta); if (Dist < _ObjRad) { check = -1; } else { check = +1; } //Convert Vertex local Positon to Screen Posiiton for Output output.pos = UnityObjectToClipPos(input.pos); return output; } float4 frag(v2f i) : SV_Target{ clip(check); //Discard if set < 1 return _Color; } ENDCG } } FallBack "Diffuse" } Is there something wrong with how this is implemented? I'm not getting any clipping of my texture. Below is what I'm trying to get to work on the above Shader.
An update: I've decided to use a surface shader, and all is fine when not modifying the alpha, but when I do there are two problems: 1. Don't get the right layering of models (inside to out), 2. Some models look as though they're inside-out 3. I can see both sides of the model with 100% alpha Transparency always worked well when using the default unity 2 sided shader, but with my own I'm afraid not so much. One solution is to change shaders when I want different effects, but I'm thinking it's possible to have a two sided shader which: 1. 2 sided 2. Alpha works well 3. I can clip pixels near to a point After spending some hours reading online, this is what I came up with: Any advice would be appreciated to get this working as it should! Code (CSharp): Shader "Frantz/ModelShader" { Properties{ _Color("Material Color (RGB)", Color) = (0,0,0,1) _Origin("Origin (World Space)", Vector) = (0,0,0,0) _Radius("Radius", Range(0,1)) = 0 _Glossiness("Smoothness", Range(0,1)) = 0 _Metallic("Metallic", Range(0,1)) = 0.0 _Conversion("Halo Effect (RGB)", Color) = (255,90,0,255) _ConvertEmission("Halo Intensity", Range(0,1)) = 0.5 _ConvertDistance("Halo Distance", Range(0,.02)) = 0.005 _EmitCol("Emisison Color (RGB)", Color) = (1,1,1,1) _Emission("Surface Intensity", Range(0,1)) = 1 } SubShader{ Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" } LOD 100 //ZWrite On //ZTest LEqual Blend SrcAlpha OneMinusSrcAlpha Cull Front CGPROGRAM //Target Only for Hololens #pragma target 5.0 #pragma only_renderers d3d11 //Shaders #pragma surface surf Standard fullforwardshadows alpha #pragma vertex vert //Input for Surface Shader struct Input { float3 worldPos; }; void vert(inout appdata_full v) { v.normal *= -1; } half _Glossiness; half _Metallic; half _ConvertDistance; half _ConvertEmission; half4 _Color; half4 _Conversion; half _Radius; float3 _Origin; void surf(Input IN, inout SurfaceOutputStandard o) { half dist = distance(IN.worldPos, _Origin); clip(dist - _Radius); float convert_mask = (dist - _Radius) / _ConvertDistance; convert_mask = clamp(convert_mask, 0, 1); fixed4 albedo = _Color; albedo *= convert_mask; fixed4 convert = _Conversion; convert *= 1.0 - convert_mask; o.Albedo = albedo.rgb + convert.rgb; o.Emission = convert.rgb * _ConvertEmission; o.Metallic = _Metallic; o.Smoothness = _Glossiness; o.Alpha = albedo.a; } ENDCG Cull Back CGPROGRAM //Target Only for Hololens #pragma target 5.0 #pragma only_renderers d3d11 //Shaders #pragma surface surf Standard fullforwardshadows alpha //Input for Surface Shader struct Input { float3 worldPos; }; //Veriables half _Glossiness; half _Metallic; half _ConvertDistance; half _ConvertEmission; half4 _Color; half4 _Conversion; half _Emission; half4 _EmitCol; half _Radius; float3 _Origin; void surf(Input IN, inout SurfaceOutputStandard o) { half dist = distance(IN.worldPos, _Origin); clip(dist - _Radius); float convert_mask = (dist - _Radius) / _ConvertDistance; convert_mask = clamp(convert_mask, 0, 1); fixed4 albedo = _Color; albedo *= convert_mask; fixed4 convert = _Conversion; convert *= 1.0 - convert_mask; o.Albedo = albedo.rgb + convert.rgb + _EmitCol.rgb*_Emission; o.Emission = convert.rgb * _ConvertEmission; o.Metallic = _Metallic; o.Smoothness = _Glossiness; o.Alpha = albedo.a; } ENDCG } Fallback "Diffuse" } This is how things are looking when I don't include the word "alpha" in the pragma surface bit: Then trying to use an alpha: *Disaster!
Are you using separate models for the head, skull, and brain? I would try using the stencil buffer. You could add a sphere around the mouse pointer (or whatever pointer your using) with a stencil mask, which will stop rendering the head, showing the brain inside.