Пример #1
0
        public void UpdateTerrain()
        {
            if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode && playMode == EditorPlay.isEditor)
            {
                Transform[] gamos = GetComponentsInChildren <Transform>();
                CanGenerate = false;
                EndAllThreads();
                for (int i = 0; i < gamos.Length; i++)
                {
                    if (gamos[i] != transform)
                    {
                        DestroyImmediate(gamos[i].gameObject);
                    }
                }
                SaveTerrains();
                playMode = EditorPlay.isPlaying;
            }
            if (playMode == EditorPlay.isEditor)
            {
                if (UnityEditor.EditorApplication.isCompiling == false && CanGenerate == false)
                {
                    Transform[] gamos = GetComponentsInChildren <Transform>();
                    for (int i = 0; i < gamos.Length; i++)
                    {
                        if (gamos[i] != transform)
                        {
                            DestroyImmediate(gamos[i].gameObject);
                        }
                    }
                    SaveTerrains();
                    Initialize();
                }
                else if (UnityEditor.EditorApplication.isCompiling && CanGenerate == true)
                {
                    CanGenerate = false;
                    Transform[] gamos = GetComponentsInChildren <Transform>();
                    for (int i = 0; i < gamos.Length; i++)
                    {
                        if (gamos[i] != transform)
                        {
                            DestroyImmediate(gamos[i].gameObject);
                            for (int t = 0; t < thread.Length; t++)
                            {
                                if (thread[t] != null)
                                {
                                    thread[t].Abort();
                                }
                            }

                            CanGenerate = false;
                        }
                    }
                    SaveTerrains();
                }
            }

            if (Camera.current != null && UnityEditor.Selection.Contains(gameObject))
            {
                if (timer <= 0.25f)
                {
                    timer += 0.03f;
                }
                Ray myray;
                if (playMode == EditorPlay.isEditor)
                {
                    myray = UnityEditor.HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                    RaycastHit hit;

                    if (Physics.Raycast(myray, out hit, Mathf.Infinity))
                    {
                        Shader.SetGlobalVector("_highlightArea", hit.point);
                        Event E = Event.current;
                        if (E.button == 1 && E.control && timer >= 0.25f)
                        {
                            SetVoxels(hit.point, value, effectArea);
                            timer = 0;
                        }
                        if (E.button == 1 && E.alt && timer >= 0.25f)
                        {
                            setMaterial(hit.point, VoxelMaterial, effectArea);
                            timer = 0;
                        }

                        Shader.SetGlobalFloat("_highlightSize", effectArea);
                    }
                }
            }
            else
            {
                Shader.SetGlobalFloat("_highlightSize", 0);
            }



            //set the wind variables on the shader comment this out if you dont use my grass shader
            if (Zone != null)
            {
                Shader.SetGlobalFloat("WindAmount", Zone.windMain);
                Shader.SetGlobalFloat("WaveSize", Zone.windPulseMagnitude);
                Shader.SetGlobalFloat("windSpeed", Zone.windPulseFrequency);
                Shader.SetGlobalFloat("SqrDistance", Zone.windTurbulence);
                Shader.SetGlobalFloat("WindDirectionx", Zone.transform.forward.x);
                Shader.SetGlobalFloat("WindDirectiony", Zone.transform.forward.y);
                Shader.SetGlobalFloat("WindDirectionz", Zone.transform.forward.z);
            }

            //if we have meshes to destroy destroy them
            //need to implement a pooling system will save on memory
            //as well as stop the garbage collector from being called
            if (Trash.Count > 0)
            {
                VoxelChunk myChunk = Trash.Dequeue();
                if (myChunk.mesh != null)
                {
                    DestroyImmediate(myChunk.mesh);
                }
                myChunk.mesh = null;
                DestroyImmediate(myChunk.m_mesh);
                myChunk.canDraw       = false;
                myChunk.canCreatemesh = false;
            }
            //create the mesh . this is done in update as calling it from another thread is not allowed
            if (MeshChunks.Count > 0)
            {
                VoxelChunk chunk = MeshChunks.Dequeue();
                chunk.CreateMesh();
                chunk.canCreatemesh = false;
                if (ActiveChunks.Contains(chunk) == false)
                {
                    ActiveChunks.Add(chunk);
                }
            }
            if (ActiveChunks.Count > 0)
            {
                for (int i = 0; i < ActiveChunks.Count; i++)
                {
                    ActiveChunks[i].RenderGrass();
                }
            }
            //need to some how check for occlusion and not render things behind other objects
        }