_recfold

  • 27 Replies
  • 339 Views

0 Members and 1 Guest are viewing this topic.

Offline Sabine62

  • *
  • Moderator
  • ******
  • Posts: 971
  • It's just a jump to the left...
    • sabine62.deviantart.com
« Reply #15 on: May 17, 2020, 09:48:21 PM »
Luca was right, scaling issue:
z.xy needs to be multiplied by .5 (though that does not explain why this formula does behave quite differently to the one in mb3d...)


Code: [Select]
vec3 recFold(inout vec3 z) {
float t;
z.xy=abs(z.xy);
t=z.x;
z.x=z.x+z.y-sym;
z.y=t-z.y-asym;
t=z.x;
z.x = z.x+z.y;
z.y = t-z.y;
z.xy*=0.5;
return z;
}

Have also changed it in the frag-code in the previous message
« Last Edit: May 17, 2020, 10:07:05 PM by Sabine62 »
To thine own self be true

Offline DarkBeam

  • *
  • Global Moderator
  • *******
  • Formula crazy engineer
  • Posts: 26
  • 2 plus 2 is 4 minus 1 is 3 quick maffs
« Reply #16 on: May 17, 2020, 10:21:16 PM »
Sabine it might also be necessary to rotate the axis because adding x and y then subtracting effectively is a rotation. My code gets a bit obscure at a point so I am not sure how. Please help  :-*

Offline Sabine62

  • *
  • Moderator
  • ******
  • Posts: 971
  • It's just a jump to the left...
    • sabine62.deviantart.com
« Reply #17 on: May 17, 2020, 10:54:07 PM »
Luca, you know I have no clue about maths!  :))

Stole reasonably successful from _rotatedFolding (though not 100% the same, of course, but a little bit closer...):

Code: [Select]
// Menger Sponge formula created by Knighty
// reference http://www.fractalforums.com/ifs-iterated-function-systems/kaleidoscopic-(escape-time-ifs)/
// The distance estimation code was originally devised by Buddhi.
// reference http://www.fractalforums.com/3d-fractal-generation/a-mandelbox-distance-estimate-formula/15/
//_recFold by Darkbeam
#info Menger Sponge Distance Estimator.

#define providesInit
//#define USE_IQ_CLOUDS
#define KN_VOLUMETRIC
#define USE_EIFFIE_SHADOW
#define MULTI_SAMPLE_AO
//uniform float time;
#include "MathUtils.frag"
#include "DE-Kn2cr11.frag"



#group Menger

uniform int maxiter;  slider[0,10,50]
uniform int ColorIterations;  slider[0,3,300]
uniform float bailout; slider[0,16,100]
uniform vec3 Rotation; slider[(-180,-180,-180),(0,0,0),(180,180,180)]
uniform vec3 PreOffset; slider[(-2,-2,-2),(0,0,0),(2,2,2)]
uniform float Scale; slider[0.0,3.0,5.0]
uniform vec3 PostOffset; slider[(-2,-2,-2),(0,0,0),(2,2,2)]

#group recfold
uniform float asym; slider[-10,0,10]
uniform float sym; slider[-10,9,20]
uniform float fold; slider[0,1,1]
uniform bool recfold; checkbox[false]

vec3 recFold1(inout vec3 z) {
float t;
z.xy=abs(z.xy);
t=z.x;
z.x=z.x+z.y-sym;
z.y=t-z.y-asym;
return z;
}

vec3 recFold2(inout vec3 z) {
float u=z.x;
z.x = z.x+z.y;
z.y = u-z.y;
z.xy*=0.5;
return z;
}

mat3 rot;
void init() {
 rot = rotationMatrixXYZ(Rotation);
}

