Skip to content

Commit

Permalink
Merge pull request #43 from aglitchman/patch/unlit-example
Browse files Browse the repository at this point in the history
Add "Unlit" material example
  • Loading branch information
britzl authored Oct 9, 2024
2 parents 6ecc66e + fd2f32e commit 4b849c6
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 5 deletions.
29 changes: 29 additions & 0 deletions assets/models/kenney_train-kit/License.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


Train Kit (1.1)

Created/distributed by Kenney (www.kenney.nl)
Creation date: 22-09-2024 23:33
Additional credit(s): Guus Vermeulen, Tony Schaer

------------------------------

License: (Creative Commons Zero, CC0)
http://creativecommons.org/publicdomain/zero/1.0/

You can use this content for personal, educational, and commercial purposes.

Support by crediting 'Kenney' or 'www.kenney.nl' (this is not a requirement)

------------------------------

• Website : www.kenney.nl
• Donate : www.kenney.nl/donate

• Patreon : patreon.com/kenney

Follow on social media for updates:

• Twitter: twitter.com/KenneyNL
• Instagram: instagram.com/kenney_nl
• Mastodon: mastodon.gamedev.place/@kenney
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
6 changes: 6 additions & 0 deletions examples/_main/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,9 @@ embedded_components {
data: "collection: \"/examples/animation/euler_rotation/euler_rotation.collection\"\n"
""
}
embedded_components {
id: "material/unlit"
type: "collectionproxy"
data: "collection: \"/examples/material/unlit/unlit.collection\"\n"
""
}
4 changes: 4 additions & 0 deletions examples/_main/loader.script
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ end
function on_message(self, message_id, message, sender)
if message_id == hash("load_example") then
print("load_example", message.example)
if message.nobg then
msg.post("/background", "disable")
end
self.current_proxy = msg.url(nil, "loader", message.example)
msg.post(self.current_proxy, "load")
self.nomenu = message.nomenu
Expand All @@ -38,6 +41,7 @@ function on_message(self, message_id, message, sender)
elseif message_id == hash("proxy_unloaded") then
msg.post("#gui", "hide")
msg.post("menu#gui", "show")
msg.post("/background", "enable")

elseif message_id == hash("set_time_step") then
msg.post(self.current_proxy, "set_time_step", message)
Expand Down
12 changes: 7 additions & 5 deletions examples/_main/menu.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ local function create_example_nodes(self, category, layout)
self.examples = {}
local c = 1
for t, ex in ipairs(self.index[category]) do
local name = type(ex) == "table" and ex.name or ex
local nobg = type(ex) == "table" and ex.nobg or false
local p = layout[c]
local n = gui.new_text_node(p, ex)
local n = gui.new_text_node(p, name)
gui.set_color(n, vmath.vector4(0.2, 0.2, 0.2, 1.0))
gui.set_font(n, "text48")
gui.set_scale(n, vmath.vector3(0.5, 0.5, 1.0))
local m = gui.get_text_metrics_from_node(n)
local size = vmath.vector3(m.width, m.height, 1)
gui.set_size(n, size)
local example = hash(category .. "/" .. ex)
table.insert(self.examples, { node = n, example = example })
local example = hash(category .. "/" .. name)
table.insert(self.examples, { node = n, example = example, nobg = nobg })
c = c + 1
end
end
Expand Down Expand Up @@ -112,7 +114,7 @@ function init(self)
"get_set_font", "get_set_texture", "get_set_material",
}
self.index["input"] = { "move", "text", "down_duration", "mouse_and_touch" }
self.index["material"] = { "vertexcolor", "uvgradient", "noise" }
self.index["material"] = { "vertexcolor", { name = "unlit", nobg = true }, "uvgradient", "noise" }
self.index["particles"] = { "particlefx", "modifiers", "fire_and_smoke" }
self.index["sound"] = { "music", "fade_in_out", "panning" }
self.index["render"] = { "camera", "screen_to_world" }
Expand Down Expand Up @@ -175,7 +177,7 @@ function on_input(self, action_id, action)

