private void HandleSplit(SgtDynamicPlanetChunk chunk)
        {
            var cubeH  = chunk.CubeH * 0.5;
            var cubeV  = chunk.CubeV * 0.5;
            var coordM = chunk.CoordM * 0.5;
            var coordH = new double2(coordM, 0.0);
            var coordV = new double2(0.0, coordM);

            chunk.Split       = true;
            chunk.Children[0] = SgtDynamicPlanetChunk.Create(chunk, chunk.Cube, cubeH, cubeV, chunk.Coord, coordM);
            chunk.Children[1] = SgtDynamicPlanetChunk.Create(chunk, chunk.Cube + cubeH, cubeH, cubeV, chunk.Coord + coordH, coordM);
            chunk.Children[2] = SgtDynamicPlanetChunk.Create(chunk, chunk.Cube + cubeV, cubeH, cubeV, chunk.Coord + coordV, coordM);
            chunk.Children[3] = SgtDynamicPlanetChunk.Create(chunk, chunk.Cube + cubeH + cubeV, cubeH, cubeV, chunk.Coord + coordH + coordV, coordM);

            Hide(chunk);

            for (var c = 0; c < 4; c++)
            {
                var child = chunk.Children[c];

                Generate(child);

                Show(child);
            }

            if (OnSplitChunk != null)
            {
                OnSplitChunk.Invoke(chunk);
            }
        }
Beispiel #2
0
        private void Rebase(SgtDynamicPlanetChunk chunk, int index)
        {
            var combine = combines[index];

            combine.transform = Matrix4x4.Translate((float3)(chunk.Corner - origin));

            combines[index] = combine;
        }
        private GameObject Spawn(SgtDynamicPlanetChunk chunk)
        {
            var clone        = Instantiate(prefab);
            var localPoint   = math.normalize(chunk.Cube + chunk.CubeH * UnityEngine.Random.value + chunk.CubeV * UnityEngine.Random.value);
            var sampledPoint = default(double3);

            cachedPlanet.TryGetLocalPoint(localPoint, ref sampledPoint);

            clone.transform.SetParent(null, false);
            clone.transform.position = cachedPlanet.TransformPoint(sampledPoint);

            switch (rotate)
            {
            case RotateType.Randomly:
            {
                clone.transform.rotation = UnityEngine.Random.rotation;
            }
            break;

            case RotateType.ToSurfaceNormal:
            {
                var localRight    = math.normalize(math.normalize(localPoint + chunk.CubeH) - localPoint) * chunk.CoordM * SgtDynamicPlanetChunk.Config.STEP;
                var localForward  = math.normalize(math.normalize(localPoint + chunk.CubeV) - localPoint) * chunk.CoordM * SgtDynamicPlanetChunk.Config.STEP;
                var sampledPointL = default(double3);
                var sampledPointR = default(double3);
                var sampledPointB = default(double3);
                var sampledPointF = default(double3);

                cachedPlanet.TryGetLocalPoint(localPoint - localRight, ref sampledPointL);
                cachedPlanet.TryGetLocalPoint(localPoint + localRight, ref sampledPointR);
                cachedPlanet.TryGetLocalPoint(localPoint - localForward, ref sampledPointB);
                cachedPlanet.TryGetLocalPoint(localPoint + localForward, ref sampledPointF);

                var vectorA = sampledPointR - sampledPointL;
                var vectorB = sampledPointF - sampledPointB;
                var normal  = math.normalize(-math.cross(vectorA, vectorB));
                var angle   = UnityEngine.Random.Range(-math.PI, math.PI);

                clone.transform.up = cachedPlanet.TransformVector(normal);
                clone.transform.Rotate(0.0f, angle, 0.0f, Space.Self);
            }
            break;

            case RotateType.ToPlanetCenter:
            {
                var normal = math.normalize(sampledPoint);
                var angle  = UnityEngine.Random.Range(-math.PI, math.PI);

                clone.transform.up = cachedPlanet.TransformVector(normal);
                clone.transform.Rotate(0.0f, angle, 0.0f, Space.Self);
            }
            break;
            }

            return(clone.gameObject);
        }
        private SgtDynamicPlanetChunk BuildRootChunk(float3 cube, float3 cubeH, float3 cubeV)
        {
            var rotation = quaternion.Euler(math.atan(1.0f / math.sqrt(2.0f)), 0.0f, 0.785398f);

            cube  = math.mul(rotation, cube);
            cubeH = math.mul(rotation, cubeH);
            cubeV = math.mul(rotation, cubeV);

            return(SgtDynamicPlanetChunk.Create(null, cube, cubeH, cubeV, new double2(0.0, 0.0), 1.0));
        }
Beispiel #5
0
        private void HandleMergeChunk(SgtDynamicPlanetChunk chunk)
        {
            var clone = default(GameObject);

            if (clones.TryGetValue(chunk, out clone) == true)
            {
                clones.Remove(chunk);

                Destroy(clone);
            }
        }
        private void Hide(SgtDynamicPlanetChunk chunk)
        {
            if (chunk != null && chunk.Visual != null)
            {
                chunk.Visual.Remove(chunk);

                if (OnHideChunk != null)
                {
                    OnHideChunk.Invoke(chunk);
                }
            }
        }
        private void HandleMergeChunk(SgtDynamicPlanetChunk chunk)
        {
            var cloneList = default(List <GameObject>);

            if (cloneLists.TryGetValue(chunk, out cloneList) == true)
            {
                cloneLists.Remove(chunk);

                foreach (var clone in cloneList)
                {
                    Destroy(clone);
                }
            }
        }
        private void Show(SgtDynamicPlanetChunk chunk)
        {
            if (chunk != null && chunk.Visual == null)
            {
                var visual = GetVisual(chunk.Depth);

                visual.Add(chunk);

                if (OnShowChunk != null)
                {
                    OnShowChunk.Invoke(chunk);
                }
            }
        }
Beispiel #9
0
        public static SgtDynamicPlanetChunk Create(SgtDynamicPlanetChunk parent, double3 cube, double3 cubeH, double3 cubeV, double2 coord, double coordM)
        {
            var chunk = Create();

            chunk.Split  = false;
            chunk.Depth  = parent != null ? parent.Depth + 1 : 0;
            chunk.Parent = parent;
            chunk.Cube   = cube;
            chunk.CubeH  = cubeH;
            chunk.CubeV  = cubeV;
            chunk.Coord  = coord;
            chunk.CoordM = coordM;

            return(chunk);
        }
Beispiel #10
0
        public void Remove(SgtDynamicPlanetChunk chunk)
        {
            for (var i = chunks.Count - 1; i >= 0; i--)
            {
                if (chunks[i] == chunk)
                {
                    chunks.RemoveAt(i);
                    combines.RemoveAt(i);

                    break;
                }
            }

            chunk.Visual = null;

            dirty = true;
        }
Beispiel #11
0
        private void HandleSplitChunk(SgtDynamicPlanetChunk chunk)
        {
            if (chunk.Depth == cachedPlanet.GetDepth(triangleSize))
            {
                var clone    = new GameObject("Collider");
                var collider = clone.AddComponent <MeshCollider>();

                clone.layer = layer;

                collider.sharedMaterial = material;
                collider.sharedMesh     = chunk.Mesh;

                clone.transform.SetParent(transform, false);
                clone.transform.localPosition = (float3)chunk.Corner;

                clones.Add(chunk, clone);
            }
        }
        private void HandleSplitChunk(SgtDynamicPlanetChunk chunk)
        {
            if (chunk.Depth == cachedPlanet.GetDepth(triangleSize) && prefab != null)
            {
                var count = GetCount(chunk.Corner);

                if (count > 0)
                {
                    var cloneList = new List <GameObject>();

                    for (var i = 0; i < count; i++)
                    {
                        cloneList.Add(Spawn(chunk));
                    }

                    cloneLists.Add(chunk, cloneList);
                }
            }
        }
        private void HandleMerge(SgtDynamicPlanetChunk chunk)
        {
            for (var c = 0; c < 4; c++)
            {
                Hide(chunk.Children[c]);

                SgtDynamicPlanetChunk.Pool.Push(chunk.Children[c]);

                chunk.Children[c] = null;
            }

            chunk.Split = false;

            Show(chunk);

            if (OnMergeChunk != null)
            {
                OnMergeChunk.Invoke(chunk);
            }
        }
