After upgrading to version 5.3.4f1, my terrain shader no longer works
Originally I wrote this shader because the terrain "follows" the player and I did not know of a way to access the "painting" data, so I would have the shader use the world position to determine the colors of the control texture (I use a different method to handle the displacement of the map), and use the channels of the control texture as masks for the other textures. It worked perfectly until I upgraded to the latest version. After doing so I found that my custom terrain shader was no longer working correctly. No matter which texture channel I use, I still get the same result: the shader only uses the last texture, a triplaner combination of _ExtraTex5 and _ExtraTex5h. The import settings for the control texture have were not changed.
Shader "Custom/CombinedTerrainShader" {
Properties {
_GlobalTextureScale ("Global Texture Scale", Range(0.0001, 10)) = 1
_TextureScale ("Texture Scale", Range(0.0001, 1)) = 1
_TriplanarBlendSharpness ("Blend Sharpness", Range(0.1, 10)) = 1
_Control ("Mixing Texture", 2D) = "white" {}
_ExtraTex1 ("Grass", 2D) = "black" {}
_ExtraTex2 ("Sand", 2D) = "green" {}
_ExtraTex3 ("Light Seabed Top", 2D) = "yellow" {}
_ExtraTex3h ("Light Seabed Side", 2D) = "yellow" {}
_ExtraTex4 ("Seabed Top", 2D) = "red" {}
_ExtraTex4h ("Seabed Side", 2D) = "red" {}
_ExtraTex5 ("Crevice Top", 2D) = "blue" {}
_ExtraTex5h ("Crevice Side", 2D) = "blue" {}
//_Glossiness ("Smoothness", Range(0,1)) = 0.5
_MainTex ("Basemap RGB", 2D) = "white" {}
}
SubShader {
//Name "Vertex Deform"
Tags {"Queue" = "Geometry-100" "RenderType"="Opaque"}
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Lambert vertex:vert
//#include "AutoLight.cginc"
#include "UnityPBSLighting.cginc"
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _Control;
sampler2D _ExtraTex1;
sampler2D _ExtraTex2;
sampler2D _ExtraTex3;
sampler2D _ExtraTex3h;
sampler2D _ExtraTex4;
sampler2D _ExtraTex4h;
sampler2D _ExtraTex5;
sampler2D _ExtraTex5h;
float _TextureScale;
float _TriplanarBlendSharpness;
float _GlobalTextureScale;
struct Input {
float2 uv_Control;
float3 worldPos;
float3 worldNormal;
};
void vert (inout appdata_full v) {
float3 worldPos = mul(_Object2World, v.vertex).xyz * _GlobalTextureScale;
}
fixed3 mixTextures(half2 yuv, half2 xuv, half2 zuv, sampler2D sampleVert, sampler2D sampleHorz, half3 blendWeights){
fixed3 yDiff = tex2D (sampleHorz, yuv);
fixed3 xDiff = tex2D (sampleVert, xuv);
fixed3 zDiff = tex2D (sampleHorz, zuv);
return xDiff * blendWeights.x + yDiff * blendWeights.y + zDiff * blendWeights.z;
}
void surf (Input IN, inout SurfaceOutput o) {
// Find our UVs for each axis based on world position of the fragment.
half2 yUV = IN.worldPos.xz * _TextureScale;
half2 xUV = IN.worldPos.zy * _TextureScale;
half2 zUV = IN.worldPos.xy * _TextureScale;
half2 gloYUV = IN.worldPos.xz * _GlobalTextureScale;
// Now do texture samples from our diffuse map with each of the 3 UV set's we've just made.
// Get the absolute value of the world normal.
// Put the blend weights to the power of BlendSharpness, the higher the value,
// the sharper the transition between the planar maps will be.
fixed3 blendWeights = pow (abs(IN.worldNormal), _TriplanarBlendSharpness);
// Divide our blend mask by the sum of it's components, this will make x+y+z=1
blendWeights = blendWeights / (blendWeights.x + blendWeights.y + blendWeights.z);
// Finally, blend together all three samples based on the blend mask.
//o.Albedo = xDiff * blendWeights.x + yDiff * blendWeights.y + zDiff * blendWeights.z;
fixed4 mixTex = tex2D (_Control, gloYUV);
fixed3 tex1 = tex2D (_ExtraTex1, yUV);
fixed3 tex2 = tex2D (_ExtraTex2, yUV);
fixed3 tex3 = mixTextures(yUV, xUV, zUV, _ExtraTex3, _ExtraTex3h, blendWeights);
fixed3 tex4 = mixTextures(yUV, xUV, zUV, _ExtraTex4, _ExtraTex4h, blendWeights);
fixed3 tex5 = mixTextures(yUV, xUV, zUV, _ExtraTex5, _ExtraTex5h, blendWeights);
float gloss = lerp(lerp(0, clamp(IN.worldPos.y, 0, 0.1) * 2.5, mixTex.a), 0, mixTex.g);
fixed3 c1 = lerp(tex1, tex2, mixTex.a);
fixed3 c2 = lerp(c1, tex3, mixTex.g);
fixed3 c3 = lerp(c2, tex4, mixTex.r);
fixed3 c = lerp(c3, tex5, mixTex.b);
//c = lerp(tex1, tex2, 0.5);
//tex1 = grass, tex2 = sand, tex3 = seabed, tex4 = deep seabed, tex5 = rocks
//fixed3 c = lerp(lerp(lerp(lerp(tex1, tex2, mixTex.a), tex3, mixTex.g), tex4, mixTex.r), tex5, mixTex.b);
o.Albedo = c.rgb;
o.Alpha = 1;
}
ENDCG
}
FallBack "Diffuse"
}
Your answer
Follow this Question
Related Questions
I can see the objects through the terrain. 1 Answer
Sprite2D CG shader: No errors, but not displaying anything 1 Answer
All of my water prefabs from the environment assets are pink. 2 Answers
Render queue on Shader Graphs seems to be ignored 1 Answer
Unity lerp between two textures based on the distance from an object via shaders 0 Answers