Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "WallThrough/WallThroughEverything" {
- Properties{
- _Color2("Color2", Color) = (0,0,1, .5)
- _CutDistance("CutDistance", Float) = 1
- _NumberSteps("Number Steps", Int) = 10
- _MaxTraceDistance("Max Trace Distance" , Float) = 6.0
- _IntersectionPrecision("Intersection Precision" , Float) = 0.0001
- _CubeMap("Cube Map" , Cube) = "defaulttexture" {}
- _Color("Color", COLOR) = (1,1,1,1)
- //--- Standard Shader Vars ---//
- // sarah difference from Standard Unity shader
- _StencilRef("Stencil Ref ID", Int) = 1
- [Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Comparison", int) = 7 //Disabled=0,Never,Less,Equal,LessEqual,Greater,NotEqual,GreaterEqual,Always=8
- [Enum(UnityEngine.Rendering.StencilOp)] _StencilOp("Stencil Pass Operation", int) = 0 //Keep=0,Zero,Replace,IncrementSaturate,DecrementSaturate,Invert,IncrementWrap,DecrementWrap=7
- _Color("Color", Color) = (1,1,1,1)
- _MainTex("Albedo", 2D) = "white" {}
- _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
- _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
- [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
- _MetallicGlossMap("Metallic", 2D) = "white" {}
- _BumpScale("Scale", Float) = 1.0
- _BumpMap("Normal Map", 2D) = "bump" {}
- _Parallax("Height Scale", Range(0.005, 0.08)) = 0.02
- _ParallaxMap("Height Map", 2D) = "black" {}
- _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
- _OcclusionMap("Occlusion", 2D) = "white" {}
- _EmissionColor("Color", Color) = (0,0,0)
- _EmissionMap("Emission", 2D) = "white" {}
- _DetailMask("Detail Mask", 2D) = "white" {}
- _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
- _DetailNormalMapScale("Scale", Float) = 1.0
- _DetailNormalMap("Normal Map", 2D) = "bump" {}
- [Enum(UV0,0,UV1,1)] _UVSec("UV Set for secondary textures", Float) = 0
- // UI-only data
- [HideInInspector] _EmissionScaleUI("Scale", Float) = 0.0
- [HideInInspector] _EmissionColorUI("Color", Color) = (1,1,1)
- // Blending state
- [HideInInspector] _Mode("__mode", Float) = 0.0
- [HideInInspector] _SrcBlend("__src", Float) = 1.0
- [HideInInspector] _DstBlend("__dst", Float) = 0.0
- [HideInInspector] _ZWrite("__zw", Float) = 1.0
- //--- End Standard Shader Vars ---//
- }
- SubShader{
- Pass{
- Stencil{
- Ref 2
- Comp Always
- Pass Replace
- }
- Cull Off
- ColorMask 0
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- uniform float _CutDistance;
- uniform float _RenderInside;
- uniform float3 _HeadGlobal;
- struct VertexIn
- {
- float4 position : POSITION;
- float3 normal : NORMAL;
- float4 texcoord : TEXCOORD0;
- float4 tangent : TANGENT;
- };
- struct VertexOut {
- float4 pos : POSITION;
- float3 normal : NORMAL;
- float4 uv : TEXCOORD0;
- float3 posGlobal : TEXCOORD1;
- };
- VertexOut vert(VertexIn v) {
- VertexOut o;
- o.normal = v.normal;
- o.uv = v.texcoord;
- o.pos = mul(UNITY_MATRIX_MVP, v.position);
- o.posGlobal = mul(unity_ObjectToWorld, v.position);
- return o;
- }
- // Fragment Shader
- fixed4 frag(VertexOut i) : COLOR{
- float dist = length(i.posGlobal - _HeadGlobal);
- float normalisedDist = dist / _CutDistance;
- if (normalisedDist > 2 && !_RenderInside) {
- discard;
- }
- return float4(0, 0, 0, 0);
- }
- ENDCG
- }
- //draw 1s to the setencil buffer where the wall should disapear to reveal the inside
- Pass{
- Stencil{
- Ref 1
- Comp Always
- Pass Replace
- }
- Cull Off
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- uniform float _CutDistance;
- uniform float _RenderInside;
- uniform float3 _HeadGlobal;
- struct VertexIn
- {
- float4 position : POSITION;
- float3 normal : NORMAL;
- float4 texcoord : TEXCOORD0;
- float4 tangent : TANGENT;
- };
- struct VertexOut {
- float4 pos : POSITION;
- float3 normal : NORMAL;
- float4 uv : TEXCOORD0;
- float3 posGlobal : TEXCOORD1;
- };
- VertexOut vert(VertexIn v) {
- VertexOut o;
- o.normal = v.normal;
- o.uv = v.texcoord;
- o.pos = mul(UNITY_MATRIX_MVP, v.position);
- o.posGlobal = mul(unity_ObjectToWorld, v.position);
- return o;
- }
- // Fragment Shader
- fixed4 frag(VertexOut i) : COLOR{
- float dist = length(i.posGlobal - _HeadGlobal);
- float normalisedDist = dist / _CutDistance;
- if (normalisedDist > 1 && !_RenderInside) {
- discard;
- }
- return float4(0, 0, 0,0);
- }
- ENDCG
- }
- Pass{
- Name "OUTSIDE"
- Cull Front
- //rely on stencil to not overdraw
- ZTest Off
- LOD 200
- //if there is a discard command in the shader you will get no performance boost from this!
- Stencil{
- Ref 1
- Comp Equal
- Pass Keep
- Fail Keep
- }
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- // Use shader model 3.0 target, to get nicer looking lighting
- #pragma target 3.0
- #include "UnityCG.cginc"
- #include "Assets/Shaders/Chunks/noise.cginc"
- uniform int _NumberSteps;
- uniform float _IntersectionPrecision;
- uniform float _MaxTraceDistance;
- uniform float4 _Color;
- uniform samplerCUBE _CubeMap;
- uniform float3 _HeadGlobal;
- struct VertexIn
- {
- float4 position : POSITION;
- float3 normal : NORMAL;
- float4 texcoord : TEXCOORD0;
- float4 tangent : TANGENT;
- };
- struct VertexOut {
- float4 pos : POSITION;
- float3 normal : NORMAL;
- float4 uv : TEXCOORD0;
- float3 ro : TEXCOORD1;
- float3 origin : TEXCOORD2;
- float3 camPos : TEXCOORD3;
- float3 worldOrigin : TEXCOORD4;
- };
- float sdBox(float3 p, float3 b) {
- float3 d = abs(p) - b;
- return min(max(d.x, max(d.y, d.z)), 0.0) +
- length(max(d, 0.0));
- }
- float sdSphere(float3 p, float s) {
- return length(p) - s;
- }
- float sdTorus(float3 p, float2 t) {
- float2 q = float2(length(p.xy) - t.x, p.z);
- return length(q) - t.y;
- }
- float sdCapsule(float3 p, float3 a, float3 b, float r)
- {
- float3 pa = p - a, ba = b - a;
- float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
- return length(pa - ba*h) - r;
- }
- float2 smoothU(float2 d1, float2 d2, float k)
- {
- float a = d1.x;
- float b = d2.x;
- float h = clamp(0.5 + 0.5*(b - a) / k, 0.0, 1.0);
- return float2(lerp(b, a, h) - k*h*(1.0 - h), lerp(d2.y, d1.y, pow(h, 2.0)));
- }
- float3 rotatedBox(float3 p, float4x4 m)
- {
- float3 q = mul(m, float4(p, 1)).xyz;
- return sdBox(q, float3(.2, .2, .2));
- }
- float2 map(in float3 pos, in float3 origin) {
- float2 res;
- float n = 0 * noise(pos * 0 + float3(_Time.y, 0, 0)) * 12 + 4 * noise(pos * 4 + float3(0, _Time.y, 0)) + 26 * noise(pos * -5 + float3(0, 0, _Time.y));
- res = float2(-sdSphere(pos - _HeadGlobal, 10), 1.);
- res.x += n * .01;
- return res;
- }
- float3 calcNormal(in float3 pos, in float3 origin) {
- float3 eps = float3(0.001, 0.0, 0.0);
- float3 nor = float3(
- map(pos + eps.xyy, origin).x - map(pos - eps.xyy, origin).x,
- map(pos + eps.yxy, origin).x - map(pos - eps.yxy, origin).x,
- map(pos + eps.yyx, origin).x - map(pos - eps.yyx, origin).x);
- return normalize(nor);
- }
- float2 calcIntersection(in float3 ro, in float3 rd, in float3 origin) {
- float h = _IntersectionPrecision * 2;
- float t = 0.0;
- float res = -1.0;
- float id = -1.0;
- for (int i = 0; i< 20; i++) {
- if (h < _IntersectionPrecision || t > _MaxTraceDistance) break;
- float3 pos = ro + rd*t;
- float2 m = map(pos, origin);
- h = m.x;
- t += h;
- id = m.y;
- }
- if (t < _MaxTraceDistance) { res = t; }
- if (t > _MaxTraceDistance) { id = -1.0; }
- return float2(res, id);
- }
- VertexOut vert(VertexIn v) {
- VertexOut o;
- o.normal = v.normal;
- o.uv = v.texcoord;
- o.pos = mul(UNITY_MATRIX_MVP, v.position);
- //display in global space
- o.ro = mul(unity_ObjectToWorld, float4(v.position.xyz, 1));
- o.camPos = _WorldSpaceCameraPos;
- o.origin = mul(unity_ObjectToWorld, float4(0, 0, 0, 1));
- o.worldOrigin = float3(0, 0, 0);
- return o;
- }
- // Fragment Shader
- fixed4 frag(VertexOut i) : COLOR{
- float3 col = _Color;
- float3 ro = i.ro;
- float3 rd = normalize(ro - i.camPos);
- float3 worldOrigin = i.worldOrigin.xyz;
- float2 res = calcIntersection(ro, rd, worldOrigin);
- if (res.y > 0) {
- float3 pos = ro + rd * res.x;
- float3 norm = calcNormal(pos, worldOrigin);
- float3 fRefl = reflect(-rd, norm);
- float3 cubeCol = texCUBE(_CubeMap, -fRefl).rgb;
- col *= cubeCol;
- }
- else {
- //discard would mean stencil test can't be before fragment shader
- //discard;
- }
- return float4(col.r, col.g, col.b, 1);
- }
- ENDCG
- }
- //draw the nice wavy looking texture on the outside
- Pass{
- Name "SURFACE"
- Blend SrcAlpha OneMinusSrcAlpha
- Cull Back
- LOD 200
- //if there is a discard command in the shader you will get no performance boost from this!
- Stencil{
- //draw if the stencil is greater or equal to 1 (i.e. 1 or 2)
- Ref 1
- Comp LEqual
- Pass Keep
- Fail Keep
- }
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- // Use shader model 3.0 target, to get nicer looking lighting
- #pragma target 3.0
- #include "UnityCG.cginc"
- #include "Assets/Shaders/Chunks/noise.cginc"
- uniform float _Temp1;
- uniform float _Temp2;
- uniform float _Temp3;
- uniform float _Temp4;
- uniform float _Temp5;
- uniform float _Temp6;
- uniform float _CutDistance;
- uniform int _NumberSteps;
- uniform float _IntersectionPrecision;
- uniform float _MaxTraceDistance;
- uniform float4 _Color;
- uniform float4 _Color2;
- uniform float3 _HeadGlobal;
- uniform samplerCUBE _CubeMap;
- struct VertexIn
- {
- float4 position : POSITION;
- float3 normal : NORMAL;
- float4 texcoord : TEXCOORD0;
- float4 tangent : TANGENT;
- };
- struct VertexOut {
- float4 pos : POSITION;
- float3 normal : NORMAL;
- float4 uv : TEXCOORD0;
- float3 camPos : TEXCOORD1;
- float3 globalPos : TEXCOORD2;
- };
- float sdBox(float3 p, float3 b) {
- float3 d = abs(p) - b;
- return min(max(d.x, max(d.y, d.z)), 0.0) +
- length(max(d, 0.0));
- }
- float sdPlane(float3 p, float4 n) {
- return dot(p, n.xyz) + n.w;
- }
- float sdSphere(float3 p, float s) {
- return length(p) - s;
- }
- float sdTorus(float3 p, float2 t) {
- float2 q = float2(length(p.xy) - t.x, p.z);
- return length(q) - t.y;
- }
- float sdCapsule(float3 p, float3 a, float3 b, float r)
- {
- float3 pa = p - a, ba = b - a;
- float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
- return length(pa - ba*h) - r;
- }
- float2 smoothU(float2 d1, float2 d2, float k)
- {
- float a = d1.x;
- float b = d2.x;
- float h = clamp(0.5 + 0.5*(b - a) / k, 0.0, 1.0);
- return float2(lerp(b, a, h) - k*h*(1.0 - h), lerp(d2.y, d1.y, pow(h, 2.0)));
- }
- float3 rotatedBox(float3 p, float4x4 m)
- {
- float3 q = mul(m, float4(p, 1)).xyz;
- return sdBox(q, float3(.2, .2, .2));
- }
- float2 map(in float3 pos, in float3 faceNormal, in float planeDist) {
- float2 res;
- float time = _Time.y * 0.5;
- //float n = _Temp1* noise(pos * _Temp2 + float3(time, 0, 0)) + _Temp3 * noise(pos * _Temp4 + float3(0, time, 0)) + _Temp5 * noise(pos * _Temp6 + float3(0, 0, time));
- float n = 7 * noise(pos * 10 + float3(time, 0, 0)) + 7 * noise(pos * 10 + float3(0, time, 0)) + 7 * noise(pos * 10 + float3(0, 0, time));
- res = float2(sdPlane(pos, float4(faceNormal, planeDist)), 1.);
- res.x += n *.01;
- return res;
- }
- float3 calcNormal(in float3 pos, in float3 faceNormal, in float planeDist) {
- float3 eps = float3(0.001, 0.0, 0.0);
- float3 nor = float3(
- map(pos + eps.xyy, faceNormal, planeDist).x - map(pos - eps.xyy, faceNormal, planeDist).x,
- map(pos + eps.yxy, faceNormal, planeDist).x - map(pos - eps.yxy, faceNormal, planeDist).x,
- map(pos + eps.yyx, faceNormal, planeDist).x - map(pos - eps.yyx, faceNormal, planeDist).x);
- return normalize(nor);
- }
- float2 calcIntersection(in float3 ro, in float3 rd, in float3 faceNormal, in float planeDist) {
- float h = _IntersectionPrecision * 2;
- float t = 0.0;
- float res = -1.0;
- float id = -1.0;
- for (int i = 0; i< 20; i++) {
- if (h < _IntersectionPrecision || t > _MaxTraceDistance) break;
- float3 pos = ro + rd*t;
- float2 m = map(pos, faceNormal, planeDist);
- h = m.x;
- t += h;
- id = m.y;
- }
- if (t < _MaxTraceDistance) { res = t; }
- if (t > _MaxTraceDistance) { id = -1.0; }
- return float2(res, id);
- }
- VertexOut vert(VertexIn v) {
- VertexOut o;
- o.normal = v.normal;
- o.uv = v.texcoord;
- o.pos = mul(UNITY_MATRIX_MVP, v.position);
- o.camPos = _WorldSpaceCameraPos;
- o.globalPos = mul(unity_ObjectToWorld, v.position);
- return o;
- }
- // Fragment Shader
- fixed4 frag(VertexOut i) : COLOR{
- float alpha = 1;
- float3 col;
- float dist = length(i.globalPos - _HeadGlobal);
- //fade out pixels that are further away from the player
- //note that this assumes the StencilBuffer will have "2"s drawn out to twice the _CutDistance radius.
- alpha = (1 - (dist / (_CutDistance * 2))) * 2;
- float3 ro = i.globalPos;
- float3 rd = normalize(ro - i.camPos);
- float3 normal = i.normal;
- float planeDist = dot(normal, -i.globalPos);
- float2 res = calcIntersection(ro, rd, normal, planeDist);
- //Clip out the bits of distance field that are far away from the plane
- //more and more as you get closer to the surface
- float insideAlphaCompare = ((_CutDistance - dist) / _CutDistance);
- insideAlphaCompare *= .4; //magic number
- if (dist > _CutDistance || res.x > insideAlphaCompare) {
- float3 pos = ro + rd * res.x;
- float3 norm = calcNormal(pos, normal, planeDist);
- col = float3(1, 1, 1);
- float3 fRefl = reflect(-rd, norm);
- float3 cubeCol = texCUBE(_CubeMap, -fRefl).rgb;
- col *= cubeCol;
- col *= _Color2;
- }
- else {
- alpha = 0;
- }
- //alpha = 0;
- return float4(col.r, col.g, col.b, alpha);
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement