Skip to content

Commit 9bba52c

Browse files
authored
content_cao: Support texture animation for upright_sprite (#10020)
1 parent e5725df commit 9bba52c

File tree

5 files changed

+64
-14
lines changed

5 files changed

+64
-14
lines changed

doc/lua_api.txt

+9-9
Original file line numberDiff line numberDiff line change
@@ -6077,15 +6077,15 @@ object you are working with still exists.
60776077
* `get_yaw()`: returns number in radians
60786078
* `set_texture_mod(mod)`
60796079
* `get_texture_mod()` returns current texture modifier
6080-
* `set_sprite(p, num_frames, framelength, select_horiz_by_yawpitch)`
6081-
* Select sprite from spritesheet with optional animation and Dungeon Master
6082-
style texture selection based on yaw relative to camera
6083-
* `p`: {x=number, y=number}, the coordinate of the first frame
6084-
(x: column, y: row), default: `{x=0, y=0}`
6085-
* `num_frames`: number, default: `1`
6086-
* `framelength`: number, default: `0.2`
6087-
* `select_horiz_by_yawpitch`: boolean, this was once used for the Dungeon
6088-
Master mob, default: `false`
6080+
* `set_sprite(p, num_frames, framelength, select_x_by_camera)`
6081+
* Specifies and starts a sprite animation
6082+
* Animations iterate along the frame `y` position.
6083+
* `p`: {x=column number, y=row number}, the coordinate of the first frame
6084+
default: `{x=0, y=0}`
6085+
* `num_frames`: Total frames in the texture, default: `1`
6086+
* `framelength`: Time per animated frame in seconds, default: `0.2`
6087+
* `select_x_by_camera`: Only for visual = `sprite`. Changes the frame `x`
6088+
position according to the view direction. default: `false`.
60896089
* `get_entity_name()` (**Deprecated**: Will be removed in a future version)
60906090
* `get_luaentity()`
60916091

games/devtest/mods/testentities/visuals.lua

+14-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ minetest.register_entity("testentities:mesh_unshaded", {
6868

6969
-- Advanced visual tests
7070

71-
-- A test entity for testing animated and yaw-modulated sprites
71+
-- An entity for testing animated and yaw-modulated sprites
7272
minetest.register_entity("testentities:yawsprite", {
7373
initial_properties = {
7474
selectionbox = {-0.3, -0.5, -0.3, 0.3, 0.3, 0.3},
@@ -79,6 +79,18 @@ minetest.register_entity("testentities:yawsprite", {
7979
initial_sprite_basepos = {x=0, y=0},
8080
},
8181
on_activate = function(self, staticdata)
82-
self.object:set_sprite({x=0, y=0}, 1, 0, true)
82+
self.object:set_sprite({x=0, y=0}, 3, 0.5, true)
83+
end,
84+
})
85+
86+
-- An entity for testing animated upright sprites
87+
minetest.register_entity("testentities:upright_animated", {
88+
initial_properties = {
89+
visual = "upright_sprite",
90+
textures = {"testnodes_anim.png"},
91+
spritediv = {x = 1, y = 4},
92+
},
93+
on_activate = function(self)
94+
self.object:set_sprite({x=0, y=0}, 4, 1.0, false)
8395
end,
8496
})

src/client/content_cao.cpp

+25-3
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,7 @@ void GenericCAO::updateTexturePos()
11761176
int row = m_tx_basepos.Y;
11771177
int col = m_tx_basepos.X;
11781178

1179+
// Yawpitch goes rightwards
11791180
if (m_tx_select_horiz_by_yawpitch) {
11801181
if (cam_to_entity.Y > 0.75)
11811182
col += 5;
@@ -1206,6 +1207,27 @@ void GenericCAO::updateTexturePos()
12061207
float tys = m_tx_size.Y;
12071208
setBillboardTextureMatrix(m_spritenode, txs, tys, col, row);
12081209
}
1210+
1211+
else if (m_meshnode) {
1212+
if (m_prop.visual == "upright_sprite") {
1213+
int row = m_tx_basepos.Y;
1214+
int col = m_tx_basepos.X;
1215+
1216+
// Animation goes downwards
1217+
row += m_anim_frame;
1218+
1219+
const auto &tx = m_tx_size;
1220+
v2f t[4] = { // cf. vertices in GenericCAO::addToScene()
1221+
tx * v2f(col+1, row+1),
1222+
tx * v2f(col, row+1),
1223+
tx * v2f(col, row),
1224+
tx * v2f(col+1, row),
1225+
};
1226+
auto mesh = m_meshnode->getMesh();
1227+
setMeshBufferTextureCoords(mesh->getMeshBuffer(0), t, 4);
1228+
setMeshBufferTextureCoords(mesh->getMeshBuffer(1), t, 4);
1229+
}
1230+
}
12091231
}
12101232

12111233
// Do not pass by reference, see header.
@@ -1247,7 +1269,7 @@ void GenericCAO::updateTextures(std::string mod)
12471269
}
12481270
}
12491271

1250-
if (m_animated_meshnode) {
1272+
else if (m_animated_meshnode) {
12511273
if (m_prop.visual == "mesh") {
12521274
for (u32 i = 0; i < m_prop.textures.size() &&
12531275
i < m_animated_meshnode->getMaterialCount(); ++i) {
@@ -1296,8 +1318,8 @@ void GenericCAO::updateTextures(std::string mod)
12961318
}
12971319
}
12981320
}
1299-
if(m_meshnode)
1300-
{
1321+
1322+
else if (m_meshnode) {
13011323
if(m_prop.visual == "cube")
13021324
{
13031325
for (u32 i = 0; i < 6; ++i)

src/client/mesh.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
203203
setMeshBufferColor(mesh->getMeshBuffer(j), color);
204204
}
205205

206+
void setMeshBufferTextureCoords(scene::IMeshBuffer *buf, const v2f *uv, u32 count)
207+
{
208+
const u32 stride = getVertexPitchFromType(buf->getVertexType());
209+
assert(buf->getVertexCount() >= count);
210+
u8 *vertices = (u8 *) buf->getVertices();
211+
for (u32 i = 0; i < count; i++)
212+
((video::S3DVertex*) (vertices + i * stride))->TCoords = uv[i];
213+
}
214+
206215
template <typename F>
207216
static void applyToMesh(scene::IMesh *mesh, const F &fn)
208217
{

src/client/mesh.h

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ void setMeshBufferColor(scene::IMeshBuffer *buf, const video::SColor &color);
5858
*/
5959
void setMeshColor(scene::IMesh *mesh, const video::SColor &color);
6060

61+
62+
/*
63+
Sets texture coords for vertices in the mesh buffer.
64+
`uv[]` must have `count` elements
65+
*/
66+
void setMeshBufferTextureCoords(scene::IMeshBuffer *buf, const v2f *uv, u32 count);
67+
6168
/*
6269
Set a constant color for an animated mesh
6370
*/

0 commit comments

Comments
 (0)