Various fixes and improvements for large worlds rendering

This commit is contained in:
Wojtek Figat
2022-06-28 20:26:01 +02:00
parent b1640515c4
commit 27d266546e
18 changed files with 80 additions and 70 deletions

View File

@@ -512,7 +512,6 @@ void Terrain::Draw(RenderContext& renderContext)
const float chunkSize = TERRAIN_UNITS_PER_VERTEX * (float)_chunkSize;
const float posToUV = 0.25f / chunkSize;
Float4 localToUV(posToUV, posToUV, 0.0f, 0.0f);
Matrix localToWorld;
for (const TerrainPatch* patch : _patches)
{
if (!patch->Heightmap)
@@ -522,8 +521,7 @@ void Terrain::Draw(RenderContext& renderContext)
patchTransform.Orientation = Quaternion::Identity;
patchTransform.Scale = Float3(1.0f, patch->_yHeight, 1.0f);
patchTransform = _transform.LocalToWorld(patchTransform);
patchTransform.GetWorld(localToWorld);
GlobalSignDistanceFieldPass::Instance()->RasterizeHeightfield(this, patch->Heightmap->GetTexture(), localToWorld, patch->_bounds, localToUV);
GlobalSignDistanceFieldPass::Instance()->RasterizeHeightfield(this, patch->Heightmap->GetTexture(), patchTransform, patch->_bounds, localToUV);
}
return;
}
@@ -533,16 +531,17 @@ void Terrain::Draw(RenderContext& renderContext)
{
if (!patch->Heightmap)
continue;
Matrix worldToLocal;
Matrix localToWorld, worldToLocal;
BoundingSphere chunkSphere;
BoundingBox localBounds;
for (int32 chunkIndex = 0; chunkIndex < TerrainPatch::CHUNKS_COUNT; chunkIndex++)
{
TerrainChunk* chunk = &patch->Chunks[chunkIndex];
Matrix::Invert(chunk->GetWorld(), worldToLocal);
chunk->GetTransform().GetWorld(localToWorld); // TODO: large-worlds
Matrix::Invert(localToWorld, worldToLocal);
BoundingBox::Transform(chunk->GetBounds(), worldToLocal, localBounds);
BoundingSphere::FromBox(chunk->GetBounds(), chunkSphere);
GlobalSurfaceAtlasPass::Instance()->RasterizeActor(this, chunk, chunkSphere, chunk->GetWorld(), localBounds, 1 << 2, false);
GlobalSurfaceAtlasPass::Instance()->RasterizeActor(this, chunk, chunkSphere, chunk->GetTransform(), localBounds, 1 << 2, false);
}
}
return;

View File

@@ -86,7 +86,7 @@ void TerrainChunk::Draw(const RenderContext& renderContext) const
return;
drawCall.InstanceCount = 1;
drawCall.Material = _cachedDrawMaterial;
drawCall.World = _world;
renderContext.View.GetWorldMatrix(_transform, drawCall.World);
drawCall.ObjectPosition = drawCall.World.GetTranslation();
drawCall.Terrain.Patch = _patch;
drawCall.Terrain.HeightmapUVScaleBias = _heightmapUVScaleBias;
@@ -142,7 +142,7 @@ void TerrainChunk::Draw(const RenderContext& renderContext, MaterialBase* materi
return;
drawCall.InstanceCount = 1;
drawCall.Material = material;
drawCall.World = _world;
renderContext.View.GetWorldMatrix(_transform, drawCall.World);
drawCall.ObjectPosition = drawCall.World.GetTranslation();
drawCall.Terrain.Patch = _patch;
drawCall.Terrain.HeightmapUVScaleBias = _heightmapUVScaleBias;
@@ -214,8 +214,7 @@ void TerrainChunk::UpdateTransform()
localTransform.Translation = _patch->_offset + Vector3(_x * size, _patch->_yOffset, _z * size);
localTransform.Orientation = Quaternion::Identity;
localTransform.Scale = Vector3(1.0f, _patch->_yHeight, 1.0f);
localTransform = terrainTransform.LocalToWorld(localTransform);
localTransform.GetWorld(_world);
_transform = terrainTransform.LocalToWorld(localTransform);
}
void TerrainChunk::CacheNeighbors()

View File

@@ -4,6 +4,7 @@
#include "Engine/Core/Math/BoundingBox.h"
#include "Engine/Core/Math/Matrix.h"
#include "Engine/Core/Math/Transform.h"
#include "Engine/Serialization/ISerializable.h"
#include "Engine/Content/Assets/MaterialBase.h"
#include "Engine/Level/Scene/Lightmap.h"
@@ -26,7 +27,7 @@ private:
TerrainPatch* _patch;
uint16 _x, _z;
Float4 _heightmapUVScaleBias;
Matrix _world;
Transform _transform;
BoundingBox _bounds;
Vector3 _boundsCenter;
float _perInstanceRandom;
@@ -85,11 +86,11 @@ public:
}
/// <summary>
/// Gets the chunk world matrix transform.
/// Gets the chunk transformation (world to local).
/// </summary>
FORCE_INLINE const Matrix& GetWorld() const
FORCE_INLINE const Transform& GetTransform() const
{
return _world;
return _transform;
}
/// <summary>