From aacbc437aafc10df7080b859f26cd2985815764c Mon Sep 17 00:00:00 2001 From: Tanasart Date: Sun, 16 Jun 2024 13:04:40 +0700 Subject: [PATCH] sdf grid --- scripts/__raymarching/__raymarching.gml | 7 +- .../node_rm_primitive/node_rm_primitive.gml | 14 ++-- scripts/panel_preview/panel_preview.gml | 14 +++- shaders/sh_rm_primitive/sh_rm_primitive.fsh | 80 ++++++++++++++----- 4 files changed, 86 insertions(+), 29 deletions(-) diff --git a/scripts/__raymarching/__raymarching.gml b/scripts/__raymarching/__raymarching.gml index 16c9736d3..86fcbb0a8 100644 --- a/scripts/__raymarching/__raymarching.gml +++ b/scripts/__raymarching/__raymarching.gml @@ -112,9 +112,9 @@ function RM_Object() constructor { shader_set_i("tileActive", tileActive); shader_set_f("tileAmount", tileAmount); shader_set_f("tileSize", tileSpace); - shader_set_f("tileShiftPos", tilePos); - shader_set_f("tileShiftRot", tileRot); - shader_set_f("tileShiftSca", tileSca); + // shader_set_f("tileShiftPos", tilePos); + // shader_set_f("tileShiftRot", tileRot); + // shader_set_f("tileShiftSca", tileSca); shader_set_f("diffuseColor", diffuseColor); shader_set_f("reflective", reflective); @@ -250,6 +250,7 @@ function RM_Environment() constructor { shader_set_f("lightPosition", light); shader_set_i("useEnv", is_surface(bgEnv)); + shader_set_i("drawGrid", false); } } \ No newline at end of file diff --git a/scripts/node_rm_primitive/node_rm_primitive.gml b/scripts/node_rm_primitive/node_rm_primitive.gml index 6fb57ad8c..f6f065c9b 100644 --- a/scripts/node_rm_primitive/node_rm_primitive.gml +++ b/scripts/node_rm_primitive/node_rm_primitive.gml @@ -79,7 +79,7 @@ function Node_RM_Primitive(_x, _y, _group = noone) : Node_RM(_x, _y, _group) con inputs[| 19] = nodeValue("Twist Amount", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0) .setDisplay(VALUE_DISPLAY.slider, { range: [ 0, 8, 0.1 ] }); - inputs[| 20] = nodeValue("Tile Distance", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 0, 0 ]) + inputs[| 20] = nodeValue("Tile Distance", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 1, 1 ]) .setDisplay(VALUE_DISPLAY.vector); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -110,7 +110,7 @@ function Node_RM_Primitive(_x, _y, _group = noone) : Node_RM(_x, _y, _group) con ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// - inputs[| 29] = nodeValue("Tile Amount", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 0, 0 ]) + inputs[| 29] = nodeValue("Tile Amount", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 1, 1 ]) .setDisplay(VALUE_DISPLAY.vector); inputs[| 30] = nodeValue("Background", self, JUNCTION_CONNECT.input, VALUE_TYPE.color, c_black); @@ -177,7 +177,7 @@ function Node_RM_Primitive(_x, _y, _group = noone) : Node_RM(_x, _y, _group) con ["Modify", false], 12, 11, ["Deform", true], 15, 16, 17, 18, 19, ["Transform", false], 2, 3, 4, - ["Tile", false, 45], 20, 29, 46, 47, 48, + ["Tile", false, 45], 20, 29, /*46, 47, 48,*/ ["Material", false], 9, 36, 35, 37, 38, ["Camera", false], 42, 43, 13, 14, 5, 6, @@ -583,12 +583,12 @@ function Node_RM_Primitive(_x, _y, _group = noone) : Node_RM(_x, _y, _group) con var _csa = _data[43]; var _ren = _data[44]; - var _tileActive = _data[46]; + var _tileActive = _data[45]; var _tileAmount = _data[29]; var _tileSpace = _data[20]; - var _tilePos = _data[47]; - var _tileRot = _data[48]; - var _tileSca = _data[49]; + var _tilePos = _data[46]; + var _tileRot = _data[47]; + var _tileSca = _data[48]; _outSurf = surface_verify(_outSurf, _dim[0], _dim[1]); diff --git a/scripts/panel_preview/panel_preview.gml b/scripts/panel_preview/panel_preview.gml index 2c4344844..9fcffc775 100644 --- a/scripts/panel_preview/panel_preview.gml +++ b/scripts/panel_preview/panel_preview.gml @@ -1038,7 +1038,7 @@ function Panel_Preview() : PanelContent() constructor { var _scale = _dist * 2; while(_scale > 32) _scale /= 2; - + shader_set_f("axisBlend", _blend); shader_set_f("scale", _scale); shader_set_f("shift", _tx / _dist / 2, _ty / _dist / 2); @@ -1195,6 +1195,18 @@ function Panel_Preview() : PanelContent() constructor { shader_set_f("viewRange", [ d3_view_camera.view_near, d3_view_camera.view_far ]); shader_set_f("depthInt", 0); + var _scale = zm / 2; + var _step = 1; + while(_scale > 32) { + _scale /= 2; + _step /= 2; + } + + shader_set_i("drawGrid", true); + shader_set_f("gridStep", _step); + shader_set_f("gridScale", zm / 2); + shader_set_f("axisBlend", 1.); + draw_sprite_stretched(s_fx_pixel, 0, 0, 0, w, h); shader_reset(); gpu_set_texfilter(false); diff --git a/shaders/sh_rm_primitive/sh_rm_primitive.fsh b/shaders/sh_rm_primitive/sh_rm_primitive.fsh index 057694cd6..640e5eb9b 100644 --- a/shaders/sh_rm_primitive/sh_rm_primitive.fsh +++ b/shaders/sh_rm_primitive/sh_rm_primitive.fsh @@ -1,3 +1,5 @@ +#extension GL_OES_standard_derivatives : enable + //Inigo Quilez //Oh where would I be without you. @@ -90,6 +92,10 @@ uniform float ambientIntns; uniform vec3 lightPosition; uniform int useEnv; +uniform int drawGrid; +uniform float gridStep; +uniform float gridScale; +uniform float axisBlend; float influences[MAX_SHAPES]; @@ -179,6 +185,16 @@ float influences[MAX_SHAPES]; return vec2((atan(n.x, n.z) / (PI * 2.)) + 0.5, 1. - acos(n.y) / PI); } + vec4 blend(in vec4 bg, in vec4 fg) { + float al = fg.a + bg.a * (1. - fg.a); + if(al == 0.) return bg; + + vec4 res = ((fg * fg.a) + (bg * bg.a * (1. - fg.a))) / al; + res.a = al; + + return res; + } + #endregion #region ////======== 2D Primitives ========== @@ -486,7 +502,27 @@ float influences[MAX_SHAPES]; // blend and return return (x * w.x + y * w.y + z * w.z) / (w.x + w.y + w.z); } - + + + vec4 viewGrid(vec2 pos, float scale) { + vec2 coord = pos * scale; // use the scale variable to set the distance between the lines + vec2 derivative = fwidth(coord); + vec2 grid = abs(fract(coord - 0.5) - 0.5) / derivative; + float line = min(grid.x, grid.y); + float minimumy = min(derivative.y, 1.); + float minimumx = min(derivative.x, 1.); + vec4 color = vec4(.3, .3, .3, 1. - min(line, 1.)); + + // y axis + if(pos.x > -1. * minimumx / scale && pos.x < 1. * minimumx / scale) + color.y = 0.3 + axisBlend * 0.7; + // x axis + + if(pos.y > -1. * minimumy / scale && pos.y < 1. * minimumy / scale) + color.x = 0.3 + axisBlend * 0.7; + return color; + } + #endregion ////========= Ray Marching ========== @@ -508,22 +544,23 @@ float sceneSDF(int index, vec3 p) { p = wave(waveAmp[index], waveShift[index], waveInt[index], p); if(tileActive[index] == 1) { - p = tilePosition(tileAmount[index], tileSize[index], p); vec3 tindex = tileIndex(tileAmount[index], tileSize[index], p); - vec3 tpos = tileShiftPos[index] * random(tindex + vec3(1., 0., 0.)); - vec3 trot = tileShiftRot[index] * random(tindex + vec3(0., 1., 0.)); + vec3 tpos = tileShiftPos[index] * (random(tindex + vec3(1., 0., 0.)) * 2. - 1.); + vec3 trot = tileShiftRot[index] * (random(tindex + vec3(0., 1., 0.)) * 2. - 1.); float tsca = 1. + tileShiftSca[index] * (random(tindex + vec3(0., 0., 1.)) * 2. - 1.); + tindex += tpos; + p = p - tindex; + mat3 trx = rotateX(trot.x); mat3 try = rotateY(trot.y); mat3 trz = rotateZ(trot.z); mat3 trotMatrix = rx * ry * rz; - mat3 tirotMatrix = inverse(rotMatrix); + mat3 tirotMatrix = inverse(trotMatrix); sca *= tsca; p /= tsca; - p -= tpos; p = tirotMatrix * p; } @@ -642,6 +679,7 @@ float operateSceneSDF(vec3 p, out vec3 blendIndx) { blendIndx.x = float(o2); influences[o1] = 0.; + influences[o2] = 1.; depth[top] = m; index[top] = o2; @@ -735,6 +773,8 @@ float marchDensity(vec3 camera, vec3 direction) { return dens; } +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + vec4 scene() { mat3 rx = rotateX(camRotation.x); mat3 ry = rotateY(camRotation.y); @@ -767,9 +807,16 @@ vec4 scene() { vec3 coll = eye + dir * depth; vec3 norm = normal(coll); + vec4 grid = vec4(0.); + + if(drawGrid == 1 && sign(eye.y) != sign(coll.y)) { + vec3 gp = eye + dir * depth * (abs(eye.y) / (abs(coll.y) + abs(eye.y))); + grid = viewGrid( gp.xz, gridStep ); + grid.a *= clamp(1. - length(gp.xz) * gridScale, 0., 1.) * 0.75; + } if(depth > viewRange.y - EPSILON) // Not hitting anything. - return vec4(0.); + return drawGrid == 1? grid : vec4(0.); /////////////////////////////////////////////////////////// @@ -818,17 +865,14 @@ vec4 scene() { float lamo = min(1., max(0., dot(norm, light)) + ambientIntns); c = mix(c * background.rgb, c, lamo); - return vec4(c, 1.); -} - -vec4 blend(in vec4 bg, in vec4 fg) { - float al = fg.a + bg.a * (1. - fg.a); - if(al == 0.) return bg; - - vec4 res = ((fg * fg.a) + (bg * bg.a * (1. - fg.a))) / al; - res.a = al; - - return res; + /////////////////////////////////////////////////////////// + + vec4 res = vec4(c, 1.); + + if(drawGrid == 1 && sign(eye.y) != sign(coll.y)) + res = blend(res, grid); + + return res; } void main() {