Exemplo n.º 1
0
        /// <summary>
        /// Renders a 3D line using texture UVs.
        /// </summary>
        /// <param name="context">
        /// The rendering context.
        /// </param>
        /// <param name="start">
        /// The start of the line.
        /// </param>
        /// <param name="end">
        /// The end of the line.
        /// </param>
        /// <param name="texture">
        /// The texture to use.
        /// </param>
        /// <param name="startUV">
        /// The UV for the start of the line.
        /// </param>
        /// <param name="endUV">
        /// The UV for the end of the line.
        /// </param>
        public void RenderLine(
            IRenderContext context,
            Vector3 start,
            Vector3 end,
            TextureAsset texture,
            Vector2 startUV,
            Vector2 endUV)
        {
            if (!context.Is3DContext)
            {
                throw new InvalidOperationException("Can't use 3D rendering utilities in 2D context.");
            }

            context.EnableTextures();
            context.SetActiveTexture(texture.Texture);

            var vertexes = new[] { new VertexPositionTexture(start, startUV), new VertexPositionTexture(end, endUV) };
            var indicies = new short[] { 0, 1 };

            foreach (var pass in context.Effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                context.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineList, vertexes, 0, 2, indicies, 0, 1);
            }
        }
Exemplo n.º 2
0
 public void SetCompiledData(
     TextureAsset texture,
     Dictionary <string, UVMapping> uvMappings)
 {
     this.AtlasTexture = texture;
     this.Mappings     = uvMappings;
     this.SourceOnly   = false;
 }
Exemplo n.º 3
0
        /// <summary>
        /// The render texture.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <param name="matrix">
        /// The matrix.
        /// </param>
        /// <param name="texture">
        /// The texture.
        /// </param>
        /// <param name="color">
        /// The color.
        /// </param>
        /// <param name="flipHorizontally">
        /// The flip horizontally.
        /// </param>
        /// <param name="flipVertically">
        /// The flip vertically.
        /// </param>
        /// <param name="sourceArea">
        /// The source area.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public void RenderTexture(
            IRenderContext context,
            Matrix matrix,
            TextureAsset texture,
            Color?color           = null,
            bool flipHorizontally = false,
            bool flipVertically   = false,
            Rectangle?sourceArea  = null)
        {
            if (!context.Is3DContext)
            {
                throw new InvalidOperationException("Can't use 3D rendering utilities in 2D context.");
            }

            this.RenderTexture(context, matrix, texture.Texture, color, flipHorizontally, flipVertically, sourceArea);
        }
Exemplo n.º 4
0
        public TextureAtlasAsset(
            IAssetContentManager assetContentManager,
            string name,
            PlatformData data)
        {
            this.Name         = name;
            this.SourceOnly   = false;
            this.CompiledOnly = true;

            var memory = new MemoryStream(data.Data);

            using (var reader = new BinaryReader(memory))
            {
                var textureSize         = reader.ReadInt32();
                var texturePlatformData = reader.ReadBytes(textureSize);

                var textureAsset = new TextureAsset(
                    assetContentManager,
                    name,
                    null,
                    new PlatformData
                {
                    Platform = data.Platform,
                    Data     = texturePlatformData
                },
                    false);
                this.AtlasTexture = textureAsset;

                this.Mappings = new Dictionary <string, UVMapping>();
                var uvMappingCount = reader.ReadInt32();
                for (var i = 0; i < uvMappingCount; i++)
                {
                    var mappingName  = reader.ReadString();
                    var topLeftU     = reader.ReadSingle();
                    var topLeftV     = reader.ReadSingle();
                    var bottomRightU = reader.ReadSingle();
                    var bottomRightV = reader.ReadSingle();
                    this.Mappings.Add(
                        mappingName,
                        new UVMapping
                    {
                        TopLeft     = new Vector2(topLeftU, topLeftV),
                        BottomRight = new Vector2(bottomRightU, bottomRightV)
                    });
                }
            }
        }
