コード例 #1
0
    public IEnumerator Start()
    {
        Instance = this;

        num = Random.Range(1, num);
        while (cubes.Count < num)
        {
            var bc = BonedCube.Make();
            bc.transform.SetParent(this.transform);             // The cubes are children of this transform so they can be placed on a ground plane in AR
            bc.a.transform.position = this.transform.position + new Vector3(LeModular.GetClosest(Random.Range(minXZ, maxXZ)), LeModular.GetClosest(Random.Range((maxHeight * -1), maxHeight)), LeModular.GetClosest(Random.Range(minXZ, maxXZ)));
            bc.Set(Random.Range(minXZ, maxXZ), Random.Range(minXZ, maxXZ), Random.Range(minXZ, maxXZ));
            cubes.Add(bc);
        }

        int count = 0;

        foreach (var bc in cubes)
        {
            if (bc == null)
            {
                continue;
            }

            // Add ModulorAgent script that will handle it's building behaviour
            var modulorAgent = bc.gameObject.AddComponent <ModulorAgent>();
            modulorAgent.bc = bc;
            modulorAgent.id = alphabet[count];
            agentDict.Add(modulorAgent, new List <Vector3>());
            // transform the world position of this transform to the local space of bc
            if (bc.a.position.y < this.transform.position.y)
            {
                // if i'm 10 meters tall and located at -1 then the visible section of me is going to be 4
                // if 10 meters tall and locate at +1 then the visible section of me is going to be 6
                float val = Random.Range(0f, 1f);
                Color col = val > 0.5f ? Color.white : Color.black;
                col.a = 0.5f;
                bc.GetComponentInChildren <Renderer>().material.color = col;
                bc.a.position          = new Vector3(bc.a.transform.position.x, this.transform.position.y, bc.a.transform.position.z);
                bc.b.position          = new Vector3(bc.b.transform.position.x, this.transform.position.y, bc.b.transform.position.z);
                modulorAgent.offGround = false;
            }
            else
            {
                float val = Random.Range(0f, 1f);
                Color col = val > 0.5f ? Color.blue : Color.red;
                col.a = 0.5f;
                bc.GetComponentInChildren <Renderer>().material.color = col;
                modulorAgent.offGround = true;
            }
            count++;
        }
        yield return(new WaitUntil(() => ready == cubes.Count));

        yield return(new WaitForSeconds(0.5f));

        Dictionary <float, Level> tmpLevels = new Dictionary <float, Level>();
        List <float> heights = new List <float>();

        foreach (Vector3 point in CubeGenerator.Instance.points)
        {
            if (!tmpLevels.ContainsKey(point.y))
            {
                tmpLevels.Add(point.y, new Level(point));
                tmpLevels[point.y].vertexPoints.Add(new Vertex(point));
            }
            else
            {
                tmpLevels[point.y].points.Add(point);
                tmpLevels[point.y].vertexPoints.Add(new Vertex(point));
            }
        }
        heights.AddRange(tmpLevels.Keys);
        heights.Sort();
        foreach (var f in heights)
        {
            Debug.Log(string.Format("Level {0}", f));
        }
        var tmpHeights = new List <float>();

        for (int i = 0; i < heights.Count; i++)
        {
            if (i < heights.Count - 1 && heights[i] != -1.1f)
            {
                var dif = heights[i + 1] - heights[i];
                if (dif > 0.6)
                {
                    if (tmpLevels.ContainsKey(heights[i]))
                    {
                        levels.Add(heights[i], tmpLevels[heights[i]]);
                    }
                }
                else
                {
                    if (tmpLevels.ContainsKey(heights[i]))
                    {
                        tmpLevels[heights[i]].vertexPoints.AddRange(tmpLevels[heights[i + 1]].vertexPoints);
                        levels.Add(heights[i], tmpLevels[heights[i]]);
                        heights[i + 1] = 1.1f;
                    }
                }
            }
        }
        indexLevels.AddRange(levels.Values);
        foreach (var entry in levels)
        {
            entry.Value.convexVertexes = JarvisMarchAlgorithm.GetConvexHull(entry.Value.vertexPoints);
            Debug.Log(string.Format("Merged Levels {0}", entry.Key));
        }
        foreach (var item in indexLevels)
        {
            if (item.convexVertexes == null)
            {
                continue;
            }

            var go = new GameObject();
            lrGos.Add(go);
            go.transform.SetParent(this.transform);
            LineRenderer lr = go.AddComponent <LineRenderer>();
            lr.material      = new Material(Shader.Find("Sprites/Default"));
            lr.positionCount = item.GetPointsFromVertexes().Count;
            lr.SetPositions(item.GetPointsFromVertexes().ToArray());
            lr.loop       = true;
            lr.startWidth = 0.1f;
            lr.endWidth   = 0.1f;
        }
        // this.transform.localScale = Vector3.one * scale;
        // Debug.Log(string.Format("Ready {0}", cubes.Count));
    }
