private void CheckKeys()
 {
   for (int index = 0; index < TerrainInspector.s_ToolKeys.Length; ++index)
   {
     if (TerrainInspector.s_ToolKeys[index].activated)
     {
       this.selectedTool = (TerrainTool) index;
       this.Repaint();
       Event.current.Use();
     }
   }
   if (TerrainInspector.s_PrevBrush.activated)
   {
     --this.m_SelectedBrush;
     if (this.m_SelectedBrush < 0)
       this.m_SelectedBrush = TerrainInspector.s_BrushTextures.Length - 1;
     this.Repaint();
     Event.current.Use();
   }
   if (TerrainInspector.s_NextBrush.activated)
   {
     ++this.m_SelectedBrush;
     if (this.m_SelectedBrush >= TerrainInspector.s_BrushTextures.Length)
       this.m_SelectedBrush = 0;
     this.Repaint();
     Event.current.Use();
   }
   int num = 0;
   if (TerrainInspector.s_NextTexture.activated)
     num = 1;
   if (TerrainInspector.s_PrevTexture.activated)
     num = -1;
   if (num == 0)
     return;
   switch (this.selectedTool)
   {
     case TerrainTool.PaintTexture:
       this.m_SelectedSplat = (int) Mathf.Repeat((float) (this.m_SelectedSplat + num), (float) this.m_Terrain.terrainData.splatPrototypes.Length);
       Event.current.Use();
       this.Repaint();
       break;
     case TerrainTool.PlaceTree:
       if (TreePainter.selectedTree >= 0)
         TreePainter.selectedTree = (int) Mathf.Repeat((float) (TreePainter.selectedTree + num), (float) this.m_TreeContents.Length);
       else if (num == -1 && this.m_TreeContents.Length > 0)
         TreePainter.selectedTree = this.m_TreeContents.Length - 1;
       else if (num == 1 && this.m_TreeContents.Length > 0)
         TreePainter.selectedTree = 0;
       Event.current.Use();
       this.Repaint();
       break;
     case TerrainTool.PaintDetail:
       this.m_SelectedDetail = (int) Mathf.Repeat((float) (this.m_SelectedDetail + num), (float) this.m_Terrain.terrainData.detailPrototypes.Length);
       Event.current.Use();
       this.Repaint();
       break;
   }
 }
        protected override void OnButtonClicked(UIComponent comp)
        {
            int          zOrder       = comp.zOrder;
            TerrainTool  terrainTool  = null;
            ResourceTool resourceTool = null;

            if (zOrder < kTools.Length + 1)
            {
                terrainTool = ToolsModifierControl.SetTool <TerrainTool>();
                if (terrainTool == null)
                {
                    return;
                }
                ShowUndoTerrainOptionsPanel(true);
                UIView.library.Show("LandscapingInfoPanel");
            }
            else
            {
                resourceTool = ToolsModifierControl.SetTool <ResourceTool>();
                if (resourceTool == null)
                {
                    return;
                }
                UIView.library.Hide("LandscapingInfoPanel");
                ShowUndoTerrainOptionsPanel(false);
            }
            ShowBrushOptionsPanel(true);

            if (zOrder == 1 || zOrder == 3)
            {
                ShowLevelHeightPanel(true);
            }
            else
            {
                ShowLevelHeightPanel(false);
            }
            //begin mod
            if (zOrder < kTools.Length)
            {
                terrainTool.m_mode        = LandscapingPanelDetour.kTools[zOrder].enumValue;
                TerrainToolDetour.isDitch = false;
            }
            else
            {
                if (zOrder < kTools.Length + 1)
                {
                    terrainTool.m_mode        = TerrainTool.Mode.Shift;
                    TerrainToolDetour.isDitch = true;
                }
                else
                {
                    resourceTool.m_resource = NaturalResourceManager.Resource.Sand;
                }
            }
            //end mod
        }
        public new void OnClick(UIComponent comp, UIMouseEventParameter p)
        {
            UIButton uiButton = p.source as UIButton;
            int      byIndex  = this.GetByIndex((UIComponent)uiButton);

            if (byIndex == -1)
            {
                return;
            }
            this.SelectByIndex(byIndex);
            Texture2D texture2D        = uiButton.objectUserData as Texture2D;
            var       brushesContainer =
                (UIScrollablePanel)typeof(BrushOptionPanel).GetField("m_BrushesContainer", BindingFlags.NonPublic | BindingFlags.Instance)
                .GetValue(this);

            if (!((UnityEngine.Object)uiButton.parent == (UnityEngine.Object)brushesContainer) || !((UnityEngine.Object)texture2D != (UnityEngine.Object)null))
            {
                return;
            }
            TerrainTool tool1 = ToolsModifierControl.GetTool <TerrainTool>();

            if ((UnityEngine.Object)tool1 != (UnityEngine.Object)null)
            {
                tool1.m_brush = texture2D;
            }
            TreeTool tool2 = ToolsModifierControl.GetTool <TreeTool>();

            if ((UnityEngine.Object)tool2 != (UnityEngine.Object)null)
            {
                tool2.m_brush = texture2D;
            }
            ResourceTool tool3 = ToolsModifierControl.GetTool <ResourceTool>();

            if ((UnityEngine.Object)tool3 != (UnityEngine.Object)null)
            {
                tool3.m_brush = texture2D;
            }
            PropTool tool4 = ToolsModifierControl.GetTool <PropTool>();

            if ((UnityEngine.Object)tool4 == (UnityEngine.Object)null)
            {
                tool4.m_brush = texture2D;
            }
            //begin mod
            try
            {
                Plugins.SetBrush(texture2D);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogException(e);
            }
            //end mod
        }
        private void SetBrushSize(float val)
        {
            var brushSizeSlider =
                (UISlider)typeof(BrushOptionPanel).GetField("m_BrushSizeSlider", BindingFlags.NonPublic | BindingFlags.Instance)
                .GetValue(this);

            PropTool currentTool1 = ToolsModifierControl.GetCurrentTool <PropTool>();

            if ((UnityEngine.Object)currentTool1 != (UnityEngine.Object)null)
            {
                currentTool1.m_brushSize = val;
                currentTool1.m_mode      = (double)currentTool1.m_brushSize != (double)brushSizeSlider.minValue ? PropTool.Mode.Brush : PropTool.Mode.Single;
            }
            TerrainTool currentTool2 = ToolsModifierControl.GetCurrentTool <TerrainTool>();

            //begin mod
            if ((UnityEngine.Object)currentTool2 != (UnityEngine.Object)null)
            {
                currentTool2.m_brushSize     = val;
                TerrainToolDetour.m_sizeMode = (double)currentTool2.m_brushSize != (double)brushSizeSlider.minValue ? TerrainToolDetour.SizeMode.Brush : TerrainToolDetour.SizeMode.Single;
            }
            //end mod
            TreeTool currentTool3 = ToolsModifierControl.GetCurrentTool <TreeTool>();

            if ((UnityEngine.Object)currentTool3 != (UnityEngine.Object)null)
            {
                currentTool3.m_brushSize = val;
                currentTool3.m_mode      = (double)currentTool3.m_brushSize != (double)brushSizeSlider.minValue ? TreeTool.Mode.Brush : TreeTool.Mode.Single;
            }
            ResourceTool currentTool4 = ToolsModifierControl.GetCurrentTool <ResourceTool>();

            if ((UnityEngine.Object)currentTool4 != (UnityEngine.Object)null)
            {
                currentTool4.m_brushSize = val;
            }
            //begin mod
            try
            {
                Plugins.SetSize(val, val == (double)brushSizeSlider.minValue);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogException(e);
            }
            //end mod
        }
