Example #1
0
        public static void Parse(string path, float scale = 1, Material material = null, Material transparentMaterial = null, bool forceTangentsCalculation = false)
        {
            if (material == null)
            {
                material = Resources.Load <Material>("ObjDefaulOpaque");
            }
            if (transparentMaterial == null)
            {
                transparentMaterial = Resources.Load <Material>("ObjDefaulTransparent");
            }

            path = ProcessPath(path);

            var streamReader = GetStreamReader(path);

            if (streamReader == null)
            {
                return;
            }

            System.Diagnostics.Stopwatch stopwatch = null;
            if (logTime)
            {
                stopwatch = new System.Diagnostics.Stopwatch();
                stopwatch.Start();
            }

            var modelData = ObjGeometryProcessor.ProcessStream(streamReader, scale);

            ParseMaterials(path, modelData, material, transparentMaterial);

            CreateGameObjects(modelData, Path.GetFileNameWithoutExtension(path), forceTangentsCalculation);

            if (stopwatch != null)
            {
                stopwatch.Stop();
                Debug.Log($"Loading time = {stopwatch.Elapsed}");
            }
        }
Example #2
0
        async public static Task <GameObject> ParseAsync(string path, float scale = 1, Material material = null, Material transparentMaterial = null, bool forceTangentsCalculation = false)
        {
            if (material == null)
            {
                material = Resources.Load <Material>("ObjDefaulOpaque");
            }
            if (transparentMaterial == null)
            {
                transparentMaterial = Resources.Load <Material>("ObjDefaulTransparent");
            }

            path = ProcessPath(path);

            var streamReader = GetStreamReader(path);

            if (streamReader == null)
            {
                return(null);
            }

            System.Diagnostics.Stopwatch stopwatch = null;
            if (logTime)
            {
                stopwatch = new System.Diagnostics.Stopwatch();
                stopwatch.Start();
            }

#if UNITY_EDITOR
            // When in editor, tasks need to be canceled on exiting play mode

            var cancellationTokenSource = new CancellationTokenSource();
            Action <UnityEditor.PlayModeStateChange> lambda = state => { if (state == UnityEditor.PlayModeStateChange.ExitingPlayMode)
                                                                         {
                                                                             cancellationTokenSource.Cancel();
                                                                         }
            };
            UnityEditor.EditorApplication.playModeStateChanged += lambda;

            ModelData modelData;
            try
            {
                modelData = await Task.Run(() => ObjGeometryProcessor.ProcessStream(streamReader, scale, cancellationTokenSource.Token), cancellationTokenSource.Token);
            }
            catch (OperationCanceledException)
            {
                UnityEditor.EditorApplication.playModeStateChanged -= lambda;
                cancellationTokenSource.Dispose();
                return(null);
            }
            UnityEditor.EditorApplication.playModeStateChanged -= lambda;
            cancellationTokenSource.Dispose();
#else
            var modelData = await Task.Run(() => ObjGeometryProcessor.ProcessStream(streamReader, scale));
#endif

            ParseMaterials(path, modelData, material, transparentMaterial);

            var result = CreateGameObjects(modelData, Path.GetFileNameWithoutExtension(path), forceTangentsCalculation);

            if (stopwatch != null)
            {
                stopwatch.Stop();
                Debug.Log($"Loading time = {stopwatch.Elapsed}");
            }

            return(result);
        }
        public static ModelData ProcessStream(StreamReader streamReader, float scale, CancellationToken?ct = null)
        {
            var geometryProcessor = new ObjGeometryProcessor();

            return(geometryProcessor.GetMeshes(streamReader, scale, ct));
        }