コード例 #2
0
    // Use this for initialization
    IEnumerator Start()
    {
        this.gameObject.name = id;

        showPoints             = true;
        showEncapsulatedPoints = true;
        showEdges = true;

        // Set the size of the volume to the closest measurement contained in the red series
        float length = LeModular.GetClosest(bc.length * 100) / 100;
        float width  = LeModular.GetClosest(bc.width * 100) / 100;
        float height = LeModular.GetClosest(bc.height * 100) / 100;

        bc.Set(length, width, height);

        // Find all the bounding points
        skinnedMesh = GetComponentInChildren <SkinnedMeshRenderer>();
        skinnedMesh.updateWhenOffscreen = true;
        bounds         = skinnedMesh.bounds;
        center         = bounds.center;
        boundingPoints = new BoundingPoints(center, new Vector3(width, height, length));
        Debug.LogFormat("{0}{1}{2}", width, height, length);
        // Add box collider and rigidbody to generate collisions
        boxCollider           = bc.gameObject.AddComponent <BoxCollider>();
        boxCollider.enabled   = true;
        boxCollider.isTrigger = true;
        boxCollider.center    = center;
        boxCollider.size      = new Vector3(bc.width, bc.height, bc.length);
        rb             = bc.gameObject.AddComponent <Rigidbody>();
        rb.isKinematic = true;
        yield return(new WaitUntil(() => triggerStay));

        CubeGenerator.Instance.ready++;
        // Debug.Log(string.Format("Modulor Agent {0} Agent List Count{1}",id, modulorAgents.Count));
        // Iterate through our bounding points to see if any of them are within the bounds of another agent
        CubeGenerator.Instance.agentDict[this].AddRange(boundingPoints.all);

        foreach (Vector3 point in boundingPoints.all)
        {
            foreach (ModulorAgent agent in modulorAgents)
            {
                if (agent.bounds.Contains(point))
                {
                    if (encapsulatedPoints.ContainsKey(agent))
                    {
                        encapsulatedPoints[agent].Add(point);
                    }
                    else
                    {
                        List <Vector3> points = new List <Vector3>();
                        points.Add(point);
                        encapsulatedPoints.Add(agent, points);
                    }
                }
                else
                {
                    //
                }
            }
        }


        foreach (var entry in this.encapsulatedPoints)
        {
            foreach (var edge in boundingPoints.edges)
            {
                if (!entry.Value.Contains(edge.v1.position) & !entry.Value.Contains(edge.v2.position))
                {
                    continue;
                }
                var dir = edge.v2.position - edge.v1.position;
                dir.Normalize();
                Ray   ray      = new Ray(edge.v1.position, dir);
                float distance = 0;

                if (entry.Key.bounds.IntersectRay(ray, out distance))
                {
                    Vector3 point = ray.origin + ray.direction * distance;
                    intersectionPoints.Add(point);
                    intersectionEdges.Add(new Edge(ray.origin, point));
                }
            }
        }

        foreach (Vector3 vector in boundingPoints.all)
        {
            Vector3 worldPoint = this.transform.TransformPoint(vector);
            if (!CubeGenerator.Instance.points.Contains(worldPoint))
            {
                CubeGenerator.Instance.points.Add(worldPoint);
            }
        }

        foreach (Vector3 vector in intersectionPoints)
        {
            Vector3 worldPoint = this.transform.TransformPoint(vector);
            if (!CubeGenerator.Instance.points.Contains(worldPoint))
            {
                CubeGenerator.Instance.points.Add(worldPoint);
            }
        }

        // bc.GetComponentInChildren<SkinnedMeshRenderer>().enabled = false;

        // TODO: refactor what's below
        var refactor = false;

        if (refactor)
        {
            // Instantiate piloti if the volume is off the ground
            if (offGround)
            {
                var piloti = Resources.Load <GameObject>("piloti");
                var pos    = center;
                pos.y -= bc.height / 2;
                piloti = Instantiate(piloti, pos, Quaternion.identity, this.transform);
                var pilotiScript = piloti.GetComponent <Pilotis>();
                // get the shortest side
                pilotiScript.length = bc.width;
                pilotiScript.width  = bc.length;
                var a = pilotiScript.length > pilotiScript.width ? pilotiScript.width : pilotiScript.length;            // get the shortet side
                a *= 0.1f;
                a  = Mathf.Clamp(a, 0.1f, 0.75f);
                pilotiScript.pilotiWidth = a;
                pilotiScript.width      -= a;
                pilotiScript.length     -= a;
                var ground = bc.a.position;
                ground.y = 0;
                height   = Vector3.Distance(bc.a.position, ground);
            }

            List <float> floors  = LeModular.Divisions(bc.height);
            var          floorBC = BonedCube.Make();
            floorBC.transform.SetParent(this.transform);
            floorBC.a.transform.position = this.bc.a.transform.position;
            floorBC.Set(bc.length, bc.width, 0.1f);
            var _floors = BuildFloors(floors);
            if (_floors != null)
            {
                foreach (var floor in _floors)
                {
                    floorBC = BonedCube.Make();
                    floorBC.transform.SetParent(this.transform);
                    floorBC.a.transform.position = this.bc.a.transform.position + new Vector3(0, floor, 0);
                    floorBC.Set(bc.length, bc.width, 0.1f);
                }
            }
        }
    }