Ejemplo n.º 1
0
        public void Draw(Matrix _viewProjection, Model isosphere, Texture2D tex)
        {
            //_spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, texFilterEffect, null);
            //_spriteBatch.Draw(tex, new Rectangle(0,0,256, 1536), Color.White);
            //_spriteBatch.End();
            ModelMeshPart meshpart = isosphere.Meshes[0].MeshParts[0];

            texFilterEffect.Parameters["WorldViewProj"].SetValue(Matrix.CreateScale(10) * _viewProjection);
            Texture.SetValue(tex);

            _graphics.SetRenderTarget(null);
            _graphics.RasterizerState = RasterizerState.CullCounterClockwise;
            _graphics.BlendState      = BlendState.Opaque;

            _graphics.SetVertexBuffer(meshpart.VertexBuffer);
            _graphics.Indices = (meshpart.IndexBuffer);
            int primitiveCount = meshpart.PrimitiveCount;
            int vertexOffset   = meshpart.VertexOffset;
            int startIndex     = meshpart.StartIndex;

            Shaders.EmissiveEffect.CurrentTechnique.Passes[0].Apply();

            texFilterEffect.CurrentTechnique.Passes[0].Apply();

            _graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, vertexOffset,
                                            startIndex, primitiveCount);
        }
