151 lines
5.7 KiB
Plaintext
151 lines
5.7 KiB
Plaintext
Shader "Custom/URP_Planet"
|
|
{
|
|
Properties
|
|
{
|
|
_MainTex("Planet Texture", 2D) = "white" {}
|
|
_TextureColor("Texture Color", Color) = (1,1,1,1)
|
|
|
|
_CloudTex("Cloud Texture", 2D) = "black" {}
|
|
_CloudColor("Cloud Color", Color) = (1,1,1,1)
|
|
_CloudSpeed("Cloud Speed", Float) = 0.05
|
|
_CloudIntensity("Cloud Intensity", Range(0,1)) = 0.3
|
|
|
|
_CityLightMap("City Light Map", 2D) = "black" {}
|
|
_CityLightColor("City Light Color", Color) = (1,1,1,1)
|
|
_CityLightIntensity("City Light Intensity", Range(0,2)) = 1
|
|
|
|
_SpecularMap("Specular Map", 2D) = "black" {}
|
|
_SpecularIntensity("Specular Intensity", Range(0,1)) = 0.5
|
|
|
|
_NormalMap("Normal Map", 2D) = "bump" {}
|
|
_NormalStrength("Normal Strength", Float) = 1
|
|
|
|
_AtmosphereColor("Atmosphere Color", Color) = (0.4,0.6,1,1)
|
|
_AtmospherePower("Atmosphere Power", Float) = 4
|
|
}
|
|
|
|
SubShader
|
|
{
|
|
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
|
|
|
|
Pass
|
|
{
|
|
Name "ForwardLit"
|
|
Tags { "LightMode" = "UniversalForward" }
|
|
|
|
HLSLPROGRAM
|
|
|
|
#pragma vertex vert
|
|
#pragma fragment frag
|
|
|
|
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
|
|
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
|
|
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
|
|
|
struct Attributes
|
|
{
|
|
float4 positionOS : POSITION;
|
|
float3 normalOS : NORMAL;
|
|
float4 tangentOS : TANGENT;
|
|
float2 uv : TEXCOORD0;
|
|
};
|
|
|
|
struct Varyings
|
|
{
|
|
float4 positionHCS : SV_POSITION;
|
|
float3 positionWS : TEXCOORD0;
|
|
float3 normalWS : TEXCOORD1;
|
|
float3 tangentWS : TEXCOORD2;
|
|
float3 bitangentWS : TEXCOORD3;
|
|
float2 uv : TEXCOORD4;
|
|
};
|
|
|
|
TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
|
|
TEXTURE2D(_CloudTex); SAMPLER(sampler_CloudTex);
|
|
TEXTURE2D(_CityLightMap); SAMPLER(sampler_CityLightMap);
|
|
TEXTURE2D(_SpecularMap); SAMPLER(sampler_SpecularMap);
|
|
TEXTURE2D(_NormalMap); SAMPLER(sampler_NormalMap);
|
|
|
|
float4 _TextureColor;
|
|
float4 _CloudColor;
|
|
float _CloudSpeed;
|
|
float _CloudIntensity;
|
|
|
|
float4 _CityLightColor;
|
|
float _CityLightIntensity;
|
|
|
|
float _SpecularIntensity;
|
|
float _NormalStrength;
|
|
|
|
float4 _AtmosphereColor;
|
|
float _AtmospherePower;
|
|
|
|
Varyings vert(Attributes v)
|
|
{
|
|
Varyings o;
|
|
|
|
o.positionWS = TransformObjectToWorld(v.positionOS.xyz);
|
|
o.positionHCS = TransformWorldToHClip(o.positionWS);
|
|
|
|
o.normalWS = TransformObjectToWorldNormal(v.normalOS);
|
|
o.tangentWS = TransformObjectToWorldDir(v.tangentOS.xyz);
|
|
o.bitangentWS = cross(o.normalWS, o.tangentWS) * v.tangentOS.w;
|
|
|
|
o.uv = v.uv;
|
|
|
|
return o;
|
|
}
|
|
|
|
float4 frag(Varyings i) : SV_Target
|
|
{
|
|
float3 viewDir = normalize(_WorldSpaceCameraPos - i.positionWS);
|
|
|
|
float3x3 TBN = float3x3(
|
|
normalize(i.tangentWS),
|
|
normalize(i.bitangentWS),
|
|
normalize(i.normalWS)
|
|
);
|
|
|
|
float3 normalTex = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uv).xyz * 2 - 1;
|
|
normalTex.xy *= _NormalStrength;
|
|
float3 normalWS = normalize(mul(normalTex, TBN));
|
|
|
|
Light light = GetMainLight();
|
|
float3 lightDir = normalize(light.direction);
|
|
float3 lightColor = light.color;
|
|
|
|
float NdotL = saturate(dot(normalWS, lightDir));
|
|
|
|
float3 planet = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv).rgb * _TextureColor.rgb;
|
|
|
|
float2 cloudUV = i.uv + float2(_Time.y * _CloudSpeed, 0);
|
|
float3 clouds = SAMPLE_TEXTURE2D(_CloudTex, sampler_CloudTex, cloudUV).rgb * _CloudColor.rgb;
|
|
|
|
float3 baseColor = planet + clouds * _CloudIntensity;
|
|
|
|
float3 city = SAMPLE_TEXTURE2D(_CityLightMap, sampler_CityLightMap, i.uv).rgb;
|
|
float night = 1.0 - NdotL;
|
|
city *= night * _CityLightIntensity * _CityLightColor.rgb;
|
|
|
|
float3 specMap = SAMPLE_TEXTURE2D(_SpecularMap, sampler_SpecularMap, i.uv).rgb;
|
|
float3 reflectDir = reflect(-lightDir, normalWS);
|
|
float spec = pow(saturate(dot(reflectDir, viewDir)), 32);
|
|
float3 specular = spec * specMap * _SpecularIntensity;
|
|
|
|
float fresnel = pow(1.0 - saturate(dot(normalWS, viewDir)), _AtmospherePower);
|
|
float3 atmosphere = fresnel * _AtmosphereColor.rgb;
|
|
|
|
float3 lit = baseColor * NdotL * lightColor;
|
|
float3 ambient = baseColor * 0.25;
|
|
|
|
float3 finalColor = lit + ambient + city + specular + atmosphere;
|
|
|
|
return float4(finalColor, 1.0);
|
|
}
|
|
|
|
ENDHLSL
|
|
}
|
|
}
|
|
} |