[orx-shade-styles] Add noise {} shade style builder

This commit is contained in:
Edwin Jakobs
2025-03-30 17:17:28 +02:00
parent 3ab5c528ca
commit 4901b22ef4
14 changed files with 1240 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
package org.openrndr.extra.shaderphrases.noise
import org.openrndr.extra.shaderphrases.spacefilling.hilbertPhrase
import org.openrndr.extra.shaderphrases.spacefilling.hilbertV3Phrase
import org.openrndr.extra.shaderphrases.spacefilling.inverseGray32Phrase
// https://www.shadertoy.com/view/3tB3z3
val hilbertR1BlueNoisePhrase = """#ifndef SP_HILBERT_R1_BLUE_NOISE
#define SP_HILBERT_R1_BLUE_NOISE
$hilbertPhrase
$kmhfPhrase
uint hilbertR1BlueNoise(uvec2 p, uint bits, uint seed) {
uint x = uint(hilbert(ivec2(p), int(bits))) % (1u << bits) + seed;
x = kmhf(x);
return x;
}
#endif
"""
val hilbertR1BlueNoiseFloatPhrase = """#ifndef SP_HILBERT_R1_BLUE_NOISE_FLOAT
#define SP_HILBERT_R1_BLUE_NOISE_FLOAT
$hilbertR1BlueNoisePhrase
float hilbertR1BlueNoiseFloat(uvec2 p, uint bits, uint seed) {
uint x = hilbertR1BlueNoise(p, bits, seed);
return float(x) / 4294967296.0;
}
#endif
"""
// https://www.shadertoy.com/view/3tB3z3
val inverseR1BlueNoisePhrase = """#ifndef SP_INVERSE_R1_BLUE_NOISE
#define SP_INVERSE_R1_BLUE_NOISE
$inverseGray32Phrase
$inverseKmhfPhrase
ivec2 inverseR1BlueNoise(uint x, uint bits) {
x = inverseKmhf(x);
return uvec2(inverseHilbert(int(x), int(bits)));}
#endif"""
// https://www.shadertoy.com/view/3tB3z3
val hilbertR1BlueNoiseV3Phrase = """#ifndef SP_HILBERT_R1_BLUE_NOISE_V3
#define SP_HILBERT_R1_BLUE_NOISE_V3
$hilbertV3Phrase
$kmhfPhrase
uint hilbertR1BlueNoise(uvec3 p, uint bits, uint seed) {
uint x = uint(hilbert(ivec3(p), int(bits))) % (1u << bits) + seed;
x = kmhf(x);
return x;
}
#endif
"""
val hilbertR1BlueNoiseFloatV3Phrase = """#ifndef SP_HILBERT_R1_BLUE_NOISE_FLOAT_V3
#define SP_HILBERT_R1_BLUE_NOISE_FLOAT_V3
$hilbertR1BlueNoiseV3Phrase
float hilbertR1BlueNoiseFloat(uvec3 p, uint bits, uint seed) {
uint x = hilbertR1BlueNoise(p, bits, seed);
return float(x) / 4294967296.0;
}
#endif
"""

View File

@@ -0,0 +1,37 @@
package org.openrndr.extra.shaderphrases.noise
/**
* Represents a shader phrase implementing Knuth's multiplicative hash function as defined for unsigned integers.
*
* The function takes a 32-bit unsigned integer as input and computes a hashed value using a fixed-point scaling factor
* (2654435789u). This technique is based on the "multiplicative hash" proposed by Donald Knuth, providing an efficient
* method for hashing without the need for additional libraries or resources.
*
* This phrase is wrapped in preprocessor guards to ensure it is only defined once during the shader compilation process.
*/
// knuth's multiplicative hash function (fixed point R1)
val kmhfPhrase = """#ifndef SP_KMHF
#define SP_KMHF
uint kmhf(uint x) {
return 0x80000000u + 2654435789u * x;
}
#endif"""
/**
* Represents a GLSL shader phrase that defines the inverse of Knuth's
* multiplicative hash function, commonly used in procedural noise generation
* or random value calculations.
*
* The inverseKmhfPhrase` provides a utility function in GLSL to compute
* the inverse of the multiplicative hash for unsigned integers. It is wrapped
* within preprocessor guards to ensure the function is only defined once
* during shader compilation.
*/
// inverse of Knuth's multiplicative hash function (fixed point R1)
val inverseKmhfPhrase = """#ifndef SP_INVERSE_KMHF
#define SP_INVERSE_KMHF
uint inverseKmhf(uint x) {
return (x - 0x80000000u) * 827988741u;
}
#endif
""".trimMargin()

