// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. #pragma once #include "MeshBase.h" #include "ModelInstanceEntry.h" #include "Config.h" #include "Types.h" class Lightmap; /// /// Represents part of the model that is made of vertices and can be rendered using custom material and transformation. /// API_CLASS(NoSpawn) class FLAXENGINE_API Mesh : public MeshBase { DECLARE_SCRIPTING_TYPE_WITH_CONSTRUCTOR_IMPL(Mesh, MeshBase); public: Mesh(const Mesh& other) : Mesh() { #if !BUILD_RELEASE CRASH; // Not used #endif } public: /// /// Gets the model owning this mesh. /// FORCE_INLINE Model* GetModel() const { return (Model*)_model; } /// /// Determines whether this mesh contains valid lightmap texture coordinates data. /// API_PROPERTY() bool HasLightmapUVs() const { return LightmapUVsIndex != -1; } /// /// Lightmap texture coordinates channel index. Value -1 indicates that channel is not available. /// API_FIELD() int32 LightmapUVsIndex = -1; public: /// /// Updates the model mesh (used by the virtual models created with Init rather than Load). /// [Deprecated in v1.10] /// /// The amount of vertices in the vertex buffer. /// The amount of triangles in the index buffer. /// The first vertex buffer data. /// The second vertex buffer data. /// The third vertex buffer data. /// The index buffer in clockwise order. /// True if failed, otherwise false. DEPRECATED("Use MeshAccessor or UpdateMesh with separate vertex attribute arrays instead.") bool UpdateMesh(uint32 vertexCount, uint32 triangleCount, const VB0ElementType* vb0, const VB1ElementType* vb1, const VB2ElementType* vb2, const uint32* ib); /// /// Updates the model mesh (used by the virtual models created with Init rather than Load). /// [Deprecated in v1.10] /// /// The amount of vertices in the vertex buffer. /// The amount of triangles in the index buffer. /// The first vertex buffer data. /// The second vertex buffer data. /// The third vertex buffer data. /// The index buffer in clockwise order. /// True if failed, otherwise false. DEPRECATED("Use MeshAccessor or UpdateMesh with separate vertex attribute arrays instead.") bool UpdateMesh(uint32 vertexCount, uint32 triangleCount, const VB0ElementType* vb0, const VB1ElementType* vb1, const VB2ElementType* vb2, const uint16* ib); /// /// Updates the model mesh (used by the virtual models created with Init rather than Load). /// Can be used only for virtual assets (see and ). /// Mesh data will be cached and uploaded to the GPU with a delay. /// [Deprecated in v1.10] /// /// The amount of vertices in the vertex buffer. /// The amount of triangles in the index buffer. /// The first vertex buffer data. /// The second vertex buffer data. /// The third vertex buffer data. /// The index buffer in clockwise order. /// True if index buffer uses 16-bit index buffer, otherwise 32-bit. /// True if failed, otherwise false. DEPRECATED("Use MeshAccessor or UpdateMesh with separate vertex attribute arrays instead.") bool UpdateMesh(uint32 vertexCount, uint32 triangleCount, const VB0ElementType* vb0, const VB1ElementType* vb1, const VB2ElementType* vb2, const void* ib, bool use16BitIndices); /// /// Updates the model mesh (used by the virtual models created with Init rather than Load). /// Can be used only for virtual assets (see and ). /// Mesh data will be cached and uploaded to the GPU with a delay. /// /// The amount of vertices in the vertex buffer. /// The amount of triangles in the index buffer. /// The mesh vertices positions. Cannot be null. /// The mesh index buffer (clockwise triangles). Uses 32-bit stride buffer. Cannot be null. /// The normal vectors (per vertex). /// The normal vectors (per vertex). Use null to compute them from normal vectors. /// The texture coordinates (per vertex). /// The vertex colors (per vertex). /// True if failed, otherwise false. bool UpdateMesh(uint32 vertexCount, uint32 triangleCount, const Float3* vertices, const uint16* triangles, const Float3* normals = nullptr, const Float3* tangents = nullptr, const Float2* uvs = nullptr, const Color32* colors = nullptr); /// /// Updates the model mesh (used by the virtual models created with Init rather than Load). /// Can be used only for virtual assets (see and ). /// Mesh data will be cached and uploaded to the GPU with a delay. /// /// The amount of vertices in the vertex buffer. /// The amount of triangles in the index buffer. /// The mesh vertices positions. Cannot be null. /// The mesh index buffer (clockwise triangles). Uses 32-bit stride buffer. Cannot be null. /// The normal vectors (per vertex). /// The normal vectors (per vertex). Use null to compute them from normal vectors. /// The texture coordinates (per vertex). /// The vertex colors (per vertex). /// True if failed, otherwise false. bool UpdateMesh(uint32 vertexCount, uint32 triangleCount, const Float3* vertices, const uint32* triangles, const Float3* normals = nullptr, const Float3* tangents = nullptr, const Float2* uvs = nullptr, const Color32* colors = nullptr); public: /// /// Load mesh data and Initialize GPU buffers /// [Deprecated in v1.10] /// /// Amount of vertices in the vertex buffer /// Amount of triangles in the index buffer /// Vertex buffer 0 data /// Vertex buffer 1 data /// Vertex buffer 2 data (may be null if not used) /// Index buffer data /// True if use 16 bit indices for the index buffer (true: uint16, false: uint32). /// True if cannot load data, otherwise false. DEPRECATED("Use Init intead.") bool Load(uint32 vertices, uint32 triangles, const void* vb0, const void* vb1, const void* vb2, const void* ib, bool use16BitIndexBuffer); public: /// /// Draws the mesh. /// /// The rendering context. /// The material to use for rendering. /// The world transformation of the model. /// The object static flags. /// True if rendered geometry can receive decals, otherwise false. /// The draw passes to use for rendering this object. /// The random per-instance value (normalized to range 0-1). /// Object sorting key. API_FUNCTION() void Draw(API_PARAM(Ref) const RenderContext& renderContext, MaterialBase* material, API_PARAM(Ref) const Matrix& world, StaticFlags flags = StaticFlags::None, bool receiveDecals = true, DrawPass drawModes = DrawPass::Default, float perInstanceRandom = 0.0f, int8 sortOrder = 0) const; /// /// Draws the mesh. /// /// The rendering context. /// The packed drawing info data. /// The LOD transition dither factor. void Draw(const RenderContext& renderContext, const DrawInfo& info, float lodDitherFactor) const; /// /// Draws the mesh. /// /// The rendering context batch. /// The packed drawing info data. /// The LOD transition dither factor. void Draw(const RenderContextBatch& renderContextBatch, const DrawInfo& info, float lodDitherFactor) const; public: // [MeshBase] bool Init(uint32 vertices, uint32 triangles, const Array>& vbData, const void* ibData, bool use16BitIndexBuffer, const Array>& vbLayout) override; void Release() override; private: // Internal bindings #if !COMPILE_WITHOUT_CSHARP API_FUNCTION(NoProxy) bool UpdateMeshUInt(int32 vertexCount, int32 triangleCount, const MArray* verticesObj, const MArray* trianglesObj, const MArray* normalsObj, const MArray* tangentsObj, const MArray* uvObj, const MArray* colorsObj); API_FUNCTION(NoProxy) bool UpdateMeshUShort(int32 vertexCount, int32 triangleCount, const MArray* verticesObj, const MArray* trianglesObj, const MArray* normalsObj, const MArray* tangentsObj, const MArray* uvObj, const MArray* colorsObj); API_FUNCTION(NoProxy) MArray* DownloadBuffer(bool forceGpu, MTypeObject* resultType, int32 typeI); #endif };