Beispiel #1
0
        internal void Internal_UpdateMaterial()
        {
            MeshRendererComponent.sharedMaterials = new Material[] { Terrain.TerrainData.Shading.CustomMaterial };

            for (int i = 1; i < Terrain.TerrainData.Geometry.LODCount; ++i)
            {
                GTerrainChunkLOD chunkLod = GetChunkLOD(i);
                chunkLod.MeshRendererComponent.sharedMaterials = MeshRendererComponent.sharedMaterials;
            }
            SetLastUpdatedTimeNow();
        }
Beispiel #2
0
        public void Refresh()
        {
            Mesh lod0 = GetMesh(0);

            MeshFilterComponent.sharedMesh   = lod0;
            MeshColliderComponent.sharedMesh = lod0;

            for (int i = 1; i < Terrain.TerrainData.Geometry.LODCount; ++i)
            {
                Mesh             lodi      = GetMesh(i);
                GTerrainChunkLOD chunkLodi = GetChunkLOD(i);
                chunkLodi.MeshFilterComponent.sharedMesh = lodi;
            }
            SetLastUpdatedTimeNow();
        }
Beispiel #3
0
        private GTerrainChunkLOD GetChunkLOD(int level)
        {
            if (level <= 0 || level >= Terrain.TerrainData.Geometry.LODCount)
            {
                throw new System.IndexOutOfRangeException();
            }
            GTerrainChunkLOD c = ChunkLowerLOD[level - 1];

            if (c == null)
            {
                Transform t = GUtilities.GetChildrenWithName(transform, name + "_LOD" + level);
                c = t.gameObject.AddComponent <GTerrainChunkLOD>();
                ChunkLowerLOD[level - 1] = c;
            }
            return(c);
        }
Beispiel #4
0
        internal void Internal_UpdateRenderer()
        {
            MeshRendererComponent.shadowCastingMode = Terrain.TerrainData.Rendering.CastShadow ?
                                                      UnityEngine.Rendering.ShadowCastingMode.On :
                                                      UnityEngine.Rendering.ShadowCastingMode.Off;
            MeshRendererComponent.receiveShadows = Terrain.TerrainData.Rendering.ReceiveShadow;

            for (int i = 1; i < Terrain.TerrainData.Geometry.LODCount; ++i)
            {
                GTerrainChunkLOD chunkLod = GetChunkLOD(i);
                chunkLod.MeshRendererComponent.shadowCastingMode = MeshRendererComponent.shadowCastingMode;
                chunkLod.MeshRendererComponent.receiveShadows    = MeshRendererComponent.receiveShadows;
                chunkLod.MeshRendererComponent.sharedMaterials   = MeshRendererComponent.sharedMaterials;
            }
            SetLastUpdatedTimeNow();
        }
Beispiel #5
0
        internal void UpdateMesh(int lod, GAlbedoToVertexColorMode albedoToVertexColor)
        {
            Mesh m = GetMesh(lod);

            m.Clear();

            int leafCount        = generationMetadata[GGeometryJobUtilities.METADATA_LEAF_COUNT];
            int removedLeafCount = generationMetadata[GGeometryJobUtilities.METADATA_LEAF_REMOVED];

            if (leafCount != removedLeafCount)
            {
                vertexNativeArray.CopyTo(vertexArray);
                uvsNativeArray.CopyTo(uvsArray);
                normalsNativeArray.CopyTo(normalsArray);
                trianglesNativeArray.CopyTo(trianglesArray);

                m.vertices  = vertexArray;
                m.uv        = uvsArray;
                m.triangles = trianglesArray;
                m.normals   = normalsArray;

                if (albedoToVertexColor != GAlbedoToVertexColorMode.None)
                {
                    vertexColorsNativeArray.CopyTo(vertexColorsArray);
                    m.colors32 = vertexColorsArray;
                }

                m.RecalculateBounds();
                RecalculateTangentIfNeeded(m);
            }

            if (lod == 0)
            {
                MeshFilterComponent.sharedMesh   = m;
                MeshColliderComponent.sharedMesh = m;
            }
            else
            {
                GTerrainChunkLOD chunkLOD = GetChunkLOD(lod);
                chunkLOD.MeshFilterComponent.sharedMesh = m;
            }

            SetLastUpdatedTimeNow();
        }
Beispiel #6
0
        internal void SetupLODGroup(int lodCount)
        {
            float transitionStep = 1.0f / lodCount;

            LOD[] lods = new LOD[lodCount];
            lods[0] = new LOD(
                GRuntimeSettings.Instance.geometryGeneration.lodTransition.Evaluate(transitionStep),
                new Renderer[] { MeshRendererComponent });

            for (int level = 1; level < lodCount; ++level)
            {
                int i = level;
                lods[i] = new LOD(
                    GRuntimeSettings.Instance.geometryGeneration.lodTransition.Evaluate((i + 1) * transitionStep),
                    new Renderer[] { GetChunkLOD(i).MeshRendererComponent });

                GTerrainChunkLOD chunkLod = GetChunkLOD(i);
                chunkLod.MeshFilterComponent.sharedMesh = null;
                chunkLod.MeshRendererComponent.enabled  = true;
            }

            LodGroupComponent.SetLODs(lods);
            Internal_UpdateRenderer();

            List <GameObject> childLod = new List <GameObject>();
            int maxIndex = transform.childCount - 1;

            for (int i = lodCount - 1; i <= maxIndex; ++i)
            {
                Transform child = transform.GetChild(i);
                if (child != null)
                {
                    childLod.Add(transform.GetChild(i).gameObject);
                }
            }
            for (int i = 0; i < childLod.Count; ++i)
            {
                GUtilities.DestroyGameobject(childLod[i]);
            }
        }
Beispiel #7
0
        internal void Internal_UpdateLODsAsync(HashSet <Vector2> vertexPool)
        {
            LOD lod0 = new LOD(0, new Renderer[] { MeshRendererComponent });

            LodGroupComponent.SetLODs(new LOD[] { lod0 });
            if (Terrain.TerrainData.Geometry.LODCount == 1)
            {
                GUtilities.ClearChildren(transform);
            }

            for (int i = 1; i < Terrain.TerrainData.Geometry.LODCount; ++i)
            {
                GTerrainChunkLOD chunkLod = GetChunkLOD(i);
                chunkLod.MeshRendererComponent.enabled = false;
            }

            int  meshResolution            = 0;
            Rect uvRange                   = GetUvRange();
            HashSet <Vector2> seamVertices = new HashSet <Vector2>();

            subDivTree.ForEachLeaf(n =>
            {
                if (!GUtilities.IsRectContainPointExclusive(uvRange, n.V0))
                {
                    seamVertices.Add(n.V0);
                }
                if (!GUtilities.IsRectContainPointExclusive(uvRange, n.V1))
                {
                    seamVertices.Add(n.V1);
                }
                if (!GUtilities.IsRectContainPointExclusive(uvRange, n.V2))
                {
                    seamVertices.Add(n.V2);
                }
                meshResolution = Mathf.Max(meshResolution, n.Level);
            });

            ThreadPool.QueueUserWorkItem(state =>
            {
                try
                {
                    GSubDivisionTree[] lodTrees = new GSubDivisionTree[Terrain.TerrainData.Geometry.LODCount];
                    for (int level = 1; level < Terrain.TerrainData.Geometry.LODCount; ++level)
                    {
                        int i = level;
                        int baseResolution = Terrain.TerrainData.Geometry.PolygonDistribution == GPolygonDistributionMode.Dynamic ?
                                             Terrain.TerrainData.Geometry.MeshBaseResolution :
                                             0;
                        int targetResolution = Mathf.Max(baseResolution, meshResolution - i);
                        lodTrees[i]          = subDivTree.Clone(targetResolution);
                        StitchSeamLOD(lodTrees[i], seamVertices);
                        //StitchGeometrySeamsOnSubDivTree(vertexPool, lodTrees[i]);
                        EnqueueMainThreadJob(() =>
                        {
                            Mesh meshLod = GetMesh(i);
                            UpdateMesh(meshLod, lodTrees[i]);
                            GTerrainChunkLOD chunkLod = GetChunkLOD(i);
                            chunkLod.MeshFilterComponent.sharedMesh = meshLod;
                        });
                    }

                    EnqueueMainThreadJob(() =>
                    {
                        float transitionStep = 1.0f / Terrain.TerrainData.Geometry.LODCount;

                        LOD[] lods = new LOD[Terrain.TerrainData.Geometry.LODCount];
                        lods[0]    = new LOD(
                            GGriffinSettings.Instance.LodTransition.Evaluate(transitionStep),
                            new Renderer[] { MeshRendererComponent });

                        for (int level = 1; level < Terrain.TerrainData.Geometry.LODCount; ++level)
                        {
                            int i   = level;
                            lods[i] = new LOD(
                                GGriffinSettings.Instance.LodTransition.Evaluate((i + 1) * transitionStep),
                                new Renderer[] { GetChunkLOD(i).MeshRendererComponent });

                            GTerrainChunkLOD chunkLod = GetChunkLOD(i);
                            chunkLod.MeshRendererComponent.enabled = true;
                        }

                        LodGroupComponent.SetLODs(lods);
                        Internal_UpdateRenderer();
                    });
                }
                catch (System.Exception e)
                {
                    Debug.Log(e.ToString());
                }
            });
        }