コード例 #1
0
ファイル: Intersection.cs プロジェクト: kuzznicki/roadcreator
    private void GenerateMesh()
    {
        List <Vector3> vertices           = new List <Vector3>();
        List <int>     triangles          = new List <int>();
        List <Vector2> uvs                = new List <Vector2>();
        List <int>     firstVertexIndexes = new List <int>();

        List <Vector3>     mainRoadsVertices  = new List <Vector3>();
        List <List <int> > mainRoadsTriangles = new List <List <int> >();
        List <Vector2>     mainRoadsUvs       = new List <Vector2>();
        List <float>       lengths            = new List <float>();

        CreateMainMesh(ref vertices, ref triangles, ref uvs, ref firstVertexIndexes);

        float[] startWidths = new float[firstVertexIndexes.Count];
        float[] endWidths   = new float[firstVertexIndexes.Count];
        float[] heights     = new float[firstVertexIndexes.Count];

        CreateExtraMeshes(firstVertexIndexes, vertices, ref startWidths, ref endWidths, ref heights);
        CreateMainRoadsMeshes(ref mainRoadsVertices, ref mainRoadsTriangles, ref mainRoadsUvs, ref vertices, ref uvs, ref lengths);
        SetupMesh(vertices, triangles, uvs, mainRoadsTriangles, lengths);

        for (int i = transform.childCount - 1; i >= 0; i--)
        {
            if (transform.GetChild(i).name == "Bridge")
            {
                DestroyImmediate(transform.GetChild(i).gameObject);
                break;
            }
        }

        if (generateBridge == true)
        {
            BridgeGeneration.GenerateSimpleBridgeIntersection(GetComponent <MeshFilter>().sharedMesh.vertices, this, bridgeSettings.bridgeMaterials, startWidths, endWidths, firstVertexIndexes.ToArray());
        }

        CreateCurvePoints();
    }
