Esempio n. 1
0
 public override void FlexStart(FlexSolver solver, FlexContainer cntr, FlexParameters parameters)
 {
     for (int i = 0; i < cntr.m_particlesCount; i++)
     {
         Collider   collider  = GetComponent <Collider>();
         Collider[] colliders = Physics.OverlapSphere(cntr.m_particles[i].pos, 1.0f);
         foreach (Collider c in colliders)
         {
             if (c == collider)
             {
                 m_lockedParticlesIds.Add(i);
                 m_lockedParticlesMasses.Add(cntr.m_particles[i].invMass);
                 cntr.m_particles[i].invMass = 0.0f;
             }
         }
     }
 }
Esempio n. 2
0
 public override void PostContainerUpdate(FlexSolver solver, FlexContainer cntr, FlexParameters parameters)
 {
     for (int i = 0; i < m_lockedParticlesIds.Count; i++)
     {
         if (m_lock)
         {
             cntr.m_particles[m_lockedParticlesIds[i]].invMass = 0.0f;
         }
         else
         {
             cntr.m_particles[m_lockedParticlesIds[i]].invMass = m_lockedParticlesMasses[i];
         }
     }
 }
Esempio n. 3
0
        void Start()
        {
            if (m_cntr == null)
            {
                m_cntr = GetComponent <FlexContainer>();
            }

            if (m_diffuseParticles == null)
            {
                m_diffuseParticles = GetComponent <FlexDiffuseParticles>();
            }

            if (m_parameters == null)
            {
                m_parameters = GetComponent <FlexParameters>();
            }

            if (m_colliders == null)
            {
                m_colliders = GetComponent <FlexColliders>();
            }

            m_errorCallback = new Flex.ErrorCallback(this.ErrorCallback);

            m_timers = new Flex.Timers();

            Flex.Error flexErr = Flex.Init(100, m_errorCallback, -1);

            Debug.Log("NVidia FleX v" + Flex.GetVersion());
            if (flexErr != Flex.Error.eFlexErrorNone)
            {
                Debug.LogError("FlexInit: " + flexErr);
            }

            if (m_diffuseParticles)
            {
                m_solverPtr = Flex.CreateSolver(m_cntr.m_maxParticlesCount, m_diffuseParticles.m_maxDiffuseParticlesCount, m_maxNeighboursCount);
            }
            else
            {
                m_solverPtr = Flex.CreateSolver(m_cntr.m_maxParticlesCount, 0, m_maxNeighboursCount);
            }

            m_parameters.GetParams(ref m_params);
            Flex.SetParams(m_solverPtr, ref m_params);

            m_cntr.UpdateContainer();

            m_processors = FindObjectsOfType <FlexProcessor>();
            foreach (FlexProcessor fp in m_processors)
            {
                fp.FlexStart(this, m_cntr, m_parameters);
            }

            PushParticlesToGPU(m_solverPtr, m_cntr, Flex.Memory.eFlexMemoryHost);

            PushConstraintsToGPU(m_solverPtr, m_cntr, Flex.Memory.eFlexMemoryHost);

            if (m_colliders)
            {
                m_colliders.ProcessColliders(m_solverPtr, Flex.Memory.eFlexMemoryHost);
            }

            //Flex.SetShapes(m_solverPtr, m_cnt.shapeGeometry, shapeGeometry.Length, shapeAabbMin, shapeAabbMax, shapeStarts, shapePositions, shapeRotations,
            //          shapePrevPositions, shapePrevRotations, shapeFlags, shapeStarts.Length, Flex.Memory.eFlexMemoryHost);
        }
Esempio n. 4
0
 // Override to modify the data after the container was updated
 // In other words, use for working directly with the container data.
 public override void PostContainerUpdate(FlexSolver solver, FlexContainer cntr, FlexParameters parameters)
 {
 }
Esempio n. 5
0
 // Override this method to modify the data just before the solver is closed
 public override void FlexClose(FlexSolver solver, FlexContainer cntr, FlexParameters parameters)
 {
 }
Esempio n. 6
0
 // Use this for initialization
 void Start()
 {
     m_params = GetComponent <FlexParameters>();
 }
