Hello, I am new to unity and currently working on a project for procedural world generation following a tutorial from Sebastian Lague but deviating of course. Instead of determining my textures on height I want to determine it by a given index.
I am writing the surface shader code to place textures on my procedurally generated terrain mesh, I’m having trouble with the transition between different biomes. I am passing the texture index on every vertice in my mesh. the shader will then interpolate the value of texture index which results in the shader looping through all values between the 2 different biomes in the texture array which I don’t want to happen. Picture of what is happening: texture index problem - Album on Imgur
Shader code:
Shader "Custom/Blend"
{
Properties {
_BaseTextures ("Terrain Textures", 2DArray) = "" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 600
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 4.0
const static int maxLayerCount = 10;
const static float epsilon = 1E-4;
float3 baseColours[maxLayerCount];
float baseStartHeights[maxLayerCount];
float baseBlends[maxLayerCount];
float baseColourStrength[maxLayerCount];
float baseTextureScales[maxLayerCount];
UNITY_DECLARE_TEX2DARRAY(_BaseTextures);
struct Input
{
float2 uv_BaseTextures;
float3 worldPos;
float3 worldNormal;
};
float3 triplanar(float3 worldPos, float scale, float3 blendAxes, float index) {
float3 scaledWorldPos = worldPos / scale;
float3 xProjection = UNITY_SAMPLE_TEX2DARRAY(_BaseTextures, float3(scaledWorldPos.y, scaledWorldPos.z, index)) * blendAxes.x;
float3 yProjection = UNITY_SAMPLE_TEX2DARRAY(_BaseTextures, float3(scaledWorldPos.x, scaledWorldPos.z, index)) * blendAxes.y;
float3 zProjection = UNITY_SAMPLE_TEX2DARRAY(_BaseTextures, float3(scaledWorldPos.x, scaledWorldPos.y, index)) * blendAxes.z;
return xProjection + yProjection + zProjection;
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
int i = IN.uv_BaseTextures.y;
float3 blendAxes = abs(IN.worldNormal);
float drawStrength = 1;
float3 baseColour = baseColours[i] * baseColourStrength[i];
float3 textureColour = triplanar(IN.worldPos, baseTextureScales[i], blendAxes, i) * (1 - baseColourStrength[i]);
o.Albedo = o.Albedo * (1 - drawStrength) + (baseColour + textureColour) * drawStrength;
}
ENDCG
}
FallBack "Diffuse"
}