예제 #1
0
        public void UpdateBezier()
        {
            if (_overridden)
                return;

            var pointsHash = PointsHash();
            if (pointsHash != _pointsHash)
            {
                if (_reverseCPs)
                {
                    points.Reverse();
                    _reverseCPs = false;
                }

                _bezier = Bezier.ConstructSmoothSpline(points, _continuous);

                var meshFilter = GetComponent<MeshFilter>();

                if (_triangulate)
                {
                    if (!meshFilter)
                        meshFilter = gameObject.AddComponent<MeshFilter>();

                    if (meshFilter.sharedMesh)
                        DestroyImmediate(meshFilter.sharedMesh);

                    if (!GetComponent<Renderer>())
                        gameObject.AddComponent<MeshRenderer>();

                    meshFilter.sharedMesh = _bezier.Triangulate((uint)_triangulateSegments);
                }
                else if (meshFilter)
                {
                    if (meshFilter.sharedMesh)
                        DestroyImmediate(meshFilter.sharedMesh);

                    if (GetComponent<Renderer>())
                        DestroyImmediate(GetComponent<Renderer>());

                    DestroyImmediate(meshFilter);
                }

                _pointsHash = pointsHash;
            }
        }
 public void OverrideBezier(Bezier b)
 {
     _bezier     = b;
     _overridden = true;
 }
예제 #3
0
 public void OverrideBezier(Bezier b)
 {
     _bezier = b;
     _overridden = true;
 }
        protected override void GenerateMesh()
        {
            foreach (Transform child in transform)
            {
                DestroyImmediate(child.gameObject);
            }

            var rand = new MersenneTwister(randomSeed);

            var totalHeight          = 0.0f;
            var scaleDown            = 1.0f;
            var combineMeshInstances = new List <CombineInstance>();

            for (int i = 0; i < layerCount; ++i)
            {
                var baseRadius = Mathf.Lerp(baseRadiusMin, baseRadiusMax, rand.NextSinglePositive()) * scaleDown;
                var baseBezier = GenerateBaseBezier(baseRadius * scaleDown, rand);

                var previousTotalHeight = totalHeight;
                totalHeight += Mathf.Lerp(layerHeightMin, layerHeightMax, rand.NextSinglePositive()) * scaleDown;
                var heightBezier = Bezier.ConstructSmoothSpline(
                    new Vector3[] {
                    Vector3.up *previousTotalHeight,
                    Vector3.up *totalHeight
                }
                    );

                var heightSegs = (uint)heightSegments;
                var pathSegs   = (uint)(radiusSegments * scaleDown);

                if (heightSegs > 0 && pathSegs > 2)
                {
                    var combineMeshInstance = new CombineInstance();
                    combineMeshInstance.mesh = Loft.GenerateMesh(
                        heightBezier,
                        baseBezier,
                        heightSegs,
                        pathSegs
                        );
                    combineMeshInstances.Add(combineMeshInstance);

                    var topCap = new CombineInstance();
                    topCap.mesh = baseBezier.Triangulate(pathSegs, Vector3.up * totalHeight);
                    combineMeshInstances.Add(topCap);
                }

                scaleDown *= Mathf.Lerp(scaleDownMin, scaleDownMax, rand.NextSinglePositive());
            }

            var meshFilter = GetComponent <MeshFilter>();

            if (meshFilter.sharedMesh)
            {
                DestroyImmediate(meshFilter.sharedMesh);
            }

            meshFilter.sharedMesh = new Mesh();
            meshFilter.sharedMesh.CombineMeshes(combineMeshInstances.ToArray(), true, false);

            foreach (var combineInstance in combineMeshInstances)
            {
                DestroyImmediate(combineInstance.mesh);
            }
        }