Skip to content

Commit 7b0281d

Browse files
authored
Add entry about SEMANTIC_TYPE_TEXTURE_TRANSFORM_2D (#609)
1 parent e947f89 commit 7b0281d

1 file changed

Lines changed: 38 additions & 4 deletions

File tree

docs/en/manuals/material.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Semantic type
6464
- `SEMANTIC_TYPE_TANGENT` Produces per-vertex tangent data for the attribute
6565
- `SEMANTIC_TYPE_WORLD_MATRIX` Produces per-vertex world matrix data for the attribute
6666
- `SEMANTIC_TYPE_NORMAL_MATRIX` Produces per-vertex normal matrix data for the attribute
67+
- `SEMANTIC_TYPE_TEXTURE_TRANSFORM_2D` Produces a per-vertex 3x3 texture transform matrix for the attribute. For particle components, the engine provides a matrix that transforms coordinates into atlas space for the image property on the component. For sprite components, the engine provides a matrix for each image the component is using (when using multi-texturing). For model components, an identity matrix is provided.
6768

6869
Data type
6970
: The data type of the backing data for the attribute.
@@ -119,6 +120,7 @@ The material system will assign a default semantic type automatically based on t
119120
- `tangent` - semantic type: `SEMANTIC_TYPE_TANGENT`
120121
- `mtx_world` - semantic type: `SEMANTIC_TYPE_WORLD_MATRIX`
121122
- `mtx_normal` - semantic type: `SEMANTIC_TYPE_NORMAL_MATRIX`
123+
- `mtx_texture_transform_2d` - semantic type: `SEMANTIC_TYPE_TEXTURE_TRANSFORM_2D`
122124

123125
If you have entries for these attributes in the material, the default semantic type will be overridden with whatever you have configured in the material editor.
124126

@@ -136,10 +138,6 @@ go.animate("#sprite", "tint", go.PLAYBACK_LOOP_PINGPONG, vmath.vector4(1,0,0,1),
136138

137139
There are some caveats to updating the vertex attributes however, whether or not a component can use the value depends on the semantic type of the attribute. For example, a sprite component supports the `SEMANTIC_TYPE_POSITION` so if you update an attribute that has this semantic type, the component will ignore the overridden value since the semantic type dictates that the data should always be produced by the sprites position.
138140

139-
::: sidenote
140-
Setting custom vertex data in runtime is currently only supported for sprite components.
141-
:::
142-
143141
In cases where that a vertex attribute is either a scalar or a vector type other than a `Vec4` you can still set the data using `go.set`:
144142

145143
```lua
@@ -150,6 +148,42 @@ go.animate("#sprite", "sprite_position_2d", go.PLAYBACK_LOOP_PINGPONG, vmath.vec
150148

151149
The same is true for matrix attributes, if the attribute is a matrix type other than a `Mat4` you can still set the data using `go.set`.
152150

151+
### Examples of using custom vertex attributes
152+
153+
Using a texture transform attribute to convert UV coordinates to atlas space:
154+
155+
```glsl
156+
#version 140
157+
158+
in vec3 position;
159+
in vec4 texcoord0;
160+
in mat3 texture_transform_2d;
161+
162+
out vec2 var_texcoord0;
163+
164+
void main()
165+
{
166+
// Extract position from the transform
167+
vec2 atlas_pos = texture_transform_2d[2].xy;
168+
// Extract the scale from the transform
169+
vec2 atlas_size = vec2(
170+
length(texture_transform_2d[0].xy),
171+
length(texture_transform_2d[1].xy)
172+
);
173+
// convert to local UV (0..1)
174+
vec2 localUV = (texcoord0 - atlas_pos) / atlas_size;
175+
176+
// Alternatively, if the UV coordinates already are in the 0..1 range,
177+
// you can transform into atlas space directly by multiplying the transform:
178+
vec2 transformedUv = texture_transform_2d * texcoord0;
179+
180+
// Pass the value into the fragment shader
181+
var_texcoord0 = localUV;
182+
183+
// ... rest of vertex shader
184+
}
185+
```
186+
153187
### Instancing
154188

155189
Instancing is a technique used to efficiently draw multiple copies of the same object in a scene. Instead of creating a separate copy of the object each time it's used, instancing allows the graphics engine to create a single object and then reuse it multiple times. For example, in a game with a large forest, instead of creating a separate tree model for each tree, instancing allows you to create one tree model and then place it hundreds or thousands of times with different positions and scales. The forest can now be rendered with a single draw call instead of individual draw calls for each tree.

0 commit comments

Comments
 (0)