View File

@@ -0,0 +1,21 @@
package org.openrndr.extra.shaderphrases.noise
const val mod289Phrase = """#ifndef SP_MOD289
#define SP_MOD289
float mod289(const in float x) { return x - floor(x * (1. / 289.)) * 289.; }
#endif"""
const val mod289V2Phrase = """#ifndef SP_MOD289V2
#define SP_MOD289V2
vec2 mod289(const in vec2 x) { return x - floor(x * (1. / 289.)) * 289.; }
#endif"""
const val mod289V3Phrase = """#ifndef SP_MOD289V3
#define SP_MOD289V3
vec3 mod289(const in vec3 x) { return x - floor(x * (1. / 289.)) * 289.; }
#endif"""
const val mod289V4Phrase = """#ifndef SP_MOD289V4
#define SP_MOD289V4
vec4 mod289(const in vec4 x) { return x - floor(x * (1. / 289.)) * 289.; }
#endif"""

View File

@@ -0,0 +1,29 @@
package org.openrndr.extra.shaderphrases.noise
val permutePhrase = """#ifndef SP_PERMUTE
#define SP_PERMUTE
$mod289Phrase
float permute(const in float x) { return mod289(((x * 34.0) + 1.0) * x); }
#endif
"""
const val permuteV2Phrase = """#ifndef SP_PERMUTEV2
#define SP_PERMUTEV2
$mod289V2Phrase
vec2 permute(const in vec2 x) { return mod289(((x * 34.0) + 1.0) * x); }
#endif
"""
const val permuteV3Phrase = """#ifndef SP_PERMUTEV3
#define SP_PERMUTEV3
$mod289V3Phrase
vec3 permute(const in vec3 x) { return mod289(((x * 34.0) + 1.0) * x); }
#endif
"""
const val permuteV4Phrase = """#ifndef SP_PERMUTEV4
#define SP_PERMUTEV4
$mod289V4Phrase
vec4 permute(const in vec4 x) { return mod289(((x * 34.0) + 1.0) * x); }
#endif
"""

View File

