Skip to content

Commit 88bfb41

Browse files
authored
Add entity picking example (#90)
1 parent 6fe1610 commit 88bfb41

File tree

18 files changed

+678
-0
lines changed

18 files changed

+678
-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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#version 140
2+
3+
// Inputs should match the vertex shader's outputs.
4+
in vec2 var_texcoord0;
5+
6+
// The texture to sample.
7+
uniform lowp sampler2D texture0;
8+
9+
// The final color of the fragment.
10+
out lowp vec4 final_color;
11+
12+
uniform fs_uniforms
13+
{
14+
mediump vec4 tint;
15+
};
16+
17+
void main()
18+
{
19+
// Pre-multiply alpha since all runtime textures already are
20+
vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
21+
22+
// Sample the texture at the fragment's texture coordinates.
23+
vec4 color = texture(texture0, var_texcoord0.xy) * tint_pm;
24+
25+
// Output the sampled color.
26+
final_color = color;
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: "unlit"
2+
tags: "model"
3+
vertex_program: "/assets/materials/unlit.vp"
4+
fragment_program: "/assets/materials/unlit.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+
}
24+
samplers {
25+
name: "texture0"
26+
wrap_u: WRAP_MODE_CLAMP_TO_EDGE
27+
wrap_v: WRAP_MODE_CLAMP_TO_EDGE
28+
filter_min: FILTER_MODE_MIN_LINEAR
29+
filter_mag: FILTER_MODE_MAG_LINEAR
30+
max_anisotropy: 0.0
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#version 140
2+
3+
// The model's vertex position and texture coordinates.
4+
in vec4 position;
5+
in vec2 texcoord0;
6+
7+
// The model's world matrix.
8+
in mat4 mtx_world;
9+
10+
// The projection and view matrices.
11+
uniform general_vp
12+
{
13+
mat4 mtx_view;
14+
mat4 mtx_proj;
15+
};
16+
17+
// The output of a vertex shader are passed to the fragment shader.
18+
// The texture coordinates of the vertex.
19+
out vec2 var_texcoord0;
20+
21+
void main()
22+
{
23+
// Pass the texture coordinates to the fragment shader.
24+
var_texcoord0 = texcoord0;
25+
26+
// Transform the vertex position to clip space.
27+
gl_Position = mtx_proj * mtx_view * mtx_world * vec4(position.xyz, 1.0);
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
Prototype Kit (1.0)
4+
5+
Created/distributed by Kenney (www.kenney.nl)
6+
Creation date: 28-08-2024 09:59
7+
8+
------------------------------
9+
10+
License: (Creative Commons Zero, CC0)
11+
http://creativecommons.org/publicdomain/zero/1.0/
12+
13+
You can use this content for personal, educational, and commercial purposes.
14+
15+
Support by crediting 'Kenney' or 'www.kenney.nl' (this is not a requirement)
16+
17+
------------------------------
18+
19+
• Website : www.kenney.nl
20+
• Donate : www.kenney.nl/donate
21+
22+
• Patreon : patreon.com/kenney
23+
24+
Follow on social media for updates:
25+
26+
• Twitter: twitter.com/KenneyNL
27+
• Instagram: instagram.com/kenney_nl
28+
• Mastodon: mastodon.gamedev.place/@kenney
Loading
Binary file not shown.
Binary file not shown.
Binary file not shown.

input/entity_picking/example.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
tags: input
3+
title: Entity Picking
4+
brief: This example demonstrates how to pick a game object from the 3D scene.
5+
author: Artsiom Trubchyk
6+
scripts: entity_picking.script
7+
---
8+
9+
This example describes method of selecting a game object from the 3D scene on the click of the mouse using collision-based picking:
10+
11+
* We use [collision object components](https://defold.com/manuals/physics-objects/) to define a pickable shape for each relevant game object. This example uses 3D physics, which is enabled in the `game.project` file.
12+
* When the user clicks the mouse button, we convert screen coordinates to world coordinates and fire a raycast into the 3D world using the `physics.raycast()` function.
13+
* If the ray intersects with a collision object, the corresponding game object is considered "picked".
14+
15+
The models used in this example are from Kenney's [Prototype Kit](https://kenney.nl/assets/prototype-kit), licensed under CC0.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local M = {}
2+
3+
--- Convert a point from 2D screen space to 3D world space. Supports only perspective.
4+
-- @param x number X coordinate on screen.
5+
-- @param y number Y coordinate on screen.
6+
-- @param z number The distance from the camera in world space to create the new point.
7+
-- @param camera_id url The camera URL to get params from.
8+
-- @return vector3 The world coordinate.
9+
function M.screen_to_world(x, y, z, camera_id)
10+
-- Camera properties
11+
local projection = camera.get_projection(camera_id)
12+
assert(projection.m33 == 0.0, "Camera must be in perspective mode")
13+
14+
local cw, ch = window.get_size()
15+
local aspect_ratio = cw / ch
16+
local near_z = camera.get_near_z(camera_id)
17+
local fov = camera.get_fov(camera_id)
18+
local inv_view = vmath.inv(camera.get_view(camera_id))
19+
20+
-- Calculate the screen click as a point on the far plane of the normalized device coordinate 'box' (z=1)
21+
local ndc_x = x / cw * 2 - 1
22+
local ndc_y = y / ch * 2 - 1
23+
24+
-- Calculate perspective projection matrix half size at the near plane
25+
local half_size = vmath.vector4(0, 0, -near_z, 1)
26+
local h = near_z * math.tan(fov / 2)
27+
half_size.x = h * aspect_ratio * ndc_x
28+
half_size.y = h * ndc_y
29+
30+
-- Transform to world space
31+
local point = inv_view * half_size
32+
33+
-- Move to distance z from the camera
34+
local world_coord = vmath.normalize(vmath.vector3(point.x - inv_view.m03, point.y - inv_view.m13, point.z - inv_view.m23))
35+
world_coord.x = world_coord.x * z + inv_view.m03
36+
world_coord.y = world_coord.y * z + inv_view.m13
37+
world_coord.z = world_coord.z * z + inv_view.m23
38+
39+
return world_coord
40+
end
41+
42+
return M

input/entity_picking/example/coin.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
components {
2+
id: "coin"
3+
component: "/example/coin.script"
4+
}
5+
embedded_components {
6+
id: "model"
7+
type: "model"
8+
data: "mesh: \"/assets/models/kenney_prototype-kit/coin.glb\"\n"
9+
"name: \"{{NAME}}\"\n"
10+
"materials {\n"
11+
" name: \"colormap\"\n"
12+
" material: \"/assets/materials/unlit.material\"\n"
13+
" textures {\n"
14+
" sampler: \"texture0\"\n"
15+
" texture: \"/assets/models/kenney_prototype-kit/Textures/colormap.png\"\n"
16+
" }\n"
17+
"}\n"
18+
""
19+
rotation {
20+
y: 0.70710677
21+
w: 0.70710677
22+
}
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function init(self)
2+
local start_angle = go.get(".", "euler.y")
3+
local to_angle = start_angle - 3600
4+
5+
-- Simply animate ...
6+
go.animate(".", "euler.y", go.PLAYBACK_LOOP_FORWARD, to_angle, go.EASING_LINEAR, 8)
7+
end

0 commit comments

Comments
 (0)