コード例 #1
0
    void OnGLTFModelLoaded(GLTFComponent model)
    {
        if (calculateColliders)
        {
            InstantiatedGLTFObject rootObject = model.GetComponentInChildren <InstantiatedGLTFObject>();

            Collider[] colliders = rootObject.GetComponentsInChildren <Collider>();

            if (colliders.Length > 0)
            {
                float lowestPointInBounds = float.PositiveInfinity;

                foreach (var c in colliders)
                {
                    float lowestPoint = c.transform.position.y - c.bounds.center.y - c.bounds.extents.y;
                    if (lowestPoint < lowestPointInBounds)
                    {
                        lowestPointInBounds = lowestPoint;
                    }
                }

                Vector3 p = rootObject.transform.position;
                p.y += p.y - lowestPointInBounds;

                rootObject.transform.position = p;
            }
        }
    }
コード例 #2
0
    public IEnumerator GLTFScenarios([ValueSource("ModelFilePaths")] string modelPath)
    {
        ActiveGLTFObject = new GameObject();

        GLTFComponent gltfcomponent = ActiveGLTFObject.AddComponent <GLTFComponent>();

        gltfcomponent.GLTFUri = GLTF_ASSETS_PATH + modelPath;

        AssetGenerator.Manifest.Camera cam = cameras[Path.GetFileNameWithoutExtension(modelPath)];
        Camera.main.transform.position = new Vector3(cam.Translation[0], cam.Translation[1], cam.Translation[2]);
        yield return(gltfcomponent.Load());

        //wait one frame for rendering to complete
        yield return(null);

        Camera        mainCamera = Camera.main;
        RenderTexture rt         = new RenderTexture(512, 512, 24);

        mainCamera.targetTexture = rt;
        Texture2D actualContents = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);

        mainCamera.Render();
        RenderTexture.active = rt;
        actualContents.ReadPixels(new Rect(0, 0, 512, 512), 0, 0);
        byte[] pngActualfile  = actualContents.EncodeToPNG();
        string outputpath     = Path.GetDirectoryName(modelPath);
        string outputfullpath = GLTF_SCENARIO_OUTPUT_PATH + outputpath;

        Directory.CreateDirectory(outputfullpath);
        string filename         = Path.GetFileNameWithoutExtension(modelPath);
        string expectedFilePath = outputfullpath + "/" + filename + "_EXPECTED.png";
        string actualFilePath   = outputfullpath + "/" + filename + "_ACTUAL.png";

        if (GENERATE_REFERENCEDATA)
        {
            File.WriteAllBytes(expectedFilePath, pngActualfile);
        }
        else
        {
            if (File.Exists(expectedFilePath))
            {
                byte[] pngActualfileContents = File.ReadAllBytes(expectedFilePath);

                File.WriteAllBytes(actualFilePath, pngActualfile);
                //compare against expected
                Texture2D expectedContents = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
                expectedContents.LoadImage(pngActualfileContents);
                Color[] expectedPixels = expectedContents.GetPixels();
                Color[] actualPixels   = actualContents.GetPixels();
                Assert.AreEqual(expectedPixels.Length, actualPixels.Length);
                string errormessage = "\r\nExpectedPath: " + expectedFilePath + "\r\n ActualPath: " + actualFilePath;

                for (int i = 0; i < expectedPixels.Length; i++)
                {
                    Assert.AreEqual(expectedPixels[i], actualPixels[i], errormessage);
                }
            }
        }
    }
コード例 #3
0
        private static void SetGltfComponentField(GLTFComponent component, string name, object value)
        {
            var field = typeof(GLTFComponent).GetField(name, BindingFlags.NonPublic | BindingFlags.Instance);

            if (field != null)
            {
                field.SetValue(component, value);
            }
        }
コード例 #4
0
    private void CreateGLTF(string url, Vector3 position, Quaternion rotation, GameObject parent)
    {
        Debug.Log("Create blob: " + url);
        GLTFComponent component = gltf.GetComponent <GLTFComponent>();

        component.Url = url;
        var obj = Instantiate(gltf, position, rotation, parent.transform);

        obj.name = url.Substring(url.LastIndexOf('/') + 1);
    }
