// STATIC LOGIC ------------------------------------------------------------------------------------------------------------------- /// <summary> /// Create a terrain from its terrain data. If the level has a terrain editor, then an existing terrain will be recycled. /// </summary> public static GameObject CreateOrRecycleTerrain(TerrainData p_terrainData, int p_terrainLayer) { // try to find an existing terrain LE_GUI3dTerrain gui3dTerrain = Object.FindObjectOfType <LE_GUI3dTerrain>(); Terrain terrain = gui3dTerrain != null ? gui3dTerrain.TerrainInstance : null; GameObject terrainGO; if (terrain != null) { // an editable terrain was already existent -> recycle it terrainGO = terrain.gameObject; gui3dTerrain.RecycleTerrain(p_terrainData, true); } else { // there is no editable terrain in this level -> create new terrain GO terrainGO = Terrain.CreateTerrainGameObject(p_terrainData); // assign terrain for further editing if (gui3dTerrain != null) { gui3dTerrain.SetTerrain(terrainGO.GetComponent <Terrain>()); } } terrainGO.layer = p_terrainLayer; terrainGO.transform.position = new Vector3(-p_terrainData.size.x * 0.5f, terrainGO.transform.position.y, -p_terrainData.size.z * 0.5f); return(terrainGO); }
/// <summary> /// This will destroy the terrain instance used by the level editor. This function works only if the terrain editor exists in the scene. /// </summary> public static void DestroyOrResetTerrain() { // try to find an existing terrain LE_GUI3dTerrain gui3dTerrain = Object.FindObjectOfType <LE_GUI3dTerrain>(); if (gui3dTerrain != null) { // if an editable terrain is existent, then it must be reset or destroyed gui3dTerrain.ResetToDefaultOrDestroyTerrain(); } }
public LE_LogicLevel( LE_ConfigLevel p_confL, LE_GUI3dTerrain p_GUI3dTerrain, LE_GUI3dObject p_GUI3dObject, bool p_isObliqueFocusCentering, Texture2D[] p_configTextures, Vector2[] p_configTextureSizes, Vector2[] p_configTextureOffsets) { m_confL = p_confL; m_GUI3dTerrain = p_GUI3dTerrain; m_GUI3dObject = p_GUI3dObject; m_isObliqueFocusCentering = p_isObliqueFocusCentering; m_configTextures = p_configTextures; m_configTextureSizes = p_configTextureSizes; m_configTextureOffsets = p_configTextureOffsets; // register to events LE_GUIInterface.Instance.events.OnLevelSaveBtn += OnLevelSaveBtn; LE_GUIInterface.Instance.events.OnLevelLoadBtn += OnLevelLoadBtn; LE_GUIInterface.Instance.events.OnLevelRenderIconBtn += OnLevelRenderIconBtn; }
public LE_GenCmdTerrain(LE_GUI3dTerrain p_gui3d, LE_TerrainManager p_terrainMgr, Mode p_cmdMode) { #if IS_DELAY_LOD_SUPPORTED m_gui3d = p_gui3d; #endif m_terrainMgr = p_terrainMgr; m_cmdMode = p_cmdMode; if (m_cmdMode == Mode.HEIGHTS_CMD) { m_heightsBeforeChange = p_terrainMgr.TerrainData.GetHeights(0, 0, p_terrainMgr.TerrainData.heightmapWidth, p_terrainMgr.TerrainData.heightmapHeight); } else { m_alphamapsBeforeChange = p_terrainMgr.TerrainData.GetAlphamaps(0, 0, p_terrainMgr.TerrainData.alphamapWidth, p_terrainMgr.TerrainData.alphamapHeight); } m_lastEditedFrame = Time.frameCount; }
public LE_LogicTerrain(LE_ConfigTerrain p_confTerrain, LE_GUI3dTerrain p_GUI3dTerrain) { m_confT = p_confTerrain; m_GUI3dTerrain = p_GUI3dTerrain; // init 3d UI m_GUI3dTerrain.TERRAIN_LAYER = p_confTerrain.TerrainLayer; if (p_confTerrain.BrushProjector != null) { m_GUI3dTerrain.BrushProjector = p_confTerrain.BrushProjector; } else { Debug.LogError("LE_GUILogicTerrain: LE_ConfigTerrain.BrushProjector was not initialized! You need to set it in the inspector."); } if (p_confTerrain.Brushes.Length > 0) { m_GUI3dTerrain.BrushAlphaTexture = p_confTerrain.Brushes[0]; } else { Debug.LogError("LE_GUILogicTerrain: LE_ConfigTerrain.Brushes was not initialized! You need to set it in the inspector."); } // init further stuff CheckParameters(); InitTerrainValues(); // register to events LE_EventInterface.OnLoadedLevelInEditor += OnLoadedLevelInEditor; LE_GUIInterface.Instance.events.OnTerrainWidthChanged += OnTerrainWidthChanged; LE_GUIInterface.Instance.events.OnTerrainLengthChanged += OnTerrainLengthChanged; LE_GUIInterface.Instance.events.OnTerrainHeightChanged += OnTerrainHeightChanged; LE_GUIInterface.Instance.events.OnTerrainBaseTextureChanged += OnTerrainBaseTextureChanged; LE_GUIInterface.Instance.events.OnTerrainBrushChanged += OnTerrainBrushChanged; LE_GUIInterface.Instance.events.OnTerrainPaintTextureChanged += OnTerrainPaintTextureChanged; LE_GUIInterface.Instance.events.OnTerrainPaintTextureAdded += OnTerrainPaintTextureAdded; LE_GUIInterface.Instance.events.OnTerrainEditBrushSizeChanged += OnTerrainEditBrushSizeChanged; LE_GUIInterface.Instance.events.OnTerrainEditBrushAmountChanged += OnTerrainEditBrushAmountChanged; LE_GUIInterface.Instance.events.OnTerrainEditBrushTargetValueChanged += OnTerrainEditBrushTargetValueChanged; LE_GUIInterface.Instance.events.OnTerrainEditDirectionChanged += OnTerrainEditDirectionChanged; LE_GUIInterface.Instance.events.OnTerrainChangeHeightModeChanged += OnTerrainChangeHeightModeChanged; LE_GUIInterface.Instance.events.OnTerrainIsDirectedSmoothChanged += OnTerrainIsDirectedSmoothChanged; LE_GUIInterface.Instance.events.OnTerrainCreateBtn += OnTerrainCreateBtn; LE_GUIInterface.Instance.events.OnTerrainEditModeBtn += OnTerrainEditModeBtn; LE_GUIInterface.Instance.events.OnTerrainReadPaintHeightBtn += OnTerrainReadPaintHeightBtn; }
// some of the content in this method must be called in the first update and cannot be initialized in the start function private void Initialize_InFirstUpdate() { LE_ConfigLevel LEConfLevel = GetComponentInChildren <LE_ConfigLevel>(); if (LEConfLevel == null) { Debug.LogError("LE_LevelEditorMain: a LE_ConfigLevel component must be added to the game object with the LE_LevelEditorMain script!"); LEConfLevel = gameObject.AddComponent <LE_ConfigLevel>(); } Camera cam = Cam; LE_ConfigTerrain LEConfTerrain = GetComponentInChildren <LE_ConfigTerrain>(); // check further parameters that have been set in Start of other scripts if (LE_GUIInterface.Instance.delegates.IsCursorOverUI == null) { Debug.LogError("LE_LevelEditorMain: you have not setup LE_GUIInterface.delegates.IsCursorOverUI. Terrain might be edited behind UI while the user interacts with the UI, same is true for object placement!"); } // initialize terrain logic if (IS_TERRAIN_EDITOR) { // init 3d UI m_GUI3dTerrain = gameObject.AddComponent <LE_GUI3dTerrain>(); m_GUI3d.Add(m_GUI3dTerrain); // init default terrain InitializeDefaultTerrain(LEConfTerrain); // init logic m_logicTerrain = new LE_LogicTerrain(LEConfTerrain, m_GUI3dTerrain); m_logic.Add(m_logicTerrain); } else if (LEConfTerrain.CustomDefaultTerrain != null) { Debug.LogWarning("LE_LevelEditorMain: IS_TERRAIN_EDITOR is set to 'false', but you have provided a value for LE_ConfigTerrain.CustomDefaultTerrain! The value will be ignored!"); } // initialize level logic m_logicLevel = new LE_LogicLevel( LEConfLevel, m_GUI3dTerrain, m_GUI3dObject, IS_OBLIQUE_FOCUS_CENTERING, LEConfTerrain.TerrainTextureConfig.TERRAIN_TEXTURES, LEConfTerrain.TerrainTextureConfig.TERRAIN_TEXTURE_SIZES, LEConfTerrain.TerrainTextureConfig.TERRAIN_TEXTURE_OFFSETS); m_logic.Add(m_logicLevel); // initialize object logic if (IS_OBJECT_EDITOR) { m_logic.Add(new LE_LogicObjects(m_GUI3dObject, ROOT_OBJECT_MAP)); } // initialize camera gizmo if (cam != null && IS_WITH_CAMERA_PERSPECTIVE_GIZMO) { if (LE_GUIInterface.Instance.delegates.GetCameraPerspectiveGizmoRightPixelOffset != null) { // calculate the screen rect of the camera perspective gizmo (used later for mouse over UI calculation) const float relativeSize = 0.2f; float rectW = relativeSize * (float)Screen.width / cam.aspect; float rectH = relativeSize * (float)Screen.height; m_perspectiveGizmoRect = new Rect(Screen.width - LE_GUIInterface.Instance.delegates.GetCameraPerspectiveGizmoRightPixelOffset() - rectW, 0f, rectW, rectH); if (string.IsNullOrEmpty(LayerMask.LayerToName(CAMERA_PERSPECTIVE_GIZMO_LAYER))) { Debug.LogWarning("LE_LevelEditorMain: Inspector property 'CAMERA_PERSPECTIVE_GIZMO_LAYER' is set to '" + CAMERA_PERSPECTIVE_GIZMO_LAYER + "', but this layer has no name in 'Tags & Layers' set. Please set the name of this layer to 'CameraPerspectiveGizmo' or change the value of the 'CAMERA_PERSPECTIVE_GIZMO_LAYER' property! To open the 'Tags & Layers' manager select any game object, in the inspector click on the layer drop down at top right then click on 'Add Layer...'."); } else if (LayerMask.LayerToName(CAMERA_PERSPECTIVE_GIZMO_LAYER) != "CameraPerspectiveGizmo") { Debug.LogWarning("LE_LevelEditorMain: Inspector property 'CAMERA_PERSPECTIVE_GIZMO_LAYER' is set to '" + CAMERA_PERSPECTIVE_GIZMO_LAYER + "', but the name of this layer is '" + LayerMask.LayerToName(CAMERA_PERSPECTIVE_GIZMO_LAYER) + "'. Is this intended? If not please set the name of this layer to 'CameraPerspectiveGizmo' or change the value of the 'CAMERA_PERSPECTIVE_GIZMO_LAYER' property! To open the 'Tags & Layers' manager select any game object, in the inspector click on the layer drop down at top right then click on 'Add Layer...'."); } // create and setup the camera perspective gizmo m_cameraPerspectiveGizmo = CPG_CameraPerspectiveGizmo.Create(cam, CAMERA_PERSPECTIVE_GIZMO_LAYER); if (m_cameraPerspectiveGizmo != null) { // move the camera perspective gizmo far away (it will not be rendered anyway, but this is needed to prevent collision) m_cameraPerspectiveGizmo.transform.position = Vector3.down * -10000f; // set ortho offset of the gizmo m_cameraPerspectiveGizmo.OrthoOffset = cam.farClipPlane * 0.2f; // make sure that the main camera will not render the gizmo cam.cullingMask = cam.cullingMask & ~(1 << CAMERA_PERSPECTIVE_GIZMO_LAYER); // set position of the gizmo m_cameraPerspectiveGizmo.RelativeScreenSize = relativeSize; float rightMenuOffset = LE_GUIInterface.Instance.delegates.GetCameraPerspectiveGizmoRightPixelOffset() / (float)Screen.width; m_cameraPerspectiveGizmo.RelativeScreenPos = new Vector2(1f - relativeSize * 0.5f / cam.aspect - rightMenuOffset, 1f - relativeSize * 0.5f); // register for events of the camera perspective gizmo m_cameraPerspectiveGizmo.m_onBeforeSwitchToOrthographic += OnCameraPerspectiveSwitchToOrthographic; m_cameraPerspectiveGizmo.m_onAfterSwitchToPerspective += OnCameraPerspectiveSwitchToPerspective; } } else { Debug.LogError("LE_LevelEditorMain: LE_GUIInterface.delegates.GetCameraPerspectiveGizmoRightPixelOffset was not set from the UI scripts! The camera perspective gizmo cannot be placed to the right screen position! To set this delegate use 'LE_GUIInterface.Instance.delegates.GetCameraPerspectiveGizmoRightPixelOffset = ...' in one of the Start functions of your scripts!"); } } // apply an oblique camera projection to bring focused objects to the middle of the visible screen // instead of being in the middle of the rendered screen, which means close to the right menu UI if (IS_OBLIQUE_FOCUS_CENTERING && !SetObliqueFocusProjectionMatrix(true)) { Debug.LogError("LE_LevelEditorMain: IS_OBLIQUE_FOCUS_CENTERING is true, but you have not provided the LE_GUIInterface.delegates.GetObliqueCameraPerspectiveRightPixelOffset delegate! Provide it to bring focused objects in the center of the visible (not covered by UI) screen area!"); } }