Ejemplo n.º 1
0
        public void NewTestScriptSimplePasses()
        {
            Curve c  = Arc.TryInit(roadPoints[0], roadPoints[1], Mathf.PI / 2);
            Curve cr = c.reversed();

            Assert.True(Algebra.isclose(c.At(0f), cr.At(1f)));

            float a1 = c.Angle_2d(1f);
            float a2 = cr.Angle_2d(0f);

            //float angleDiff = (a2 - a1) / Mathf.PI;
            Debug.Log(a1);
            Debug.Log(a2);
            Assert.True(Algebra.isclose(a2, Mathf.PI / 2));
        }
Ejemplo n.º 2
0
    /*create meash for linear rendered 3D object*/
    public void CreateMesh(Curve curve, float offset, Material linearMaterial, Material crossMaterial, Polygon cross)
    {
        List <Vector2> fragments = cross.toFragments();

        int segmentCount = Mathf.CeilToInt(curve.length * curve.maximumCurvature / maxAngleDiff);
        int base_vcount  = (segmentCount + 1) * fragments.Count;
        int base_tcount  = segmentCount * 2 * 3 * fragments.Count;

        int[] crossTriangles = cross.createMeshTriangle();
        int   cross_vcount   = fragments.Count;
        int   cross_tcount   = crossTriangles.Length;

        Vector2[] crossUVs = cross.createUV();

        Vector3[] all_vertices     = new Vector3[base_vcount + 2 * cross_vcount];
        int[]     linear_triangles = new int[base_tcount];
        Vector2[] linearUVs        = new Vector2[base_vcount];

        for (int i = 0; i != segmentCount + 1; ++i)
        {
            Vector3        roadPoint      = curve.At(1.0f / segmentCount * i);
            float          direction      = curve.Angle_2d(1.0f / segmentCount * i) - Mathf.PI / 2;
            List <Vector3> localFragments = fragments.ConvertAll((input) => roadPoint +
                                                                 Algebra.toVector3(Algebra.twodRotate(Vector2.right * (offset + input.x), direction)) +
                                                                 Vector3.up * input.y);
            /*stretch Z*/
            List <float> cross_y_offset = cross.getVResizeOffset(roadPoint.y);
            for (int j = 0; j != localFragments.Count; ++j)
            {
                localFragments[j] += Vector3.up * cross_y_offset[j];
            }

            float cross_diameter   = fragments.Sum((input) => input.magnitude);
            float partial_diameter = 0f;
            for (int j = i * cross_vcount, local_j = 0; j != i * cross_vcount + cross_vcount; ++j, ++local_j)
            {
                all_vertices[j]   = localFragments[local_j];
                linearUVs[j]      = new Vector2(partial_diameter / cross_diameter, i * 1.0f / segmentCount * curve.length / cross_diameter);
                partial_diameter += fragments[local_j].magnitude;
            }
        }

        for (int i = 0; i != cross_vcount; ++i)
        {
            all_vertices[base_vcount + i] = all_vertices[i];
            all_vertices[base_vcount + cross_vcount + i] = all_vertices[base_vcount - cross_vcount + i];
        }

        for (int i = 0, triangle = 0; i != segmentCount; ++i)
        {
            for (int j = 0; j != cross_vcount; ++j, triangle += 6)
            {
                linear_triangles[triangle]     = i * cross_vcount + j;
                linear_triangles[triangle + 1] = i * cross_vcount + (j + 1) % cross_vcount;
                linear_triangles[triangle + 2] = (i + 1) * cross_vcount + j;

                linear_triangles[triangle + 3] = i * cross_vcount + (j + 1) % cross_vcount;
                linear_triangles[triangle + 4] = (i + 1) * cross_vcount + (j + 1) % cross_vcount;
                linear_triangles[triangle + 5] = (i + 1) * cross_vcount + j;
            }
        }

        int[] cross_triangles = new int[2 * cross_tcount];
        /*Add tris at start*/
        for (int j = 0; j != cross_tcount; ++j)
        {
            cross_triangles[j] = crossTriangles[j] + base_vcount;
        }

        /*Add tris at end*/
        for (int j = 0; j != cross_tcount; ++j)
        {
            if (j % 3 == 1)
            {
                cross_triangles[cross_tcount + j] = crossTriangles[j + 1];
            }
            else
            {
                if (j % 3 == 2)
                {
                    cross_triangles[cross_tcount + j] = crossTriangles[j - 1];
                }
                else
                {
                    cross_triangles[cross_tcount + j] = crossTriangles[j];
                }
            }
            cross_triangles[cross_tcount + j] += (base_vcount + cross_vcount);
        }

        Vector2[] modifiedCrossUVs = new Vector2[base_vcount + 2 * cross_vcount];
        for (int i = 0; i != base_vcount; ++i)
        {
            modifiedCrossUVs[i] = linearUVs[i];
        }
        for (int i = 0; i != cross_vcount; ++i)
        {
            modifiedCrossUVs[i + base_vcount] = modifiedCrossUVs[i + base_vcount + cross_vcount] = crossUVs[i];
        }


        GetComponent <MeshRenderer>().sharedMaterials = new Material[2] {
            crossMaterial, linearMaterial
        };

        MeshFilter meshFilter = GetComponent <MeshFilter>();

        if (meshFilter.sharedMesh == null)
        {
            meshFilter.sharedMesh = new Mesh();
        }

        meshFilter.sharedMesh.subMeshCount = 2;
        meshFilter.sharedMesh.SetVertices(all_vertices.ToList());
        meshFilter.sharedMesh.SetTriangles(cross_triangles, 0);
        meshFilter.sharedMesh.SetTriangles(linear_triangles, 1);
        meshFilter.sharedMesh.SetUVs(0, modifiedCrossUVs.ToList());
    }