Exemplo n.º 5
0
        public void RenderPlane(IRenderContext context, Matrix transform, TextureAsset texture, Vector2 topLeftUV,
                                Vector2 bottomRightUV)
        {
            if (!context.Is3DContext)
            {
                throw new InvalidOperationException("Can't use 3D rendering utilities in 2D context.");
            }

            var vertexes = new[]
            {
                new VertexPositionNormalTexture(new Vector3(0, 0, 0), new Vector3(0, -1, 0), new Vector2(topLeftUV.X, topLeftUV.Y)),
                new VertexPositionNormalTexture(new Vector3(0, 0, 1), new Vector3(0, -1, 0), new Vector2(topLeftUV.X, bottomRightUV.Y)),
                new VertexPositionNormalTexture(new Vector3(1, 0, 0), new Vector3(0, -1, 0), new Vector2(bottomRightUV.X, topLeftUV.Y)),
                new VertexPositionNormalTexture(new Vector3(1, 0, 1), new Vector3(0, -1, 0), new Vector2(bottomRightUV.X, bottomRightUV.Y)),
            };

            var indicies = new short[]
            {
                0, 2, 1,
                3, 1, 2,
            };

            context.EnableTextures();
            context.SetActiveTexture(texture.Texture);

            var world = context.World;

            context.World = transform;

            foreach (var pass in context.Effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                context.GraphicsDevice.DrawUserIndexedPrimitives(
                    PrimitiveType.TriangleList,
                    vertexes,
                    0,
                    vertexes.Length,
                    indicies,
                    0,
                    indicies.Length / 3);
            }

            context.World = world;
        }
        public void RenderTexture(
            IRenderContext context,
            IEffect effect,
            IEffectParameterSet effectParameterSet,
            Matrix matrix,
            TextureAsset texture,
            Color?color,
            bool flipHorizontally,
            bool flipVertically,
            Rectangle?sourceArea)
        {
            if (!context.IsCurrentRenderPass <I3DRenderPass>())
            {
                throw new InvalidOperationException("Can't use 3D rendering utilities in 2D context.");
            }

            RenderTexture(context, effect, effectParameterSet, matrix, texture.Texture, color, flipHorizontally, flipVertically, sourceArea);
        }
 /// <summary>
 /// Renders a texture at the specified position.
 /// </summary>
 /// <param name="context">
 /// The rendering context.
 /// </param>
 /// <param name="position">
 /// The position to render the texture.
 /// </param>
 /// <param name="texture">
 /// The texture.
 /// </param>
 /// <param name="size">
 /// The size to render the texture as (defaults to the texture size).
 /// </param>
 /// <param name="color">
 /// The colorization to apply to the texture.
 /// </param>
 /// <param name="rotation">
 /// The rotation to apply to the texture.
 /// </param>
 /// <param name="flipHorizontally">
 /// If set to <c>true</c> the texture is flipped horizontally.
 /// </param>
 /// <param name="flipVertically">
 /// If set to <c>true</c> the texture is flipped vertically.
 /// </param>
 /// <param name="sourceArea">
 /// The source area of the texture (defaults to the full texture).
 /// </param>
 public void RenderTexture(
     IRenderContext context,
     Vector2 position,
     TextureAsset texture,
     Vector2?size          = null,
     Color?color           = null,
     float rotation        = 0,
     bool flipHorizontally = false,
     bool flipVertically   = false,
     Rectangle?sourceArea  = null)
 {
     RenderTexture(
         context,
         position,
         texture.Texture,
         size,
         color,
         rotation,
         flipHorizontally,
         flipVertically,
         sourceArea);
 }
        /// <summary>
        /// Renders a texture at the specified position.
        /// </summary>
        /// <param name="context">
        /// The rendering context.
        /// </param>
        /// <param name="position">
        /// The position to render the texture.
        /// </param>
        /// <param name="texture">
        /// The texture.
        /// </param>
        /// <param name="size">
        /// The size to render the texture as (defaults to the texture size).
        /// </param>
        /// <param name="color">
        /// The colorization to apply to the texture.
        /// </param>
        /// <param name="rotation">
        /// The rotation to apply to the texture.
        /// </param>
        /// <param name="flipHorizontally">
        /// If set to <c>true</c> the texture is flipped horizontally.
        /// </param>
        /// <param name="flipVertically">
        /// If set to <c>true</c> the texture is flipped vertically.
        /// </param>
        /// <param name="sourceArea">
        /// The source area of the texture (defaults to the full texture).
        /// </param>
        public void RenderTexture(
            IRenderContext context,
            Vector2 position,
            TextureAsset texture,
            Vector2?size          = null,
            Color?color           = null,
            float rotation        = 0,
            bool flipHorizontally = false,
            bool flipVertically   = false,
            Rectangle?sourceArea  = null)
        {
            if (size == null)
            {
                size = new Vector2(texture.Texture.Width, texture.Texture.Height);
            }

            if (color == null)
            {
                color = Color.White;
            }

            var effects =
                (SpriteEffects)
                ((int)(flipHorizontally ? SpriteEffects.FlipHorizontally : SpriteEffects.None)
                 + (int)(flipVertically ? SpriteEffects.FlipVertically : SpriteEffects.None));

            context.SpriteBatch.Draw(
                texture.Texture,
                new Rectangle((int)position.X, (int)position.Y, (int)size.Value.X, (int)size.Value.Y),
                sourceArea,
                color.Value.ToPremultiplied(),
                rotation,
                new Vector2(0, 0),
                effects,
                0);
        }
        public void RenderLine(IRenderContext context, IEffect effect, IEffectParameterSet effectParameterSet, Vector3 start, Vector3 end, TextureAsset texture, Vector2 startUV, Vector2 endUV)
        {
            if (!context.IsCurrentRenderPass <I3DRenderPass>())
            {
                throw new InvalidOperationException("Can't use 3D rendering utilities in 2D context.");
            }

            var vertexes = _renderCache.GetOrSet(
                "renderlinetex3dvb:" + start + ":" + end + ":" + startUV + ":" + endUV,
                () =>
            {
                var vb = new VertexBuffer(context.GraphicsDevice, VertexPositionTexture.VertexDeclaration, 2,
                                          BufferUsage.WriteOnly);
                vb.SetData(new[] { new VertexPositionTexture(start, startUV), new VertexPositionTexture(end, endUV) });
                return(vb);
            });
            var indicies = _renderCache.GetOrSet(
                "renderline3dib",
                () =>
            {
                var ib = new IndexBuffer(context.GraphicsDevice, IndexElementSize.SixteenBits, 2,
                                         BufferUsage.WriteOnly);
                ib.SetData(new short[] { 0, 1 });
                return(ib);
            });

            context.GraphicsDevice.SetVertexBuffer(vertexes);
            context.GraphicsDevice.Indices = indicies;

            var semantic = effectParameterSet.GetSemantic <ITextureEffectSemantic>();

            if (semantic.Texture != texture.Texture)
            {
                semantic.Texture = texture.Texture;
            }

            effect.LoadParameterSet(context, effectParameterSet);
            foreach (var pass in effect.NativeEffect.CurrentTechnique.Passes)
            {
                pass.Apply();

                context.GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, 1);
            }
        }