コード例 #5
0
 void LoadAndSpawnGLTFModel(string url, Vector3 position, Quaternion rotation)
 {
     if (gltfPrefab)
     {
         GLTFComponent gltfSpawner = Instantiate(gltfPrefab);
         gltfSpawner.GLTFUri            = url;
         gltfSpawner.transform.position = position;
         gltfSpawner.transform.rotation = rotation;
         gltfSpawner.onLoadFinished    += OnGLTFModelLoaded;
     }
 }
コード例 #6
0
    public void SetupEnvironment()
    {
        var scenePrefab = MonoBehaviour.Instantiate(Resources.Load <GameObject>("TestScenePrefab"));

        gltfComponent = scenePrefab.GetComponentInChildren <GLTFComponent>();

        // Parse manifest into a lookup dictionary
        List <Manifest> manifests = JsonConvert.DeserializeObject <List <Manifest> >(File.ReadAllText(GLTF_MANIFEST_PATH));

        foreach (Manifest manifest in manifests)
        {
            foreach (Manifest.Model model in manifest.Models)
            {
                // Rather than establishing a new data structure to hold the information, simply augment the
                // SampleImageName (which is actually a path) to include the starting folder as well.
                model.SampleImageName = Path.Combine(manifest.Folder, model.SampleImageName);
                modelManifests[Path.GetFileNameWithoutExtension(model.FileName)] = model;
            }
        }
    }
コード例 #7
0
        protected override void OnLoad(System.Action OnSuccess, System.Action OnFail)
        {
            gltfComponent = asset.container.AddComponent <GLTFComponent>();

            GLTFComponent.Settings tmpSettings = new GLTFComponent.Settings()
            {
                useVisualFeedback = settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION,
                initialVisibility = settings.visibleFlags != AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE,
                shaderOverride    = settings.shaderOverride,
                addMaterialsToPersistentCaching = (settings.cachingFlags & MaterialCachingHelper.Mode.CACHE_MATERIALS) != 0
            };

            tmpSettings.OnWebRequestStartEvent += ParseGLTFWebRequestedFile;

            gltfComponent.LoadAsset(url, GetId() as string, false, tmpSettings);
            gltfComponent.OnSuccess += OnSuccess;
            gltfComponent.OnFail    += OnFail;

            asset.name = url;
        }
コード例 #8
0
ファイル: GLTFBenchmark.cs プロジェクト: menduz/explorer
    void RunTest()
    {
        if (count > sampleCount)
        {
            Debug.Log($"Url = {url} ... Min time = {minTime * 1000}... Max time = {maxTime * 1000}");
            return;
        }

        count++;
        GameObject gameObject = new GameObject("Test");

        lastGameObjectCreated = gameObject;
        GLTFComponent gltfComponent = gameObject.AddComponent <GLTFComponent>();

        GLTFComponent.Settings tmpSettings = new GLTFComponent.Settings()
        {
            useVisualFeedback = false,
            initialVisibility = true,
        };

        gltfComponent.OnFinishedLoadingAsset += RunTest;
        gltfComponent.LoadAsset(Utils.GetTestsAssetsPath() + url, false, tmpSettings);
    }
        private IEnumerator LoadSourceControllerModel(InteractionSource source)
        {
            byte[]     fileBytes;
            GameObject controllerModelGameObject;

            if (GLTFMaterial == null)
            {
                Debug.Log("If using glTF, please specify a material on " + name + ".");
                yield break;
            }

#if !UNITY_EDITOR
            // This API returns the appropriate glTF file according to the motion controller you're currently using, if supported.
            IAsyncOperation <IRandomAccessStreamWithContentType> modelTask = source.TryGetRenderableModelAsync();

            if (modelTask == null)
            {
                Debug.Log("Model task is null; loading alternate.");
                LoadAlternateControllerModel(source);
                yield break;
            }

            while (modelTask.Status == AsyncStatus.Started)
            {
                yield return(null);
            }

            IRandomAccessStreamWithContentType modelStream = modelTask.GetResults();

            if (modelStream == null)
            {
                Debug.Log("Model stream is null; loading alternate.");
                LoadAlternateControllerModel(source);
                yield break;
            }

            if (modelStream.Size == 0)
            {
                Debug.Log("Model stream is empty; loading alternate.");
                LoadAlternateControllerModel(source);
                yield break;
            }

            fileBytes = new byte[modelStream.Size];

            using (DataReader reader = new DataReader(modelStream))
            {
                DataReaderLoadOperation loadModelOp = reader.LoadAsync((uint)modelStream.Size);

                while (loadModelOp.Status == AsyncStatus.Started)
                {
                    yield return(null);
                }

                reader.ReadBytes(fileBytes);
            }
#else
            IntPtr controllerModel = new IntPtr();
            uint   outputSize      = 0;

            if (TryGetMotionControllerModel(source.id, out outputSize, out controllerModel))
            {
                fileBytes = new byte[Convert.ToInt32(outputSize)];

                Marshal.Copy(controllerModel, fileBytes, 0, Convert.ToInt32(outputSize));
            }
            else
            {
                Debug.Log("Unable to load controller models; loading alternate.");
                LoadAlternateControllerModel(source);
                yield break;
            }
#endif

            controllerModelGameObject = new GameObject {
                name = "glTFController"
            };
            GLTFComponent gltfScript = controllerModelGameObject.AddComponent <GLTFComponent>();
            gltfScript.GLTFConstant = gltfScript.GLTFStandard = gltfScript.GLTFStandardSpecular = GLTFMaterial.shader;
            gltfScript.UseStream    = true;
            gltfScript.GLTFStream   = new MemoryStream(fileBytes);

            yield return(gltfScript.WaitForModelLoad());

            FinishControllerSetup(controllerModelGameObject, source.handedness, GenerateKey(source));
        }