Esempio n. 5
0
        public static void Prefix()
        {
            TerrainTool tool = ToolsModifierControl.GetTool <TerrainTool>();

            if (!TerrainHandler.IgnoreAll && ReflectionHelper.GetAttr <ToolBase.ToolErrors>(tool, "m_toolErrors") == ToolBase.ToolErrors.None)
            {
                Command.SendToAll(new TerrainModificationCommand
                {
                    BrushData      = Singleton <ToolController> .instance.BrushData,
                    BrushSize      = tool.m_brushSize,
                    StartPosition  = ReflectionHelper.GetAttr <Vector3>(tool, "m_startPosition"),
                    EndPosition    = ReflectionHelper.GetAttr <Vector3>(tool, "m_endPosition"),
                    MousePosition  = ReflectionHelper.GetAttr <Vector3>(tool, "m_mousePosition"),
                    Mode           = tool.m_mode,
                    Strength       = tool.m_strength,
                    MouseRightDown = ReflectionHelper.GetAttr <bool>(tool, "m_mouseRightDown")
                });
            }
        }
        public override void Handle(TerrainModificationCommand command)
        {
            TerrainTool tool = ToolSimulator.GetTool <TerrainTool>(command.SenderId);

            // Apply data from command
            command.BrushData.CopyTo(ReflectionHelper.GetAttr <ToolController>(tool, "m_toolController").BrushData, 0);
            tool.m_brushSize = command.BrushSize;
            tool.m_strength  = command.Strength;
            ReflectionHelper.SetAttr(tool, "m_mousePosition", command.MousePosition);
            ReflectionHelper.SetAttr(tool, "m_startPosition", command.StartPosition);
            ReflectionHelper.SetAttr(tool, "m_endPosition", command.EndPosition);
            ReflectionHelper.SetAttr(tool, "m_currentCost", 0);
            tool.m_mode = command.Mode;
            ReflectionHelper.SetAttr(tool, "m_mouseRightDown", command.MouseRightDown);

            TerrainHandler.IgnoreAll = true;
            // Call original method
            ReflectionHelper.Call(tool, "ApplyBrush");

            TerrainHandler.IgnoreAll = false;
        }
        private void SetBrushStrength(float val)
        {
            PropTool currentTool1 = ToolsModifierControl.GetCurrentTool <PropTool>();

            if ((UnityEngine.Object)currentTool1 != (UnityEngine.Object)null)
            {
                currentTool1.m_strength = val;
            }
            TerrainTool currentTool2 = ToolsModifierControl.GetCurrentTool <TerrainTool>();

            if ((UnityEngine.Object)currentTool2 != (UnityEngine.Object)null)
            {
                currentTool2.m_strength = val;
            }
            TreeTool currentTool3 = ToolsModifierControl.GetCurrentTool <TreeTool>();

            if ((UnityEngine.Object)currentTool3 != (UnityEngine.Object)null)
            {
                currentTool3.m_strength = val;
            }
            ResourceTool currentTool4 = ToolsModifierControl.GetCurrentTool <ResourceTool>();

            if ((UnityEngine.Object)currentTool4 != (UnityEngine.Object)null)
            {
                currentTool4.m_strength = val;
            }
            //begin mod
            try
            {
                Plugins.SetStrength(val);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogException(e);
            }
            //end mod
        }
        protected override void OnButtonClicked(UIComponent comp)
        {
            int         zOrder      = comp.zOrder;
            TerrainTool terrainTool = ToolsModifierControl.SetTool <TerrainTool>();

            if (terrainTool == null)
            {
                return;
            }
            var panel = (TerrainPanel)Convert.ChangeType(this, typeof(TerrainPanel));

            ShowUndoTerrainOptionsPanel(panel, true);
            ShowBrushOptionsPanel(panel, true);
            //begin mod
            UIView.library.Show("LandscapingInfoPanel");
            //end mod
            if (zOrder == 1 || zOrder == 3)
            {
                ShowLevelHeightPanel(panel, true);
            }
            else
            {
                ShowLevelHeightPanel(panel, false);
            }
            //begin mod
            if (zOrder < kTools.Length)
            {
                terrainTool.m_mode        = TerrainPanelDetour.kTools[zOrder].enumValue;
                TerrainToolDetour.isDitch = false;
            }
            else
            {
                terrainTool.m_mode        = TerrainTool.Mode.Shift;
                TerrainToolDetour.isDitch = true;
            }
            //end mod
        }
 public override void OnInspectorGUI()
 {
   this.Initialize();
   if (TerrainInspector.styles == null)
     TerrainInspector.styles = new TerrainInspector.Styles();
   if (!(bool) ((UnityEngine.Object) this.m_Terrain.terrainData))
   {
     GUI.enabled = false;
     GUILayout.BeginHorizontal();
     GUILayout.FlexibleSpace();
     GUILayout.Toolbar(-1, TerrainInspector.styles.toolIcons, TerrainInspector.styles.command, new GUILayoutOption[0]);
     GUILayout.FlexibleSpace();
     GUILayout.EndHorizontal();
     GUI.enabled = true;
     GUILayout.BeginVertical(EditorStyles.helpBox, new GUILayoutOption[0]);
     GUILayout.Label("Terrain Asset Missing");
     this.m_Terrain.terrainData = EditorGUILayout.ObjectField("Assign:", (UnityEngine.Object) this.m_Terrain.terrainData, typeof (TerrainData), false, new GUILayoutOption[0]) as TerrainData;
     GUILayout.EndVertical();
   }
   else
   {
     GUILayout.BeginHorizontal();
     GUILayout.FlexibleSpace();
     GUI.changed = false;
     int selectedTool = (int) this.selectedTool;
     this.selectedTool = (TerrainTool) GUILayout.Toolbar(selectedTool, TerrainInspector.styles.toolIcons, TerrainInspector.styles.command, new GUILayoutOption[0]);
     if (this.selectedTool != (TerrainTool) selectedTool && (UnityEngine.Object) Toolbar.get != (UnityEngine.Object) null)
       Toolbar.get.Repaint();
     GUILayout.FlexibleSpace();
     GUILayout.EndHorizontal();
     this.CheckKeys();
     GUILayout.BeginVertical(EditorStyles.helpBox, new GUILayoutOption[0]);
     if (selectedTool >= 0 && selectedTool < TerrainInspector.styles.toolIcons.Length)
     {
       GUILayout.Label(TerrainInspector.styles.toolNames[selectedTool].text);
       GUILayout.Label(TerrainInspector.styles.toolNames[selectedTool].tooltip, EditorStyles.wordWrappedMiniLabel, new GUILayoutOption[0]);
     }
     else
     {
       GUILayout.Label("No tool selected");
       GUILayout.Label("Please select a tool", EditorStyles.wordWrappedMiniLabel, new GUILayoutOption[0]);
     }
     GUILayout.EndVertical();
     switch (selectedTool)
     {
       case 0:
         this.ShowRaiseHeight();
         break;
       case 1:
         this.ShowSetHeight();
         break;
       case 2:
         this.ShowSmoothHeight();
         break;
       case 3:
         this.ShowTextures();
         break;
       case 4:
         this.ShowTrees();
         break;
       case 5:
         this.ShowDetails();
         break;
       case 6:
         this.ShowSettings();
         break;
     }
     GUILayout.Space(5f);
   }
 }