Exemplo n.º 10
0
        public void RenderPlane(IRenderContext context, IEffect effect, IEffectParameterSet effectParameterSet, Matrix transform, TextureAsset texture, Vector2 topLeftUV, Vector2 bottomRightUV)
        {
            if (!context.IsCurrentRenderPass <I3DRenderPass>())
            {
                throw new InvalidOperationException("Can't use 3D rendering utilities in 2D context.");
            }

            var vertexes = new[]
            {
                new VertexPositionNormalTexture(new Vector3(0, 0, 0), new Vector3(0, -1, 0), new Vector2(topLeftUV.X, topLeftUV.Y)),
                new VertexPositionNormalTexture(new Vector3(0, 0, 1), new Vector3(0, -1, 0), new Vector2(topLeftUV.X, bottomRightUV.Y)),
                new VertexPositionNormalTexture(new Vector3(1, 0, 0), new Vector3(0, -1, 0), new Vector2(bottomRightUV.X, topLeftUV.Y)),
                new VertexPositionNormalTexture(new Vector3(1, 0, 1), new Vector3(0, -1, 0), new Vector2(bottomRightUV.X, bottomRightUV.Y))
            };

            var indicies = new short[]
            {
                0, 2, 1,
                3, 1, 2
            };

            var semantic = effectParameterSet.GetSemantic <ITextureEffectSemantic>();

            if (semantic.Texture != texture.Texture)
            {
                semantic.Texture = texture.Texture;
            }

            var world = context.World;

            context.World = transform;

            effect.LoadParameterSet(context, effectParameterSet);
            foreach (var pass in effect.NativeEffect.CurrentTechnique.Passes)
            {
                pass.Apply();

                context.GraphicsDevice.DrawUserIndexedPrimitives(
                    PrimitiveType.TriangleList,
                    vertexes,
                    0,
                    vertexes.Length,
                    indicies,
                    0,
                    indicies.Length / 3);
            }

            context.World = world;
        }
