| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  immediate_mesh.h                                                      */ | 
|---|
| 3 | /**************************************************************************/ | 
|---|
| 4 | /*                         This file is part of:                          */ | 
|---|
| 5 | /*                             GODOT ENGINE                               */ | 
|---|
| 6 | /*                        https://godotengine.org                         */ | 
|---|
| 7 | /**************************************************************************/ | 
|---|
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
|---|
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
|---|
| 10 | /*                                                                        */ | 
|---|
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
|---|
| 12 | /* a copy of this software and associated documentation files (the        */ | 
|---|
| 13 | /* "Software"), to deal in the Software without restriction, including    */ | 
|---|
| 14 | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
|---|
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
|---|
| 16 | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
|---|
| 17 | /* the following conditions:                                              */ | 
|---|
| 18 | /*                                                                        */ | 
|---|
| 19 | /* The above copyright notice and this permission notice shall be         */ | 
|---|
| 20 | /* included in all copies or substantial portions of the Software.        */ | 
|---|
| 21 | /*                                                                        */ | 
|---|
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
|---|
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
|---|
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
|---|
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
|---|
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
|---|
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
|---|
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
|---|
| 29 | /**************************************************************************/ | 
|---|
| 30 |  | 
|---|
| 31 | #ifndef IMMEDIATE_MESH_H | 
|---|
| 32 | #define IMMEDIATE_MESH_H | 
|---|
| 33 |  | 
|---|
| 34 | #include "core/templates/local_vector.h" | 
|---|
| 35 | #include "scene/resources/mesh.h" | 
|---|
| 36 |  | 
|---|
| 37 | class ImmediateMesh : public Mesh { | 
|---|
| 38 | GDCLASS(ImmediateMesh, Mesh) | 
|---|
| 39 |  | 
|---|
| 40 | RID mesh; | 
|---|
| 41 |  | 
|---|
| 42 | bool uses_colors = false; | 
|---|
| 43 | bool uses_normals = false; | 
|---|
| 44 | bool uses_tangents = false; | 
|---|
| 45 | bool uses_uvs = false; | 
|---|
| 46 | bool uses_uv2s = false; | 
|---|
| 47 |  | 
|---|
| 48 | Color current_color; | 
|---|
| 49 | Vector3 current_normal; | 
|---|
| 50 | Plane current_tangent; | 
|---|
| 51 | Vector2 current_uv; | 
|---|
| 52 | Vector2 current_uv2; | 
|---|
| 53 |  | 
|---|
| 54 | LocalVector<Color> colors; | 
|---|
| 55 | LocalVector<Vector3> normals; | 
|---|
| 56 | LocalVector<Plane> tangents; | 
|---|
| 57 | LocalVector<Vector2> uvs; | 
|---|
| 58 | LocalVector<Vector2> uv2s; | 
|---|
| 59 | LocalVector<Vector3> vertices; | 
|---|
| 60 |  | 
|---|
| 61 | struct Surface { | 
|---|
| 62 | PrimitiveType primitive; | 
|---|
| 63 | Ref<Material> material; | 
|---|
| 64 | bool vertex_2d = false; | 
|---|
| 65 | int array_len = 0; | 
|---|
| 66 | uint32_t format = 0; | 
|---|
| 67 | AABB aabb; | 
|---|
| 68 | }; | 
|---|
| 69 |  | 
|---|
| 70 | LocalVector<Surface> surfaces; | 
|---|
| 71 |  | 
|---|
| 72 | bool surface_active = false; | 
|---|
| 73 | Surface active_surface_data; | 
|---|
| 74 |  | 
|---|
| 75 | Vector<uint8_t> surface_vertex_create_cache; | 
|---|
| 76 | Vector<uint8_t> surface_attribute_create_cache; | 
|---|
| 77 |  | 
|---|
| 78 | const Vector3 SMALL_VEC3 = Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON); | 
|---|
| 79 |  | 
|---|
| 80 | protected: | 
|---|
| 81 | static void _bind_methods(); | 
|---|
| 82 |  | 
|---|
| 83 | public: | 
|---|
| 84 | void surface_begin(PrimitiveType p_primitive, const Ref<Material> &p_material = Ref<Material>()); | 
|---|
| 85 | void surface_set_color(const Color &p_color); | 
|---|
| 86 | void surface_set_normal(const Vector3 &p_normal); | 
|---|
| 87 | void surface_set_tangent(const Plane &p_tangent); | 
|---|
| 88 | void surface_set_uv(const Vector2 &p_uv); | 
|---|
| 89 | void surface_set_uv2(const Vector2 &p_uv2); | 
|---|
| 90 | void surface_add_vertex(const Vector3 &p_vertex); | 
|---|
| 91 | void surface_add_vertex_2d(const Vector2 &p_vertex); | 
|---|
| 92 | void surface_end(); | 
|---|
| 93 |  | 
|---|
| 94 | void clear_surfaces(); | 
|---|
| 95 |  | 
|---|
| 96 | virtual int get_surface_count() const override; | 
|---|
| 97 | virtual int surface_get_array_len(int p_idx) const override; | 
|---|
| 98 | virtual int surface_get_array_index_len(int p_idx) const override; | 
|---|
| 99 | virtual Array surface_get_arrays(int p_surface) const override; | 
|---|
| 100 | virtual TypedArray<Array> surface_get_blend_shape_arrays(int p_surface) const override; | 
|---|
| 101 | virtual Dictionary surface_get_lods(int p_surface) const override; | 
|---|
| 102 | virtual BitField<ArrayFormat> surface_get_format(int p_idx) const override; | 
|---|
| 103 | virtual PrimitiveType surface_get_primitive_type(int p_idx) const override; | 
|---|
| 104 | virtual void surface_set_material(int p_idx, const Ref<Material> &p_material) override; | 
|---|
| 105 | virtual Ref<Material> surface_get_material(int p_idx) const override; | 
|---|
| 106 | virtual int get_blend_shape_count() const override; | 
|---|
| 107 | virtual StringName get_blend_shape_name(int p_index) const override; | 
|---|
| 108 | virtual void set_blend_shape_name(int p_index, const StringName &p_name) override; | 
|---|
| 109 |  | 
|---|
| 110 | virtual AABB get_aabb() const override; | 
|---|
| 111 |  | 
|---|
| 112 | virtual RID get_rid() const override; | 
|---|
| 113 |  | 
|---|
| 114 | ImmediateMesh(); | 
|---|
| 115 | ~ImmediateMesh(); | 
|---|
| 116 | }; | 
|---|
| 117 |  | 
|---|
| 118 | #endif // IMMEDIATE_MESH_H | 
|---|
| 119 |  | 
|---|