-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHolographic.shader
107 lines (89 loc) · 3.38 KB
/
Holographic.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// https://github.com/nuonical/unity-holographic-shader/
// Added vr support for single pass instancing : https://docs.unity3d.com/Manual/SinglePassInstancing.html
Shader "Custom/Holographic"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,0,0,1)
_Distance ("Distance", Float) = 100
_Scale ("Scale", Float) = 1
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" "ColorMask"="RGBA" }
LOD 100
Pass
{
ZTest Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Distance;
float _Scale;
float4 _Color;
v2f vert (appdata v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
// Calculate lens origin and direction vectors in view space
float3 lens_origin = UnityObjectToViewPos(float3(0,0,0));
float3 p0 = UnityObjectToViewPos(float3(0,0, _Distance));
float3 n = UnityObjectToViewPos(float3(0,0, 1)) - lens_origin;
float3 uDir = UnityObjectToViewPos(float3(1,0,0)) - lens_origin;
float3 vDir = UnityObjectToViewPos(float3(0,1,0)) - lens_origin;
float3 vert = UnityObjectToViewPos(v.vertex);
// Project the vertex position onto the lens plane
float a = dot(p0, n) / dot(vert, n);
float3 vert_prime = a * vert;
// Convert the vertex position back to clip space
o.vertex = UnityObjectToClipPos(v.vertex);
// Calculate UV coordinates based on projected vertex position
o.uv = float2(dot(vert_prime - p0, uDir), dot(vert_prime - p0, vDir));
o.uv = o.uv / (_Scale * _Distance);
// Center the UVs around the lens center
o.uv += float2(0.5, 0.5);
UNITY_TRANSFER_FOG(o, o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
// Sample the texture
fixed4 tex = tex2D(_MainTex, i.uv);
fixed4 col = _Color;
col.a = tex.r;
// Clamp UVs to restrict rendering within the lens
if(i.uv.x < 0 || i.uv.x > 1 || i.uv.y < 0 || i.uv.y > 1)
{
col.a = 0;
}
// Apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}