Only buffer mesh each frame.
This commit is contained in:
@@ -25,4 +25,9 @@ void CubeMeshComponent::meshLoad(std::shared_ptr<SceneLoadContext> ctx) {
|
||||
} else {
|
||||
this->size = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
void CubeMeshComponent::setSize(const glm::vec3 &size) {
|
||||
this->size = size;
|
||||
this->invalidate();
|
||||
}
|
@@ -13,5 +13,13 @@ namespace Dawn {
|
||||
|
||||
void meshLoad(std::shared_ptr<SceneLoadContext> ctx) override;
|
||||
void meshBuffer(std::shared_ptr<Mesh> mesh) override;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Sets the size of the cube.
|
||||
*
|
||||
* @param size Size of the cube.
|
||||
*/
|
||||
void setSize(const glm::vec3 &size);
|
||||
};
|
||||
}
|
@@ -4,6 +4,7 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "MeshComponent.hpp"
|
||||
#include "scene/Scene.hpp"
|
||||
#include "component/display/MeshRenderer.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
@@ -11,11 +12,21 @@ using namespace Dawn;
|
||||
void MeshComponent::buffer() {
|
||||
if(!mesh) mesh = std::make_shared<Mesh>();
|
||||
this->meshBuffer(mesh);
|
||||
this->valid = true;
|
||||
events.clear();
|
||||
}
|
||||
|
||||
void MeshComponent::invalidate() {
|
||||
if(valid) return;
|
||||
valid = false;
|
||||
events.push_back(getScene()->onNextFrame.listen([this]() {
|
||||
this->buffer();
|
||||
valid = true;
|
||||
}));
|
||||
}
|
||||
|
||||
void MeshComponent::onInit() {
|
||||
this->buffer();
|
||||
|
||||
auto renderer = getItem()->getComponent<MeshRenderer>();
|
||||
if(renderer) renderer->mesh = mesh;
|
||||
}
|
||||
|
@@ -9,6 +9,14 @@
|
||||
|
||||
namespace Dawn {
|
||||
class MeshComponent : public SceneComponent {
|
||||
private:
|
||||
bool_t valid = true;
|
||||
|
||||
/**
|
||||
* Buffers the mesh.
|
||||
*/
|
||||
void buffer();
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Called when the mesh should be loaded.
|
||||
@@ -25,9 +33,9 @@ namespace Dawn {
|
||||
virtual void meshBuffer(std::shared_ptr<Mesh> mesh) = 0;
|
||||
|
||||
/**
|
||||
* Buffers the mesh.
|
||||
* Invalidates the mesh.
|
||||
*/
|
||||
void buffer();
|
||||
void invalidate();
|
||||
|
||||
public:
|
||||
std::shared_ptr<Mesh> mesh;
|
||||
|
@@ -65,4 +65,29 @@ void PlaneMeshComponent::meshBuffer(std::shared_ptr<Mesh> mesh) {
|
||||
columns, rows,
|
||||
uv
|
||||
);
|
||||
}
|
||||
|
||||
void PlaneMeshComponent::setColumns(const int32_t columns) {
|
||||
assertTrue(columns > 0, "Columns must be greater than 0.");
|
||||
this->columns = columns;
|
||||
this->invalidate();
|
||||
}
|
||||
|
||||
|
||||
void PlaneMeshComponent::setRows(const int32_t rows) {
|
||||
assertTrue(rows > 0, "Rows must be greater than 0.");
|
||||
this->rows = rows;
|
||||
this->invalidate();
|
||||
}
|
||||
|
||||
void PlaneMeshComponent::setPlaneSize(const glm::vec2 &planeSize) {
|
||||
assertTrue(planeSize.x > 0.0f, "Plane size x must be greater than 0.");
|
||||
assertTrue(planeSize.y > 0.0f, "Plane size y must be greater than 0.");
|
||||
this->planeSize = planeSize;
|
||||
this->invalidate();
|
||||
}
|
||||
|
||||
void PlaneMeshComponent::setUV(const glm::vec4 &uv) {
|
||||
this->uv = uv;
|
||||
this->invalidate();
|
||||
}
|
@@ -18,6 +18,9 @@ namespace Dawn {
|
||||
void meshBuffer(std::shared_ptr<Mesh> mesh) override;
|
||||
|
||||
public:
|
||||
|
||||
void setColumns(const int32_t columns);
|
||||
void setRows(const int32_t rows);
|
||||
void setPlaneSize(const glm::vec2 &planeSize);
|
||||
void setUV(const glm::vec4 &uv);
|
||||
};
|
||||
}
|
@@ -35,10 +35,10 @@ void QuadMeshComponent::meshLoad(std::shared_ptr<SceneLoadContext> ctx) {
|
||||
|
||||
void QuadMeshComponent::setPositions(const glm::vec4 &positions) {
|
||||
this->positions = positions;
|
||||
this->buffer();
|
||||
this->invalidate();
|
||||
}
|
||||
|
||||
void QuadMeshComponent::setUVs(const glm::vec4 &uvs) {
|
||||
this->uvs = uvs;
|
||||
this->buffer();
|
||||
this->invalidate();
|
||||
}
|
Reference in New Issue
Block a user