void DistributeForces()
        {
            //easy fix for when forces get registered before system has initialized resources
            if (m_forcesBuffer == null)
            {
                return;
            }

            //Update data
            for (int t = 0; t < ForceTypeKernelCount; ++t)
            {
                m_forcesCount[t] = 0;
            }

            for (int i = 0; i < m_forcesList.Count; i++)
            {
                TCForce f = m_forcesList[i];

                if (i >= MaxForces)
                {
                    break;
                }

                int t = (f.forceType == ForceType.Turbulence || f.forceType == ForceType.TurbulenceTexture) ? 1 : 0;
                m_forcesReference[t][m_forcesCount[t]] = f;
                m_forcesCount[t]++;
            }
        }
        public void Dispatch()
        {
            if (useBoidsFlocking)
            {
                if (m_boidsFlock == null)
                {
                    CreateBoids();
                }

                m_boidsFlock.UpdateBoids();
            }


            if (MaxForces == 0)
            {
                return;
            }

            if (m_forcesCount[0] > 0)
            {
                m_forcesBuffer[0].SetData(m_forcesStruct[0]);
                ComputeShader.SetBuffer(UpdateForcesKernel, "forces", m_forcesBuffer[0]);
                Manager.SetPariclesToKernel(ComputeShader, UpdateForcesKernel);
                ComputeShader.Dispatch(UpdateForcesKernel, Manager.DispatchCount, m_forcesCount[0], 1);
            }

            if (m_forcesCount[1] > 0)
            {
                m_forcesBuffer[1].SetData(m_forcesStruct[1]);
                ComputeShader.SetBuffer(UpdateTurbulenceForcesKernel, "turbulenceForces", m_forcesBuffer[1]);

                for (int k = 0; k < m_forcesCount[1]; ++k)
                {
                    TCForce force = m_forcesReference[1][k];

                    if (force.CurrentForceVolume == null)
                    {
                        continue;
                    }

                    ComputeShader.SetTexture(UpdateTurbulenceForcesKernel, "turbulenceTexture", force.CurrentForceVolume);

                    Matrix4x4 rotation = Matrix4x4.TRS(Vector3.zero, force.transform.rotation, Vector3.one);
                    TCHelper.SetMatrix3(ComputeShader, "turbulenceRotation", rotation);
                    TCHelper.SetMatrix3(ComputeShader, "invTurbulenceRotation", rotation.inverse);
                    ComputeShader.SetInt("turbulenceKernelOffset", k);
                    Manager.SetPariclesToKernel(ComputeShader, UpdateTurbulenceForcesKernel);

                    ComputeShader.Dispatch(UpdateTurbulenceForcesKernel, Manager.DispatchCount, 1, 1);
                }
            }
        }
Ejemplo n.º 3
0
    public TCNoiseForceGenerator(TCForce target)
    {
        m_target = target;

        arrowMat  = Resources.Load("ArrowMat", typeof(Material)) as Material;
        arrowMesh = Resources.Load("Arrow1", typeof(Mesh)) as Mesh;

        var triangles = arrowMesh.triangles;
        var vertices  = arrowMesh.vertices;

        arrowBuffer = new ComputeBuffer(triangles.Length, 12);
        arrowBuffer.SetData(triangles.Select(tri => vertices[tri]).ToArray());
    }
        public TCNoiseForceVisualize(TCForce target)
        {
            m_target = target;

            arrowMat = Resources.Load("Editor/ArrowMat", typeof(Material)) as Material;
            var arrowMesh = Resources.Load("Editor/Arrow1", typeof(Mesh)) as Mesh;

            Debug.Assert(arrowMesh != null, nameof(arrowMesh) + " != null");
            var triangles = arrowMesh.triangles;
            var vertices  = arrowMesh.vertices;

            arrowBuffer = new ComputeBuffer(triangles.Length, 12);
            arrowBuffer.SetData(triangles.Select(tri => vertices[tri]).ToArray());
        }
Ejemplo n.º 5
0
    void OnGUI()
    {
        float width = Screen.width;
        float height = Screen.height;

        if (m_demo == 4)
        {
            if (m_forcesExample == null)
            {
                GameObject go = GameObject.Find("ForceExampleForce");

                if (go != null)
                    m_forcesExample = go.GetComponent<TCForce>();
            }

            if (m_forcesExample != null)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Force type: ", GUILayout.Width(100.0f));
                string forceType = m_forcesExample.forceType.ToString();

                if (forceType == "Vector")
                    forceType += " (up)";

                if (forceType == "Radial" && m_forcesExample.power < 0)
                    forceType += " (inward)";

                GUILayout.Label(forceType);
                GUILayout.EndHorizontal();
            }
        }

        GUI.BeginGroup(new Rect(0.0f, height - 60.0f, width, 60.0f), "", "Box");

        GUI.enabled = m_demo > 0;
        if (GUI.Button(new Rect(10.0f, 10.0f, 100.0f, 40.0f), "Previous demo"))
            PreviousDemo();
        GUI.enabled = true;

        GUI.Label(new Rect(155.0f, 25.0f, 350.0f, 60.0f), s_levelInfo[m_demo]);
        GUI.Label(new Rect(width / 2.0f + 75.0f, 25.0f, 150.0f, 60.0f), "FPS: " + m_fpsText);

        GUI.enabled = m_demo < 6;
        if (GUI.Button(new Rect(width - 10.0f - 150.0f, 10.0f, 100.0f, 40.0f), "Next demo"))
            NextDemo();
        GUI.enabled = true;

        GUI.EndGroup();
    }
        void CreateBuffers()
        {
            if (MaxForces == 0)
            {
                return;
            }

            for (int t = 0; t < ForceTypeKernelCount; ++t)
            {
                if (m_forcesBuffer[t] != null)
                {
                    m_forcesBuffer[t].Release();
                }

                m_forcesBuffer[t]    = new ComputeBuffer(MaxForces, ForcesStride);
                m_forcesStruct[t]    = new Force[MaxForces];
                m_forcesReference[t] = new TCForce[MaxForces];
            }
        }
        public void RegisterForce(TCForce force)
        {
            if (m_forcesList == null)
            {
                m_forcesList = new List <TCForce>();
            }

            if (!m_forcesList.Contains(force))
            {
                int objLayerMask = (1 << force.gameObject.layer);

                if ((ForceLayers.value & objLayerMask) <= 0)
                {
                    return;
                }

                m_forcesList.Add(force);
            }
        }
        float GetForcePoints(TCForce force)
        {
            if (BaseForces != null && BaseForces.Contains(force))
            {
                return(float.MaxValue);
            }

            if (!force.enabled)
            {
                return(float.MinValue);
            }

            if (force.transform.parent == Transform)
            {
                return(float.MaxValue);
            }

            float projectedForce = force.power;
            float dist           = (force.transform.position - Transform.position).magnitude;

            switch (force.attenuationType)
            {
            case AttenuationType.Linear:
            case AttenuationType.EaseInOut:
                projectedForce = projectedForce * (1.0f - dist / force.radius.Max * force.Attenuation);
                break;


            case AttenuationType.Divide:
                projectedForce = Mathf.Lerp(projectedForce, projectedForce / dist, force.Attenuation);
                break;
            }


            var points = Mathf.Abs(projectedForce);

            return(points);
        }
Ejemplo n.º 9
0
 // Use this for initialization
 void Start()
 {
     force      = GetComponent <TCForce>();
     startPower = force.power;
     startTime  = Time.time;
 }
Ejemplo n.º 10
0
 void Start()
 {
     m_force      = GetComponent <TCForce>();
     m_startPower = m_force.power;
     m_startTime  = Time.time;
 }
Ejemplo n.º 11
0
 // Use this for initialization
 void Start()
 {
     force = GetComponent<TCForce>();
     startPower = force.power;
     startTime = Time.time;
 }
