Skip to content

Commit c3d6c02

Browse files
committed
Added textured mesh example
1 parent 48ab1d4 commit c3d6c02

File tree

9 files changed

+296
-0
lines changed

9 files changed

+296
-0
lines changed

mesh/textured/example.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
tags: mesh
3+
title: Textured Mesh
4+
brief: This example shows how to create a textured mesh component in the shape of a rectangle.
5+
author: Defold Foundation
6+
scripts: texturedmesh.fp, texturedmesh.vp
7+
---
8+
9+
This example contains a game object with a mesh component in the shape of a rectangle (quad). The quad is defined in `quad.buffer` as the four points (triangle strip) in the `position` stream. The triangle also defines the texture coordinate (UV) at each point.
10+
11+
```
12+
[
13+
{
14+
"name": "position",
15+
"type": "float32",
16+
"count": 3,
17+
"data": [
18+
-0.5, -0.5, 0,
19+
0.5, -0.5, 0,
20+
-0.5, 0.5, 0,
21+
0.5, 0.5, 0
22+
]
23+
},
24+
{
25+
"name": "texcoord0",
26+
"type": "float32",
27+
"count": 2,
28+
"data": [
29+
0.0, 0.0,
30+
1.0, 0.0,
31+
0.0, 1.0,
32+
1.0, 1.0
33+
]
34+
}
35+
]
36+
```
37+
38+
Texture by [Kenney.nl](https://kenney.nl/assets/prototype-textures)
4.08 KB
Loading
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", 3)
3+
-- The speed of the zoom
4+
go.property("zoom_speed", 0.1)
5+
-- The speed of the rotation
6+
go.property("rotation_speed", 0.5)
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 = 0
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.15, 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, 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

mesh/textured/example/quad.buffer

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[
2+
{
3+
"name": "position",
4+
"type": "float32",
5+
"count": 3,
6+
"data": [
7+
-0.5, -0.5, 0,
8+
0.5, -0.5, 0,
9+
-0.5, 0.5, 0,
10+
0.5, 0.5, 0
11+
]
12+
},
13+
{
14+
"name": "texcoord0",
15+
"type": "float32",
16+
"count": 2,
17+
"data": [
18+
0.0, 0.0,
19+
1.0, 0.0,
20+
0.0, 1.0,
21+
1.0, 1.0
22+
]
23+
}
24+
]
25+
]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: "texturedmesh"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "go"
5+
data: "embedded_components {\n"
6+
" id: \"mesh\"\n"
7+
" type: \"mesh\"\n"
8+
" data: \"material: \\\"/example/texturedmesh.material\\\"\\n"
9+
"vertices: \\\"/example/quad.buffer\\\"\\n"
10+
"textures: \\\"/example/floor_ground_grass.png\\\"\\n"
11+
"primitive_type: PRIMITIVE_TRIANGLE_STRIP\\n"
12+
"position_stream: \\\"position\\\"\\n"
13+
"normal_stream: \\\"position\\\"\\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+
"}\n"
24+
"embedded_components {\n"
25+
" id: \"camera\"\n"
26+
" type: \"camera\"\n"
27+
" data: \"aspect_ratio: 1.0\\n"
28+
"fov: 0.7854\\n"
29+
"near_z: 0.01\\n"
30+
"far_z: 1000.0\\n"
31+
"auto_aspect_ratio: 1\\n"
32+
"\"\n"
33+
"}\n"
34+
""
35+
position {
36+
z: 6.0
37+
}
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#version 140
2+
3+
in highp vec4 var_position;
4+
in mediump vec2 var_texcoord0;
5+
6+
out vec4 out_fragColor;
7+
8+
uniform mediump sampler2D tex0;
9+
10+
uniform fs_uniforms
11+
{
12+
mediump vec4 tint;
13+
};
14+
15+
void main()
16+
{
17+
// Pre-multiply alpha since all runtime textures already are
18+
vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
19+
20+
vec4 color = texture(tex0, var_texcoord0) * tint_pm;
21+
22+
out_fragColor = vec4(color.rgb, 1.0);
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: "mesh"
2+
tags: "model"
3+
vertex_program: "/example/texturedmesh.vp"
4+
fragment_program: "/example/texturedmesh.fp"
5+
vertex_space: VERTEX_SPACE_LOCAL
6+
vertex_constants {
7+
name: "mtx_proj"
8+
type: CONSTANT_TYPE_PROJECTION
9+
}
10+
vertex_constants {
11+
name: "mtx_worldview"
12+
type: CONSTANT_TYPE_WORLDVIEW
13+
}
14+
vertex_constants {
15+
name: "mtx_view"
16+
type: CONSTANT_TYPE_VIEW
17+
}
18+
fragment_constants {
19+
name: "tint"
20+
type: CONSTANT_TYPE_USER
21+
value {
22+
x: 1.0
23+
y: 1.0
24+
z: 1.0
25+
w: 1.0
26+
}
27+
}
28+
samplers {
29+
name: "tex0"
30+
wrap_u: WRAP_MODE_CLAMP_TO_EDGE
31+
wrap_v: WRAP_MODE_CLAMP_TO_EDGE
32+
filter_min: FILTER_MODE_MIN_LINEAR
33+
filter_mag: FILTER_MODE_MAG_LINEAR
34+
max_anisotropy: 0.0
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#version 140
2+
3+
// Positions can be world or local space, since world and normal
4+
// matrices are identity for world vertex space materials.
5+
// If world vertex space is selected, you can remove the
6+
// normal matrix multiplication for optimal performance.
7+
8+
in highp vec4 position;
9+
in mediump vec2 texcoord0;
10+
11+
out highp vec4 var_position;
12+
out mediump vec2 var_texcoord0;
13+
14+
uniform vs_uniforms
15+
{
16+
uniform mediump mat4 mtx_worldview;
17+
uniform mediump mat4 mtx_proj;
18+
uniform mediump mat4 mtx_view;
19+
};
20+
21+
void main()
22+
{
23+
vec4 p = mtx_worldview * vec4(position.xyz, 1.0);
24+
var_position = p;
25+
var_texcoord0 = texcoord0;
26+
gl_Position = mtx_proj * p;
27+
}

mesh/textured/game.project

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

0 commit comments

Comments
 (0)