Exemplo n.º 1
0
        protected override void OnUpdate(long tick)
        {
            base.OnUpdate(tick);

            Tick = tick;
            if (tick % 15 == 0)
            {
                TextInstances[0].Text =
                    $"FPS: {ActualFPS:#.##}\n" +
                    $"UPS: {ActualUPS:#.##}\n" +
                    $"Billboards: {Billboards}\n" +
                    $"Particles: {ParticleCount}\n" +
                    $"Mouse: {(MouseState ? "Down" : "Up")}\n" +
                    $"Mouse position: {MousePositionWindow}";
                TextRenderer.SetTextInstances(TextInstances, 1);
            }

            CamAngle      = new Angle(0f, CamAngle.Pitch, CamAngle.Yaw + 0.2f);
            CamPos        = CamAngle.Forward * CamDist;
            VP.View       = Matrix4.CreateLookAt(CamPos, CamTarget);
            VP.Projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.ToRad(80f), (float)Width / (float)Height, 0.1f, 1000f);
            BViewProjection.Write(ref VP);

            TextRenderer.Projection = Matrix4.CreateOrtho(Vector2.Zero, (Vector2)ClientSize, 0f, 1f);

            MeshRenderer.View       = Matrix4.Identity;
            MeshRenderer.Projection = Matrix4.CreateOrtho(Vector2.Zero, (Vector2)ClientSize, 0f, 1f);

            ParticleRenderer.Projection = Matrix4.CreateOrtho(Vector2.Zero, (Vector2)ClientSize, 0f, 1f);

            LastUpdateTick = Tick;
        }
Exemplo n.º 2
0
        public void Draw(VKImage image, float tick)
        {
            if (Count == 0)
            {
                return;
            }

            var vp = new ViewProjection {
                View = View, Projection = Projection
            };

            UProjection.Write(ref vp);
            UTime.Write(ref tick);

            var cb = CBuffer[image];

            cb.Begin();
            cb.BeginPass(Pipeline);
            cb.Draw(Graphics.SquareSprite, Instances, Count);
            cb.EndPass();
            cb.End();

            cb.Submit(true);
            cb.Reset();
        }
Exemplo n.º 3
0
 public void CreateParticles(ParticleInfo[] particles, int count)
 {
     if (NextWritePos + count >= MaxParticles)
     {
         var a1 = particles.Take(MaxParticles - NextWritePos).ToArray();
         var a2 = particles.Skip(MaxParticles - NextWritePos).ToArray();
         CreateParticles(a1, a1.Length);
         CreateParticles(a2, count - a1.Length);
     }
     else
     {
         Instances.Write(particles, NextWritePos);
         Count        = Math.Min(Count + count, MaxParticles);
         NextWritePos = (NextWritePos + count) % MaxParticles;
     }
 }
Exemplo n.º 4
0
        public ParticleRenderer(Graphics g, Texture2D tex, Sprite sprite, string vertexShader, string fragmentShader, int maxParticles = 1024)
        {
            MaxParticles  = maxParticles;
            Graphics      = g;
            Texture       = tex;
            Sprite        = sprite;
            Instances     = VKBuffer.InstanceInfo <ParticleInfo>(g, maxParticles);
            UProjection   = VKBuffer.UniformBuffer <ViewProjection>(g, 1);
            UTime         = VKBuffer.UniformBuffer <float>(g, 1);
            USpriteCoords = VKBuffer.UniformBuffer <SpriteCoords>(g, 1);
            UColor        = VKBuffer.UniformBuffer <Color4>(g, 1);
            var spriteCoords = new SpriteCoords
            {
                LeftTop     = sprite.LeftTop / Texture.SizeF,
                RightBottom = sprite.RightBottom / Texture.SizeF,
                Center      = sprite.TextureCenter
            };

            USpriteCoords.Write(ref spriteCoords);
            var color = Color4.White;

            UColor.Write(ref color);

            Pipeline = new PipelineController(Graphics);
            Pipeline.ClearDepthOnBeginPass = true;
            Pipeline.DepthTest             = false;
            Pipeline.DepthWrite            = false;
            Pipeline.BlendMode             = BlendMode.AlphaPremultiplied;
            Pipeline.Instancing            = true;
            Pipeline.InstanceInfoType      = typeof(ParticleInfo);
            Pipeline.Shaders         = new[] { vertexShader, fragmentShader };
            Pipeline.DescriptorItems = new[] {
                DescriptorItem.UniformBuffer(DescriptorItem.ShaderType.Vertex, UProjection),
                DescriptorItem.UniformBuffer(DescriptorItem.ShaderType.Vertex, UTime),
                DescriptorItem.UniformBuffer(DescriptorItem.ShaderType.Vertex, USpriteCoords),
                DescriptorItem.UniformBuffer(DescriptorItem.ShaderType.Fragment, UColor),
                DescriptorItem.CombinedImageSampler(DescriptorItem.ShaderType.Fragment, tex, DescriptorItem.SamplerFilter.Nearest, DescriptorItem.SamplerFilter.Nearest)
            };
        }
