I’ve been tinkering with a procedural noise shader but I’m not sure how I’d go about “binding” the 3D noise to the object position?
I’ve tried various approaches, like passing in a position from a script and using various vertex parameters, but when it comes to most of this stuff I feel like that dog in that meme:
Shader "Custom/NoiseShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_Offset ("Offset", Vector) = (0,0,0,0)
}
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
struct Input
{
float2 uv_MainTex;
float3 worldPos;
float3 objPos;
float3 vertex;
float3 uv;
};
void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input,o);
o.objPos = mul(unity_WorldToObject, v.vertex);
o.vertex = v.vertex;
o.uv = v.texcoord;
}
half _Glossiness;
half _Metallic;
fixed4 _Color;
float3 _Offset;
float hash13(float3 p3)
{
p3 = frac(p3 * .1031);
p3 += dot(p3, p3.zyx + 31.32);
return frac((p3.x + p3.y) * p3.z);
}
float noise( in float3 x )
{
float3 i = floor(x);
float3 f = frac(x);
f = f*f*(3.0-2.0*f);
return lerp(lerp(lerp( hash13(i+float3(0,0,0)),
hash13(i+float3(1,0,0)),f.x),
lerp( hash13(i+float3(0,1,0)),
hash13(i+float3(1,1,0)),f.x),f.y),
lerp(lerp( hash13(i+float3(0,0,1)),
hash13(i+float3(1,0,1)),f.x),
lerp( hash13(i+float3(0,1,1)),
hash13(i+float3(1,1,1)),f.x),f.y),f.z);
}
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = _Color;
float sc = 1.0;
float3 p = floor(sc * (IN.worldPos - IN.objPos) * 0.99999);
float val = noise(p);
c += val * 0.5;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
any help would be appreciated.