float DE(vec3 pos)
{
int i = 0;
vec3 z = vec3(pos);
float rr = 0.0;
float Dd = 1.0;
if (recfold) {
recFold1(z);
}
for( i=0; i<maxiter ; i++)
{
//if (recfold) {z.x = fold-abs(z.x+fold);recFold2(z);}
if (recfold) {z.xy = abs(z.xy+fold) - abs(z.xy-fold) - z.xy;recFold2(z);}


z *= rot;
z += PreOffset;
z = abs(z);
if (z.x - z.y < 0.0) z.xy = z.yx;
if (z.x - z.z < 0.0) z.xz = z.zx;
if (z.y - z.z < 0.0) z.yz = z.zy;

z *= Scale;
Dd *= Scale;

z.x -= 2.0;
z.y -= 2.0;
if (z.z > 1.0) z.z -= 2.0;

z += PostOffset;

rr = dot(z,z);

if (i<ColorIterations) orbitTrap = min(orbitTrap, abs(vec4(z.xyz,rr)));

if ( rr > bailout)
{
float r = sqrt(rr);
return r / Dd;
}
}
}












#preset Default
FOV = 0.5062657
Eye = -0.798576713,-2.3653276,3.9251585
Target = -0.330844641,-0.918207645,1.61163473
Up = -0.145643562,0.849645317,0.502011895
EquiRectangular = false
FocalPlane = 1
Aperture = 0
InFocusAWidth = 0
DofCorrect = true
ApertureNbrSides = 7
ApertureRot = 0
ApStarShaped = false
Gamma = 2.08335
ToneMapping = 3
Exposure = 0.6522
Brightness = 1
Contrast = 1
Saturation = 1
GaussianWeight = 1
AntiAliasScale = 2
Bloom = false
BloomIntensity = 0
BloomPow = 2
BloomTaps = 4
BloomStrong = 1
DepthToAlpha = false
ShowDepth = false
DepthMagnitude = 1
Detail = -3.173575
FudgeFactor = 0.244013683
MaxRaySteps = 629
MaxDistance = 20
Dither = 0
DetailAO = -1.52980132
coneApertureAO = 0.5
maxIterAO = 20
FudgeAO = 0.5
AO_ambient = 0.7
AO_camlight = 1
AO_pointlight = 1
AoCorrect = 0
SpecularExp = 16.364
CamLight = 1,1,1,0.489361702
AmbiantLight = 1,1,1,0.299516909
Reflection = 0,0,0
ReflectionsNumber = 0
SpotGlow = true
LightSize = 0
LightFallOff = 0
LightGlowRad = 0
LightGlowExp = 1
HardShadow = 1
ShadowSoft = 0.961098392
ShadowBlur = 0.005668934
perf = false
SSS = false
sss1 = 0.1
sss2 = 0.5
BaseColor = 1,1,1
OrbitStrength = 1
X = 1,1,1,0.198614318
Y = 0.345097989,0.666666985,0,0.055427251
Z = 1,0.666666985,0,0.692840647
R = 0.0784313977,1,0.941175997,-0.031213872
BackgroundColor = 0.450980395,0.709803939,0.866666675
GradientBackground = 0.3261
CycleColors = false
Cycles = 4.04901
EnableFloor = false
FloorNormal = 0,0,0
FloorColor = 1,1,1
HF_Fallof = 0.1
HF_Const = 0
HF_Intensity = 0
HF_Dir = 0,0,1
HF_Offset = 0
HF_Color = 1,1,1,1
HF_Scatter = 0
HF_Anisotropy = 0,0,0
HF_FogIter = 1
HF_CastShadow = false
EnCloudsDir = false
CloudDir = 0,0,1
CloudScale = 1
CloudFlatness = 0
CloudDensity = 1
CloudRoughness = 1
CloudContrast = 1
CloudColor = 0.649999976,0.680000007,0.699999988
CloudColor2 = 0.0700000003,0.170000002,0.239999995
SunLightColor = 0.699999988,0.5,0.300000012
Cloudvar1 = 0.99
Cloudvar2 = 1
CloudIter = 5
CloudBgMix = 1
WindDir = 0,0,1
WindSpeed = 1
RefineSteps = 4
NormalBackStep = 1
Specular = 1
SpotLight = 1,1,1,2.81212121
LightPos = -0.818854,1.08108107,2.16216218
FloorHeight = 0
CloudTops = 1
CloudBase = -1
asym = 0
sym = 0
recfold = true
maxiter = 10
ColorIterations = 3
bailout = 16
Rotation = 0,0,0
PreOffset = 0,0,0
Scale = 3
PostOffset = 0,0,0
fold = 0.070175438
#endpreset


