Exemplo n.º 1
0
Arquivo: Road.cs Projeto: yazici/Cigen
    public void BuildMesh()
    {
        float   roadWidth  = city.settings.roadDimensions.x;
        float   roadHeight = city.settings.roadDimensions.y;
        Texture texture    = city.settings.roadTexture;

        Vector3 from      = city.transform.TransformPoint(parentNode.Position);
        Vector3 to        = city.transform.TransformPoint(childNode.Position);
        Vector3 direction = (from - to).normalized;
        Vector3 offset    = direction * roadWidth / 2f;

        from -= offset; //so the endings don't go all the way to the point, they line up neatly with intersections
        to   += offset;
        transform.position = from;
        float length = Vector3.Distance(from, to);

        Vector3[] vertices = new Vector3[4];
        vertices[0] = -transform.right * roadWidth / 2f;
        vertices[1] = (transform.forward * length) - (transform.right * roadWidth / 2f);
        vertices[2] = (transform.forward * length) + (transform.right * roadWidth / 2f);
        vertices[3] = transform.right * roadWidth / 2f;

        Vector2 mainTexScale;

        GetComponent <MeshFilter>().mesh = HullMesher2D.BuildPolygon(vertices, out mainTexScale);
        Material mat = GetComponent <MeshRenderer>().material;

        if (texture != null)
        {
            mat.mainTexture      = texture;
            mat.mainTextureScale = mainTexScale;
        }
        else
        {
            mat.color = Color.gray;
        }

        Mesh m = GetComponent <MeshFilter>().mesh;

        Quaternion rotation = Quaternion.identity;
        Vector3    z        = Vector3.zero;
        Matrix4x4  op0      = Matrix4x4.TRS(z, rotation, Vector3.one);
        Matrix4x4  op1      = Matrix4x4.TRS(transform.up * roadHeight, rotation, Vector3.one);

        Matrix4x4[] ops = new Matrix4x4[] { op0, op1, };

        MeshExtrusion.ExtrudeMesh(m, GetComponent <MeshFilter>().mesh, ops, true);

        m.RecalculateNormals();
        m.RecalculateBounds();

        transform.position = from;
        transform.LookAt(to);

        {
            Func <Vector3, Transform, Vector3> a = (v, t) => {
                Vector3 b;
                //converts the vector from this local space into world space, then converts it again into the transforms local space
                b = t.InverseTransformPoint(transform.TransformPoint(v)); //store the vertices in world coordinates
                GizmosToDraw.Add(b);
                return(b);
            };

            //maybe add these vertices to the intersections vertex list?
            parentNode.AddVerts(a(vertices[0], parentNode.transform), a(vertices[3], parentNode.transform));
            childNode.AddVerts(a(vertices[1], childNode.transform), a(vertices[2], childNode.transform));
        }
    }