62 <<
"Updating MeshRender system for entity " << entityIdentifier;
69 const auto &transformComponent =
71 const auto &modelComponent =
74 const std::string &meshPath = modelComponent.getMeshPath();
75 const std::string &texturePath = modelComponent.getTexturePath();
76 const auto &pose = transformComponent.getPose();
78 getLogger().debug() <<
"Rendering model '" << meshPath
79 <<
"' for entity " << entityIdentifier;
81 auto model = texturePath.empty()
82 ? _ressourceProvider->loadModel(meshPath, pose)
83 : _ressourceProvider->loadModel(meshPath, pose, texturePath);
86 getLogger().warning() <<
"Failed to load model '" << meshPath
87 <<
"' for entity " << entityIdentifier;
96 utility::graphic::PositionF localMin(0.0f, 0.0f, 0.0f);
97 utility::graphic::PositionF localMax(0.0f, 0.0f, 0.0f);
99 for (
const auto &mesh: model->getMeshes()) {
100 for (
const auto &vertex: mesh->getVertices()) {
101 const auto position = vertex.getPosition();
102 for (std::size_t axis = 0; axis < 3; ++axis) {
103 if (first || position[axis] < localMin[axis]) {
104 localMin[axis] = position[axis];
106 if (first || position[axis] > localMax[axis]) {
107 localMax[axis] = position[axis];
113 const float scale = modelComponent.scaleFor(localMax[0] - localMin[0],
114 localMax[1] - localMin[1]);
119 const float halfDepth = (localMax[2] - localMin[2]) * scale / 2.0f;
120 const auto towardViewer =
121 -pose.getOrientation().getForward() * (halfDepth + 1.0f);
122 const utility::graphic::PositionF center(
123 pose.getPosition().x + bound.getWidth() / 2.0f + towardViewer[0],
124 pose.getPosition().y - bound.getHeight() / 2.0f + towardViewer[1],
125 pose.getPosition().z + towardViewer[2]);
127 std::vector<utility::graphic::Mesh> meshes;
128 meshes.reserve(model->getMeshes().size());
129 for (
const auto &mesh: model->getMeshes()) {
130 std::vector<utility::graphic::VertexF> vertices;
131 vertices.reserve(mesh->getVertices().size());
132 for (
auto vertex: mesh->getVertices()) {
133 const auto position = vertex.getPosition();
134 const utility::graphic::PositionF local(
135 (position[0] - (localMin[0] + localMax[0]) / 2.0f) * scale,
136 (position[1] - (localMin[1] + localMax[1]) / 2.0f) * scale,
137 (position[2] - (localMin[2] + localMax[2]) / 2.0f) * scale);
139 local, pose.getOrientation());
140 vertex.setPosition(utility::graphic::PositionF(
141 center[0] + rotated[0], center[1] + rotated[1],
142 center[2] + rotated[2]));
143 vertices.push_back(vertex);
145 meshes.emplace_back(vertices, mesh->getIndices());
148 auto renderable = std::make_shared<MeshRenderable>(
150 texturePath.empty() ?
"default_material" :
"image_" + texturePath);
153 if (handle ==
nullptr) {
154 size_t objectId = _engine.createObject(renderable);
158 if (!_engine.updateObject(renderable, handle->
objectId)) {
159 handle->
objectId = _engine.createObject(renderable);