Skip to content

Commit 06a5e93

Browse files
authored
Add world-to-screen example (#138)
1 parent 59f5235 commit 06a5e93

File tree

16 files changed

+441
-0
lines changed

16 files changed

+441
-0
lines changed
146 KB
Binary file not shown.
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+
Graveyard Kit (5.0)
4+
5+
Created/distributed by Kenney (www.kenney.nl)
6+
Creation date: 17-10-2025 11:00
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+
• BlueSky: kenney.bsky.social
28+
• Instagram: instagram.com/kenney_nl
111 KB
Binary file not shown.
10.7 KB
Loading
24.5 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
font: "/assets/SourceSansPro-Semibold.ttf"
2+
material: "/builtins/fonts/font.material"
3+
size: 48
4+
outline_alpha: 0.0
5+
outline_width: 0.0

render/world_to_screen/example.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
tags: render
3+
title: World to Screen
4+
brief: This example demonstrates how to convert 3D world coordinates to 2D screen coordinates using camera transformations.
5+
author: Artsiom Trubchyk
6+
scripts: player.script
7+
thumbnail: thumbnail.png
8+
---
9+
10+
This example shows how to convert world positions to screen coordinates for UI positioning. It features:
11+
12+
* A `world_to_screen()` function that transforms 3D world positions to 2D screen coordinates using the camera's view and projection matrices.
13+
* A ghost character that rotates around a crypt in 3D space while floating up and down.
14+
* A player name label in the GUI that follows the character's world position by converting it to screen coordinates.
15+
* Demonstrates practical use of world-to-screen conversion for positioning UI elements relative to 3D objects.

0 commit comments

Comments
 (0)