]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
content_cao: Support texture animation for upright_sprite (#10020)
authorsfan5 <sfan5@live.de>
Thu, 30 Jul 2020 15:39:57 +0000 (17:39 +0200)
committerGitHub <noreply@github.com>
Thu, 30 Jul 2020 15:39:57 +0000 (17:39 +0200)
doc/lua_api.txt
games/devtest/mods/testentities/visuals.lua
src/client/content_cao.cpp
src/client/mesh.cpp
src/client/mesh.h

index 2d22dc8998f6bc140acb0ee21a9db8c6df764e57..8ac3ad7f2f40b9271bdc4cc98d93bf310fec5432 100644 (file)
@@ -6077,15 +6077,15 @@ object you are working with still exists.
 * `get_yaw()`: returns number in radians
 * `set_texture_mod(mod)`
 * `get_texture_mod()` returns current texture modifier
-* `set_sprite(p, num_frames, framelength, select_horiz_by_yawpitch)`
-    * Select sprite from spritesheet with optional animation and Dungeon Master
-      style texture selection based on yaw relative to camera
-    * `p`: {x=number, y=number}, the coordinate of the first frame
-      (x: column, y: row), default: `{x=0, y=0}`
-    * `num_frames`: number, default: `1`
-    * `framelength`: number, default: `0.2`
-    * `select_horiz_by_yawpitch`: boolean, this was once used for the Dungeon
-      Master mob, default: `false`
+* `set_sprite(p, num_frames, framelength, select_x_by_camera)`
+    * Specifies and starts a sprite animation
+    * Animations iterate along the frame `y` position.
+    * `p`: {x=column number, y=row number}, the coordinate of the first frame
+      default: `{x=0, y=0}`
+    * `num_frames`: Total frames in the texture, default: `1`
+    * `framelength`: Time per animated frame in seconds, default: `0.2`
+    * `select_x_by_camera`: Only for visual = `sprite`. Changes the frame `x`
+      position according to the view direction. default: `false`.
 * `get_entity_name()` (**Deprecated**: Will be removed in a future version)
 * `get_luaentity()`
 
index 83f361f16aeb0756f96c2946fde0a63b9b609063..8848ba49f6bef09cc29ecdcf38b18bf0c1039bb2 100644 (file)
@@ -68,7 +68,7 @@ minetest.register_entity("testentities:mesh_unshaded", {
 
 -- Advanced visual tests
 
--- A test entity for testing animated and yaw-modulated sprites
+-- An entity for testing animated and yaw-modulated sprites
 minetest.register_entity("testentities:yawsprite", {
        initial_properties = {
                selectionbox = {-0.3, -0.5, -0.3, 0.3, 0.3, 0.3},
@@ -79,6 +79,18 @@ minetest.register_entity("testentities:yawsprite", {
                initial_sprite_basepos = {x=0, y=0},
        },
        on_activate = function(self, staticdata)
-               self.object:set_sprite({x=0, y=0}, 1, 0, true)
+               self.object:set_sprite({x=0, y=0}, 3, 0.5, true)
+       end,
+})
+
+-- An entity for testing animated upright sprites
+minetest.register_entity("testentities:upright_animated", {
+       initial_properties = {
+               visual = "upright_sprite",
+               textures = {"testnodes_anim.png"},
+               spritediv = {x = 1, y = 4},
+       },
+       on_activate = function(self)
+               self.object:set_sprite({x=0, y=0}, 4, 1.0, false)
        end,
 })
index 4f949f6b037f2cd56e3234556e4e2fe6eb7b5253..88688d18c692e8722cd6dbe6037e8e90da1951f4 100644 (file)
@@ -1176,6 +1176,7 @@ void GenericCAO::updateTexturePos()
                int row = m_tx_basepos.Y;
                int col = m_tx_basepos.X;
 
+               // Yawpitch goes rightwards
                if (m_tx_select_horiz_by_yawpitch) {
                        if (cam_to_entity.Y > 0.75)
                                col += 5;
@@ -1206,6 +1207,27 @@ void GenericCAO::updateTexturePos()
                float tys = m_tx_size.Y;
                setBillboardTextureMatrix(m_spritenode, txs, tys, col, row);
        }
+
+       else if (m_meshnode) {
+               if (m_prop.visual == "upright_sprite") {
+                       int row = m_tx_basepos.Y;
+                       int col = m_tx_basepos.X;
+
+                       // Animation goes downwards
+                       row += m_anim_frame;
+
+                       const auto &tx = m_tx_size;
+                       v2f t[4] = { // cf. vertices in GenericCAO::addToScene()
+                               tx * v2f(col+1, row+1),
+                               tx * v2f(col, row+1),
+                               tx * v2f(col, row),
+                               tx * v2f(col+1, row),
+                       };
+                       auto mesh = m_meshnode->getMesh();
+                       setMeshBufferTextureCoords(mesh->getMeshBuffer(0), t, 4);
+                       setMeshBufferTextureCoords(mesh->getMeshBuffer(1), t, 4);
+               }
+       }
 }
 
 // Do not pass by reference, see header.
@@ -1247,7 +1269,7 @@ void GenericCAO::updateTextures(std::string mod)
                }
        }
 
-       if (m_animated_meshnode) {
+       else if (m_animated_meshnode) {
                if (m_prop.visual == "mesh") {
                        for (u32 i = 0; i < m_prop.textures.size() &&
                                        i < m_animated_meshnode->getMaterialCount(); ++i) {
@@ -1296,8 +1318,8 @@ void GenericCAO::updateTextures(std::string mod)
                        }
                }
        }
-       if(m_meshnode)
-       {
+
+       else if (m_meshnode) {
                if(m_prop.visual == "cube")
                {
                        for (u32 i = 0; i < 6; ++i)
index e1ec22068034b20603c8c8c66812d2ee12b2b387..2400a374cb3990a763952842ef1ac64879680a09 100644 (file)
@@ -203,6 +203,15 @@ void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
                setMeshBufferColor(mesh->getMeshBuffer(j), color);
 }
 
+void setMeshBufferTextureCoords(scene::IMeshBuffer *buf, const v2f *uv, u32 count)
+{
+       const u32 stride = getVertexPitchFromType(buf->getVertexType());
+       assert(buf->getVertexCount() >= count);
+       u8 *vertices = (u8 *) buf->getVertices();
+       for (u32 i = 0; i < count; i++)
+               ((video::S3DVertex*) (vertices + i * stride))->TCoords = uv[i];
+}
+
 template <typename F>
 static void applyToMesh(scene::IMesh *mesh, const F &fn)
 {
index 103c61e45fe2f0d3941d71d426a84d515aa4b3ec..dbc091a06fd716ca64987ab5752d9f77d3fd717c 100644 (file)
@@ -58,6 +58,13 @@ void setMeshBufferColor(scene::IMeshBuffer *buf, const video::SColor &color);
 */
 void setMeshColor(scene::IMesh *mesh, const video::SColor &color);
 
+
+/*
+       Sets texture coords for vertices in the mesh buffer.
+       `uv[]` must have `count` elements
+*/
+void setMeshBufferTextureCoords(scene::IMeshBuffer *buf, const v2f *uv, u32 count);
+
 /*
        Set a constant color for an animated mesh
 */