uudet hexat integroitu teemun shaderiin
This commit is contained in:
@ -35,34 +35,107 @@ float noise(in vec2 xy, in float seed) {
|
||||
return fract(tan(distance(xy * PHI, xy) * seed) * xy.x);
|
||||
}
|
||||
|
||||
float getScaledFFT(int index, float scale, float offset) {
|
||||
// Clamp index to valid range
|
||||
index = clamp(index, 0, 511);
|
||||
|
||||
// Get raw FFT value
|
||||
float raw = fft_output[index];
|
||||
|
||||
// Apply logarithmic scaling: log(1 + value * scale) + offset
|
||||
return log(1.0 + raw * scale) + offset;
|
||||
}
|
||||
|
||||
/////////////////
|
||||
// GEOMETRY //
|
||||
/////////////////
|
||||
|
||||
// Hexagonal prism, circumcircle variant
|
||||
float fHexagonCircumcircle(vec3 p, vec2 h) {
|
||||
vec3 q = abs(p);
|
||||
return max(q.y - h.y, max(q.x * sqrt(3.) * 0.5 + q.z * 0.5, q.z) - h.x);
|
||||
//this is mathematically equivalent to this line, but less efficient:
|
||||
//return max(q.y - h.y, max(dot(vec2(cos(PI/3), sin(PI/3)), q.zx), q.z) - h.x);
|
||||
}
|
||||
|
||||
float sdHex(vec3 pos, float i, float angle) {
|
||||
float d1 = fHexagonCircumcircle(pos, vec2(0.86, i));
|
||||
return d1;
|
||||
float hexPylon(vec3 p, vec2 h) {//float r, float ht){
|
||||
|
||||
//vec3 p = vec3(p.x, p.z, p2.y);
|
||||
vec3 b = vec3(h.x, h.y, h.x);
|
||||
|
||||
// Hexagon.
|
||||
p.xz = abs(p.xz);
|
||||
p.xz = vec2(p.x*.866025 + p.z*.5, p.z);
|
||||
// The ".015" is a subtle rounding factor. Zero gives sharp edges,
|
||||
// and larger numbers give a more rounded look.
|
||||
return length(max(abs(p) - b + .015, 0.)) - .015;
|
||||
}
|
||||
|
||||
#define zclamp(a) max(a,0.0) //Clamp negative values at zero
|
||||
float DF_RoundedHex( vec3 p, vec2 h) //float width, float height)
|
||||
{
|
||||
float width = h.x;
|
||||
float height = h.y;
|
||||
//Modified version (smooth edges) of the exagon prism found here:
|
||||
//https://iquilezles.org/articles/distfunctions
|
||||
float smoothRadius = 0.05;
|
||||
width -= smoothRadius*2.0;
|
||||
|
||||
//Hexagon prism constructed using X,Y,Z symmetry.
|
||||
//Only quadrant 1 needs to be solved, but the joining diagonal to quadrant IV is also
|
||||
//required for distance blending (see db).
|
||||
p = abs(p);
|
||||
|
||||
//Hexagonal edge distances :
|
||||
//Note : [.8666,0.5] = [sin(PI/3,cos(PI/3)] -> Hexagon edges rotation coeff (60 degrees).
|
||||
float da = (p.x*0.866025+p.z*0.5)-width; //quadrant I diagonal edge distance
|
||||
float db = (p.x*0.866025-p.z*0.5)-width; //quadrant IV diagonal edge distance (needed for blending)
|
||||
float dc = p.z-width; //upper distance
|
||||
|
||||
vec3 d = zclamp(vec3(da,db,dc));
|
||||
//Note: this is not an euclidian length, therefore this operation slightly distorts our distance field.
|
||||
//Yet, it is harmless to convergence, and does the smoothing job quite well.
|
||||
float dw = length(d)-smoothRadius; //hexagonal part smoothness (blending at 60 deg)
|
||||
float dh = p.y-height;
|
||||
|
||||
//Now that we have xz distance(dw) and y distance (dh), we can compute the distance
|
||||
//for the given isovalue (the smoothing radius).
|
||||
//Note : internal distance (maxX,maxY,maxZ) is also used to genereate internal signed dist,
|
||||
// helping convergence when overstepping (very frequent with domain repetition).
|
||||
float externalDistance = length(zclamp(vec2(dh,dw)))-smoothRadius; //Smoothed, unsigned
|
||||
float internalDistance = max(max(da,dc),dh); //Sharp, signed.
|
||||
return min(externalDistance,internalDistance);
|
||||
}
|
||||
|
||||
// Return local coordinates inside hex AND axial ID
|
||||
struct HexData {
|
||||
vec3 local; // Local position inside hex
|
||||
vec2 axial; // Axial ID (q, r)
|
||||
};
|
||||
|
||||
HexData hexTile(vec3 p, float radius) {
|
||||
float q = (sqrt(3.0)/3.0 * p.x - 1.0/3.0 * p.z) / radius;
|
||||
float r = (2.0/3.0 * p.z) / radius;
|
||||
|
||||
float rq = round(q);
|
||||
float rr = round(r);
|
||||
float rs = round(-q - r);
|
||||
|
||||
float dq = abs(rq - q);
|
||||
float dr = abs(rr - r);
|
||||
float ds = abs(rs + q + r);
|
||||
|
||||
if (dq > dr && dq > ds) rq = -rr - rs;
|
||||
else if (dr > ds) rr = -rq - rs;
|
||||
|
||||
float hx = radius * sqrt(3.0) * (rq + rr * 0.5);
|
||||
float hz = radius * 1.5 * rr;
|
||||
|
||||
HexData outData;
|
||||
outData.local = p - vec3(hx, 0.0, hz);
|
||||
outData.axial = vec2(rq, rr); // Hex ID
|
||||
return outData;
|
||||
}
|
||||
|
||||
struct HexData {
|
||||
vec3 local;
|
||||
vec2 axial;
|
||||
};
|
||||
|
||||
float hexDistance(vec2 axial) {
|
||||
float q = axial.x;
|
||||
float r = axial.y;
|
||||
float s = -q - r;
|
||||
return max(abs(q), max(abs(r), abs(s)));
|
||||
}
|
||||
|
||||
|
||||
float sdSphere(vec3 p, float r){
|
||||
return length(p) -r;
|
||||
}
|
||||
@ -86,69 +159,27 @@ vec2 mapScene(in vec3 p) {
|
||||
float mat = 1.;
|
||||
float d = 1e9;
|
||||
float a = 0.;
|
||||
float hexRadius = 0.83;
|
||||
|
||||
vec3 hexpos = vec3(p.x, p.y - 2.5, p.z);
|
||||
|
||||
vec2 rippleCenter = vec2(7.,7.);
|
||||
float rippleSpeed = 4.0;
|
||||
float rippleFreq = 1.0;
|
||||
float rippleDecay = 0.25;
|
||||
HexData hex = hexTile(hexpos, 1.0);
|
||||
|
||||
// Hexagonal grid
|
||||
float hexGap = 0.2;
|
||||
/*
|
||||
for(float j = 0.; j < 16.; j++) {
|
||||
vec3 po = p-vec3(-2.0,-5., 0.0);
|
||||
po += vec3((1.6 + hexGap) * 8, -5., -(1.88 + hexGap) * 10);
|
||||
po += vec3(0, 0., (1.88 + hexGap) * j);
|
||||
// Use axial coordinates as a stable hex ID
|
||||
float distFromCenter = hexDistance(hex.axial);
|
||||
int fftIndex = int(clamp(distFromCenter +1.0, 0.0, 511.0)); // tweak 15.0 to taste
|
||||
|
||||
for(float i = 0.; i < 16.; i++) {
|
||||
if(mod(i, 2.) == 0.) {
|
||||
po -= vec3(1.6 + hexGap, 0., 1.);
|
||||
} else {
|
||||
po += vec3(-(1.6 + hexGap), 0., 1.);
|
||||
}
|
||||
float fftVal = fft_output[fftIndex];
|
||||
float hexHeight = 1.0 + fftVal * 3.0;
|
||||
|
||||
// Add individual hexagon ripples based on distance from center
|
||||
int hexDist = int(length(vec2(i, j) - rippleCenter.xy));
|
||||
// Rotate individual hex tiles if needed
|
||||
vec3 r = hex.local;
|
||||
//r.yz *= rot2D(1.0);
|
||||
r.xz *= rot2D(0.5);
|
||||
|
||||
//float wave = sin(hexDist * rippleFreq - u_time * rippleSpeed) * exp(-hexDist * rippleDecay);
|
||||
float d1 = fHexagonCircumcircle(vec3(r.x,(r.y-hexHeight/2),r.z), vec2(hexRadius, hexHeight/2));
|
||||
res = min(res, vec2(d1,0.));
|
||||
|
||||
// Apply ripple to hexagon size and position
|
||||
|
||||
// float hexSize = fft_output[int(i+1)*int(j+1)]*5.0; // sin(1.5*u_time)+ wave
|
||||
//float hexSize = fft_output[hexDist] * 5.0;
|
||||
float hexSize = getScaledFFT(hexDist, 15.0, 0.0) * 2.0; // Adjusted multiplier
|
||||
a = sdHex(po, 1. + hexSize, 0.);
|
||||
//d = min(d, a);
|
||||
res = opU(res, vec2(a, 2.));
|
||||
}
|
||||
}
|
||||
*/
|
||||
// main note effect shapes
|
||||
// res = opU( res, vec2( sdSphere(p- vec3(2.0 + (test.x * 2.), 12. + (test.y * 10.), 0.0), 0.2 ), 1.));
|
||||
|
||||
|
||||
float gridSize = 16.0; // or GRID if you want full size
|
||||
//float hexGap = 0.2;
|
||||
|
||||
for(float j = 0.; j < gridSize; j++) {
|
||||
for(float i = 0.; i < gridSize; i++) {
|
||||
ivec2 texSize = textureSize(u_hexGridTex, 0);
|
||||
vec2 texCoord = (vec2(i, j)) / vec2(texSize);
|
||||
|
||||
vec4 hexData = texture(u_hexGridTex, texCoord); // RGBA: x, y, z, dist
|
||||
|
||||
vec3 hexPos = hexData.rgb;
|
||||
float hexDist = hexData.a;
|
||||
|
||||
// Optionally use hexDist for ripple effect with FFT
|
||||
int index = clamp(int((hexDist / 34.)*512.), 0, 511);
|
||||
float hexSize = getScaledFFT(index, 15. ,0.);
|
||||
float a = sdHex(p - hexPos, 1.0 + hexSize, 0.0);
|
||||
res = min(res, vec2(a, 1.));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
float gridSize2 = 16.;
|
||||
for(float j = 0.; j < gridSize2; j++) {
|
||||
for(float i = 0.; i < gridSize2; i++) {
|
||||
@ -166,7 +197,6 @@ vec2 mapScene(in vec3 p) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -175,24 +205,63 @@ vec2 mapScene(in vec3 p) {
|
||||
////////////////
|
||||
|
||||
vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) {
|
||||
float t = 0.;
|
||||
float mat = 0.;
|
||||
float hit = 0.;
|
||||
// Reduced from 40 to 24 steps
|
||||
for(int i = 0; i < 30; i++) {
|
||||
vec3 d;
|
||||
float t = 0.,ad,tmax=400.; // total distance travelled
|
||||
const float tolerance = 0.001;
|
||||
const float Z_REPEAT_DIST = 1.;
|
||||
vec2 res;
|
||||
|
||||
// Raymarching
|
||||
for (int i = 0; i < 50; i++) {
|
||||
pos = ro + rd * t;
|
||||
vec2 res = mapScene(pos);
|
||||
// Increase step size multiplier for faster marching
|
||||
t += res.x;
|
||||
res = mapScene(pos); // Get distance to objects
|
||||
ad = abs(res.x);
|
||||
mat = res.y;
|
||||
if(t > 400.) { // Reduced max distance
|
||||
break;
|
||||
}
|
||||
if(res.x < 0.001 * t) { // Less precise hit detection
|
||||
hit = 1.;
|
||||
if (t > tmax) break;
|
||||
if (ad < tolerance*(t*0.125 + 1.0)) {
|
||||
hit = 1.0;
|
||||
break;
|
||||
}
|
||||
t += res.x; // "march" the ray
|
||||
}
|
||||
t -= Z_REPEAT_DIST*1.;
|
||||
|
||||
for( int i=0; i<30; i++ )
|
||||
{
|
||||
vec3 pos2 = ro + rd * t;
|
||||
res = mapScene(pos2); // get distance to objects
|
||||
ad = abs(res.x);
|
||||
mat = res.y;
|
||||
if (ad < (tolerance))
|
||||
{
|
||||
hit = 1.0;
|
||||
pos = pos2;
|
||||
break;
|
||||
}
|
||||
if (t > tmax) break;
|
||||
t += min(d.x, Z_REPEAT_DIST/2.0); // "march" the ray
|
||||
}
|
||||
//if (t >= tmax) {
|
||||
// t= - 1.0;
|
||||
// hit = 0.;
|
||||
//}
|
||||
//Reduced from 40 to 24 steps
|
||||
// for(int i = 0; i < 30; i++) {
|
||||
// pos = ro + rd * t;
|
||||
// vec2 res = mapScene(pos);
|
||||
// // Increase step size multiplier for faster marching
|
||||
// t += res.x;
|
||||
// mat = res.y;
|
||||
// if(t > 400.) { // Reduced max distance
|
||||
// break;
|
||||
// }
|
||||
// if(res.x < 0.00001 * (t*0.00125 + 1.0)) { // Less precise hit detection
|
||||
// hit = 1.;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// This will break fog effect
|
||||
//if (t > 100.)
|
||||
// t = 0.;
|
||||
@ -436,7 +505,7 @@ vec3 getCameraRayDir(vec2 uv, vec3 camPos, vec3 camTarget, float fov)
|
||||
|
||||
vec3 render(vec2 uv) {
|
||||
|
||||
vec3 camPos = vec3(-20.0, 20.0, -80.0);
|
||||
vec3 camPos = vec3(-20.0, 20.0, -20.0);
|
||||
vec3 camTarget = vec3(0.0, 10.0, 0.0); // Adjust target as needed
|
||||
float fov = 1.0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user