// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

Shader "URP/Environment/Sky&Fog/Cubemap_custom" {
Properties {
    _Color ("Tint Color", Color) = (.5, .5, .5, .5)
    [Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
    _Rotation ("Rotation", Range(0, 360)) = 0
	_PrevRotation("Prev Rotation", Range(0, 360)) = 0
	_Blend("Rotation", Range(0, 1)) = 0
    [NoScaleOffset] _MainTex ("Cubemap   (HDR)", Cube) = "grey" {}
	[NoScaleOffset] _PrevTex("Prev Cubemap   (HDR)", Cube) = "grey" {}
}

SubShader {
	Tags{ "Queue" = "Background" "RenderType" = "Background" "PreviewType" = "Skybox" }
    Cull Off ZWrite Off
	ZTest LEqual

    Pass {
        HLSLPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma target 2.0
		//#pragma enable_d3d11_debug_symbols
        // #pragma multi_compile _ REFLECTION_PASS
        #pragma multi_compile_instancing

		#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
		#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
		#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
		#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
		#include "VolumetricFogForObject.hlsl"



        TEXTURECUBE(_MainTex);    SAMPLER(sampler_MainTex);
		TEXTURECUBE(_PrevTex);    SAMPLER(sampler_PrevTex);
        CBUFFER_START(UnityPerMaterial)
            half4 _MainTex_HDR;
			half4 _PrevTex_HDR;
            half4 _Color;
            half _Exposure;
            float _Rotation;
			float _PrevRotation;
			float _Blend;
			float _isFog;
         CBUFFER_END

         uniform half _SceneBloomFactor;

        float3 RotateAroundYInDegrees (float3 vertex, float degrees)
        {
            float alpha = degrees * PI / 180.0;
            float sina, cosa;
            sincos(alpha, sina, cosa);
            float2x2 m = float2x2(cosa, -sina, sina, cosa);
            return float3(mul(m, vertex.xz), vertex.y).xzy;
        }

        struct appdata_t {
            float4 vertex : POSITION;
            UNITY_VERTEX_INPUT_INSTANCE_ID
        };

        struct v2f {
            float4 pos : SV_POSITION;
            float3 texcoord : TEXCOORD0;
			float4 fogCoord : TEXCOORD1;
            UNITY_VERTEX_INPUT_INSTANCE_ID
        };

        v2f vert (appdata_t v)
        {
            v2f o;
            UNITY_SETUP_INSTANCE_ID(v);
            UNITY_TRANSFER_INSTANCE_ID(v,o);
            float3 ori_vertex = v.vertex.xyz;
            //float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
            //v.vertex = float4(rotated,1.0);
			//o.pos = TransformObjectToHClip(ori_vertex);
			VertexPositionInputs vertexInput = GetVertexPositionInputs(ori_vertex.xyz);
			o.pos = vertexInput.positionCS;
            o.texcoord = ori_vertex;

			o.fogCoord = VolumetricFog_GetFogCoord(vertexInput.positionNDC, vertexInput.positionVS.z);

            return o;
        }

#ifdef UNITY_COLORSPACE_GAMMA
#define unity_ColorSpaceDouble half4(2.0, 2.0, 2.0, 2.0)
#else // Linear values
#define unity_ColorSpaceDouble half4(4.59479380, 4.59479380, 4.59479380, 2.0)
#endif

		// Decodes HDR textures
		// handles dLDR, RGBM formats
		inline half3 DecodeHDR(half4 data, half4 decodeInstructions)
		{
			// Take into account texture alpha if decodeInstructions.w is true(the alpha value affects the RGB channels)
			half alpha = decodeInstructions.w * (data.a - 1.0) + 1.0;

			// If Linear mode is not supported we can skip exponent part
#if defined(UNITY_COLORSPACE_GAMMA)
			return (decodeInstructions.x * alpha) * data.rgb;
#else
#   if defined(UNITY_USE_NATIVE_HDR)
			return decodeInstructions.x * data.rgb; // Multiplier for future HDRI relative to absolute conversion.
#   else
			return (decodeInstructions.x * pow(alpha, decodeInstructions.y)) * data.rgb;
#   endif
#endif
		}

        half4 frag (v2f i) : SV_Target
        {
            UNITY_SETUP_INSTANCE_ID(i);
            //X2M_PIXEL_RECT_CULL(i)
            half4 tex = SAMPLE_TEXTURECUBE (_MainTex, sampler_MainTex, RotateAroundYInDegrees(i.texcoord, _Rotation));
			half4 prevTex = SAMPLE_TEXTURECUBE(_PrevTex, sampler_PrevTex, RotateAroundYInDegrees(i.texcoord, _PrevRotation));
            half3 c1 = DecodeHDR (tex, _MainTex_HDR);
			half3 c2 = DecodeHDR(prevTex, _PrevTex_HDR);
			half3 c = lerp(c2, c1, _Blend);
            c = c * _Color.rgb * unity_ColorSpaceDouble.rgb;
            c *= _Exposure;
		
			c.rgb = lerp(c.rgb, VolumetricFog_GetFog(c.rgb, i.fogCoord), _isFog);

            return half4(c, _SceneBloomFactor);
        }
        ENDHLSL
    }
}
Fallback Off
}
