void TeleportParticles(FlexContainer.ParticleData _particleData)
        {
            Matrix4x4 offset = Matrix4x4.TRS(m_teleportPosition, Quaternion.identity, Vector3.one)
                               * Matrix4x4.TRS(Vector3.zero, m_teleportRotation, Vector3.one)
                               * Matrix4x4.TRS(Vector3.zero, Quaternion.Inverse(transform.rotation), Vector3.one)
                               * Matrix4x4.TRS(-transform.position, Quaternion.identity, Vector3.one);

            if (m_currentAsset && m_instanceHandle)
            {
                FlexExt.Instance instance = m_instanceHandle.instance;
                int[]            indices  = new int[instance.numParticles];
                FlexUtils.FastCopy(instance.particleIndices, indices);
                foreach (var index in indices)
                {
                    Vector4 particle = _particleData.GetParticle(index);
                    float   storeW   = particle.w;
                    particle   = offset.MultiplyPoint3x4(particle);
                    particle.w = storeW;
                    _particleData.SetParticle(index, particle);
                    _particleData.SetVelocity(index, Vector3.zero);
                }
            }
            transform.position   = m_teleportPosition;
            transform.rotation   = m_teleportRotation;
            transform.hasChanged = false;
        }
 void ApplyImpulses(FlexContainer.ParticleData _particleData)
 {
     if (m_currentAsset && m_instanceHandle)
     {
         FlexExt.Instance instance = m_instanceHandle.instance;
         int[]            indices  = new int[instance.numParticles];
         FlexUtils.FastCopy(instance.particleIndices, indices);
         foreach (var info in m_impulses)
         {
             if (info.impulse.sqrMagnitude < float.Epsilon)
             {
                 continue;
             }
             float mass = 0;
             foreach (var index in indices)
             {
                 if (info.particle == -1 || info.particle == index)
                 {
                     Vector4 particle = _particleData.GetParticle(index);
                     mass += 1.0f / particle.w;
                 }
             }
             if (mass < float.Epsilon)
             {
                 continue;
             }
             Vector3 velocityChange = info.impulse / mass;
             foreach (var index in indices)
             {
                 _particleData.SetVelocity(index, _particleData.GetVelocity(index) + velocityChange);
             }
         }
     }
 }
 protected virtual void CreateInstance()
 {
     if (m_currentContainer && m_currentContainer.handle && m_currentAsset && m_currentAsset.handle)
     {
         int        group = m_particleGroup < 0 ? ++sm_nextGroup : m_particleGroup;
         Flex.Phase flags = Flex.Phase.Default;
         if (m_selfCollide)
         {
             flags |= Flex.Phase.SelfCollide;
         }
         if (m_selfCollideFilter)
         {
             flags |= Flex.Phase.SelfCollideFilter;
         }
         if (m_fluid)
         {
             flags |= Flex.Phase.Fluid;
         }
         m_phase          = Flex.MakePhase(group, flags);
         m_instanceHandle = m_currentContainer.CreateInstance(m_currentAsset.handle, transform.localToWorldMatrix, Vector3.zero, m_phase, m_massScale);
         if (m_instanceHandle)
         {
             FlexExt.Instance instance = m_instanceHandle.instance;
             m_indices    = new int[m_currentAsset.maxParticles];
             m_indexCount = instance.numParticles;
             if (m_indexCount > 0)
             {
                 FlexUtils.FastCopy(instance.particleIndices, 0, ref m_indices[0], 0, sizeof(int) * m_indexCount);
             }
         }
     }
 }
示例#4
0
            public int GetPhase(int _index)
            {
                int phase = 0;

                FlexUtils.FastCopy(particleData.phases, _index * 4, ref phase, 0, 4);
                return(phase);
            }
示例#5
0
        void CopyBufferContent(Flex.Buffer source, ComputeBuffer target)
        {
            IntPtr ptr = Flex.Map(source, Flex.MapFlags.Wait);

            FlexUtils.FastCopy(ptr, m_temporaryBuffer);
            target.SetData(m_temporaryBuffer);
            Flex.Unmap(source);
        }
