Exemple #1
0
        public void Calculate()
        {
            var uix = new UnitInterval(height*uvSize);
            var uiy = new UnitInterval(height*uvSize);
            var hv = transform.up*height;
            aBottom = new List<Vector3> { transform.GetChild(0).localPosition };
            bBottom = new List<Vector3>();
            aTop = new List<Vector3> { aBottom[0] + hv + transform.up * transform.GetChild(0).GetComponent<WallPoint>().heightOffset };
            bTop = new List<Vector3>();

            //Generate general points
            int currentChild = 0;
            while (currentChild < transform.childCount - 1)
            {
                var awp = transform.GetChild(currentChild).GetComponent<WallPoint>();
                var a = awp.transform.localPosition;
                var atop = a + (hv + transform.up * awp.heightOffset);
                var bwp = transform.GetChild(currentChild + 1).GetComponent<WallPoint>();
                var b = bwp.transform.localPosition;
                var btop = b + (hv + transform.up * bwp.heightOffset);
                var isBezier = bwp.isCurveCenter;
                if (isBezier)
                {
                    var cwp = transform.GetChild(currentChild + 2).GetComponent<WallPoint>();
                    var c = cwp.transform.localPosition;
                    var ctop = c + (hv + transform.up * cwp.heightOffset);

                    var bezierPoints = new Vector3[100];
                    for (int i = 0; i < 100; i++)
                        bezierPoints[i] = GetBezierPoint(0.01f*i, a, b, c);
                    var bezierLength = 0f;
                    for (int i = 0; i < 99; i++)
                        bezierLength += Vector3.Distance(bezierPoints[i], bezierPoints[i + 1]);

                    var steps = (int) (bezierLength/smoothDistance);
                    for (int i = 1; i < steps + 1; i++)
                    {
                        aBottom.Add(GetBezierPoint(1f/steps*i, a, b, c));
                        aTop.Add(GetBezierPoint(1f / steps * i, atop, btop, ctop));
                    }

                }
                else
                {
                    aBottom.Add(b);
                    aTop.Add(btop);
                }

                if (isBezier)
                    currentChild++;
                currentChild++;
            }

            var last = aBottom.Count - 1;
            //Generate other points
            bBottom.Add(aBottom[0] + GetBrotherPoint(aBottom[1], aBottom[0]));
            for (int i = 1; i < aBottom.Count-1; i++)
            {
                bBottom.Add(aBottom[i] + GetBrotherPoint(aBottom[i - 1], aBottom[i], aBottom[i + 1]));
            }
            bBottom.Add(aBottom[last] + GetBrotherPoint(aBottom[last], aBottom[last - 1]));

            bTop.Add(aTop[0] + GetBrotherPoint(aTop[1], aTop[0]));
            for (int i = 1; i < aTop.Count - 1; i++)
            {
                bTop.Add(aTop[i] + GetBrotherPoint(aTop[i - 1], aTop[i], aTop[i + 1]));
            }
            bTop.Add(aTop[last] + GetBrotherPoint(aTop[last], aTop[last - 1]));

            //Created meshes - порядок диктуется порядком в развертке - важная деталь
            //в начале
            var startMesh = Quad(bTop[0], bBottom[0], aBottom[0], aTop[0]);
            {
                var uvs = new List<Vector2>();
                var dist = Vector3.Distance(aBottom[0], aTop[0]);
                uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                uvs.Add(new Vector2(uix.Get(), 0));
                uix.Plus(Vector3.Distance(aBottom[0], bBottom[0]));
                uvs.Add(new Vector2(uix.Get(), 0));
                uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                startMesh.uv = uvs.ToArray();
            }
            //слева
            var aMesh = BendPlane(aBottom.ToArray(), aTop.ToArray(), false);
            {
                var uvs = new List<Vector2>();
                for (int i = 0; i < aBottom.Count; i++)
                {
                    var dist = Vector3.Distance(aBottom[i], aTop[i]);
                    if (i > 0)
                        uix.Plus(Vector3.Distance(aBottom[i - 1], aBottom[i]));
                    uvs.Add(new Vector2(uix.Get(), 0));
                    uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                }
                aMesh.uv = uvs.ToArray();
            }
            //в конце
            var endMesh = Quad(aTop[last], aBottom[last], bBottom[last], bTop[last]);
            {
                var uvs = new List<Vector2>();
                var dist = Vector3.Distance(aBottom[last], aTop[last]);
                uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                uvs.Add(new Vector2(uix.Get(), 0));
                uix.Plus(Vector3.Distance(aBottom[last], bBottom[last]));
                uvs.Add(new Vector2(uix.Get(), 0));
                uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                endMesh.uv = uvs.ToArray();
            }
            //справа
            var bMesh = BendPlane(bBottom.ToArray(), bTop.ToArray(), true);
            {
                var uvs = new List<Vector2>();
                for (int i = bBottom.Count-1; i >= 0; i--)
                {
                    var dist = Vector3.Distance(bBottom[i], bTop[i]);
                    if (i < bBottom.Count - 1)
                        uix.Minus(Vector3.Distance(bBottom[i], bBottom[i + 1]));
                    uvs.Insert(0, new Vector2(uix.Get(), uiy.Get(dist)));
                    uvs.Insert(0, new Vector2(uix.Get(), 0));

                }
                bMesh.uv = uvs.ToArray();
            }

            //сверху
            var topMesh = BendPlane(aTop.ToArray(), bTop.ToArray(), false);
            {
                var uvs = new List<Vector2>();
                for (int i = 0; i < aTop.Count; i++)
                {
                    var dist = Vector3.Distance(aTop[i], bTop[i]);
                    if (i > 0)
                        uix.Plus(Vector3.Distance(aTop[i - 1], aTop[i]));
                    uvs.Add(new Vector2(uix.Get(), 0));
                    uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                }
                topMesh.uv = uvs.ToArray();
            }

            var combineMesh = CombineMerge(true, aMesh, bMesh, startMesh, endMesh);
            combineMesh = CombineSub(true, combineMesh, topMesh);
            combineMesh.RecalculateNormals();
            var f = GetComponent<MeshFilter>();
            if (f.sharedMesh)
                DestroyImmediate(f.sharedMesh);
            f.sharedMesh = combineMesh;
        }
