Beispiel #1
0
 public TransformGizmoChange change;        // [TODO] shouldn't be using gizmo change for this?
 public GrabInfo(Cockpit cockpit, TransformableSO so, Frame3f handF)
 {
     this.cockpit      = cockpit;
     this.so           = so;
     this.startHandF   = handF;
     this.startObjFW   = so.GetLocalFrame(CoordSpace.WorldCoords);
     this.startObjRelF = this.startHandF.ToFrame(this.startObjFW);
     this.stickDelta   = Vector2f.Zero;
     change            = new TransformGizmoChange()
     {
         parentSO          = new WeakReference(so),
         parentBefore      = so.GetLocalFrame(CoordSpace.SceneCoords),
         parentScaleBefore = so.GetLocalScale()
     };
 }
Beispiel #2
0
        public static void TranslateInFrame(TransformableSO so, Vector3f translate, CoordSpace eSpace = CoordSpace.ObjectCoords)
        {
            Frame3f f = so.GetLocalFrame(eSpace);

            f.Origin += translate;
            so.SetLocalFrame(f, eSpace);
        }
        /// <summary>
        /// input objectF is in Object (local) coords of so, apply all intermediate
        /// transforms to get it to Scene coords
        /// </summary>
        public static Frame3f ObjectToScene(TransformableSO so, Frame3f objectF)
        {
            Frame3f         sceneF = objectF;
            TransformableSO curSO  = so;

            while (curSO != null)
            {
                Frame3f  curF  = curSO.GetLocalFrame(CoordSpace.ObjectCoords);
                Vector3f scale = curSO.GetLocalScale();
                Util.gDevAssert(IsUniformScale(scale));
                sceneF.Scale(scale);
                sceneF = curF.FromFrame(sceneF);
                SOParent parent = curSO.Parent;
                if (parent is FScene)
                {
                    return(sceneF);
                }
                curSO = (parent as TransformableSO);
            }
            if (curSO == null)
            {
                DebugUtil.Error("SceneTransforms.TransformTo: found null parent SO!");
            }
            return(sceneF);
        }
Beispiel #4
0
        void update_source()
        {
            TransformableSO from   = Source as TransformableSO;
            Frame3f         FrameS = from.GetLocalFrame(CoordSpace.SceneCoords);
            TransformableSO to     = Target as TransformableSO;

            relativeF = SceneTransforms.SceneToObject(to, FrameS);
        }
Beispiel #5
0
        public static void EmitTransform(this SceneSerializer s, IOutputStream o, TransformableSO so)
        {
            o.BeginStruct(IOStrings.TransformStruct);
            Frame3f f = so.GetLocalFrame(CoordSpace.ObjectCoords);

            o.AddAttribute(IOStrings.APosition, f.Origin);
            o.AddAttribute(IOStrings.AOrientation, f.Rotation);
            o.AddAttribute(IOStrings.AScale, so.RootGameObject.transform.localScale);
            o.EndStruct();
        }
Beispiel #6
0
        public void Connect(TransformableSO source, TransformableSO target)
        {
            //this.source = source;
            this.target = target;
            Frame3f sourceW = source.GetLocalFrame(CoordSpace.WorldCoords);

            this.SetLocalFrame(sourceW, CoordSpace.WorldCoords);

            target.RootGameObject.transform.SetParent(gameObject.transform, true);

            increment_timestamp();
        }
        // 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 TransformableSO ImportExistingUnityMesh(GameObject go, FScene scene,
                                                              bool bAddToScene = true, bool bKeepWorldPosition = true, bool bRecenterFrame = true,
                                                              Func <Mesh, SOMaterial, TransformableSO> MakeSOFunc = null)
        {
            MeshFilter meshF = go.GetComponent <MeshFilter>();

            if (meshF == null)
            {
                throw new Exception("SceneUtil.ImportExistingUnityMesh: gameObject is not a mesh!!");
            }

            Vector3f scale = go.GetLocalScale();

            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();
            }

            TransformableSO 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 TransformableSO ImportExistingUnityGO(GameObject go, FScene scene,
                                                            bool bAddToScene = true, bool bKeepWorldPosition = true, bool bRecenterFrame = true,
                                                            Func <DMesh3, SOMaterial, TransformableSO> 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);
            }

            TransformableSO newSO = (MakeSOFunc != null) ?
                                    MakeSOFunc(CombineMesh, scene.DefaultMeshSOMaterial)
                : new DMeshSO().Create(CombineMesh, 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.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);
        }