Hey all!
I'm struggling to implement terrain modification using polyvox with unreal engine 4.
I use a raycasting function in UE4 to locate the block location, create a reference of the chunk actor, and then access the PolyVox::PagedVolume<PolyVox::MaterialDensityPair44> pointer to modify the chunk with setVoxel().
Code:
void ATerrainGenManager::EditVoxelAtLoc(FVector Loc)
{
float locx = Loc.X;
float locy = Loc.Y;
float locz = Loc.Z;
PolyVox::MaterialDensityPair44 Air;
Air.setMaterial(1);
Air.setDensity(255);
UE_LOG(LogTemp, Error, TEXT("chunk fullfilled"))
FVector UpdateLoc = GetActorLocation();
float upx = UpdateLoc.X;
float upy = UpdateLoc.Y;
float upz = UpdateLoc.Z;
//org wit up
PolyVox::Region ToExtract(PolyVox::Vector3DInt32(0, 0, 0), PolyVox::Vector3DInt32(sizevec_x, sizevec_y, sizevec_z));
PolyVox::PagedVolume<PolyVox::MaterialDensityPair44>* VoxChunk = VoxelVolume.Get();
//VoxChunk->getVoxel(10, 10, 10);
/*for (int x = ToExtract.getLowerX(); x <= ToExtract.getUpperX(); x++)
{
for (int y = ToExtract.getLowerY(); y <= ToExtract.getUpperY(); y++)
{
for (int z = ToExtract.getLowerZ(); z <= ToExtract.getUpperZ(); z++) {
VoxChunk->setVoxel(x, y, z, Air);
}
}
}*/
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
for (int z = 0; z < 100; z++) {
//VoxChunk->setVoxel(x, y, z, Air);
VoxChunk->getChunk(upx, upy, upz)->setVoxel(x, y, z, Air);
UE_LOG(LogTemp, Error, TEXT("chunk"))
}
}
}
// Define variables to pass into the CreateMeshSection function
auto Vertices = TArray<FVector>();
auto Indices = TArray<int32>();
auto Normals = TArray<FVector>();
auto UV0 = TArray<FVector2D>();
auto Colors = TArray<FColor>();
auto Tangents = TArray<FProcMeshTangent>();
//auto ExtractedMesh = PolyVox::extractCubicMesh(PolyVoxelVolume.Get(), ToExtract);
auto ExtractedMesh = PolyVox::extractMarchingCubesMesh(VoxelVolume.Get(), ToExtract);
auto DecodedMesh = decodeMesh(ExtractedMesh);
auto Index = DecodedMesh.getNoOfIndices();
//UE_LOG(LogTemp, Error, TEXT("number of indices: %d"), Index)
/*if(Index > 0){
for (uint32 i = 0; i < Index - 2; i+=3)
UE_LOG(LogTemp, Error, TEXT("Generated"))
}*/
UE_LOG(LogTemp, Error, TEXT("Loading new indices from the extractor"))
if(Index >= 2){
// Loop over all of the triangle vertex indices
for (uint32 i = 0; i < Index - 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);
Indices.Add(Vertices.Add(FPolyVoxVector(Vertex2.position) * 100.f));
Index = DecodedMesh.getIndex(i + 1);
auto Vertex1 = DecodedMesh.getVertex(Index);
Indices.Add(Vertices.Add(FPolyVoxVector(Vertex1.position) * 100.f));
Index = DecodedMesh.getIndex(i);
auto Vertex0 = DecodedMesh.getVertex(Index);
Indices.Add(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 j = 0; j < 3; j++)
{
Tangents.Add(FProcMeshTangent(TangentX, false));
Normals.Add(TangentZ);
}
}
}
Mesh->CreateMeshSection(0, Vertices, Indices, Normals, UV0, Colors, Tangents, true);
As you can see, I'm trying to fill a chunk with solid blocks.
But when I try to modify a chunk, it shrinks and changes shape.
When I use Mesh->UpdateMeshSection(), nothing happens.
What's the right way to modify the terrain with polyvox, and how should I fix my code?
Thanks.