diff --git a/addons/material_maker/engine/gen_sdf.gd b/addons/material_maker/engine/gen_sdf.gd index 08ffa722..22a61f9f 100644 --- a/addons/material_maker/engine/gen_sdf.gd +++ b/addons/material_maker/engine/gen_sdf.gd @@ -27,14 +27,26 @@ func get_filtered_parameter_defs(parameters_filter : String) -> Array: return defs func get_output_defs(_show_hidden : bool = false) -> Array: + var outputs : Array + var color_output : String + var gs_output : String match get_scene_type(): "SDF3D": - return [ { type="sdf3d" } ] + outputs = [ { type="sdf3d" } ] + color_output = "tex3d" + gs_output = "tex3d_gs" _: if editor: - return [ { type="rgb" } ] + outputs = [ { type="rgb" } ] else: - return [ { type="sdf2d" } ] + outputs = [ { type="sdf2d" } ] + color_output = "rgba" + gs_output = "float" + outputs.push_back({type=color_output, channel="albedo"}) + outputs.push_back({type=gs_output, channel="metallic"}) + outputs.push_back({type=gs_output, channel="roughness"}) + outputs.push_back({type=color_output, channel="emission"}) + return outputs func get_scene_type() -> String: if scene.empty(): diff --git a/addons/material_maker/nodes/fbm2.mmg b/addons/material_maker/nodes/fbm2.mmg index da20b3f7..82ff5529 100644 --- a/addons/material_maker/nodes/fbm2.mmg +++ b/addons/material_maker/nodes/fbm2.mmg @@ -7,7 +7,7 @@ "parameters": { "folds": 0, "iterations": 5, - "noise": 0, + "noise": 9, "offset": 0, "persistence": 0.5, "scale_x": 2, @@ -17,7 +17,7 @@ "seed_locked": false, "shader_model": { "code": "", - "global": "float fbm_value(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat p00 = rand(mod(o, size));\n\tfloat p01 = rand(mod(o + vec2(0.0, 1.0), size));\n\tfloat p10 = rand(mod(o + vec2(1.0, 0.0), size));\n\tfloat p11 = rand(mod(o + vec2(1.0, 1.0), size));\n\tp00 = sin(p00 * 6.28318530718 + offset * 6.28318530718) / 2.0 + 0.5;\n\tp01 = sin(p01 * 6.28318530718 + offset * 6.28318530718) / 2.0 + 0.5;\n\tp10 = sin(p10 * 6.28318530718 + offset * 6.28318530718) / 2.0 + 0.5;\n\tp11 = sin(p11 * 6.28318530718 + offset * 6.28318530718) / 2.0 + 0.5;\n\tvec2 t = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n\treturn mix(mix(p00, p10, t.x), mix(p01, p11, t.x), t.y);\n}\n\nfloat fbm_perlin(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat a00 = rand(mod(o, size)) * 6.28318530718 + offset * 6.28318530718;\n\tfloat a01 = rand(mod(o + vec2(0.0, 1.0), size)) * 6.28318530718 + offset * 6.28318530718;\n\tfloat a10 = rand(mod(o + vec2(1.0, 0.0), size)) * 6.28318530718 + offset * 6.28318530718;\n\tfloat a11 = rand(mod(o + vec2(1.0, 1.0), size)) * 6.28318530718 + offset * 6.28318530718;\n\tvec2 v00 = vec2(cos(a00), sin(a00));\n\tvec2 v01 = vec2(cos(a01), sin(a01));\n\tvec2 v10 = vec2(cos(a10), sin(a10));\n\tvec2 v11 = vec2(cos(a11), sin(a11));\n\tfloat p00 = dot(v00, f);\n\tfloat p01 = dot(v01, f - vec2(0.0, 1.0));\n\tfloat p10 = dot(v10, f - vec2(1.0, 0.0));\n\tfloat p11 = dot(v11, f - vec2(1.0, 1.0));\n\tvec2 t = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n\treturn 0.5 + mix(mix(p00, p10, t.x), mix(p01, p11, t.x), t.y);\n}\n\nfloat fbm_perlinabs(vec2 coord, vec2 size, float offset, float seed) {\n\treturn abs(2.0*fbm_perlin(coord, size, offset, seed)-1.0);\n}\n\nfloat mod289(float x) {\n return x - floor(x * (1.0 / 289.0)) * 289.0;\n}\n\nfloat permute(float x) {\n return mod289(((x * 34.0) + 1.0) * x);\n}\n\nvec2 rgrad2(vec2 p, float rot, float seed) {\n\tfloat u = permute(permute(p.x) + p.y) * 0.0243902439 + rot; // Rotate by shift\n\tu = fract(u) * 6.28318530718; // 2*pi\n\treturn vec2(cos(u), sin(u));\n}\n\nfloat fbm_simplex(vec2 coord, vec2 size, float offset, float seed) {\n\tcoord *= 2.0; // needed for it to tile\n\tcoord += rand2(vec2(seed, 1.0-seed)) + size;\n\tsize *= 2.0; // needed for it to tile\n\tcoord.y += 0.001;\n vec2 uv = vec2(coord.x + coord.y*0.5, coord.y);\n vec2 i0 = floor(uv);\n vec2 f0 = fract(uv);\n vec2 i1 = (f0.x > f0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);\n vec2 p0 = vec2(i0.x - i0.y * 0.5, i0.y);\n vec2 p1 = vec2(p0.x + i1.x - i1.y * 0.5, p0.y + i1.y);\n vec2 p2 = vec2(p0.x + 0.5, p0.y + 1.0);\n i1 = i0 + i1;\n vec2 i2 = i0 + vec2(1.0, 1.0);\n vec2 d0 = coord - p0;\n vec2 d1 = coord - p1;\n vec2 d2 = coord - p2;\n vec3 xw = mod(vec3(p0.x, p1.x, p2.x), size.x);\n vec3 yw = mod(vec3(p0.y, p1.y, p2.y), size.y);\n vec3 iuw = xw + 0.5 * yw;\n vec3 ivw = yw;\n vec2 g0 = rgrad2(vec2(iuw.x, ivw.x), offset, seed);\n vec2 g1 = rgrad2(vec2(iuw.y, ivw.y), offset, seed);\n vec2 g2 = rgrad2(vec2(iuw.z, ivw.z), offset, seed);\n vec3 w = vec3(dot(g0, d0), dot(g1, d1), dot(g2, d2));\n vec3 t = 0.8 - vec3(dot(d0, d0), dot(d1, d1), dot(d2, d2));\n t = max(t, vec3(0.0));\n vec3 t2 = t * t;\n vec3 t4 = t2 * t2;\n float n = dot(t4, w);\n return 0.5 + 5.5 * n;\n}\n\nfloat fbm_cellular(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.25 * sin(offset * 6.28318530718 + 6.28318530718 * node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = length(diff);\n\t\t\tmin_dist = min(min_dist, dist);\n\t\t}\n\t}\n\treturn min_dist;\n}\n\nfloat fbm_cellular2(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist1 = 2.0;\n\tfloat min_dist2 = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.25 * sin(offset * 6.28318530718 + 6.28318530718*node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = length(diff);\n\t\t\tif (min_dist1 > dist) {\n\t\t\t\tmin_dist2 = min_dist1;\n\t\t\t\tmin_dist1 = dist;\n\t\t\t} else if (min_dist2 > dist) {\n\t\t\t\tmin_dist2 = dist;\n\t\t\t}\n\t\t}\n\t}\n\treturn min_dist2-min_dist1;\n}\n\nfloat fbm_cellular3(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.25 * sin(offset * 6.28318530718 + 6.28318530718*node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = abs((diff).x) + abs((diff).y);\n\t\t\tmin_dist = min(min_dist, dist);\n\t\t}\n\t}\n\treturn min_dist;\n}\n\nfloat fbm_cellular4(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist1 = 2.0;\n\tfloat min_dist2 = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.25 * sin(offset * 6.28318530718 + 6.28318530718*node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = abs((diff).x) + abs((diff).y);\n\t\t\tif (min_dist1 > dist) {\n\t\t\t\tmin_dist2 = min_dist1;\n\t\t\t\tmin_dist1 = dist;\n\t\t\t} else if (min_dist2 > dist) {\n\t\t\t\tmin_dist2 = dist;\n\t\t\t}\n\t\t}\n\t}\n\treturn min_dist2-min_dist1;\n}\n\nfloat fbm_cellular5(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.5 * sin(offset * 6.28318530718 + 6.28318530718*node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = max(abs((diff).x), abs((diff).y));\n\t\t\tmin_dist = min(min_dist, dist);\n\t\t}\n\t}\n\treturn min_dist;\n}\n\nfloat fbm_cellular6(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist1 = 2.0;\n\tfloat min_dist2 = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.25 * sin(offset * 6.28318530718 + 6.28318530718*node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = max(abs((diff).x), abs((diff).y));\n\t\t\tif (min_dist1 > dist) {\n\t\t\t\tmin_dist2 = min_dist1;\n\t\t\t\tmin_dist1 = dist;\n\t\t\t} else if (min_dist2 > dist) {\n\t\t\t\tmin_dist2 = dist;\n\t\t\t}\n\t\t}\n\t}\n\treturn min_dist2-min_dist1;\n}\n\n// MIT License Inigo Quilez - https://www.shadertoy.com/view/Xd23Dh\nfloat fbm_voronoise( vec2 coord, vec2 size, float offset, float seed) {\n vec2 i = floor(coord) + rand2(vec2(seed, 1.0-seed)) + size;\n vec2 f = fract(coord);\n \n\tvec2 a = vec2(0.0);\n\t\n for( int y=-2; y<=2; y++ ) {\n \tfor( int x=-2; x<=2; x++ ) {\n \tvec2 g = vec2( float(x), float(y) );\n\t\t\tvec3 o = rand3( mod(i + g, size) + vec2(seed) );\n\t\t\to.xy += 0.25 * sin(offset * 6.28318530718 + 6.28318530718*o.xy);\n\t\t\tvec2 d = g - f + o.xy;\n\t\t\tfloat w = pow( 1.0-smoothstep(0.0, 1.414, length(d)), 1.0 );\n\t\t\ta += vec2(o.z*w,w);\n\t\t}\n }\n\t\n return a.x/a.y;\n}", + "global": "float value_noise_2d(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat p00 = rand(mod(o, size));\n\tfloat p01 = rand(mod(o + vec2(0.0, 1.0), size));\n\tfloat p10 = rand(mod(o + vec2(1.0, 0.0), size));\n\tfloat p11 = rand(mod(o + vec2(1.0, 1.0), size));\n\tp00 = sin(p00 * 6.28318530718 + offset * 6.28318530718) / 2.0 + 0.5;\n\tp01 = sin(p01 * 6.28318530718 + offset * 6.28318530718) / 2.0 + 0.5;\n\tp10 = sin(p10 * 6.28318530718 + offset * 6.28318530718) / 2.0 + 0.5;\n\tp11 = sin(p11 * 6.28318530718 + offset * 6.28318530718) / 2.0 + 0.5;\n\tvec2 t = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n\treturn mix(mix(p00, p10, t.x), mix(p01, p11, t.x), t.y);\n}\n\nfloat fbm_2d_value(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = value_noise_2d(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n\nfloat perlin_noise_2d(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat a00 = rand(mod(o, size)) * 6.28318530718 + offset * 6.28318530718;\n\tfloat a01 = rand(mod(o + vec2(0.0, 1.0), size)) * 6.28318530718 + offset * 6.28318530718;\n\tfloat a10 = rand(mod(o + vec2(1.0, 0.0), size)) * 6.28318530718 + offset * 6.28318530718;\n\tfloat a11 = rand(mod(o + vec2(1.0, 1.0), size)) * 6.28318530718 + offset * 6.28318530718;\n\tvec2 v00 = vec2(cos(a00), sin(a00));\n\tvec2 v01 = vec2(cos(a01), sin(a01));\n\tvec2 v10 = vec2(cos(a10), sin(a10));\n\tvec2 v11 = vec2(cos(a11), sin(a11));\n\tfloat p00 = dot(v00, f);\n\tfloat p01 = dot(v01, f - vec2(0.0, 1.0));\n\tfloat p10 = dot(v10, f - vec2(1.0, 0.0));\n\tfloat p11 = dot(v11, f - vec2(1.0, 1.0));\n\tvec2 t = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n\treturn 0.5 + mix(mix(p00, p10, t.x), mix(p01, p11, t.x), t.y);\n}\n\nfloat fbm_2d_perlin(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = perlin_noise_2d(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n\nfloat perlinabs_noise_2d(vec2 coord, vec2 size, float offset, float seed) {\n\treturn abs(2.0*perlin_noise_2d(coord, size, offset, seed)-1.0);\n}\n\nfloat fbm_2d_perlinabs(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = perlinabs_noise_2d(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n\nfloat fbm_2d_mod289(float x) {\n return x - floor(x * (1.0 / 289.0)) * 289.0;\n}\n\nfloat fbm_2d_permute(float x) {\n return fbm_2d_mod289(((x * 34.0) + 1.0) * x);\n}\n\nvec2 fbm_2d_rgrad2(vec2 p, float rot, float seed) {\n\tfloat u = fbm_2d_permute(fbm_2d_permute(p.x) + p.y) * 0.0243902439 + rot; // Rotate by shift\n\tu = fract(u) * 6.28318530718; // 2*pi\n\treturn vec2(cos(u), sin(u));\n}\n\nfloat simplex_noise_2d(vec2 coord, vec2 size, float offset, float seed) {\n\tcoord *= 2.0; // needed for it to tile\n\tcoord += rand2(vec2(seed, 1.0-seed)) + size;\n\tsize *= 2.0; // needed for it to tile\n\tcoord.y += 0.001;\n vec2 uv = vec2(coord.x + coord.y*0.5, coord.y);\n vec2 i0 = floor(uv);\n vec2 f0 = fract(uv);\n vec2 i1 = (f0.x > f0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);\n vec2 p0 = vec2(i0.x - i0.y * 0.5, i0.y);\n vec2 p1 = vec2(p0.x + i1.x - i1.y * 0.5, p0.y + i1.y);\n vec2 p2 = vec2(p0.x + 0.5, p0.y + 1.0);\n i1 = i0 + i1;\n vec2 i2 = i0 + vec2(1.0, 1.0);\n vec2 d0 = coord - p0;\n vec2 d1 = coord - p1;\n vec2 d2 = coord - p2;\n vec3 xw = mod(vec3(p0.x, p1.x, p2.x), size.x);\n vec3 yw = mod(vec3(p0.y, p1.y, p2.y), size.y);\n vec3 iuw = xw + 0.5 * yw;\n vec3 ivw = yw;\n vec2 g0 = fbm_2d_rgrad2(vec2(iuw.x, ivw.x), offset, seed);\n vec2 g1 = fbm_2d_rgrad2(vec2(iuw.y, ivw.y), offset, seed);\n vec2 g2 = fbm_2d_rgrad2(vec2(iuw.z, ivw.z), offset, seed);\n vec3 w = vec3(dot(g0, d0), dot(g1, d1), dot(g2, d2));\n vec3 t = 0.8 - vec3(dot(d0, d0), dot(d1, d1), dot(d2, d2));\n t = max(t, vec3(0.0));\n vec3 t2 = t * t;\n vec3 t4 = t2 * t2;\n float n = dot(t4, w);\n return 0.5 + 5.5 * n;\n}\n\nfloat fbm_2d_simplex(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = simplex_noise_2d(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n\nfloat cellular_noise_2d(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.25 * sin(offset * 6.28318530718 + 6.28318530718 * node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = length(diff);\n\t\t\tmin_dist = min(min_dist, dist);\n\t\t}\n\t}\n\treturn min_dist;\n}\n\nfloat fbm_2d_cellular(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = cellular_noise_2d(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n\nfloat cellular2_noise_2d(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist1 = 2.0;\n\tfloat min_dist2 = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.25 * sin(offset * 6.28318530718 + 6.28318530718*node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = length(diff);\n\t\t\tif (min_dist1 > dist) {\n\t\t\t\tmin_dist2 = min_dist1;\n\t\t\t\tmin_dist1 = dist;\n\t\t\t} else if (min_dist2 > dist) {\n\t\t\t\tmin_dist2 = dist;\n\t\t\t}\n\t\t}\n\t}\n\treturn min_dist2-min_dist1;\n}\n\nfloat fbm_2d_cellular2(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = cellular2_noise_2d(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n\nfloat cellular3_noise_2d(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.25 * sin(offset * 6.28318530718 + 6.28318530718*node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = abs((diff).x) + abs((diff).y);\n\t\t\tmin_dist = min(min_dist, dist);\n\t\t}\n\t}\n\treturn min_dist;\n}\n\nfloat fbm_2d_cellular3(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = cellular3_noise_2d(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n\nfloat cellular4_noise_2d(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist1 = 2.0;\n\tfloat min_dist2 = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.25 * sin(offset * 6.28318530718 + 6.28318530718*node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = abs((diff).x) + abs((diff).y);\n\t\t\tif (min_dist1 > dist) {\n\t\t\t\tmin_dist2 = min_dist1;\n\t\t\t\tmin_dist1 = dist;\n\t\t\t} else if (min_dist2 > dist) {\n\t\t\t\tmin_dist2 = dist;\n\t\t\t}\n\t\t}\n\t}\n\treturn min_dist2-min_dist1;\n}\n\nfloat fbm_2d_cellular4(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = cellular4_noise_2d(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n\nfloat cellular5_noise_2d(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.5 * sin(offset * 6.28318530718 + 6.28318530718*node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = max(abs((diff).x), abs((diff).y));\n\t\t\tmin_dist = min(min_dist, dist);\n\t\t}\n\t}\n\treturn min_dist;\n}\n\nfloat fbm_2d_cellular5(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = cellular5_noise_2d(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n\nfloat cellular6_noise_2d(vec2 coord, vec2 size, float offset, float seed) {\n\tvec2 o = floor(coord)+rand2(vec2(seed, 1.0-seed))+size;\n\tvec2 f = fract(coord);\n\tfloat min_dist1 = 2.0;\n\tfloat min_dist2 = 2.0;\n\tfor(float x = -1.0; x <= 1.0; x++) {\n\t\tfor(float y = -1.0; y <= 1.0; y++) {\n\t\t\tvec2 neighbor = vec2(float(x),float(y));\n\t\t\tvec2 node = rand2(mod(o + vec2(x, y), size)) + vec2(x, y);\n\t\t\tnode = 0.5 + 0.25 * sin(offset * 6.28318530718 + 6.28318530718*node);\n\t\t\tvec2 diff = neighbor + node - f;\n\t\t\tfloat dist = max(abs((diff).x), abs((diff).y));\n\t\t\tif (min_dist1 > dist) {\n\t\t\t\tmin_dist2 = min_dist1;\n\t\t\t\tmin_dist1 = dist;\n\t\t\t} else if (min_dist2 > dist) {\n\t\t\t\tmin_dist2 = dist;\n\t\t\t}\n\t\t}\n\t}\n\treturn min_dist2-min_dist1;\n}\n\nfloat fbm_2d_cellular6(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = cellular6_noise_2d(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n\n// MIT License Inigo Quilez - https://www.shadertoy.com/view/Xd23Dh\nfloat voronoise_noise_2d( vec2 coord, vec2 size, float offset, float seed) {\n vec2 i = floor(coord) + rand2(vec2(seed, 1.0-seed)) + size;\n vec2 f = fract(coord);\n \n\tvec2 a = vec2(0.0);\n\t\n for( int y=-2; y<=2; y++ ) {\n \tfor( int x=-2; x<=2; x++ ) {\n \tvec2 g = vec2( float(x), float(y) );\n\t\t\tvec3 o = rand3( mod(i + g, size) + vec2(seed) );\n\t\t\to.xy += 0.25 * sin(offset * 6.28318530718 + 6.28318530718*o.xy);\n\t\t\tvec2 d = g - f + o.xy;\n\t\t\tfloat w = pow( 1.0-smoothstep(0.0, 1.414, length(d)), 1.0 );\n\t\t\ta += vec2(o.z*w,w);\n\t\t}\n }\n\t\n return a.x/a.y;\n}\n\nfloat fbm_2d_voronoise(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = voronoise_noise_2d(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n", "inputs": [ { "default": "$offset", @@ -28,12 +28,12 @@ "type": "f" } ], - "instance": "float $(name)_fbm(vec2 coord, vec2 size, int folds, int octaves, float persistence, float offset, float seed) {\n\tfloat normalize_factor = 0.0;\n\tfloat value = 0.0;\n\tfloat scale = 1.0;\n\tfor (int i = 0; i < octaves; i++) {\n\t\tfloat noise = fbm_$noise(coord*size, size, offset, seed);\n\t\tfor (int f = 0; f < folds; ++f) {\n\t\t\tnoise = abs(2.0*noise-1.0);\n\t\t}\n\t\tvalue += noise * scale;\n\t\tnormalize_factor += scale;\n\t\tsize *= 2.0;\n\t\tscale *= persistence;\n\t}\n\treturn value / normalize_factor;\n}\n", + "instance": "", "longdesc": "Generates a noise made of several octaves of a simple noise", "name": "FBM Noise", "outputs": [ { - "f": "$(name)_fbm($(uv), vec2($(scale_x), $(scale_y)), int($(folds)), int($(iterations)), $(persistence), $offset_in($uv), $(seed))", + "f": "fbm_2d_$noise($(uv), vec2($(scale_x), $(scale_y)), int($(folds)), int($(iterations)), $(persistence), $offset_in($uv), $(seed))", "longdesc": "Shows a greyscale image of the generated noise", "shortdesc": "Output", "type": "f" diff --git a/addons/material_maker/sdf_builder/sdf2d/color.gd b/addons/material_maker/sdf_builder/sdf2d/color.gd new file mode 100644 index 00000000..3db26b65 --- /dev/null +++ b/addons/material_maker/sdf_builder/sdf2d/color.gd @@ -0,0 +1,20 @@ +extends "res://addons/material_maker/sdf_builder/sdf2d/union.gd" + +export var channel_name : String +export(int, "greyscale", "rgba") var type : int + +func _ready(): + pass # Replace with function body. + +func get_children_types(): + return [ "TEX" ] + +func get_parameter_defs(): + return [ + ] + +func get_includes(): + return [ ] + +func scene_to_shader_model(scene : Dictionary, uv : String = "$uv", editor : bool = false) -> Dictionary: + return {} diff --git a/addons/material_maker/sdf_builder/sdf_builder.gd b/addons/material_maker/sdf_builder/sdf_builder.gd index 3742b28f..a173717f 100644 --- a/addons/material_maker/sdf_builder/sdf_builder.gd +++ b/addons/material_maker/sdf_builder/sdf_builder.gd @@ -90,7 +90,8 @@ func scene_to_shader_model(scene : Dictionary, uv : String = "$uv", editor = fal _: print("Unsupported parameter %s of type %s" % [ p.name, p.type ]) return {} - shader_model.includes = get_includes(scene) + if ! shader_model.empty(): + shader_model.includes = get_includes(scene) return shader_model func generate_rotate_3d(variable, _scene) -> String: diff --git a/addons/material_maker/sdf_builder/sdf_builder.tscn b/addons/material_maker/sdf_builder/sdf_builder.tscn index 049e54f4..b723ec23 100644 --- a/addons/material_maker/sdf_builder/sdf_builder.tscn +++ b/addons/material_maker/sdf_builder/sdf_builder.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=50 format=2] +[gd_scene load_steps=51 format=2] [ext_resource path="res://addons/material_maker/sdf_builder/sdf2d/circle.gd" type="Script" id=1] [ext_resource path="res://addons/material_maker/sdf_builder/sdf_builder.gd" type="Script" id=2] @@ -36,6 +36,7 @@ [ext_resource path="res://addons/material_maker/sdf_builder/sdf3d/revolution.gd" type="Script" id=34] [ext_resource path="res://addons/material_maker/sdf_builder/sdf3d/cylinder.gd" type="Script" id=35] [ext_resource path="res://addons/material_maker/sdf_builder/sdf2d/polygon.gd" type="Script" id=36] +[ext_resource path="res://addons/material_maker/sdf_builder/sdf2d/color.gd" type="Script" id=37] [sub_resource type="AtlasTexture" id=1] flags = 4 @@ -246,6 +247,34 @@ script = ExtResource( 13 ) item_type = "Morph" item_category = "SDF2D" +[node name="Color" type="Node" parent="SDF2D"] + +[node name="Albedo" type="Node" parent="SDF2D/Color"] +script = ExtResource( 37 ) +item_type = "Albedo2D" +item_category = "SDF2D" +channel_name = "albedo" +type = 1 + +[node name="Metallic" type="Node" parent="SDF2D/Color"] +script = ExtResource( 37 ) +item_type = "Metallic2D" +item_category = "SDF2D" +channel_name = "metallic" + +[node name="Roughness" type="Node" parent="SDF2D/Color"] +script = ExtResource( 37 ) +item_type = "Roughness2D" +item_category = "SDF2D" +channel_name = "roughness" + +[node name="Emission" type="Node" parent="SDF2D/Color"] +script = ExtResource( 37 ) +item_type = "Albedo2D" +item_category = "SDF2D" +channel_name = "emission" +type = 1 + [node name="SDF3D" type="Node" parent="."] [node name="Shape" type="Node" parent="SDF3D"] diff --git a/material_maker/windows/sdf_builder/preview_2d.gd b/material_maker/windows/sdf_builder/preview_2d.gd new file mode 100644 index 00000000..1f71671a --- /dev/null +++ b/material_maker/windows/sdf_builder/preview_2d.gd @@ -0,0 +1,5 @@ +extends "res://material_maker/panels/preview_2d/preview_2d_panel.gd" + +func _ready(): + pass # Replace with function body. + diff --git a/material_maker/windows/sdf_builder/preview_2d.tscn b/material_maker/windows/sdf_builder/preview_2d.tscn new file mode 100644 index 00000000..2ccea65a --- /dev/null +++ b/material_maker/windows/sdf_builder/preview_2d.tscn @@ -0,0 +1,91 @@ +[gd_scene load_steps=7 format=2] + +[ext_resource path="res://material_maker/panels/preview_2d/preview_2d_panel.tscn" type="PackedScene" id=1] + +[sub_resource type="Shader" id=2] +resource_local_to_scene = true +code = "shader_type canvas_item; + +void fragment() { + COLOR = vec4(0.0, 0.0, 0.0, 1.0); +} +" + +[sub_resource type="ShaderMaterial" id=3] +resource_local_to_scene = true +shader = SubResource( 2 ) + +[sub_resource type="GDScript" id=1] +script/source = "extends \"res://material_maker/panels/preview_2d/preview_2d_panel.gd\" + +func _ready(): + pass # Replace with function body. +" + +[sub_resource type="Shader" id=4] +resource_local_to_scene = true + +[sub_resource type="ShaderMaterial" id=5] +resource_local_to_scene = true +shader = SubResource( 4 ) + +[node name="Preview2D" instance=ExtResource( 1 )] +material = SubResource( 3 ) +script = SubResource( 1 ) +shader = "uniform vec2 preview_2d_size = vec2(100.0); +uniform float preview_2d_scale = 1.2; +uniform vec2 preview_2d_center = vec2(0.5); +uniform int mode = 0; +uniform vec4 background_color_1 = vec4(0.0); +uniform vec4 background_color_2 = vec4(1.0); + +void fragment() { + vec2 ratio = preview_2d_size; + vec2 uv = preview_2d_center+(UV-0.5)*preview_2d_scale*ratio/min(ratio.x, ratio.y); + if (mode == 2 && (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0)) { + COLOR = vec4(0.0); + } else { + if (mode == 1) { + uv = fract(uv); + } + vec4 image = preview_2d_postprocessed(uv); + float checkerboard = mod(floor(uv.x*32.0)+floor(uv.y*32.0), 2.0); + vec3 image_with_background = mix(mix(background_color_1, background_color_2, checkerboard).rgb, image.rgb, image.a); + COLOR = vec4(image_with_background, 1.0); + } +} +" +shader_accumulate = "uniform sampler2D sum; +uniform bool clear = false; +uniform vec2 preview_2d_size = vec2(100.0); +uniform float preview_2d_scale = 1.2; +uniform vec2 preview_2d_center = vec2(0.5); + +void fragment() { + vec2 ratio = preview_2d_size; + vec2 jitter_uv = UV+0.75*fract(sin(vec2(TIME, TIME*0.5)*43758.5453123))/preview_2d_size; + vec2 uv = preview_2d_center+(jitter_uv-0.5)*preview_2d_scale*ratio/min(ratio.x, ratio.y); + if (clear) { + COLOR = preview_2d(uv); + } else { + COLOR = textureLod(sum, UV, 0.0)+preview_2d(uv); + } +} +" +shader_divide = "shader_type canvas_item; + +uniform sampler2D sum; +uniform int divide = 1; + +void fragment() { + COLOR = vec4(texture(sum, UV).rgb/float(divide), 1.0); +}" + +[node name="ContextMenu" parent="." index="15"] +items = [ "Reset view", null, 0, false, false, 0, 0, null, "", false ] + +[node name="View" parent="ContextMenu" index="0"] +items = [ "Extend", null, 2, true, false, 0, 0, null, "", false, "Repeat", null, 2, false, false, 1, 0, null, "", false, "Clamp", null, 2, false, false, 2, 0, null, "", false, "Temporal AA", null, 2, false, false, 3, 0, null, "", false ] + +[node name="Iteration" parent="Accumulate" index="0"] +material = SubResource( 5 ) diff --git a/material_maker/windows/sdf_builder/preview_3d.gd b/material_maker/windows/sdf_builder/preview_3d.gd index 5305ef9c..71cad32c 100644 --- a/material_maker/windows/sdf_builder/preview_3d.gd +++ b/material_maker/windows/sdf_builder/preview_3d.gd @@ -104,7 +104,6 @@ func on_float_parameters_changed(parameter_changes : Dictionary) -> bool: break return return_value - func _on_Preview3D_resized(): if viewport != null: viewport.size = rect_size diff --git a/material_maker/windows/sdf_builder/preview_3d.tscn b/material_maker/windows/sdf_builder/preview_3d.tscn index 0fd7a656..b2073841 100644 --- a/material_maker/windows/sdf_builder/preview_3d.tscn +++ b/material_maker/windows/sdf_builder/preview_3d.tscn @@ -103,8 +103,12 @@ mouse_filter = 1 size_flags_horizontal = 3 size_flags_vertical = 3 script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} [node name="Viewport" type="Viewport" parent="."] +size = Vector2( 1, 1 ) handle_input_locally = false render_target_update_mode = 3 physics_object_picking = true diff --git a/material_maker/windows/sdf_builder/sdf_builder.tscn b/material_maker/windows/sdf_builder/sdf_builder.tscn index 2f1de848..6336818d 100644 --- a/material_maker/windows/sdf_builder/sdf_builder.tscn +++ b/material_maker/windows/sdf_builder/sdf_builder.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=8 format=2] [ext_resource path="res://addons/material_maker/engine/gen_sdf.gd" type="Script" id=1] -[ext_resource path="res://material_maker/panels/preview_2d/preview_2d_panel.tscn" type="PackedScene" id=2] +[ext_resource path="res://material_maker/windows/sdf_builder/preview_2d.tscn" type="PackedScene" id=2] [ext_resource path="res://material_maker/windows/sdf_builder/sdf_builder.gd" type="Script" id=3] [ext_resource path="res://material_maker/windows/sdf_builder/sdf_builder_tree.gd" type="Script" id=4] [ext_resource path="res://material_maker/windows/sdf_builder/preview_3d.tscn" type="PackedScene" id=5]