コード例 #1
0
ファイル: Example.cs プロジェクト: blueinsert/Unity3DStudy
    /// <summary>Create a number of agents in circle and restart simulation</summary>
    public void CreateAgents(int num)
    {
        this.m_agentCount = num;

        goals  = new List <Vector3>(m_agentCount);
        colors = new List <Color>(m_agentCount);

        sim.ClearAgents();
        sim.SetTimeStep(m_timeStep);
        sim.SetAgentDefaults(saftyFactor: m_safetyFactor, angleSampleCount: m_angleSampleCount, velSampleCount: m_velSampleCount, neighborDist: m_neighbourDist, maxNeighbors: 10, radius: m_radius, prefSpeed: m_maxSpeed, maxSpeed: m_maxSpeed, maxAccel: m_maxAccel);

        if (type == RVOExampleType.Circle)
        {
            float circleRad = Mathf.Sqrt(m_agentCount * m_radius * m_radius * 4 / Mathf.PI) * exampleScale * 0.05f;

            for (int i = 0; i < m_agentCount; i++)
            {
                Vector3 pos = new Vector3(Mathf.Cos(i * Mathf.PI * 2.0f / m_agentCount), 0, Mathf.Sin(i * Mathf.PI * 2.0f / m_agentCount)) * circleRad * (1 + Random.value * 0.01f);
                sim.AddAgent(new Vector2(pos.x, pos.z));
                goals.Add(-pos);
                colors.Add(ColorUtility.HSVToRGB(i * 360.0f / m_agentCount, 0.8f, 0.6f));
            }
        }
        else if (type == RVOExampleType.Line)
        {
            for (int i = 0; i < m_agentCount; i++)
            {
                Vector3 pos = new Vector3((i % 2 == 0 ? 1 : -1) * exampleScale, 0, (i / 2) * m_radius * 2.5f);
                sim.AddAgent(new Vector2(pos.x, pos.z));
                goals.Add(new Vector3(-pos.x, pos.y, pos.z));
                colors.Add(i % 2 == 0 ? Color.red : Color.blue);
            }
        }
        else if (type == RVOExampleType.Point)
        {
            for (int i = 0; i < m_agentCount; i++)
            {
                Vector3 pos = new Vector3(Mathf.Cos(i * Mathf.PI * 2.0f / m_agentCount), 0, Mathf.Sin(i * Mathf.PI * 2.0f / m_agentCount)) * m_radius;
                sim.AddAgent(new Vector2(pos.x, pos.z));
                //sim.AddAgent(new Vector2(0, 0));
                goals.Add(new Vector3(0, pos.y, 0));
                colors.Add(ColorUtility.HSVToRGB(i * 360.0f / m_agentCount, 0.8f, 0.6f));
            }
        }
        else if (type == RVOExampleType.RandomStreams)
        {
            float circleRad = Mathf.Sqrt(m_agentCount * m_radius * m_radius * 4 / Mathf.PI) * exampleScale * 0.05f;

            for (int i = 0; i < m_agentCount; i++)
            {
                float   angle       = Random.value * Mathf.PI * 2.0f;
                float   targetAngle = Random.value * Mathf.PI * 2.0f;
                Vector3 pos         = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * uniformDistance(circleRad);
                sim.AddAgent(new Vector2(pos.x, pos.z));
                goals.Add(new Vector3(Mathf.Cos(targetAngle), 0, Mathf.Sin(targetAngle)) * uniformDistance(circleRad));
                colors.Add(ColorUtility.HSVToRGB(targetAngle * Mathf.Rad2Deg, 0.8f, 0.6f));
            }
        }
        else if (type == RVOExampleType.Crossing)
        {
            float distanceBetweenGroups = exampleScale * m_radius * 0.5f;
            int   directions            = (int)Mathf.Sqrt(m_agentCount / 25f);
            directions = Mathf.Max(directions, 2);

            const int AgentsPerDistance = 10;
            for (int i = 0; i < m_agentCount; i++)
            {
                float   angle = ((i % directions) / (float)directions) * Mathf.PI * 2.0f;
                var     dist  = distanceBetweenGroups * ((i / (directions * AgentsPerDistance) + 1) + 0.3f * Random.value);
                Vector3 pos   = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * dist;
                sim.AddAgent(new Vector2(pos.x, pos.z));
                goals.Add(-pos.normalized * distanceBetweenGroups * 3);
                colors.Add(ColorUtility.HSVToRGB(angle * Mathf.Rad2Deg, 0.8f, 0.6f));
            }
        }

        verts      = new Vector3[4 * m_agentCount];
        uv         = new Vector2[verts.Length];
        tris       = new int[m_agentCount * 2 * 3];
        meshColors = new Color[verts.Length];
    }