Skip to content

Commit 6a90ea0

Browse files
committed
Added Crop node
1 parent ad19fcf commit 6a90ea0

5 files changed

Lines changed: 272 additions & 0 deletions

File tree

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
{
2+
"name": "crop",
3+
"node_position": {
4+
"x": 0,
5+
"y": 0
6+
},
7+
"parameters": {
8+
"edge": 3,
9+
"edge_color": {
10+
"a": 1.0,
11+
"b": 0.0,
12+
"g": 0.0,
13+
"r": 0.0,
14+
"type": "Color"
15+
},
16+
"keep_aspect": false,
17+
"pa": 0.5,
18+
"x1": -0.5,
19+
"x2": 0.5,
20+
"y1": -0.5,
21+
"y2": 0.5
22+
},
23+
"seed_int": 0,
24+
"shader_model": {
25+
"code": "",
26+
"global": "",
27+
"inputs": [
28+
{
29+
"default": "vec4(0.0, 0.0, 0.0, 1.0)",
30+
"function": true,
31+
"label": "",
32+
"longdesc": "Input image to be cropped",
33+
"name": "in",
34+
"shortdesc": "Input image",
35+
"type": "rgba"
36+
}
37+
],
38+
"instance": [
39+
"vec4 $(name)_crop(vec2 uv) {",
40+
"\tvec2 new_uv = uv * vec2( $x2 - $x1, $y2 - $y1) + vec2($x1 + 0.5, $y1 + 0.5);",
41+
"\tfloat mask = float(floor(uv) == vec2(0.0));",
42+
"\tif ($edge == 0) {",
43+
"\t\tvec4 col = $in(new_uv);",
44+
"\t\treturn col * mask;",
45+
"\t} else if ($edge == 1) {",
46+
"\t\tnew_uv = fract(uv) * vec2( $x2 - $x1, $y2 - $y1) + vec2($x1 + 0.5, $y1 + 0.5);",
47+
"\t\treturn $in(new_uv);",
48+
"\t} else if ($edge == 2){",
49+
"\t\treturn $in(new_uv);",
50+
"\t} else {",
51+
"\t\tvec4 col = $in(new_uv);",
52+
"\t\treturn mix($edge_color, col, mask);",
53+
"\t}",
54+
"}",
55+
"",
56+
"vec2 $(name)_sca_fac(vec2 uv) {",
57+
"\tvec2 sca = abs(vec2($x2 - $x1, $y2 - $y1));",
58+
"\treturn sca / max(sca.x, sca.y) * 0.5 + 0.5;",
59+
"}",
60+
"",
61+
"vec4 $(name)_crop2(vec2 uv) {",
62+
"\tuv -= vec2($x1 + 0.5, $y1 + 0.5);",
63+
"\tvec2 new_uv = mod(uv, vec2( $x2 - $x1, $y2 - $y1) ) + vec2($x1 + 0.5, $y1 + 0.5);",
64+
"\treturn $in(new_uv);",
65+
"}",
66+
"",
67+
"vec4 $(name)_crop_preview(vec2 uv) {",
68+
"\tfloat mask = uv.x - 0.5 > $x1 && uv.x - 0.5 < $x2 && uv.y - 0.5 > $y1 && uv.y - 0.5 < $y2 ? 1.0 : $pa;",
69+
"\treturn $in(uv) * mask;",
70+
"}",
71+
"",
72+
"float $(name)_fade_mask(vec2 uv, float fade_width) {",
73+
"\tuv -= vec2($x1 + 0.5, $y1 + 0.5);",
74+
"\tfloat vertical_mask = clamp(uv.x / fade_width + 0.5, 0.0, 1.0);",
75+
"\tuv -= vec2( $x2 - $x1, $y2 - $y1);",
76+
"\tvertical_mask *= clamp(-uv.x / fade_width + 0.5, 0.0, 1.0);",
77+
"\treturn vertical_mask;",
78+
"} "
79+
],
80+
"longdesc": "Crops an image",
81+
"name": "Crop",
82+
"outputs": [
83+
{
84+
"longdesc": "Image cropping preview",
85+
"rgba": "$(name)_crop_preview($uv)",
86+
"shortdesc": "Preview",
87+
"type": "rgba"
88+
},
89+
{
90+
"longdesc": "Result of the crop operation",
91+
"rgba": "$keep_aspect ? $(name)_crop(($uv - 0.5) / (2.0*$(name)_sca_fac($uv)-1.0) + 0.5) : $(name)_crop($uv)",
92+
"shortdesc": "Cropped image",
93+
"type": "rgba"
94+
}
95+
],
96+
"parameters": [
97+
{
98+
"default": 3.0,
99+
"label": "Edge",
100+
"longdesc": "Specifies filling mode beyond the crop area.",
101+
"name": "edge",
102+
"shortdesc": "Edge Mode",
103+
"type": "enum",
104+
"values": [
105+
{
106+
"name": "Alpha",
107+
"value": "0"
108+
},
109+
{
110+
"name": "Repeat",
111+
"value": "1"
112+
},
113+
{
114+
"name": "Continous",
115+
"value": "2"
116+
},
117+
{
118+
"name": "Color",
119+
"value": "3"
120+
}
121+
]
122+
},
123+
{
124+
"default": false,
125+
"label": "Keep Aspect",
126+
"longdesc": "When set, cropped output is scaled to fit the unit square, centered with aspect ratio(based on the crop area) kept.",
127+
"name": "keep_aspect",
128+
"shortdesc": "Keep Aspect",
129+
"type": "boolean"
130+
},
131+
{
132+
"control": "P1.x",
133+
"default": -0.5,
134+
"label": "Corner 1",
135+
"longdesc": "Position for the first crop handle on the X axis.",
136+
"max": 0.5,
137+
"min": -0.5,
138+
"name": "x1",
139+
"shortdesc": "First Corner X",
140+
"step": 0.01,
141+
"type": "float"
142+
},
143+
{
144+
"control": "P1.y",
145+
"default": -0.5,
146+
"label": "3:",
147+
"longdesc": "Position for the first crop handle on the Y axis.",
148+
"max": 0.5,
149+
"min": -0.5,
150+
"name": "y1",
151+
"shortdesc": "First Corner Y",
152+
"step": 0.01,
153+
"type": "float"
154+
},
155+
{
156+
"control": "P2.x",
157+
"default": 0.5,
158+
"label": "Corner 2",
159+
"longdesc": "Position for the second crop handle on the X axis",
160+
"max": 0.5,
161+
"min": -0.5,
162+
"name": "x2",
163+
"shortdesc": "Second Corner X",
164+
"step": 0.01,
165+
"type": "float"
166+
},
167+
{
168+
"control": "P2.y",
169+
"default": 0.5,
170+
"label": "4:",
171+
"longdesc": "Position for the second crop handle on the Y axis.",
172+
"max": 0.5,
173+
"min": -0.5,
174+
"name": "y2",
175+
"shortdesc": "Second Corner Y",
176+
"step": 0.01,
177+
"type": "float"
178+
},
179+
{
180+
"control": "None",
181+
"default": 0.5,
182+
"label": "Preview",
183+
"longdesc": "Background opacity in preview output.",
184+
"max": 1.0,
185+
"min": 0.0,
186+
"name": "pa",
187+
"shortdesc": "Preview Alpha",
188+
"step": 0.01,
189+
"type": "float"
190+
},
191+
{
192+
"default": {
193+
"a": 1.0,
194+
"b": 0.0,
195+
"g": 0.0,
196+
"r": 0.0
197+
},
198+
"label": "1:",
199+
"longdesc": "Custom color to use beyond the cropped content. Only effective if 'Edge' is set to 'Color'.",
200+
"name": "edge_color",
201+
"shortdesc": "Edge Color",
202+
"type": "color"
203+
}
204+
],
205+
"shortdesc": "Crop"
206+
},
207+
"type": "shader"
208+
}
15.6 KB
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Crop node
2+
~~~~~~~~~
3+
4+
The **Crop** node crop input images.
5+
6+
.. image:: images/node_transform_crop.png
7+
:align: center
8+
9+
Inputs
10+
++++++
11+
12+
The **Crop** node accepts a RGBA input.
13+
14+
Outputs
15+
+++++++
16+
17+
The **Crop** node outputs a preview that is useful when adjusting the crop area,
18+
and a second output which shows the resulting crop.
19+
20+
Parameters
21+
++++++++++
22+
23+
The **Crop** node has the following parameters:
24+
25+
* *Edge Mode* which specifies the behavior of contents shown beyond the
26+
crop area, that can be the following: *Alpha*, *Repeat*, *Continous* and *Color*.
27+
28+
* *Edge Color* specifies the color to use for contents beyond the crop area. This
29+
option is only effective if *Edge* is set to *Color*
30+
31+
* *Keep Aspect* specifies whether the cropped content will be centered and scaled
32+
to the unit square with aspect ratio (based on the crop size) kept. Otherwise
33+
the cropped content is stretched to fill the unit square.
34+
35+
* *Corner 1* specifies the position of the first crop handle alogn the X and Y axis.
36+
37+
* *Corner 2* specifies the position of the second crop handle alogn the X and Y axis.
38+
39+
* *Preview Alpha* adjusts the opacity of the background beyodn the crop area. This is
40+
only visible in the preview output for ease of adjusting the crop.

