⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 d3dtriangle.cs

📁 Particle System Test Application on C#
💻 CS
📖 第 1 页 / 共 2 页
字号:
        device.RenderState.CullMode = Cull.Clockwise;  
        device.RenderState.Lighting = false;        //make sure lighting is disabled, since the sample uses lit vertices.
    }

    static private void DrawLine(
        CustomVertex.PositionColored[] vertices,
        ref int index,
        Vector3 from,
        Vector3 to,
        Int32 color)
    {
        vertices[index].X = from.X;
        vertices[index].Y = from.Y;
        vertices[index].Z = from.Z;
        vertices[index].Color = color;
        ++index;

        vertices[index].X = to.X;
        vertices[index].Y = to.Y;
        vertices[index].Z = to.Z;
        vertices[index].Color = color;
        ++index;
    }

    static private void DrawAxisAlignedBoxLine(
        CustomVertex.PositionColored[] vertices,
        ref int index,
        Vector3 corner,
        Vector3 oppositeCorner,
        Int32 color,
        int fromCornerIndex,
        int toCornerIndex)
    {
        if( fromCornerIndex < 0 || fromCornerIndex > 7 )
        {
            throw new System.Exception("fromCornerIndex parameter must be between 0 and 7");
        }

        if( toCornerIndex < 0 || toCornerIndex > 7 )
        {
            throw new System.Exception("toCornerIndex parameter must be between 0 and 7");
        }

        bool lFromXChooser = (fromCornerIndex & 1) != 0;
        bool lFromYChooser = (fromCornerIndex & 2) != 0;
        bool lFromZChooser = (fromCornerIndex & 4) != 0;

        bool lToXChooser = (toCornerIndex & 1) != 0;
        bool lToYChooser = (toCornerIndex & 2) != 0;
        bool lToZChooser = (toCornerIndex & 4) != 0;


        Vector3 lFrom = new Vector3(
            lFromXChooser ? oppositeCorner.X : corner.X,
            lFromYChooser ? oppositeCorner.Y : corner.Y,
            lFromZChooser ? oppositeCorner.Z : corner.Z);

        Vector3 lTo = new Vector3(
            lToXChooser ? oppositeCorner.X : corner.X,
            lToYChooser ? oppositeCorner.Y : corner.Y,
            lToZChooser ? oppositeCorner.Z : corner.Z);

        DrawLine(vertices, ref index, lFrom, lTo, color);

    }


    private static void FillVertexBuffer(
        VertexBuffer linesVB, 
        out int renderedLines,
        ParticleSystem ps)
    {
    {
        CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])linesVB.Lock(0,0);

        int lIndex = 0;

        Particle[] lParticles = ps.Particles;
        foreach( Particle lParticle in lParticles )
        {
            float lSide = 0.2f;

            Vector3 lPosition = lParticle.Position;

            verts[lIndex].X= lPosition.X; 
            verts[lIndex].Y= lPosition.Y - lSide; 
            verts[lIndex].Z= lPosition.Z; 
            verts[lIndex].Color = System.Drawing.Color.Red.ToArgb();
            ++lIndex;

            verts[lIndex].X= lPosition.X; 
            verts[lIndex].Y= lPosition.Y + lSide; 
            verts[lIndex].Z= lPosition.Z; 
            verts[lIndex].Color = System.Drawing.Color.Red.ToArgb();
            ++lIndex;

            verts[lIndex].X= lPosition.X - lSide; 
            verts[lIndex].Y= lPosition.Y; 
            verts[lIndex].Z= lPosition.Z; 
            verts[lIndex].Color = System.Drawing.Color.Red.ToArgb();
            ++lIndex;

            verts[lIndex].X= lPosition.X + lSide; 
            verts[lIndex].Y= lPosition.Y; 
            verts[lIndex].Z= lPosition.Z; 
            verts[lIndex].Color = System.Drawing.Color.Red.ToArgb();
            ++lIndex;

            verts[lIndex].X= lPosition.X; 
            verts[lIndex].Y= lPosition.Y; 
            verts[lIndex].Z= lPosition.Z - lSide; 
            verts[lIndex].Color = System.Drawing.Color.Red.ToArgb();
            ++lIndex;

            verts[lIndex].X= lPosition.X; 
            verts[lIndex].Y= lPosition.Y; 
            verts[lIndex].Z= lPosition.Z + lSide; 
            verts[lIndex].Color = System.Drawing.Color.Red.ToArgb();
            ++lIndex;

        }

        IConstraint[] lConstraints = ps.Constraints;
        foreach( IConstraint lConstraint in lConstraints )
        {
            TwoParticleHolder lTwoParticleHolder = lConstraint as TwoParticleHolder;
            if( lTwoParticleHolder != null )
            {
                Int32 lColor;

                if( lTwoParticleHolder is Stick )
                {
                    lColor = System.Drawing.Color.Yellow.ToArgb();
                }
                else if( lTwoParticleHolder is Rope )
                {
                    lColor = System.Drawing.Color.Lime.ToArgb();
                }
                else
                {
                    lColor = System.Drawing.Color.White.ToArgb();
                }

                DrawLine(
                    verts, 
                    ref lIndex, 
                    lTwoParticleHolder.Particle1.Position,
                    lTwoParticleHolder.Particle2.Position,
                    lColor);
            }
        }

        IGlobalConstraint[] lGlobalConstraints = ps.GlobalConstraints;
        foreach( IGlobalConstraint lGlobalConstraint in lGlobalConstraints )
        {
            AxisAlignedBox lAxisAlignedBox = lGlobalConstraint as AxisAlignedBox;
            if( lAxisAlignedBox != null )
            {
                Int32 lColor = System.Drawing.Color.White.ToArgb();

                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    0,
                    1);
						
                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    1,
                    3);

                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    3,
                    2);

                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    2,
                    0);

                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    4,
                    5);
						
                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    5,
                    7);

                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    7,
                    6);

                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    6,
                    4);

                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    0,
                    4);

                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    1,
                    5);

                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    2,
                    6);

                DrawAxisAlignedBoxLine(
                    verts,
                    ref lIndex,
                    lAxisAlignedBox.Corner,
                    lAxisAlignedBox.OppositeCorner,
                    lColor,
                    3,
                    7);
            }

        }

        linesVB.Unlock();
        renderedLines = lIndex / 2;
    }
    }

    /// <summary>
    /// Called when a vertex buffer needs to be filled with data.
    /// </summary>
    public void OnCreateVertexBuffer(object sender, EventArgs e)
    {
    }

    private void InitializeComponent()
    {
        // 
        // ourRenderTarget
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(400, 300);
        this.Name = "ourRenderTarget";
        this.Closing += new System.ComponentModel.CancelEventHandler(this.ourRenderTarget_Closing);
        this.Load += new System.EventHandler(this.ourRenderTarget_Load);

    }

    private void ourRenderTarget_Load(object sender, System.EventArgs e)
    {
	
    }

    private void ourRenderTarget_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -