Exemple #1
0
        private Entity CreateChildEntity(PhysicsComponent component, ColliderShape shape, RenderGroup renderGroup, bool addOffset)
        {
            if (shape == null)
            {
                return(null);
            }

            switch (shape)
            {
            case BoxColliderShape _:
            case ConvexHullColliderShape _:
            case SphereColliderShape _:
            case CylinderColliderShape _:
            case CapsuleColliderShape _:
            case MeshColliderShape _:
            {
                IDebugPrimitive debugPrimitive;
                var             shapeType = shape.GetType();
                if (shapeType == typeof(CapsuleColliderShape) || shapeType == typeof(ConvexHullColliderShape) || shapeType == typeof(MeshColliderShape))
                {
                    if (!debugMeshCache2.TryGetValue(shape, out debugPrimitive))
                    {
                        debugPrimitive = new DebugPrimitive {
                            shape.CreateDebugPrimitive(graphicsDevice)
                        };
                        debugMeshCache2[shape] = debugPrimitive;
                    }
                }
                else
                {
                    if (!debugMeshCache.TryGetValue(shape.GetType(), out debugPrimitive))
                    {
                        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       = model,
                        RenderGroup = renderGroup,
                    },
                };

                var offset = addOffset ? Matrix.RotationQuaternion(shape.LocalRotation) * Matrix.Translation(shape.LocalOffset) : Matrix.Identity;

                entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * offset * Matrix.Scaling(shape.Scaling);

                entity.Transform.UseTRS = false;

                shape.DebugEntity = entity;

                return(entity);
            }

            default:
                return(null);
            }
        }
Exemple #2
0
        private Entity CreateChildEntity(PhysicsComponent component, ColliderShape shape, RenderGroup renderGroup, bool addOffset)
        {
            if (shape == null)
            {
                return(null);
            }

            switch (shape.Type)
            {
            case ColliderShapeTypes.Compound:
            {
                var entity = new Entity();

                //We got to recurse
                var compound = (CompoundColliderShape)shape;
                for (var i = 0; i < compound.Count; i++)
                {
                    var subShape  = compound[i];
                    var subEntity = CreateChildEntity(component, subShape, renderGroup, true);         //always add offsets to compounds
                    if (subEntity != null)
                    {
                        entity.AddChild(subEntity);
                    }
                }

                entity.Transform.LocalMatrix = Matrix.Identity;
                entity.Transform.UseTRS      = false;

                compound.DebugEntity = entity;

                return(entity);
            }

            case ColliderShapeTypes.Box:
            case ColliderShapeTypes.Capsule:
            case ColliderShapeTypes.ConvexHull:
            case ColliderShapeTypes.Cylinder:
            case ColliderShapeTypes.Sphere:
            case ColliderShapeTypes.Cone:
            case ColliderShapeTypes.StaticPlane:
            case ColliderShapeTypes.StaticMesh:
            case ColliderShapeTypes.Heightfield:
            {
                IDebugPrimitive debugPrimitive;
                var             type = shape.GetType();
                if (type == typeof(HeightfieldColliderShape) || type.BaseType == 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) || type == typeof(StaticMeshColliderShape))
                {
                    if (!debugMeshCache2.TryGetValue(shape, out debugPrimitive))
                    {
                        debugPrimitive = new DebugPrimitive {
                            shape.CreateDebugPrimitive(graphicsDevice)
                        };
                        debugMeshCache2[shape] = debugPrimitive;
                    }
                }
                else
                {
                    if (!debugMeshCache.TryGetValue(shape.GetType(), out debugPrimitive))
                    {
                        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       = model,
                        RenderGroup = renderGroup,
                    },
                };

                var offset = addOffset ? Matrix.RotationQuaternion(shape.LocalRotation) * Matrix.Translation(shape.LocalOffset) : Matrix.Identity;

                entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * offset * Matrix.Scaling(shape.Scaling);

                entity.Transform.UseTRS = false;

                shape.DebugEntity = entity;

                return(entity);
            }

            default:
                return(null);
            }
        }