示例#6
0
 public void UpdateBuffer(FlexExt.ParticleData _particleData)
 {
     if (m_particleArray == null || m_particleArray.Length != m_maxParticles)
     {
         m_particleArray = new Vector4[m_maxParticles];
     }
     FlexUtils.FastCopy(_particleData.particles, m_particleArray);
     m_particleBuffer.SetData(m_particleArray);
 }
        protected void StoreAsset(FlexExt.Asset _asset)
        {
            m_particles = new Vector4[_asset.numParticles];
            if (_asset.numParticles > 0)
            {
                FlexUtils.FastCopy(_asset.particles, m_particles);
            }
            m_maxParticles = _asset.maxParticles;

            m_springIndices = new int[_asset.numSprings * 2];
            if (_asset.numSprings > 0)
            {
                FlexUtils.FastCopy(_asset.springIndices, m_springIndices);
            }
            m_springCoefficients = new float[_asset.numSprings];
            if (_asset.numSprings > 0)
            {
                FlexUtils.FastCopy(_asset.springCoefficients, m_springCoefficients);
            }
            m_springRestLengths = new float[_asset.numSprings];
            if (_asset.numSprings > 0)
            {
                FlexUtils.FastCopy(_asset.springRestLengths, m_springRestLengths);
            }

            m_shapeIndices = new int[_asset.numShapeIndices];
            if (_asset.numShapeIndices > 0)
            {
                FlexUtils.FastCopy(_asset.shapeIndices, m_shapeIndices);
            }
            m_shapeOffsets = new int[_asset.numShapes];
            if (_asset.numShapes > 0)
            {
                FlexUtils.FastCopy(_asset.shapeOffsets, m_shapeOffsets);
            }
            m_shapeCoefficients = new float[_asset.numShapes];
            if (_asset.numShapes > 0)
            {
                FlexUtils.FastCopy(_asset.shapeCoefficients, m_shapeCoefficients);
            }
            m_shapeCenters = new Vector3[_asset.numShapes];
            if (_asset.numShapes > 0)
            {
                FlexUtils.FastCopy(_asset.shapeCenters, m_shapeCenters);
            }

            m_triangleIndices = new int[_asset.numTriangles * 3];
            if (_asset.numTriangles > 0)
            {
                FlexUtils.FastCopy(_asset.triangleIndices, m_triangleIndices);
            }

            m_inflatable          = _asset.inflatable;
            m_inflatableVolume    = _asset.inflatableVolume;
            m_inflatablePressure  = _asset.inflatablePressure;
            m_inflatableStiffness = _asset.inflatableStiffness;
        }
 protected void SetIndices(int[] _indices, int _count)
 {
     m_indexCount = Mathf.Min(_count, m_currentAsset.maxParticles);
     Array.Copy(_indices, m_indices, m_indexCount);
     FlexExt.Instance instance = m_instanceHandle.instance;
     instance.numParticles = m_indexCount;
     if (m_indexCount > 0)
     {
         FlexUtils.FastCopy(ref m_indices[0], 0, instance.particleIndices, 0, sizeof(int) * m_indexCount);
     }
     m_instanceHandle.instance = instance;
 }
示例#9
0
        //This function is to set the initial velocity of the fluid
        bool PresetVelocities(FlexContainer.ParticleData _particleData)
        {
            Vector3 initVelocity = new Vector3(0, 0, -2.0f);              //Change the Vector to adjust the intial velocity

            FlexExt.Instance instance = m_instanceHandle.instance;
            int[]            indices  = new int[instance.numParticles];
            FlexUtils.FastCopy(instance.particleIndices, indices);

            foreach (var index in indices)
            {
                _particleData.SetVelocity(index, initVelocity);
            }
            return(true);
        }
示例#10
0
        protected override void OnFlexUpdate(FlexContainer.ParticleData _particleData)
        {
            if (Application.isPlaying && handle)
            {
                FlexExt.Instance instance = handle.instance;
                Vector3          position = Vector3.zero;
                FlexUtils.FastCopy(instance.shapeTranslations, 0, ref position, 0, 12);
                Quaternion rotation = Quaternion.identity;
                FlexUtils.FastCopy(instance.shapeRotations, 0, ref rotation, 0, 16);
                transform.position   = position - rotation * m_asset.shapeCenters[0];
                transform.rotation   = rotation;
                transform.hasChanged = false;
            }

            base.OnFlexUpdate(_particleData);
        }
        protected void CreateAsset()
        {
            m_asset = default(FlexExt.Asset);
            if (m_particles.Length > 0)
            {
                m_asset.particles = Marshal.AllocHGlobal(m_particles.Length * Marshal.SizeOf(default(Vector4)));
                FlexUtils.FastCopy(m_particles, m_asset.particles);
                m_asset.numParticles = m_particles.Length;
            }
            m_asset.maxParticles = Mathf.Max(m_maxParticles, m_particles.Length);
            if (m_springIndices.Length > 0 && m_springCoefficients.Length > 0 && m_springRestLengths.Length > 0)
            {
                m_asset.springIndices = Marshal.AllocHGlobal(m_springIndices.Length * Marshal.SizeOf(default(int)));
                FlexUtils.FastCopy(m_springIndices, m_asset.springIndices);
                m_asset.springCoefficients = Marshal.AllocHGlobal(m_springCoefficients.Length * Marshal.SizeOf(default(float)));
                FlexUtils.FastCopy(m_springCoefficients, m_asset.springCoefficients);
                m_asset.springRestLengths = Marshal.AllocHGlobal(m_springRestLengths.Length * Marshal.SizeOf(default(float)));
                FlexUtils.FastCopy(m_springRestLengths, m_asset.springRestLengths);
                m_asset.numSprings = m_springRestLengths.Length;
            }
            if (m_shapeIndices.Length > 0 && m_shapeOffsets.Length > 0 && m_shapeCoefficients.Length > 0 && m_shapeCenters.Length > 0)
            {
                m_asset.shapeIndices = Marshal.AllocHGlobal(m_shapeIndices.Length * Marshal.SizeOf(default(int)));
                FlexUtils.FastCopy(m_shapeIndices, m_asset.shapeIndices);
                m_asset.numShapeIndices = m_shapeIndices.Length;
                m_asset.shapeOffsets    = Marshal.AllocHGlobal(m_shapeOffsets.Length * Marshal.SizeOf(default(int)));
                FlexUtils.FastCopy(m_shapeOffsets, m_asset.shapeOffsets);
                m_asset.shapeCoefficients = Marshal.AllocHGlobal(m_shapeCoefficients.Length * Marshal.SizeOf(default(float)));
                FlexUtils.FastCopy(m_shapeCoefficients, m_asset.shapeCoefficients);
                m_asset.shapeCenters = Marshal.AllocHGlobal(m_shapeCenters.Length * Marshal.SizeOf(default(Vector3)));
                FlexUtils.FastCopy(m_shapeCenters, m_asset.shapeCenters);
                m_asset.numShapes = m_shapeCenters.Length;
            }
            if (m_triangleIndices.Length > 0)
            {
                m_asset.triangleIndices = Marshal.AllocHGlobal(m_triangleIndices.Length * Marshal.SizeOf(default(int)));
                FlexUtils.FastCopy(m_triangleIndices, m_asset.triangleIndices);
                m_asset.numTriangles = m_triangleIndices.Length / 3;
            }
            m_asset.inflatable          = m_inflatable;
            m_asset.inflatableVolume    = m_inflatableVolume;
            m_asset.inflatablePressure  = m_inflatablePressure;
            m_asset.inflatableStiffness = m_inflatableStiffness;

            m_assetHandle.Allocate();
            m_assetHandle.asset = m_asset;
        }
        void OnFlexUpdate(FlexContainer.ParticleData _particleData)
        {
            if (m_actor && m_actor.handle && m_skinningBones.Length > 0)
            {
                FlexExt.Instance instance = m_actor.handle.instance;
                int        boneCount      = m_skinningBones.Length;
                Vector3    bonePosition   = Vector3.zero;
                Quaternion boneRotation   = Quaternion.identity;

                for (int i = 0; i < boneCount; ++i)
                {
                    FlexUtils.FastCopy(instance.shapeTranslations, sizeof(float) * 3 * i, ref bonePosition, 0, sizeof(float) * 3);
                    m_skinningBones[i].position = bonePosition;
                    FlexUtils.FastCopy(instance.shapeRotations, sizeof(float) * 4 * i, ref boneRotation, 0, sizeof(float) * 4);
                    m_skinningBones[i].rotation = boneRotation;
                }
            }
        }
示例#13
0
        //This function is to get the velocity of 40 frames
        //And then call the CalcAcceleration function to calculate the accelerations
        bool CoutAccelerations(FlexContainer.ParticleData _particleData)
        {
            FlexExt.Instance instance = m_instanceHandle.instance;
            int[]            indices  = new int[instance.numParticles];
            FlexUtils.FastCopy(instance.particleIndices, indices);
            float totalzVelocity = 0;

            foreach (var index in indices)
            {
                totalzVelocity += _particleData.GetVelocity(index).z;
            }

            frameVelocity.Add(totalzVelocity / indices.Length);

            if (frameVelocity.Count >= 40)                              //Change the 40 to other numbers to adjust the time of data collections
            {
                return(CalcAcceleration());
            }
            else
            {
                return(false);
            }
        }
示例#14
0
 public Vector4 GetNormal(int _index)
 {
     FlexUtils.FastCopy(particleData.normals, _index * 16, ref vec4Buffer, 0, 16);
     return(vec4Buffer);
 }
示例#15
0
 public void SetNormals(int _start, int _count, Vector4[] _normals)
 {
     FlexUtils.FastCopy(ref _normals[0], 0, particleData.normals, _start * 16, _count * 16);
 }
示例#16
0
 public void SetPhase(int _index, int _phase)
 {
     FlexUtils.FastCopy(ref _phase, 0, particleData.phases, _index * 4, 4);
 }
示例#17
0
 public void SetPhases(int _start, int _count, int[] _phases)
 {
     FlexUtils.FastCopy(ref _phases[0], 0, particleData.phases, _start * 4, _count * 4);
 }
示例#18
0
 public void SetVelocity(int _index, Vector3 _velocity)
 {
     FlexUtils.FastCopy(ref _velocity, 0, particleData.velocities, _index * 12, 12);
 }
        void Create()
        {
            m_actor = GetComponentInParent <FlexActor>();
            if (m_actor == null)
            {
                Debug.LogError("_auxFlexDrawPartcles should be parented to a FlexActor");
                Debug.Break();
            }

            if (m_actor && m_actor.handle)
            {
                FlexExt.Instance instance = m_actor.handle.instance;

                int[] indices = new int[instance.numParticles];
                if (instance.numParticles > 0)
                {
                    FlexUtils.FastCopy(instance.particleIndices, indices);
                }

                m_mesh          = new Mesh();
                m_mesh.name     = "Flex Particles";
                m_mesh.vertices = new Vector3[1];

                if (m_actor is FlexSoftActor)
                {
                    int shapeCount = m_actor.asset.shapeOffsets.Length;
                    m_indexBuffers      = new ComputeBuffer[shapeCount];
                    m_particleMaterials = new Material[shapeCount];
                    m_mesh.subMeshCount = shapeCount;
                    for (int i = 0; i < shapeCount; ++i)
                    {
                        int start = i == 0 ? 0 : m_actor.asset.shapeOffsets[i - 1];
                        int count = m_actor.asset.shapeOffsets[i] - start;
                        m_mesh.SetIndices(new int[count], MeshTopology.Points, i);
                        m_indexBuffers[i] = new ComputeBuffer(count, 4);
                        int[] shape = new int[count];
                        for (int j = 0; j < count; ++j)
                        {
                            shape[j] = indices[m_actor.asset.shapeIndices[start + j]];
                        }
                        m_indexBuffers[i].SetData(shape);
                        m_particleMaterials[i]           = new Material(Shader.Find(PARTICLES_SHADER));
                        m_particleMaterials[i].hideFlags = HideFlags.HideAndDontSave;
                    }
                }
                else
                {
                    m_mesh.subMeshCount = 1;
                    m_mesh.SetIndices(new int[0], MeshTopology.Points, 0);
                    m_mesh.SetIndices(new int[instance.numParticles], MeshTopology.Points, 0);
                    m_indexBuffers = new ComputeBuffer[1];
                    if (instance.numParticles > 0)
                    {
                        m_indexBuffers[0] = new ComputeBuffer(instance.numParticles, sizeof(int));
                        m_indexBuffers[0].SetData(indices);
                    }
                    m_particleMaterials              = new Material[1];
                    m_particleMaterials[0]           = new Material(Shader.Find(PARTICLES_SHADER));
                    m_particleMaterials[0].hideFlags = HideFlags.HideAndDontSave;
                }

                m_mesh.bounds = m_actor.bounds;

                MeshFilter meshFilter = GetComponent <MeshFilter>();
                meshFilter.mesh = m_mesh;

                MeshRenderer meshRenderer = GetComponent <MeshRenderer>();
                meshRenderer.materials         = m_particleMaterials;
                meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
#if UNITY_EDITOR
                EditorUtility.SetSelectedRenderState(meshRenderer, EditorSelectedRenderState.Hidden);
#endif
                UpdateMesh();
            }
        }
示例#20
0
 public Vector4 GetRestParticle(int _index)
 {
     FlexUtils.FastCopy(particleData.restParticles, _index * 16, ref vec4Buffer, 0, 16);
     return(vec4Buffer);
 }
示例#21
0
 public void SetParticle(int _index, Vector4 _particle)
 {
     FlexUtils.FastCopy(ref _particle, 0, particleData.particles, _index * 16, 16);
 }
示例#22
0
 public void SetNormal(int _index, Vector4 _normal)
 {
     FlexUtils.FastCopy(ref _normal, 0, particleData.normals, _index * 16, 16);
 }
示例#23
0
 public void GetParticles(int _start, int _count, Vector4[] _particles)
 {
     FlexUtils.FastCopy(particleData.particles, _start * 16, ref _particles[0], 0, _count * 16);
 }
示例#24
0
        public ShapeData(Collider collider)
        {
            bool dynamic = false;

            if (collider is SphereCollider)
            {
                SphereCollider sphereCollider = collider as SphereCollider;
                Vector3        scale          = collider.transform.localScale;
                geometry.sphere.radius = sphereCollider.radius * Mathf.Max(scale.x, scale.y, scale.z);
                flags          = Flex.MakeShapeFlags(Flex.CollisionShapeType.Sphere, dynamic);
                shapeMin       = Vector3.one * -geometry.sphere.radius;
                shapeMax       = Vector3.one * geometry.sphere.radius;
                shapeCenter    = sphereCollider.center;
                shapeCenter.x *= scale.x; shapeCenter.y *= scale.y; shapeCenter.z *= scale.z;
            }
            else if (collider is CapsuleCollider)
            {
                CapsuleCollider capsuleCollider = collider as CapsuleCollider;
                Vector3         scale           = collider.transform.localScale;
                if (capsuleCollider.direction == 1)
                {
                    scale = new Vector3(scale.y, scale.z, scale.x);
                }
                if (capsuleCollider.direction == 2)
                {
                    scale = new Vector3(scale.z, scale.x, scale.y);
                }
                geometry.capsule.radius     = capsuleCollider.radius * Mathf.Max(scale.y, scale.z);
                geometry.capsule.halfHeight = capsuleCollider.height * scale.x * 0.5f - geometry.capsule.radius;
                flags          = Flex.MakeShapeFlags(Flex.CollisionShapeType.Capsule, dynamic);
                shapeMin       = new Vector3(-geometry.capsule.halfHeight - geometry.capsule.radius, -geometry.capsule.radius, -geometry.capsule.radius);
                shapeMax       = new Vector3(geometry.capsule.halfHeight + geometry.capsule.radius, geometry.capsule.radius, geometry.capsule.radius);
                shapeCenter    = capsuleCollider.center;
                shapeCenter.x *= scale.x; shapeCenter.y *= scale.y; shapeCenter.z *= scale.z;
                if (capsuleCollider.direction == 1)
                {
                    shapePreRotation = Quaternion.Euler(0.0f, 0.0f, 90.0f);
                }
                if (capsuleCollider.direction == 2)
                {
                    shapePreRotation = Quaternion.Euler(0.0f, 90.0f, 0.0f);
                }
            }
            else if (collider is BoxCollider)
            {
                BoxCollider boxCollider = collider as BoxCollider;
                Vector3     scale       = collider.transform.localScale;
                Vector3     halfSize    = new Vector3(boxCollider.size.x * scale.x * 0.5f, boxCollider.size.y * scale.y * 0.5f, boxCollider.size.z * scale.z * 0.5f);
                geometry.box.halfExtents = halfSize;
                flags          = Flex.MakeShapeFlags(Flex.CollisionShapeType.Box, dynamic);
                shapeMin       = -halfSize;
                shapeMax       = halfSize;
                shapeCenter    = boxCollider.center;
                shapeCenter.x *= scale.x; shapeCenter.y *= scale.y; shapeCenter.z *= scale.z;
            }
            else if (collider is MeshCollider)
            {
                MeshCollider meshCollider = collider as MeshCollider;
                Mesh         mesh         = meshCollider.sharedMesh;
                if (mesh)
                {
                    Vector3   scale     = collider.transform.localScale;
                    Vector3   boundsMin = new Vector3(mesh.bounds.min.x * scale.x, mesh.bounds.min.y * scale.y, mesh.bounds.min.z * scale.z);
                    Vector3   boundsMax = new Vector3(mesh.bounds.max.x * scale.x, mesh.bounds.max.y * scale.y, mesh.bounds.max.z * scale.z);
                    Vector3[] vertices  = mesh.vertices;
                    int[]     triangles = mesh.triangles;
                    if (vertices.Length > 0 && triangles.Length > 0)
                    {
                        bool useConvex = meshCollider.convex;
                        if (useConvex)
                        {
                            Vector4[] planes     = new Vector4[triangles.Length / 3];
                            Vector3[] bounds     = new Vector3[2];
                            int       planeCount = FlexUtils.ConvexPlanes(ref vertices[0], ref scale, ref triangles[0], triangles.Length / 3, ref planes[0], ref bounds[0]);
                            // Convex meshes in Flex can't have more than 64 faces
                            if (planeCount <= 64)
                            {
                                m_flexConvex = Flex.CreateConvexMesh(FlexContainer.library);
                                Flex.Buffer planeBuffer = Flex.AllocBuffer(FlexContainer.library, planeCount, sizeof(float) * 4, Flex.BufferType.Host);
                                FlexUtils.FastCopy(planes, Flex.Map(planeBuffer)); Flex.Unmap(planeBuffer);
                                Flex.UpdateConvexMesh(FlexContainer.library, m_flexConvex, planeBuffer, planeCount, ref bounds[0], ref bounds[1]);
                                geometry.convexMesh.scale = Vector3.one;
                                geometry.convexMesh.mesh  = m_flexConvex;
                                Flex.FreeBuffer(planeBuffer);
                                flags    = Flex.MakeShapeFlags(Flex.CollisionShapeType.ConvexMesh, dynamic);
                                shapeMin = bounds[0];
                                shapeMax = bounds[1];
                            }
                            else
                            {
                                useConvex = false;
                            }
                        }
                        if (!useConvex)
                        {
                            int[]     uniqueInds          = new int[vertices.Length];
                            int[]     originalToUniqueMap = new int[vertices.Length];
                            int       uniqueVertCount     = FlexExt.CreateWeldedMeshIndices(ref vertices[0], vertices.Length, ref uniqueInds[0], ref originalToUniqueMap[0], 0.0001f); // @@@
                            Vector4[] uniqueVerts         = new Vector4[uniqueVertCount];
                            for (int i = 0; i < uniqueVertCount; ++i)
                            {
                                uniqueVerts[i] = new Vector3(vertices[uniqueInds[i]].x * scale.x, vertices[uniqueInds[i]].y * scale.y, vertices[uniqueInds[i]].z * scale.z);
                            }
                            Flex.Buffer vertexBuffer = Flex.AllocBuffer(FlexContainer.library, uniqueVerts.Length, sizeof(float) * 4, Flex.BufferType.Host);
                            FlexUtils.FastCopy(uniqueVerts, Flex.Map(vertexBuffer)); Flex.Unmap(vertexBuffer);
                            int[] indices = new int[triangles.Length];
                            for (int i = 0; i < triangles.Length; ++i)
                            {
                                indices[i] = originalToUniqueMap[triangles[i]];
                            }
                            Flex.Buffer indexBuffer = Flex.AllocBuffer(FlexContainer.library, indices.Length, sizeof(int), Flex.BufferType.Host);
                            FlexUtils.FastCopy(indices, Flex.Map(indexBuffer)); Flex.Unmap(indexBuffer);
                            m_flexMesh = Flex.CreateTriangleMesh(FlexContainer.library);
                            Flex.UpdateTriangleMesh(FlexContainer.library, m_flexMesh, vertexBuffer, indexBuffer, uniqueVertCount, indices.Length / 3, ref boundsMin, ref boundsMax);
                            geometry.triMesh.scale = Vector3.one;
                            geometry.triMesh.mesh  = m_flexMesh;
                            Flex.FreeBuffer(vertexBuffer);
                            Flex.FreeBuffer(indexBuffer);
                            flags = Flex.MakeShapeFlags(Flex.CollisionShapeType.TriangleMesh, dynamic);
                            Flex.GetTriangleMeshBounds(FlexContainer.library, m_flexMesh, ref shapeMin, ref shapeMax);
                        }
                    }
                }
            }
            shapePrevPosition = collider.transform.position + collider.transform.rotation * shapeCenter;
            shapePrevRotation = collider.transform.rotation * shapePreRotation;
        }
