Something went wrong. Try again.
Simple scenery generator
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#include "tree_mesh.h"
#include <godot_cpp/classes/multi_mesh.hpp>
#include "tree_genlayer.h"
using namespace godot;
void TreeMesh::GenerateMesh(TreeChunk* chunk){ std::map<TreePieceTemplate*,TreePieceMesh*> templates; // For each tree in the chunk for(auto tree : chunk->trees){ // And for each piece in the tree for(auto piece : tree->pieces){ // Check if a template is already part of the mesh structure auto treePieceTemplate = templates.find(piece.second); TreePieceMesh* multiMesh = nullptr; if(treePieceTemplate==templates.end()){ // If not // Check if there is an actual mesh if(piece.second->mesh!=nullptr){ // If yes // Add the mesh, and set the correct values multiMesh = memnew(TreePieceMesh); templates.insert(std::pair<TreePieceTemplate*,TreePieceMesh*>(piece.second,multiMesh)); meshes.push_back(multiMesh); multiMesh->SetMesh(piece.second->mesh); if(piece.second->material!=nullptr) multiMesh->set_material_override(piece.second->material); add_child(multiMesh); multiMesh->set_position(Vector3(0,0,0)); }
}else{ // If yes // Use the already existing multimesh multiMesh = treePieceTemplate->second; } // Add a new instance if(multiMesh!=nullptr) multiMesh->AddInstance(piece.first); } } for(auto mesh : meshes){ // For each mesh, set the multimesh info mesh->Generate(); }}
void TreePieceMesh::AddInstance(Transform3D t){ instances.push_back(t);}
void TreePieceMesh::Generate(){ MultiMesh* multiMesh = memnew(MultiMesh); multiMesh->set_transform_format(MultiMesh::TransformFormat::TRANSFORM_3D); multiMesh->set_instance_count(instances.size()); for(uint32_t i = 0; i < instances.size();i++){ multiMesh->set_instance_transform(i,instances[i]); } if(mesh!=nullptr) multiMesh->set_mesh(mesh); set_multimesh(multiMesh);}
void TreePieceMesh::SetMesh(Ref<Mesh> mesh){ this->mesh = mesh;}