I'm trying to follow a tutorial and set some simple shader properties, but according to the Inspector, they aren't being set. I'm new to shaders, not quite sure what I'm doing wrong.
I have a MeshA that has been assigned MaterialA. The mesh itself is adjusted/generated via code, run through an Editor Script-added button.
The terrainMaterial I am changing in my code below, is a reference to the material ASSET, not this MeshA.
When playing the game, I also spawn (lets say 10) new Mesh Objects, all of which use MaterialA.
The code in game:
protected void UpdateMeshHeights()
{
Debug.Log($"------------------------- Updating Mesh Heights ------------------------");
//Shader.SetGlobalFloat("_minHeight", generationData.MinHeight);
//Shader.SetGlobalFloat("_maxHeight", generationData.MaxHeight);
terrainMaterial.SetFloat("_minHeight", generationData.MinHeight);
terrainMaterial.SetFloat("_maxHeight", generationData.MaxHeight);
Debug.Log($"Shader Min Height: {terrainMaterial.GetFloat("_minHeight")}"); // value correct
Debug.Log($"Shader Max Height: {terrainMaterial.GetFloat("_maxHeight")}"); // value correct
}
The custom shader code:
Shader "Custom/TerrainShader"
{
Properties
{
_minHeight ("_minHeight", float) = 0.0
_maxHeight ("_maxHeight", float) = 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
float _minHeight;
float _maxHeight;
struct Input
{
float3 worldPos;
};
float inverseLerp(float a, float b, float value)
{
return saturate((value-a)/(b - a));
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
float proportion = inverseLerp(_minHeight, _maxHeight, IN.worldPos.y);
o.Albedo = proportion;
}
ENDCG
}
FallBack "Diffuse"
}
So when reading the values, e.g. _maxHeight, it shows the value I expect. However, the properties on the material still say 0 (and the material is all white, per the inverse lerp calculation with values of 0 for both a and b).
I used global floats temporarily (without anything in Properties) to check the code is working, I see the shader doing exactly what I expect so the issue is definitely how I'm setting (or not setting rather) the properties.
EDIT: Added extra context