Esempio n. 7
0
        // Override to modify the data after the flex game objects were updated but before the data from them was copied to the container arrays
        // In other words, use this for working with flex game objects.
        public override void PreContainerUpdate(FlexSolver solver, FlexContainer cntr, FlexParameters parameters)
        {
            const int maxCopies = 2048;
            const int maxEdits  = 2048;

            FlexExt.FlexExtTearingParticleClone[] particleCopies = new FlexExt.FlexExtTearingParticleClone[maxCopies];
            int numParticleCopies = 0;

            FlexExt.FlexExtTearingMeshEdit[] triangleEdits = new FlexExt.FlexExtTearingMeshEdit[maxEdits];
            int numTriangleEdits = 0;

            int numParticles = m_flexParticles.m_particlesCount;
            int numSprings   = m_flexSprings.m_springsCount;

            uFlexAPI.uFlexTearClothMesh(ref numParticles, numParticles * 2, numSprings, m_flexParticles.m_particles, m_flexSprings.m_springIndices, m_flexSprings.m_springRestLengths, m_flexSprings.m_springCoefficients, m_flexTriangles.m_triangleIndices, m_clothMeshPtr,
                                        m_maxStrain, m_maxSplits, particleCopies, ref numParticleCopies, maxCopies, triangleEdits, ref numTriangleEdits, maxEdits);

            // copy particles
            for (int i = 0; i < numParticleCopies; ++i)
            {
                int srcIndex  = particleCopies[i].srcIndex;
                int destIndex = particleCopies[i].destIndex;

                m_flexParticles.m_particles[destIndex]         = m_flexParticles.m_particles[srcIndex];
                m_flexParticles.m_restParticles[destIndex]     = m_flexParticles.m_restParticles[srcIndex];
                m_flexParticles.m_velocities[destIndex]        = m_flexParticles.m_velocities[srcIndex];
                m_flexParticles.m_colours[destIndex]           = m_flexParticles.m_colours[srcIndex];
                m_flexParticles.m_phases[destIndex]            = m_flexParticles.m_phases[srcIndex];
                m_flexParticles.m_particlesActivity[destIndex] = m_flexParticles.m_particlesActivity[srcIndex];

                mappingsRev[destIndex] = mappingsRev[srcIndex];

                //deactivate inflation if there is any break
                if (m_flexInflatable != null)
                {
                    m_flexInflatable.enabled = false;
                }
            }

            // apply triangle modifications to index buffer
            for (int i = 0; i < numTriangleEdits; ++i)
            {
                int index    = triangleEdits[i].triIndex;
                int newValue = triangleEdits[i].newParticleIndex;

                m_flexTriangles.m_triangleIndices[index] = newValue;
            }


            m_flexParticles.m_particlesCount += numParticleCopies;
        }
Esempio n. 8
0
 // Override this method to modify the data just before the solver is closed
 public virtual void FlexClose(FlexSolver solver, FlexContainer cntr, FlexParameters parameters)
 {
 }
Esempio n. 9
0
 // Override to modify the data after the container was updated
 // In other words, use for working directly with the container data.
 public virtual void PostContainerUpdate(FlexSolver solver, FlexContainer cntr, FlexParameters parameters)
 {
 }
        //void Start()
        //{
        //    m_Dropdown = FindObjectOfType<Dropdown>().GetComponent<Dropdown>();
        //    m_Dropdown.onValueChanged.AddListener(delegate { DeformCharacter(m_Dropdown); });
        //    //m_Deform.AddListener(delegate { deformParticle(flexCont, pInd, pVect); });
        //    //m_Dropdown.onValueChanged.AddListener(delegate { assignDeform = true; PostContainerUpdate(flexSolver, flexCont, flexParams); });

        //}

        // Start is called before the first frame update
        public override void PostContainerUpdate(FlexSolver solver, FlexContainer cntr, FlexParameters parameters)
        {
            //flexSolver.m_solverSubSteps = (int) solverSubSteps;
            //flexParams.m_numIterations = (int) numOfIterations;

            //solver.m_solverSubSteps = (int)solverSubSteps;
            //parameters.m_numIterations = (int)numOfIterations;

            //In Child objects
            //if OntriggerEnter(Coll)
            //  if (Coll.tag = "fear", "happy", "sad", "excited")
            //     Set appropriate behavior/change box collider position/change iterations or solver substeps
            if (assignDeform)
            {
                //print(pInd);
                //print(pVect);
                print("Assign Deform");
                deformParticle(/*flexCont*/ cntr, pInd, pVect);
                assignDeform = false;
            }

            if (resetDeform)
            {
                resetParticle(cntr, pInd);
                resetDeform = false;
            }
        }
