Created
April 1, 2025 01:05
-
-
Save ahzkwid/4ab310fd2369428867257be8a5caa5b9 to your computer and use it in GitHub Desktop.
Unity/PerlinNoise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//유니티쉐이더용 펄린노이즈 | |
//원문은 https://en.wikipedia.org/wiki/Perlin_noise 에서 가져왔고 | |
//포팅만 했을뿐 만든건 Ken Perlin | |
Shader "Perlin/Noise" | |
{ | |
Properties | |
{ | |
[HideInInspector]_MainTex ("Texture", 2D) = "white" {} | |
[PowerSlider(4)]_Scale("Scale", Range(0,8192)) = 1000 | |
_Speed("Speed", Range(0,8)) = 0 | |
//[HideInInspector] | |
_Key("Key", float) = 0 | |
} | |
SubShader | |
{ | |
Tags{ | |
"RenderType" = "Transparent" | |
"Queue" = "Transparent" | |
"IgnoreProjector"="True" | |
} | |
LOD 100 | |
blend srcalpha oneminussrcalpha | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
// make fog work | |
#pragma multi_compile_fog | |
#include "UnityCG.cginc" | |
float _Speed; | |
float _Key; | |
float interpolate(float a0, float a1, float w) { | |
/* // You may want clamping by inserting: | |
* if (0.0 > w) return a0; | |
* if (1.0 < w) return a1; | |
*/ | |
return (a1 - a0) * w + a0; | |
/* // Use this cubic interpolation [[Smoothstep]] instead, for a smooth appearance: | |
* return (a1 - a0) * (3.0 - w * 2.0) * w * w + a0; | |
* | |
* // Use [[Smootherstep]] for an even smoother result with a second derivative equal to zero on boundaries: | |
* return (a1 - a0) * ((w * (w * 6.0 - 15.0) + 10.0) * w * w * w) + a0; | |
*/ | |
} | |
float2 randomGradient(int ix, int iy) { | |
// No precomputed gradients mean this works for any number of grid coordinates | |
uint w = 8 * (4294967295+1); | |
uint s = w / 2; // rotation width | |
uint a = ix, b = iy; | |
a *= 3284157443; | |
b ^= a << s | a >> w-s; | |
b *= 1911520717; | |
a ^= b << s | b >> w-s; | |
a *= 2048419325; | |
float random = a * (3.14159265 / ~(~0u >> 1)); // in [0, 2*Pi] | |
random += _Time.y*_Speed; | |
random += _Key; | |
float2 v; | |
v.x = cos(random); v.y = sin(random); | |
return v; | |
} | |
// Computes the dot product of the distance and gradient vectors. | |
float dotGridGradient(int ix, int iy, float x, float y) { | |
// Get gradient from integer coordinates | |
float2 gradient = randomGradient(ix, iy); | |
// Compute the distance vector | |
float dx = x - (float)ix; | |
float dy = y - (float)iy; | |
// Compute the dot-product | |
return (dx*gradient.x + dy*gradient.y); | |
} | |
// Compute Perlin noise at coordinates x, y | |
float perlin(float x, float y) { | |
// Determine grid cell coordinates | |
int x0 = (int)floor(x); | |
int x1 = x0 + 1; | |
int y0 = (int)floor(y); | |
int y1 = y0 + 1; | |
// Determine interpolation weights | |
// Could also use higher order polynomial/s-curve here | |
float sx = x - (float)x0; | |
float sy = y - (float)y0; | |
// Interpolate between grid point gradients | |
float n0, n1, ix0, ix1, value; | |
n0 = dotGridGradient(x0, y0, x, y); | |
n1 = dotGridGradient(x1, y0, x, y); | |
ix0 = interpolate(n0, n1, sx); | |
n0 = dotGridGradient(x0, y1, x, y); | |
n1 = dotGridGradient(x1, y1, x, y); | |
ix1 = interpolate(n0, n1, sx); | |
value = interpolate(ix0, ix1, sy); | |
return value; // Will return in range -1 to 1. To make it in range 0 to 1, multiply by 0.5 and add 0.5 | |
} | |
float perlin(float2 uv) { | |
return perlin(uv.x,uv.y)*0.5+0.5; | |
} | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float2 uv : TEXCOORD0; | |
UNITY_FOG_COORDS(1) | |
float4 vertex : SV_POSITION; | |
}; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
float _Scale; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.uv = TRANSFORM_TEX(v.uv, _MainTex); | |
UNITY_TRANSFER_FOG(o,o.vertex); | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
fixed4 col = 1; | |
col.rgb = perlin(i.uv*_Scale); | |
return col; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment