From 0a4e84ec069563b6cba30cba5d83ac5ba912a2af Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Mon, 29 Oct 2018 22:22:35 +0100 Subject: [PATCH] Implemented color, perlin and shape as generic nodes. --- addons/material_maker/common.shader | 69 ------------------- .../material_maker/examples/mandelbrot.ptex | 2 +- addons/material_maker/nodes/color.mmn | 1 + addons/material_maker/nodes/noise.mmn | 28 +------- addons/material_maker/nodes/perlin.mmn | 1 + .../material_maker/nodes/perlin/perlin.tscn | 3 +- addons/material_maker/nodes/shape.mmn | 1 + 7 files changed, 6 insertions(+), 99 deletions(-) create mode 100644 addons/material_maker/nodes/color.mmn create mode 100644 addons/material_maker/nodes/perlin.mmn create mode 100644 addons/material_maker/nodes/shape.mmn diff --git a/addons/material_maker/common.shader b/addons/material_maker/common.shader index 7a9927a..c861af8 100644 --- a/addons/material_maker/common.shader +++ b/addons/material_maker/common.shader @@ -13,45 +13,6 @@ vec3 rand3(vec2 x) { dot(x, vec2(13.254, 5.867)))) * 43758.5453); } -float circle(vec2 uv, float sides, float size, float edge) { - uv = 2.0*uv-1.0; - edge = max(edge, 1.0e-8); - float distance = length(uv); - return clamp((1.0-distance/size)/edge, 0.0, 1.0); -} - -float polygon(vec2 uv, float sides, float size, float edge) { - uv = 2.0*uv-1.0; - edge = max(edge, 1.0e-8); - float angle = atan(uv.x, uv.y)+3.14159265359; - float slice = 6.28318530718/sides; - return clamp((size-cos(floor(0.5+angle/slice)*slice-angle)*length(uv))/(edge*size), 0.0, 1.0); -} - -float star(vec2 uv, float sides, float size, float edge) { - uv = 2.0*uv-1.0; - edge = max(edge, 1.0e-8); - float angle = atan(uv.x, uv.y); - float slice = 6.28318530718/sides; - return clamp((size-cos(floor(1.5+angle/slice-2.0*step(0.5*slice, mod(angle, slice)))*slice-angle)*length(uv))/(edge*size), 0.0, 1.0); -} - -float curved_star(vec2 uv, float sides, float size, float edge) { - uv = 2.0*uv-1.0; - edge = max(edge, 1.0e-8); - float angle = 2.0*(atan(uv.x, uv.y)+3.14159265359); - float slice = 6.28318530718/sides; - return clamp((size-cos(floor(0.5+0.5*angle/slice)*2.0*slice-angle)*length(uv))/(edge*size), 0.0, 1.0); -} - -float rays(vec2 uv, float sides, float size, float edge) { - uv = 2.0*uv-1.0; - edge = 0.5*max(edge, 1.0e-8)*size; - float slice = 6.28318530718/sides; - float angle = mod(atan(uv.x, uv.y)+3.14159265359, slice)/slice; - return clamp(min((size-angle)/edge, angle/edge), 0.0, 1.0); -} - float wave_constant(float x) { return 1.0; } @@ -184,36 +145,6 @@ vec2 transform_norepeat(vec2 uv, vec2 translate, float rotate, vec2 scale) { return clamp(transform(uv, translate, rotate, scale), vec2(0.0), vec2(1.0)); } -float dots(vec2 uv, float size, float density, int seed) { - vec2 seed2 = rand2(vec2(float(seed), 1.0-float(seed))); - uv /= size; - vec2 point_pos = floor(uv)+vec2(0.5); - float color = step(rand(seed2+point_pos), density); - return color; -} - -float perlin(vec2 uv, vec2 size, int iterations, float persistence, int seed) { - vec2 seed2 = rand2(vec2(float(seed), 1.0-float(seed))); - float rv = 0.0; - float coef = 1.0; - float acc = 0.0; - for (int i = 0; i < iterations; ++i) { - vec2 step = vec2(1.0)/size; - vec2 xy = floor(uv*size); - float f0 = rand(seed2+mod(xy, size)); - float f1 = rand(seed2+mod(xy+vec2(1.0, 0.0), size)); - float f2 = rand(seed2+mod(xy+vec2(0.0, 1.0), size)); - float f3 = rand(seed2+mod(xy+vec2(1.0, 1.0), size)); - vec2 mixval = smoothstep(0.0, 1.0, fract(uv*size)); - rv += coef * mix(mix(f0, f1, mixval.x), mix(f2, f3, mixval.x), mixval.y); - acc += coef; - size *= 2.0; - coef *= persistence; - } - - return rv / acc; -} - vec4 voronoi(vec2 uv, vec2 size, float intensity, int seed) { vec2 seed2 = rand2(vec2(float(seed), 1.0-float(seed))); uv *= size; diff --git a/addons/material_maker/examples/mandelbrot.ptex b/addons/material_maker/examples/mandelbrot.ptex index 6f96de8..60fc43a 100644 --- a/addons/material_maker/examples/mandelbrot.ptex +++ b/addons/material_maker/examples/mandelbrot.ptex @@ -1 +1 @@ -{"connections":[{"from":"colorize_0","from_port":0,"to":"Material","to_port":0},{"from":"custom_0","from_port":0,"to":"colorize_0","to_port":0}],"nodes":[{"albedo_color":{"a":1,"b":1,"g":1,"r":1,"type":"Color"},"ao_light_affect":1,"depth_scale":1,"emission_energy":1,"metallic":1,"name":"Material","node_position":{"x":200,"y":0},"normal_scale":1,"resolution":1,"roughness":1,"type":"material"},{"gradient":{"points":[{"b":0,"g":0,"pos":0,"r":1},{"b":0,"g":0.96875,"pos":0.2,"r":1},{"b":0.0625,"g":1,"pos":0.4,"r":0},{"b":1,"g":1,"pos":0.618182,"r":0},{"b":0.965909,"g":0,"pos":0.818182,"r":0},{"b":1,"g":0,"pos":1,"r":0.9375}],"type":"Gradient"},"name":"colorize_0","node_position":{"x":1.25,"y":77.75},"type":"colorize"},{"contrast":1,"model_data":{"global":"float mandelbrot(vec2 uv) {\n float di = 1.0;\n vec2 z = vec2(0.0);\n float m2 = 0.0;\n vec2 dz = vec2(0.0);\n for( int i=0; i<200; i++ )\n {\n if (m2>1024.0 ) {\n\t\t\tdi=0.0;\n\t\t\tbreak;\n\t\t}\n\n\t\t// Z' -> 2·Z·Z' + 1\n dz = 2.0*vec2(z.x*dz.x-z.y*dz.y, z.x*dz.y + z.y*dz.x) + vec2(1.0,0.0);\n\t\t\t\n // Z -> Z² + c\t\t\t\n z = vec2( z.x*z.x - z.y*z.y, 2.0*z.x*z.y ) + uv;\n\t\t\t\n m2 = dot(z,z);\n }\n\n // distance\t\n\t// d(c) = |Z|·log|Z|/|Z'|\n\tfloat d = 0.5*sqrt(dot(z,z)/dot(dz,dz))*log(dot(z,z));\n if( di>0.5 ) d=0.0;\n\treturn d;\n}","instance":"float $(name)_xyz(vec2 uv) {\n\treturn mandelbrot(uv);\n}","name":"Mandelbrot","outputs":[{"f":"clamp($(name)_xyz(vec2($(x), $(y))+$(scale)*(2.0*$(uv)-1.0))*$(contrast), 0.0, 1.0)"}],"parameters":[{"label":"Scale","max":3,"min":0,"name":"scale","step":0.0001,"type":"float","widget":"spinbox"},{"label":"X","max":2,"min":-2,"name":"x","step":0.0001,"type":"float","widget":"spinbox"},{"label":"Y","max":2,"min":-2,"name":"y","step":0.0001,"type":"float","widget":"spinbox"},{"label":"Contrast","max":10000,"min":1,"name":"contrast","step":0.0001,"type":"float","widget":"spinbox"}]},"name":"custom_0","node_position":{"x":-211,"y":208},"scale":0,"type":"custom","x":0,"y":0}]} \ No newline at end of file +{"connections":[{"from":"colorize_0","from_port":0,"to":"Material","to_port":0},{"from":"custom_0","from_port":0,"to":"colorize_0","to_port":0}],"nodes":[{"albedo_color":{"a":1,"b":1,"g":1,"r":1,"type":"Color"},"ao_light_affect":1,"depth_scale":1,"emission_energy":1,"metallic":1,"name":"Material","node_position":{"x":200,"y":0},"normal_scale":1,"resolution":1,"roughness":1,"type":"material"},{"gradient":{"points":[{"b":0,"g":0,"pos":0,"r":1},{"b":0,"g":0.96875,"pos":0.2,"r":1},{"b":0.0625,"g":1,"pos":0.4,"r":0},{"b":1,"g":1,"pos":0.618182,"r":0},{"b":0.965909,"g":0,"pos":0.818182,"r":0},{"b":1,"g":0,"pos":1,"r":0.9375}],"type":"Gradient"},"name":"colorize_0","node_position":{"x":1.25,"y":77.75},"type":"colorize"},{"contrast":10,"model_data":{"global":"float mandelbrot(vec2 uv) {\n float di = 1.0;\n vec2 z = vec2(0.0);\n float m2 = 0.0;\n vec2 dz = vec2(0.0);\n for( int i=0; i<200; i++ )\n {\n if (m2>1024.0 ) {\n\t\t\tdi=0.0;\n\t\t\tbreak;\n\t\t}\n\n\t\t// Z' -> 2·Z·Z' + 1\n dz = 2.0*vec2(z.x*dz.x-z.y*dz.y, z.x*dz.y + z.y*dz.x) + vec2(1.0,0.0);\n\t\t\t\n // Z -> Z² + c\t\t\t\n z = vec2( z.x*z.x - z.y*z.y, 2.0*z.x*z.y ) + uv;\n\t\t\t\n m2 = dot(z,z);\n }\n\n // distance\t\n\t// d(c) = |Z|·log|Z|/|Z'|\n\tfloat d = 0.5*sqrt(dot(z,z)/dot(dz,dz))*log(dot(z,z));\n if( di>0.5 ) d=0.0;\n\treturn d;\n}","instance":"float $(name)_xyz(vec2 uv) {\n\treturn mandelbrot(uv);\n}","name":"Mandelbrot","outputs":[{"f":"clamp($(name)_xyz(vec2($(x), $(y))+$(scale)*(2.0*$(uv)-1.0))*$(contrast), 0.0, 1.0)"}],"parameters":[{"label":"Scale","max":3,"min":0,"name":"scale","step":0.0001,"type":"float","widget":"spinbox"},{"label":"X","max":2,"min":-2,"name":"x","step":0.0001,"type":"float","widget":"spinbox"},{"label":"Y","max":2,"min":-2,"name":"y","step":0.0001,"type":"float","widget":"spinbox"},{"label":"Contrast","max":10000,"min":1,"name":"contrast","step":0.0001,"type":"float","widget":"spinbox"}]},"name":"custom_0","node_position":{"x":-211,"y":208},"scale":1,"type":"custom","x":0,"y":0}]} \ No newline at end of file diff --git a/addons/material_maker/nodes/color.mmn b/addons/material_maker/nodes/color.mmn new file mode 100644 index 0000000..2a652e3 --- /dev/null +++ b/addons/material_maker/nodes/color.mmn @@ -0,0 +1 @@ +{"global":"","instance":"","name":"Uniform","outputs":[{"rgba":"$(color)"}],"parameters":[{"default":{"a":1,"b":1,"g":1,"r":1},"label":"","name":"color","type":"color"}]} \ No newline at end of file diff --git a/addons/material_maker/nodes/noise.mmn b/addons/material_maker/nodes/noise.mmn index cc216ba..85b1136 100644 --- a/addons/material_maker/nodes/noise.mmn +++ b/addons/material_maker/nodes/noise.mmn @@ -1,27 +1 @@ -{ - "name":"Noise", - "parameters":[ - { "name":"size", "label":"Grid Size:", "type":"size", "first":2, "last":12, "default":8 }, - { "name":"density", "label":"Density:", "type":"float", "min":0.0, "max":1.0 } - ], - "global":" - float rand(vec2 x) { - return fract(cos(dot(x, vec2(13.9898, 8.141))) * 43758.5453); - } - vec2 rand2(vec2 x) { - return fract(cos(vec2(dot(x, vec2(13.9898, 8.141)), - dot(x, vec2(3.4562, 17.398)))) * 43758.5453); - } - vec3 rand3(vec2 x) { - return fract(cos(vec3(dot(x, vec2(13.9898, 8.141)), - dot(x, vec2(3.4562, 17.398)), - dot(x, vec2(13.254, 5.867)))) * 43758.5453); - }", - "instance":" - float $(name)_f(vec2 uv) { - return dots(uv, 1.0/$(size), $(density), $(seed)); - }", - "outputs":[ - { "f":"$(name)_f($(uv))" } - ] -} \ No newline at end of file +{"global":"float dots(vec2 uv, float size, float density, int seed) {\n\tvec2 seed2 = rand2(vec2(float(seed), 1.0-float(seed)));\n\tuv /= size;\n\tvec2 point_pos = floor(uv)+vec2(0.5);\n\tfloat color = step(rand(seed2+point_pos), density);\n return color;\n}","instance":"\n\t\tfloat $(name)_f(vec2 uv) {\n\t\t\treturn dots(uv, 1.0/$(size), $(density), $(seed));\n\t\t}","name":"Noise","outputs":[{"f":"$(name)_f($(uv))"}],"parameters":[{"default":8,"first":2,"label":"Grid Size:","last":8,"name":"size","type":"size"},{"default":0.5,"label":"Density:","max":1,"min":0,"name":"density","step":0,"type":"float"}]} \ No newline at end of file diff --git a/addons/material_maker/nodes/perlin.mmn b/addons/material_maker/nodes/perlin.mmn new file mode 100644 index 0000000..c26eaee --- /dev/null +++ b/addons/material_maker/nodes/perlin.mmn @@ -0,0 +1 @@ +{"global":"float perlin(vec2 uv, vec2 size, int iterations, float persistence, int seed) {\n\tvec2 seed2 = rand2(vec2(float(seed), 1.0-float(seed)));\n float rv = 0.0;\n float coef = 1.0;\n float acc = 0.0;\n for (int i = 0; i < iterations; ++i) {\n \tvec2 step = vec2(1.0)/size;\n\t\tvec2 xy = floor(uv*size);\n float f0 = rand(seed2+mod(xy, size));\n float f1 = rand(seed2+mod(xy+vec2(1.0, 0.0), size));\n float f2 = rand(seed2+mod(xy+vec2(0.0, 1.0), size));\n float f3 = rand(seed2+mod(xy+vec2(1.0, 1.0), size));\n vec2 mixval = smoothstep(0.0, 1.0, fract(uv*size));\n rv += coef * mix(mix(f0, f1, mixval.x), mix(f2, f3, mixval.x), mixval.y);\n acc += coef;\n size *= 2.0;\n coef *= persistence;\n }\n \n return rv / acc;\n}\n","instance":"","name":"Perlin","outputs":[{"f":"perlin($(uv), vec2($(scale_x), $(scale_y)), int($(iterations)), $(persistence), $(seed))"}],"parameters":[{"default":4,"label":"Scale X","max":32,"min":1,"name":"scale_x","step":1,"type":"float","widget":"spinbox"},{"default":4,"label":"Scale Y","max":32,"min":1,"name":"scale_y","step":1,"type":"float","widget":"spinbox"},{"default":3,"label":"Iterations","max":10,"min":1,"name":"iterations","step":1,"type":"float","widget":"spinbox"},{"default":0.5,"label":"Persistance","max":1,"min":0,"name":"persistence","step":0.05,"type":"float","widget":"spinbox"}]} \ No newline at end of file diff --git a/addons/material_maker/nodes/perlin/perlin.tscn b/addons/material_maker/nodes/perlin/perlin.tscn index 0cea9b2..b182271 100644 --- a/addons/material_maker/nodes/perlin/perlin.tscn +++ b/addons/material_maker/nodes/perlin/perlin.tscn @@ -2,7 +2,6 @@ [ext_resource path="res://addons/material_maker/nodes/perlin/perlin.gd" type="Script" id=1] - [sub_resource type="Theme" id=1] @@ -35,7 +34,7 @@ slot/0/left_type = 0 slot/0/left_color = Color( 0.5, 0.5, 1, 1 ) slot/0/right_enabled = true slot/0/right_type = 0 -slot/0/right_color = Color( 0.756863, 0.756863, 0.756863, 1 ) +slot/0/right_color = Color( 0.5, 0.5, 1, 1 ) script = ExtResource( 1 ) _sections_unfolded = [ "Theme", "slot", "slot/0" ] diff --git a/addons/material_maker/nodes/shape.mmn b/addons/material_maker/nodes/shape.mmn new file mode 100644 index 0000000..c6fb9bc --- /dev/null +++ b/addons/material_maker/nodes/shape.mmn @@ -0,0 +1 @@ +{"global":"float shape_circle(vec2 uv, float sides, float size, float edge) {\n uv = 2.0*uv-1.0;\n\tedge = max(edge, 1.0e-8);\n float distance = length(uv);\n return clamp((1.0-distance/size)/edge, 0.0, 1.0);\n}\n\nfloat shape_polygon(vec2 uv, float sides, float size, float edge) {\n uv = 2.0*uv-1.0;\n\tedge = max(edge, 1.0e-8);\n float angle = atan(uv.x, uv.y)+3.14159265359;\n float slice = 6.28318530718/sides;\n return clamp((size-cos(floor(0.5+angle/slice)*slice-angle)*length(uv))/(edge*size), 0.0, 1.0);\n}\n\nfloat shape_star(vec2 uv, float sides, float size, float edge) {\n uv = 2.0*uv-1.0;\n\tedge = max(edge, 1.0e-8);\n float angle = atan(uv.x, uv.y);\n float slice = 6.28318530718/sides;\n return clamp((size-cos(floor(1.5+angle/slice-2.0*step(0.5*slice, mod(angle, slice)))*slice-angle)*length(uv))/(edge*size), 0.0, 1.0);\n}\n\nfloat shape_curved_star(vec2 uv, float sides, float size, float edge) {\n uv = 2.0*uv-1.0;\n\tedge = max(edge, 1.0e-8);\n float angle = 2.0*(atan(uv.x, uv.y)+3.14159265359);\n float slice = 6.28318530718/sides;\n return clamp((size-cos(floor(0.5+0.5*angle/slice)*2.0*slice-angle)*length(uv))/(edge*size), 0.0, 1.0);\n}\n\nfloat shape_rays(vec2 uv, float sides, float size, float edge) {\n uv = 2.0*uv-1.0;\n\tedge = 0.5*max(edge, 1.0e-8)*size;\n\tfloat slice = 6.28318530718/sides;\n float angle = mod(atan(uv.x, uv.y)+3.14159265359, slice)/slice;\n return clamp(min((size-angle)/edge, angle/edge), 0.0, 1.0);\n}\n\n","instance":"","name":"Shape","outputs":[{"f":"shape_$(shape)($(uv), $(sides), $(radius), $(edge))"}],"parameters":[{"default":0,"label":"","name":"shape","type":"enum","values":[{"name":"Circle","value":"circle"},{"name":"Polygon","value":"polygon"},{"name":"Star","value":"star"},{"name":"Curved star","value":"curved_star"},{"name":"Rays","value":"rays"}]},{"default":3,"label":"","max":32,"min":2,"name":"sides","step":1,"type":"float"},{"default":1,"label":"","max":1,"min":0,"name":"radius","step":0,"type":"float"},{"default":0.2,"label":"","max":1,"min":0,"name":"edge","step":0,"type":"float"}]} \ No newline at end of file