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

📄 tool.cs

📁 Particle System Test Application on C#
💻 CS
📖 第 1 页 / 共 3 页
字号:
					public NullParticlePreProcessor()
					{
					}

                    /// <summary>
                    /// Constructor
                    /// </summary>
                    /// <param name="nextPreprocessor">A IParticlePreProcessor</param>
					public NullParticlePreProcessor(IParticlePreProcessor nextPreprocessor)
						:	base(nextPreprocessor)
					{
					}

                    /// <summary>
                    /// Abstract method. Inheritors should implement this to
                    /// apply their preprocessing step
                    /// </summary>
                    /// <param name="particles">A set of particles</param>
                    override protected void ApplyThisPreProcessor(ArrayList particles)
					{
					}
				}

				/// <summary>
				/// Applies a transform matrix to all particles that should be
				/// preprocessed
				/// </summary>
				public class MatrixTransformParticlePreProcessor : ChainableParticlePreProcessor
				{
                    /// <summary>
                    /// Constructor
                    /// </summary>
                    /// <param name="transform">A transform matrix</param>
					public MatrixTransformParticlePreProcessor(Matrix transform)
					{
						mTransform = transform;
					}

                    /// <summary>
                    /// Constructor
                    /// </summary>
                    /// <param name="transform">A transform matrix</param>
                    /// <param name="nextPreprocessor">A IParticlePreProcessor </param>
					public MatrixTransformParticlePreProcessor(
						Matrix transform,
						IParticlePreProcessor nextPreprocessor)
						:	base(nextPreprocessor)
					{
						mTransform = transform;
					}

                    /// <summary>
                    /// Abstract method. Inheritors should implement this to
                    /// apply their preprocessing step
                    /// </summary>
                    /// <param name="particles">A set of particles</param>
                    override protected void ApplyThisPreProcessor(ArrayList particles)
					{
						foreach(Particle lParticle in particles)
						{
							lParticle.Transform(mTransform);
						}
					}

					private Matrix mTransform;
				}

                /// <summary>
                /// An example of a non-linear particle preprocessor. 
                /// SqueezeParticlePreProcessor "squeezes" particles towards
                /// or from origo depending of the particle's distance to a 
                /// plane and a squeeze ratio
                /// </summary>
                public class SqueezeParticlePreProcessor : Tools.ChainableParticlePreProcessor
                {
                    /// <summary>
                    /// Constructor
                    /// </summary>
                    /// <param name="planeNormal">Plane normal</param>
                    /// <param name="planeTranslation">Plane translation</param>
                    /// <param name="squeezeRatio">Squeeze ratio</param>
                    public SqueezeParticlePreProcessor(
                        Vector3 planeNormal, 
                        float planeTranslation,
                        Vector3 squeezeRatio)
                    {
                        mPlaneNormal = planeNormal;
                        mPlaneTranslation = planeTranslation;
                        mSqueezeRatio = squeezeRatio;
                    }

                    /// <summary>
                    /// Constructor
                    /// </summary>
                    /// <param name="planeNormal">Plane normal</param>
                    /// <param name="planeTranslation">Plane translation</param>
                    /// <param name="squeezeRatio">Squeeze ratio</param>
                    /// <param name="nextPreprocessor">Next preprocessor in the chain</param>
                    public SqueezeParticlePreProcessor(
                        Vector3 planeNormal, 
                        float planeTranslation,
                        Vector3 squeezeRatio,
                        IParticlePreProcessor nextPreprocessor)
                        :	base(nextPreprocessor)
                    {
                        mPlaneNormal = planeNormal;
                        mPlaneTranslation = planeTranslation;
                        mSqueezeRatio = squeezeRatio;
                    }

                    private Vector3 SqueezeVector(Vector3 v)
                    {
                        Vector3 lCopy = v;

                        float lFactor = 
                            Vector3.Dot(mPlaneNormal, lCopy) + mPlaneTranslation;

                        Vector3 lSqueezeRatio = lFactor * mSqueezeRatio;

                        lCopy.X += lCopy.X * lSqueezeRatio.X;
                        lCopy.Y += lCopy.Y * lSqueezeRatio.Y;
                        lCopy.Z += lCopy.Z * lSqueezeRatio.Z;
			
                        return lCopy;
                    }

                    /// <summary>
                    /// Applies a transform matrix to all particles that should be
                    /// preprocessed
                    /// </summary>
                    override protected void ApplyThisPreProcessor(ArrayList particles)
                    {
                        foreach(Particle lParticle in particles)
                        {
                            lParticle.Position = SqueezeVector(lParticle.Position);
                            lParticle.PreviousPosition = SqueezeVector(lParticle.PreviousPosition);
                        }
                    }

                    private Vector3 mPlaneNormal;
                    private float mPlaneTranslation;
                    private Vector3 mSqueezeRatio;
                }

                /// <summary>
				/// Creates an octahedron
				/// </summary>
				/// <param name="centerPosition">Center position of octahedron</param>
				/// <param name="mass">Mass of octahedron</param>
				/// <param name="sideLength">Side lenght of octahedron</param>
				/// <param name="constraintFactory">A constraint factory</param>
                /// <param name="transaction">A transaction</param>
                /// <returns>An array of particles that was created</returns>
				public static Particle[] CreateOctahedron(
					Vector3 centerPosition,
					float mass,
					float sideLength,
					ITwoParticleConstraintFactory constraintFactory,
                    ParticleSystem.Transaction transaction)
                {
					if( !(sideLength > float.Epsilon) )
					{
						throw new Exception("sideLength parameter must be greater than 0");
					}

					Matrix lScaleMatrix = 
						Matrix.Scaling(sideLength, sideLength, sideLength);
					Matrix lTranslationMatrix = 
						Matrix.Translation(centerPosition);

					return CreateOctahedron(
						mass,
						lScaleMatrix * lTranslationMatrix,
						constraintFactory,
						transaction);
				}

				/// <summary>
				/// Creates an octahedron
				/// </summary>
				/// <param name="mass">Mass of octahedron</param>
				/// <param name="transformMatrix">Matrix used to transform particles positions</param>
				/// <param name="constraintFactory">A constraint factory</param>
                /// <param name="transaction">A transaction</param>
                /// <returns>An array of particles that was created</returns>
				public static Particle[] CreateOctahedron(
					float mass,
					Matrix transformMatrix,
					ITwoParticleConstraintFactory constraintFactory,
                    ParticleSystem.Transaction transaction)
                {
					return CreateOctahedron(
						mass, 
						new MatrixTransformParticlePreProcessor(transformMatrix),
						constraintFactory,
						transaction);
				}

				/// <summary>
				/// Creates an octahedron
				/// </summary>
				/// <param name="mass">Mass of octahedron</param>
				/// <param name="preprocessor">Preprocessor applied to all created particles</param>
				/// <param name="constraintFactory">A constraint factory</param>
                /// <param name="transaction">A transaction</param>
                /// <returns>An array of particles that was created</returns>
				public static Particle[] CreateOctahedron(
					float mass,
					IParticlePreProcessor preprocessor,
					ITwoParticleConstraintFactory constraintFactory,
                    ParticleSystem.Transaction transaction)
                {
					if( !(mass > float.Epsilon) )
					{
						throw new Exception("mass parameter must be greater than 0");
					}

					float lHalfSideLength = 0.5f;
					float lHalfHeight = 1.0f / (float)Math.Sqrt(2);

					Particle lTopParticle = new Particle(mass / 6.0f, new Vector3(
						0.0f, 
						+ lHalfHeight,
						0.0f));

					Particle lMiddle00Particle = new Particle(mass / 6.0f, new Vector3(
						- lHalfSideLength, 
						0.0f,
						- lHalfSideLength));

					Particle lMiddle01Particle = new Particle(mass / 6.0f, new Vector3(
						- lHalfSideLength, 
						0.0f,
						+ lHalfSideLength));

					Particle lMiddle11Particle = new Particle(mass / 6.0f, new Vector3(
						+ lHalfSideLength, 
						0.0f,
						+ lHalfSideLength));

					Particle lMiddle10Particle = new Particle(mass / 6.0f, new Vector3(
						+ lHalfSideLength, 
						0.0f,
						- lHalfSideLength));

					Particle lBottomParticle = new Particle(mass / 6.0f, new Vector3(
						0.0f, 
						 - lHalfHeight,
						0.0f));

					ArrayList lCreatedParticles = new ArrayList();

					lCreatedParticles.Add(lTopParticle);
					lCreatedParticles.Add(lMiddle00Particle);
					lCreatedParticles.Add(lMiddle01Particle);
					lCreatedParticles.Add(lMiddle11Particle);
					lCreatedParticles.Add(lMiddle10Particle);
					lCreatedParticles.Add(lBottomParticle);
					preprocessor.Apply(lCreatedParticles);

	                constraintFactory.CreateConstraint(transaction, lTopParticle, lMiddle00Particle);
					constraintFactory.CreateConstraint(transaction, lTopParticle, lMiddle01Particle);
					constraintFactory.CreateConstraint(transaction, lTopParticle, lMiddle11Particle);
					constraintFactory.CreateConstraint(transaction, lTopParticle, lMiddle10Particle);

					constraintFactory.CreateConstraint(transaction, lMiddle00Particle, lMiddle01Particle);
					constraintFactory.CreateConstraint(transaction, lMiddle01Particle, lMiddle11Particle);
					constraintFactory.CreateConstraint(transaction, lMiddle11Particle, lMiddle10Particle);
					constraintFactory.CreateConstraint(transaction, lMiddle10Particle, lMiddle00Particle);

					constraintFactory.CreateConstraint(transaction, lBottomParticle, lMiddle00Particle);
					constraintFactory.CreateConstraint(transaction, lBottomParticle, lMiddle01Particle);
					constraintFactory.CreateConstraint(transaction, lBottomParticle, lMiddle11Particle);
					constraintFactory.CreateConstraint(transaction, lBottomParticle, lMiddle10Particle);

                    AddParticlesToTransaction(lCreatedParticles, transaction);
                    return ConvertArrayListToParticleList(lCreatedParticles);
                }

				/// <summary>
				/// Creates a "wobbly" box. The box is actually stiff but due
				/// to its design its hard for the particle system to relax
				/// the constraints effeciently which gives the illusion of 
				/// box made out jelly. Especially on low numbers of relax
				/// iterations
				/// </summary>
				/// <param name="centerPosition">Center position of box</param>
				/// <param name="mass">Mass of box</param>
				/// <param name="sideLength">Sidelength of box</param>
				/// <param name="constraintFactory">A constraint factory</param>
                /// <param name="transaction">A transaction</param>
                /// <returns>An array of particles that was created</returns>
				public static Particle[] CreateWobblyBox(
					Vector3 centerPosition,
					float mass,
					float sideLength,
					ITwoParticleConstraintFactory constraintFactory,
                    ParticleSystem.Transaction transaction)
                {
					if( !(sideLength > float.Epsilon) )
					{
						throw new Exception("sideLength parameter must be greater than 0");
					}

					Matrix lScaleMatrix = 
						Matrix.Scaling(sideLength, sideLength, sideLength);
					Matrix lTranslationMatrix = 
						Matrix.Translation(centerPosition);

					return CreateWobblyBox(
						mass,
						lScaleMatrix * lTranslationMatrix, 
						constraintFactory,
						transaction);
				}

                /// <summary>
                /// Creates a "wobbly" box. The box is actually stiff but due
                /// to its design its hard for the particle system to relax
                /// the constraints effeciently which gives the illusion of 
                /// box made out jelly. Especially on low numbers of relax
                /// iterations
                /// </summary>
                /// <param name="mass">Mass of box</param>
                /// <param name="transformMatrix">Matrix used to transform particles positions</param>
                /// <param name="constraintFactory">A constraint factory</param>
                /// <param name="transaction">A transaction</param>
                /// <returns>An array of particles that was created</returns>
                public static Particle[] CreateWobblyBox(
					float mass,
					Matrix transformMatrix,
					ITwoParticleConstraintFactory constraintFactory,
                    ParticleSystem.Transaction transaction)
                {
					return CreateWobblyBox(
						mass,
						new MatrixTransformParticlePreProcessor(transformMatrix),
						constraintFactory,
						transaction);
				}

                /// <summary>
                /// Creates a "wobbly" box. The box is actually stiff but due
                /// to its design its hard for the particle system to relax
                /// the constraints effeciently which gives the illusion of 
                /// box made out jelly. Especially on low numbers of relax
                /// iterations
                /// </summary>
                /// <param name="mass">Mass of box</param>
                /// <param name="preprocessor">Preprocessor applied to all created particles</param>
                /// <param name="constraintFactory">A constraint factory</param>
                /// <param name="transaction">A transaction</param>
                /// <returns>An array of particles that was created</returns>
                public static Particle[] CreateWobblyBox(
					float mass,
					IParticlePreProcessor preprocessor,
					ITwoParticleConstraintFactory constraintFactory,
                    ParticleSystem.Transaction transaction)
                {
					if( !(mass > float.Epsilon) )
					{
						throw new Exception("mass parameter must be greater than 0");
					}

					float lHalfSideLength = 0.5f;

					Particle l000Particle = new Particle(mass / 6.0f, new Vector3(
						- lHalfSideLength, 
						- lHalfSideLength,
						- lHalfSideLength));

					Particle l001Particle = new Particle(mass / 6.0f, new Vector3(
						- lHalfSideLength, 
						- lHalfSideLength,
						+ lHalfSideLength));

					Particle l010Particle = new Particle(mass / 6.0f, new Vector3(
						- lHalfSideLength, 

⌨️ 快捷键说明

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