Bladeren bron

tutorial: make the noise shader computationally more efficient.

legacy
Sam Hocevar sam 12 jaren geleden
bovenliggende
commit
45da878e36
1 gewijzigde bestanden met toevoegingen van 40 en 54 verwijderingen
  1. +40
    -54
      tutorial/03_noise.lolfx

+ 40
- 54
tutorial/03_noise.lolfx Bestand weergeven

@@ -15,26 +15,17 @@ void main(void)

uniform float u_Time;

vec3 mod289(vec3 x)
vec4 mod289(vec4 x)
{
return x - floor(x * (1.0 / 289.0)) * 289.0;
}

vec3 perm(vec3 x)
vec4 perm(vec4 x)
{
return mod289(((x * 34.0) + 1.0) * x);
}

vec4 noise3d(vec3 p1, vec3 p2, vec3 p3, vec3 p4)
{
p1 = perm(perm(perm(p1) + p1.zxy) + p1.yzx);
p2 = perm(perm(perm(p2) + p2.zxy) + p2.yzx);
p3 = perm(perm(perm(p3) + p3.zxy) + p3.yzx);
p4 = perm(perm(perm(p4) + p4.zxy) + p4.yzx);
return fract(vec4(p1.x, p2.x, p3.x, p4.x) / 41.0);
}

float perlin3d(vec3 p)
float noise3d(vec3 p)
{
mat3 r = mat3(0.36, 0.48, -0.8, -0.8, 0.60, 0.0, 0.48, 0.64, 0.60);
p = r * p;
@@ -43,19 +34,21 @@ float perlin3d(vec3 p)
vec3 d = p - a;
d = d * d * (3.0 - 2.0 * d);

vec4 o1 = noise3d(a,
a + vec3(0.0, 1.0, 0.0),
a + vec3(0.0, 0.0, 1.0),
a + vec3(0.0, 1.0, 1.0));
vec4 o2 = noise3d(a + vec3(1.0, 0.0, 0.0),
a + vec3(1.0, 1.0, 0.0),
a + vec3(1.0, 0.0, 1.0),
a + vec3(1.0, 1.0, 1.0));
vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
vec4 k1 = perm(b.xyxy);
vec4 k2 = perm(k1.xyxy + b.zzww);

vec4 c = k2 + a.zzzz;
vec4 k3 = perm(c);
vec4 k4 = perm(c + 1.0);

vec4 o1 = fract(k3 * (1.0 / 41.0));
vec4 o2 = fract(k4 * (1.0 / 41.0));

vec4 o3 = o2 * d.x + o1 * (1.0 - d.x);
vec2 o4 = o3.yw * d.y + o3.xz * (1.0 - d.y);
vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);

return o4.y * d.z + o4.x * (1.0 - d.z);
return o4.y * d.y + o4.x * (1.0 - d.y);
}

void main(void)
@@ -64,10 +57,10 @@ void main(void)
xy.y -= u_Time * 400.0;
float z = u_Time * 2.0;

float p = perlin3d(vec3(xy / 80.0, z + 1.5)) / 2.0
+ perlin3d(vec3(xy / 40.0, z + 0.3)) / 4.0
+ perlin3d(vec3(xy / 20.0, z + 3.3)) / 8.0
+ perlin3d(vec3(xy / 10.0, z + 2.0)) / 16.0;
float p = noise3d(vec3(xy / 80.0, z)) / 2.0
+ noise3d(vec3(xy / 40.0, z)) / 4.0
+ noise3d(vec3(xy / 20.0, z)) / 8.0
+ noise3d(vec3(xy / 10.0, z)) / 16.0;

/* Scroll by adding [-.5,.5] */
p -= gl_FragCoord.y / 720.0 - 0.5;
@@ -96,26 +89,17 @@ void main(float2 in_Position : POSITION,

-- HLSL.Frag --

float3 mod289(float3 x)
float4 mod289(float4 x)
{
return x - floor(x * (1.0 / 289.0)) * 289.0;
}

float3 perm(float3 x)
float4 perm(float4 x)
{
return mod289(((x * 34.0) + 1.0) * x);
}

float4 noise3d(float3 p1, float3 p2, float3 p3, float3 p4)
{
p1 = perm(perm(perm(p1) + p1.zxy) + p1.yzx);
p2 = perm(perm(perm(p2) + p2.zxy) + p2.yzx);
p3 = perm(perm(perm(p3) + p3.zxy) + p3.yzx);
p4 = perm(perm(perm(p4) + p4.zxy) + p4.yzx);
return frac(float4(p1.x, p2.x, p3.x, p4.x) / 41.0);
}

float perlin3d(float3 p)
float noise3d(float3 p)
{
float3x3 r = float3x3(0.36, 0.48, -0.8, -0.8, 0.60, 0.0, 0.48, 0.64, 0.60);
p = mul(r, p);
@@ -124,19 +108,21 @@ float perlin3d(float3 p)
float3 d = p - a;
d = d * d * (3.0 - 2.0 * d);

float4 o1 = noise3d(a,
a + float3(0.0, 1.0, 0.0),
a + float3(0.0, 0.0, 1.0),
a + float3(0.0, 1.0, 1.0));
float4 o2 = noise3d(a + float3(1.0, 0.0, 0.0),
a + float3(1.0, 1.0, 0.0),
a + float3(1.0, 0.0, 1.0),
a + float3(1.0, 1.0, 1.0));
float4 b = a.xxyy + float4(0.0, 1.0, 0.0, 1.0);
float4 k1 = perm(b.xyxy);
float4 k2 = perm(k1.xyxy + b.zzww);

float4 c = k2 + a.zzzz;
float4 k3 = perm(c);
float4 k4 = perm(c + 1.0);

float4 o1 = frac(k3 * (1.0 / 41.0));
float4 o2 = frac(k4 * (1.0 / 41.0));

float4 o3 = o2 * d.x + o1 * (1.0 - d.x);
float2 o4 = o3.yw * d.y + o3.xz * (1.0 - d.y);
float4 o3 = o2 * d.z + o1 * (1.0 - d.z);
float2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);

return o4.y * d.z + o4.x * (1.0 - d.z);
return o4.y * d.y + o4.x * (1.0 - d.y);
}

void main(in float2 pass_Position : TEXCOORD0,
@@ -147,10 +133,10 @@ void main(in float2 pass_Position : TEXCOORD0,
xy.y -= u_Time * 400.0;
float z = u_Time * 2.0;

float p = perlin3d(float3(xy / 80.0, z + 1.5)) / 2.0
+ perlin3d(float3(xy / 40.0, z + 0.3)) / 4.0
+ perlin3d(float3(xy / 20.0, z + 3.3)) / 8.0
+ perlin3d(float3(xy / 10.0, z + 2.0)) / 16.0;
float p = noise3d(float3(xy / 80.0, z)) / 2.0
+ noise3d(float3(xy / 40.0, z)) / 4.0
+ noise3d(float3(xy / 20.0, z)) / 8.0
+ noise3d(float3(xy / 10.0, z)) / 16.0;

/* Scroll by adding [-.5,.5] */
p -= pass_Position.y / 1440.0;


Laden…
Annuleren
Opslaan