public TransformGizmo(GraphicsDevice graphicsDevice, ContentManager contentManager, Scene scene, Entity camera, Prefab prefab) { this.graphicsDevice = graphicsDevice ?? throw new ArgumentNullException(nameof(graphicsDevice)); this.scene = scene ?? throw new ArgumentNullException(nameof(scene)); Camera = camera ?? throw new ArgumentNullException(nameof(camera)); List <Entity> entities = prefab.Instantiate(); if (entities.Count == 1) { gizmoRoot = entities[0]; } else { throw new Exception("There are more than 1 root entity of gizmo"); } // build gizmo modes dict gizmoModesDict = new Dictionary <Entity, GizmoModes>(); gizmoModesDict.Add(gizmoRoot.FindChild("TranslationX"), GizmoModes.TranslationX); gizmoModesDict.Add(gizmoRoot.FindChild("TranslationY"), GizmoModes.TranslationY); gizmoModesDict.Add(gizmoRoot.FindChild("TranslationZ"), GizmoModes.TranslationZ); gizmoModesDict.Add(gizmoRoot.FindChild("TranslationXZ"), GizmoModes.TranslationPlaneXZ); gizmoModesDict.Add(gizmoRoot.FindChild("TranslationYZ"), GizmoModes.TranslationPlaneYZ); gizmoModesDict.Add(gizmoRoot.FindChild("TranslationXY"), GizmoModes.TranslationPlaneXY); gizmoModesDict.Add(gizmoRoot.FindChild("TranslationCC"), GizmoModes.TranslationPlaneCamera); gizmoModesDict.Add(gizmoRoot.FindChild("RotationX"), GizmoModes.RotationX); gizmoModesDict.Add(gizmoRoot.FindChild("RotationY"), GizmoModes.RotationY); gizmoModesDict.Add(gizmoRoot.FindChild("RotationZ"), GizmoModes.RotationZ); gizmoModesDict.Add(gizmoRoot.FindChild("RotationC"), GizmoModes.RotationCamera); // build gizmo entities dict (opposite to gizmoModesDict) gizmoEntitiesDict = new Dictionary <GizmoModes, Entity>(); foreach (Entity k in gizmoModesDict.Keys) { gizmoEntitiesDict.Add(gizmoModesDict[k], k); } // set materials SetEntityMaterial("TranslationX", ColorAxisX); SetEntityMaterial("TranslationYZ", ColorAxisX); SetEntityMaterial("RotationX", ColorAxisX); SetEntityMaterial("TranslationY", ColorAxisY); SetEntityMaterial("TranslationXZ", ColorAxisY); SetEntityMaterial("RotationY", ColorAxisY); SetEntityMaterial("TranslationZ", ColorAxisZ); SetEntityMaterial("TranslationXY", ColorAxisZ); SetEntityMaterial("RotationZ", ColorAxisZ); SetEntityMaterial("TranslationCC", ColorAxisC); SetEntityMaterial("RotationC", ColorAxisC); // initialize mode = GizmoModes.None; SetGizmoMode(GizmoModes.None); }
private void SetGizmoMode(GizmoModes mode) { // reset highlight foreach (Entity gizmoEntity in gizmoModesDict.Keys) { Material material = gizmoEntity.Get <ModelComponent>().Materials[0]; material.Passes[0].Parameters.Set(MaterialKeys.EmissiveIntensity, 0.1f); } // set highlight if (mode != GizmoModes.None) { gizmoEntitiesDict[mode].Get <ModelComponent>().Materials[0].Passes[0].Parameters.Set(MaterialKeys.EmissiveIntensity, 1.0f); } }
/// <summary> /// Main Logic for the editor part /// </summary> /// <param name="gameTime"></param> /// <param name="entities"></param> /// <param name="data"></param> public void Update(GameTime gameTime, List <BasicEntity> entities, List <Decal> decals, List <PointLight> pointLights, List <DirectionalLight> dirLights, EnvironmentSample envSample, List <DebugEntity> debugEntities, EditorReceivedData data, MeshMaterialLibrary meshMaterialLibrary) { if (!GameSettings.e_enableeditor) { return; } if (!DebugScreen.ConsoleOpen) { if (Input.WasKeyPressed(Keys.R)) { GameStats.e_gizmoMode = GizmoModes.Rotation; } if (Input.WasKeyPressed(Keys.T)) { GameStats.e_gizmoMode = GizmoModes.Translation; } if (Input.WasKeyPressed(Keys.Z)) { GameStats.e_gizmoMode = GizmoModes.Scale; } } _gizmoMode = GameStats.e_gizmoMode; int hoveredId = data.HoveredId; if (_gizmoTransformationMode) { if (Input.mouseState.LeftButton == ButtonState.Pressed) { GizmoControl(_gizmoId, data); } else { _gizmoTransformationMode = false; } } else if (Input.WasLMBClicked() && !GUIControl.UIWasUsed) { previousMouseX = Input.mouseState.X; previousMouseY = Input.mouseState.Y; //Gizmos if (hoveredId >= 1 && hoveredId <= 3) { _gizmoId = hoveredId; GizmoControl(_gizmoId, data); return; } if (hoveredId <= 0) { SelectedObject = null; return; } bool foundnew = false; //Get the selected entity! for (int index = 0; index < entities.Count; index++) { var VARIABLE = entities[index]; if (VARIABLE.Id == hoveredId) { SelectedObject = VARIABLE; foundnew = true; break; } } if (foundnew == false) { for (int index = 0; index < decals.Count; index++) { Decal decal = decals[index]; if (decal.Id == hoveredId) { SelectedObject = decal; break; } } for (int index = 0; index < pointLights.Count; index++) { PointLight pointLight = pointLights[index]; if (pointLight.Id == hoveredId) { SelectedObject = pointLight; break; } } for (int index = 0; index < dirLights.Count; index++) { DirectionalLight directionalLight = dirLights[index]; if (directionalLight.Id == hoveredId) { SelectedObject = directionalLight; break; } } { if (envSample.Id == hoveredId) { SelectedObject = envSample; } } for (int index = 0; index < debugEntities.Count; index++) { DirectionalLight debugEntity = dirLights[index]; if (debugEntity.Id == hoveredId) { SelectedObject = debugEntity; break; } } } } //Controls if (Input.WasKeyPressed(Keys.Delete)) { //Find object if (SelectedObject is BasicEntity) { entities.Remove((BasicEntity)SelectedObject); meshMaterialLibrary.DeleteFromRegistry((BasicEntity)SelectedObject); SelectedObject = null; } else if (SelectedObject is Decal) { decals.Remove((Decal)SelectedObject); SelectedObject = null; } else if (SelectedObject is PointLight) { pointLights.Remove((PointLight)SelectedObject); SelectedObject = null; } else if (SelectedObject is DirectionalLight) { dirLights.Remove((DirectionalLight)SelectedObject); SelectedObject = null; } } if (Input.WasKeyPressed(Keys.Insert) || (Input.keyboardState.IsKeyDown(Keys.LeftControl) && Input.WasKeyPressed(Keys.C))) { if (SelectedObject is BasicEntity) { BasicEntity copy = (BasicEntity)SelectedObject.Clone; copy.RegisterInLibrary(meshMaterialLibrary); entities.Add(copy); } else if (SelectedObject is Decal) { Decal copy = (Decal)SelectedObject.Clone; decals.Add(copy); } else if (SelectedObject is PointLight) { PointLight copy = (PointLight)SelectedObject.Clone; pointLights.Add(copy); } else if (SelectedObject is DirectionalLight) { DirectionalLight copy = (DirectionalLight)SelectedObject.Clone; dirLights.Add(copy); } } }
public Vector3 GetTransformAxis(GizmoModes mode) { Vector3 axis; switch (mode) { case GizmoModes.None: axis = Vector3.Zero; break; case GizmoModes.TranslationX: axis = Vector3.UnitX; break; case GizmoModes.TranslationY: axis = Vector3.UnitY; break; case GizmoModes.TranslationZ: axis = Vector3.UnitZ; break; case GizmoModes.TranslationPlaneXY: axis = new Vector3(1.0f, 1.0f, 0.0f); break; case GizmoModes.TranslationPlaneYZ: axis = new Vector3(0.0f, 1.0f, 1.0f); break; case GizmoModes.TranslationPlaneXZ: axis = new Vector3(1.0f, 0.0f, 1.0f); break; case GizmoModes.TranslationPlaneCamera: axis = new Vector3(1.0f, 1.0f, 1.0f); break; case GizmoModes.RotationX: axis = Vector3.UnitX; break; case GizmoModes.RotationY: axis = Vector3.UnitY; break; case GizmoModes.RotationZ: axis = Vector3.UnitZ; break; case GizmoModes.RotationCamera: axis = Camera.Transform.GetWorldPosition() - Root.Transform.GetWorldPosition(); axis.Normalize(); break; default: throw new NotImplementedException(); } if (mode != GizmoModes.TranslationPlaneCamera && mode != GizmoModes.RotationCamera) { Quaternion rotation = Root.Transform.GetWorldTransformation().Rotation; rotation.Rotate(ref axis); } return(axis); }
/// <summary> /// Main Logic for the editor part /// </summary> /// <param name="gameTime"></param> /// <param name="entities"></param> /// <param name="data"></param> public void Update(GameTime gameTime, List <BasicEntity> entities, List <PointLightSource> pointLights, List <DirectionalLightSource> dirLights, EditorReceivedData data, MeshMaterialLibrary meshMaterialLibrary) { if (Input.WasKeyPressed(Keys.R)) { _gizmoMode = GizmoModes.Rotation; } if (Input.WasKeyPressed(Keys.T)) { _gizmoMode = GizmoModes.Translation; } int hoveredId = data.HoveredId; if (_gizmoTransformationMode) { if (Input.mouseState.LeftButton == ButtonState.Pressed) { GizmoControl(_gizmoId, data); } else { _gizmoTransformationMode = false; } } else if (Input.WasLMBPressed()) { //Gizmos if (hoveredId >= 1 && hoveredId <= 3) { _gizmoId = hoveredId; GizmoControl(_gizmoId, data); return; } if (hoveredId <= 0) { SelectedObject = null; return; } bool foundnew = false; //Get the selected entity! for (int index = 0; index < entities.Count; index++) { var VARIABLE = entities[index]; if (VARIABLE.Id == hoveredId) { SelectedObject = VARIABLE; foundnew = true; break; } } if (foundnew == false) { for (int index = 0; index < pointLights.Count; index++) { PointLightSource pointLightSource = pointLights[index]; if (pointLightSource.Id == hoveredId) { SelectedObject = pointLightSource; break; } } for (int index = 0; index < dirLights.Count; index++) { DirectionalLightSource directionalLightSource = dirLights[index]; if (directionalLightSource.Id == hoveredId) { SelectedObject = directionalLightSource; break; } } } } //Controls if (Input.WasKeyPressed(Keys.Delete)) { //Find object if (SelectedObject is BasicEntity) { entities.Remove((BasicEntity)SelectedObject); meshMaterialLibrary.DeleteFromRegistry((BasicEntity)SelectedObject); SelectedObject = null; } else if (SelectedObject is PointLightSource) { pointLights.Remove((PointLightSource)SelectedObject); SelectedObject = null; } else if (SelectedObject is DirectionalLightSource) { dirLights.Remove((DirectionalLightSource)SelectedObject); SelectedObject = null; } } if (Input.WasKeyPressed(Keys.Insert)) { if (SelectedObject is BasicEntity) { BasicEntity copy = (BasicEntity)SelectedObject.Clone; copy.RegisterInLibrary(meshMaterialLibrary); entities.Add(copy); } else if (SelectedObject is PointLightSource) { PointLightSource copy = (PointLightSource)SelectedObject.Clone; pointLights.Add(copy); } else if (SelectedObject is DirectionalLightSource) { DirectionalLightSource copy = (DirectionalLightSource)SelectedObject.Clone; dirLights.Add(copy); } } if (SelectedObject != null) { DebugScreen.AddString(SelectedObject.Position.ToString()); } }