Exemplo n.º 11
0
        public void Compile(TextureAtlasAsset asset, TargetPlatform platform)
        {
            var textures = asset.AssetManager
                           .GetAllNames()
                           .Where(x => asset.SourceTextureNames.Contains(x))
                           .Select(x => asset.AssetManager.Get <TextureAsset>(x))
                           .ToList();

            var textureFallbacks = new Dictionary <TextureAsset, Bitmap>();
            var texturePacker    = new TexturePacker <TextureAsset>();

            foreach (var texture in textures)
            {
                if (texture.PlatformData == null)
                {
                    this.m_TextureAssetCompiler.Compile(texture, platform);

                    texture.ReloadTexture();
                }

                // We can end up with textures that have no Texture set
                // here if we are running inside the compilation tool (where
                // there's no graphics device to load textures).  In that
                // case we fall back to loading the texture's raw data with
                // System.Drawing.Bitmap.
                float width, height;
                if (texture.Texture == null)
                {
                    if (texture.CompiledOnly)
                    {
                        throw new InvalidOperationException("No graphics device service and no source data for texture.");
                    }

                    var fallback = new Bitmap(new MemoryStream(texture.RawData));
                    width  = fallback.Width;
                    height = fallback.Height;
                    textureFallbacks.Add(texture, fallback);
                }
                else
                {
                    width  = texture.Texture.Width;
                    height = texture.Texture.Height;
                }

                texturePacker.AddTexture(
                    new Vector2(width + this.PixelOverscan * 2, height + this.PixelOverscan * 2),
                    texture);
            }

            Vector2 size;
            var     packedTextures = texturePacker.Pack(out size);
            var     bitmap         = new Bitmap((int)size.X, (int)size.Y);

            foreach (var packedTexture in packedTextures)
            {
                if (packedTexture.Texture.Texture == null)
                {
                    if (!textureFallbacks.ContainsKey(packedTexture.Texture))
                    {
                        throw new InvalidOperationException();
                    }

                    var fallback = textureFallbacks[packedTexture.Texture];
                    for (var x = -this.PixelOverscan; x < fallback.Width + this.PixelOverscan; x++)
                    {
                        for (var y = -this.PixelOverscan; y < fallback.Height + this.PixelOverscan; y++)
                        {
                            bitmap.SetPixel(
                                (int)packedTexture.Position.X + this.PixelOverscan + x,
                                (int)packedTexture.Position.Y + this.PixelOverscan + y,
                                fallback.GetPixel(
                                    MathHelper.Clamp(x, 0, fallback.Width - 1),
                                    MathHelper.Clamp(y, 0, fallback.Height - 1)));
                        }
                    }
                }
                else
                {
                    var data = new Microsoft.Xna.Framework.Color[packedTexture.Texture.Texture.Width * packedTexture.Texture.Texture.Height];
                    packedTexture.Texture.Texture.GetData(data);

                    for (var x = -this.PixelOverscan; x < packedTexture.Texture.Texture.Width + this.PixelOverscan; x++)
                    {
                        for (var y = -this.PixelOverscan; y < packedTexture.Texture.Texture.Height + this.PixelOverscan; y++)
                        {
                            var ax = MathHelper.Clamp(x, 0, packedTexture.Texture.Texture.Width - 1);
                            var ay = MathHelper.Clamp(y, 0, packedTexture.Texture.Texture.Height - 1);

                            bitmap.SetPixel(
                                (int)packedTexture.Position.X + this.PixelOverscan + x,
                                (int)packedTexture.Position.Y + this.PixelOverscan + y,
                                System.Drawing.Color.FromArgb(
                                    data[ax + ay * packedTexture.Texture.Texture.Width].A,
                                    data[ax + ay * packedTexture.Texture.Texture.Width].R,
                                    data[ax + ay * packedTexture.Texture.Texture.Width].G,
                                    data[ax + ay * packedTexture.Texture.Texture.Width].B));
                        }
                    }
                }
            }

            byte[] bitmapData;
            using (var stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Png);
                var length = stream.Position;
                stream.Seek(0, SeekOrigin.Begin);
                bitmapData = new byte[length];
                stream.Read(bitmapData, 0, (int)length);
            }

            var textureAsset = new TextureAsset(
                this.m_AssetContentManager,
                asset.Name,
                bitmapData,
                null,
                true);

            this.m_TextureAssetCompiler.Compile(
                textureAsset,
                platform);

            var uvMappings  = new Dictionary <string, UVMapping>();
            var pixelVector = new Vector2(this.PixelOverscan, this.PixelOverscan);

            foreach (var texture in packedTextures)
            {
                uvMappings.Add(
                    texture.Texture.Name,
                    new UVMapping
                {
                    TopLeft     = (texture.Position + pixelVector) / new Vector2(size.X, size.Y),
                    BottomRight = (texture.Position + texture.Size - pixelVector) / new Vector2(size.X, size.Y),
                });
            }

            asset.SetCompiledData(
                textureAsset,
                uvMappings);
        }
Exemplo n.º 12
0
 public void RenderPlane(IRenderContext context, IEffect effect, IEffectParameterSet effectParameterSet, Matrix transform,
                         TextureAsset texture, Vector2 topLeftUV, Vector2 bottomRightUV)
 {
     throw new NotSupportedException();
 }
Exemplo n.º 13
0
 public void RenderTexture(IRenderContext context, IEffect effect, IEffectParameterSet effectParameterSet, Matrix matrix,
                           TextureAsset texture, Color?color = null, bool flipHorizontally = false, bool flipVertically = false,
                           Rectangle?sourceArea = null)
 {
     throw new NotSupportedException();
 }
Exemplo n.º 14
0
 public void RenderLine(IRenderContext context, IEffect effect, IEffectParameterSet effectParameterSet, Vector3 start,
                        Vector3 end, TextureAsset texture, Vector2 startUV, Vector2 endUV)
 {
     throw new NotSupportedException();
 }
Exemplo n.º 15
0
        private void UpdateCachedModel(IMaterial material, ref bool changedRenderRequest, ref string changedRenderRequestBy)
        {
            if (_lastCachedModel != _model)
            {
                changedRenderRequest    = true;
                changedRenderRequestBy += ":model";

                if (material.TextureDiffuse != null)
                {
                    if (material.TextureDiffuse.TextureAsset != null)
                    {
                        _lastCachedDiffuseTexture = material.TextureDiffuse.TextureAsset;
                    }
                    else
                    {
                        _lastCachedDiffuseTexture =
                            _textureFromHintPath.GetTextureFromHintPath(material.TextureDiffuse);
                    }

                    if (material.TextureNormal != null)
                    {
                        if (material.TextureNormal.TextureAsset != null)
                        {
                            _lastCachedNormalMapTexture = material.TextureNormal.TextureAsset;
                        }
                        else
                        {
                            _lastCachedNormalMapTexture =
                                _textureFromHintPath.GetTextureFromHintPath(material.TextureNormal);
                        }
                    }
                    else
                    {
                        _lastCachedNormalMapTexture = null;
                    }

                    if (material.PowerSpecular != null)
                    {
                        _lastCachedSpecularPower = material.PowerSpecular.Value;

                        if (material.TextureSpecular != null)
                        {
                            if (material.TextureSpecular.TextureAsset != null)
                            {
                                _lastCachedSpecularColorMapTexture = material.TextureSpecular.TextureAsset;
                            }
                            else
                            {
                                _lastCachedSpecularColorMapTexture =
                                    _textureFromHintPath.GetTextureFromHintPath(material.TextureSpecular);
                            }
                        }
                        else if (material.ColorSpecular != null)
                        {
                            _lastCachedSpecularColor = material.ColorSpecular.Value;
                        }
                        else
                        {
                            _lastCachedSpecularColor = null;
                        }
                    }
                    else
                    {
                        _lastCachedSpecularPower = null;
                    }

                    _mode = "texture";
                }
                else if (material.ColorDiffuse != null)
                {
                    _mode = "diffuse";
                }
                else
                {
                    _mode = "color";
                }
                _lastCachedModel = _model;
            }
        }
