Пример #1
0
        protected PrimitiveType ConvertEntityToUnityPrimType(EntityRepresentation.AssetPrimType assetPrimType)
        {
            switch (assetPrimType)
            {
            case EntityRepresentation.AssetPrimType.Cube:
                return(PrimitiveType.Cube);

            case EntityRepresentation.AssetPrimType.Sphere:
                return(PrimitiveType.Sphere);

            case EntityRepresentation.AssetPrimType.Cylinder:
                return(PrimitiveType.Cylinder);

            case EntityRepresentation.AssetPrimType.Capsule:
                return(PrimitiveType.Capsule);

            case EntityRepresentation.AssetPrimType.Quad:
                return(PrimitiveType.Quad);

            case EntityRepresentation.AssetPrimType.Plane:
                return(PrimitiveType.Plane);
            }
            Debug.LogError("Asset type " + assetPrimType.ToString() + " not mapped to a primitive.");
            return(PrimitiveType.Cube);
        }
Пример #2
0
        protected VrXmlRepresentation.AssetType ConvertRepresentationPrimType(EntityRepresentation.AssetPrimType assetPrimType)
        {
            switch (assetPrimType)
            {
            case EntityRepresentation.AssetPrimType.Capsule:
                return(VrXmlRepresentation.AssetType.Capsule);

            case EntityRepresentation.AssetPrimType.Cube:
                return(VrXmlRepresentation.AssetType.Cube);

            case EntityRepresentation.AssetPrimType.Cylinder:
                return(VrXmlRepresentation.AssetType.Cylinder);

            case EntityRepresentation.AssetPrimType.Plane:
                return(VrXmlRepresentation.AssetType.Plane);

            case EntityRepresentation.AssetPrimType.Quad:
                return(VrXmlRepresentation.AssetType.Quad);

            case EntityRepresentation.AssetPrimType.Sphere:
                return(VrXmlRepresentation.AssetType.Sphere);

            case EntityRepresentation.AssetPrimType.None:
                return(VrXmlRepresentation.AssetType.None);
            }
            return(VrXmlRepresentation.AssetType.None);
        }
Пример #3
0
        protected IEnumerator CreateRepresentationObject(EntityData entityData, VrXmlRepresentation representation)
        {
            string representationId              = representation.id;
            string representationAssetName       = representation.assetName;
            string representationAssetBundleName = representation.assetBundleName;

            EntityRepresentation.AssetType     representationAssetType     = EntityRepresentation.AssetType.None;
            EntityRepresentation.AssetPrimType representationAssetPrimType = EntityRepresentation.AssetPrimType.None;
            ModelImportOptions importOptions = representation.importOptions;

            switch (representation.assetType)
            {
            case VrXmlRepresentation.AssetType.Model:
                representationAssetType = EntityRepresentation.AssetType.Model;
                break;

            case VrXmlRepresentation.AssetType.AssetBundle:
            case VrXmlRepresentation.AssetType.Prefab:
                representationAssetType = EntityRepresentation.AssetType.Prefab;
                break;

            case VrXmlRepresentation.AssetType.Cube:
            case VrXmlRepresentation.AssetType.Sphere:
            case VrXmlRepresentation.AssetType.Cylinder:
            case VrXmlRepresentation.AssetType.Capsule:
            case VrXmlRepresentation.AssetType.Quad:
            case VrXmlRepresentation.AssetType.Plane:
                representationAssetType = EntityRepresentation.AssetType.Primitive;
                break;
            }
            if (representationAssetType == EntityRepresentation.AssetType.Primitive)
            {
                representationAssetPrimType = ConvertAssetTypeToAssetPrimType(representation.assetType);
            }

            yield return(CreateRepresentationObject(
                             entityData,
                             representationId,
                             representationAssetName,
                             representationAssetBundleName,
                             representationAssetType,
                             representationAssetPrimType,
                             importOptions));
        }
