Fixes for Terrain with Large Worlds usage

This commit is contained in:
Wojtek Figat
2022-07-03 15:38:39 +02:00
parent 98d8543334
commit 62ff305fad
9 changed files with 19 additions and 13 deletions

View File

@@ -67,6 +67,7 @@ namespace FlaxEditor.Gizmo
var renderList = RenderList.GetFromPool();
var prevList = renderContext.List;
renderContext.List = renderList;
renderContext.View.Pass = DrawPass.GBuffer | DrawPass.Forward;
try
{
Viewport.DrawEditorPrimitives(context, ref renderContext, target, targetDepth);

View File

@@ -24,10 +24,11 @@ namespace FlaxEditor.Tools.Terrain.Brushes
/// <summary>
/// Gets the brush material for the terrain chunk rendering. It must have domain set to Terrain. Setup material parameters within this call.
/// </summary>
/// <param name="renderContext">The rendering context.</param>
/// <param name="position">The world-space brush position.</param>
/// <param name="color">The brush position.</param>
/// <returns>The ready to render material for terrain chunks overlay on top of the terrain.</returns>
public abstract MaterialInstance GetBrushMaterial(ref Vector3 position, ref Color color);
public abstract MaterialInstance GetBrushMaterial(ref RenderContext renderContext, ref Vector3 position, ref Color color);
/// <summary>
/// Loads the brush material from the internal location. It's later cached by the object and reused.

View File

@@ -97,7 +97,7 @@ namespace FlaxEditor.Tools.Terrain.Brushes
}
/// <inheritdoc />
public override MaterialInstance GetBrushMaterial(ref Vector3 position, ref Color color)
public override MaterialInstance GetBrushMaterial(ref RenderContext renderContext, ref Vector3 position, ref Color color)
{
var material = CacheMaterial(EditorAssets.TerrainCircleBrushMaterial);
if (material)
@@ -108,7 +108,7 @@ namespace FlaxEditor.Tools.Terrain.Brushes
float falloff = halfSize * Falloff;
float radius = halfSize - falloff;
material.SetParameterValue("Color", color);
material.SetParameterValue("BrushData0", new Float4(position, radius));
material.SetParameterValue("BrushData0", new Float4(position - renderContext.View.Origin, radius));
material.SetParameterValue("BrushData1", new Float4(falloff, (float)FalloffType, 0, 0));
}
return material;

View File

@@ -88,14 +88,14 @@ namespace FlaxEditor.Tools.Terrain
var patchCoord = Mode.SelectedPatchCoord;
if (!terrain.HasPatch(ref patchCoord) && _planeModel)
{
var planeSize = 256.0f;
var planeSize = 100.0f;
var patchSize = terrain.ChunkSize * FlaxEngine.Terrain.UnitsPerVertex * FlaxEngine.Terrain.PatchEdgeChunksCount;
Matrix world = Matrix.RotationZ(-Mathf.PiOverTwo) *
Matrix world = Matrix.RotationX(-Mathf.PiOverTwo) *
Matrix.Scaling(patchSize / planeSize) *
Matrix.Translation(patchSize * (0.5f + patchCoord.X), 0, patchSize * (0.5f + patchCoord.Y)) *
Matrix.Scaling(terrain.Scale) *
Matrix.RotationQuaternion(terrain.Orientation) *
Matrix.Translation(terrain.Position);
Matrix.Translation(terrain.Position - renderContext.View.Origin);
_planeModel.Draw(ref renderContext, _highlightMaterial, ref world);
}

View File

@@ -74,7 +74,7 @@ namespace FlaxEditor.Tools.Terrain
{
var brushPosition = Mode.CursorPosition;
var brushColor = new Color(1.0f, 0.85f, 0.0f); // TODO: expose to editor options
var brushMaterial = Mode.CurrentBrush.GetBrushMaterial(ref brushPosition, ref brushColor);
var brushMaterial = Mode.CurrentBrush.GetBrushMaterial(ref renderContext, ref brushPosition, ref brushColor);
if (!brushMaterial)
return;

View File

@@ -74,7 +74,7 @@ namespace FlaxEditor.Tools.Terrain
{
var brushPosition = Mode.CursorPosition;
var brushColor = new Color(1.0f, 0.85f, 0.0f); // TODO: expose to editor options
var brushMaterial = Mode.CurrentBrush.GetBrushMaterial(ref brushPosition, ref brushColor);
var brushMaterial = Mode.CurrentBrush.GetBrushMaterial(ref renderContext, ref brushPosition, ref brushColor);
if (!brushMaterial)
return;

View File

@@ -556,10 +556,12 @@ void Terrain::Draw(RenderContext& renderContext)
// Frustum vs Box culling for patches
const BoundingFrustum frustum = renderContext.View.CullingFrustum;
const Vector3 origin = renderContext.View.Origin;
for (int32 patchIndex = 0; patchIndex < _patches.Count(); patchIndex++)
{
const auto patch = _patches[patchIndex];
if (renderContext.View.IsCullingDisabled || frustum.Intersects(patch->_bounds))
BoundingBox bounds(patch->_bounds.Minimum - origin, patch->_bounds.Maximum - origin);
if (renderContext.View.IsCullingDisabled || frustum.Intersects(bounds))
{
// Skip if has no heightmap or it's not loaded
if (patch->Heightmap == nullptr || patch->Heightmap->GetTexture()->ResidentMipLevels() == 0)
@@ -570,7 +572,8 @@ void Terrain::Draw(RenderContext& renderContext)
{
auto chunk = &patch->Chunks[chunkIndex];
chunk->_cachedDrawLOD = 0;
if (renderContext.View.IsCullingDisabled || frustum.Intersects(chunk->_bounds))
bounds = BoundingBox(chunk->_bounds.Minimum - origin, chunk->_bounds.Maximum - origin);
if (renderContext.View.IsCullingDisabled || frustum.Intersects(bounds))
{
if (chunk->PrepareDraw(renderContext))
{

View File

@@ -45,7 +45,7 @@ bool TerrainChunk::PrepareDraw(const RenderContext& renderContext)
// Calculate chunk distance to view
const auto lodView = (renderContext.LodProxyView ? renderContext.LodProxyView : &renderContext.View);
const float distance = Float3::Distance(_boundsCenter, lodView->Position); // TODO: large-worlds
const float distance = Float3::Distance(_boundsCenter - lodView->Origin, lodView->Position);
lod = (int32)Math::Pow(distance / chunkEdgeSize, lodDistribution);
lod += lodBias;

View File

@@ -2244,7 +2244,8 @@ void TerrainPatch::CacheDebugLines()
void TerrainPatch::DrawPhysicsDebug(RenderView& view)
{
if (!_physicsShape || !view.CullingFrustum.Intersects(_bounds))
const BoundingBox bounds(_bounds.Minimum - view.Origin, _bounds.Maximum - view.Origin);
if (!_physicsShape || !view.CullingFrustum.Intersects(bounds))
return;
const Transform terrainTransform = _terrain->_transform;
@@ -2258,7 +2259,7 @@ void TerrainPatch::DrawPhysicsDebug(RenderView& view)
else
{
BoundingSphere sphere;
BoundingSphere::FromBox(_bounds, sphere);
BoundingSphere::FromBox(bounds, sphere);
if (Vector3::Distance(sphere.Center, view.Position) - sphere.Radius < 4000.0f)
{
if (_debugLines.IsEmpty())