Textures set by Custom Shader only appear on Editor restart
For my procedural generated terrain I created a Terrain Generator, which generates Terrain Chunks on request. These Chunks use a custom Material powered by a custom Shader. The Custom Shader enables the use of colors and/or textures on different height levels, that can also blend into another on the edge between the height zones.
The shader works for now, but strangely enough when generating a new Terrain chunk, the terrain textures are not loaded. Only if I restart the Unity Editor, the textures are loaded and displayed as they are supposed to.
I honestly have no idea where this problem comes from. Following the code of the shader and two screenshots of the problem. Keep in mind that these are placeholder textures and the shader is work in progress.
Shader "Custom/TerrainShader"
{
Properties
{
_Color ("Base Color", Color) = (1,1,1,1)
_MainTex ("Texture Area 1 / Base", 2D) = "white" {}
_HeightArea1("Border Height 1 to 2", Range(-100,100)) = 0.2
_BlendArea1("Blend Area 1 to 2", Range(0,100)) = 0.5
_Color2 ("Color2", Color) = (1,1,1,1)
_MainTex2 ("Texture Area 2", 2D) = "white" {}
_HeightArea2("Border Height 2 to 3", Range(-100,100)) = 30
_BlendArea2("Blend Area 2 to 3", Range(0,100)) = 1
_Color3 ("Color3", Color) = (1,1,1,1)
_MainTex3 ("Texture Area 3", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
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 3.0
const static float epsilon = 1E-4;
sampler2D _MainTex;
sampler2D _MainTex2;
sampler2D _MainTex3;
struct Input {
float3 worldPos;
float3 worldNormal;
float2 uv_MainTex;
float2 uv_MainTex2;
float2 uv_MainTex3;
float2 uv_BumpMap;
};
float inverseLerp(float a, float b, float value) {
return saturate((value-a)/(b-a));
}
half _Glossiness;
half _Metallic;
fixed4 _Color;
fixed4 _Color2;
fixed4 _Color3;
half _BlendArea1;
half _BlendArea2;
half _HeightArea1;
half _HeightArea2;
sampler2D _BumpMap;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
float3 blendAxes = abs(IN.worldNormal);
blendAxes /= blendAxes.x + blendAxes.y + blendAxes.z;
// first area, default
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
// border 1
if (IN.worldPos.y > _HeightArea1-_BlendArea1 && IN.worldPos.y < _HeightArea1+_BlendArea1/2){
float heightPercent = inverseLerp(_HeightArea1-_BlendArea1/2,_HeightArea1+_BlendArea1/2, IN.worldPos.y);
float drawStrength = inverseLerp(-_BlendArea1/2, _BlendArea1/2, heightPercent);
o.Albedo = lerp(tex2D (_MainTex, IN.uv_MainTex) * _Color , tex2D (_MainTex2, IN.uv_MainTex2) * _Color2, heightPercent);
}
// second area
if (IN.worldPos.y > _HeightArea1+_BlendArea1/2 && IN.worldPos.y < _HeightArea2-_BlendArea2/2){
fixed4 c = tex2D (_MainTex2, IN.uv_MainTex2) * _Color2;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
// second border
if (IN.worldPos.y > _HeightArea2-_BlendArea2 && IN.worldPos.y < _HeightArea2+_BlendArea2/2){
float heightPercent = inverseLerp(_HeightArea2-_BlendArea2/2,_HeightArea2+_BlendArea2/2, IN.worldPos.y);
o.Albedo = lerp(tex2D (_MainTex2, IN.uv_MainTex2) * _Color2 , tex2D (_MainTex3, IN.uv_MainTex3) * _Color3, heightPercent);
}
// third area
if (IN.worldPos.y > _HeightArea2+_BlendArea2/2){
fixed4 c = tex2D (_MainTex3, IN.uv_MainTex3) * _Color3;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
FallBack "Diffuse"
}
When generating Terrain/Applying the Material&Shader
When restarting Unity