示例#25
0
 public void SetRestParticles(int _start, int _count, Vector4[] _restParticles)
 {
     FlexUtils.FastCopy(ref _restParticles[0], 0, particleData.restParticles, _start * 16, _count * 16);
 }
示例#26
0
        public void UpdateShapes(FlexContainer.ParticleData _particleData)
        {
            int colliderCount = 0;

            foreach (var item in m_colliders)
            {
                Collider collider = item.Key;

                if (!(collider is SphereCollider) && !(collider is CapsuleCollider) && !(collider is BoxCollider) && !(collider is MeshCollider))
                {
                    continue;
                }

                ++colliderCount;
            }
            if (colliderCount > 0)
            {
                if (m_geometryBuffer == null || colliderCount > m_geometryBuffer.count)
                {
                    if (m_geometryBuffer != null)
                    {
                        m_geometryBuffer.Release();
                        m_shapePositionBuffer.Release();
                        m_shapeRotationBuffer.Release();
                        m_shapePrevPositionBuffer.Release();
                        m_shapePrevRotationBuffer.Release();
                        m_shapeFlagsBuffer.Release();
                    }
                    m_geometryBuffer          = new FlexBuffer(FlexContainer.library, colliderCount, sizeof(float) * 4);
                    m_shapePositionBuffer     = new FlexBuffer(FlexContainer.library, colliderCount, sizeof(float) * 4);
                    m_shapeRotationBuffer     = new FlexBuffer(FlexContainer.library, colliderCount, sizeof(float) * 4);
                    m_shapePrevPositionBuffer = new FlexBuffer(FlexContainer.library, colliderCount, sizeof(float) * 4);
                    m_shapePrevRotationBuffer = new FlexBuffer(FlexContainer.library, colliderCount, sizeof(float) * 4);
                    m_shapeFlagsBuffer        = new FlexBuffer(FlexContainer.library, colliderCount, sizeof(int));
                }

                int shapeIndex = 0;

                foreach (var item in m_colliders)
                {
                    Collider  collider  = item.Key;
                    ShapeData shapeData = item.Value;

                    if (!(collider is SphereCollider) && !(collider is CapsuleCollider) && !(collider is BoxCollider) && !(collider is MeshCollider))
                    {
                        continue;
                    }

                    m_geometryBuffer.Set(shapeIndex, shapeData.geometry);
                    m_shapeFlagsBuffer.Set(shapeIndex, shapeData.flags);
                    m_shapePositionBuffer.Set(shapeIndex, (Vector4)(collider.transform.position + collider.transform.rotation * shapeData.shapeCenter));
                    m_shapeRotationBuffer.Set(shapeIndex, collider.transform.rotation * shapeData.shapePreRotation);
                    m_shapePrevPositionBuffer.Set(shapeIndex, (Vector4)shapeData.shapePrevPosition);
                    m_shapePrevRotationBuffer.Set(shapeIndex, shapeData.shapePrevRotation);

                    shapeData.shapePrevPosition = collider.transform.position + collider.transform.rotation * shapeData.shapeCenter;
                    shapeData.shapePrevRotation = collider.transform.rotation * shapeData.shapePreRotation;

                    ++shapeIndex;
                }

                Flex.SetShapes(m_scene.container.solver, m_geometryBuffer.handle,
                               m_shapePositionBuffer.handle, m_shapeRotationBuffer.handle,
                               m_shapePrevPositionBuffer.handle, m_shapePrevRotationBuffer.handle,
                               m_shapeFlagsBuffer.handle, colliderCount);
            }
            else
            {
                Flex.SetShapes(m_scene.container.solver, default(Flex.Buffer),
                               default(Flex.Buffer), default(Flex.Buffer),
                               default(Flex.Buffer), default(Flex.Buffer),
                               default(Flex.Buffer), 0);
            }

            Vector3 lower = Vector3.zero, upper = Vector3.zero;

            FlexUtils.FastCopy(_particleData.particleData.lower, 0, ref lower, 0, sizeof(float) * 3);
            FlexUtils.FastCopy(_particleData.particleData.upper, 0, ref upper, 0, sizeof(float) * 3);

            if (lower.x < upper.x && lower.y < upper.y && lower.z < upper.z)
            {
                m_trigger.transform.position = (upper + lower) * 0.5f;
                m_trigger.size = upper - lower;
            }
        }
示例#27
0
 public void SetRestParticle(int _index, Vector4 _restParticles)
 {
     FlexUtils.FastCopy(ref _restParticles, 0, particleData.restParticles, _index * 16, 16);
 }
示例#28
0
 public Vector3 GetVelocity(int _index)
 {
     FlexUtils.FastCopy(particleData.velocities, _index * 12, ref vec3Buffer, 0, 12);
     return(vec3Buffer);
 }
示例#29
0
 public void SetVelocities(int _start, int _count, Vector3[] _velocities)
 {
     FlexUtils.FastCopy(ref _velocities[0], 0, particleData.velocities, _start * 12, _count * 12);
 }
示例#30
0
 public void Set(int _index, float _value)
 {
     Map(); FlexUtils.FastCopy(ref _value, 0, m_pointer, _index * m_stride, m_stride);
 }