Ejemplo n.º 2
0
 public override void UpdateView()
 {
     if (effect != null)
     {
         FreeLook freelook = Demo.Freelook;
         effect.View  = Matrix.CreateLookAt(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), MathHelper.Convert(freelook.Up));
         effect.View *= Matrix.CreateScale(-1, 1, 1);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Draws the individual <see cref="Komodo.Core.ECS.Components.Drawable2DComponent"/>.
        /// </summary>
        /// <param name="component"><see cref="Komodo.Core.ECS.Components.Drawable2DComponent"/> to render.</param>
        /// <param name="spriteBatch"><see cref="Microsoft.Xna.Framework.Graphics.SpriteBatch"/> to render with.</param>
        private void DrawComponent(Drawable2DComponent component, SpriteBatch spriteBatch)
        {
            var position = component.WorldPosition;
            var rotation = component.Rotation;
            var scale    = component.Scale;

            if (ActiveCamera != null)
            {
                if (component.IsBillboard)
                {
                    position = Vector3.Transform(
                        position,
                        ActiveCamera.ViewMatrix * Matrix.CreateScale(1f, -1f, 1f)
                        );
                }
                else
                {
                    position = Vector3.Transform(
                        position,
                        Matrix.CreateScale(1f, -1f, 1f)
                        );
                }
            }

            switch (component)
            {
            case SpriteComponent spriteComponent:
                spriteBatch.Draw(
                    spriteComponent.Texture.MonoGameTexture,
                    position.XY.MonoGameVector,
                    null,
                    Color.White,
                    -rotation.Z,
                    spriteComponent.Center.MonoGameVector,
                    scale.XY.MonoGameVector,
                    SpriteEffects.None,
                    position.Z
                    );
                break;

            case TextComponent textComponent:
                spriteBatch.DrawString(
                    textComponent.Font,
                    textComponent.Text,
                    position.XY.MonoGameVector,
                    textComponent.Color,
                    -rotation.Z,
                    textComponent.IsCentered ? textComponent.Center.MonoGameVector : Vector2.Zero.MonoGameVector,
                    scale.XY.MonoGameVector,
                    SpriteEffects.None,
                    position.Z
                    );
                break;
            }
        }
Ejemplo n.º 4
0
        public void ApplyTransformation()
        {
            if (_dynamicPhysicsObject == null)
            {
                RotationMatrix = Matrix.CreateRotationX((float)AngleX) * Matrix.CreateRotationY((float)AngleY) *
                                 Matrix.CreateRotationZ((float)AngleZ);
                Matrix scaleMatrix = Matrix.CreateScale(Scale);
                _worldOldMatrix = scaleMatrix * RotationMatrix * Matrix.CreateTranslation(Position);

                WorldTransform.Scale = Scale;
                WorldTransform.World = _worldOldMatrix;

                if (StaticPhysicsObject != null)
                {
                    AffineTransform change = new AffineTransform(
                        new BEPUutilities.Vector3(Scale.X, Scale.Y, Scale.Z),
                        Quaternion.CreateFromRotationMatrix(MathConverter.Convert(RotationMatrix)),
                        MathConverter.Convert(Position));

                    if (!MathConverter.Equals(change.Matrix, StaticPhysicsObject.WorldTransform.Matrix))
                    {
                        //StaticPhysicsMatrix = MathConverter.Copy(Change.Matrix);

                        StaticPhysicsObject.WorldTransform = change;
                    }
                }
            }
            else
            {
                //Has something changed?
                WorldTransform.Scale = Scale;
                _worldOldMatrix      = Extensions.CopyFromBepuMatrix(_worldOldMatrix, _dynamicPhysicsObject.WorldTransform);
                Matrix scaleMatrix = Matrix.CreateScale(Scale);
                //WorldOldMatrix = Matrix.CreateScale(Scale)*WorldOldMatrix;
                WorldTransform.World = scaleMatrix * _worldOldMatrix;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Renders the given <see cref="Komodo.Core.ECS.Components.Drawable3DComponent"/>.
        /// </summary>
        /// <param name="component"><see cref="Komodo.Core.ECS.Components.Drawable3DComponent"/> to render.</param>
        private void DrawComponent(Drawable3DComponent component)
        {
            var position           = component.WorldPosition;
            var rotationQuaternion = component.RotationQuaternion;
            var scale = component.Scale;

            if (ActiveCamera != null)
            {
                var bounds = component.BoundingBox;
                if (ActiveCamera.BoundingFrustum.Contains(bounds) == ContainmentType.Disjoint)
                {
                    return;
                }
            }
            var positionMatrix = Matrix.CreateTranslation(position.MonoGameVector);

            foreach (var mesh in component.ModelData.MonoGameModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.Texture        = component.Texture?.MonoGameTexture;
                    effect.TextureEnabled = effect.Texture != null;
                    effect.DiffuseColor   = component.DiffuseColor.ToVector3();

                    effect.Projection = ActiveCamera != null ? ActiveCamera.Projection : Matrix.Identity;
                    effect.View       = ActiveCamera != null ? ActiveCamera.ViewMatrix : Matrix.Identity;
                    effect.World      = (
                        Matrix.CreateScale(scale.MonoGameVector)
                        * Matrix.CreateFromQuaternion(rotationQuaternion)
                        * positionMatrix
                        );
                }

                mesh.Draw();
            }
        }
Ejemplo n.º 6
0
        public void SetLocalTransform(AnimatableProperty <XNAV3> s, AnimatableProperty <XNAQUAT> r, AnimatableProperty <XNAV3> t)
        {
            var ss = s != null && s.IsAnimated;
            var rr = r != null && r.IsAnimated;
            var tt = t != null && t.IsAnimated;

            if (!(ss || rr || tt))
            {
                _UseAnimatedTransforms = false;
                _LocalScale            = null;
                _LocalRotation         = null;
                _LocalTranslation      = null;
                return;
            }

            _UseAnimatedTransforms = true;
            _LocalScale            = s;
            _LocalRotation         = r;
            _LocalTranslation      = t;

            var m = XNAMAT.Identity;

            if (s != null)
            {
                m *= XNAMAT.CreateScale(s.Value);
            }
            if (r != null)
            {
                m *= XNAMAT.CreateFromQuaternion(r.Value);
            }
            if (t != null)
            {
                m.Translation = t.Value;
            }
            _LocalMatrix = m;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Renders the given <see cref="Komodo.Core.ECS.Components.Drawable2DComponent"/> objects.
        /// </summary>
        /// <param name="components"><see cref="Komodo.Core.ECS.Components.Drawable2DComponent"/> objects to render.</param>
        /// <param name="spriteBatch"><see cref="Microsoft.Xna.Framework.Graphics.SpriteBatch"/> to render with.</param>
        /// <param name="drawBillboards">Whether or not to billboard the <see cref="Komodo.Core.ECS.Components.Drawable2DComponent"/>.</param>
        /// <param name="shader">Shader to render with.</param>
        private void DrawComponents(
            [NotNull] IEnumerable <Drawable2DComponent> components,
            [NotNull] SpriteBatch spriteBatch,
            bool drawBillboards = false,
            Effect shader       = null
            )
        {
            var oldViewMatrix  = Matrix.Identity;
            var oldWorldMatrix = Matrix.Identity;

            switch (shader)
            {
            case BasicEffect effect:
                oldViewMatrix  = effect.View;
                oldWorldMatrix = effect.World;
                break;

            case SpriteEffect _:
            case null:
            default:
                break;
            }
            try
            {
                switch (shader)
                {
                case BasicEffect effect:
                    effect.Projection = ActiveCamera != null ? ActiveCamera.Projection : Matrix.Identity;

                    if (drawBillboards)
                    {
                        effect.View = Matrix.Identity;
                    }
                    else
                    {
                        effect.View = ActiveCamera != null ? ActiveCamera.ViewMatrix : Matrix.Identity;
                    }
                    if (ActiveCamera != null && ActiveCamera.IsPerspective)
                    {
                        effect.World = Matrix.CreateScale(1f, -1f, 1f);
                    }
                    else
                    {
                        effect.World = Matrix.CreateScale(1f, 1f, 1f);
                    }
                    break;

                case SpriteEffect _:
                case null:
                default:
                    break;
                }
                spriteBatch.Begin(
                    SpriteSortMode.FrontToBack,
                    null,
                    TextureFilter,
                    DepthStencilState.DepthRead,
                    RasterizerState.CullNone,
                    shader
                    );
                foreach (var component in components)
                {
                    if (component.IsEnabled && component.Parent != null && component.Parent.IsEnabled && component.IsBillboard == drawBillboards)
                    {
                        DrawComponent(component, spriteBatch);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
            }
            finally
            {
                spriteBatch.End();
                switch (shader)
                {
                case BasicEffect effect:
                    effect.View  = oldViewMatrix;
                    effect.World = oldWorldMatrix;
                    break;

                case SpriteEffect _:
                case null:
                default:
                    break;
                }
            }
        }
Ejemplo n.º 8
0
 public static Matrix CreateScale(Vector3 scale)
 {
     return(new Matrix(Matrix4x4.CreateScale(scale)));
 }