Firstborn/Library/PackageCache/com.unity.terrain-tools@4.0.5/Shaders/Visualization/Builtin/BLT_TerrainVisualizationCom...

83 lines
2.9 KiB
HLSL
Raw Normal View History

2023-03-28 13:24:16 -04:00
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
#ifndef TERRAIN_HEIGHTMAP_COMMON_CGINC_INCLUDED
#define TERRAIN_HEIGHTMAP_COMMON_CGINC_INCLUDED
struct Input
{
float4 tc;
float4 vertex;
#ifndef TERRAIN_BASE_PASS
UNITY_FOG_COORDS(0) // needed because finalcolor oppresses fog code generation.
#endif
};
#if defined(UNITY_INSTANCING_ENABLED) && !defined(SHADER_API_D3D11_9X)
sampler2D _TerrainHeightmapTexture;
sampler2D _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)
#if defined(TERRAIN_BASE_PASS) && defined(UNITY_PASS_META)
// When we render albedo for GI baking, we actually need to take the ST
float4 _MainTex_ST;
#endif
void SplatmapVert(inout appdata_full v, out Input data)
{
UNITY_INITIALIZE_OUTPUT(Input, data);
#if defined(UNITY_INSTANCING_ENABLED) && !defined(SHADER_API_D3D11_9X)
float2 patchVertex = v.vertex.xy;
float4 instanceData = UNITY_ACCESS_INSTANCED_PROP(Terrain, _TerrainPatchInstanceData);
float4 uvscale = instanceData.z * _TerrainHeightmapRecipSize;
float4 uvoffset = instanceData.xyxy * uvscale;
uvoffset.xy += 0.5f * _TerrainHeightmapRecipSize.xy;
float2 sampleCoords = (patchVertex.xy * uvscale.xy + uvoffset.xy);
float hm = UnpackHeightmap(tex2Dlod(_TerrainHeightmapTexture, float4(sampleCoords, 0, 0)));
v.vertex.xz = (patchVertex.xy + instanceData.xy) * _TerrainHeightmapScale.xz * instanceData.z; //(x + xBase) * hmScale.x * skipScale;
v.vertex.y = hm * _TerrainHeightmapScale.y;
v.vertex.w = 1.0f;
v.texcoord.xy = (patchVertex.xy * uvscale.zw + uvoffset.zw);
v.texcoord3 = v.texcoord2 = v.texcoord1 = v.texcoord;
#ifdef TERRAIN_INSTANCED_PERPIXEL_NORMAL
v.normal = float3(0, 1, 0); // TODO: reconstruct the tangent space in the pixel shader. Seems to be hard with surface shader especially when other attributes are packed together with tSpace.
data.tc.zw = sampleCoords;
#else
float3 nor = tex2Dlod(_TerrainNormalmapTexture, float4(sampleCoords, 0, 0)).xyz;
v.normal = 2.0f * nor - 1.0f;
#endif
#endif
v.tangent.xyz = cross(v.normal, float3(0,0,1));
v.tangent.w = -1;
data.tc.xy = v.texcoord.xy;
data.vertex = mul(unity_ObjectToWorld, v.vertex);
#ifdef TERRAIN_BASE_PASS
#ifdef UNITY_PASS_META
data.tc.xy = TRANSFORM_TEX(v.texcoord.xy, _MainTex);
#endif
#else
float4 pos = UnityObjectToClipPos(v.vertex);
UNITY_TRANSFER_FOG(data, pos);
#endif
}
#endif // TERRAIN_HEATMAP_COMMON_CGINC_INCLUDED