- Home /
Question by
$$anonymous$$ · Jun 09, 2020 at 08:53 PM ·
shaderprocedural meshheightmaptexturingprocedural texturing
How to apply specific texture for specific triangle in mesh based on height in shader?
Hi, I've created a terrain and I can paint each individual triangle in the right color based on the height of the middle of its edge.
public void CalculateColors(){
float сentre;
for (int i = 0; i < triangles.Length; i = i + 3){
сentre = (vertices[i].y+vertices[i + 1].y)/2;
if (сentre > 100){
colors[i] = Color.blue;
colors[i + 1] = Color.blue;
colors[i + 2] = Color.blue;
}
}
}
but I want to apply texture for each triangle in mehs. I know how to do it for each pixel.
void surf (Input IN, inout SurfaceOutputStandard o) {
float heightPercent = inverseLerp(minHeight,maxHeight, IN.worldPos.y);
float3 blendAxes = abs(IN.worldNormal);
blendAxes /= blendAxes.x + blendAxes.y + blendAxes.z;
for (int i = 0; i < layerCount; i ++) {
float drawStrength = inverseLerp(-baseBlends[i]/2 - epsilon, baseBlends[i]/2, heightPercent - baseStartHeights[i]);
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;
}
}
I thought to use colors of mesh as mask for textures, but I'm not sure is it good way or not.
Maybe there is a way to do it right? Thank you for any help in advance.
Comment
Your answer
Follow this Question
Related Questions
Procedural UV map on a procedural mesh 2 Answers
How to do texture bombing in shader graph 0 Answers
Using tex2Dbias in shader results in compiler error 0 Answers
HeightMap Problem in my mesh 1 Answer