for i, ex in ipairs(self.examples) do
if gui.pick_node(ex.node, action.x, action.y) then
msg.post("/loader#script", "load_example", { example = ex.example })
msg.post("/loader#script", "load_example", { example = ex.example, nobg = ex.nobg })
end
end
end
Expand Down
54 changes: 54 additions & 0 deletions examples/material/unlit/unlit.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: "unlit"
scale_along_z: 1
embedded_instances {
id: "train"
data: "embedded_components {\n"
" id: \"model\"\n"
" type: \"model\"\n"
" data: \"mesh: \\\"/assets/models/kenney_train-kit/train-locomotive-b.glb\\\"\\n"
"name: \\\"{{NAME}}\\\"\\n"
"materials {\\n"
" name: \\\"colormap\\\"\\n"
" material: \\\"/examples/material/unlit/unlit.material\\\"\\n"
" textures {\\n"
" sampler: \\\"texture0\\\"\\n"
" texture: \\\"/assets/models/kenney_train-kit/Textures/colormap.png\\\"\\n"
" }\\n"
"}\\n"
"\"\n"
"}\n"
""
}
embedded_instances {
id: "camera"
data: "embedded_components {\n"
" id: \"camera\"\n"
" type: \"camera\"\n"
" data: \"aspect_ratio: 1.0\\n"
"fov: 0.7854\\n"
"near_z: 0.1\\n"
"far_z: 1000.0\\n"
"auto_aspect_ratio: 1\\n"
"\"\n"
"}\n"
""
position {
x: 3.543836
y: 3.619002
z: 3.104797
}
rotation {
x: -0.26769933
y: 0.3956773
z: 0.12174567
w: 0.8700313
}
}
embedded_instances {
id: "main"
data: "components {\n"
" id: \"unlit\"\n"
" component: \"/examples/material/unlit/unlit.script\"\n"
"}\n"
""
}
19 changes: 19 additions & 0 deletions examples/material/unlit/unlit.fp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 140

// Inputs should match the vertex shader's outputs.
in vec2 var_texcoord0;

// The texture to sample.
uniform lowp sampler2D texture0;

// The final color of the fragment.
out lowp vec4 final_color;

void main()
{
// Sample the texture at the fragment's texture coordinates.
vec4 color = texture(texture0, var_texcoord0.xy);

// Output the sampled color.
final_color = color;
}
25 changes: 25 additions & 0 deletions examples/material/unlit/unlit.material
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: "unlit"
tags: "model"
vertex_program: "/examples/material/unlit/unlit.vp"
fragment_program: "/examples/material/unlit/unlit.fp"
vertex_space: VERTEX_SPACE_LOCAL
vertex_constants {
name: "mtx_world"
type: CONSTANT_TYPE_WORLD
}
vertex_constants {
name: "mtx_view"
type: CONSTANT_TYPE_VIEW
}
vertex_constants {
name: "mtx_proj"
type: CONSTANT_TYPE_PROJECTION
}
samplers {
name: "texture0"
wrap_u: WRAP_MODE_CLAMP_TO_EDGE
wrap_v: WRAP_MODE_CLAMP_TO_EDGE
filter_min: FILTER_MODE_MIN_LINEAR
filter_mag: FILTER_MODE_MAG_LINEAR
max_anisotropy: 0.0
}
13 changes: 13 additions & 0 deletions examples/material/unlit/unlit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Unlit
brief: This example demonstrates how to create and apply an custom non-lit material to a 3D model.
scripts: unlit.script, unlit.vp, unlit.fp
---

In industry-established terms, a material that is not affected by lighting is called "unlit" or "non-lit". It is used to create retro-style graphics or for effects that should not depend on lighting (headlights, lamps).

This example contains a game object with a model that has an `unlit` material applied to it. The material is assigned custom vertex and fragment shaders. To set up a perspective camera, a camera is added to the collection, which is enabled in the script via the `acquire_camera_focus` message when the collection loads.

The unlit shader is very simple and just transfers the texture color to the model. This is an excellent starting point for creating new materials and for creating effects that do not depend on lighting. The shaders are written in GLSL 1.40, which is available from Defold 1.9.2.

The model used in this example is from Kenney's [Train Pack](https://kenney.itch.io/train-pack), licensed under CC0.
4 changes: 4 additions & 0 deletions examples/material/unlit/unlit.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function init(self)
msg.post("/camera", "acquire_camera_focus")
msg.post("@render:", "use_camera_projection")
end
26 changes: 26 additions & 0 deletions examples/material/unlit/unlit.vp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#version 140

// The model's vertex position and texture coordinates.
in vec4 position;
in vec2 texcoord0;

// The projection, view and world matrices.
uniform general_vp
{
mat4 mtx_world;
mat4 mtx_view;
mat4 mtx_proj;
};

// The output of a vertex shader are passed to the fragment shader.
// The texture coordinates of the vertex.
out vec2 var_texcoord0;

void main()
{
// Pass the texture coordinates to the fragment shader.
var_texcoord0 = texcoord0;

// Transform the vertex position to clip space.
gl_Position = mtx_proj * mtx_view * mtx_world * vec4(position.xyz, 1.0);
}

0 comments on commit 4b849c6

Please sign in to comment.