Skip to content

Commit 4f64bd1

Browse files
Model vertex color example (#94)
* Model vertex color example Adding a 3D model vertex color example - displays exported vertex color attribute from model through a shader. * edit correct title small edit
1 parent f94be91 commit 4f64bd1

File tree

9 files changed

+268
-0
lines changed

9 files changed

+268
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
path_settings {
2+
path: "**"
3+
profile: "Default"
4+
}
5+
profiles {
6+
name: "Default"
7+
platforms {
8+
os: OS_ID_GENERIC
9+
formats {
10+
format: TEXTURE_FORMAT_RGBA
11+
compression_level: BEST
12+
compression_type: COMPRESSION_TYPE_DEFAULT
13+
}
14+
mipmaps: false
15+
max_texture_size: 0
16+
premultiply_alpha: true
17+
}
18+
}
Binary file not shown.

mesh/modelvertexcolor/example.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: Vertex Color (3D model)
3+
tags: model
4+
title: Model Vertex Color
5+
brief: This example demonstrates how to apply a vertex color shader using exported attributes from a 3D model.
6+
author: Agustin R.
7+
scripts: vertexcolor.fp, vertexcolor.fp
8+
---
9+
10+
Vertex color attributes are usually made up as a vector4 of floats represented as rgba(red, green, blue, alpha) channels. They can be applied to 3d models and exported from many 3d editor applications and are commonly used in games for many effects. This example we are displaying a 3d model with vertex color attribute through a shader. No textures or uv's are used to display the colors.
11+
12+
A game object with a model that has a `vertexcolor` material applied to it. The material is assigned custom vertex and fragment shaders. The shader is very simple and just transfers the vertex color data from the model to the vertex and fragment program to display them. The shaders are written in GLSL 1.40, which is available from Defold 1.9.2.
13+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
-- The initial zoom level
2+
go.property("zoom", 1)
3+
-- The speed of the zoom
4+
go.property("zoom_speed", 0.04)
5+
-- The speed of the rotation
6+
go.property("rotation_speed", 0.25)
7+
-- The offset of the camera from the origin
8+
go.property("offset", vmath.vector3(0, 0, 0))
9+
10+
function init(self)
11+
-- Acquire input focus to receive input events
12+
msg.post(".", "acquire_input_focus")
13+
14+
-- Initialize start values
15+
self.yaw = go.get(".", "euler.y")
16+
self.pitch = go.get(".", "euler.x")
17+
self.zoom_offset = -2.5
18+
self.current_yaw = self.yaw
19+
self.current_pitch = self.pitch
20+
self.current_zoom = self.zoom_offset
21+
end
22+
23+
function update(self, dt)
24+
-- Animate camera rotation and zoom
25+
self.current_yaw = vmath.lerp(0.15, self.current_yaw, self.yaw)
26+
self.current_pitch = vmath.lerp(0.15, self.current_pitch, self.pitch)
27+
self.current_zoom = vmath.lerp(0.1, self.current_zoom, self.zoom_offset)
28+
29+
-- Calculate rotation and position
30+
local camera_yaw = vmath.quat_rotation_y(math.rad(self.current_yaw))
31+
local camera_pitch = vmath.quat_rotation_x(math.rad(self.current_pitch))
32+
local camera_rotation = camera_yaw * camera_pitch
33+
local camera_position = self.offset + vmath.rotate(camera_rotation, vmath.vector3(0, -0.5, self.zoom + self.current_zoom))
34+
35+
-- Set camera position and rotation
36+
go.set_position(camera_position)
37+
go.set_rotation(camera_rotation)
38+
end
39+
40+
function on_input(self, action_id, action)
41+
if action_id == hash("touch") and not action.pressed then
42+
self.yaw = self.yaw - action.dx * self.rotation_speed
43+
self.pitch = self.pitch + action.dy * self.rotation_speed
44+
elseif action_id == hash("mouse_wheel_up") then
45+
self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed
46+
elseif action_id == hash("mouse_wheel_down") then
47+
self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed
48+
end
49+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#version 140
2+
3+
// Inputs should match the vertex shader's outputs.
4+
in vec4 vertex_color;
5+
6+
// The final color of the fragment.
7+
out lowp vec4 final_color;
8+
9+
uniform fs_uniforms
10+
{
11+
mediump vec4 tint;
12+
};
13+
14+
void main()
15+
{
16+
// brightening up the displayed vertex colors
17+
lowp float brightness = 0.1;
18+
// Pre-multiply alpha for tint
19+
vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
20+
21+
// Sample the vertex color from vertices, add a little brightness with tint.
22+
vec4 color = vertex_color + brightness * tint_pm ;
23+
24+
// Output the sampled color.
25+
final_color = color;
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: "vertexcolor"
2+
tags: "model"
3+
vertex_program: "/example/vertexcolor.vp"
4+
fragment_program: "/example/vertexcolor.fp"
5+
vertex_space: VERTEX_SPACE_LOCAL
6+
vertex_constants {
7+
name: "mtx_view"
8+
type: CONSTANT_TYPE_VIEW
9+
}
10+
vertex_constants {
11+
name: "mtx_proj"
12+
type: CONSTANT_TYPE_PROJECTION
13+
}
14+
fragment_constants {
15+
name: "tint"
16+
type: CONSTANT_TYPE_USER
17+
value {
18+
x: 1.0
19+
y: 1.0
20+
z: 1.0
21+
w: 1.0
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#version 140
2+
3+
// Models vertex color attribute comes in as rgba floats (vec4)
4+
in vec4 color;
5+
6+
// The model's vertex position.
7+
in vec4 position;
8+
9+
// The model's world matrix.
10+
in mat4 mtx_world;
11+
12+
// The projection and view matrices.
13+
uniform general_vp
14+
{
15+
mat4 mtx_view;
16+
mat4 mtx_proj;
17+
};
18+
19+
// The output of a vertex shader are passed to the fragment shader.
20+
out vec4 vertex_color;
21+
22+
void main()
23+
{
24+
// Setting the vertex colors to the passed varying.
25+
vertex_color = color;
26+
27+
// Transform the vertex position to clip space.
28+
gl_Position = mtx_proj * mtx_view * mtx_world * vec4(position.xyz, 1.0);
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: "unlit"
2+
scale_along_z: 1
3+
embedded_instances {
4+
id: "Defold"
5+
data: "embedded_components {\n"
6+
" id: \"model\"\n"
7+
" type: \"model\"\n"
8+
" data: \"mesh: \\\"/assets/models/Defold_Logo_vertexColor.glb\\\"\\n"
9+
"name: \\\"{{NAME}}\\\"\\n"
10+
"materials {\\n"
11+
" name: \\\"Defold_Material\\\"\\n"
12+
" material: \\\"/example/vertexcolor.material\\\"\\n"
13+
"}\\n"
14+
"\"\n"
15+
"}\n"
16+
""
17+
}
18+
embedded_instances {
19+
id: "camera"
20+
data: "components {\n"
21+
" id: \"orbit_camera\"\n"
22+
" component: \"/example/orbit_camera.script\"\n"
23+
" properties {\n"
24+
" id: \"zoom\"\n"
25+
" value: \"28.0\"\n"
26+
" type: PROPERTY_TYPE_NUMBER\n"
27+
" }\n"
28+
" properties {\n"
29+
" id: \"offset\"\n"
30+
" value: \"0.0, 0.5, 0.0\"\n"
31+
" type: PROPERTY_TYPE_VECTOR3\n"
32+
" }\n"
33+
"}\n"
34+
"embedded_components {\n"
35+
" id: \"camera\"\n"
36+
" type: \"camera\"\n"
37+
" data: \"aspect_ratio: 1.0\\n"
38+
"fov: 0.1\\n"
39+
"near_z: 0.1\\n"
40+
"far_z: 1000.0\\n"
41+
"auto_aspect_ratio: 1\\n"
42+
"\"\n"
43+
"}\n"
44+
""
45+
}

mesh/modelvertexcolor/game.project

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[project]
2+
title = Defold-examples
3+
version = 0.1
4+
5+
[bootstrap]
6+
main_collection = /example/vertexcolors.collectionc
7+
8+
[input]
9+
game_binding = /builtins/input/all.input_bindingc
10+
repeat_interval = 0.05
11+
12+
[display]
13+
width = 720
14+
height = 720
15+
high_dpi = 1
16+
17+
[physics]
18+
scale = 0.02
19+
gravity_y = -500.0
20+
21+
[script]
22+
shared_state = 1
23+
24+
[collection_proxy]
25+
max_count = 256
26+
27+
[label]
28+
subpixels = 1
29+
30+
[sprite]
31+
subpixels = 1
32+
max_count = 32765
33+
34+
[windows]
35+
iap_provider =
36+
37+
[android]
38+
package = com.defold.examples
39+
40+
[ios]
41+
bundle_identifier = com.defold.examples
42+
43+
[osx]
44+
bundle_identifier = com.defold.examples
45+
46+
[html5]
47+
show_fullscreen_button = 0
48+
show_made_with_defold = 0
49+
scale_mode = no_scale
50+
heap_size = 64
51+
52+
[graphics]
53+
texture_profiles = /all.texture_profiles
54+
55+
[collection]
56+
max_instances = 32765
57+
58+
[particle_fx]
59+
max_emitter_count = 1024
60+
61+
[render]
62+
clear_color_blue = 0.33
63+
clear_color_green = 0.29
64+
clear_color_red = 0.29
65+

0 commit comments

Comments
 (0)