Пример #1
0
        public static GameObject SetupForSceneOrientationAndUnits(GameObject sceneRoot, JSONNode metadata)
        {
            JSONNode pt  = metadata ["worldBoundingBox"] ["maxXYZ"];
            Vector3  max = new Vector3(pt [0].AsFloat, pt [1].AsFloat, pt [2].AsFloat);

            pt = metadata ["worldBoundingBox"] ["minXYZ"];
            Vector3 min    = new Vector3(pt [0].AsFloat, pt [1].AsFloat, pt [2].AsFloat);
            Vector3 center = (max + min) / 2f;

            //Vector3 size =(max - min) / 2f ;
            //Bounds boundsBubble =new Bounds (center, size) ;
            //Bounds bounds =BoundsExtensions.GameObjectBounds (sceneRoot) ;
            pt = metadata ["refPointLMV"] [1];
            Vector3 pivot = pt.AsArray.Count == 3 ? new Vector3(pt [0].AsFloat, pt [1].AsFloat, pt [2].AsFloat) : Vector3.zero;

            if (sceneRoot.transform.parent == null)
            {
                string     name  = sceneRoot.name + "Pivot";
                GameObject gRoot = new GameObject(name);
                sceneRoot.transform.parent = gRoot.transform;
            }
            Transform root   = sceneRoot.transform.parent;
            JSONNode  marker = metadata ["marker"];

            if (marker != null && marker ["point"] != null)
            {
                pt = marker ["point"];
                Vector3 tr = new Vector3(pt ["x"].AsFloat, pt ["y"].AsFloat, pt ["z"].AsFloat);
                pivot += center + tr;
            }
            root.localPosition = pivot;
            Transform main = sceneRoot.transform;

            main.localPosition = -pivot;
            JSONArray upvector = metadata ["worldUpVector"] [1].AsArray;

            if (upvector [2].AsFloat != 0f)                 // Z axis
            {
                root.Rotate(Vector3.right, -90);
                root.localScale = new Vector3(-1f, 1f, 1f);
            }
            string units        = metadata ["units"];
            float  unitsConvert = ForgeLoaderEngine.convertToMeter(units);

            if (unitsConvert != 1.0f)
            {
                root.localScale = new Vector3(root.localScale.x * unitsConvert, root.localScale.y * unitsConvert, root.localScale.z * unitsConvert);
            }
            // Now move to final position
            root.localPosition = Vector3.zero;

            return(root.gameObject);
        }
Пример #2
0
        public static Matrix4x4 fromUnityMatrix(GameObject sceneRoot, JSONNode metadata = null)
        {
            if (metadata == null)
            {
                metadata = sceneRoot.GetComponent <ForgeProperties> ().Properties;
            }

            Vector3 center = GetSceneBoundingBox(sceneRoot, metadata).center;
            Vector3 pivot  = GetSceneOrigin(sceneRoot, metadata);

            if (IsMarkerDefined(sceneRoot, metadata))
            {
                pivot += /*center +*/ GetMarkerPoint(sceneRoot, metadata);
            }
            Vector3 upvector = GetSceneUpVector(sceneRoot, metadata);
            string  units    = GetSceneUnit(sceneRoot, metadata);

            Transform tr = new GameObject().transform;

            if (upvector [2] != 0f)                 // Z axis
            {
                tr.Rotate(Vector3.right, -90);
                tr.localScale = new Vector3(-1f, 1f, 1f);
            }
            float unitsConvert = ForgeLoaderEngine.convertToMeter(units);

            if (unitsConvert != 1.0f)
            {
                tr.localScale = new Vector3(tr.localScale.x * unitsConvert, tr.localScale.y * unitsConvert, tr.localScale.z * unitsConvert);
            }
            //tr.localPosition =-pivot ;

            Matrix4x4 mat = Matrix4x4.identity;

            mat = tr.worldToLocalMatrix;

            Matrix4x4 mTR = Matrix4x4.identity;

            mTR.m03 = pivot.x;
            mTR.m13 = pivot.y;
            mTR.m23 = pivot.z;
            mat     = mTR * mat;
            GameObject.DestroyImmediate(tr.gameObject);
            return(mat);
        }