Ejemplo n.º 12
0
        protected override void Bind()
        {
            DistributeForces();


            if (MaxForces == 0 || NumForcesTotal == 0)
            {
                return;
            }

            Vector3 parentPosition = Vector3.zero;

            if (Transform.parent != null)
            {
                parentPosition = Transform.parent.position;
            }


            for (int t = 0; t < ForceTypeKernelCount; ++t)
            {
                for (int f = 0; f < m_forcesCount[t]; ++f)
                {
                    Force   curForce = m_forcesStruct[t][f];
                    TCForce force    = m_forcesReference[t][f];

                    Transform forceTransform = force.transform;
                    Vector3   forcePos       = forceTransform.position;

                    curForce.type = (uint)force.forceType;

                    curForce.axisX = forceTransform.right;
                    curForce.axisY = forceTransform.up;
                    curForce.axisZ = forceTransform.forward;

                    curForce.minRadius   = -1.0f;
                    curForce.attenuation = force.Attenuation;

                    switch (force.shape)
                    {
                    case ForceShape.Sphere:
                        curForce.radius = force.radius.Max;

                        if (force.radius.IsConstant)
                        {
                            curForce.minRadius = -1.0f;
                        }
                        else
                        {
                            curForce.minRadius = force.radius.Min * force.radius.Min;
                        }

                        curForce.enclosingRadius = force.radius.Max;
                        curForce.boxSize         = Vector3.zero;
                        curForce.vtype           = 0;
                        break;

                    case ForceShape.Capsule:
                        float xzScale = Mathf.Max(force.transform.localScale.x, force.transform.localScale.z);

                        float axisHeight = force.height / 2.0f * force.transform.localScale.y - force.radius.Max * xzScale;
                        axisHeight               = Mathf.Max(0, axisHeight);
                        curForce.boxSize         = new Vector3(0.0f, axisHeight * force.transform.localScale.y, 0.0f);
                        curForce.radius          = force.radius.Max;
                        curForce.enclosingRadius = curForce.radius + force.height / 2.0f * force.transform.localScale.y;
                        curForce.vtype           = 1;
                        break;

                    case ForceShape.Box:
                        curForce.radius          = 0.1f;
                        curForce.boxSize         = force.boxSize * 0.5f - new Vector3(0.1f, 0.1f, 0.1f);
                        curForce.boxSize         = Vector3.Scale(curForce.boxSize, forceTransform.localScale);
                        curForce.enclosingRadius = Mathf.Max(force.boxSize.x, force.boxSize.y, force.boxSize.z) * 0.5f;
                        curForce.vtype           = 2;
                        curForce.attenuation     = 0;
                        break;

                    case ForceShape.Hemisphere:
                        curForce.radius  = 0.1f;
                        curForce.boxSize = Vector3.Scale(new Vector3(force.radius.Max, force.radius.Max, force.radius.Max),
                                                         force.transform.localScale);
                        curForce.enclosingRadius = force.radius.Max;
                        curForce.vtype           = 3;
                        break;

                    case ForceShape.Disc:
                        curForce.minRadius = -1.0f;
                        curForce.vtype     = (uint)force.discType;
                        curForce.radius    = Mathf.Max(0.1f, force.discRounding);

                        float rMin = force.radius.IsConstant ? 0.0f : force.radius.Min;
                        float rMax = force.radius.Max;

                        curForce.boxSize = new Vector3(rMin, force.discHeight / 2.0f - force.discRounding, rMax);

                        curForce.enclosingRadius = Mathf.Max(force.discHeight / 2.0f, force.radius.Max + force.discRounding);

                        switch (force.discType)
                        {
                        case DiscType.Full:
                            curForce.vtype = 4;
                            break;

                        case DiscType.Half:
                            curForce.vtype = 5;
                            break;

                        case DiscType.Quarter:
                            curForce.vtype = 6;
                            break;
                        }
                        break;

                    case ForceShape.Constant:
                        curForce.radius          = 0.1f;
                        curForce.attenuation     = 0.0f;
                        curForce.enclosingRadius = -1.0f;
                        curForce.vtype           = 7;
                        break;
                    }

                    curForce.force = force.power * Manager.ParticleTimeDelta;

                    switch (force.forceType)
                    {
                    case ForceType.Vector:
                        if (force.forceDirection != Vector3.zero)
                        {
                            if (force.forceDirectionSpace == TCForce.ForceSpace.World)
                            {
                                curForce.axis = Vector3.Normalize(force.forceDirection);
                            }
                            else
                            {
                                curForce.axis = force.transform.TransformDirection(Vector3.Normalize(force.forceDirection));
                            }
                        }
                        break;

                    case ForceType.Vortex:
                        curForce.axis        = force.vortexAxis;
                        curForce.inwardForce = force.inwardForce / 1000.0f * Manager.ParticleTimeDelta * 60.0f;
                        break;

                    case ForceType.Turbulence:
                    case ForceType.TurbulenceTexture:
                        curForce.axis = force.noiseExtents;
                        break;

                    default:
                        curForce.axis = Vector3.zero;
                        break;
                    }

                    if (force.IsPrimaryForce)
                    {
                        curForce.velocity = force.Velocity * force.InheritVelocity;
                    }
                    else
                    {
                        curForce.velocity = Vector3.zero;
                    }

                    curForce.attenType        = (uint)force.attenuationType;
                    curForce.turbulencePosFac = 1.0f - force.smoothness;

                    curForce.pos = forcePos;

                    switch (Manager.SimulationSpace)
                    {
                    case Space.Local:
                        curForce.pos   = Transform.InverseTransformPoint(curForce.pos);
                        curForce.axisX = Transform.InverseTransformDirection(curForce.axisX);
                        curForce.axisY = Transform.InverseTransformDirection(curForce.axisY);
                        curForce.axisZ = Transform.InverseTransformDirection(curForce.axisZ);
                        break;


                    case Space.Parent:
                        curForce.pos = forcePos - parentPosition;
                        break;
                    }

                    m_forcesStruct[t][f] = curForce;
                }
            }
        }
Ejemplo n.º 13
0
 public void RemoveForce(TCForce force)
 {
     m_forcesList.Remove(force);
 }
 public TCNoiseForceGenerator(TCForce target)
 {
     this.target = target;
 }
Ejemplo n.º 15
0
 /// <summary>.
 /// Remove a force from the system. Doesn't have to be called manually usually
 /// </summary>
 /// <param name="force">The force to remove</param>
 public void RemoveForce(TCForce force)
 {
     _forcesManager.RemoveForce(force);
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Register a force for use. Doesn't have to be called manually usually
 /// </summary>
 /// <param name="force">The force to register</param>
 public void RegisterForce(TCForce force)
 {
     _forcesManager.RegisterForce(force);
 }