Shader "CustomShaders/FesnelRipple"

{
    Properties
    {
		_base_alpha ("base alpha", Range(0.0, 1.0)) = 0.5
		_shield_color ("shield color", Color) = (0, 0, 1, 1)
		_shield_color_brightness ("shield color brightness", Range(0.25, 20)) = 10.0
		_shield_intensity ("shield intensity", Range(0.1,20)) = 2.0
		_rotation ("rotation", Vector) = (0.1, 0.0, 0.0)
		_shield_size ("shield size", Range(0.0,0.5)) = 0.0
		_shield_pulse_frequency ("shield pulse frequency", float)= 1.0
		_shield_pulse_density ("shield pulse density", float) = 1.0
		_shield_pulse_amplitude ("shield pulse amplitude", float) = 1.0
		_shield_pulse_blend ("shield pulse blend", float) = 1.0
		_shield_pulse_radius ("shield pulse radius", float) = 1.0
		_impact_origin ("impact origin", Vector) = (1.0, 0.0, 0.0)
		_impact_frequency ("impact frequency", float) = 5.0
		_impact_density ("impact density", float) = 5.0
		_impact_amplitude ("impact amplitude", float) = 6.0
		_impact_blend ("impact blend", float) = 0
		_impact_radius ("impact radius", float) = 1.1
		_impact_anim ("impact anim", Range(0.0,0.1)) = 0.0
    }
	HLSLINCLUDE
            #pragma vertex vert
			#pragma fragment frag
			#pragma enable_d3d11_debug_symbols
			#include "UnityCG.cginc"
			// properties
			float _base_alpha;
			float4 _shield_color;
			float _shield_color_brightness;
			float _shield_intensity;
			float3 _rotation;
			float _shield_size;
			float _shield_pulse_frequency;
			float _shield_pulse_density;
			float _shield_pulse_amplitude;
			float _shield_pulse_blend;
			float _shield_pulse_radius;
			float3 _impact_origin;
			float _impact_frequency;
			float _impact_amplitude;
			float _impact_blend;
			float _impact_density;
			float _impact_radius;
			float _impact_anim;

			// Impact functions

			float _FadeRipple(float dist, float blend, float radius) {
				// create a radius 
				return smoothstep(radius, 0, dist);
			}

			float _ComputeRipple(float3 vert, float3 orig, float blend, float radius, float freq, float dens, float ampl, float anim) {
				// calculate the intensity of the impact
				float dist = distance(vert, orig);
				float i = sin((freq * anim - dist * dens)) * ampl;
				return i * _FadeRipple(dist, blend, radius);
			}

			// Shield functions

			float ComputeFresnel(float3 norm, float3 view_dir, float intensity) {
				float width = 0.95;
				float softness = 0.96;
				float fresnel = 1.0 - saturate(dot(norm, view_dir));
				fresnel = pow(fresnel, intensity);
				fresnel = lerp(1, smoothstep(width, softness, fresnel), step(0, width));
				return fresnel;
			}

			float4 Rotate(float3 vert, float3 speed) {
				// build the 3 rotation matrices
				speed = speed * _Time * 5.0;
				
				float4x4 xrot = float4x4(
					float4(1.0, 0.0, 0.0, 0.0),
					float4(0.0, cos(speed.x), -sin(speed.x), 0.0),
					float4(0.0, sin(speed.x), cos(speed.x), 0.0),
					float4(0.0, 0.0, 0.0, 1.0));
				
				float4x4 yrot = float4x4(
					float4(cos(speed.y), 0.0, -sin(speed.y), 0.0),
					float4(0.0, 1.0, 0.0, 0.0),
					float4(sin(speed.y), 0.0, cos(speed.y), 0.0),
					float4(0.0, 0.0, 0.0, 1.0));
				
				float4x4 zrot = float4x4(
					float4(cos(speed.z), -sin(speed.z), 0.0, 0.0),
					float4(sin(speed.z), cos(speed.z), 0.0, 0.0),
					float4(0.0, 0.0, 1.0, 0.0),
					float4(0.0, 0.0, 0.0, 1.0));
				
				float4 output = mul(xrot, mul(yrot, mul(zrot, float4(vert.xyz, 1.0))));
				return output;
			}

			// vertex shader inputs
            struct appdata
            {
                float4 vertex : POSITION;
				float4 normal : NORMAL;
                float2 uv : TEXCOORD0;
            };

			// vertex shader outputs to fragment
            struct v2f
            {
                float2 uv : TEXCOORD0; // texture coordinate
                float4 vertex : SV_POSITION; // clips space position
				float3 normal : NORMAL;
				float3 viewDirection : TEXCOORD2;
            };

			// vertex shader
            v2f vert (appdata v)
            {
				v.vertex = Rotate(v.vertex, _rotation);
				v.normal = Rotate(v.normal, _rotation);

				float4 vert_world_pos = mul(unity_ObjectToWorld, v.vertex);

				float impact_ripple = _ComputeRipple(v.vertex.xyz, 
				_impact_origin.xyz, 
				_impact_blend,
				_impact_radius, 
				_impact_frequency, 
				_impact_density, 
				_impact_amplitude,
				_impact_anim);

				v.vertex += v.normal * (impact_ripple + _shield_size);

                v2f o;
				// transform position to clip space
				// (multiply with model*view*projection matrix)
                o.vertex = UnityObjectToClipPos(v.vertex);
				// just pass the texture coordinate
				o.uv = v.uv;
				o.normal = v.normal.xyz;
				o.viewDirection = normalize(WorldSpaceViewDir(vert_world_pos));
                return o;
            }
	ENDHLSL
    SubShader
    {
		Tags { "RenderType"="Transparent" 
		"RenderPipeline" = "UniversalPipeline"
		"Queue"="Transparent" 
		"ForceNoShadowCasting"="True"
		// "IgnoreProjector"="True" 
		// "Zwrite"="Off"
		}

		Blend SrcAlpha OneMinusSrcAlpha
        Pass
        {
			Tags
			{
				"LightMode" = "UniversalForward"
				"RenderType"="Opaque" 
				"Queue"="Transparent"
			}
			Name "BackCullingPass"
			Cull Back

            HLSLPROGRAM
            half4 frag (v2f i) : SV_Target
            {
				float f = ComputeFresnel(i.normal, i.viewDirection, _shield_intensity);
				half4 col = 0;			
				half3 normalToGray = (0.299 * i.normal.r) + (0.587 * i.normal.g) + (0.114 * i.normal.b);
				col.rgb = f * _shield_color_brightness * _shield_color + (abs(normalToGray) - 0.3);
				col.a = f * _base_alpha;
				return col;
            }
            ENDHLSL
        }
        Pass
        {
			Tags
			{
			 	"LightMode" = "SRPDefaultUnLit"
				"RenderType"="Transparent" 
				"Queue"="Transparent"
			}
			Name "FrontCullingPass"
			Cull Front

            HLSLPROGRAM
            half4 frag (v2f i) : SV_Target
            {
				//float f = ComputeFresnel(i.normal, i.viewDirection, _shield_intensity);
				half4 col = 0;			
				half3 normalToGray = (0.299 * i.normal.r) + (0.587 * i.normal.g) + (0.114 * i.normal.b);
				col.rgb = _shield_color_brightness * _shield_color + (abs(normalToGray) * 0.5 - 0.5);
				col.a = _base_alpha * 0.5;
				return col;
            }
            ENDHLSL
        }
    }
}