Exemple #3
0
        private Entity CreateChildEntity(PhysicsComponent component, ColliderShape shape, bool addOffset)
        {
            if (shape == null)
            {
                return(null);
            }

            switch (shape.Type)
            {
            case ColliderShapeTypes.Compound:
            {
                var entity = new Entity();

                //We got to recurse
                var compound = (CompoundColliderShape)shape;
                for (var i = 0; i < compound.Count; i++)
                {
                    var subShape  = compound[i];
                    var subEntity = CreateChildEntity(component, subShape, true);         //always add offsets to compounds
                    if (subEntity != null)
                    {
                        entity.AddChild(subEntity);
                    }
                }

                entity.Transform.LocalMatrix = Matrix.Identity;
                entity.Transform.UseTRS      = false;

                compound.DebugEntity = entity;

                return(entity);
            }

            case ColliderShapeTypes.Box:
            case ColliderShapeTypes.Capsule:
            case ColliderShapeTypes.ConvexHull:
            case ColliderShapeTypes.Cylinder:
            case ColliderShapeTypes.Sphere:
            case ColliderShapeTypes.Cone:
            {
                var mat = triggerMaterial;

                var rigidbodyComponent = component as RigidbodyComponent;
                if (rigidbodyComponent != null)
                {
                    mat = rigidbodyComponent.IsKinematic ? kinematicMaterial : dynamicMaterial;
                    mat = rigidbodyComponent.IsTrigger ? triggerMaterial : mat;
                }
                else if (component is CharacterComponent)
                {
                    mat = characterMaterial;
                }
                else if (component is StaticColliderComponent)
                {
                    var staticCollider = (StaticColliderComponent)component;
                    mat = staticCollider.IsTrigger ? triggerMaterial : staticMaterial;
                }

                MeshDraw draw;
                var      type = shape.GetType();
                if (type == typeof(CapsuleColliderShape) || type == typeof(ConvexHullColliderShape))
                {
                    if (!debugMeshCache2.TryGetValue(shape, out draw))
                    {
                        draw = shape.CreateDebugPrimitive(graphicsDevice);
                        debugMeshCache2[shape] = draw;
                    }
                }
                else
                {
                    if (!debugMeshCache.TryGetValue(shape.GetType(), out draw))
                    {
                        draw = shape.CreateDebugPrimitive(graphicsDevice);
                        debugMeshCache[shape.GetType()] = draw;
                    }
                }

                var entity = new Entity
                {
                    new ModelComponent
                    {
                        Model = new Model
                        {
                            mat,
                            new Mesh
                            {
                                Draw = draw
                            }
                        }
                    }
                };

                var offset = addOffset ? Matrix.RotationQuaternion(shape.LocalRotation) * Matrix.Translation(shape.LocalOffset) : Matrix.Identity;

                entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * offset * Matrix.Scaling(shape.Scaling);

                entity.Transform.UseTRS = false;

                shape.DebugEntity = entity;

                return(entity);
            }

            default:
                return(null);
            }
        }
        private Entity CreateChildEntity(PhysicsComponent component, ColliderShape shape, bool addOffset)
        {
            if (shape == null)
                return null;

            switch (shape.Type)
            {
                case ColliderShapeTypes.Compound:
                    {
                        var entity = new Entity();

                        //We got to recurse
                        var compound = (CompoundColliderShape)shape;
                        for (var i = 0; i < compound.Count; i++)
                        {
                            var subShape = compound[i];
                            var subEntity = CreateChildEntity(component, subShape, true); //always add offsets to compounds
                            if (subEntity != null)
                            {
                                entity.AddChild(subEntity);
                            }
                        }

                        entity.Transform.LocalMatrix = Matrix.Identity;
                        entity.Transform.UseTRS = false;

                        compound.DebugEntity = entity;

                        return entity;
                    }
                case ColliderShapeTypes.Box:
                case ColliderShapeTypes.Capsule:
                case ColliderShapeTypes.ConvexHull:
                case ColliderShapeTypes.Cylinder:
                case ColliderShapeTypes.Sphere:
                case ColliderShapeTypes.Cone:
                    {
                        var mat = triggerMaterial;

                        var rigidbodyComponent = component as RigidbodyComponent;
                        if (rigidbodyComponent != null)
                        {
                            mat = rigidbodyComponent.IsKinematic ? kinematicMaterial : dynamicMaterial;
                            mat = rigidbodyComponent.IsTrigger ? triggerMaterial : mat;
                        }
                        else if (component is CharacterComponent)
                        {
                            mat = characterMaterial;
                        }
                        else if (component is StaticColliderComponent)
                        {
                            var staticCollider = (StaticColliderComponent)component;
                            mat = staticCollider.IsTrigger ? triggerMaterial : staticMaterial;
                        }

                        MeshDraw draw;
                        var type = shape.GetType();
                        if (type == typeof(CapsuleColliderShape) || type == typeof(ConvexHullColliderShape))
                        {
                            if (!debugMeshCache2.TryGetValue(shape, out draw))
                            {
                                draw = shape.CreateDebugPrimitive(graphicsDevice);
                                debugMeshCache2[shape] = draw;
                            }
                        }
                        else
                        {
                            if (!debugMeshCache.TryGetValue(shape.GetType(), out draw))
                            {
                                draw = shape.CreateDebugPrimitive(graphicsDevice);
                                debugMeshCache[shape.GetType()] = draw;
                            }
                        }

                        var entity = new Entity
                        {
                            new ModelComponent
                            {
                                Model = new Model
                                {
                                    mat,
                                    new Mesh
                                    {
                                        Draw = draw
                                    }
                                }
                            }
                        };

                        var offset = addOffset ? Matrix.RotationQuaternion(shape.LocalRotation) * Matrix.Translation(shape.LocalOffset) : Matrix.Identity;

                        entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * offset * Matrix.Scaling(shape.Scaling);

                        entity.Transform.UseTRS = false;

                        shape.DebugEntity = entity;

                        return entity;
                    }
                default:
                    return null;
            }
        }