shader based on surface distance from center ?

hello everyone !

Before explaining my question, here’s what i’m aiming to:
a procedural generated planet starting from a sphere, where perlin noise determine random vertexes heigths.

and this part is working :slight_smile:

now, i’d like to procedurally apply certain textures, and blending them depending on the surface distance from the center of sphere.

Since shader programming is still completely obscure to me, my main question is “does such a shader exist / where should i start trying to modify an existing one”
Or a good hint on which nodes to riddle with in the wonderful Strumpy Shader Editor :slight_smile:

alt text

I’ll thank you in advance, but snippet of shaders code with “try to adapt from this” really won’t help me at the moment, since i plan studying shaders a bit later on.

thank you for your time :slight_smile:

Completely possible, given that you have access to all the information needed! Here’s the relevant pages, since you don’t want me writing your shader for you (and I don’t really have time right now anyway…)

Add ‘worldPos’ to your input structure (at the bottom)

Add a ‘centrePos’ vector4 to the properties (modify this in a script)

Then, in your shader you can use some variant of the line

output.albedo = lerp(sand.rgb, rock.rgb, distance(centrePos.xyz, worldPos.xyz));

Here, I’ve made up something to start you off. This only has two textures, but it should be possible to extend this to include more.

Shader "Custom/DistanceLerp" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_ChangePoint ("Change at this distance", Float) = 3
		_OuterTex ("Base (RGB)", 2D) = "black" {}
		_CentrePoint ("Centre", Vector) = (0, 0, 0, 0)
		_BlendThreshold ("Blend Distance", Float) = 0.5
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;
		float _ChangePoint;
		float4 _CentrePoint;
		sampler2D _OuterTex;
		float _BlendThreshold;

		struct Input {
			float2 uv_MainTex;
			float3 worldPos;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			half4 main = tex2D (_MainTex, IN.uv_MainTex);
			half4 outer = tex2D (_OuterTex, IN.uv_MainTex);
			
			float startBlending = _ChangePoint - _BlendThreshold;
			float endBlending = _ChangePoint + _BlendThreshold;
			
			float curDistance = distance(_CentrePoint.xyz, IN.worldPos);
			float changeFactor = saturate((curDistance - startBlending) / (_BlendThreshold * 2));
			
			half4 c = lerp(main, outer, changeFactor);
			
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

Also, if you want to work with shaders, have a read up of the CG standard library. There’s absolutely no documentation for this in the Unity docs, only unity-specific stuff- so not many people actually know what the language even is!

thank you so much :slight_smile:
at the moment, I’ fiddling in Strumpy Shader Editor, since as I mentioned, I really can’t find a way to start a shader from scratch, and tried lerping the distance between WorldPosition and an arbitrary float (which stores the same coordinates of the center of my object) since i can’t see something closer to an “object center” node.
but I can’t really see why it doesn’t work like should.

my aim, would be to have a slider, to control the distance threshold of each texture, so i probably need to do some more calculations, than just the difference of distances.