Exemplo n.º 16
0
        public void Render(ComponentizedEntity entity, IGameContext gameContext, IRenderContext renderContext)
        {
            if (!Enabled)
            {
                return;
            }

            if (renderContext.IsCurrentRenderPass <I3DRenderPass>())
            {
                if (Effect == null)
                {
                    _useDefaultEffects = true;
                }
                else
                {
                    _useDefaultEffects = false;
                }

                if (_useDefaultEffects && _uberEffectAsset == null)
                {
                    _uberEffectAsset = _assetManager.Get <UberEffectAsset>("effect.BuiltinSurface");
                }

                if (Model != null)
                {
                    var matrix = FinalTransform.AbsoluteMatrix;

                    bool   changedRenderRequest   = _lastWorldMatrix != matrix;
                    string changedRenderRequestBy = changedRenderRequest ? "matrix" : "";

                    var material = OverrideMaterial ?? _model.Material;

                    UpdateCachedModel(material, ref changedRenderRequest, ref changedRenderRequestBy);

                    var effect = GetEffect(ref changedRenderRequest, ref changedRenderRequestBy);

                    var parameterSet = GetEffectParameterSet(material, ref changedRenderRequest, ref changedRenderRequestBy);

                    var animation = GetModelAnimation(ref changedRenderRequest, ref changedRenderRequestBy);

                    if (animation != null)
                    {
                        animation.Apply(_model, _animationTracker.ElapsedMilliseconds / 1000f, 0.5f);

                        _renderRequest = _model.CreateRenderRequest(renderContext, effect, parameterSet, matrix);
                    }
                    else if (changedRenderRequest || _renderRequest == null)
                    {
                        _renderRequest = _model.CreateRenderRequest(renderContext, effect, parameterSet, matrix);
                    }

                    _lastWorldMatrix = matrix;

                    _renderBatcher.QueueRequest(
                        renderContext,
                        _renderRequest);
                }
                else
                {
                    _lastCachedModel          = null;
                    _lastCachedDiffuseTexture = null;
                }
            }
        }
Exemplo n.º 17
0
 public void RenderTexture(IRenderContext context, Vector2 position, TextureAsset texture, Vector2?size = null,
                           Color?color          = null, float rotation = 0, Vector2?rotationAnchor = null, bool flipHorizontally = false, bool flipVertically = false,
                           Rectangle?sourceArea = null)
 {
     throw new NotSupportedException();
 }