Beispiel #14
0
        public void Add(SgtDynamicPlanetChunk chunk)
        {
            var index   = chunks.Count;
            var combine = new CombineInstance();

            chunk.Visual = this;

            combine.mesh = chunk.Mesh;

            chunks.Add(chunk);
            combines.Add(combine);

            if (index == 0)
            {
                Rebase();
            }
            else
            {
                Rebase(chunk, index);
            }

            dirty = true;
        }
Beispiel #15
0
        private void Generate(SgtDynamicPlanetChunk chunk)
        {
            var startJob = new ChunkStartJob();

            startJob.Points  = SgtDynamicPlanetChunk.Config.POINTS;
            startJob.Cube    = chunk.Cube;
            startJob.CubeH   = chunk.CubeH;
            startJob.CubeV   = chunk.CubeV;
            startJob.Vectors = chunkVectors;
            startJob.Heights = chunkHeights;

            var handle = startJob.Schedule();

            foreach (var cachedHeightWriter in CachedHeightWriters)             // NOTE: Property
            {
                cachedHeightWriter.WriteHeight(ref handle, chunkHeights, chunkVectors);
            }

            if (heightmapData.IsCreated == true)
            {
                var job = new HeightmapJob();

                job.Heights    = chunkHeights;
                job.Vectors    = chunkVectors;
                job.Data       = heightmapData;
                job.WaterLevel = waterLevel;
                job.Size       = heightmapSize;
                job.Scale      = heightmapSize / new double2(math.PI * 2.0, math.PI);

                handle = job.Schedule(chunkVectors.Length, math.max(1, chunkVectors.Length / SgtDynamicPlanetChunk.Config.BATCH_SPLIT), handle);
            }

            var scaleJob = new ScaleJob();

            scaleJob.Heights      = chunkHeights;
            scaleJob.Radius       = radius;
            scaleJob.Displacement = displacement;
            scaleJob.WaterLevel   = waterLevel;
            scaleJob.ClampWater   = clampWater;

            handle = scaleJob.Schedule(chunkHeights.Length, 1, handle);

            var endJob = new ChunkEndJob();

            endJob.Heights       = chunkHeights;
            endJob.Vectors       = chunkVectors;
            endJob.Coord         = chunk.Coord;
            endJob.CoordM        = chunk.CoordM;
            endJob.MeshPositions = meshPositions;
            endJob.MeshCoords0   = meshCoords0;
            endJob.MeshNormals   = meshNormals;
            endJob.MeshTangents  = meshTangents;

            endJob.Schedule(handle).Complete();

            chunk.Corner = chunkVectors[SgtDynamicPlanetChunk.Config.LAST_POINT] * chunkHeights[SgtDynamicPlanetChunk.Config.LAST_POINT];

            chunk.Mesh.SetVertices(ConvertNativeArray(meshPositions));
            chunk.Mesh.SetNormals(ConvertNativeArray(meshNormals));
            chunk.Mesh.SetTangents(ConvertNativeArray(meshTangents));
            chunk.Mesh.SetUVs(0, ConvertNativeArray(meshCoords0));
            chunk.Mesh.SetTriangles(SgtDynamicPlanetVisual.Config.INDICES, 0);
        }