Beispiel #1
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)
            };
        }
Beispiel #2
0
        public CharRenderer(Graphics g, Texture2D tex, string vertexShader, string fragmentShader, int maxChars = 1024)
        {
            MaxChars    = maxChars;
            Graphics    = g;
            Texture     = tex;
            Instances   = VKBuffer.InstanceInfo <CharInfo>(g, MaxChars);
            UProjection = VKBuffer.UniformBuffer <ViewProjection>(g, 1);
            UTime       = VKBuffer.UniformBuffer <float>(g, 1);

            Pipeline = new PipelineController(Graphics);
            Pipeline.ClearDepthOnBeginPass = true;
            Pipeline.DepthTest             = false;
            Pipeline.DepthWrite            = false;
            Pipeline.BlendMode             = BlendMode.AlphaPremultiplied;
            Pipeline.Instancing            = true;
            Pipeline.InstanceInfoType      = typeof(CharInfo);
            Pipeline.Shaders         = new[] { vertexShader, fragmentShader };
            Pipeline.DescriptorItems = new[] {
                DescriptorItem.UniformBuffer(DescriptorItem.ShaderType.Vertex, UProjection),
                DescriptorItem.UniformBuffer(DescriptorItem.ShaderType.Vertex, UTime),
                DescriptorItem.CombinedImageSampler(DescriptorItem.ShaderType.Fragment, tex, DescriptorItem.SamplerFilter.Nearest, DescriptorItem.SamplerFilter.Nearest)
            };
        }
Beispiel #3
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();
        }