Exemplo n.º 5
0
        protected override void OnInit()
        {
            base.OnInit();

            var rot = Matrix4.CreateRotationX(0) * Matrix4.CreateRotationY(0) * Matrix4.CreateRotationZ(90);

            Console.WriteLine(rot);
            Console.WriteLine(Matrix4.CreateRotation(new Angle(30f, 45f, 30f)).Rotation);

            Positions = new Vector3[Billboards];
            var rand = new Random();

            for (var i = 0; i < Billboards; i++)
            {
                Positions[i] = new Vector3(-200f + (float)rand.NextDouble() * 400f, -200f + (float)rand.NextDouble() * 400f, 0f);
            }

            Instances = VKBuffer.InstanceInfo <InstanceInfo>(Graphics, Billboards);
            Instances.Write(Positions);
            BViewProjection = VKBuffer.UniformBuffer <ViewProjection>(Graphics, 1);
            var testImg = Content.Get <Image>("Data/tex.png");

            using (var imgBuilder = new ImageBuilder(testImg))
            {
                var data = imgBuilder.GetData();
                for (var i = 0; i < data.Length; i += 4)
                {
                    data[i + 2] += 128;
                }
                imgBuilder.SetData(data);
                imgBuilder.Finish();
                TestTex = Texture2D.FromBitmap(Graphics, imgBuilder, new Rectangle[] { new Rectangle(Point.Empty, testImg.Size) });
            }

            Pipeline                  = new PipelineController(Graphics);
            Pipeline.DepthTest        = true;
            Pipeline.DepthWrite       = true;
            Pipeline.BlendMode        = BlendMode.Alpha;
            Pipeline.Instancing       = true;
            Pipeline.InstanceInfoType = typeof(InstanceInfo);
            Pipeline.Shaders          = new[] { "Data/billboard.vert.spv", "Data/billboard.frag.spv" };
            Pipeline.DescriptorItems  = new[] {
                DescriptorItem.UniformBuffer(DescriptorItem.ShaderType.Vertex, BViewProjection),
                DescriptorItem.CombinedImageSampler(DescriptorItem.ShaderType.Fragment, TestTex, DescriptorItem.SamplerFilter.Nearest, DescriptorItem.SamplerFilter.Nearest)
            };
            Pipeline.Build();

            /*var font = SpriteFont.FromSprites(
             *  Graphics, Content.Get<Texture2D>("Data/ascii.png"), new Vector2(10f, 18f), Vector2.Zero, new Vector2(16f, 16f), 256 / 16,
             *  " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
             * );*/
            var font = SpriteFont.FromFont(Graphics, "Arial", FontStyle.Regular, 20.0f, 20f, true, '\n', (char)127, 16);

            TextRenderer = new TextRenderer(Graphics, font, "Data/text.vert.spv", "Data/text.frag.spv", 128);
            TextRenderer.BuildPipeline();
            TextInstances = new[] { new TextRenderer.TextInstance(Vector2.Zero, Vector2.One, Color4.LightGray, 1, new Color4(0f, 0f, 0f, 1f), new Vector2(0f, 0f), Vector2.Zero, "N/A") };

            MeshRenderer = new MeshRenderer(Graphics, Content.Get <Texture2D>("Data/tex.png"), "Data/mesh.vert.spv", "Data/mesh.frag.spv", 32, 4);
            MeshRenderer.BuildPipeline();
            MeshRenderer.SetMeshInfo(
                new[] {
                MeshRenderer.CreateMeshInfo(
                    Mesh.FromPolygon(Graphics, new[]
                {
                    new Vertex(new Vector3(-180f, -90f, 0f), new Vector3(0f, 0f, 1f), new Vector2(0f, 0f), Color4.White),
                    new Vertex(new Vector3(0f, -180f, 0f), new Vector3(0f, 0f, 1f), new Vector2(0.5f, 0f), Color4.White),
                    new Vertex(new Vector3(180f, 0f, 0f), new Vector3(0f, 0f, 1f), new Vector2(1f, 0.2f), Color4.White),
                    new Vertex(new Vector3(0f, 120f, 0f), new Vector3(0f, 0f, 1f), new Vector2(0f, 1f), Color4.White),
                    new Vertex(new Vector3(-180f, 90f, 0f), new Vector3(0f, 0f, 1f), new Vector2(0.1f, 1f), Color4.White)
                }),
                    new Sprite(new Vector2(0f, 0f), new Vector2(64f, 64f)),
                    new Vector3(400f, 300f, 0f),
                    Vector3.One,
                    Matrix4.CreateRotationZ(0f),
                    Vector3.Zero
                    ),
                MeshRenderer.CreateMeshInfo(
                    new Mesh(
                        Graphics,
                        new Vertex[]
                {
                    new Vertex(new Vector3(-180f, -90f, 0f), new Vector3(0f, 0f, 1f), new Vector2(0f, 0f), Color4.White),
                    new Vertex(new Vector3(0f, -90f, 0f), new Vector3(0f, 0f, 1f), new Vector2(1f, 0f), Color4.White),
                    new Vertex(new Vector3(180f, 90f, 0f), new Vector3(0f, 0f, 1f), new Vector2(1f, 1f), Color4.White),
                    new Vertex(new Vector3(0f, 90f, 0f), new Vector3(0f, 0f, 1f), new Vector2(0f, 1f), Color4.White)
                },
                        new int[] { 0, 1, 2, 2, 3, 0 }
                        ),
                    new Sprite(new Vector2(0f, 0f), new Vector2(64f, 64f)),
                    new Vector3(580f, 300f, 0f),
                    Vector3.One,
                    Matrix4.CreateRotationZ(0f),
                    Vector3.Zero
                    )
            },
                2
                );

            ParticleRenderer = new ParticleRenderer(
                Graphics, Content.Get <Texture2D>("Data/fire.png"),
                new Sprite(Vector2.Zero, new Vector2(64f, 64f), new Vector2(64f, 64f)),
                "Data/particle.vert.spv", "Data/particle.frag.spv", 900000
                );
            ParticleRenderer.BlendMode = BlendMode.Add;
            ParticleRenderer.BuildPipeline();
        }
Exemplo n.º 6
0
 public void SetCharInfo(CharInfo[] chars, int count)
 {
     Count = count;
     Instances.Write(chars);
 }
Exemplo n.º 7
0
 public void SetSpriteInfo(SpriteInfo[] sprites, int count)
 {
     Count = count;
     Instances.Write(sprites);
 }