Get it on Google Play


Wm뮤 :: PerlinNoise HLSL

블로그 이미지
가끔 그림그리거나 3D모델링하거나
취미로 로봇만드는
퇴직한 전자과 게임프로그래머
2020.3.48f1 , 2022.3.6f1 주로 사용
모카쨩
@Ahzkwid

Recent Comment

Archive


2023. 4. 25. 03:07 Unity/shader

 

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
        }
    }
}

 

내가 포팅한 펄린노이즈임

원문은 https://en.wikipedia.org/wiki/Perlin_noise 에서 가져왔고

포팅만 했을뿐 만든건 Ken Perlin이므로 어떻게 쓰던 알바아님😜

자기가 포팅했다고 하는 노양심짓만 안 해주면 좋을듯

'Unity > shader' 카테고리의 다른 글

DepthCameraTexture를 _CameraDepthTexture처럼 쓰기  (0) 2023.10.14
스크린 오버레이 샘플  (0) 2023.01.20
유니티 스카이박스 관련  (0) 2023.01.16
posted by 모카쨩

  • total
  • today
  • yesterday

Recent Post

저사양 유저용 블로그 진입