Im still learning glsl / vector math very new but how would i replicate the applyMatrix function applyMatrix in glsl considering its a mat4 and also the different drawing /rendering pipeline of p5js along w its position above my push pop translate for the loop versus glsl tops pixel / fragment pipeline and its different matrix function in general if glsl computes it differently
but same mat4 values i used so far tried within a table dat w 4 rows/cols for the matrix parameter in my glsl top matricies define
.Also will this only work for raymarching in my pixel shader if so my first glsl code here will be my raymarching code id like to apply it to (and without if possible) and then after ill put my two different attempts at adding the mat4 to match p5.js pipeline / matrix effect and first of course heres my p5js loop with applyMatrix
function setup() {
createCanvas(600, 600, WEBGL);
}
function draw() {
background(0);
strokeWeight(1.5);
stroke(255);
applyMatrix(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0, 0.0,
0.0, 0.0, 1.0, 0.005,
0.0, 0.0, 0.0, 1.0
);
lfo1 = 154cos(frameCount0.05);
for (let x = -300; x <= 300; x += 10) {
for (let y = -300; y <= 300; y += 10) {
push();
translate(x, y);
fill(0);
box(10, 10, 10+lfo1);
pop();
}
}
}
//help apply matrix to this
uniform vec2 iResolution;
uniform float uTime;
uniform float lfo1;
uniform float lfo2;
uniform mat4 mMatrix;
mat2 rot2D(float angle) {
float s = sin(angle);
float c = cos(angle);
return mat2(c, -s, s, c);
}
float sdBox(vec3 p, vec3 b) {
vec3 boxp = vec3 (3.0, 0.0, 10.0);
vec3 d = abs((p)-boxp) - b;
return length(max(d, 0.0)) + min(max(d.x, max(d.y, d.z)), 0.0);
}
float map(vec3 p) {
float m = 10.0;
for (float x = -15.0; x <= 15.0; x += 1.5) {
for (float y = -15.0; y <= 15.0; y +=1.5) {
vec3 boxPos = vec3(float(x), float(y), 1.0);
float d = sdBox(p - boxPos, vec3(0.5));
m = min(m, d);
}
}
return m;
}
out vec4 fragColor;
void main() {
vec2 uv = (gl_FragCoord.xy * 2.0 - iResolution) / iResolution.y;
vec3 ro = vec3(0.0, 0.0, -3.0);
vec3 rd = normalize(vec3(uv, 1.0));
vec3 col = vec3(0);
float t = 0.0;
for (int i = 0; i < 80; i++) {
vec3 p = ro + rd * t;
float d = map(p);
t += d;
if (d < 0.001 || t > 100.0) break;
}
col = vec3(t * 0.025);
fragColor = vec4(col, 1.0);
}
// my attempts
//mat4 * box sdf
uniform vec2 iResolution;
uniform float uTime;
uniform float lfo1;
uniform float lfo2;
uniform mat4 mMatrix;
float sdBox(vec3 p, vec3 b) {
vec3 boxp = vec3(0.0, 0.0, 10.0);
vec3 d = abs(p - boxp) - b;
return length(max(d, 0.0)) + min(max(d.x, max(d.y, d.z)), 0.0);
}
float map(vec3 p) {
float m = 10.0;
for (float x = -15.0; x <= 15.0; x += 0.5) {
for (float y = -15.0; y <= 15.0; y += 0.5) {
vec3 boxPos = vec3(float(x), float(y), 1.0);
boxPos = (mMatrix * vec4(boxPos, 1.0)).xyz;
float d = sdBox(p - boxPos, vec3(0.5));
m = min(m, d);
}
}
return m;
}
out vec4 fragColor;
void main() {
vec2 uv = (gl_FragCoord.xy * 2.0 - iResolution) / iResolution.y;
vec3 ro = vec3(0.0, 0.0, -3.0);
vec3 rd = normalize(vec3(uv, 1.0));
vec3 col = vec3(0);
float t = 0.0;
for (int i = 0; i < 80; i++) {
vec3 p = ro + rd * t;
float d = map(p);
t += d;
if (d < 0.001 || t > 100.0) break;
}
col = vec3(t * 0.025);
fragColor = vec4(col, 1.0);
}
//mat4 * box pos
uniform vec2 iResolution;
uniform float uTime;
uniform float lfo1;
uniform float lfo2;
uniform mat4 mMatrix;
mat2 rot2D(float angle) {
float s = sin(angle);
float c = cos(angle);
return mat2(c, -s, s, c);
}
float sdBox(vec3 p, vec3 b) {
vec3 boxp = vec3(0.0, 0.0, 0.0);
vec4 transformedBoxPos = mMatrix * vec4(boxp, 1.0);
boxp = transformedBoxPos.xyz;
vec3 d = abs(p - boxp) - b;
return length(max(d, 0.0)) + min(max(d.x, max(d.y, d.z)), 0.0);
}
float map(vec3 p) {
float m = 10.0;
for (float x = -15.0; x <= 15.0; x += 0.5) {
for (float y = -15.0; y <= 15.0; y += 0.5) {
vec3 boxPos = vec3(float(x), float(y), 1.0);
float d = sdBox(p - boxPos, vec3(0.5));
m = min(m, d);
}
}
return m;
}
out vec4 fragColor;
void main() {
vec2 uv = (gl_FragCoord.xy * 2.0 - iResolution) / iResolution.y;
vec3 ro = vec3(0.0, 0.0, -3.0);
vec3 rd = normalize(vec3(uv, 1.0));
vec3 col = vec3(0);
float t = 0.0;
for (int i = 0; i < 80; i++) {
vec3 p = ro + rd * t;
float d = map(p);
t += d;
if (d < 0.001 || t > 100.0) break;
}
col = vec3(t * 0.025);
fragColor = vec4(col, 1.0);
}