Exemplo n.º 1
0
        public AngleConstraint2D makeAngleConstraint(Spring2D s1, Spring2D s2)
        {
            AngleConstraint2D angle = new AngleConstraint2D(this, s1, s2);

            angles.Add(angle);
            return(angle);
        }
Exemplo n.º 2
0
        void applyConstraints()
        {
            //gravity
            if (setting.gravity != Vector2.zero)
            {
                for (int i = 0; i < sim.numberOfParticles(); ++i)
                {
                    p = sim.getParticle(i);
                    if (p.IsFree)
                    {
                        p.Position += setting.gravity;
                    }
                }
            }

            //iterations
            for (int iter = 0; iter < setting.iteration; iter++)
            {
                if (setting.applyString)
                {
                    for (int p = 1; p <= sim.maxSpringConvergenceID; p++)
                    {
                        for (int i = 0; i < sim.numberOfSprings(); i++)
                        {
                            Spring2D sp = sim.getSpring(i);
                            if (sp.convergenceGroupID == p)
                            {
                                sp.apply();
                            }
                        }
                    }
                }

                if (setting.applyAngle)
                {
                    for (int p = 1; p <= sim.maxAngleConvergenceID; p++)
                    {
                        for (int i = 0; i < sim.numberOfAngleConstraints(); i++)
                        {
                            AngleConstraint2D ag = sim.getAngleConstraint(i);
                            if (ag.convergenceGroupID == p)
                            {
                                ag.GetDelta();
                            }
                        }

                        for (int i = 0; i < sim.numberOfAngleConstraints(); i++)
                        {
                            AngleConstraint2D ag = sim.getAngleConstraint(i);
                            if (ag.convergenceGroupID == p)
                            {
                                ag.apply();
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
 public void removeAngleConstraint(AngleConstraint2D a)
 {
     angles.Remove(a);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Calculates the convergence ID of the springs and angles that are connected to particle specified by particleIndex
        /// </summary>
        /// <param name="particleIndex">Particle index.</param>
        private void CalcConvergenceID(int particleIndex)
        {
            Particle2D pp = this.getParticle(particleIndex);

            //Spring
            List <Spring2D> sp        = new List <Spring2D> (10);   //the conencted spring for particle index
            int             springNum = this.numberOfSprings();

            //get the connected spring
            for (int i = 0; i < springNum; i++)
            {
                Spring2D   s = this.getSpring(i);
                Particle2D a = s.ParticleA;
                Particle2D b = s.ParticleB;
                if (a == pp || b == pp)
                {
                    sp.Add(s);
                }
            }
            //calc the convergence id for the springs which connects to this particle
            List <int> IDinUse = new List <int> (10);

            for (int i = 0; i < sp.Count; i++)
            {
                if (sp[i].convergenceGroupID != 0)
                {
                    IDinUse.Add(sp[i].convergenceGroupID);
                }
            }
            for (int i = 0; i < sp.Count; i++)
            {
                if (sp[i].convergenceGroupID == 0)
                {
                    int id = 1;
                    while (IDinUse.Contains(id))
                    {
                        id++;
                    }
                    sp[i].convergenceGroupID = id;
                    IDinUse.Add(id);
                    if (id > this.maxSpringConvergenceID)
                    {
                        this.maxSpringConvergenceID = id;
                    }
                }
            }

            //angle
            List <AngleConstraint2D> ag = new List <AngleConstraint2D> (10);
            int angleNum = this.numberOfAngleConstraints();

            //get the connected angles
            for (int i = 0; i < angleNum; i++)
            {
                AngleConstraint2D angle = this.getAngleConstraint(i);
                Particle2D        b     = angle.ParticleB;
                Particle2D        m     = angle.ParticleM;
                if (b == pp || m == pp)
                {
                    ag.Add(angle);
                }
            }
            //recalc the convergence id for the angles that are connected to this particle
            IDinUse.Clear();
            for (int i = 0; i < ag.Count; i++)
            {
                if (ag[i].convergenceGroupID != 0)
                {
                    IDinUse.Add(ag[i].convergenceGroupID);
                }
            }
            for (int i = 0; i < ag.Count; i++)
            {
                if (ag[i].convergenceGroupID == 0)
                {
                    int id = 1;
                    while (IDinUse.Contains(id))
                    {
                        id++;
                    }
                    ag[i].convergenceGroupID = id;
                    IDinUse.Add(id);
                    if (id > this.maxAngleConvergenceID)
                    {
                        this.maxAngleConvergenceID = id;
                    }
                }
            }
        }
Exemplo n.º 5
0
        public SimBuffer_Angle(Simulation sim, Vector2[] particleUV, int width, int height)
        {
            this.sim = sim;
            int angleNum = sim.numberOfAngleConstraints();
            int parNum   = sim.numberOfParticles();

            //angle uv
            float usage = 0f;

            if (!SimBuffer.GetTexDimension(angleNum, out deltaRTWidth, out deltaRTHeight, out usage))
            {
                Debug.LogError("Cannot create SimBuffer Angle deltaw rt with wrong dimesnion!");
                return;
            }
            angleUV = new Vector2[angleNum];
            int   count = 0;
            float halfW = 0.5f / deltaRTWidth;
            float halfH = 0.5f / deltaRTHeight;

            for (int y = 0; y < deltaRTHeight; y++)
            {
                for (int x = 0; x < deltaRTWidth; x++)
                {
                    if (count < angleNum)
                    {
                        angleUV[count] = new Vector2((float)x / (float)deltaRTWidth + halfW, (float)y / (float)deltaRTHeight + halfH);
                    }
                    count++;
                }
            }

            tempRT      = new RenderTexture[sim.maxAngleConvergenceID - 1];
            tempDeltaRT = new RenderTexture[sim.maxAngleConvergenceID];
            paramRT     = new RenderTexture[sim.maxAngleConvergenceID];
            deltaMesh   = new Mesh[sim.maxAngleConvergenceID];

            ID_AngleParamRT  = Shader.PropertyToID("_AngleParamRT");
            ID_AngleConstant = Shader.PropertyToID("_AngleConstant");
            ID_PositionRT    = Shader.PropertyToID("_PositionRT");
            ID_AngleDeltaRT  = Shader.PropertyToID("_AngleDeltaRT");

            //rg = the other end point's uv, ba = uv in the delta rt
            Color[]   paramRTColor = new Color[width * height];
            Texture2D tempTex      = new Texture2D(width, height, TextureFormat.RGBAFloat, false, false);

            for (int i = 0; i < sim.maxAngleConvergenceID; i++)
            {
                //delta mesh
                int agbyid = sim.numberOfAnglesByConvID(i + 1);
                deltaMesh[i] = SimBuffer.PointMesh(agbyid);
                List <Vector3> vtc = new List <Vector3> (agbyid);         //xy = a uv,y=fixedangle
                List <Color>   cl  = new List <Color> (agbyid);           //rg=b uv,ba = m uv;
                List <Vector2> uv  = new List <Vector2> (agbyid);         //delta rt uv;

                //init rt
                paramRT[i] = new RenderTexture(width, height, 0, RTFormat.ARGB);
                paramRT[i].Create();

                //prepare temp color
                for (int k = 0; k < width * height; k++)
                {
                    if (k < parNum)
                    {
                        Vector2 puv = particleUV[k];
                        paramRTColor[k] = new Color(puv.x, puv.y, 0f, 0f);                      //rg = other end,ba = uv in deltart
                    }
                    else
                    {
                        paramRTColor[k] = Color.clear;
                    }
                }

                //get info
                for (int j = 0; j < angleNum; j++)
                {
                    AngleConstraint2D ag = sim.getAngleConstraint(j);
                    if (ag.convergenceGroupID == i + 1)
                    {
                        int a      = sim.getParticleIndex(ag.ParticleB);
                        int b      = sim.getParticleIndex(ag.ParticleM);
                        int aIndex = sim.getParticleIndex(ag.ParticleA);
                        //if it's free, rg = the other end's uv, else = own uv
                        if (ag.ParticleB.IsFree)
                        {
                            paramRTColor[a].r = particleUV[b].x;
                            paramRTColor[a].g = particleUV[b].y;
                        }
                        if (ag.ParticleM.IsFree)
                        {
                            paramRTColor[b].r = particleUV[a].x;
                            paramRTColor[b].g = particleUV[a].y;
                        }
                        paramRTColor[a].b = paramRTColor[b].b = angleUV[j].x;
                        paramRTColor[a].a = paramRTColor[b].a = angleUV[j].y;

                        //mesh vtc and cl
                        vtc.Add(new Vector3(angleUV[j].x, angleUV[j].y, ag.angle_Fixed));
                        uv.Add(particleUV[aIndex]);
                        cl.Add(new Color(particleUV[a].x, particleUV[a].y, particleUV[b].x, particleUV[b].y));
                    }
                }

                //blit
                tempTex.SetPixels(paramRTColor);
                tempTex.Apply();
                Graphics.Blit(tempTex, paramRT[i]);

                //delta mesh
                deltaMesh[i].vertices = vtc.ToArray();
                deltaMesh[i].colors   = cl.ToArray();
                deltaMesh[i].uv       = uv.ToArray();
                //deltaMesh[i].UploadMeshData(true);
            }

            Extension.ObjDestroy(tempTex);

            //mpb
            mpb = new MaterialPropertyBlock[sim.maxSpringConvergenceID];
            for (int i = 0; i < sim.maxAngleConvergenceID; i++)
            {
                mpb[i] = new MaterialPropertyBlock();
                mpb[i].SetTexture(ID_AngleParamRT, paramRT[i]);
                mpb[i].SetFloat(ID_AngleConstant, sim.Settings.angleConstant);
            }
        }