Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created November 9, 2017 08:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unitycoder/e64363b9b08285535f22a17680633424 to your computer and use it in GitHub Desktop.
Save unitycoder/e64363b9b08285535f22a17680633424 to your computer and use it in GitHub Desktop.
XRay Shader 2017
// 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"
}
@forsini
Copy link

forsini commented Aug 15, 2020

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);

if (Physics.Raycast(ray, out hit, Mathf.Infinity)) 
{
	GetComponent<Renderer>().material.SetVector("_ObjPos", new Vector4(hit.point.x,hit.point.y,hit.point.z,0));
}

// box rotation for testing
if (Input.GetKey ("a"))
{
	transform.Rotate(new Vector3(0,30,0) * Time.deltaTime);
}
if (Input.GetKey ("d"))
{
	transform.Rotate(new Vector3(0,-30,0) * Time.deltaTime);
}

// mousewheel for radius
if (Input.GetAxis("Mouse ScrollWheel")!=0)
{
	radius += (float) Input.GetAxis("Mouse ScrollWheel")*0.8f;
	GetComponent<Renderer>().material.SetFloat( "_Radius", radius);
}

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment