Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MeshParams_vec_sp buildExtractedMesh(TerrainThreadRequest_sp threadReq) {
- int materialCount = threadReq->terrainMaterials->Num();
- MeshParams_vec_sp meshParams_vec_sp = MakeShareable(new std::vector<MeshParams>(materialCount));
- threadReq->location_cm = getLocFromRegionLoc(threadReq->regionWidth, threadReq->regionLoc);
- int chunkCounter = 0;
- PolyVox::Mesh<PolyVox::Vertex<MDP88>, unsigned int> DecodedMesh;
- if (threadReq->smooth) {
- //auto DecodedMesh = decodeMesh(threadReq->marchingExtractedMesh);
- DecodedMesh = decodeMesh(threadReq->marchingExtractedMesh);
- }
- else {
- DecodedMesh = decodeMesh(threadReq->cubicExtractedMesh);
- }
- for (int Material = 0; Material < materialCount; Material++)
- {
- MeshParams* mp = &meshParams_vec_sp->data()[Material];
- mp->material_index = Material;
- mp->location = threadReq->location_cm;
- mp->colliderEnabled = true;
- // Loop over all of the triangle vertex indices
- for (uint32 i = 0; i < DecodedMesh.getNoOfIndices() - 2; i += 3)
- {
- // We need to add the vertices of each triangle in reverse or the mesh will be upside down
- auto Index = DecodedMesh.getIndex(i + 2);
- auto Vertex2 = DecodedMesh.getVertex(Index);
- auto TriangleMaterial = Vertex2.data.getMaterial();
- // Before we continue, we need to be sure that the triangle is the right material; we don't want to use verticies from other materials
- if (TriangleMaterial == (Material + 1))
- {
- // If it is of the same material, then we need to add the correct indices now
- mp->Indices.Add(mp->Vertices.Add(FPolyVoxVector(Vertex2.position) * 100.f));
- Index = DecodedMesh.getIndex(i + 1);
- auto Vertex1 = DecodedMesh.getVertex(Index);
- mp->Indices.Add(mp->Vertices.Add(FPolyVoxVector(Vertex1.position) * 100.f));
- Index = DecodedMesh.getIndex(i);
- auto Vertex0 = DecodedMesh.getVertex(Index);
- mp->Indices.Add(mp->Vertices.Add(FPolyVoxVector(Vertex0.position) * 100.f));
- // Calculate the tangents of our triangle
- const FVector Edge01 = FPolyVoxVector(Vertex1.position - Vertex0.position);
- const FVector Edge02 = FPolyVoxVector(Vertex2.position - Vertex0.position);
- const FVector TangentX = Edge01.GetSafeNormal();
- FVector TangentZ = (Edge01 ^ Edge02).GetSafeNormal();
- for (int32 i = 0; i < 3; i++)
- {
- mp->Tangents.Add(FRuntimeMeshTangent(TangentX, false));
- mp->Normals.Add(TangentZ);
- }
- }
- }
- }
- return meshParams_vec_sp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement