浏览代码

Converted Terrain base shader to new shader library.

/main
Felipe Lira 7 年前
当前提交
82d0bec8
共有 2 个文件被更改,包括 75 次插入52 次删除
  1. 62
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightCore.hlsl
  2. 65
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightStandardTerrain.shader

62
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightCore.hlsl


return inVec * rsqrt(dp3);
}
half3 UnpackNormalScale(half4 packednormal, half bumpScale)
// Unpack normal as DXT5nm (1, y, 1, x) or BC5 (x, y, 0, 1)
// Note neutral texture like "bump" is (0, 0, 1, 1) to work with both plain RGB normal and DXT5nm/BC5
half3 UnpackNormalmapRGorAG(half4 packedNormal, half bumpScale)
#if defined(UNITY_NO_DXT5nm)
half3 normal = packednormal.xyz * 2 - 1;
#if (SHADER_TARGET >= 30)
// SM2.0: instruction count limitation
// SM2.0: normal scaler is not supported
// This do the trick
packedNormal.x *= packedNormal.w;
half3 normal;
normal.xy = packedNormal.xy * 2 - 1;
#endif
normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
#else
// This do the trick
packednormal.x *= packednormal.w;
}
half3 normal;
normal.xy = (packednormal.xy * 2 - 1);
#if (SHADER_TARGET >= 30)
// SM2.0: instruction count limitation
// SM2.0: normal scaler is not supported
half3 UnpackNormalRGB(half4 packedNormal, half bumpScale)
{
half3 normal = packedNormal.xyz * 2 - 1;
#endif
normal.z = sqrt(1.0 - saturate(dot(normal.xy, normal.xy)));
}
half3 UnpackNormal(half4 packedNormal)
{
// Compiler will optimize the scale away
#if defined(UNITY_NO_DXT5nm)
return UnpackNormalRGB(packedNormal, 1.0)
#else
return UnpackNormalmapRGorAG(packedNormal, 1.0);
#endif
}
half3 UnpackNormalScale(half4 packedNormal, half bumpScale)
{
#if defined(UNITY_NO_DXT5nm)
return UnpackNormalRGB(packedNormal, bumpScale)
#else
return UnpackNormalmapRGorAG(packedNormal, bumpScale);
#endif
}

return max(half3(0, 0, 0), SampleSH(normalWS));
}
half3 SampleGI(float4 sampleData, half3 normalWS)
half3 SampleLightmap(float2 lightmapUV, half3 normalWS)
#ifdef LIGHTMAP_ON
// Only baked GI is sample as dynamic GI is not supported in Lightweight
#ifdef UNITY_LIGHTMAP_FULL_HDR
bool encodedLightmap = false;

#ifdef DIRLIGHTMAP_COMBINED
return SampleDirectionalLightmap(TEXTURE2D_PARAM(unity_Lightmap, samplerunity_Lightmap),
TEXTURE2D_PARAM(unity_LightmapInd, samplerunity_Lightmap),
sampleData.xy, transformCoords, normalWS, encodedLightmap);
lightmapUV, transformCoords, normalWS, encodedLightmap);
return SampleSingleLightmap(TEXTURE2D_PARAM(unity_Lightmap, samplerunity_Lightmap), sampleData.xy, transformCoords, encodedLightmap);
return SampleSingleLightmap(TEXTURE2D_PARAM(unity_Lightmap, samplerunity_Lightmap), lightmapUV, transformCoords, encodedLightmap);
}
half3 SampleGI(float4 sampleData, half3 normalWS)
{
#ifdef LIGHTMAP_ON
return SampleLightmap(sampleData.xy, normalWS);
#endif // LIGHTMAP_ON
// If lightmap is not enabled we sample GI from SH

65
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightStandardTerrain.shader


Pass
{
Tags { "LightMode" = "LightweightForward" }
CGPROGRAM
HLSLPROGRAM
// needs more than 8 texcoords
#pragma exclude_renderers gles psp2
#include "LightweightLighting.cginc"
#pragma multi_compile _ _MAIN_LIGHT_COOKIE
#pragma multi_compile _MAIN_DIRECTIONAL_LIGHT _MAIN_SPOT_LIGHT
#pragma multi_compile _ _ADDITIONAL_LIGHTS

#pragma multi_compile __ _TERRAIN_NORMAL_MAP
#pragma multi_compile_fog
#define TERRAIN_STANDARD_SHADER
#define TERRAIN_SURFACE_OUTPUT SurfaceOutputStandard
#include "LightweightLighting.hlsl"
CBUFFER_START(_Terrain)
half _Metallic0;
half _Metallic1;
half _Metallic2;

half _Smoothness2;
half _Smoothness3;
sampler2D _Control;
sampler2D _Splat0, _Splat1, _Splat2, _Splat3;
CBUFFER_END
sampler2D _Control;
sampler2D _Splat0, _Splat1, _Splat2, _Splat3;
struct VertexInput
{
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
half3 normal : TEXCOORD3;
half3 tangent : TEXCOORD3;
half3 binormal : TEXCOORD4;
half3 normal : TEXCOORD5;
#else
half3 normal : TEXCOORD3;
half3 tangent : TEXCOORD4;
half3 binormal : TEXCOORD5;
#endif
half4 fogFactorAndVertexLight : TEXCOORD6; // x: fogFactor, yzw: vertex light
float3 positionWS : TEXCOORD7;

void SplatmapMix(VertexOutput IN, half4 defaultAlpha, out half4 splat_control, out half weight, out fixed4 mixedDiffuse, inout fixed3 mixedNormal)
void SplatmapMix(VertexOutput IN, half4 defaultAlpha, out half4 splat_control, out half weight, out half4 mixedDiffuse, inout half3 mixedNormal)
{
splat_control = tex2D(_Control, IN.uvControlAndLM.xy);
weight = dot(splat_control, half4(1, 1, 1, 1));

mixedDiffuse += splat_control.a * tex2D(_Splat3, IN.uvSplat23.zw) * half4(1.0, 1.0, 1.0, defaultAlpha.a);
#ifdef _TERRAIN_NORMAL_MAP
fixed4 nrm = 0.0f;
half4 nrm = 0.0f;
nrm += splat_control.r * tex2D(_Normal0, IN.uvSplat01.xy);
nrm += splat_control.g * tex2D(_Normal1, IN.uvSplat01.zw);
nrm += splat_control.b * tex2D(_Normal2, IN.uvSplat23.xy);

mixedNormal = fixed3(0, 0, 1);
mixedNormal = half3(0, 0, 1);
VertexOutput SplatmapVert(appdata_full v)
VertexOutput SplatmapVert(VertexInput v)
UNITY_INITIALIZE_OUTPUT(VertexOutput, o);
float4 clipPos = UnityObjectToClipPos(v.vertex);
float3 positionWS = mul(unity_ObjectToWorld, v.vertex).xyz;
float3 positionWS = TransformObjectToWorld(v.vertex.xyz);
float4 clipPos = TransformWorldToHClip(positionWS);
o.uvSplat01.xy = TRANSFORM_TEX(v.texcoord, _Splat0);
o.uvSplat01.zw = TRANSFORM_TEX(v.texcoord, _Splat1);

float4 vertexTangent = float4(cross(v.normal, float3(0, 0, 1)), -1.0);
OutputTangentToWorld(vertexTangent, v.normal, o.tangent, o.binormal, o.normal);
#else
o.normal = UnityObjectToWorldNormal(v.normal);
o.normal = TransformObjectToWorldNormal(v.normal);
#endif
o.fogFactorAndVertexLight.x = ComputeFogFactor(clipPos.z);
o.fogFactorAndVertexLight.yzw = VertexLighting(positionWS, o.normal);

{
half4 splat_control;
half weight;
fixed4 mixedDiffuse;
half4 mixedDiffuse;
half4 defaultSmoothness = half4(_Smoothness0, _Smoothness1, _Smoothness2, _Smoothness3);
half3 normalTangent;
SplatmapMix(IN, defaultSmoothness, splat_control, weight, mixedDiffuse, normalTangent);

half3 indirectDiffuse = half3(0, 0, 0);
#if LIGHTMAP_ON
float2 lightmapUV = IN.uvControlAndLM.zw;
float4 lightmapUV = half4(IN.uvControlAndLM.zw, 0.0, 0.0);
indirectDiffuse = SampleLightmap(lightmapUV, normalWS);
#endif

IN.fogFactorAndVertexLight.yzw, albedo, metallic, specular, smoothness, /* occlusion */ 1.0, /* emission */ half3(0, 0, 0), alpha);
ApplyFog(color.rgb, fogFactor);
return OUTPUT_COLOR(color);
return color;
ENDCG
ENDHLSL
}
Pass

ZWrite On
ColorMask 0
CGPROGRAM
HLSLPROGRAM
#include "UnityCG.cginc"
#include "LightweightCore.hlsl"
return UnityObjectToClipPos(pos);
return TransformObjectToHClip(pos.xyz);
}
half4 frag() : SV_TARGET

ENDCG
ENDHLSL
}
}
正在加载...
取消
保存