Esempio n. 10
0
    // Update is called once per frame
    public void EditorUpdate(Camera cam)
    {
        //Event e = Event.current;
        //if (e.type == EventType.MouseDown) {
        //    mouseDownPos = e.mousePosition;
        //    if (e.button == 0) {
        //        if (curOperator == Operator.AddBlock) {
        //            if (bShowPreview) {
        //                int gx = (int)previewBlockPos.x;
        //                int gy = (int)previewBlockPos.y;
        //                int gz = (int)previewBlockPos.z;
        //                blockManager.setBlock(gx, gy, gz, (short)BlockTypeEnum.BT_Sand);
        //                bDirty = true;
        //            }
        //        }
        //        if (curOperator == Operator.DeleteBlock) {
        //            if (bShowPreview) {
        //                int gx = (int)selectedBlockPos.x;
        //                int gy = (int)selectedBlockPos.y;
        //                int gz = (int)selectedBlockPos.z;
        //                blockManager.setBlock(gx, gy, gz, (short)BlockTypeEnum.BT_Air);
        //                bDirty = true;
        //            }
        //        }
        //    }
        //}
        //else if (e.type == EventType.mouseMove) {
        //    if (curOperator == Operator.AddBlock || curOperator == Operator.DeleteBlock) {
        //        Ray ray = cam.ScreenPointToRay(new Vector3(e.mousePosition.x, cam.pixelHeight - e.mousePosition.y, 0));
        //        Vector3 startPos = transform.InverseTransformPoint(ray.origin);
        //        Vector3 dir = transform.InverseTransformDirection(ray.direction);
        //        RayCastRestult rlt = rtm.rayCast(startPos, dir, 50);
        //        bShowPreview = rlt.bHit;
        //        if (rlt.bHit) {
        //            //Debug.Log(rlt.hitFaceIndex.ToString());
        //            if (curOperator == Operator.AddBlock) {
        //                Vector3 hitPos = startPos + dir * (rlt.hitLength + 0.001f);
        //                float gx = (int)(hitPos.x / Const.BlockSize) + 0.5f;
        //                float gy = (int)(hitPos.y / Const.BlockSize) + 0.5f;
        //                float gz = (int)(hitPos.z / Const.BlockSize) + 0.5f;
        //                selectedBlockPos = new Vector3(gx, gy, gz);
        //                previewBlockPos = new Vector3(gx, gy, gz) + Const.getFaceNormal(rlt.hitFaceIndex) * Const.BlockSize;
        //            }
        //            else if (curOperator == Operator.DeleteBlock) {
        //                Vector3 hitPos = startPos + dir * (rlt.hitLength + 0.001f);
        //                float gx = (int)(hitPos.x / Const.BlockSize) + 0.5f;
        //                float gy = (int)(hitPos.y / Const.BlockSize) + 0.5f;
        //                float gz = (int)(hitPos.z / Const.BlockSize) + 0.5f;
        //                selectedBlockPos = new Vector3(gx, gy, gz);
        //            }
        //        }
        //    }
        //}

        if (bDirty)
        {
            bDirty = false;
            DebugTool.Log("开始更新光线追踪");
            rtm.clearAll();
            blockManager.forEachBlock((short block, int gx, int gy, int gz) => {
                if (block != (short)BlockTypeEnum.Air)
                {
                    rtm.setBlock(gx, gy, gz, RayCastBlockType.All);
                }
            });

            //初始化AO计算
            RayMarchingAo rma = new RayMarchingAo();
            rma.Init(rtm);

            m_meshes.Clear();
            DebugTool.Log("开始计算光照");
            TerrainTool.calcLight2(blockManager, 32);
            DebugTool.Log("开始更新网格");

            BlockTypeFun blockTypeFun = new BlockTypeFun();

            for (int i = 0; i < SpaceX; i++)
            {
                for (int k = 0; k < SpaceZ; k++)
                {
                    for (int j = 0; j < SpaceY; j++)
                    {
                        BlockChunk chunk = new BlockChunk(blockManager, i * Const.ChunkSize, j * Const.ChunkSize, k * Const.ChunkSize);
                        for (int f = 0; f < 6; f++)
                        {
                            List <MeshTool.BlockSurface> surface = MeshTool.getChunkSurface(chunk, blockTypeFun, f);
                            Texture2D texSurface = MeshTool.SurfacePointsToTexture(surface, f);

                            RenderTexture targetAoResult = rma.RenderByCalcShader(texSurface, new Vector3(i, j, k) * Const.ChunkSize, f);
                            //回读亮度数据
                            RenderTexture.active = targetAoResult;
                            Texture2D readback = new Texture2D(targetAoResult.width, targetAoResult.height);
                            readback.ReadPixels(new Rect(0, 0, targetAoResult.width, targetAoResult.width), 0, 0);
                            MeshTool.SetRaytraceAo(surface, readback);
                            Mesh mesh = MeshTool.createMesh2(surface, f, blockTypeFun);
                            if (mesh)
                            {
                                Vector3 pos = ((new Vector3(i, j, k))) * Const.ChunkSize;
                                m_meshes.Add(new MeshInfo(mesh, pos));
                            }
                        }
                    }
                }
            }
            DebugTool.Log("结束");
        }
    }
