Created
November 9, 2017 08:16
-
-
Save unitycoder/e64363b9b08285535f22a17680633424 to your computer and use it in GitHub Desktop.
XRay Shader 2017
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// xray mouse pos shader test v2.0 - mgear - http://unitycoder.com/blog | |
Shader "UnityLibrary/Effects/XRay2017" | |
{ | |
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"} | |
LOD 100 | |
Cull Off // draw backfaces also, comment this line if no need for backfaces | |
CGPROGRAM | |
#pragma surface surf Lambert alphatest:_Cutoff | |
struct Input | |
{ | |
float2 uv_MainTex; | |
float3 worldPos; | |
}; | |
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,1); | |
o.Albedo = col; // color is from texture | |
o.Alpha = dist; // alpha is from distance to the mouse | |
} | |
ENDCG | |
} | |
FallBack "Diffuse" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for making this shader. I have ported the Javascript file to C# if you want to add it - works fine in Unity 2019.4
// update object position to shader v1.0 - mgear - http://unitycoder.com/blog
using UnityEngine;
public class MousePos2Shader : MonoBehaviour
{
private float radius = 2;
private RaycastHit hit;
private Ray ray;
void Update ()
{
// get mouse pos
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
}
}