Make shading of CAOs optional (#10033)
authorDanila Shutov <dcbrwn2@gmail.com>
Tue, 16 Jun 2020 19:48:31 +0000 (22:48 +0300)
committerGitHub <noreply@github.com>
Tue, 16 Jun 2020 19:48:31 +0000 (20:48 +0100)
client/shaders/object_shader/opengl_vertex.glsl
doc/lua_api.txt
games/devtest/mods/testentities/visuals.lua
src/client/content_cao.cpp
src/client/mesh.cpp
src/client/shader.cpp
src/client/tile.h
src/object_properties.cpp
src/object_properties.h
src/script/common/c_content.cpp

index 59171145f51c82b10a5221256c6cb78000280ea2..968a07e2225af16a63d0dd9d833b31fb018f7dd2 100644 (file)
@@ -39,11 +39,15 @@ void main(void)
        lightVec = sunPosition - worldPosition;
        eyeVec = -(gl_ModelViewMatrix * gl_Vertex).xyz;
 
+#if (MATERIAL_TYPE == TILE_MATERIAL_PLAIN) || (MATERIAL_TYPE == TILE_MATERIAL_PLAIN_ALPHA)
+       vIDiff = 1.0;
+#else
        // This is intentional comparison with zero without any margin.
        // If normal is not equal to zero exactly, then we assume it's a valid, just not normalized vector
        vIDiff = length(gl_Normal) == 0.0
                ? 1.0
                : directional_ambient(normalize(gl_Normal));
+#endif
 
        gl_FrontColor = gl_BackColor = gl_Color;
 }
index 07e9698e8c1100098bdfe6d85271ac59b4a28a6e..11c5cfb91322d81a32645dd8083378af2706641f 100644 (file)
@@ -6644,6 +6644,9 @@ Player properties need to be saved manually.
 
         damage_texture_modifier = "^[brighten",
         -- Texture modifier to be applied for a short duration when object is hit
+
+        shaded = true,
+        -- Setting this to 'false' disables diffuse lighting of entity
     }
 
 Entity definition
index 314f51657ec465cc5ab2867cd34a6822dc8eec72..83f361f16aeb0756f96c2946fde0a63b9b609063 100644 (file)
@@ -55,6 +55,17 @@ minetest.register_entity("testentities:mesh", {
        },
 })
 
+minetest.register_entity("testentities:mesh_unshaded", {
+       initial_properties = {
+               visual = "mesh",
+               mesh = "testnodes_pyramid.obj",
+               textures = {
+                       "testnodes_mesh_stripes2.png"
+               },
+               shaded = false,
+       },
+})
+
 -- Advanced visual tests
 
 -- A test entity for testing animated and yaw-modulated sprites
@@ -71,4 +82,3 @@ minetest.register_entity("testentities:yawsprite", {
                self.object:set_sprite({x=0, y=0}, 1, 0, true)
        end,
 })
-
index eec4e3de00a643c64df255c214e20ace82ad6713..5352c35f4761c769273c09f9a6587e1bb12deff5 100644 (file)
@@ -577,10 +577,16 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
 
        if (m_enable_shaders) {
                IShaderSource *shader_source = m_client->getShaderSource();
-               u32 shader_id = shader_source->getShader(
-                               "object_shader",
-                               (m_prop.use_texture_alpha) ? TILE_MATERIAL_ALPHA : TILE_MATERIAL_BASIC,
-                               NDT_NORMAL);
+               MaterialType material_type;
+
+               if (m_prop.shaded && m_prop.glow == 0)
+                       material_type = (m_prop.use_texture_alpha) ?
+                               TILE_MATERIAL_ALPHA : TILE_MATERIAL_BASIC;
+               else
+                       material_type = (m_prop.use_texture_alpha) ?
+                               TILE_MATERIAL_PLAIN_ALPHA : TILE_MATERIAL_PLAIN;
+
+               u32 shader_id = shader_source->getShader("object_shader", material_type, NDT_NORMAL);
                m_material_type = shader_source->getShaderInfo(shader_id).material;
        } else {
                m_material_type = (m_prop.use_texture_alpha) ?
@@ -1504,6 +1510,7 @@ bool GenericCAO::visualExpiryRequired(const ObjectProperties &new_) const
        return old.backface_culling != new_.backface_culling ||
                old.is_visible != new_.is_visible ||
                old.mesh != new_.mesh ||
+               old.shaded != new_.shaded ||
                old.use_texture_alpha != new_.use_texture_alpha ||
                old.visual != new_.visual ||
                old.visual_size != new_.visual_size ||
index 68832849e255c9774881be73e866472e869ffe88..e1ec22068034b20603c8c8c66812d2ee12b2b387 100644 (file)
@@ -341,7 +341,7 @@ bool checkMeshNormals(scene::IMesh *mesh)
                // hurting the performance and covering only really weird broken models.
                f32 length = buffer->getNormal(0).getLength();
 
-               if (!std::isfinite(length) || std::fabs(length) < 1e-10f)
+               if (!std::isfinite(length) || length < 1e-10f)
                        return false;
        }
 