Update: Sadly does not at all revert to standard menger sponge if set to 0  ::)
Good gracious, I wish I knew what I am doing  :))
« Last Edit: June 05, 2020, 11:26:37 AM by Sabine62 »

Offline Sabine62

  • *
  • Moderator
  • ******
  • Posts: 971
  • It's just a jump to the left...
    • sabine62.deviantart.com
« Reply #18 on: May 18, 2020, 12:24:25 AM »
Had another go at it, still not right but at least it reverts to standard Menger:

Code: [Select]
// Menger Sponge formula created by Knighty
// reference http://www.fractalforums.com/ifs-iterated-function-systems/kaleidoscopic-(escape-time-ifs)/
// The distance estimation code was originally devised by Buddhi.
// reference http://www.fractalforums.com/3d-fractal-generation/a-mandelbox-distance-estimate-formula/15/

#info Menger Sponge Distance Estimator.

#define providesInit
//#define USE_IQ_CLOUDS
#define KN_VOLUMETRIC
#define USE_EIFFIE_SHADOW
#define MULTI_SAMPLE_AO
//uniform float time;
#include "MathUtils.frag"
#include "DE-Kn2cr11.frag"



#group Menger

uniform int maxiter;  slider[0,10,50]
uniform int ColorIterations;  slider[0,3,300]
uniform float bailout; slider[0,16,100]
uniform vec3 Rotation; slider[(-180,-180,-180),(0,0,0),(180,180,180)]
uniform vec3 PreOffset; slider[(-2,-2,-2),(0,0,0),(2,2,2)]
uniform float Scale; slider[0.0,3.0,5.0]
uniform vec3 PostOffset; slider[(-2,-2,-2),(0,0,0),(2,2,2)]

#group recfold
uniform float asym; slider[-10,0,10]
uniform float sym; slider[-10,9,20]
uniform float fold; slider[0,1,1]
uniform bool recfold; checkbox[false]

vec3 recFold1(inout vec3 z) {
float t;
z.xy=abs(z.xy);
t=z.x;
z.x=z.x+z.y-sym;
z.y=t-z.y-asym;
return z;
}

vec3 recFold2(inout vec3 z) {
float u=z.x;
z.x = z.x-z.y;
z.y = u+z.y;
z.xy*=0.5;
return z;
}

mat3 rot;
void init() {
 rot = rotationMatrixXYZ(Rotation);
}





float DE(vec3 pos)
{
int i = 0;
vec3 z = vec3(pos);
float rr = 0.0;
float Dd = 1.0;

for( i=0; i<maxiter ; i++)
{
//if (recfold) {z.x = fold-abs(z.x+fold);recFold2(z);}
//if (recfold) {recFold1(z);z = abs(z+fold) - abs(z-fold) - z;recFold2(z);}
if (recfold) {recFold1(z);z = fold-abs(z+fold);recFold2(z);}


z *= rot;
z += PreOffset;
z = abs(z);
if (z.x - z.y < 0.0) z.xy = z.yx;
if (z.x - z.z < 0.0) z.xz = z.zx;
if (z.y - z.z < 0.0) z.yz = z.zy;

z *= Scale;
Dd *= Scale;

z.x -= 2.0;
z.y -= 2.0;
if (z.z > 1.0) z.z -= 2.0;

z += PostOffset;

rr = dot(z,z);

if (i<ColorIterations) orbitTrap = min(orbitTrap, abs(vec4(z.xyz,rr)));

if ( rr > bailout)
{

float r = sqrt(rr);
return r / Dd;
}
}
}


Offline 3DickUlus

  • *
  • Administrator
  • *******
  • Posts: 1866
    • Digilantism
« Reply #19 on: May 18, 2020, 12:47:18 AM »
Recfold2 causes the bevel.
try...
Code: [Select]
...
if (Recfold1) {
recFold1(z);
}
if (Recfold2) {
recFold2(z);
}
for( i=0; i<maxiter ; i++)
{
if (Recfold1) {z.xy = abs(z.xy+fold) - abs(z.xy-fold) - z.xy;}
if (Recfold2) recFold2(z);
...

edit: Thanks Sabine  :thumbs: some interesting structures using Knighty's menger sphere too. should have had this for the IOTM contest ;)
« Last Edit: May 18, 2020, 03:08:41 AM by 3DickUlus, Reason: pic »

Offline mclarekin

  • *
  • Fractal Freak
  • **
  • Posts: 784
« Reply #20 on: May 18, 2020, 03:47:18 AM »
@ sabine the second abs(y).  Anyhow what ever we do, we end up with  interesting folds on a menger3 (and nice images folks!).  Darkbeam is correct about a scaling, if we write out x as one line with  no offsets,  we get x = 2x, so a scaling of 0.5 would fix that


Code: [Select]
x=abs(x);
y=abs(y);

t= x;
x = x+y-sym;
y = t-y-asym;

y = -abs(y); // this one

t= x;
x = x+y;
y = t-y;

Offline Sabine62

  • *
  • Moderator
  • ******
  • Posts: 971
  • It's just a jump to the left...
    • sabine62.deviantart.com
« Reply #21 on: May 18, 2020, 08:50:02 AM »
Nice results with Menger Sphere, Dick!  :thumbs:

Graeme, doubly blind, what can I say  :embarrass: :embarrass: :embarrass:


Offline mclarekin

  • *
  • Fractal Freak
  • **
  • Posts: 784
« Reply #22 on: May 19, 2020, 09:25:29 AM »
we are all doubly blind at times, i sure am :)

Offline mclarekin

  • *
  • Fractal Freak
  • **
  • Posts: 784
« Reply #23 on: June 30, 2020, 06:13:38 AM »
I have had another play with this, I have not checked if it has same result as m3d,  added some bools.  There is still more options to test later

Code: [Select]
uniform int MaxRecFoldIters;  slider[0,4,10]
uniform float sym;  slider[-5,0,5]
uniform float asym;  slider[-5,0,5]
uniform bool negAbsX; checkbox[false]
uniform bool negAbsY; checkbox[true]
uniform float scaleZ;  slider[0,1,1]
uniform bool Xfold; checkbox[true]
uniform float foldX;  slider[-5,1,5]
uniform bool Yfold; checkbox[false]
uniform float foldY;  slider[-5,1,5]

vec3 recFold(vec3 z, int i)
{
if (i < MaxRecFoldIters) {
z.x = abs(z.x);
z.y = abs(z.y);

float t = z.x;
z.x = z.x + z.y - sym;
z.y = t - z.y - asym;
if (negAbsX) z.x = -abs(z.x);
if (negAbsY) z.y = -abs(z.y);

t = z.x;
z.x = z.x + z.y;
z.y = t - z.y;
z.x *= 0.5;
z.y *= 0.5;
z.z *= scaleZ;
if (Xfold)
z.x = foldX - abs(z.x + foldX);
if (Yfold)
z.y = foldY - abs(z.y + foldY); }
return z;
}

Offline Sabine62

  • *
  • Moderator
  • ******
  • Posts: 971
  • It's just a jump to the left...
    • sabine62.deviantart.com
