Rendering Sprites with Shaders
Last updated
Was this helpful?
Was this helpful?
using FlatRedBall;
using FlatRedBall.Graphics;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShaderProject
{
public class CustomShaderSprite : PositionedObject, IDrawableBatch
{
Sprite sprite;
Effect effect;
short[] indexData = new short[]
{
0,1,2,
0,2,3
};
public Texture2D Texture { get; set; }
public bool UpdateEveryFrame
{
get
{
return true;
}
}
public CustomShaderSprite (Effect effect)
{
sprite = new Sprite();
sprite.TextureScale = 1;
this.effect = effect;
}
public void Destroy()
{
}
public void Draw(Camera camera)
{
camera.SetDeviceViewAndProjection(effect, relativeToCamera: false);
var textureParameter = effect.Parameters["CurrentTexture"];
textureParameter.SetValue(Texture);
effect.CurrentTechnique = effect.Techniques["TexturePixelShader_Point"];
var graphicsDevice = FlatRedBallServices.GraphicsDevice;
foreach(var pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
graphicsDevice.DrawUserIndexedPrimitives(
// Render a triangle:
PrimitiveType.TriangleList,
// The array of verts that we want to render
sprite.VerticesForDrawing,
// The offset, which is 0 since we want to start
// at the beginning of the verts array
0,
4,
indexData,
0,
2);
}
}
public void Update()
{
sprite.Position = this.Position;
sprite.RotationMatrix = this.RotationMatrix;
sprite.Texture = Texture;
SpriteManager.ManualUpdate(sprite);
}
}
}public partial class GameScreen
{
CustomShaderSprite customShaderSprite;
void CustomInitialize()
{
customShaderSprite = new CustomShaderSprite(Shader);
customShaderSprite.Texture = TextureFile;
SpriteManager.AddDrawableBatch(customShaderSprite);
}
void CustomActivity(bool firstTimeCalled)
{
}
void CustomDestroy()
{
SpriteManager.RemoveDrawableBatch(customShaderSprite);
}
static void CustomLoadStaticContent(string contentManagerName)
{
}
}