material_maker/doc/nodes_transform.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ The transform nodes are nodes that affect the geometry of their input.
2929
node_transform_spherize
3030
node_transform_inverse_circle_map
3131
node_transform_directional_warp
32+
node_transform_crop

material_maker/library/base.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8261,6 +8261,29 @@
82618261
},
82628262
"tree_item": "3D/Texture/Warp",
82638263
"type": "tex3d_warp"
8264+
},
8265+
{
8266+
"display_name": "Crop",
8267+
"icon_data": "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAQKADAAQAAAABAAAAQAAAAABGUUKwAAAAUVBMVEUAAAD///////////////////////////////////////////////////////////////////////////////////////////////////////8IN+deAAAAGnRSTlMAAwQICQoLEhMXGUd5epGns7W/z9Hf9Pb4/s4VnowAAACfSURBVHja7dGFEQRBCETRXnd38g/0Sla7zilfXgB/BgDzI6hk45JDoxWZHSh0IhJawAK3DGRtdzIdgbJ7okpB/FHIFqjlmQokXn4LdGDFrAzADU/6I+CFLOHAr1cILWABC1jgjgELJCELjkAfnrjPAs8MW4DMhTIgSwxS/RYYfZC06p5ojsDUnbQZnlJc4RYBC1jAnUVaaOTLmEEl8kEem4tAOMFllkEAAAAASUVORK5CYII=",
8268+
"name": "crop",
8269+
"type": "crop",
8270+
"parameters": {
8271+
"edge": 3.0,
8272+
"edge_color": {
8273+
"a": 1.0,
8274+
"b": 0.0,
8275+
"g": 0.0,
8276+
"r": 0.0,
8277+
"type": "Color"
8278+
},
8279+
"keep_aspect": false,
8280+
"pa": 0.5,
8281+
"x1": -0.5,
8282+
"x2": 0.5,
8283+
"y1": -0.5,
8284+
"y2": 0.5
8285+
},
8286+
"tree_item": "Transform/Crop"
82648287
}
82658288
],
82668289
"name": "Base library"

0 commit comments

Comments
 (0)