Esempio n. 11
0
 public static void Undo(TerrainTool tool)
 {
     m_undoRequest = true;
 }
Esempio n. 12
0
        protected override void OnButtonClicked(UIComponent comp)
        {
            if (ToolsModifierControl.toolController.m_mode == ItemClass.Availability.ThemeEditor)
            {
                int          zOrder       = comp.zOrder;
                TerrainTool  terrainTool  = null;
                ResourceTool resourceTool = null;
                var          panel        = (TerrainPanel)Convert.ChangeType(this, typeof(TerrainPanel));
                if (zOrder < kTools.Length + 1)
                {
                    terrainTool = ToolsModifierControl.SetTool <TerrainTool>();
                    if (terrainTool == null)
                    {
                        return;
                    }
                    ShowUndoTerrainOptionsPanel(panel, true);
                    UIView.library.Show("LandscapingInfoPanel");
                }
                else
                {
                    resourceTool = ToolsModifierControl.SetTool <ResourceTool>();
                    if (resourceTool == null)
                    {
                        return;
                    }
                    UIView.library.Hide("LandscapingInfoPanel");
                    ShowUndoTerrainOptionsPanel(panel, false);
                }
                ShowBrushOptionsPanel(panel, true);

                if (zOrder == 1 || zOrder == 3)
                {
                    ShowLevelHeightPanel(panel, true);
                }
                else
                {
                    ShowLevelHeightPanel(panel, false);
                }
                //begin mod
                if (zOrder < kTools.Length)
                {
                    terrainTool.m_mode        = kTools[zOrder].enumValue;
                    TerrainToolDetour.isDitch = false;
                }
                else
                {
                    if (zOrder < kTools.Length + 1)
                    {
                        terrainTool.m_mode        = TerrainTool.Mode.Shift;
                        TerrainToolDetour.isDitch = true;
                    }
                    else
                    {
                        resourceTool.m_resource = NaturalResourceManager.Resource.Sand;
                    }
                }
                //end mod
            }
            else
            {
                int         zOrder      = comp.zOrder;
                TerrainTool terrainTool = ToolsModifierControl.SetTool <TerrainTool>();
                if (terrainTool == null)
                {
                    return;
                }
                var panel = (TerrainPanel)Convert.ChangeType(this, typeof(TerrainPanel));
                ShowUndoTerrainOptionsPanel(panel, true);
                ShowBrushOptionsPanel(panel, true);
                //begin mod
                UIView.library.Show("LandscapingInfoPanel");
                //end mod
                if (zOrder == 1 || zOrder == 3)
                {
                    ShowLevelHeightPanel(panel, true);
                }
                else
                {
                    ShowLevelHeightPanel(panel, false);
                }
                //begin mod
                if (zOrder < kTools.Length)
                {
                    terrainTool.m_mode        = TerrainPanelDetour.kTools[zOrder].enumValue;
                    TerrainToolDetour.isDitch = false;
                }
                else
                {
                    terrainTool.m_mode        = TerrainTool.Mode.Shift;
                    TerrainToolDetour.isDitch = true;
                }
                //end mod
            }
        }
 public static void Undo(TerrainTool tool)
 {
     m_undoRequest = true;
 }