コード例 #2
0
    private void GenerateNormalMesh()
    {
        List <Vector3> vertices           = new List <Vector3>();
        List <int>     triangles          = new List <int>();
        List <Vector2> uvs                = new List <Vector2>();
        List <int>     firstVertexIndexes = new List <int>();

        float[] totalLengths       = new float[connections.Count];
        float[] exactLengths       = new float[connections.Count];
        int     vertexIndex        = 0;
        Vector3 lastVertexPosition = Misc.MaxVector3;

        for (int i = 0; i < connections.Count; i++)
        {
            Vector3 firstPoint       = connections[i].leftPoint;
            Vector3 firstCenterPoint = connections[i].lastPoint;
            Vector3 nextPoint;
            Vector3 nextCenterPoint;
            totalLengths[i] = connections[i].length;
            firstVertexIndexes.Add(vertexIndex);

            if (i == connections.Count - 1)
            {
                nextPoint        = connections[0].rightPoint;
                nextCenterPoint  = connections[0].lastPoint;
                totalLengths[i] += connections[0].length;
            }
            else
            {
                nextPoint        = connections[i + 1].rightPoint;
                nextCenterPoint  = connections[i + 1].lastPoint;
                totalLengths[i] += connections[i + 1].length;
            }

            if (connections[i].curvePoint == null)
            {
                return;
            }

            float segments = totalLengths[i] * settings.FindProperty("resolution").floatValue * 5;
            segments = Mathf.Max(3, segments);
            float distancePerSegment = 1f / segments;

            for (float t = 0; t <= 1; t += 0.1f)
            {
                Vector3 pos = Misc.Lerp3(Vector3.zero, new Vector3(0.5f, 0.5f, 0.5f), Vector3.one, t);
            }

            for (float t = 0; t <= 1 + distancePerSegment; t += distancePerSegment)
            {
                float modifiedT = t;
                if (Mathf.Abs(0.5f - t) < distancePerSegment && t > 0.5f)
                {
                    modifiedT = 0.5f;
                }

                if (t > 1)
                {
                    modifiedT = 1;
                }

                vertices.Add(Misc.Lerp3CenterHeight(firstPoint, connections[i].curvePoint, nextPoint, modifiedT) + new Vector3(0, yOffset, 0) - transform.position);

                if (t > 0)
                {
                    exactLengths[i]   += Vector3.Distance(lastVertexPosition, vertices[vertices.Count - 1]);
                    lastVertexPosition = vertices[vertices.Count - 1];
                }
                else
                {
                    lastVertexPosition = vertices[vertices.Count - 1];
                }

                if (modifiedT < 0.5f)
                {
                    Vector3 point = Vector3.Lerp(firstCenterPoint, transform.position, modifiedT * 2) - transform.position;
                    point.y = Mathf.Lerp(firstPoint.y, nextPoint.y, modifiedT) - transform.position.y + yOffset;
                    vertices.Add(point);
                }
                else
                {
                    Vector3 point = Vector3.Lerp(transform.position, nextCenterPoint, 2 * (modifiedT - 0.5f)) - transform.position;
                    point.y = Mathf.Lerp(firstPoint.y, nextPoint.y, modifiedT) - transform.position.y + yOffset;
                    vertices.Add(point);
                }

                uvs.Add(new Vector2(0, modifiedT));

                if (stretchTexture == true)
                {
                    uvs.Add(new Vector2(1, modifiedT));
                }
                else
                {
                    uvs.Add(new Vector2(Vector3.Distance(vertices[vertices.Count - 1], vertices[vertices.Count - 2]), modifiedT));
                }

                if (t < 1)
                {
                    triangles = AddTriangles(triangles, vertexIndex);
                }

                vertexIndex += 2;
            }

            Mesh mesh = new Mesh();
            mesh.vertices  = vertices.ToArray();
            mesh.triangles = triangles.ToArray();
            mesh.uv        = uvs.ToArray();
            mesh.RecalculateNormals();

            GetComponent <MeshFilter>().sharedMesh       = mesh;
            GetComponent <MeshCollider>().sharedMesh     = mesh;
            GetComponent <MeshCollider>().sharedMaterial = physicMaterial;

            if (overlayMaterial == null)
            {
                GetComponent <MeshRenderer>().sharedMaterials = new Material[] { baseMaterial };
            }
            else
            {
                GetComponent <MeshRenderer>().sharedMaterials = new Material[] { baseMaterial, overlayMaterial };
            }
        }

        float[] startWidths = new float[firstVertexIndexes.Count];
        float[] endWidths   = new float[firstVertexIndexes.Count];
        float[] heights     = new float[firstVertexIndexes.Count];

        GenerateIntersectionExtraMeshes(firstVertexIndexes, vertices, exactLengths, totalLengths, ref startWidths, ref endWidths, ref heights);

        for (int i = transform.childCount - 1; i >= 0; i--)
        {
            if (transform.GetChild(i).name == "Bridge")
            {
                DestroyImmediate(transform.GetChild(i).gameObject);
                break;
            }
        }

        if (generateBridge == true)
        {
            BridgeGeneration.GenerateSimpleBridgeIntersection(GetComponent <MeshFilter>().sharedMesh.vertices, this, bridgeSettings.bridgeMaterials, startWidths, endWidths, firstVertexIndexes.ToArray());
        }

        CreateCurvePoints();
    }
コード例 #3
0
ファイル: Intersection.cs プロジェクト: walklook/roadcreator
    public void CreateMesh(bool fromRoad = false)
    {
        if (globalSettings == null)
        {
            globalSettings = GameObject.FindObjectOfType <GlobalSettings>();
        }

        for (int i = 0; i < connections.Count; i++)
        {
            if (connections[i].road == null)
            {
                connections.RemoveAt(i);
            }
        }

        if (connections.Count < 2)
        {
            RoadCreator[] roads = GameObject.FindObjectsOfType <RoadCreator>();

            for (int i = 0; i < roads.Length; i++)
            {
                if (roads[i].startIntersection == this)
                {
                    roads[i].startIntersection = null;
                    break;
                }
                else if (roads[i].endIntersection == this)
                {
                    roads[i].endIntersection = this;
                    break;
                }
            }

            GameObject.DestroyImmediate(gameObject);
        }
        else
        {
            if (baseMaterial == null)
            {
                baseMaterial = Resources.Load("Materials/Low Poly/Intersections/Asphalt Intersection") as Material;
            }

            if (bridgeMaterials == null || bridgeMaterials.Length == 0 || bridgeMaterials[0] == null)
            {
                bridgeMaterials = new Material[] { Resources.Load("Materials/Low Poly/Concrete") as Material };
            }

            if (pillarPrefab == null || pillarPrefab.GetComponent <MeshFilter>() == null)
            {
                pillarPrefab = Resources.Load("Prefabs/Low Poly/Bridges/Cylinder Bridge Pillar") as GameObject;
            }

            List <Vector3> vertices    = new List <Vector3>();
            List <int>     triangles   = new List <int>();
            List <Vector2> uvs         = new List <Vector2>();
            int            vertexIndex = 0;

            for (int i = 0; i < connections.Count; i++)
            {
                Vector3 firstPoint       = connections[i].leftPoint.ToNormalVector3();
                Vector3 firstCenterPoint = connections[i].lastPoint.ToNormalVector3();
                Vector3 nextPoint;
                Vector3 nextCenterPoint;
                float   totalLength = connections[i].length;

                if (i == connections.Count - 1)
                {
                    nextPoint       = connections[0].rightPoint.ToNormalVector3();
                    nextCenterPoint = connections[0].lastPoint.ToNormalVector3();
                    totalLength    += connections[0].length;
                }
                else
                {
                    nextPoint       = connections[i + 1].rightPoint.ToNormalVector3();
                    nextCenterPoint = connections[i + 1].lastPoint.ToNormalVector3();
                    totalLength    += connections[i + 1].length;
                }

                if (connections[i].curvePoint == null)
                {
                    return;
                }

                float segments = totalLength * globalSettings.resolution * 5;
                segments = Mathf.Max(3, segments);
                float distancePerSegment = 1f / segments;

                for (float t = 0; t <= 1 + distancePerSegment; t += distancePerSegment)
                {
                    float modifiedT = t;
                    if (Mathf.Abs(0.5f - t) < distancePerSegment)
                    {
                        modifiedT = 0.5f;
                    }

                    if (modifiedT > 1)
                    {
                        modifiedT = 1;
                    }

                    vertices.Add(Misc.Lerp3(firstPoint, connections[i].curvePoint.ToNormalVector3(), nextPoint, modifiedT) + new Vector3(0, yOffset, 0) - transform.position);

                    if (modifiedT < 0.5f)
                    {
                        Vector3 point = Vector3.Lerp(firstCenterPoint, transform.position, modifiedT * 2) + new Vector3(0, yOffset, 0) - transform.position;
                        point.y = Mathf.Lerp(firstPoint.y, nextPoint.y, modifiedT) - transform.position.y + yOffset;
                        vertices.Add(point);
                    }
                    else
                    {
                        Vector3 point = Vector3.Lerp(transform.position, nextCenterPoint, 2 * (modifiedT - 0.5f)) + new Vector3(0, yOffset, 0) - transform.position;
                        point.y = Mathf.Lerp(firstPoint.y, nextPoint.y, modifiedT) - transform.position.y + yOffset;
                        vertices.Add(point);
                    }

                    uvs.Add(new Vector2(0, modifiedT));

                    if (stretchTexture == true)
                    {
                        uvs.Add(new Vector2(1, modifiedT));
                    }
                    else
                    {
                        uvs.Add(new Vector2(Vector3.Distance(vertices[vertices.Count - 1], vertices[vertices.Count - 2]), modifiedT));
                    }

                    if (t < 1)
                    {
                        triangles = AddTriangles(triangles, vertexIndex);
                    }

                    vertexIndex += 2;
                }

                Mesh mesh = new Mesh();
                mesh.vertices  = vertices.ToArray();
                mesh.triangles = triangles.ToArray();
                mesh.uv        = uvs.ToArray();

                GetComponent <MeshFilter>().sharedMesh       = mesh;
                GetComponent <MeshCollider>().sharedMesh     = mesh;
                GetComponent <MeshCollider>().sharedMaterial = physicMaterial;

                if (overlayMaterial == null)
                {
                    GetComponent <MeshRenderer>().sharedMaterials = new Material[] { baseMaterial };
                }
                else
                {
                    GetComponent <MeshRenderer>().sharedMaterials = new Material[] { baseMaterial, overlayMaterial };
                }
            }

            for (int i = transform.childCount - 1; i >= 0; i--)
            {
                if (transform.GetChild(i).name == "Bridge")
                {
                    DestroyImmediate(transform.GetChild(i).gameObject);
                    break;
                }
            }

            if (bridgeGenerator == RoadSegment.BridgeGenerator.simple)
            {
                BridgeGeneration.GenerateSimpleBridgeIntersection(GetComponent <MeshFilter>().sharedMesh.vertices, this, bridgeMaterials);
            }

            CreateCurvePoints();
        }

        if (fromRoad == false)
        {
            for (int i = 0; i < connections.Count; i++)
            {
                connections[i].road.transform.parent.parent.parent.parent.GetComponent <RoadCreator>().CreateMesh(true);
            }
        }
    }