- Home /
X-Ray shader / Cutting a hole through a mesh
I'm looking for a Shader that can do this essentially: https://unitycoder.com/blog/2012/02/22/x-ray-cutout-shader-with-mouse/#comment-76005 and or this https://www.youtube.com/watch?v=vksqOpdgMQA
I've tried searching the Asset store but the "X-Ray" scripts there are either something that I could do myself very easily (Raytracing from camera to player and hiding objects in the way or making them transparent) or a the one that uses another camera which is resource intensive.
The first link has the package for the Shader which I will actually post
// xray mouse pos shader test v1.0 - mgear - http://unitycoder.com/blog
Shader "mShaders/XRay1"
{
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_ObjPos ("ObjPos", Vector) = (1,1,1,1)
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
_Radius ("HoleRadius", Range(0.1,5)) = 2
}
SubShader {
Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
Cull Off // draw backfaces also, comment this line if no need for backfaces
AlphaTest Greater [_Cutoff]
CGPROGRAM
// #pragma target 3.0
#pragma surface surf Lambert
// #include "UnityCG.cginc"
struct Input
{
float2 uv_MainTex;
// float3 depth;
// float4 pos;
// float3 viewDir;
float3 worldPos;
// float3 worldRefl;
// float3 worldNormal;
// float4 screenPos;
// INTERNAL_DATA
};
sampler2D _MainTex;
uniform float4 _ObjPos;
uniform float _Radius;
void surf (Input IN, inout SurfaceOutput o)
{
half3 col = tex2D (_MainTex, IN.uv_MainTex).rgb;
float dx = length(_ObjPos.x-IN.worldPos.x);
float dy = length(_ObjPos.y-IN.worldPos.y);
float dz = length(_ObjPos.z-IN.worldPos.z);
float dist = (dx*dx+dy*dy+dz*dz)*_Radius;
dist = clamp(dist,0.5,1);
o.Albedo = col; // color is from texture
o.Alpha = dist; // alpha is from distance to the mouse
}
ENDCG
}
FallBack "Diffuse"
}
However in the current version of Unity this shader no longer does anything, Dark_Seth made the video for the second link and also made a unity forum post (https://forum.unity3d.com/threads/wip-x-ray-shader-works-on-all-mesh.413968/), however no one discussed how to do it there.
So basically, I need help getting the provided shader to work, or for someone to make a new shader like this one and post it on the asset store as a free or paid asset.
Answer by Destolos · Aug 15, 2017 at 10:25 AM
This is not perfect, but it works for me:
Shader "mShaders/XRay1"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_ObjPos ("ObjPos", Vector) = (1,1,1,1)
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
_Radius ("HoleRadius", Range(0.1,2)) = 2
}
SubShader
{
Pass
{
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform sampler2D _MainTex;
float _Radius;
float4 _ObjPos;
struct vertexInput {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 worldPos : POSITION1;
float4 tex : TEXCOORD0;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.tex = input.texcoord;
output.pos = UnityObjectToClipPos(input.vertex);
output.worldPos = mul(input.vertex, unity_ObjectToWorld);
return output;
}
float4 frag(vertexOutput input) : COLOR
{
float4 textureColor = tex2D(_MainTex, input.tex.xy);
if (distance(input.worldPos.xyz, _ObjPos) < _Radius)
{
discard;
}
return textureColor;
}
ENDCG
}
}
FallBack "Diffuse"
}
Your answer
Follow this Question
Related Questions
Is it possible to use a single function for OnRenderImage AND [ImageEffectOpaque]OnRenderImage? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to render a RenderTexture into a specific mipLevel 0 Answers
Why when changing the doors colors it's not changing anything ? 1 Answer