@@ -0,0 +1,285 @@
package org.openrndr.extra.shaderphrases.noise
val grad4Phrase = """#ifndef SP_GRAD4
#define SP_GRAD4
vec4 grad4(float j, vec4 ip) {
const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0);
vec4 p,s;
p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0;
p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
s = vec4(lessThan(p, vec4(0.0)));
p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www;
return p;
}
#endif
"""
val simplex12 = """#ifndef SP_SIMPLEX12
#define SP_SIMPLEX12
$mod289V2Phrase
$mod289V3Phrase
$permuteV2Phrase
$permuteV3Phrase
float simplex12(in vec2 v) {
const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0
0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
-0.577350269189626, // -1.0 + 2.0 * C.x
0.024390243902439); // 1.0 / 41.0
// First corner
vec2 i = floor(v + dot(v, C.yy) );
vec2 x0 = v - i + dot(i, C.xx);
// Other corners
vec2 i1;
//i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
//i1.y = 1.0 - i1.x;
i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
// x0 = x0 - 0.0 + 0.0 * C.xx ;
// x1 = x0 - i1 + 1.0 * C.xx ;
// x2 = x0 - 1.0 + 2.0 * C.xx ;
vec4 x12 = x0.xyxy + C.xxzz;
x12.xy -= i1;
// Permutations
i = mod289(i); // Avoid truncation effects in permutation
vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
+ i.x + vec3(0.0, i1.x, 1.0 ));
vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
m = m*m ;
m = m*m ;
// Gradients: 41 points uniformly over a line, mapped onto a diamond.
// The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
vec3 x = 2.0 * fract(p * C.www) - 1.0;
vec3 h = abs(x) - 0.5;
vec3 ox = floor(x + 0.5);
vec3 a0 = x - ox;
// Normalise gradients implicitly by scaling m
// Approximation of: m *= inversesqrt( a0*a0 + h*h );
m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
// Compute final noise value at P
vec3 g;
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
return 130.0 * dot(m, g);
}
#endif
"""
val simplex13 = """#ifndef SP_SIMPLEX13
#define SP_SIMPLEX13
$mod289V3Phrase
$mod289V4Phrase
$permuteV3Phrase
$permuteV4Phrase
$taylorInvSqrtV4Phrase
float simplex13(in vec3 v) {
const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
// First corner
vec3 i = floor(v + dot(v, C.yyy) );
vec3 x0 = v - i + dot(i, C.xxx) ;
// Other corners
vec3 g = step(x0.yzx, x0.xyz);
vec3 l = 1.0 - g;
vec3 i1 = min( g.xyz, l.zxy );
vec3 i2 = max( g.xyz, l.zxy );
// x0 = x0 - 0.0 + 0.0 * C.xxx;
// x1 = x0 - i1 + 1.0 * C.xxx;
// x2 = x0 - i2 + 2.0 * C.xxx;
// x3 = x0 - 1.0 + 3.0 * C.xxx;
vec3 x1 = x0 - i1 + C.xxx;
vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
// Permutations
i = mod289(i);
vec4 p = permute( permute( permute(
i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
+ i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
+ i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
// Gradients: 7x7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
float n_ = 0.142857142857; // 1.0/7.0
vec3 ns = n_ * D.wyz - D.xzx;
vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
vec4 x_ = floor(j * ns.z);
vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
vec4 x = x_ *ns.x + ns.yyyy;
vec4 y = y_ *ns.x + ns.yyyy;
vec4 h = 1.0 - abs(x) - abs(y);
vec4 b0 = vec4( x.xy, y.xy );
vec4 b1 = vec4( x.zw, y.zw );
//vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
//vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
vec4 s0 = floor(b0)*2.0 + 1.0;
vec4 s1 = floor(b1)*2.0 + 1.0;
vec4 sh = -step(h, vec4(0.0));
vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
vec3 p0 = vec3(a0.xy,h.x);
vec3 p1 = vec3(a0.zw,h.y);
vec3 p2 = vec3(a1.xy,h.z);
vec3 p3 = vec3(a1.zw,h.w);
//Normalise gradients
vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
// Mix final noise value
vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
m = m * m;
return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
dot(p2,x2), dot(p3,x3) ) );
#endif
}"""
val simplex14 = """#ifndef SP_SIMPLEX14
#define SP_SIMPLEX14
$mod289Phrase
$mod289V2Phrase
$mod289V3Phrase
$mod289V4Phrase
$permutePhrase
$permuteV2Phrase
$permuteV3Phrase
$permuteV4Phrase
$taylorInvSqrtPhrase
$taylorInvSqrtV2Phrase
$taylorInvSqrtV3Phrase
$taylorInvSqrtV4Phrase
$grad4Phrase
float simplex14(vec4 v) {
const vec4 C = vec4( 0.138196601125011, // (5 - sqrt(5))/20 G4
0.276393202250021, // 2 * G4
0.414589803375032, // 3 * G4
-0.447213595499958); // -1 + 4 * G4
// First corner
vec4 i = floor(v + dot(v, vec4(.309016994374947451)) ); // (sqrt(5) - 1)/4
vec4 x0 = v - i + dot(i, C.xxxx);
// Other corners
// Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
vec4 i0;
vec3 isX = step( x0.yzw, x0.xxx );
vec3 isYZ = step( x0.zww, x0.yyz );
// i0.x = dot( isX, vec3( 1.0 ) );
i0.x = isX.x + isX.y + isX.z;
i0.yzw = 1.0 - isX;
// i0.y += dot( isYZ.xy, vec2( 1.0 ) );
i0.y += isYZ.x + isYZ.y;
i0.zw += 1.0 - isYZ.xy;
i0.z += isYZ.z;
i0.w += 1.0 - isYZ.z;
// i0 now contains the unique values 0,1,2,3 in each channel
vec4 i3 = clamp( i0, 0.0, 1.0 );
vec4 i2 = clamp( i0-1.0, 0.0, 1.0 );
vec4 i1 = clamp( i0-2.0, 0.0, 1.0 );
// x0 = x0 - 0.0 + 0.0 * C.xxxx
// x1 = x0 - i1 + 1.0 * C.xxxx
// x2 = x0 - i2 + 2.0 * C.xxxx
// x3 = x0 - i3 + 3.0 * C.xxxx
// x4 = x0 - 1.0 + 4.0 * C.xxxx
vec4 x1 = x0 - i1 + C.xxxx;
vec4 x2 = x0 - i2 + C.yyyy;
vec4 x3 = x0 - i3 + C.zzzz;
vec4 x4 = x0 + C.wwww;
// Permutations
i = mod289(i);
float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x);
vec4 j1 = permute( permute( permute( permute (
i.w + vec4(i1.w, i2.w, i3.w, 1.0 ))
+ i.z + vec4(i1.z, i2.z, i3.z, 1.0 ))
+ i.y + vec4(i1.y, i2.y, i3.y, 1.0 ))
+ i.x + vec4(i1.x, i2.x, i3.x, 1.0 ));
// Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
// 7*7*6 = 294, which is close to the ring size 17*17 = 289.
vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ;
vec4 p0 = grad4(j0, ip);
vec4 p1 = grad4(j1.x, ip);
vec4 p2 = grad4(j1.y, ip);
vec4 p3 = grad4(j1.z, ip);
vec4 p4 = grad4(j1.w, ip);
// Normalise gradients
vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
p4 *= taylorInvSqrt(dot(p4,p4));
// Mix contributions from the five corners
vec3 m0 = max(0.6 - vec3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0);
vec2 m1 = max(0.6 - vec2(dot(x3,x3), dot(x4,x4) ), 0.0);
m0 = m0 * m0;
m1 = m1 * m1;
return 49.0 * ( dot(m0*m0, vec3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 )))
+ dot(m1*m1, vec2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ;
}
#endif
"""
val simplex22 = """#ifndef SP_SIMPLEX22
#define SP_SIMPLEX22
$simplex12
vec2 simplex22( vec2 x ){
float s = simplex12(vec2( x ));
float s1 = simplex12(vec2( x.y - 19.1, x.x + 47.2 ));
return vec2( s , s1 );
}
#endif
"""
val simplex33 = """#ifndef SP_SIMPLEX33
#define SP_SIMPLEX33
$simplex13
vec3 simplex33( vec3 x ){
float s = simplex13(vec3( x ));
float s1 = simplex13(vec3( x.y - 19.1 , x.z + 33.4 , x.x + 47.2 ));
float s2 = simplex13(vec3( x.z + 74.2 , x.x - 124.5 , x.y + 99.4 ));
return vec3( s , s1 , s2 );
}
#endif
"""
val simplex34 = """#ifndef SP_SIMPLEX34
#define SP_SIMPLEX34
$simplex14
vec3 simplex34( vec4 x ){
float s = simplex14(vec4( x ));
float s1 = simplex14(vec4( x.y - 19.1 , x.z + 33.4 , x.x + 47.2, x.w ));
float s2 = simplex14(vec4( x.z + 74.2 , x.x - 124.5 , x.y + 99.4, x.w ));
return vec3( s , s1 , s2 );
}
#endif
"""