Exemple #2
0
        public void Calculate()
        {
            var uix = new UnitInterval(height * uvSize);
            var uiy = new UnitInterval(height * uvSize);
            var hv  = transform.up * height;

            aBottom = new List <Vector3> {
                transform.GetChild(0).localPosition
            };
            bBottom = new List <Vector3>();
            aTop    = new List <Vector3> {
                aBottom[0] + hv + transform.up * transform.GetChild(0).GetComponent <WallPoint>().heightOffset
            };
            bTop = new List <Vector3>();

            //Generate general points
            int currentChild = 0;

            while (currentChild < transform.childCount - 1)
            {
                var awp      = transform.GetChild(currentChild).GetComponent <WallPoint>();
                var a        = awp.transform.localPosition;
                var atop     = a + (hv + transform.up * awp.heightOffset);
                var bwp      = transform.GetChild(currentChild + 1).GetComponent <WallPoint>();
                var b        = bwp.transform.localPosition;
                var btop     = b + (hv + transform.up * bwp.heightOffset);
                var isBezier = bwp.isCurveCenter;
                if (isBezier)
                {
                    var cwp  = transform.GetChild(currentChild + 2).GetComponent <WallPoint>();
                    var c    = cwp.transform.localPosition;
                    var ctop = c + (hv + transform.up * cwp.heightOffset);

                    var bezierPoints = new Vector3[100];
                    for (int i = 0; i < 100; i++)
                    {
                        bezierPoints[i] = GetBezierPoint(0.01f * i, a, b, c);
                    }
                    var bezierLength = 0f;
                    for (int i = 0; i < 99; i++)
                    {
                        bezierLength += Vector3.Distance(bezierPoints[i], bezierPoints[i + 1]);
                    }



                    var steps = (int)(bezierLength / smoothDistance);
                    for (int i = 1; i < steps + 1; i++)
                    {
                        aBottom.Add(GetBezierPoint(1f / steps * i, a, b, c));
                        aTop.Add(GetBezierPoint(1f / steps * i, atop, btop, ctop));
                    }
                }
                else
                {
                    aBottom.Add(b);
                    aTop.Add(btop);
                }

                if (isBezier)
                {
                    currentChild++;
                }
                currentChild++;
            }

            var last = aBottom.Count - 1;

            //Generate other points
            bBottom.Add(aBottom[0] + GetBrotherPoint(aBottom[1], aBottom[0]));
            for (int i = 1; i < aBottom.Count - 1; i++)
            {
                bBottom.Add(aBottom[i] + GetBrotherPoint(aBottom[i - 1], aBottom[i], aBottom[i + 1]));
            }
            bBottom.Add(aBottom[last] + GetBrotherPoint(aBottom[last], aBottom[last - 1]));

            bTop.Add(aTop[0] + GetBrotherPoint(aTop[1], aTop[0]));
            for (int i = 1; i < aTop.Count - 1; i++)
            {
                bTop.Add(aTop[i] + GetBrotherPoint(aTop[i - 1], aTop[i], aTop[i + 1]));
            }
            bTop.Add(aTop[last] + GetBrotherPoint(aTop[last], aTop[last - 1]));


            //Created meshes - порядок диктуется порядком в развертке - важная деталь
            //в начале
            var startMesh = Quad(bTop[0], bBottom[0], aBottom[0], aTop[0]);
            {
                var uvs  = new List <Vector2>();
                var dist = Vector3.Distance(aBottom[0], aTop[0]);
                uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                uvs.Add(new Vector2(uix.Get(), 0));
                uix.Plus(Vector3.Distance(aBottom[0], bBottom[0]));
                uvs.Add(new Vector2(uix.Get(), 0));
                uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                startMesh.uv = uvs.ToArray();
            }
            //слева
            var aMesh = BendPlane(aBottom.ToArray(), aTop.ToArray(), false);
            {
                var uvs = new List <Vector2>();
                for (int i = 0; i < aBottom.Count; i++)
                {
                    var dist = Vector3.Distance(aBottom[i], aTop[i]);
                    if (i > 0)
                    {
                        uix.Plus(Vector3.Distance(aBottom[i - 1], aBottom[i]));
                    }
                    uvs.Add(new Vector2(uix.Get(), 0));
                    uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                }
                aMesh.uv = uvs.ToArray();
            }
            //в конце
            var endMesh = Quad(aTop[last], aBottom[last], bBottom[last], bTop[last]);
            {
                var uvs  = new List <Vector2>();
                var dist = Vector3.Distance(aBottom[last], aTop[last]);
                uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                uvs.Add(new Vector2(uix.Get(), 0));
                uix.Plus(Vector3.Distance(aBottom[last], bBottom[last]));
                uvs.Add(new Vector2(uix.Get(), 0));
                uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                endMesh.uv = uvs.ToArray();
            }
            //справа
            var bMesh = BendPlane(bBottom.ToArray(), bTop.ToArray(), true);
            {
                var uvs = new List <Vector2>();
                for (int i = bBottom.Count - 1; i >= 0; i--)
                {
                    var dist = Vector3.Distance(bBottom[i], bTop[i]);
                    if (i < bBottom.Count - 1)
                    {
                        uix.Minus(Vector3.Distance(bBottom[i], bBottom[i + 1]));
                    }
                    uvs.Insert(0, new Vector2(uix.Get(), uiy.Get(dist)));
                    uvs.Insert(0, new Vector2(uix.Get(), 0));
                }
                bMesh.uv = uvs.ToArray();
            }



            //сверху
            var topMesh = BendPlane(aTop.ToArray(), bTop.ToArray(), false);
            {
                var uvs = new List <Vector2>();
                for (int i = 0; i < aTop.Count; i++)
                {
                    var dist = Vector3.Distance(aTop[i], bTop[i]);
                    if (i > 0)
                    {
                        uix.Plus(Vector3.Distance(aTop[i - 1], aTop[i]));
                    }
                    uvs.Add(new Vector2(uix.Get(), 0));
                    uvs.Add(new Vector2(uix.Get(), uiy.Get(dist)));
                }
                topMesh.uv = uvs.ToArray();
            }

            var combineMesh = CombineMerge(true, aMesh, bMesh, startMesh, endMesh);

            combineMesh = CombineSub(true, combineMesh, topMesh);
            combineMesh.RecalculateNormals();
            var f = GetComponent <MeshFilter>();

            if (f.sharedMesh)
            {
                DestroyImmediate(f.sharedMesh);
            }
            f.sharedMesh = combineMesh;
        }