index eda415ce6ee59d54622c117a1b15ed73e299aff3..ee6079f7aeaba6423d4d2494e275ed75fd3c49f3 100644 (file)
@@ -537,11 +537,13 @@ ShaderInfo generate_shader(const std::string &name, u8 material_type, u8 drawtyp
                shaderinfo.base_material = video::EMT_SOLID;
                break;
        case TILE_MATERIAL_ALPHA:
+       case TILE_MATERIAL_PLAIN_ALPHA:
        case TILE_MATERIAL_LIQUID_TRANSPARENT:
        case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT:
                shaderinfo.base_material = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
                break;
        case TILE_MATERIAL_BASIC:
+       case TILE_MATERIAL_PLAIN:
        case TILE_MATERIAL_WAVING_LEAVES:
        case TILE_MATERIAL_WAVING_PLANTS:
        case TILE_MATERIAL_WAVING_LIQUID_BASIC:
@@ -644,9 +646,11 @@ ShaderInfo generate_shader(const std::string &name, u8 material_type, u8 drawtyp
                "TILE_MATERIAL_WAVING_LIQUID_BASIC",
                "TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT",
                "TILE_MATERIAL_WAVING_LIQUID_OPAQUE",
+               "TILE_MATERIAL_PLAIN",
+               "TILE_MATERIAL_PLAIN_ALPHA",
        };
 
-       for (int i = 0; i < 10; i++){
+       for (int i = 0; i < 12; i++){
                shaders_header += "#define ";
                shaders_header += materialTypes[i];
                shaders_header += " ";
index 533df676e2f2f5523f1cdd2c823d959088840e4a..52e0a2b2b25482d36e86b1cff8f01d227f5ccb43 100644 (file)
@@ -150,6 +150,8 @@ enum MaterialType{
        TILE_MATERIAL_WAVING_LIQUID_BASIC,
        TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT,
        TILE_MATERIAL_WAVING_LIQUID_OPAQUE,
+       TILE_MATERIAL_PLAIN,
+       TILE_MATERIAL_PLAIN_ALPHA
 };
 
 // Material flags
index 6ff344dcee35faf9e8f41a284122b9c7523196f9..8d51bcbfa1cdf1f4212727475d8c529b16c0526d 100644 (file)
@@ -69,6 +69,7 @@ std::string ObjectProperties::dump()
        os << ", zoom_fov=" << zoom_fov;
        os << ", use_texture_alpha=" << use_texture_alpha;
        os << ", damage_texture_modifier=" << damage_texture_modifier;
+       os << ", shaded=" << shaded;
        return os.str();
 }
 
@@ -116,6 +117,7 @@ void ObjectProperties::serialize(std::ostream &os) const
        writeF32(os, zoom_fov);
        writeU8(os, use_texture_alpha);
        os << serializeString(damage_texture_modifier);
+       writeU8(os, shaded);
 
        // Add stuff only at the bottom.
        // Never remove anything, because we don't want new versions of this
@@ -170,5 +172,9 @@ void ObjectProperties::deSerialize(std::istream &is)
        use_texture_alpha = readU8(is);
        try {
                damage_texture_modifier = deSerializeString(is);
+               u8 tmp = readU8(is);
+               if (is.eof())
+                       throw SerializationError("");
+               shaded = tmp;
        } catch (SerializationError &e) {}
 }
index f7848f5a212b3c59a6a39d36aa36f625efcd3b0f..f010c1dafabc03d527d091423042905600015530 100644 (file)
@@ -61,6 +61,7 @@ struct ObjectProperties
        float eye_height = 1.625f;
        float zoom_fov = 0.0f;
        bool use_texture_alpha = false;
+       bool shaded = true;
 
        ObjectProperties();
        std::string dump();
index 2157d087d562e1e4e80d16fe31c0b95b8add30a2..3dfd7ce61f250531bd9ed81e00e85dd7ae85c75e 100644 (file)
@@ -327,6 +327,7 @@ void read_object_properties(lua_State *L, int index,
 
        getfloatfield(L, -1, "zoom_fov", prop->zoom_fov);
        getboolfield(L, -1, "use_texture_alpha", prop->use_texture_alpha);
+       getboolfield(L, -1, "shaded", prop->shaded);
 
        getstringfield(L, -1, "damage_texture_modifier", prop->damage_texture_modifier);
 }
@@ -411,6 +412,8 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
        lua_setfield(L, -2, "zoom_fov");
        lua_pushboolean(L, prop->use_texture_alpha);
        lua_setfield(L, -2, "use_texture_alpha");
+       lua_pushboolean(L, prop->shaded);
+       lua_setfield(L, -2, "shaded");
        lua_pushlstring(L, prop->damage_texture_modifier.c_str(), prop->damage_texture_modifier.size());
        lua_setfield(L, -2, "damage_texture_modifier");
 }