Skip to content

Instantly share code, notes, and snippets.

@yumayanagisawa
Created May 4, 2018 19:27
Show Gist options
  • Save yumayanagisawa/89fa58d4db6e8d04aa5073256d384fc7 to your computer and use it in GitHub Desktop.
Save yumayanagisawa/89fa58d4db6e8d04aa5073256d384fc7 to your computer and use it in GitHub Desktop.
Experiment with "_WorldSpaceCameraPos" in Unity3D
// based on this shader (https://assetstore.unity.com/packages/vfx/shaders/gem-shader-3)
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "FX/Gem_CameraSpace"
{
Properties {
[HDR] _Color ("Color", Color) = (1,1,1,1)
_ReflectionStrength ("Reflection Strength", Range(0.0,2.0)) = 1.0
_EnvironmentLight ("Environment Light", Range(0.0,2.0)) = 1.0
_Emission ("Emission", Range(0.0,2.0)) = 0.0
[NoScaleOffset] _RefractTex ("Refraction Texture", Cube) = "" {}
_Alpha ("Gem Alpha", Range(0.0, 1.0)) = 1.0
_Distance ("Gem Scale", Float) = 50.0
}
SubShader {
Tags {
"Queue" = "Transparent"
}
// First pass - here we render the backfaces of the diamonds. Since those diamonds are more-or-less
// convex objects, this is effectively rendering the inside of them.
Pass {
Cull Front
Blend SrcAlpha OneMinusSrcAlpha
ZWrite On//Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float3 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
};
v2f vert (float4 v : POSITION, float3 n : NORMAL)
{
v2f o;
o.pos = UnityObjectToClipPos(v);
o.worldPos = mul(unity_ObjectToWorld, v).xyz;
// TexGen CubeReflect:
// reflect view direction along the normal, in view space.
float3 viewDir = normalize(ObjSpaceViewDir(v));
o.uv = -reflect(viewDir, n);
o.uv = mul(unity_ObjectToWorld, float4(o.uv,0));
return o;
}
fixed4 _Color;
samplerCUBE _RefractTex;
half _EnvironmentLight;
half _Emission;
// alpha channel hack
float _Alpha;
float _Distance;
half4 frag (v2f i) : SV_Target
{
float distance = length(i.worldPos.xyz - _WorldSpaceCameraPos.xyz) / _Distance;
_EnvironmentLight = (distance > 1.5) ? 1.5 : distance;
half3 refraction = texCUBE(_RefractTex, i.uv).rgb * _Color.rgb;
half4 reflection = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, i.uv);
reflection.rgb = DecodeHDR (reflection, unity_SpecCube0_HDR);
half3 multiplier = reflection.rgb * _EnvironmentLight + _Emission;
//return half4(refraction.rgb * multiplier.rgb, 1.0f);
half4 BeforeAlphaCalcColor = half4(refraction.rgb * multiplier.rgb, 1.0f);
_Alpha = (distance > 1.0) ? 1.0 : distance;
BeforeAlphaCalcColor.a = _Alpha;
return BeforeAlphaCalcColor;
}
ENDCG
}
// Second pass - here we render the front faces of the diamonds.
Pass {
ZWrite On
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float3 uv : TEXCOORD0;
half fresnel : TEXCOORD1;
float3 worldPos : TEXCOORD2;
};
v2f vert (float4 v : POSITION, float3 n : NORMAL)
{
v2f o;
o.pos = UnityObjectToClipPos(v);
o.worldPos = mul(unity_ObjectToWorld, v).xyz;
// TexGen CubeReflect:
// reflect view direction along the normal, in view space.
float3 viewDir = normalize(ObjSpaceViewDir(v));
o.uv = -reflect(viewDir, n);
o.uv = mul(unity_ObjectToWorld, float4(o.uv,0));
o.fresnel = 1.0 - saturate(dot(n,viewDir));
return o;
}
fixed4 _Color;
samplerCUBE _RefractTex;
half _ReflectionStrength;
half _EnvironmentLight;
half _Emission;
float _Distance;
half4 frag (v2f i) : SV_Target
{
float distance = length(i.worldPos.xyz - _WorldSpaceCameraPos.xyz) / _Distance;
_EnvironmentLight = (distance > 1.5) ? 1.5 : distance;
_ReflectionStrength = (distance > 1.0) ? 1.0 : distance;
half3 refraction = texCUBE(_RefractTex, i.uv).rgb * _Color.rgb;
half4 reflection = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, i.uv);
reflection.rgb = DecodeHDR (reflection, unity_SpecCube0_HDR);
half3 reflection2 = reflection * _ReflectionStrength * i.fresnel;
half3 multiplier = reflection.rgb * _EnvironmentLight + _Emission;
return fixed4(reflection2 + refraction.rgb * multiplier, 1.0f);
}
ENDCG
}
// Shadow casting & depth texture support -- so that gems can
// cast shadows
UsePass "VertexLit/SHADOWCASTER"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment