浏览代码

Merge pull request #1901 from Unity-Technologies/gpu-terrain

Enabled per-pixel terrain normal on LW terrain shader.
/main
GitHub 6 年前
当前提交
237718e2
共有 8 个文件被更改,包括 55 次插入30 次删除
  1. 3
      com.unity.render-pipelines.high-definition/CHANGELOG.md
  2. 1
      com.unity.render-pipelines.lightweight/CHANGELOG.md
  3. 52
      com.unity.render-pipelines.lightweight/LWRP/ShaderLibrary/Terrain/LightweightPassLitTerrain.hlsl
  4. 5
      com.unity.render-pipelines.lightweight/LWRP/Shaders/Terrain/LightweightStandardTerrain.shader
  5. 2
      com.unity.render-pipelines.lightweight/LWRP/Shaders/Terrain/LightweightStandardTerrainBase.shader
  6. 4
      com.unity.testing.srp.lightweight/Tests/Scenes/035_Shader_TerrainShaders.unity
  7. 10
      com.unity.testing.srp.lightweight/Tests/Scenes/035_Shader_TerrainShaders/New Terrain.asset
  8. 8
      com.unity.testing.srp.lightweight/Tests/Scenes/035_Shader_TerrainShaders/Terrain.mat

3
com.unity.render-pipelines.high-definition/CHANGELOG.md


## [3.4.0-preview]
### Added
- Added a new TerrainLit shader that supports rendering of Unity terrains.
### Fixed
- Fixed an issue where sometimes the deferred shadow texture would not be valid, causing wrong rendering.

1
com.unity.render-pipelines.lightweight/CHANGELOG.md


- The `RenderingData` struct now holds a reference to `CullResults`.
- When __HDR__ is enabled in the Camera but disabled in the Asset, an information box in the Camera Inspector informs you about it.
- When __MSAA__ is enabled in the Camera but disabled in the Asset, an information box in the Camera Inspector informs you about it.
- Enabled instancing on the terrain shader.
### Changed
- The `RenderingData` struct is now read-only.
- `ScriptableRenderer`always perform a Clear before calling `IRendererSetup::Setup.`

52
com.unity.render-pipelines.lightweight/LWRP/ShaderLibrary/Terrain/LightweightPassLitTerrain.hlsl


#include "LWRP/ShaderLibrary/Lighting.hlsl"
#if defined(UNITY_INSTANCING_ENABLED) && defined(_TERRAIN_INSTANCED_PERPIXEL_NORMAL)
#define ENABLE_TERRAIN_PERPIXEL_NORMAL
#endif
#ifdef UNITY_INSTANCING_ENABLED
TEXTURE2D(_TerrainHeightmapTexture);
TEXTURE2D(_TerrainNormalmapTexture);
SAMPLER(sampler_TerrainNormalmapTexture);
float4 _TerrainHeightmapRecipSize; // float4(1.0f/width, 1.0f/height, 1.0f/(width-1), 1.0f/(height-1))
float4 _TerrainHeightmapScale; // float4(hmScale.x, hmScale.y / (float)(kMaxHeight), hmScale.z, 0.0f)
#endif
UNITY_INSTANCING_BUFFER_START(Terrain)
UNITY_DEFINE_INSTANCED_PROP(float4, _TerrainPatchInstanceData) // float4(xBase, yBase, skipScale, ~)
UNITY_INSTANCING_BUFFER_END(Terrain)
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID

float4 uvSplat23 : TEXCOORD2; // xy: splat2, zw: splat3
#endif
#if _NORMALMAP
#if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
half4 normal : TEXCOORD3; // xyz: normal, w: viewDir.x
half4 tangent : TEXCOORD4; // xyz: tangent, w: viewDir.y
half4 binormal : TEXCOORD5; // xyz: binormal, w: viewDir.z

input.positionWS = IN.positionWS;
#ifdef _NORMALMAP
#if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
#elif defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
half3 viewDir = IN.viewDir;
float2 sampleCoords = (IN.uvMainAndLM.xy / _TerrainHeightmapRecipSize.zw + 0.5f) * _TerrainHeightmapRecipSize.xy;
half3 normalWS = TransformObjectToWorldNormal(normalize(SAMPLE_TEXTURE2D(_TerrainNormalmapTexture, sampler_TerrainNormalmapTexture, sampleCoords).rgb * 2 - 1));
half3 tangentWS = cross(GetObjectToWorldMatrix()._13_23_33, normalWS);
input.normalWS = TangentToWorldNormal(normalTS, tangentWS, cross(normalWS, tangentWS), normalWS);
#else
half3 viewDir = IN.viewDir;
input.normalWS = FragmentNormalWS(IN.normal);

nrm += SAMPLE_TEXTURE2D(_Normal2, sampler_Normal0, IN.uvSplat23.xy) * splatControl.b;
nrm += SAMPLE_TEXTURE2D(_Normal3, sampler_Normal0, IN.uvSplat23.zw) * splatControl.a;
mixedNormal = UnpackNormal(nrm);
#else
mixedNormal = half3(0.0h, 0.0h, 1.0h);
#endif
}

ApplyFog(color.rgb, fogCoord);
#endif
}
#ifdef UNITY_INSTANCING_ENABLED
TEXTURE2D(_TerrainHeightmapTexture);
TEXTURE2D(_TerrainNormalmapTexture);
float4 _TerrainHeightmapRecipSize; // float4(1.0f/width, 1.0f/height, 1.0f/(width-1), 1.0f/(height-1))
float4 _TerrainHeightmapScale; // float4(hmScale.x, hmScale.y / (float)(kMaxHeight), hmScale.z, 0.0f)
#endif
UNITY_INSTANCING_BUFFER_START(Terrain)
UNITY_DEFINE_INSTANCED_PROP(float4, _TerrainPatchInstanceData) // float4(xBase, yBase, skipScale, ~)
UNITY_INSTANCING_BUFFER_END(Terrain)
void TerrainInstancing(inout float4 vertex, inout float3 normal, inout float2 uv)
{

vertex.xz = sampleCoords * _TerrainHeightmapScale.xz;
vertex.y = height * _TerrainHeightmapScale.y;
normal = _TerrainNormalmapTexture.Load(int3(sampleCoords, 0)).rgb * 2 - 1;
#ifdef ENABLE_TERRAIN_PERPIXEL_NORMAL
normal = float3(0, 1, 0);
#else
normal = _TerrainNormalmapTexture.Load(int3(sampleCoords, 0)).rgb * 2 - 1;
#endif
uv = sampleCoords * _TerrainHeightmapRecipSize.zw;
#endif
}

half3 viewDir = VertexViewDirWS(GetCameraPositionWS() - positionWS.xyz);
#ifdef _NORMALMAP
float4 vertexTangent = float4(cross(v.normal, float3(0, 0, 1)), -1.0);
#if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
float4 vertexTangent = float4(cross(float3(0, 0, 1), v.normal), 1.0);
OutputTangentToWorld(vertexTangent, v.normal, o.tangent.xyz, o.binormal.xyz, o.normal.xyz);
o.normal.w = viewDir.x;

// Used in Standard Terrain shader
half4 SplatmapFragment(VertexOutput IN) : SV_TARGET
{
half3 normalTS = half3(0.0h, 0.0h, 1.0h);
half3 normalTS = float3(0, 1, 0);
half3 albedo = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uvMainAndLM.xy).rgb;
half smoothness = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uvMainAndLM.xy).a;
half metallic = SAMPLE_TEXTURE2D(_MetallicTex, sampler_MetallicTex, IN.uvMainAndLM.xy).r;

half weight;
half4 mixedDiffuse;
half4 defaultSmoothness = half4(_Smoothness0, _Smoothness1, _Smoothness2, _Smoothness3);
half3 normalTS;
SplatmapMix(IN, defaultSmoothness, splatControl, weight, mixedDiffuse, normalTS);
half3 albedo = mixedDiffuse.rgb;

5
com.unity.render-pipelines.lightweight/LWRP/Shaders/Terrain/LightweightStandardTerrain.shader


// used in fallback on old cards & base map
[HideInInspector] _MainTex("BaseMap (RGB)", 2D) = "grey" {}
[HideInInspector] _Color("Main Color", Color) = (1,1,1,1)
// TODO: Implement ShaderGUI for the shader and display the checkbox only when instancing is enabled.
[Toggle(_TERRAIN_INSTANCED_PERPIXEL_NORMAL)] _TERRAIN_INSTANCED_PERPIXEL_NORMAL("Enable Instanced Per-pixel Normal", Float) = 0
}
SubShader

#pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap
#pragma shader_feature _NORMALMAP
// Sample normal in pixel shader when doing instancing
#pragma shader_feature _TERRAIN_INSTANCED_PERPIXEL_NORMAL
#include "LWRP/ShaderLibrary/Terrain/InputSurfaceTerrain.hlsl"
#include "LWRP/ShaderLibrary/Terrain/LightweightPassLitTerrain.hlsl"

2
com.unity.render-pipelines.lightweight/LWRP/Shaders/Terrain/LightweightStandardTerrainBase.shader


#pragma fragment SplatmapFragment
#pragma shader_feature _NORMALMAP
// Sample normal in pixel shader when doing instancing
#pragma shader_feature _TERRAIN_INSTANCED_PERPIXEL_NORMAL
#define TERRAIN_SPLAT_BASEPASS 1
#include "LWRP/ShaderLibrary/Terrain/InputSurfaceTerrain.hlsl"

4
com.unity.testing.srp.lightweight/Tests/Scenes/035_Shader_TerrainShaders.unity


m_HeightmapMaximumLOD: 0
m_CastShadows: 1
m_DrawHeightmap: 1
m_DrawInstanced: 0
m_DrawInstanced: 1
m_DrawTreesAndFoliage: 1
m_ReflectionProbeUsage: 1
m_MaterialType: 3

m_PreserveTreePrototypeLayers: 0
m_ScaleInLightmap: 0.0512
m_LightmapParameters: {fileID: 15203, guid: 0000000000000000f000000000000000, type: 0}
m_GroupingID: 0
m_AllowAutoConnect: 0
--- !u!4 &745712513
Transform:
m_ObjectHideFlags: 0

10
com.unity.testing.srp.lightweight/Tests/Scenes/035_Shader_TerrainShaders/New Terrain.asset
文件差异内容过多而无法显示
查看文件

8
com.unity.testing.srp.lightweight/Tests/Scenes/035_Shader_TerrainShaders/Terrain.mat


Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_EnableInstancingVariants: 0
m_EnableInstancingVariants: 1
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}

- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _TERRAIN_INSTANCED_PERPIXEL_NORMAL: 0
- _UVSec: 0
- _WorkflowMode: 1
- _ZWrite: 1

正在加载...
取消
保存