Hello, I have been working on a custom shader script that blends textures based on distance from the center of the mesh object. What I am attempting to do now is to calculate the “slope” of the vertex. I’m new to shader scripting and matrix math so i figured I’d post a question and see if anyone can help me out.
What i think i need to do is first get a directional “vector” or float3 from the center of the mesh object to the vertex, then i should be able to find the dot product of the first direction against the normal direction of the vertex which should tell me the slope of said vertex. Any information as to if i am not thinking this through correctly and really how to find the directional vector from the center of the mesh to the vertex would be much appreciated.
Here is a copy of my shader script. which is currently non-functional due to the “planetdirection” not being correct. Thanks and have a great one!
Shader “Custom/FromCenter” {
Properties {
_CentrePoint (“Centre”, Vector) = (0, 0, 0, 0)
_BlendThreshold ("Blend Distance", Float) = 0.5
_WaterTex ("Water Texture", 2D) = "white" {}
_DesertTex ("Desert Texture", 2D) = "white" {}
_ForestTex ("Forest Texture", 2D) = "white" {}
_MountainTex ("Mountain Texture", 2D) = "white" {}
_SwampTex ("Swamp Texture", 2D) = "white" {}
_ArcticTex ("Arctic Texture", 2D) = "white" {}
_PlainsTex ("Plains Texture", 2D) = "white" {}
_SwampHeight ("Swamp Height", Float) = 3
_DesertHeight ("Desert Height", Float) = 3
_PlainsHeight ("Plains Height", Float) = 3
_ForestHeight ("Forest Height", Float) = 3
_MountainHeight ("Mountain Height", Float) = 3
_ArcticHeight ("Arctic Height", Float) = 3
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
float4 _CentrePoint;
float _BlendThreshold;
float _SlopeLimt;
sampler2D _WaterTex;
sampler2D _SwampTex;
sampler2D _DesertTex;
sampler2D _PlainsTex;
sampler2D _ForestTex;
sampler2D _MountainTex;
sampler2D _ArcticTex;
float _SwampHeight;
float _DesertHeight;
float _PlainsHeight;
float _ForestHeight;
float _MountainHeight;
float _ArcticHeight;
struct Input {
float2 uv_WaterTex;
float3 worldPos;
float3 localPos;
float3 worldNormal;
INTERNAL_DATA
};
void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input,o);
o.localPos = v.vertex.xyz;
}
void surf (Input IN, inout SurfaceOutput o) {
half4 water = tex2D (_WaterTex, IN.uv_WaterTex);
half4 swamp = tex2D (_SwampTex, IN.uv_WaterTex);
half4 desert = tex2D (_DesertTex, IN.uv_WaterTex);
half4 plains = tex2D (_PlainsTex, IN.uv_WaterTex);
half4 forest = tex2D (_ForestTex, IN.uv_WaterTex);
half4 mountain = tex2D (_MountainTex, IN.uv_WaterTex);
half4 arctic = tex2D (_ArcticTex, IN.uv_WaterTex);
float dist = distance(_CentrePoint.xyz, IN.worldPos);
half4 c = water;
float3 planetDirection = _CentrePoint.xyz - IN.worldPos;
if(dot(normalize(IN.worldNormal), planetDirection) < _SlopeLimit){
c = mountain;
} else {
if (dist > _SwampHeight){
float startBlending = _SwampHeight - _BlendThreshold;
float changeFactor = saturate((dist - startBlending) / (_BlendThreshold * 2));
c = lerp(water, swamp, changeFactor);
}
if(dist > _DesertHeight){
float startBlending = _DesertHeight - _BlendThreshold;
float changeFactor = saturate((dist - startBlending) / (_BlendThreshold * 2));
c = lerp(swamp, desert, changeFactor );
}
if(dist > _PlainsHeight){
float startBlending = _PlainsHeight - _BlendThreshold;
float changeFactor = saturate((dist - startBlending) / (_BlendThreshold * 2));
c = lerp(desert, plains, changeFactor);
}
if(dist > _ForestHeight){
float startBlending = _ForestHeight - _BlendThreshold;
float changeFactor = saturate((dist - startBlending) / (_BlendThreshold * 2));
c = lerp(plains, forest, changeFactor);
}
if(dist > _MountainHeight){
float startBlending = _MountainHeight - _BlendThreshold;
float changeFactor = saturate((dist - startBlending) / (_BlendThreshold * 2));
c = lerp(forest, mountain, changeFactor);
}
if(dist > _ArcticHeight){
float startBlending = _ArcticHeight - _BlendThreshold;
float changeFactor = saturate((dist - startBlending) / (_BlendThreshold * 2));
c = lerp(mountain, arctic, changeFactor);
}
}
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}