Path: blob/master/scene/resources/canvas_item_material.h
9903 views
/**************************************************************************/1/* canvas_item_material.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "scene/resources/material.h"3334class CanvasItemMaterial : public Material {35GDCLASS(CanvasItemMaterial, Material);3637public:38enum BlendMode {39BLEND_MODE_MIX,40BLEND_MODE_ADD,41BLEND_MODE_SUB,42BLEND_MODE_MUL,43BLEND_MODE_PREMULT_ALPHA,44BLEND_MODE_DISABLED45};4647enum LightMode {48LIGHT_MODE_NORMAL,49LIGHT_MODE_UNSHADED,50LIGHT_MODE_LIGHT_ONLY51};5253private:54union MaterialKey {55struct {56uint32_t blend_mode : 4;57uint32_t light_mode : 4;58uint32_t particles_animation : 1;59uint32_t invalid_key : 1;60};6162uint32_t key = 0;6364static uint32_t hash(const MaterialKey &p_key) {65return hash_murmur3_one_32(p_key.key);66}67bool operator==(const MaterialKey &p_key) const {68return key == p_key.key;69}70};7172struct ShaderNames {73StringName particles_anim_h_frames;74StringName particles_anim_v_frames;75StringName particles_anim_loop;76};7778static ShaderNames *shader_names;7980struct ShaderData {81RID shader;82int users = 0;83};8485static HashMap<MaterialKey, ShaderData, MaterialKey> shader_map;8687MaterialKey current_key;8889_FORCE_INLINE_ MaterialKey _compute_key() const {90MaterialKey mk;91mk.key = 0;92mk.blend_mode = blend_mode;93mk.light_mode = light_mode;94mk.particles_animation = particles_animation;95return mk;96}9798static Mutex material_mutex;99static SelfList<CanvasItemMaterial>::List dirty_materials;100SelfList<CanvasItemMaterial> element;101102void _update_shader();103_FORCE_INLINE_ void _queue_shader_change();104105BlendMode blend_mode = BLEND_MODE_MIX;106LightMode light_mode = LIGHT_MODE_NORMAL;107bool particles_animation = false;108109// Proper values set in constructor.110int particles_anim_h_frames = 0;111int particles_anim_v_frames = 0;112bool particles_anim_loop = false;113114protected:115static void _bind_methods();116void _validate_property(PropertyInfo &p_property) const;117118public:119void set_blend_mode(BlendMode p_blend_mode);120BlendMode get_blend_mode() const;121122void set_light_mode(LightMode p_light_mode);123LightMode get_light_mode() const;124125void set_particles_animation(bool p_particles_anim);126bool get_particles_animation() const;127128void set_particles_anim_h_frames(int p_frames);129int get_particles_anim_h_frames() const;130void set_particles_anim_v_frames(int p_frames);131int get_particles_anim_v_frames() const;132133void set_particles_anim_loop(bool p_loop);134bool get_particles_anim_loop() const;135136static void init_shaders();137static void finish_shaders();138static void flush_changes();139140virtual RID get_shader_rid() const override;141142virtual Shader::Mode get_shader_mode() const override;143144CanvasItemMaterial();145virtual ~CanvasItemMaterial();146};147148VARIANT_ENUM_CAST(CanvasItemMaterial::BlendMode)149VARIANT_ENUM_CAST(CanvasItemMaterial::LightMode)150151152