Пример #4
0
        protected IEnumerator CreateRepresentationObject(
            EntityData entityData,
            string representationId,
            string representationAssetName,
            string representationAssetBundleName,
            EntityRepresentation.AssetType representationAssetType,
            EntityRepresentation.AssetPrimType representationAssetPrimType,
            ModelImportOptions importOptions
            )
        {
            bool created = false;
            EntityRepresentation entityRepresentation = null;

            //if (!string.IsNullOrEmpty(representationId))
            {
                PrimitiveType?primitiveCreated = null;
                EntityRepresentation.AssetPrimType primType = EntityRepresentation.AssetPrimType.None;
                switch (representationAssetType)
                {
                //case EntityRepresentation.AssetType.AssetBundle:
                //    if (!string.IsNullOrEmpty(representationAssetBundleName)
                //        && !string.IsNullOrEmpty(representationAssetName))
                //    {
                //        yield return InstantiateGameObjectAsync(representationAssetBundleName, representationAssetName, representationId, entityData.id);
                //        created = true;
                //    }
                //    break;
                case EntityRepresentation.AssetType.Model:
                    if (!string.IsNullOrEmpty(representationAssetName))
                    {
                        ModelLoader importer = GetComponent <ModelLoader>();
                        if (importer == null)
                        {
                            importer = gameObject.AddComponent <ModelLoader>();
                            importer.CreatedModel  += OnModelCreated;
                            importer.ImportedModel += OnModelImported;
                            importer.ImportError   += OnModelError;
                        }
                        importer.ImportModelAsync(representationId, representationAssetName, entityObjects[entityData.id].transform, importOptions);
                        while (importer.Running)
                        {
                            yield return(null);
                        }
                        EntityRepresentation entityRepr = null;
                        while (entityRepr == null && !importer.ErrorOccurred)
                        {
                            yield return(null);

                            entityRepr = entityObjects[entityData.id].GetComponentInChildren <EntityRepresentation>();
                        }
                        if (entityRepr != null)
                        {
                            entityRepresentation = entityRepr;
                            while (entityRepr.assetType != EntityRepresentation.AssetType.Model)
                            {
                                yield return(null);
                            }
                            entityRepr.assetName     = representationAssetName;
                            entityRepr.importOptions = importOptions;
                            entityObjects[entityData.id].GetComponent <EntityData>().activeRepresentation = entityRepr;

                            created = true;
                        }
                    }
                    break;

                case EntityRepresentation.AssetType.AssetBundle:
                case EntityRepresentation.AssetType.Prefab:
                    if (!string.IsNullOrEmpty(representationAssetName))
                    {
                        // TODO: this must be documented
                        string folder = "Representations/";
                        if (!string.IsNullOrEmpty(representationAssetBundleName))
                        {
                            folder += representationAssetBundleName + "/";
                        }
                        string     resourceName = folder + representationAssetName;
                        GameObject go           = null;
                        try
                        {
                            go = Instantiate(Resources.Load(resourceName), Vector3.zero, Quaternion.identity) as GameObject;
                        }
                        catch (Exception e)
                        {
                            Debug.LogError($"Failed to load resource {resourceName}: {e}");
                        }
                        if (go)
                        {
                            go.transform.SetParent(entityData.transform, false);
                            EntityRepresentation entityRepr = go.GetComponent <EntityRepresentation>();
                            if (entityRepr == null)
                            {
                                entityRepr = go.AddComponent <EntityRepresentation>();
                            }
                            go.name              = representationId;
                            entityRepr.name      = representationAssetName;
                            entityRepr.assetName = representationAssetName;
                            created              = true;
                            entityRepresentation = entityRepr;
                        }
                    }
                    break;

                case EntityRepresentation.AssetType.Primitive:
                    primitiveCreated = ConvertEntityToUnityPrimType(representationAssetPrimType);
                    primType         = representationAssetPrimType;
                    break;
                }
                if (primitiveCreated.HasValue)
                {
                    GameObject go = GameObject.CreatePrimitive(primitiveCreated.Value);
                    if (go)
                    {
                        go.transform.SetParent(entityData.transform, false);
                        EntityRepresentation entityRepr = go.GetComponent <EntityRepresentation>();
                        if (entityRepr == null)
                        {
                            entityRepr = go.AddComponent <EntityRepresentation>();
                        }
                        entityRepr.assetType     = EntityRepresentation.AssetType.Primitive;
                        entityRepr.assetPrimType = primType;
                        entityRepr.name          = representationId;
                        if (string.IsNullOrEmpty(entityRepr.name))
                        {
                            entityRepr.name = primitiveCreated.Value.ToString();
                        }
                        entityRepr.assetBundleName = null;
                        entityRepr.assetName       = null;
                        created = true;
                        entityRepresentation = entityRepr;
                    }
                }
            }

            if (!created)
            {
                if (representationAssetType != EntityRepresentation.AssetType.None)
                {
                    if (string.IsNullOrEmpty(representationAssetName))
                    {
                        Debug.LogWarning($"Asset information missing for the representation {representationId}");
                    }
                    else
                    {
                        Debug.LogWarning($"Failed to load representation from asset {representationAssetName}");
                        // TODO: Add a symbolic representation (cube/text)?
                    }
                }
            }
            if (entityRepresentation)
            {
                entityData.activeRepresentation = entityRepresentation;
                createdRepresentations++;
            }
        }