Skip to content

Commit de81acc

Browse files
committed
Updated Node-to-Code docs with working examples
Original examples seem to have been written for an earlier version of Malt and no longer work with the latest version (2b81a03). Each of the five updated examples have been tested with Blender v4.3.2
1 parent e1eda97 commit de81acc

File tree

7 files changed

+137
-37
lines changed

7 files changed

+137
-37
lines changed

docs/From-Nodes-To-Code/From-Nodes-To-Code.md

Lines changed: 137 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# From Nodes To Code
2+
_Last Updated **2025-02-28**_ for Malt release [2b81a03](https://github.com/bnpr/Malt/commit/2b81a03980c41824166b75f39ee5842de2306bcf)
23

34
This tutorial will teach you how to make your own *Malt* shaders.
45

56
*Malt* shaders are written in *GLSL* (OpenGL Shading Language), the shader programming language used by *OpenGL* and *Vulkan*.
67

78
This tutorial assumes you are already familiar with *EEVEE* nodes.
8-
You may be surprised by how much of your knowledge about node based programming can easily be applied to code and how much more powerfull and convenient can be for advanced tasks.
9+
You may be surprised by how much of your knowledge about node based programming can easily be applied to code and how much more powerfull and convenient it can be for advanced tasks.
910

1011
## Before we start
1112

@@ -35,7 +36,7 @@ Now we can make changes in our shader file and as soon as we save it (*Ctrl+S*)
3536
Let's start by looking at some basic *Malt* examples and what would be their *EEVEE* counterparts, so you can see how they compare to each other.
3637
Read the code, but don't worry if you don't really understand it, we will take a more detailed look at it later.
3738

38-
To get a better feel of it, let's test this examples as we go.
39+
To get a better feel of it, let's test these examples as we go.
3940
You could just copy-paste them, but you will get a better grasp by typing them yourself.
4041
It will also serve to get familiar with the extra goodies VSCode provides, like code auto-completion.
4142

@@ -48,12 +49,15 @@ Try to play with the examples, modify them, break them on purpose and take a loo
4849
<div style="width: 50%; float:left">
4950

5051
```glsl
51-
#include "NPR_Pipeline.glsl"
52+
#include "NPR_MeshShader.glsl"
5253
53-
void COMMON_PIXEL_SHADER(Surface S, inout PixelOutput PO)
54+
#ifdef PIXEL_SHADER
55+
void MAIN_PASS_PIXEL_SHADER()
5456
{
5557
5658
}
59+
#endif // PIXEL_SHADER
60+
5761
```
5862

5963
</div>
@@ -74,14 +78,23 @@ void COMMON_PIXEL_SHADER(Surface S, inout PixelOutput PO)
7478
<div style="width: 50%; float:left">
7579

7680
```glsl
77-
#include "NPR_Pipeline.glsl"
81+
#include "NPR_MeshShader.glsl"
82+
83+
uniform vec4 color = vec4(0,1,0,1);
7884
79-
uniform vec3 color = vec3(0,1,0);
85+
#ifdef MAIN_PASS
86+
layout (location = 0) out vec4 OUT_COLOR;
87+
#endif // MAIN_PASS
8088
81-
void COMMON_PIXEL_SHADER(Surface S, inout PixelOutput PO)
89+
#ifdef PIXEL_SHADER
90+
void MAIN_PASS_PIXEL_SHADER()
8291
{
83-
PO.color.rgb = color;
92+
#ifdef MAIN_PASS
93+
OUT_COLOR = color;
94+
#endif // MAIN_PASS
8495
}
96+
#endif // PIXEL_SHADER
97+
8598
```
8699

87100
</div>
@@ -95,8 +108,8 @@ void COMMON_PIXEL_SHADER(Surface S, inout PixelOutput PO)
95108
<div style="clear: both"></div>
96109

97110
*See how a new color property has appeared in your Material panel?*
98-
*Could you change the default green value ```vec3(0,1,0)``` in the code to red?*
99-
*What happens if you set ```vec3(0.5)``` as the default value?*
111+
*Could you change the default green value ```vec4(0,1,0,1)``` in the code to red?*
112+
*What happens if you set ```vec4(0.5)``` as the default value?*
100113

101114
> If you have edited a property in the UI, you can always *Right Click > Reset to Defaul Value*
102115
@@ -105,19 +118,41 @@ void COMMON_PIXEL_SHADER(Surface S, inout PixelOutput PO)
105118
<div style="width: 50%; float:left">
106119

107120
```glsl
108-
#include "NPR_Pipeline.glsl"
121+
#include "NPR_MeshShader.glsl"
122+
#include "Node Utils 2/node_utils_2.glsl"
123+
#include "Node Utils/node_utils.glsl"
124+
125+
#ifdef MAIN_PASS
126+
layout (location = 0) out vec4 OUT_COLOR;
127+
#endif // MAIN_PASS
109128
129+
// set from within blender
130+
OPTIONALLY_BINDLESS uniform sampler2D color_texture;
110131
uniform int uv_channel = 0;
111-
uniform sampler2D color_texture;
132+
uniform bool smooth_interpolation = true;
112133
113-
void COMMON_PIXEL_SHADER(Surface S, inout PixelOutput PO)
134+
#ifdef PIXEL_SHADER
135+
void MAIN_PASS_PIXEL_SHADER()
114136
{
115-
vec2 texture_coordinates = S.uv[uv_channel];
116-
117-
vec4 sampled_color = texture(color_texture, texture_coordinates);
118-
119-
PO.color = sampled_color;
137+
vec2 texture_coordinates;
138+
UV_Map(uv_channel, texture_coordinates);
139+
140+
vec4 sampled_color;
141+
vec2 out_resolution;
142+
Image(
143+
color_texture,
144+
texture_coordinates,
145+
smooth_interpolation,
146+
sampled_color,
147+
out_resolution
148+
);
149+
150+
#ifdef MAIN_PASS
151+
OUT_COLOR = sampled_color;
152+
#endif // MAIN_PASS
153+
120154
}
155+
#endif // PIXEL_SHADER
121156
```
122157

123158
</div>
@@ -138,14 +173,41 @@ void COMMON_PIXEL_SHADER(Surface S, inout PixelOutput PO)
138173
<div style="width: 50%; float:left">
139174

140175
```glsl
141-
#include "NPR_Pipeline.glsl"
176+
#include "NPR_MeshShader.glsl"
177+
#include "Node Utils 2/node_utils_2.glsl"
178+
#include "Node Utils/node_utils.glsl"
142179
143-
uniform vec3 color = vec3(0,1,0);
180+
#ifdef MAIN_PASS
181+
layout (location = 0) out vec4 OUT_COLOR;
182+
#endif // MAIN_PASS
144183
145-
void COMMON_PIXEL_SHADER(Surface S, inout PixelOutput PO)
184+
OPTIONALLY_BINDLESS uniform sampler1D diffuse_gradient;
185+
uniform vec3 base_color;
186+
uniform vec3 color;
187+
188+
#ifdef PIXEL_SHADER
189+
void MAIN_PASS_PIXEL_SHADER()
146190
{
147-
PO.color.rgb = color * get_diffuse();
191+
vec3 base_color_result = Vec3_color_property(base_color);
192+
vec3 color_result = Vec3_color_property(color);
193+
vec3 diffuse_result = NPR_diffuse_shading(
194+
base_color_result,
195+
color_result,
196+
diffuse_gradient,
197+
/*diffuse_offset=*/0,
198+
/*diffuse_full_range=*/false,
199+
/*diffuse_max_contribution=*/false,
200+
/*diffuse_shadows=*/0,
201+
MATERIAL_LIGHT_GROUPS,
202+
POSITION,
203+
NORMAL
204+
);
205+
206+
#ifdef MAIN_PASS
207+
OUT_COLOR = vec4_from_vec3(diffuse_result);
208+
#endif // MAIN_PASS
148209
}
210+
#endif // PIXEL_SHADER
149211
```
150212

151213
</div>
@@ -173,25 +235,62 @@ Let's combine the previous examples!
173235
<div style="width: 50%; float:left">
174236

175237
```glsl
176-
#include "NPR_Pipeline.glsl"
177-
178-
uniform vec3 ambient_color = vec3(0.5,0.5,0.5);
179-
238+
#include "NPR_MeshShader.glsl"
239+
#include "Node Utils 2/node_utils_2.glsl"
240+
#include "Node Utils/node_utils.glsl"
241+
242+
#ifdef MAIN_PASS
243+
layout (location = 0) out vec4 OUT_COLOR;
244+
#endif // MAIN_PASS
245+
246+
OPTIONALLY_BINDLESS uniform sampler1D diffuse_gradient;
247+
OPTIONALLY_BINDLESS uniform sampler2D color_texture;
248+
uniform vec3 base_color;
249+
uniform vec3 color;
180250
uniform int uv_channel;
181-
uniform sampler2D color_texture;
251+
uniform bool smooth_interpolation;
182252
183-
void COMMON_PIXEL_SHADER(Surface S, inout PixelOutput PO)
253+
#ifdef PIXEL_SHADER
254+
void MAIN_PASS_PIXEL_SHADER()
184255
{
185-
vec3 light_color = get_diffuse() + ambient_color;
186-
187-
vec2 texture_coordinates = S.uv[uv_channel];
188-
189-
vec4 surface_color = texture(color_texture, texture_coordinates);
190-
191-
vec3 result = surface_color.rgb * light_color;
192-
193-
PO.color.rgb = result;
256+
vec3 base_color_result = Vec3_color_property(base_color);
257+
vec3 color_result = Vec3_color_property(color);
258+
vec3 diffuse_result = NPR_diffuse_shading(
259+
base_color_result,
260+
color_result,
261+
diffuse_gradient,
262+
/*diffuse_offset=*/0,
263+
/*diffuse_full_range=*/false,
264+
/*diffuse_max_contribution=*/false,
265+
/*diffuse_shadows=*/0,
266+
MATERIAL_LIGHT_GROUPS,
267+
POSITION,
268+
NORMAL
269+
);
270+
271+
vec2 texture_coordinates;
272+
UV_Map(uv_channel, texture_coordinates);
273+
274+
vec4 sampled_color;
275+
vec2 out_resolution;
276+
Image(
277+
color_texture,
278+
texture_coordinates,
279+
smooth_interpolation,
280+
sampled_color,
281+
out_resolution
282+
);
283+
284+
vec4 computed_color = Vec4_multiply(
285+
vec4_from_vec3(diffuse_result),
286+
sampled_color
287+
);
288+
289+
#ifdef MAIN_PASS
290+
OUT_COLOR = computed_color;
291+
#endif // MAIN_PASS
194292
}
293+
#endif // PIXEL_SHADER
195294
```
196295

197296
</div>
@@ -368,3 +467,4 @@ void third_example()
368467
>I'm working on new chapters, meanwhile, [The Book of Shaders](https://thebookofshaders.com/) is a great resource\!
369468
370469

470+
**Fun Fact:** Malt ([2b81a03](https://github.com/bnpr/Malt/commit/2b81a03980c41824166b75f39ee5842de2306bcf)) auto-generates GLSL files from your node graphs and saves them to `/your-blender-project/.malt-autogenerated/[your-material].mesh.glsl`
71.2 KB
Loading
22.8 KB
Loading

docs/From-Nodes-To-Code/image.png

40 KB
Loading
69.4 KB
Loading
19 KB
Loading
43.5 KB
Loading

0 commit comments

Comments
 (0)