Esempio n. 14
0
    public void Init(Camera cam, EditorCommandManager commandManager)
    {
        //计算格子
        List <Vector3> tempLines = new List <Vector3>();

        for (int i = 0; i <= SpaceX * Const.ChunkSize; i++)
        {
            tempLines.Add(new Vector3(i, halfSpaceSize.y, 0) - halfSpaceSize);
            tempLines.Add(new Vector3(i, halfSpaceSize.y, SpaceY * Const.ChunkSize) - halfSpaceSize);
        }
        for (int j = 0; j <= SpaceY * Const.ChunkSize; j++)
        {
            tempLines.Add(new Vector3(0, halfSpaceSize.y, j) - halfSpaceSize);
            tempLines.Add(new Vector3(SpaceX * Const.ChunkSize, halfSpaceSize.y, j) - halfSpaceSize);
        }
        gridLines = tempLines.ToArray();
        //预处理光线
        rays = new Vector3[6][];
        {
            for (int i = 0; i < 6; i++)
            {
                rays[i] = TerrainTool.getRandomRays(9, i);
            }
        }
        DebugTool.Log("预处理光线追踪");

        camera = cam;
        this.commandManager = commandManager;
        blockManager        = new BlockManager();
        blockManager.create(SpaceX * Const.ChunkSize, SpaceY * Const.ChunkSize, SpaceZ * Const.ChunkSize, new BlockTypeFun());
        //blockManager.forEachChunk((BlockChunk chunk, int i, int j, int k) => {
        //    for (int x = 0; x < Const.ChunkSize; x++) {
        //        for (int y = 0; y < Const.ChunkSize; y++) {
        //            for (int z = 0; z < Const.ChunkSize; z++) {
        //                int gx = i * Const.ChunkSize + x;
        //                int gy = j * Const.ChunkSize + y;
        //                int gz = k * Const.ChunkSize + z;
        //                if ((new Vector3(gx, gy, gz) - new Vector3(SpaceX / 2.0f, SpaceY / 2.0f, SpaceZ / 2.0f) * Const.ChunkSize).magnitude < 10) {
        //                    chunk.setBlock(x, y, z, (int)BlockTypeEnum.Sand);
        //                }
        //            }
        //        }
        //    }
        //});

        rtm = new RayCastManager();
        rtm.create(blockManager.SizeX, blockManager.SizeY, blockManager.SizeZ);
        //rtm.moveTo(0, 0, 0, (int gx, int gy, int gz) => {
        //    return blockManager.getBlock(gx, gy, gz) != 0 ? RayCastBlockType.All : RayCastBlockType.Nothing;
        //});
        for (int x = 0; x < SpaceX * Const.ChunkSize; x++)
        {
            for (int y = 0; y < SpaceY * Const.ChunkSize; y++)
            {
                for (int z = 0; z < SpaceZ * Const.ChunkSize; z++)
                {
                    rtm.setBlock(x, y, z, blockManager.getBlock(x, y, z) != 0 ? RayCastBlockType.All : RayCastBlockType.Nothing);
                }
            }
        }

        bDirty = true;

        previewMesh = GLRender.createCubeMesh(0.5f, Color.white);
    }
Esempio n. 15
0
    public void Update()
    {
        if (bDirty)
        {
            bDirty = false;
            bNeedReselectPreview = true;
            DebugTool.Log("开始更新射线模块");
            rtm.clearAll();
            blockManager.forEachBlock((short block, int gx, int gy, int gz) => {
                if (block != (short)BlockTypeEnum.Air)
                {
                    rtm.setBlock(gx, gy, gz, RayCastBlockType.All);
                }
            });

            //初始化AO计算
            RayMarchingAo rma = new RayMarchingAo();
            rma.Init(rtm);

            foreach (var obj in objects)
            {
                GameObject.Destroy(obj);
            }
            objects.Clear();

            //m_meshes.Clear();
            DebugTool.Log("开始计算光照");
            {
                //计算光照
                Vector3 sunDir = new Vector3(1, -3, 1);
                sunDir.Normalize();
                TerrainTool.calcLight(blockManager, rtm, rays, sunDir);
            }

            BlockTypeFun blockTypeFun = new BlockTypeFun();

            //TerrainTool.calcLight2(blockManager, 16);
            DebugTool.Log("开始更新网格");
            for (int i = 0; i < SpaceX; i++)
            {
                for (int k = 0; k < SpaceZ; k++)
                {
                    for (int j = 0; j < SpaceY; j++)
                    {
                        BlockChunk chunk = new BlockChunk(blockManager, i * Const.ChunkSize, j * Const.ChunkSize, k * Const.ChunkSize);
                        for (int f = 0; f < 6; f++)
                        {
                            List <MeshTool.BlockSurface> surface = MeshTool.getChunkSurface(chunk, blockTypeFun, f);
                            Texture2D texSurface = MeshTool.SurfacePointsToTexture(surface, f);

                            Texture2D readback = null;
                            if (surface.Count > 0)
                            {
                                RenderTexture targetAoResult = rma.RenderByCalcShader(texSurface, new Vector3(i, j, k) * Const.ChunkSize, f);
                                //回读亮度数据
                                RenderTexture.active = targetAoResult;
                                readback             = new Texture2D(targetAoResult.width, targetAoResult.height);
                                readback.ReadPixels(new Rect(0, 0, targetAoResult.width, targetAoResult.width), 0, 0);
                                MeshTool.SetRaytraceAo(surface, readback);
                            }

                            Mesh mesh = MeshTool.createMesh2(surface, f, blockTypeFun);
                            if (mesh)
                            {
                                Vector3 pos = ((new Vector3(i, j, k))) * Const.ChunkSize - halfSpaceSize;
                                if (mesh != null)
                                {
                                    GameObject obj = new GameObject("Chunk", typeof(MeshRenderer), typeof(MeshFilter));
                                    obj.isStatic = true;
                                    obj.GetComponent <Renderer>().material = GlobalResources.getBlockMaterial();
                                    obj.GetComponent <MeshFilter>().mesh   = mesh;
                                    obj.transform.position = pos;
                                    objects.Add(obj);
                                }
                            }
                        }
                    }
                }
            }
            DebugTool.Log("结束");
        }
    }
