Exemplo n.º 1
0
    float NeighbourDistancesSumNative()
    {
        kdTreeNative = new KDTreeStruct();
        kdTreeNative.MakeFromPoints(pointsNative);

        float neighbourDistancesSum = 0f;

        for (int i = 0; i < queries.Length; i++)
        {
            int id = kdTreeNative.FindNearest(queries[i]);
            neighbourDistancesSum = neighbourDistancesSum + math.length(queries[i] - pointsNative[id]);
        }

        return(neighbourDistancesSum);
    }
        Velocity Calculate(int i)
        {
            float2 posi            = positions[i];
            float3 currentVelocity = velocities[i];
            float2 velocity        = currentVelocity.xz;

            float previousMaxDistanceSqr = 0.01f;

            for (int k = 0; k < 1; k++)
            {
                int j = kdTree.FindNearest(positions[i], previousMaxDistanceSqr);

                if (i != j && j >= 0)
                {
                    float2 posj = positions[j];

                    float2 relative    = posi - posj;
                    float  distanceSqr = math.dot(relative, relative);
                    previousMaxDistanceSqr = distanceSqr;

                    float str = math.max(0, radiusSqr - distanceSqr) / radiusSqr;
                    velocity += math.normalize(relative) * str * maxForce * dt;
                }
            }

            float2 dampingStr = velocity * damping * dt;

            velocity -= dampingStr;

            float3 collisionVelocity = new float3(velocity.x, 0, velocity.y) - currentVelocity;

            currentVelocity += collisionVelocity;

            if (math.dot(currentVelocity, currentVelocity) > maxVelocitySqr)
            {
                currentVelocity = math.normalize(currentVelocity) * maxVelocity;
            }

            Velocity velocityComponent = new Velocity
            {
                Value             = currentVelocity,
                CollisionVelocity = collisionVelocity
            };

            return(velocityComponent);
        }
Exemplo n.º 3
0
 public void Execute(int i)
 {
     answersJob[i] = kdJob.FindNearest(queriesJob[i]);
 }
Exemplo n.º 4
0
    void Update()
    {
        float neighbourDistancesSum = 0f;

        if (imode == 0)
        {
            for (int i = 0; i < queries.Length; i++)
            {
                int j = kdTreeRegular.FindNearest(queries[i]);

                if (calculateDistancesSumOnUpdate)
                {
                    neighbourDistancesSum = neighbourDistancesSum + math.length(queries[i] - pointsNative[j]);
                }
            }
        }
        else if (imode == 1)
        {
            for (int i = 0; i < queries.Length; i++)
            {
                int j = kdTreeNative.FindNearest(queries[i]);

                if (calculateDistancesSumOnUpdate)
                {
                    neighbourDistancesSum = neighbourDistancesSum + math.length(queries[i] - pointsNative[j]);
                }
            }
        }
        else if (imode == 2)
        {
            int processorCount = System.Environment.ProcessorCount;

            new KdSearchJob
            {
                kdJob      = kdTreeNative,
                pointsJob  = pointsNative,
                queriesJob = queries,
                answersJob = answers
            }.Schedule(queries.Length, processorCount).Complete();

            for (int i = 0; i < answers.Length; i++)
            {
                int j = answers[i];

                if (calculateDistancesSumOnUpdate)
                {
                    neighbourDistancesSum = neighbourDistancesSum + math.length(queries[i] - pointsNative[j]);
                }
            }
        }
        else if (imode == 3)
        {
            int processorCount = System.Environment.ProcessorCount;

            new KdSearchJobBursted
            {
                kdJob      = kdTreeNative,
                pointsJob  = pointsNative,
                queriesJob = queries,
                answersJob = answers
            }.Schedule(queries.Length, processorCount).Complete();

            for (int i = 0; i < answers.Length; i++)
            {
                int j = answers[i];

                if (calculateDistancesSumOnUpdate)
                {
                    neighbourDistancesSum = neighbourDistancesSum + math.length(queries[i] - pointsNative[j]);
                }
            }
        }

        if (calculateDistancesSumOnUpdate)
        {
            Debug.Log(neighbourDistancesSum);
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            imode = 0;
            Debug.Log("Running with regular arrays");
        }
        else if (Input.GetKeyDown(KeyCode.B))
        {
            imode = 1;
            Debug.Log("Running with native arrays");
        }
        else if (Input.GetKeyDown(KeyCode.C))
        {
            imode = 2;
            Debug.Log("Running jobified");
        }
        else if (Input.GetKeyDown(KeyCode.D))
        {
            imode = 3;
            Debug.Log("Running jobified with burst");
        }
    }