Exemple #1
0
        public override void Update()
        {
            if (ContentEntity == null)
            {
                return;
            }

            if ((!IsEnabled || !Component.Enabled) && Component.DebugEntity != null)
            {
                if (!rendering)
                {
                    return;
                }

                Component.DebugEntity.Enable <ModelComponent>(false, true);
                rendering = false;
                return;
            }

            // Create and add the element missing
            PhysicsElementInfo entityInfo;

            var skinnedElement = Component as PhysicsSkinnedComponentBase;
            var modelComponent = ContentEntity.Get <ModelComponent>();
            var skeleton       = modelComponent?.Skeleton;

            if (!elementToEntity.TryGetValue(Component, out entityInfo) || entityInfo.HasChanged(Component, skeleton))
            {
                //remove and clean up the old debug entity
                if (Component.DebugEntity != null)
                {
                    spawnedEntities.Remove(Component.DebugEntity);
                    Component.RemoveDebugEntity(EditorScene);
                    Component.DebugEntity = null;
                }

                //compose shape and fill data as data is not being filled by the processor when we run from the editor
                Component.ComposeShape();

                // check for bones
                if (skinnedElement?.ColliderShape != null)
                {
                    if (skeleton != null)
                    {
                        var boneIndex = skeleton.Nodes.IndexOf(x => x.Name == skinnedElement.NodeName);
                        if (boneIndex != -1)
                        {
                            skinnedElement.BoneIndex = boneIndex;

                            Vector3    position, scale;
                            Quaternion rotation;

                            ContentEntity.Transform.UpdateWorldMatrix();
                            var isScalingNegative = false;
                            if (ContentEntity.Transform.WorldMatrix.Decompose(out scale, out rotation, out position))
                            {
                                isScalingNegative = scale.X * scale.Y * scale.Z < 0.0f;
                            }
                            skeleton.NodeTransformations[0].LocalMatrix       = ContentEntity.Transform.WorldMatrix;
                            skeleton.NodeTransformations[0].IsScalingNegative = isScalingNegative;
                            skeleton.UpdateMatrices();

                            skinnedElement.BoneWorldMatrixOut = skeleton.NodeTransformations[boneIndex].WorldMatrix;

                            if (skinnedElement.ColliderShape.LocalOffset != Vector3.Zero || skinnedElement.ColliderShape.LocalRotation != Quaternion.Identity)
                            {
                                skinnedElement.BoneWorldMatrixOut = Matrix.Multiply(skinnedElement.ColliderShape.PositiveCenterMatrix, skinnedElement.BoneWorldMatrixOut);
                            }
                        }
                    }
                }

                Component.AddDebugEntity(EditorScene, RenderGroup, Component.BoneIndex == -1);
                if (Component.DebugEntity != null)
                {
                    spawnedEntities.Add(Component.DebugEntity);
                    Component.DebugEntity?.Enable <ModelComponent>(false, true);
                    rendering = false; //make sure we refresh enabled flags?
                }

                elementToEntity[Component] = new PhysicsElementInfo(Component, skeleton);
            }

            if (Component.DebugEntity != null)
            {
                if (IsEnabled && Component.Enabled && !rendering)
                {
                    Component.DebugEntity?.Enable <ModelComponent>(true, true);
                    rendering = true;
                }

                if (skinnedElement != null && skinnedElement.BoneIndex != -1 && skinnedElement.ColliderShape != null)
                {
                    Vector3    pos, scale;
                    Quaternion rot;

                    if (skeleton != null)
                    {
                        ContentEntity.Transform.UpdateWorldMatrix();
                        var isScalingNegative = false;
                        if (ContentEntity.Transform.WorldMatrix.Decompose(out scale, out rot, out pos))
                        {
                            isScalingNegative = scale.X * scale.Y * scale.Z < 0.0f;
                        }
                        skeleton.NodeTransformations[0].LocalMatrix       = ContentEntity.Transform.WorldMatrix;
                        skeleton.NodeTransformations[0].IsScalingNegative = isScalingNegative;
                        skeleton.UpdateMatrices();

                        skinnedElement.BoneWorldMatrixOut = skeleton.NodeTransformations[skinnedElement.BoneIndex].WorldMatrix;

                        if (skinnedElement.ColliderShape.LocalOffset != Vector3.Zero || skinnedElement.ColliderShape.LocalRotation != Quaternion.Identity)
                        {
                            skinnedElement.BoneWorldMatrixOut = Matrix.Multiply(skinnedElement.ColliderShape.PositiveCenterMatrix, skinnedElement.BoneWorldMatrixOut);
                        }
                    }

                    skinnedElement.BoneWorldMatrixOut.Decompose(out scale, out rot, out pos);
                    Component.DebugEntity.Transform.Position = pos;
                    Component.DebugEntity.Transform.Rotation = rot;

                    if (Component.CanScaleShape && Component.DebugEntity.Transform.Scale != scale)
                    {
                        Component.DebugEntity.Transform.Scale = scale;
                    }
                }
                else
                {
                    Vector3    scale, pos;
                    Quaternion rot;
                    ContentEntity.Transform.WorldMatrix.Decompose(out scale, out rot, out pos);
                    Component.DebugEntity.Transform.Position = pos;
                    Component.DebugEntity.Transform.Rotation = rot;

                    if (Component.CanScaleShape && Component.DebugEntity.Transform.Scale != scale)
                    {
                        Component.DebugEntity.Transform.Scale = scale;
                    }
                }
            }

            GizmoRootEntity.Transform.LocalMatrix = ContentEntity.Transform.WorldMatrix;
            GizmoRootEntity.Transform.UseTRS      = false;
        }
Exemple #2
0
        public override void Update()
        {
            if (ContentEntity == null)
            {
                return;
            }

            if ((!IsEnabled || !Component.Enabled) && Component.DebugEntity != null)
            {
                if (!rendering)
                {
                    return;
                }

                Component.DebugEntity.Enable <ModelComponent>(false, true);
                rendering = false;
                return;
            }

            // Create and add the element missing
            PhysicsElementInfo entityInfo;

            var modelComponent = ContentEntity.Get <ModelComponent>();

            if (!elementToEntity.TryGetValue(Component, out entityInfo) || entityInfo.HasChanged(Component))
            {
                //remove and clean up the old debug entity
                if (Component.DebugEntity != null)
                {
                    spawnedEntities.Remove(Component.DebugEntity);
                    Component.RemoveDebugEntity(EditorScene);
                    Component.DebugEntity = null;
                }

                //compose shape and fill data as data is not being filled by the processor when we run from the editor
                Component.ComposeShape();

                Component.AddDebugEntity(EditorScene, RenderGroup, true);
                if (Component.DebugEntity != null)
                {
                    spawnedEntities.Add(Component.DebugEntity);
                    Component.DebugEntity?.Enable <ModelComponent>(false, true);
                    rendering = false; //make sure we refresh enabled flags?
                }

                elementToEntity[Component] = new PhysicsElementInfo(Component);
            }

            if (Component.DebugEntity != null)
            {
                if (IsEnabled && Component.Enabled && !rendering)
                {
                    Component.DebugEntity?.Enable <ModelComponent>(true, true);
                    rendering = true;
                }

                Vector3    pos;
                Quaternion rot;
                ContentEntity.Transform.WorldMatrix.Decompose(out _, out rot, out pos);
                Component.DebugEntity.Transform.Position = pos;
                Component.DebugEntity.Transform.Rotation = rot;
            }

            GizmoRootEntity.Transform.LocalMatrix = ContentEntity.Transform.WorldMatrix;
            GizmoRootEntity.Transform.UseTRS      = false;
        }