コード例 #1
0
ファイル: ParticleEmitter.cs プロジェクト: pentiumx/HLSLTest
        protected void GenerateParticles()
        {
            // Create particle and index arrays
            if (Mode == BillboardMode.Cross) {
                int crossBillboardNum = 2;
                Vector3 z = Vector3.Zero;
                particles = new ParticleVertex[ParticleNum * crossBillboardNum * 4];
                indices = new int[ParticleNum * crossBillboardNum * 6];

                int x = 0;
                // For each billboard...
                for (int i = 0; i < ParticleNum * 4 * crossBillboardNum; i += 4 * crossBillboardNum) {
                    //Vector3 pos = particlePositions[i / (4 * crossBillboardNum)];
                    Vector3 pos = Vector3.Zero;
                    Vector3 offsetX = new Vector3(ParticleSize.X / 2.0f,
                        ParticleSize.Y / 2.0f, 0);
                    Vector3 offsetZ = new Vector3(0, offsetX.Y, offsetX.X);

                    // Add 4 vertices per rectangle
                    particles[i + 0] = new ParticleVertex(pos +
                        new Vector3(-1, 1, 0) * offsetX, new Vector2(0, 0), z, 0, -1);
                    particles[i + 1] = new ParticleVertex(pos +
                        new Vector3(-1, -1, 0) * offsetX, new Vector2(0, 1), z, 0, -1);
                    particles[i + 2] = new ParticleVertex(pos +
                        new Vector3(1, -1, 0) * offsetX, new Vector2(1, 1), z, 0, -1);
                    particles[i + 3] = new ParticleVertex(pos +
                        new Vector3(1, 1, 0) * offsetX, new Vector2(1, 0), z, 0, -1);
                    particles[i + 4] = new ParticleVertex(pos +
                        new Vector3(0, 1, -1) * offsetZ, new Vector2(0, 0), z, 0, -1);
                    particles[i + 5] = new ParticleVertex(pos +
                        new Vector3(0, -1, -1) * offsetZ, new Vector2(0, 1), z, 0, -1);
                    particles[i + 6] = new ParticleVertex(pos +
                        new Vector3(0, -1, 1) * offsetZ, new Vector2(1, 1), z, 0, -1);
                    particles[i + 7] = new ParticleVertex(pos +
                        new Vector3(0, 1, 1) * offsetZ, new Vector2(1, 0), z, 0, -1);

                    // Add 6 indices per rectangle to form four triangles
                    indices[x++] = i + 0;
                    indices[x++] = i + 3;
                    indices[x++] = i + 2;
                    indices[x++] = i + 2;
                    indices[x++] = i + 1;
                    indices[x++] = i + 0;
                    indices[x++] = i + 0 + 4;
                    indices[x++] = i + 3 + 4;
                    indices[x++] = i + 2 + 4;
                    indices[x++] = i + 2 + 4;
                    indices[x++] = i + 1 + 4;
                    indices[x++] = i + 0 + 4;
                }
            } else {
                particles = new ParticleVertex[ParticleNum * 4];
                indices = new int[ParticleNum * 6];
                Vector3 z = Vector3.Zero;
                int x = 0;

                // Initialize particle settings and fill index and vertex arrays
                for (int i = 0; i < ParticleNum * 4; i += 4) {
                    particles[i + 0] = new ParticleVertex(z, new Vector2(0, 0), z, 0, -1, 0, z);
                    particles[i + 1] = new ParticleVertex(z, new Vector2(0, 1), z, 0, -1, 0, z);
                    particles[i + 2] = new ParticleVertex(z, new Vector2(1, 1), z, 0, -1, 0, z);
                    particles[i + 3] = new ParticleVertex(z, new Vector2(1, 0), z, 0, -1, 0, z);
                    indices[x++] = i + 0;
                    indices[x++] = i + 3;
                    indices[x++] = i + 2;
                    indices[x++] = i + 2;
                    indices[x++] = i + 1;
                    indices[x++] = i + 0;
                }
            }
        }
コード例 #2
0
ファイル: LaserBillboard.cs プロジェクト: pentiumx/HLSLTest
        void GenerateParticles(Vector3[] particlePositions)
        {
            // Create vertex and index arrays
            particles = new ParticleVertex[nBillboards * 4];
            indices = new int[nBillboards * 6];
            int x = 0;
            Vector3 z = Vector3.Zero;
            // For each billboard...
            for (int i = 0; i < nBillboards * 4; i += 4) {
                Vector3 pos = particlePositions[i / 4];
                // Add 4 vertices at the billboard's position
                particles[i + 0] = new ParticleVertex(pos, new Vector2(0, 0), z, 0, -1, 0, End);
                particles[i + 1] = new ParticleVertex(pos, new Vector2(0, 1), z, 0, -1, 0, End);
                particles[i + 2] = new ParticleVertex(pos, new Vector2(1, 1), z, 0, -1, 0, End);
                particles[i + 3] = new ParticleVertex(pos, new Vector2(1, 0), z, 0, -1, 0, End);

                // Add 6 indices to form two triangles
                indices[x++] = i + 0;
                indices[x++] = i + 3;
                indices[x++] = i + 2;
                indices[x++] = i + 2;
                indices[x++] = i + 1;
                indices[x++] = i + 0;
            }

            // Create and set the vertex buffer
            vertexBuffers = new VertexBuffer(graphicsDevice,
                typeof(ParticleVertex),
                nBillboards * 4, BufferUsage.WriteOnly);
            vertexBuffers.SetData<ParticleVertex>(particles);
            // Create and set the index buffer
            indexBuffers = new IndexBuffer(graphicsDevice,
                IndexElementSize.ThirtyTwoBits,
                nBillboards * 6, BufferUsage.WriteOnly);
            indexBuffers.SetData<int>(indices);
        }