View File

@@ -0,0 +1,25 @@
package org.openrndr.extra.shaderphrases.noise
const val taylorInvSqrtPhrase = """#ifndef SP_TAYLORINVSQRT
#define SP_TAYLORINVSQRT
float taylorInvSqrt(in float r) { return 1.79284291400159 - 0.85373472095314 * r; }
#endif
"""
const val taylorInvSqrtV2Phrase = """#ifndef SP_TAYLORINVSQRTV2
#define SP_TAYLORINVSQRTV2
vec2 taylorInvSqrt(in vec2 r) { return 1.79284291400159 - 0.85373472095314 * r; }
#endif
"""
const val taylorInvSqrtV3Phrase = """#ifndef SP_TAYLORINVSQRTV3
#define SP_TAYLORINVSQRTV3
vec3 taylorInvSqrt(in vec3 r) { return 1.79284291400159 - 0.85373472095314 * r; }
#endif
"""
const val taylorInvSqrtV4Phrase = """#ifndef SP_TAYLORINVSQRTV4
#define SP_TAYLORINVSQRTV4
vec4 taylorInvSqrt(in vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }
#endif
"""

View File

@@ -0,0 +1,117 @@
package org.openrndr.extra.shaderphrases.noise
/**
* uniform hash shader phrase
*/
val uhash11Phrase = """
#ifndef PHRASE_UHASH11
#define PHRASE_UHASH11
uint uhash11(uint x) {
uint a = x;
a = a ^ (a >> 16);
a *= 0x7feb352du;
a = a ^ (a >> 15);
a *= 0x846ca68bu;
a = a ^ (a >> 16);
return a;
}
#endif
"""
/**
* uniform hash shader phrase
*/
val fhash11Phrase = """
$uhash11Phrase
#ifndef PHRASE_FHASH11
#define PHRASE_FHASH11
float fhash11(float x) {
uint a = uhash11(floatBitsToUint(x));
return float(a) / 4294967296.0;
}
#endif
"""
/**
* uniform hash shader phrase
*/
val uhash12Phrase = """
$uhash11Phrase
#ifndef PHRASE_UHASH12
#define PHRASE_UHASH12
uint uhash12(uvec2 x) {
uint a = uhash11(x.y + uhash11(x.x));
return a;
}
#endif
"""
/**
* uniform hash shader phrase
*/
val fhash12Phrase = """
$uhash12Phrase
#ifndef PHRASE_FHASH12
#define PHRASE_FHASH12
float fhash12(vec2 x) {
uint a = uhash12(floatBitsToUint(x));
return float(a) / 4294967296.0;
}
#endif
"""
/**
* uniform hash shader phrase
*/
val uhash13Phrase = """
$uhash11Phrase
#ifndef PHRASE_UHASH13
#define PHRASE_UHASH13
uint uhash13(uvec3 x) {
uint a = uhash11(x.z + uhash11(x.y + uhash11(x.x)));
return a;
}
#endif
"""
/**
* uniform hash shader phrase
*/
val fhash13Phrase = """
$uhash13Phrase
#ifndef PHRASE_FHASH13
#define PHRASE_FHASH13
float fhash13(vec3 x) {
uint a = uhash13(floatBitsToUint(x));
return float(a) / 4294967296.0;
}
#endif
"""
/**
* uniform hash shader phrase
*/
val uhash14Phrase = """
$uhash11Phrase
#ifndef PHRASE_UHASH14
#define PHRASE_UHASH14
uint uhash14(uvec4 x) {
uint a = uhash11(x.w + uhash11(x.z + uhash11(x.y + uhash11(x.x))));
return a;
}
#endif
"""
/**
* uniform hash shader phrase
*/
val fhash14Phrase = """
$uhash14Phrase
#ifndef PHRASE_FHASH14
#define PHRASE_FHASH14
float fhash14(vec4 x) {
uint a = uhash14(floatBitsToUint(x));
return float(a) / 4294967296.0;
}
#endif
"""

View File

@@ -0,0 +1,156 @@
package org.openrndr.extra.shaderphrases.spacefilling
const val part1by1Phrase = """#ifndef SP_PART1BY1
#define SP_PART1BY1
uint part1by1 (uint x) {
x = (x & 0x0000ffffu);
x = ((x ^ (x << 8u)) & 0x00ff00ffu);
x = ((x ^ (x << 4u)) & 0x0f0f0f0fu);
x = ((x ^ (x << 2u)) & 0x33333333u);
x = ((x ^ (x << 1u)) & 0x55555555u);
return x;
}
#endif
"""
const val compact1by1Phrase = """#ifndef SP_COMPACT1BY1
#define SP_COMPACT1BY1
uint compact1by1 (uint x) {
x = (x & 0x55555555u);
x = ((x ^ (x >> 1u)) & 0x33333333u);
x = ((x ^ (x >> 2u)) & 0x0f0f0f0fu);
x = ((x ^ (x >> 4u)) & 0x00ff00ffu);
x = ((x ^ (x >> 8u)) & 0x0000ffffu);
return x;
}
#endif
"""
const val inverseGray32Phrase = """#ifndef SP_INVERSEGRAY32
#define SP_INVERSEGRAY32
uint inverse_gray32(uint n) {
n = n ^ (n >> 1);
n = n ^ (n >> 2);
n = n ^ (n >> 4);
n = n ^ (n >> 8);
n = n ^ (n >> 16);
return n;
}
#endif"""
// forward Hilbert https://www.shadertoy.com/view/llGcDm
const val hilbertPhrase = """#ifndef SP_HILBERT
#define SP_HILBERT
int hilbert(ivec2 p, int level) {
int d = 0;
for (int k = 0; k < level; k++) {
int n = level - k -1;
ivec2 r = (p >> n) & 1;
d += ((3 * r.x) ^ r.y) << (2 * n);
if (r.y == 0) { if (r.x == 1) { p = (1 <<n) - 1 - p; } p = p.yx; }
}
return d;
}
#endif
"""
// https://www.shadertoy.com/view/llGcDm
const val inverseHilbertPhrase = """#ifndef SP_INVERSEHILBERT
#define SP_INVERSEHILBERT
ivec2 inverseHilbert( int i, int level ) {
ivec2 p = ivec2(0, 0);
for (int k = 0; k < level; k++) {
ivec2 r = ivec2(i >> 1, i ^(i >> 1)) & 1;
if (r.y==0) { if(r.x == 1) { p = (1 << k) - 1 - p; } p = p.yx; }
p += r << k;
i >>= 2;
}
return p;
}
#endif
"""
const val hilbertV3Phrase = """#ifndef SP_HILBERTV3
#define SP_HILBERTV3
// Convert 3D coordinates to a Hilbert curve index in GLSL
int hilbert(ivec3 pos, int order) {
// Input position vector (x, y, z)
int x = pos.x;
int y = pos.y;
int z = pos.z;
// Initialize the index to 0
int hilbertIndex = 0;
// Temporary variables for coordinate transformation
int rx, ry, rz;
int bits;
// Process each bit from MSB to LSB
for (int i = order - 1; i >= 0; i--) {
// Extract bit i from each coordinate
bits = ((x >> i) & 1) | (((y >> i) & 1) << 1) | (((z >> i) & 1) << 2);
// Calculate position in subcube
rx = 0;
ry = 0;
rz = 0;
// Transform coordinates based on subcube position
if (bits == 0) {
rx = y;
ry = x;
rz = z;
}
else if (bits == 1) {
rx = x;
ry = y;
rz = z;
}
else if (bits == 2) {
rx = x;
ry = y + (1 << i);
rz = z;
}
else if (bits == 3) {
rx = (1 << i) - 1 - x;
ry = (1 << i) - 1 - y;
rz = z;
}
else if (bits == 4) {
rx = (1 << i) - 1 - x;
ry = y;
rz = z + (1 << i);
}
else if (bits == 5) {
rx = y;
ry = x;
rz = z + (1 << i);
}
else if (bits == 6) {
rx = x;
ry = y;
rz = z + (1 << i);
}
else if (bits == 7) {
rx = (1 << i) - 1 - y;
ry = (1 << i) - 1 - x;
rz = z + (1 << i);
}
// Add the current subcube's contribution to the index
// Each subcube contains 8^i cells
hilbertIndex |= (bits << (3 * i));
// Update coordinates
x = rx;
y = ry;
z = rz;
}
return hilbertIndex;
}
#endif
"""