Skip to content
Permalink
Browse files

Merge remote-tracking branch 'origin/master-3.0'

  • Loading branch information
xen2 committed Apr 9, 2019
2 parents f60d8bb + 29b3e75 commit 41f9389825ed6d5c2e0423cb17e3c56c1c6efad8
@@ -129,6 +129,15 @@ public virtual MeshDraw CreateDebugPrimitive(GraphicsDevice device)
return null;
}

public virtual IDebugPrimitive CreateUpdatableDebugPrimitive(GraphicsDevice device)
{
return null;
}

public virtual void UpdateDebugPrimitive(CommandList commandList, IDebugPrimitive debugPrimitive)
{
}

public Matrix DebugPrimitiveMatrix;

internal bool NeedsCustomCollisionCallback;
@@ -32,5 +32,7 @@ public enum ColliderShapeTypes
StaticPlane,

Cone,

Heightfield,
}
}
@@ -0,0 +1,34 @@
// Copyright (c) Xenko contributors (https://xenko.com)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

using System.Collections;
using System.Collections.Generic;
using Xenko.Rendering;

namespace Xenko.Physics
{
public class DebugPrimitive : IDebugPrimitive, IEnumerable<MeshDraw>
{
public readonly List<MeshDraw> MeshDraws = new List<MeshDraw>();

public void Add(MeshDraw meshDraw)
{
MeshDraws.Add(meshDraw);
}

public IEnumerable<MeshDraw> GetMeshDraws()
{
return MeshDraws;
}

public IEnumerator<MeshDraw> GetEnumerator()
{
return MeshDraws.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
@@ -50,5 +50,26 @@ private static Material CreateInternal(GraphicsDevice device, Color color, float

return material;
}

public static Material CreateHeightfieldMaterial(GraphicsDevice device, Color color, float intensity)
{
var colorVertexStream = new ComputeVertexStreamColor { Stream = new ColorVertexStreamDefinition() };
var computeColor = new ComputeBinaryColor(new ComputeColor(new Color4(color).ToColorSpace(device.ColorSpace)), colorVertexStream, BinaryOperator.Multiply);

var material = Material.New(device, new MaterialDescriptor
{
Attributes = new MaterialAttributes
{
Diffuse = new MaterialDiffuseMapFeature(computeColor),
DiffuseModel = new MaterialDiffuseLambertModelFeature(),
Emissive = new MaterialEmissiveMapFeature(computeColor),
},
});

// set the color to the material
material.Passes[0].Parameters.Set(MaterialKeys.EmissiveIntensity, intensity);

return material;
}
}
}
@@ -44,6 +44,8 @@ public PhysicsProcessor()

internal void RenderColliderShapes(bool enabled)
{
debugShapeRendering.Enabled = enabled;

colliderShapesRendering = enabled;

if (!colliderShapesRendering)
@@ -6,7 +6,9 @@
using Xenko.Core;
using Xenko.Core.Mathematics;
using Xenko.Engine;
using Xenko.Games;
using Xenko.Graphics;
using Xenko.Physics.Shapes;
using Xenko.Rendering;

namespace Xenko.Physics.Engine
@@ -35,9 +37,13 @@ private enum ComponentType

private readonly Dictionary<ComponentType, Material> componentTypeDefaultMaterial = new Dictionary<ComponentType, Material>();
private readonly Dictionary<ComponentType, Material> componentTypeStaticPlaneMaterial = new Dictionary<ComponentType, Material>();
private readonly Dictionary<ComponentType, Material> componentTypeHeightfieldMaterial = new Dictionary<ComponentType, Material>();

private readonly Dictionary<Type, MeshDraw> debugMeshCache = new Dictionary<Type, MeshDraw>();
private readonly Dictionary<ColliderShape, MeshDraw> debugMeshCache2 = new Dictionary<ColliderShape, MeshDraw>();
private readonly Dictionary<Type, IDebugPrimitive> debugMeshCache = new Dictionary<Type, IDebugPrimitive>();
private readonly Dictionary<ColliderShape, IDebugPrimitive> debugMeshCache2 = new Dictionary<ColliderShape, IDebugPrimitive>();
private readonly Dictionary<ColliderShape, IDebugPrimitive> updatableDebugMeshCache = new Dictionary<ColliderShape, IDebugPrimitive>();

private readonly Dictionary<ColliderShape, IDebugPrimitive> updatableDebugMeshes = new Dictionary<ColliderShape, IDebugPrimitive>();

public override void Initialize()
{
@@ -48,11 +54,32 @@ public override void Initialize()
var type = (ComponentType)typeObject;
componentTypeDefaultMaterial[type] = PhysicsDebugShapeMaterial.CreateDefault(graphicsDevice, Color.AdjustSaturation(componentTypeColor[type], 0.77f), 1);
componentTypeStaticPlaneMaterial[type] = componentTypeDefaultMaterial[type];
componentTypeHeightfieldMaterial[type] = PhysicsDebugShapeMaterial.CreateHeightfieldMaterial(graphicsDevice, Color.AdjustSaturation(componentTypeColor[type], 0.77f), 1);
// TODO enable this once material is implemented.
// ComponentTypeStaticPlaneMaterial[type] = PhysicsDebugShapeMaterial.CreateStaticPlane(graphicsDevice, Color.AdjustSaturation(ComponentTypeColor[type], 0.77f), 1);
}
}

public override void Update(GameTime gameTime)
{
var unusedShapes = new List<ColliderShape>();
foreach (var keyValuePair in updatableDebugMeshes)
{
if (keyValuePair.Value != null && keyValuePair.Key.DebugEntity?.Scene != null)
{
keyValuePair.Key.UpdateDebugPrimitive(Game.GraphicsContext.CommandList, keyValuePair.Value);
}
else
{
unusedShapes.Add(keyValuePair.Key);
}
}
foreach (var shape in unusedShapes)
{
updatableDebugMeshes.Remove(shape);
}
}

public PhysicsShapesRenderingService(IServiceRegistry registry) : base(registry)
{
}
@@ -129,38 +156,53 @@ private Entity CreateChildEntity(PhysicsComponent component, ColliderShape shape
case ColliderShapeTypes.Sphere:
case ColliderShapeTypes.Cone:
case ColliderShapeTypes.StaticPlane:
case ColliderShapeTypes.Heightfield:
{
MeshDraw draw;
IDebugPrimitive debugPrimitive;
var type = shape.GetType();
if (type == typeof(CapsuleColliderShape) || type == typeof(ConvexHullColliderShape))
if (type == typeof(HeightfieldColliderShape))
{
if (!updatableDebugMeshCache.TryGetValue(shape, out debugPrimitive))
{
debugPrimitive = shape.CreateUpdatableDebugPrimitive(graphicsDevice);
updatableDebugMeshCache[shape] = debugPrimitive;
}
if (!updatableDebugMeshes.ContainsKey(shape))
{
updatableDebugMeshes.Add(shape, debugPrimitive);
}
}
else if (type == typeof(CapsuleColliderShape) || type == typeof(ConvexHullColliderShape))
{
if (!debugMeshCache2.TryGetValue(shape, out draw))
if (!debugMeshCache2.TryGetValue(shape, out debugPrimitive))
{
draw = shape.CreateDebugPrimitive(graphicsDevice);
debugMeshCache2[shape] = draw;
debugPrimitive = new DebugPrimitive { shape.CreateDebugPrimitive(graphicsDevice) };
debugMeshCache2[shape] = debugPrimitive;
}
}
else
{
if (!debugMeshCache.TryGetValue(shape.GetType(), out draw))
if (!debugMeshCache.TryGetValue(shape.GetType(), out debugPrimitive))
{
draw = shape.CreateDebugPrimitive(graphicsDevice);
debugMeshCache[shape.GetType()] = draw;
debugPrimitive = new DebugPrimitive { shape.CreateDebugPrimitive(graphicsDevice) };
debugMeshCache[shape.GetType()] = debugPrimitive;
}
}

var model = new Model
{
GetMaterial(component, shape),
};
foreach (var meshDraw in debugPrimitive.GetMeshDraws())
{
model.Add(new Mesh { Draw = meshDraw });
}

var entity = new Entity
{
new ModelComponent
{
Model = new Model
{
GetMaterial(component, shape),
new Mesh
{
Draw = draw,
},
},
Model = model,
RenderGroup = renderGroup,
},
};
@@ -200,9 +242,18 @@ private Material GetMaterial(EntityComponent component, ColliderShape shape)
componentType = staticCollider.IsTrigger ? ComponentType.Trigger : ComponentType.Static;
}

return shape is StaticPlaneColliderShape
? componentTypeStaticPlaneMaterial[componentType]
: componentTypeDefaultMaterial[componentType];
if (shape is StaticPlaneColliderShape)
{
return componentTypeStaticPlaneMaterial[componentType];
}
else if (shape is HeightfieldColliderShape)
{
return componentTypeHeightfieldMaterial[componentType];
}
else
{
return componentTypeDefaultMaterial[componentType];
}
}
}
}
@@ -0,0 +1,12 @@
// Copyright (c) Xenko contributors (https://xenko.com)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

namespace Xenko.Physics
{
public enum HeightfieldTypes
{
Byte = 1,
Short = 2,
Float = 4,
}
}
@@ -0,0 +1,13 @@
// Copyright (c) Xenko contributors (https://xenko.com)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

using System.Collections.Generic;
using Xenko.Rendering;

namespace Xenko.Physics
{
public interface IDebugPrimitive
{
IEnumerable<MeshDraw> GetMeshDraws();
}
}

0 comments on commit 41f9389

Please sign in to comment.
You can’t perform that action at this time.