Esempio n. 11
0
        public override void PostContainerUpdate(FlexSolver solver, FlexContainer cntr, FlexParameters parameters)
        {
            if (Input.GetMouseButtonDown(0))
            {
                // need up to date positions
                //    Flex.GetParticles(m_solverPtr, m_cntr.m_particlesHndl.AddrOfPinnedObject(), m_cntr.m_maxParticlesCount, Flex.Memory.eFlexMemoryHost);
                //    Flex.GetVelocities(m_solverPtr, m_cntr.m_velocitiesHndl.AddrOfPinnedObject(), m_cntr.m_maxParticlesCount, Flex.Memory.eFlexMemoryHost);

                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                m_mouseParticle = PickParticle(ray.origin, ray.direction, cntr.m_particles, cntr.m_phases, cntr.m_particlesCount, parameters.m_radius * 0.8f, ref m_mouseT);

                if (m_mouseParticle != -1)
                {
                    Debug.Log("picked: " + m_mouseParticle);

                    m_mousePos  = ray.origin + ray.direction * m_mouseT;
                    m_mouseMass = cntr.m_particles[m_mouseParticle].invMass;
                    cntr.m_particles[m_mouseParticle].invMass = 0.0f;

                    //     Flex.SetParticles(m_solverPtr, m_cntr.m_particles, m_cntr.m_particlesCount, Flex.Memory.eFlexMemoryHost);
                }
            }

            if (Input.GetMouseButtonUp(0))
            {
                if (m_mouseParticle != -1)
                {
                    cntr.m_particles[m_mouseParticle].invMass = m_mouseMass;
                    m_mouseParticle = -1;

                    // need to update positions straight away otherwise particle might be left with increased mass
                    //       Flex.SetParticles(m_solverPtr, m_cntr.m_particles, m_cntr.m_particlesCount, Flex.Memory.eFlexMemoryHost);
                }
            }

            if (m_mouseParticle != -1)
            {
                //    Flex.GetParticles(m_solverPtr, m_cntr.m_particlesHndl.AddrOfPinnedObject(), m_cntr.m_maxParticlesCount, Flex.Memory.eFlexMemoryHost);
                //    Flex.GetVelocities(m_solverPtr, m_cntr.m_velocitiesHndl.AddrOfPinnedObject(), m_cntr.m_maxParticlesCount, Flex.Memory.eFlexMemoryHost);

                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                m_mousePos = ray.origin + ray.direction * m_mouseT;

                Vector3 pos   = cntr.m_particles[m_mouseParticle].pos;
                Vector3 p     = Vector3.Lerp(pos, m_mousePos, 0.8f);
                Vector3 delta = p - pos;

                cntr.m_particles[m_mouseParticle].pos = p;
                cntr.m_velocities[m_mouseParticle]    = delta / Time.fixedTime;

                //    Flex.SetParticles(m_solverPtr, m_cntr.m_particlesHndl.AddrOfPinnedObject(), m_cntr.m_particlesCount, Flex.Memory.eFlexMemoryHost);
                //    Flex.SetVelocities(m_solverPtr, m_cntr.m_velocitiesHndl.AddrOfPinnedObject(), m_cntr.m_particlesCount, Flex.Memory.eFlexMemoryHost);
            }
        }
Esempio n. 12
0
        public override void PreContainerUpdate(FlexSolver solver, FlexContainer cntr, FlexParameters parameters)
        {
            Vector3 vel = transform.forward * m_speed;
            Vector3 pos = transform.position;


            if (Input.GetKeyDown(m_key))
            {
                m_color = new Color(Random.value, Random.value, Random.value, 1);
            }

            if (Input.GetKey(m_key) || m_alwaysOn)
            {
                for (int i = m_id; i < m_id + m_rate && i < m_particles.m_particlesCount; i++)
                {
                    m_particles.m_particlesActivity[i] = true;
                    m_particles.m_particles[i].pos     = pos + UnityEngine.Random.insideUnitSphere * m_radius;
                    m_particles.m_velocities[i]        = vel;
                    m_particles.m_colours[i]           = m_color;
                }

                m_id += m_rate;

                if (m_loop && m_id > m_particles.m_particlesCount - 1)
                {
                    m_id = 0;
                }
            }
        }