worldNormal in custom Terrain shader making texture greyscale

Hey,

Context:
New to shaders, trying to be able to apply a texture to a custom mesh.

Specifically I’m following this series and I’m on this video at the step on 9:28:

I suspect the problem is with my use of the worldNormal, however it follows exactly how it is used in the video. I am using the most recent version of unity however and this video series appears to be over a year old.

Would anyone know why my texture is turning grey when I apply the worldNormal to my math? I would greatly appreciate any help.

More info on problem below.

Problem:
My shader is identical to the video as far as I can tell, but when I try to use the triplanar mapping it turns the entire texture greyscale

here is my current version of the shader:

Shader "Custom/Terrain" {
Properties {
testTexture("Texture", 2D) = "white"{}
testScale("Scale", Float) = 1
}
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 int maxColourCount = 8;
const static float epsilon = 1E-4;

int baseColourCount;
float3 baseColours[maxColourCount];
float baseStartHeights[maxColourCount];
float baseBlends[maxColourCount];

float minHeight;
float maxHeight;

sampler2D testTexture;
float testScale;

struct Input {
float3 worldPos;
float3 worldNormal;
};

float inverseLerp(float a, float b, float value) {
return saturate((value - a) / (b - a));
}

void surf (Input IN, inout SurfaceOutputStandard o) {

// Triplanar mapping
float3 scaledWorldPos = IN.worldPos / testScale;
float3 blendAxes = abs(IN.worldNormal);

// This just makes the greyscale even darker so comment it out until I fix this.
//blendAxes /= blendAxes.x + blendAxes.y + blendAxes.z;

float xProjection = tex2D(testTexture, scaledWorldPos.yz) * blendAxes.x;
float yProjection = tex2D(testTexture, scaledWorldPos.xz) * blendAxes.y;
float zProjection = tex2D(testTexture, scaledWorldPos.xy) * blendAxes.z;

o.Albedo = xProjection + yProjection + zProjection;

}

ENDCG
}
FallBack "Diffuse"
}
// if I simply replace
o.Albedo = xProjection + yProjection + zProjection;

// with
o.Albedo = tex2D(testTexture, scaledWorldPos.xy);
//then the texture turns back to green, but once again has the initial issue of looking bad on certain axes.

I also uploaded the texture I am using.

You have float values where you should have float3 or float4, so you’re only getting and blending the red channel from the three textures.

1 Like

Yay that did it, thanks!

I can’t believe I didn’t see that.