« Reply #24 on: June 30, 2020, 10:09:48 AM »
What's your code in the loop, Graeme? Just recFold(z,i); ?
Does not get built here/shown in the variable editor... :(

Offline mclarekin

  • *
  • Fractal Freak
  • **
  • Posts: 784
« Reply #25 on: June 30, 2020, 12:22:49 PM »
@ sabine , here is a frag, also added an xy swap

Code: [Select]
#info Menger Distance Estimator.
#define providesInit
#include "MathUtils.frag"
#include "DE-Raytracer.frag"
#group Menger
// Based on Knighty's Kaleidoscopic IFS 3D Fractals, described here:
// http://www.fractalforums.com/3d-fractal-generation/kaleidoscopic-%28escape-time-ifs%29/
// rearranged
// testing two similar types of variable scale (only need one)
// scaleVary != 0.0 to work!
// placed an additional 3 angle rotation before the offset
//   mclarekin 23-02-19

// Pre_Rotation
uniform vec3 RotVector; slider[(0,0,0),(1,1,1),(1,1,1)]
uniform float RotAngle; slider[0.00,0,180]

// Scale parameter. A perfect Menger is 3.0
uniform float Scale; slider[0.00,3.0,4.00]
uniform float scaleVary; slider[-2.00,0.0,2.00]
uniform float scaleOffset; slider[-2.00,0.0,2.00]
uniform bool varyMode2; checkbox[false]

// 3 angle rotation
uniform vec3 Rotation; slider[(-180,-180,-180),(0,0,0),(180,180,180)]

uniform vec3 Offset; slider[(0,0,0),(1,1,1),(5,5,5)]

mat3 rotA;
mat3 rotB;
void init() {

rotA = rotationMatrix3(normalize(RotVector), RotAngle);
rotB = rotationMatrixXYZ(Rotation);
}

// Number of fractal iterations.
uniform int Iterations;  slider[0,8,100]
uniform int ColorIterations;  slider[0,8,100]
uniform float Bailout;  slider[0,20,100]
uniform float DEtweak;  slider[0,2,5]


uniform int MaxRecFoldIters;  slider[0,4,10]
uniform bool altXYswap; checkbox[false]
uniform float sym;  slider[-5,0,5]
uniform float asym;  slider[-5,0,5]
uniform bool negAbsX; checkbox[false]
uniform bool negAbsY; checkbox[true]
uniform float scaleZ;  slider[0,1,1]
uniform bool Xfold; checkbox[true]
uniform float foldX;  slider[-5,1,5]
uniform bool Yfold; checkbox[false]
uniform float foldY;  slider[-5,1,5]

vec3 recFold(vec3 z, int i)
{
if (i < MaxRecFoldIters) {
z.x = abs(z.x);
z.y = abs(z.y);
if (altXYswap) z.xy = z.yx;

float t = z.x;
z.x = z.x + z.y - sym;
z.y = t - z.y - asym;
if (negAbsX) z.x = -abs(z.x);
if (negAbsY) z.y = -abs(z.y);

t = z.x;
z.x = z.x + z.y;
z.y = t - z.y;
z.x *= 0.5;
z.y *= 0.5;
z.z *= scaleZ;
if (Xfold)
z.x = foldX - abs(z.x + foldX);
if (Yfold)
z.y = foldY - abs(z.y + foldY);


}
return z;
}



float DE(vec3 z)
{
int i = 0;
float useScale = 1.0;
float addScale = 0.0;
float Dd = 1.0;
float rr = dot(z,z);



while(rr<Bailout && (i<Iterations))  {
z = recFold (z, i);


// pre-rotation
z = rotA *z;

// MENGER SPONGE
// menger sponge abs and swizzle
z = abs(z);
if (z.x<z.y){ z.xy = z.yx;}
if (z.x<z.z){ z.xz = z.zx;}
if (z.y<z.z){ z.yz = z.zy;}

// menger sponge scale
useScale = addScale + Scale;
z *= useScale;
Dd = Dd * abs(useScale);

// update addScale for next iteration
float vary = scaleVary * (abs(addScale) - scaleOffset);
if (varyMode2) addScale = -vary;
else addScale -= vary;

// rotation inside menger sponge
z = rotB *z;

  // menger sponge conditional offset
z -= Offset;
if( z.z<-0.5*Offset.z)  z.z+=Offset.z;

//orbitTrap color
rr = dot(z,z);
if (i<ColorIterations) orbitTrap = min(orbitTrap, (vec4(abs(z),rr)));

i++;
}
return sqrt(rr) / (Dd + DEtweak);
}

Offline Sabine62

  • *
  • Moderator
  • ******
  • Posts: 971
  • It's just a jump to the left...
    • sabine62.deviantart.com
« Reply #26 on: June 30, 2020, 12:36:40 PM »
Oops, made a little error with naming a bool the same as a #group...  :embarrass: :embarrass: :embarrass:
Pasted your code and still the same happening :)) :)) :))  Blind, that's me...
Thanks, Graeme ;)

Offline Sabine62

  • *
  • Moderator
  • ******
  • Posts: 971
  • It's just a jump to the left...
    • sabine62.deviantart.com
« Reply #27 on: June 30, 2020, 01:36:06 PM »
More playstuff, thank you Graeme!  :horsie: