// configure gizmo - constructs necessary ITransformWrapper to provide // desired gizmo behavior. You can modify behavior in subclasses by // overriding InitializeTransformWrapper (but you must void SetActiveFrame(FrameType eFrame) { // disconect existing wrapper if (targetWrapper != null) { targetWrapper.Target.OnTransformModified -= onTransformModified; } // if we have multiple targets, we construct a transient SO to // act as a parent (stored as internalGroupSO) if (targets.Count > 1 && internalGroupSO == null) { internalGroupSO = new TransientGroupSO(); internalGroupSO.Create(); parentScene.AddSceneObject(internalGroupSO); internalGroupSO.AddChildren(targets); } SceneObject useSO = (targets.Count == 1) ? targets[0] : internalGroupSO; // construct the wrapper targetWrapper = InitializeTransformWrapper(useSO, eFrame); //connect up to it targetWrapper.Target.OnTransformModified += onTransformModified; onTransformModified(null); // configure gizmo update_active(); }
public static GOWrapperSO CombineAnySOs(SceneObject s1, SceneObject s2, bool bDeleteExisting = true) { FScene scene = s1.GetScene(); if (scene.IsSelected(s1)) { scene.Deselect(s1); } if (scene.IsSelected(s2)) { scene.Deselect(s2); } fGameObject parentGO = GameObjectFactory.CreateParentGO("combined"); GOWrapperSO.AppendSOGeometry(parentGO, s1, true); GOWrapperSO.AppendSOGeometry(parentGO, s2, true); GOWrapperSO wrapperSO = new GOWrapperSO() { AllowMaterialChanges = false }; wrapperSO.Create(parentGO); if (bDeleteExisting) { scene.RemoveSceneObject(s1, false); scene.RemoveSceneObject(s2, false); } scene.AddSceneObject(wrapperSO, false); return(wrapperSO); }
public override OpStatus Apply() { if (scene.HasDeletedSceneObject(so)) { scene.RestoreDeletedSceneObject(so); } else { scene.AddSceneObject(so, bKeepWorldPosition); } return(OpStatus.Success); }
public override OpStatus Apply() { if (created_group == null) { created_group = new GroupSO(); created_group.Create(); Scene.AddSceneObject(created_group, false); } else { Scene.RestoreDeletedSceneObject(created_group); } created_group.AddChildren(Objects); return(OpStatus.Success); }
void Restore_OnEndSceneObject() { if (eState != RestoreState.InSceneObject) { throw new FormatException("[Serializer] not in correct state for EndSceneObject"); } SceneObject so = SOFactory.Build(activeScene, this, CurAttribs); if (so != null) { activeScene.AddSceneObject(so); } PopAttribSet(); PopState(); }
public static GroupSO CreateGroupSO(TransformableSO so1, TransformableSO so2) { FScene scene = so1.GetScene(); if (scene.IsSelected(so1)) { scene.Deselect(so1); } if (scene.IsSelected(so2)) { scene.Deselect(so2); } GroupSO group = new GroupSO(); group.Create(); scene.AddSceneObject(group); group.AddChild(so1); group.AddChild(so2); return(group); }
// extracts MeshFilter object from input GameObject and passes it to a custom constructor // function MakeSOFunc (if null, creates basic MeshSO). Then optionally adds to Scene, // preserving existing 3D position if desired (default true) public static SceneObject ImportExistingUnityMesh(GameObject go, FScene scene, bool bAddToScene = true, bool bKeepWorldPosition = true, bool bRecenterFrame = true, Func <Mesh, SOMaterial, SceneObject> MakeSOFunc = null) { MeshFilter meshF = go.GetComponent <MeshFilter>(); if (meshF == null) { throw new Exception("SceneUtil.ImportExistingUnityMesh: gameObject is not a mesh!!"); } Vector3f scale = go.GetLocalScale(); // [RMS] why don't we bake transform into mesh ??! then we could handle non-uniform scaling... if (SceneTransforms.IsUniformScale(scale) == false) { throw new Exception("UnitySceneUtil.ImportExistingUnityMesh: nonuniform scaling is not supported..."); } Mesh useMesh = meshF.mesh; // makes a copy AxisAlignedBox3f bounds = useMesh.bounds; // bounds.Center is wrt local frame of input go // ie offset from origin in local coordinates // if we want to move frame to center of mesh, we have to re-center it at origin // in local coordinates if (bRecenterFrame) { UnityUtil.TranslateMesh(useMesh, -bounds.Center.x, -bounds.Center.y, -bounds.Center.z); useMesh.RecalculateBounds(); } SceneObject newSO = (MakeSOFunc != null) ? MakeSOFunc(useMesh, scene.DefaultMeshSOMaterial) : new MeshSO().Create(useMesh, scene.DefaultMeshSOMaterial); if (bAddToScene) { scene.AddSceneObject(newSO, false); } if (bKeepWorldPosition) { // compute world rotation/location. If we re-centered the mesh, we need // to offset by the transform we applied above in local coordinates // (hence we have to rotate & scale) if (go.transform.parent != null) { throw new Exception("UnitySceneUtil.ImportExistingUnityMesh: Not handling case where GO has a parent transform"); } Frame3f goFrameW = UnityUtil.GetGameObjectFrame(go, CoordSpace.WorldCoords); Vector3f originW = goFrameW.Origin; if (bRecenterFrame) { originW += goFrameW.Rotation * (scale * bounds.Center); // offset initial frame to be at center of mesh } // convert world frame and offset to scene coordinates Frame3f goFrameS = scene.ToSceneFrame(goFrameW); Vector3f boundsCenterS = scene.ToSceneP(originW); // translate new object to position in scene Frame3f curF = newSO.GetLocalFrame(CoordSpace.SceneCoords); curF.Origin += boundsCenterS; newSO.SetLocalFrame(curF, CoordSpace.SceneCoords); // apply rotation (around current origin) curF = newSO.GetLocalFrame(CoordSpace.SceneCoords); curF.RotateAround(curF.Origin, goFrameS.Rotation); newSO.SetLocalFrame(curF, CoordSpace.SceneCoords); // apply local scale newSO.SetLocalScale(scale); } return(newSO); }
// extracts all MeshFilter objects from input GameObject and appends them, then passes to // function MakeSOFunc (if null, creates basic MeshSO). Then optionally adds to Scene, // preserving existing 3D position if desired (default true) public static SceneObject ImportExistingUnityGO(GameObject go, FScene scene, bool bAddToScene = true, bool bKeepWorldPosition = true, bool bRecenterFrame = true, Func <DMesh3, SOMaterial, SceneObject> MakeSOFunc = null) { List <MeshFilter> filters = new List <MeshFilter>(); List <GameObject> children = new List <GameObject>() { go }; UnityUtil.CollectAllChildren(go, children); foreach (var cgo in children) { if (cgo.GetComponent <MeshFilter>() != null) { filters.Add(cgo.GetComponent <MeshFilter>()); } } if (filters.Count == 0) { throw new Exception("SceneUtil.ImportExistingUnityGO: no meshes!!"); } DMesh3 CombineMesh = new DMesh3(MeshComponents.VertexNormals | MeshComponents.VertexColors); MeshEditor editor = new MeshEditor(CombineMesh); int gid = 0; foreach (MeshFilter mesh in filters) { fMesh uMesh = new fMesh(mesh.sharedMesh); using (var imesh = uMesh.CreateCachedIMesh()) { editor.AppendMesh(imesh, ++gid); } } Vector3f scale = go.GetLocalScale(); AxisAlignedBox3d bounds = CombineMesh.CachedBounds; // bounds.Center is wrt local frame of input go // ie offset from origin in local coordinates // if we want to move frame to center of mesh, we have to re-center it at origin // in local coordinates if (bRecenterFrame) { MeshTransforms.Translate(CombineMesh, -bounds.Center.x, -bounds.Center.y, -bounds.Center.z); } SceneObject newSO = (MakeSOFunc != null) ? MakeSOFunc(CombineMesh, scene.DefaultMeshSOMaterial) : new DMeshSO().Create(CombineMesh, scene.DefaultMeshSOMaterial); newSO.Name = go.name; if (bAddToScene) { scene.AddSceneObject(newSO, false); } if (bKeepWorldPosition) { // compute world rotation/location. If we re-centered the mesh, we need // to offset by the transform we applied above in local coordinates // (hence we have to rotate & scale) if (go.transform.parent != null) { throw new Exception("UnitySceneUtil.ImportExistingUnityGO: Not handling case where GO has a parent transform"); } Frame3f goFrameW = UnityUtil.GetGameObjectFrame(go, CoordSpace.WorldCoords); Vector3f originW = goFrameW.Origin; if (bRecenterFrame) { originW += goFrameW.Rotation * (scale * (Vector3f)bounds.Center); // offset initial frame to be at center of mesh } // convert world frame and offset to scene coordinates Frame3f goFrameS = scene.ToSceneFrame(goFrameW); Vector3f boundsCenterS = scene.ToSceneP(originW); // translate new object to position in scene Frame3f curF = newSO.GetLocalFrame(CoordSpace.SceneCoords); curF.Origin += boundsCenterS; newSO.SetLocalFrame(curF, CoordSpace.SceneCoords); // apply rotation (around current origin) curF = newSO.GetLocalFrame(CoordSpace.SceneCoords); curF.RotateAround(curF.Origin, goFrameS.Rotation); newSO.SetLocalFrame(curF, CoordSpace.SceneCoords); // apply local scale newSO.SetLocalScale(scale); } return(newSO); }
public static GOWrapperSO CombineAnySOs(SceneObject s1, SceneObject s2, bool bDeleteExisting = true) { FScene scene = s1.GetScene(); if (scene.IsSelected(s1)) { scene.Deselect(s1); } if (scene.IsSelected(s2)) { scene.Deselect(s2); } fGameObject parentGO = GameObjectFactory.CreateParentGO("combined"); fGameObject copy1 = GameObjectFactory.Duplicate(s1.RootGameObject); fGameObject copy2 = GameObjectFactory.Duplicate(s2.RootGameObject); // if inputs are DMeshSOs, they do not have colliders, which we will need... if (s1 is DMeshSO) { foreach (var go in copy1.Children()) { if (go.GetComponent <MeshFilter>() != null && go.GetComponent <MeshCollider>() == null) { go.AddComponent <MeshCollider>(); } } } if (s2 is DMeshSO) { foreach (var go in copy2.Children()) { if (go.GetComponent <MeshFilter>() != null && go.GetComponent <MeshCollider>() == null) { go.AddComponent <MeshCollider>(); } } } parentGO.AddChild(copy1, true); parentGO.AddChild(copy2, true); GOWrapperSO wrapperSO = new GOWrapperSO() { AllowMaterialChanges = false }; wrapperSO.Create(parentGO); if (bDeleteExisting) { scene.RemoveSceneObject(s1, false); scene.RemoveSceneObject(s2, false); } scene.AddSceneObject(wrapperSO, false); return(wrapperSO); }