TexturedAnimatedQuadExample
parent
dd8a416052
commit
7fe6fba2d7
|
@ -0,0 +1,164 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using MoonWorks;
|
||||
using MoonWorks.Graphics;
|
||||
using MoonWorks.Input;
|
||||
using MoonWorks.Math.Float;
|
||||
|
||||
namespace MoonWorksGraphicsTests;
|
||||
|
||||
class TexturedAnimatedQuadExample : Example
|
||||
{
|
||||
private GraphicsPipeline Pipeline;
|
||||
private GpuBuffer VertexBuffer;
|
||||
private GpuBuffer IndexBuffer;
|
||||
private Texture Texture;
|
||||
private Sampler Sampler;
|
||||
|
||||
private float t;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct FragmentUniforms
|
||||
{
|
||||
public Vector4 MultiplyColor;
|
||||
|
||||
public FragmentUniforms(Vector4 multiplyColor)
|
||||
{
|
||||
MultiplyColor = multiplyColor;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs)
|
||||
{
|
||||
Window = window;
|
||||
GraphicsDevice = graphicsDevice;
|
||||
|
||||
Window.SetTitle("TexturedAnimatedQuad");
|
||||
|
||||
// Load the shaders
|
||||
Shader vertShader = new Shader(
|
||||
GraphicsDevice,
|
||||
TestUtils.GetShaderPath("TexturedQuadWithMatrix.vert"),
|
||||
"main",
|
||||
ShaderStage.Vertex,
|
||||
ShaderFormat.SPIRV
|
||||
);
|
||||
|
||||
Shader fragShader = new Shader(
|
||||
GraphicsDevice,
|
||||
TestUtils.GetShaderPath("TexturedQuadWithMultiplyColor.frag"),
|
||||
"main",
|
||||
ShaderStage.Fragment,
|
||||
ShaderFormat.SPIRV
|
||||
);
|
||||
|
||||
// Create the graphics pipeline
|
||||
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
|
||||
Window.SwapchainFormat,
|
||||
vertShader,
|
||||
fragShader
|
||||
);
|
||||
pipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions[0].BlendState = ColorAttachmentBlendState.AlphaBlend;
|
||||
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
|
||||
pipelineCreateInfo.VertexShaderResourceInfo = new GraphicsPipelineResourceInfo
|
||||
{
|
||||
UniformBufferCount = 1
|
||||
};
|
||||
pipelineCreateInfo.FragmentShaderResourceInfo = new GraphicsPipelineResourceInfo
|
||||
{
|
||||
SamplerCount = 1,
|
||||
UniformBufferCount = 1
|
||||
};
|
||||
Pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
||||
|
||||
Sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
|
||||
|
||||
// Create and populate the GPU resources
|
||||
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
||||
|
||||
VertexBuffer = resourceUploader.CreateBuffer(
|
||||
[
|
||||
new PositionTextureVertex(new Vector3(-0.5f, -0.5f, 0), new Vector2(0, 0)),
|
||||
new PositionTextureVertex(new Vector3(0.5f, -0.5f, 0), new Vector2(1, 0)),
|
||||
new PositionTextureVertex(new Vector3(0.5f, 0.5f, 0), new Vector2(1, 1)),
|
||||
new PositionTextureVertex(new Vector3(-0.5f, 0.5f, 0), new Vector2(0, 1)),
|
||||
],
|
||||
BufferUsageFlags.Vertex
|
||||
);
|
||||
|
||||
IndexBuffer = resourceUploader.CreateBuffer<ushort>(
|
||||
[
|
||||
0, 1, 2,
|
||||
0, 2, 3,
|
||||
],
|
||||
BufferUsageFlags.Index
|
||||
);
|
||||
|
||||
Texture = resourceUploader.CreateTexture2DFromCompressed(TestUtils.GetTexturePath("ravioli.png"));
|
||||
|
||||
resourceUploader.Upload();
|
||||
resourceUploader.Dispose();
|
||||
}
|
||||
|
||||
public override void Update(System.TimeSpan delta)
|
||||
{
|
||||
t += (float) delta.TotalSeconds;
|
||||
}
|
||||
|
||||
public override void Draw(double alpha)
|
||||
{
|
||||
TransformVertexUniform vertUniforms;
|
||||
FragmentUniforms fragUniforms;
|
||||
|
||||
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
|
||||
Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window);
|
||||
if (swapchainTexture != null)
|
||||
{
|
||||
var renderPass = cmdbuf.BeginRenderPass(new ColorAttachmentInfo(swapchainTexture, false, Color.Black));
|
||||
renderPass.BindGraphicsPipeline(Pipeline);
|
||||
renderPass.BindVertexBuffer(VertexBuffer);
|
||||
renderPass.BindIndexBuffer(IndexBuffer, IndexElementSize.Sixteen);
|
||||
renderPass.BindFragmentSampler(new TextureSamplerBinding(Texture, Sampler));
|
||||
|
||||
// Top-left
|
||||
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ(t) * Matrix4x4.CreateTranslation(new Vector3(-0.5f, -0.5f, 0)));
|
||||
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Sin(t) * 0.5f, 1f, 1f));
|
||||
renderPass.PushVertexUniformData(vertUniforms);
|
||||
renderPass.PushFragmentUniformData(fragUniforms);
|
||||
renderPass.DrawIndexedPrimitives(0, 0, 2);
|
||||
|
||||
// Top-right
|
||||
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ((2 * System.MathF.PI) - t) * Matrix4x4.CreateTranslation(new Vector3(0.5f, -0.5f, 0)));
|
||||
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Cos(t) * 0.5f, 1f, 1f));
|
||||
renderPass.PushVertexUniformData(vertUniforms);
|
||||
renderPass.PushFragmentUniformData(fragUniforms);
|
||||
renderPass.DrawIndexedPrimitives(0, 0, 2);
|
||||
|
||||
// Bottom-left
|
||||
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ(t) * Matrix4x4.CreateTranslation(new Vector3(-0.5f, 0.5f, 0)));
|
||||
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Sin(t) * 0.2f, 1f, 1f));
|
||||
renderPass.PushVertexUniformData(vertUniforms);
|
||||
renderPass.PushFragmentUniformData(fragUniforms);
|
||||
renderPass.DrawIndexedPrimitives(0, 0, 2);
|
||||
|
||||
// Bottom-right
|
||||
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ(t) * Matrix4x4.CreateTranslation(new Vector3(0.5f, 0.5f, 0)));
|
||||
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Cos(t) * 1f, 1f, 1f));
|
||||
renderPass.PushVertexUniformData(vertUniforms);
|
||||
renderPass.PushFragmentUniformData(fragUniforms);
|
||||
renderPass.DrawIndexedPrimitives(0, 0, 2);
|
||||
|
||||
cmdbuf.EndRenderPass(renderPass);
|
||||
}
|
||||
|
||||
GraphicsDevice.Submit(cmdbuf);
|
||||
}
|
||||
|
||||
public override void Destroy()
|
||||
{
|
||||
Pipeline.Dispose();
|
||||
VertexBuffer.Dispose();
|
||||
IndexBuffer.Dispose();
|
||||
Texture.Dispose();
|
||||
Sampler.Dispose();
|
||||
}
|
||||
}
|
|
@ -1,134 +0,0 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using MoonWorks.Graphics;
|
||||
using MoonWorks.Math.Float;
|
||||
|
||||
namespace MoonWorks.Test
|
||||
{
|
||||
class TexturedAnimatedQuadGame : Game
|
||||
{
|
||||
private GraphicsPipeline pipeline;
|
||||
private GpuBuffer vertexBuffer;
|
||||
private GpuBuffer indexBuffer;
|
||||
private Texture texture;
|
||||
private Sampler sampler;
|
||||
|
||||
private float t;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct FragmentUniforms
|
||||
{
|
||||
public Vector4 MultiplyColor;
|
||||
|
||||
public FragmentUniforms(Vector4 multiplyColor)
|
||||
{
|
||||
MultiplyColor = multiplyColor;
|
||||
}
|
||||
}
|
||||
|
||||
public TexturedAnimatedQuadGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
|
||||
{
|
||||
// Load the shaders
|
||||
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuadWithMatrix.vert"));
|
||||
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuadWithMultiplyColor.frag"));
|
||||
|
||||
// Create the graphics pipeline
|
||||
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
|
||||
MainWindow.SwapchainFormat,
|
||||
vertShaderModule,
|
||||
fragShaderModule
|
||||
);
|
||||
pipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions[0].BlendState = ColorAttachmentBlendState.AlphaBlend;
|
||||
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
|
||||
pipelineCreateInfo.VertexShaderInfo = GraphicsShaderInfo.Create<TransformVertexUniform>(vertShaderModule, "main", 0);
|
||||
pipelineCreateInfo.FragmentShaderInfo = GraphicsShaderInfo.Create<FragmentUniforms>(fragShaderModule, "main", 1);
|
||||
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
||||
|
||||
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
|
||||
|
||||
// Create and populate the GPU resources
|
||||
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
||||
|
||||
vertexBuffer = resourceUploader.CreateBuffer(
|
||||
[
|
||||
new PositionTextureVertex(new Vector3(-0.5f, -0.5f, 0), new Vector2(0, 0)),
|
||||
new PositionTextureVertex(new Vector3(0.5f, -0.5f, 0), new Vector2(1, 0)),
|
||||
new PositionTextureVertex(new Vector3(0.5f, 0.5f, 0), new Vector2(1, 1)),
|
||||
new PositionTextureVertex(new Vector3(-0.5f, 0.5f, 0), new Vector2(0, 1)),
|
||||
],
|
||||
BufferUsageFlags.Vertex
|
||||
);
|
||||
|
||||
indexBuffer = resourceUploader.CreateBuffer<ushort>(
|
||||
[
|
||||
0, 1, 2,
|
||||
0, 2, 3,
|
||||
],
|
||||
BufferUsageFlags.Index
|
||||
);
|
||||
|
||||
texture = resourceUploader.CreateTexture2DFromCompressed(TestUtils.GetTexturePath("ravioli.png"));
|
||||
|
||||
resourceUploader.Upload();
|
||||
resourceUploader.Dispose();
|
||||
}
|
||||
|
||||
protected override void Update(System.TimeSpan delta)
|
||||
{
|
||||
t += (float) delta.TotalSeconds;
|
||||
}
|
||||
|
||||
protected override void Draw(double alpha)
|
||||
{
|
||||
TransformVertexUniform vertUniforms;
|
||||
FragmentUniforms fragUniforms;
|
||||
|
||||
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
|
||||
Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow);
|
||||
if (backbuffer != null)
|
||||
{
|
||||
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, Color.Black));
|
||||
cmdbuf.BindGraphicsPipeline(pipeline);
|
||||
cmdbuf.BindVertexBuffers(vertexBuffer);
|
||||
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
|
||||
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, sampler));
|
||||
|
||||
// Top-left
|
||||
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ(t) * Matrix4x4.CreateTranslation(new Vector3(-0.5f, -0.5f, 0)));
|
||||
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Sin(t) * 0.5f, 1f, 1f));
|
||||
cmdbuf.PushVertexShaderUniforms(vertUniforms);
|
||||
cmdbuf.PushFragmentShaderUniforms(fragUniforms);
|
||||
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
|
||||
|
||||
// Top-right
|
||||
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ((2 * System.MathF.PI) - t) * Matrix4x4.CreateTranslation(new Vector3(0.5f, -0.5f, 0)));
|
||||
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Cos(t) * 0.5f, 1f, 1f));
|
||||
cmdbuf.PushVertexShaderUniforms(vertUniforms);
|
||||
cmdbuf.PushFragmentShaderUniforms(fragUniforms);
|
||||
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
|
||||
|
||||
// Bottom-left
|
||||
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ(t) * Matrix4x4.CreateTranslation(new Vector3(-0.5f, 0.5f, 0)));
|
||||
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Sin(t) * 0.2f, 1f, 1f));
|
||||
cmdbuf.PushVertexShaderUniforms(vertUniforms);
|
||||
cmdbuf.PushFragmentShaderUniforms(fragUniforms);
|
||||
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
|
||||
|
||||
// Bottom-right
|
||||
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ(t) * Matrix4x4.CreateTranslation(new Vector3(0.5f, 0.5f, 0)));
|
||||
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Cos(t) * 1f, 1f, 1f));
|
||||
cmdbuf.PushVertexShaderUniforms(vertUniforms);
|
||||
cmdbuf.PushFragmentShaderUniforms(fragUniforms);
|
||||
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
|
||||
|
||||
cmdbuf.EndRenderPass();
|
||||
}
|
||||
GraphicsDevice.Submit(cmdbuf);
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
TexturedAnimatedQuadGame game = new TexturedAnimatedQuadGame();
|
||||
game.Run();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -47,6 +47,7 @@
|
|||
<Compile Include="Examples\StoreLoadExample.cs" />
|
||||
<Compile Include="Examples\Texture3DCopyExample.cs" />
|
||||
<Compile Include="Examples\Texture3DExample.cs" />
|
||||
<Compile Include="Examples\TexturedAnimatedQuadExample.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<Import Project=".\CopyMoonlibs.targets" />
|
||||
|
|
|
@ -30,7 +30,8 @@ class Program : Game
|
|||
new RenderTextureMipmapsExample(),
|
||||
new StoreLoadExample(),
|
||||
new Texture3DCopyExample(),
|
||||
new Texture3DExample()
|
||||
new Texture3DExample(),
|
||||
new TexturedAnimatedQuadExample()
|
||||
];
|
||||
|
||||
int ExampleIndex = 0;
|
||||
|
|
Loading…
Reference in New Issue