Esempio n. 16
0
File: main.cs Progetto: wachel/block
    IEnumerator InitWorld()
    {
        Block.BlockManager bm = new Block.BlockManager();

        for (int i = 0; i < (int)Game.BlockType.Num; i++)
        {
            for (int f = 0; f < 6; f++)
            {
                Game.BlockType block = (Game.BlockType)i;
                texturePacker.AddTexture(block, f, TextureNameConfig.GetTextureName(block, f));
            }
        }
        texturePacker.Pack();
        packedTexture = texturePacker.GetPackedTexture();

        BlockTypeFun blockTypeFun = new BlockTypeFun();

        blockTypeFun.texturePacker = texturePacker;
        //float startTime = Time.realtimeSinceStartup;
        int WorldSizeX = 8;
        int WorldSizeY = 2;
        int WorldSizeZ = 8;

        //申请内存
        bm.create(WorldSizeX * Const.ChunkSize, WorldSizeY * Const.ChunkSize, WorldSizeZ * Const.ChunkSize, blockTypeFun);

        DebugTool.Log("申请内存");
        yield return(null);

        //
        TerrainTool.createTerrain(bm, WorldSizeX, WorldSizeY, WorldSizeZ);
        DebugTool.Log("创建地形");
        yield return(null);

        TerrainTool.createTree(bm, WorldSizeX, WorldSizeY, WorldSizeZ);
        DebugTool.Log("创建树");
        yield return(null);

        List <HouseItem> houseItems = new List <HouseItem>();

        TerrainTool.createBuildings(bm, ref houseItems);
        DebugTool.Log("创建建筑完成");
        yield return(null);

        //光线追踪初始化
        RayCastManager rtm = new RayCastManager();

        rtm.create(bm.SizeX, bm.SizeY, bm.SizeZ);
        for (int x = 0; x < rtm.getSizeX(); x++)
        {
            for (int z = 0; z < rtm.getSizeZ(); z++)
            {
                for (int y = 0; y < rtm.getSizeY(); y++)
                {
                    short            block = bm.getBlock(x, y, z);
                    RayCastBlockType rlt   = 0;
                    rlt |= blockTypeFun.isCollider(block) ? RayCastBlockType.Collider : RayCastBlockType.Nothing;
                    rlt |= blockTypeFun.isOpacity(block) ? RayCastBlockType.Opacity : RayCastBlockType.Nothing;
                    rtm.setBlock(x, y, z, rlt);
                }
                rtm.updateInSun(x, z);
            }
        }

        //预处理光线
        Vector3[][] rays = new Vector3[6][];
        {
            for (int i = 0; i < 6; i++)
            {
                rays[i] = TerrainTool.getRandomRays(9, i);
            }
        }
        DebugTool.Log("预处理光线追踪完成");
        yield return(null);

        rtm.updateAllLight();
        DebugTool.Log("扩散光照计算完毕");
        yield return(null);

        //int[] lightCount = new int[Const.MaxLightIteration + 1];
        //for (int i = 0; i < WorldSizeX * Const.ChunkSize; i++) {
        //    for (int k = 0; k < WorldSizeZ * Const.ChunkSize; k++) {
        //        for (int j = 0; j < WorldSizeY * Const.ChunkSize; j++) {
        //            lightCount[rtm.getLight(i, j, k)]++;
        //        }
        //    }
        //}
        //for (int i = 0; i < lightCount.Length; i++) {
        //    Debug.Log("light (" + i + ") = " + lightCount[i]);
        //}

        Texture2D lightMap = new Texture2D(256, 256, TextureFormat.RGBAHalf, false);

        Color[] colors = new Color[256 * 256];
        for (int i = 0; i < 256; i++)
        {
            for (int j = 0; j < 256; j++)
            {
                colors[j * 256 + i] = new Color(i / 256.0f, i / 256.0f, i / 256.0f, 1) * 2;
            }
        }
        lightMap.SetPixels(colors);
        lightMap.Apply();


        LightmapData[] lightmaps = new LightmapData[1];
        lightmaps[0]               = new LightmapData();
        lightmaps[0].lightmapFar   = lightMap;
        lightmaps[0].lightmapNear  = lightMap;
        LightmapSettings.lightmaps = lightmaps;

        GameObject itemsRoot = new GameObject("items");

        for (int i = 0; i < houseItems.Count; i++)
        {
            HouseItem  item   = houseItems[i];
            string     path   = HouseItemGenerator.GetItemPrefabPath(item.item);
            GameObject prefab = GlobalResources.loadPrefab(path);
            if (prefab != null)
            {
                GameObject inst = GameObject.Instantiate <GameObject>(prefab);
                inst.name               = houseItems[i].item.ToString();
                inst.transform.parent   = itemsRoot.transform;
                inst.transform.position = houseItems[i].pos + new Vector3(0.5f, 0f, 0.5f);
                MeshRenderer renderer = inst.GetComponentInChildren <MeshRenderer>();
                renderer.lightmapIndex = 0;
                inst.isStatic          = true;
                float light = rtm.getLight(item.pos.x, item.pos.y, item.pos.z) / (float)Const.MaxLightIteration;
                light = Mathf.Lerp(0.0f, 0.6f, light);
                renderer.lightmapScaleOffset = new Vector4(0, 0, light, 0f);
                //MeshFilter meshFilter = inst.GetComponentInChildren<MeshFilter>();
                //Mesh mesh = meshFilter.mesh;
                //Color32[] colors = new Color32[mesh.colors.Length];
                //byte c = (byte)(rtm.getLight(item.pos.x, item.pos.y, item.pos.z) * 255);
                //ArrayTool.SetAll(colors, new Color32(c,c,c,1));
                //mesh.colors32 = colors;
            }
            else
            {
                int a = 0;
            }
        }

        //初始化AO计算
        rma = new RayMarchingAo();
        rma.Init(rtm);

        //ViewVoxel viewVoxel = GameObject.FindObjectOfType<ViewVoxel>();
        //if (viewVoxel) {
        //    Texture3D voxelTex3D = rma.GetVoxelTexture();
        //    //OpenGLLibrary.glEnable(OpenGL.GL_TEXTURE_3D);
        //    //OpenGLLibrary.glBindTexture(OpenGL.GL_TEXTURE_3D, voxelTex3D.GetNativeTexturePtr());
        //    //byte[] data = new byte[voxelTex3D.width * voxelTex3D.height * voxelTex3D.depth * 4];
        //    //for (int i = 0; i < data.Length; i++ ){
        //    //    data[i] = 0x56;
        //    //}
        //    //OpenGL.TexSubImage3D(OpenGL.GL_TEXTURE_3D, 0, 0, 0, 0, 32, 32, 32, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, ref data);
        //    //OpenGL.TextureSubImage3D(voxelTex3D, 0, 0, 0, 0, 1, 1, 1, OpenGL.GL_RED, OpenGL.GL_UNSIGNED_BYTE, ref data);
        //    viewVoxel.SetVexelTex(voxelTex3D);
        //}

        //计算光照
        Vector3 sunDir = new Vector3(1, -3, 1);

        sunDir.Normalize();
        {
            //TerrainTool.calcLight(bm, rtm, rays, sunDir);
            //TerrainTool.calcLight2(bm,32);
        }
        DebugTool.Log("计算光照完成");
        yield return(null);

        Material mat = GlobalResources.getBlockMaterial();

        mat.mainTexture = texturePacker.GetPackedTexture();
        GameObject root = GameObject.Find("Root");

        if (root == null)
        {
            root = new GameObject("Root");
        }

        Level level = GameObject.FindObjectOfType <Level>();

        if (level)
        {
            level.scene = rtm;
        }

        //创建网格
        for (int i = 0; i < WorldSizeX; i++)
        {
            for (int k = 0; k < WorldSizeZ; k++)
            {
                for (int j = 0; j < WorldSizeY; j++)
                {
                    int baseX = i * Const.ChunkSize;
                    int baseY = j * Const.ChunkSize;
                    int baseZ = k * Const.ChunkSize;

                    BlockChunk chunk = new BlockChunk(bm, baseX, baseY, baseZ);

                    Vector3 chunkPos = new Vector3(i * Const.ChunkSize * Const.BlockSize, j * Const.ChunkSize * Const.BlockSize, k * Const.ChunkSize * Const.BlockSize);

                    //bool[,,] visibleBlocks = new bool[Const.ChunkSize,Const.ChunkSize,Const.ChunkSize];// = MeshTool.GetVisibleBlocks(chunk, blockTypeFun);
                    //TerrainTool.calcChunkLight(chunk, i, j, k, rtm, rays, sunDir);
                    //Mesh mesh = MeshTool.createMesh(chunk, blockTypeFun, 0, 0, 0);
                    for (int f = 0; f < 6; f++)
                    {
                        List <MeshTool.BlockSurface> surface = MeshTool.getChunkSurface(chunk, blockTypeFun, f);
                        //for (int s = 0; s < surface.Count; s++) {
                        //    visibleBlocks[surface[s].pos.x, surface[s].pos.y, surface[s].pos.z] = true;
                        //}


                        Texture2D texSurface = MeshTool.SurfacePointsToTexture(surface, f);

                        RenderTexture targetAoResult = rma.RenderByCalcShader(texSurface, new Vector3(i, j, k) * Const.ChunkSize, f);

                        //回读亮度数据
                        RenderTexture.active = targetAoResult;
                        Texture2D readback = new Texture2D(targetAoResult.width, targetAoResult.height);
                        readback.ReadPixels(new Rect(0, 0, targetAoResult.width, targetAoResult.width), 0, 0);
                        MeshTool.SetRaytraceAo(surface, readback);
                        MeshTool.SetExtendAo(surface, rtm, f, new VecInt3(i * Const.ChunkSize, j * Const.ChunkSize, k * Const.ChunkSize));

                        //将可行走区域标记黑色
                        //if (f == (int)BlockFaceIndex.BFI_y1) {
                        //    for (int s = 0; s < surface.Count; s++) {
                        //        int gx = baseX + surface[s].pos.x;
                        //        int gy = baseY + surface[s].pos.y;
                        //        int gz = baseZ + surface[s].pos.z;
                        //        if (rtm.testBlock(gx, gy, gz, (byte)RayTraceBlockType.Walkable)) {
                        //            for (int v = 0; v < 4; v++) {
                        //                surface[s].raytraceAo[v] *= 0.1f;
                        //            }
                        //        }
                        //    }
                        //}

                        Mesh mesh = MeshTool.createMesh2(surface, f, blockTypeFun);

                        if (mesh != null)
                        {
                            GameObject obj = new GameObject("Chunk", typeof(MeshRenderer), typeof(MeshFilter));
                            obj.isStatic = true;
                            obj.GetComponent <Renderer>().material = mat;
                            obj.GetComponent <MeshFilter>().mesh   = mesh;
                            obj.transform.SetParent(root.transform);
                            obj.transform.position = chunkPos;
                        }
                    }

                    //physics.AddChunk(visibleBlocks, chunkPos);
                    yield return(null);
                }
            }
        }
        StaticBatchingUtility.Combine(root);
        StaticBatchingUtility.Combine(itemsRoot);
        DebugTool.Log("生成网格");
        yield return(null);
    }
		private void CheckKeys()
		{
			if (GUIUtility.textFieldInput)
			{
				return;
			}
			for (int i = 0; i < TerrainInspector.s_ToolKeys.Length; i++)
			{
				if (TerrainInspector.s_ToolKeys[i].activated)
				{
					this.selectedTool = (TerrainTool)i;
					base.Repaint();
					Event.current.Use();
				}
			}
			if (TerrainInspector.s_PrevBrush.activated)
			{
				this.m_SelectedBrush--;
				if (this.m_SelectedBrush < 0)
				{
					this.m_SelectedBrush = TerrainInspector.s_BrushTextures.Length - 1;
				}
				base.Repaint();
				Event.current.Use();
			}
			if (TerrainInspector.s_NextBrush.activated)
			{
				this.m_SelectedBrush++;
				if (this.m_SelectedBrush >= TerrainInspector.s_BrushTextures.Length)
				{
					this.m_SelectedBrush = 0;
				}
				base.Repaint();
				Event.current.Use();
			}
			int num = 0;
			if (TerrainInspector.s_NextTexture.activated)
			{
				num = 1;
			}
			if (TerrainInspector.s_PrevTexture.activated)
			{
				num = -1;
			}
			if (num != 0)
			{
				switch (this.selectedTool)
				{
				case TerrainTool.PaintTexture:
					this.m_SelectedSplat = (int)Mathf.Repeat((float)(this.m_SelectedSplat + num), (float)this.m_Terrain.terrainData.splatPrototypes.Length);
					Event.current.Use();
					base.Repaint();
					break;
				case TerrainTool.PlaceTree:
					TreePainter.selectedTree = (int)Mathf.Repeat((float)(TreePainter.selectedTree + num), (float)this.m_TreeContents.Length);
					Event.current.Use();
					base.Repaint();
					break;
				case TerrainTool.PaintDetail:
					this.m_SelectedDetail = (int)Mathf.Repeat((float)(this.m_SelectedDetail + num), (float)this.m_Terrain.terrainData.detailPrototypes.Length);
					Event.current.Use();
					base.Repaint();
					break;
				}
			}
		}