예제 #1
0
        protected void requestMesh(object sender, IRequestInterface args)
        {
            // Meshes are safe because they will be called only once
            MeshRequest reqObj = args as MeshRequest;

            reqObj.uri = new System.Uri(ForgeLoaderConstants._endpoint + URN + "/scenes/" + SCENEID + "/mesh/" + reqObj.fragId.Item1 + "/" + reqObj.fragId.Item2);
            //UnityEngine.Debug.Log ("URI request: " + reqObj.uri.ToString ());
            _mgr.Register(reqObj);
        }
예제 #2
0
        public virtual void Update()
        {
            //UnityEngine.Profiling.Profiler.BeginSample ("Forge AR|VR Toolkit");
            if (!Active)
            {
                return;
            }
            // Do we got more requests to fire?
            int pending = _mgr.Count(SceneLoadingStatus.ePending);

            if (pending < ForgeLoaderConstants.NB_MAX_REQUESTS)
            {
                RequestQueueMgrEnumerator news = _mgr.GetTypeEnumerator(SceneLoadingStatus.eNew);
                pending = _mgr.Count(SceneLoadingStatus.eNew);
                if (news.MoveNext())
                {
                    //UnityEngine.Debug.Log (DateTime.Now.ToString ("HH:mm:ss.f") + " / " + ((IRequestInterface)news.Current).uri.ToString ());
                    _mgr.FireRequest(news.Current);
                }
            }

            // For each request we got an answer for, we build the corresponding scene object
            RequestQueueMgrEnumerator items = _mgr.GetCompletedEnumerator();

            while (items.MoveNext())
            {
                IRequestInterface item = items.Current;

                if (item.resolved == SceneLoadingStatus.eInstanceTree)
                {
                    item.fireRequestCallback += requestSceneObjectDetails;
                }
                else if (item.resolved == SceneLoadingStatus.eMaterial)
                {
                    item.fireRequestCallback += requestTextures;
                }

                GameObject obj = item.BuildScene(
                    item.resolved == SceneLoadingStatus.eInstanceTree ?
                    SCENEID : item.GetName(),
                    SaveToDisk
                    );
                if (obj != null && item.resolved == SceneLoadingStatus.eInstanceTree)
                {
                    if (ROOT == null)
                    {
                        ROOT = obj;
                    }
                    else
                    {
                        obj.transform.parent        = ROOT.transform;
                        obj.transform.localPosition = Vector3.zero;
                    }
                }
                else if (item.resolved == SceneLoadingStatus.eMaterial)                       // Safe as we make only 1 request per material
                {
                    _materialLib.Add((item as MaterialRequest).matId, (item as MaterialRequest).material);
                }
                else if (item.resolved == SceneLoadingStatus.eProperties)                       // Safe as we make only 1 request per propertyset
                {
                    _properties.Add((item as PropertiesRequest).dbId, (item as PropertiesRequest).properties);
                }                 // else if ( item.resolved == SceneLoadingStatus.eTexture )

                break;
            }

            // Assign Material to Mesh waiting for it
            items = _mgr.GetTypeEnumerator(SceneLoadingStatus.eWaitingMaterial);
            while (items.MoveNext())
            {
                MeshRequest item = items.Current as MeshRequest;
                if (!_materialLib.ContainsKey(item.materialId))
                {
                    continue;
                }
                MeshRenderer renderer = item.gameObject.GetComponent <MeshRenderer> ();
                renderer.sharedMaterial = _materialLib [item.materialId];
                item.state = item.resolved;
            }

            // Showing progress
            if (ProcessedNodes != null)
            {
                int total = _mgr.Count();
                int built = _mgr.Count(new SceneLoadingStatus [] {
                    SceneLoadingStatus.eInstanceTree, SceneLoadingStatus.eMesh, SceneLoadingStatus.eMaterial, SceneLoadingStatus.eTexture, SceneLoadingStatus.eProperties,
                    SceneLoadingStatus.eWaitingMaterial, SceneLoadingStatus.eWaitingTexture
                });
                int completed = _mgr.Count(SceneLoadingStatus.eReceived);

                float val = 100.0f * Math.Min(completed + built, total) / total;
                ProcessedNodes(this, val);

                if (ProcessingNodesCompleted != null)
                {
                    built = _mgr.Count(new SceneLoadingStatus [] {
                        SceneLoadingStatus.eInstanceTree, SceneLoadingStatus.eMesh, SceneLoadingStatus.eMaterial, SceneLoadingStatus.eTexture, SceneLoadingStatus.eProperties,
                    });
                    int unprocessed = _mgr.Count(new SceneLoadingStatus [] {
                        SceneLoadingStatus.eCancelled, SceneLoadingStatus.eError,
                    });

                    if (built + unprocessed == total)
                    {
                        ProcessingNodesCompleted(this, unprocessed);
                        TimeSpan tm = DateTime.Now - started;
                        UnityEngine.Debug.Log(URN + "-" + SCENEID + " loaded in: " + tm.TotalSeconds.ToString());
                        Sleep();                           // Sleep ourself
                    }
                }
            }

            //UnityEngine.Profiling.Profiler.EndSample ();
        }
        protected void IteratorNodes(JSONNode node, GameObject go)
        {
            GameObject obj = null;

            switch (node ["type"].Value)
            {
            case "Transform":
                int    dbId0    = node ["id"].AsInt;
                string nodeName = buildName("transform", dbId0, 0, node ["pathid"]);
                obj = new GameObject(nodeName);
                setTransform(node, obj);
                obj.transform.parent = go.transform;

                // But requesting properties here does not really make sense and is slowing down the streaming.
                // Instead, we could get properties on demand or at the end.

                if (_properties.Add(dbId0))
                {
                    PropertiesRequest req = new PropertiesRequest(loader, null, bearer, dbId0);
                    req.gameObject = obj;
                    if (fireRequestCallback != null)
                    {
                        fireRequestCallback(this, req);
                    }
                }

                break;

            case "Mesh":
                int dbId = node ["id"].AsInt;
                for (int i = 0; i < node ["fragments"].AsArray.Count; i++)
                {
                    int fragId = node ["fragments"] [i].AsInt;
                    int matId  = node ["materials"] [i].AsInt;
                    obj = CreateMeshObject(dbId, fragId, node ["pathid"]);
                    setTransform(node, obj);
                    obj.transform.parent = go.transform;

                    int polys = node ["fragPolys"] [i].AsInt;
                    if (polys > 0)
                    {
                        // Create a new request to get the Mesh definition
                        if (_fragments.Add(new Eppy.Tuple <int, int> (dbId, fragId)))
                        {
                            MeshRequest req = new MeshRequest(loader, null, bearer, new Eppy.Tuple <int, int> (dbId, fragId), matId, node);
                            req.gameObject = obj;
                            if (fireRequestCallback != null)
                            {
                                fireRequestCallback(this, req);
                            }
                        }

                        // Create a new request to get the Material definition
                        if (_materials.Add(matId))
                        {
                            MaterialRequest req = new MaterialRequest(loader, null, bearer, node ["materials"] [i].AsInt, node);
                            req.gameObject = obj;
                            if (fireRequestCallback != null)
                            {
                                fireRequestCallback(this, req);
                            }
                        }
                    }

                    // Create a new request to get the properties (but only once), and store it on the parent transform
                }

                // But requesting properties here does not really make sense and is slowing down the streaming.
                // Instead, we could get properties on demand or at the end.

                //if ( _properties.Add (dbId) ) {
                //	PropertiesRequest req = new PropertiesRequest (loader, null, bearer, dbId);
                //	req.gameObject = obj.transform.parent.gameObject;
                //	if ( fireRequestCallback != null )
                //		fireRequestCallback (this, req);
                //}

                break;

            default:
                break;
            }
            if (obj != null && node ["childs"] != null)
            {
                try {
                    foreach (JSONNode child in node["childs"].AsArray)
                    {
                        IteratorNodes(child, obj);
                    }
                } catch (System.Exception /*ex*/) {
                }
            }
        }