124 lines
3.4 KiB
HLSL
124 lines
3.4 KiB
HLSL
|
//
|
||
|
// Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader
|
||
|
//
|
||
|
// Original work (webgl-noise) Copyright (C) 2011 Stefan Gustavson
|
||
|
// Translation and modification was made by Keijiro Takahashi.
|
||
|
//
|
||
|
// This shader is based on the webgl-noise GLSL shader. For further details
|
||
|
// of the original shader, please see the following description from the
|
||
|
// original source code.
|
||
|
//
|
||
|
|
||
|
//
|
||
|
// GLSL textureless classic 2D noise "cnoise",
|
||
|
// with an RSL-style periodic variant "pnoise".
|
||
|
// Author: Stefan Gustavson (stefan.gustavson@liu.se)
|
||
|
// Version: 2011-08-22
|
||
|
//
|
||
|
// Many thanks to Ian McEwan of Ashima Arts for the
|
||
|
// ideas for permutation and gradient selection.
|
||
|
//
|
||
|
// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
|
||
|
// Distributed under the MIT license. See LICENSE file.
|
||
|
// https://github.com/ashima/webgl-noise
|
||
|
//
|
||
|
|
||
|
#include "NoiseUtils.hlsl"
|
||
|
|
||
|
// Classic Perlin noise
|
||
|
float cnoise(float2 P)
|
||
|
{
|
||
|
float4 Pi = floor(P.xyxy) + float4(0.0, 0.0, 1.0, 1.0);
|
||
|
float4 Pf = frac (P.xyxy) - float4(0.0, 0.0, 1.0, 1.0);
|
||
|
Pi = mod289(Pi); // To avoid truncation effects in permutation
|
||
|
float4 ix = Pi.xzxz;
|
||
|
float4 iy = Pi.yyww;
|
||
|
float4 fx = Pf.xzxz;
|
||
|
float4 fy = Pf.yyww;
|
||
|
|
||
|
float4 i = permute(permute(ix) + iy);
|
||
|
|
||
|
float4 gx = frac(i / 41.0) * 2.0 - 1.0 ;
|
||
|
float4 gy = abs(gx) - 0.5 ;
|
||
|
float4 tx = floor(gx + 0.5);
|
||
|
gx = gx - tx;
|
||
|
|
||
|
float2 g00 = float2(gx.x,gy.x);
|
||
|
float2 g10 = float2(gx.y,gy.y);
|
||
|
float2 g01 = float2(gx.z,gy.z);
|
||
|
float2 g11 = float2(gx.w,gy.w);
|
||
|
|
||
|
float4 norm = taylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
|
||
|
g00 *= norm.x;
|
||
|
g01 *= norm.y;
|
||
|
g10 *= norm.z;
|
||
|
g11 *= norm.w;
|
||
|
|
||
|
float n00 = dot(g00, float2(fx.x, fy.x));
|
||
|
float n10 = dot(g10, float2(fx.y, fy.y));
|
||
|
float n01 = dot(g01, float2(fx.z, fy.z));
|
||
|
float n11 = dot(g11, float2(fx.w, fy.w));
|
||
|
|
||
|
float2 fade_xy = fade(Pf.xy);
|
||
|
float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x);
|
||
|
float n_xy = lerp(n_x.x, n_x.y, fade_xy.y);
|
||
|
return 2.3 * n_xy;
|
||
|
}
|
||
|
|
||
|
// Classic Perlin noise, periodic variant
|
||
|
float pnoise(float2 P, float2 rep)
|
||
|
{
|
||
|
float4 Pi = floor(P.xyxy) + float4(0.0, 0.0, 1.0, 1.0);
|
||
|
float4 Pf = frac (P.xyxy) - float4(0.0, 0.0, 1.0, 1.0);
|
||
|
Pi = mod(Pi, rep.xyxy); // To create noise with explicit period
|
||
|
Pi = mod289(Pi); // To avoid truncation effects in permutation
|
||
|
float4 ix = Pi.xzxz;
|
||
|
float4 iy = Pi.yyww;
|
||
|
float4 fx = Pf.xzxz;
|
||
|
float4 fy = Pf.yyww;
|
||
|
|
||
|
float4 i = permute(permute(ix) + iy);
|
||
|
|
||
|
float4 gx = frac(i / 41.0) * 2.0 - 1.0 ;
|
||
|
float4 gy = abs(gx) - 0.5 ;
|
||
|
float4 tx = floor(gx + 0.5);
|
||
|
gx = gx - tx;
|
||
|
|
||
|
float2 g00 = float2(gx.x,gy.x);
|
||
|
float2 g10 = float2(gx.y,gy.y);
|
||
|
float2 g01 = float2(gx.z,gy.z);
|
||
|
float2 g11 = float2(gx.w,gy.w);
|
||
|
|
||
|
float4 norm = taylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
|
||
|
g00 *= norm.x;
|
||
|
g01 *= norm.y;
|
||
|
g10 *= norm.z;
|
||
|
g11 *= norm.w;
|
||
|
|
||
|
float n00 = dot(g00, float2(fx.x, fy.x));
|
||
|
float n10 = dot(g10, float2(fx.y, fy.y));
|
||
|
float n01 = dot(g01, float2(fx.z, fy.z));
|
||
|
float n11 = dot(g11, float2(fx.w, fy.w));
|
||
|
|
||
|
float2 fade_xy = fade(Pf.xy);
|
||
|
float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x);
|
||
|
float n_xy = lerp(n_x.x, n_x.y, fade_xy.y);
|
||
|
return 2.3 * n_xy;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
// BEGIN JIMMY'S MODIFICATIONS
|
||
|
|
||
|
void PerlinNoise2D_float(float2 input, out float Out)
|
||
|
{
|
||
|
Out = cnoise(input);
|
||
|
}
|
||
|
|
||
|
void PerlinNoise2DPeriodic_float(float2 input, float2 period, out float Out)
|
||
|
{
|
||
|
Out = pnoise(input, period);
|
||
|
}
|
||
|
|
||
|
// END JIMMY'S MODIFICATIONS
|