コード例 #10
0
    public IEnumerator GLTFScenarios([ValueSource("SceneFilePaths")] string scenePath)
    {
        SceneManager.LoadScene(Path.GetFileNameWithoutExtension(scenePath));

        //wait one frame for loading to complete
        yield return(null);

        var objects = GameObject.FindObjectsOfType(typeof(GameObject));

        foreach (GameObject o in objects)
        {
            if (o.name.Contains("GLTF"))
            {
                GLTFComponent gltfcomponent = o.GetComponent <GLTFComponent>();
                gltfcomponent.Load();
            }
        }

        //wait one seconds for textures to load
        yield return(null);

        Camera mainCamera = Camera.main;

        Debug.Assert(mainCamera != null, "Make sure you have a main camera");
        RenderTexture rt = new RenderTexture(512, 512, 24);

        mainCamera.targetTexture = rt;
        Texture2D actualContents = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);

        mainCamera.Render();
        RenderTexture.active = rt;
        actualContents.ReadPixels(new Rect(0, 0, 512, 512), 0, 0);
        byte[] pngActualfile  = actualContents.EncodeToPNG();
        string outputpath     = Path.GetDirectoryName(scenePath);
        string outputfullpath = GLTF_SCENARIO_OUTPUT_PATH + outputpath;

        Directory.CreateDirectory(outputfullpath);
        string filename         = Path.GetFileNameWithoutExtension(scenePath);
        string expectedFilePath = outputfullpath + "/" + filename + "_EXPECTED.png";
        string actualFilePath   = outputfullpath + "/" + filename + "_ACTUAL.png";

        //uncomment to reggenerate master images
        if (GENERATE_REFERENCEDATA)
        {
            File.WriteAllBytes(expectedFilePath, pngActualfile);
        }
        else
        {
            if (File.Exists(expectedFilePath))
            {
                byte[] pngActualfileContents = File.ReadAllBytes(expectedFilePath);

                File.WriteAllBytes(actualFilePath, pngActualfile);
                //compare against expected
                Texture2D expectedContents = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
                expectedContents.LoadImage(pngActualfileContents);
                Color[] expectedPixels = expectedContents.GetPixels();
                Color[] actualPixels   = actualContents.GetPixels();
                Assert.AreEqual(expectedPixels.Length, actualPixels.Length);
                string errormessage = "\r\nExpectedPath: " + expectedFilePath + "\r\n ActualPath: " + actualFilePath;

                for (int i = 0; i < expectedPixels.Length; i++)
                {
                    Assert.AreEqual(expectedPixels[i], actualPixels[i], errormessage);
                }
            }
        }
    }
コード例 #11
0
ファイル: MeshScaler.cs プロジェクト: kblood/Quest-VR-Menu
 private void Awake()
 {
     gltf = GetComponent <GLTFComponent>();
 }