private IEnumerator GetWWWFiles(string objFilePath, bool useMtl, Material standardMaterial, Material transparentMaterial, ObjData objData)
    {
        var www = new WWW(objFilePath);

        while (!www.isDone)
        {
            objData.SetProgress(www.progress * (useMtl? .5f : 1.0f));
            yield return(null);
        }
        if (www.error != null)
        {
            Debug.Log(www.error);
            objData.SetDone();
            yield break;
        }

        string objFile  = www.text;
        string filePath = "";
        Dictionary <string, Material> materials = null;

        if (useMtl)
        {
            string mtlFileName = GetMTLFileName(ref objFilePath, ref objFile, ref filePath);
            if (mtlFileName != "")
            {
                // Read in mtl file
                www = new WWW(filePath + mtlFileName);
                yield return(www);

                if (www.error != null)
                {
                    Debug.Log(www.error);
                    objData.SetDone();
                    yield break;
                }
                if (www.text != "")
                {
                    // Get textures and parse MTL file
                    var linesRef = new LinesRef();
                    var textures = new Dictionary <string, Texture2D>();
                    yield return(StartCoroutine(GetTexturesAsync(www.text, linesRef, filePath, textures, objData)));

                    materials = ParseMTL(linesRef.lines, standardMaterial, transparentMaterial, filePath, textures);
                    if (materials == null)
                    {
                        useMtl = false;
                    }
                }
            }
            else
            {
                useMtl = false;
            }
        }

        CreateObjects(ref objFile, useMtl, materials, standardMaterial, objData);
    }
    private IEnumerator GetTexturesAsync(string mtlFile, LinesRef linesRef, string filePath, Dictionary <string, Texture2D> textures, ObjData objData)
    {
        Texture2D diffuseTexture = null;

        string[] lines            = SplitLines(ref mtlFile);
        int      numberOfTextures = 0;

        // See how many textures there are (to use for progress)
        for (int i = 0; i < lines.Length; i++)
        {
            var line = lines[i];
            CleanLine(ref line);
            lines[i] = line;

            if (line.Length < 7 || line[0] == '#')
            {
                continue;
            }

            if (line.StartsWith("map_Kd") && filePath != "")
            {
                numberOfTextures++;
            }
        }

        float progress = .5f;

        for (int i = 0; i < lines.Length; i++)
        {
            if (lines[i].Length < 7 || lines[i][0] == '#')
            {
                continue;
            }

            // Get diffuse texture
            if (lines[i].StartsWith("map_Kd") && filePath != "")
            {
                var lineInfo = lines[i].Split(' ');
                if (lineInfo.Length > 1)
                {
                    var textureFilePath = filePath + lineInfo[1];
                    var www             = new WWW(textureFilePath);
                    while (!www.isDone)
                    {
                        objData.SetProgress(progress + (www.progress / numberOfTextures) * .5f);
                        yield return(null);
                    }
                    if (www.error != null)
                    {
                        Debug.Log(www.error);
                        objData.SetDone();
                        yield break;
                    }
                    progress      += (1.0f / numberOfTextures) * .5f;
                    diffuseTexture = new Texture2D(4, 4);
                    www.LoadImageIntoTexture(diffuseTexture);
                    textures[lineInfo[1]] = diffuseTexture;
                }
                continue;
            }
        }
        linesRef.lines = lines;
    }
Ejemplo n.º 3
0
    private IEnumerator GetWWWFiles(string objFilePath, bool useMtl, Material standardMaterial, Material transparentMaterial, ObjData objData)
    {
        var www = new WWW(objFilePath);
        while (!www.isDone) {
            objData.SetProgress (www.progress * (useMtl? .5f : 1.0f));
            if (objData.cancel) {
                yield break;
            }
            yield return null;
        }
        if (www.error != null) {
            Debug.LogError ("Error loading " + objFilePath + ": " + www.error);
            objData.SetDone();
            yield break;
        }

        string objFile = www.text;
        string filePath = "";
        Dictionary<string, Material> materials = null;

        if (useMtl) {
            string mtlFileName = GetMTLFileName (ref objFilePath, ref objFile, ref filePath);
            if (mtlFileName != "") {
                // Read in mtl file
                www = new WWW(filePath + mtlFileName);
                while (!www.isDone) {
                    if (objData.cancel) {
                        yield break;
                    }
                    yield return null;
                }
                if (www.error != null) {
                    if (!useMTLFallback) {
                        Debug.LogError ("Error loading " + (filePath + mtlFileName) + ": " + www.error);
                        objData.SetDone();
                        yield break;
                    }
                    else {
                        useMtl = false;
                    }
                }
                if (useMtl && www.text != "") {
                    // Get textures and parse MTL file
                    var linesRef = new LinesRef();
                    var textures = new Dictionary<string, Texture2D>();
                    var loadError = new BoolRef(false);
                    yield return StartCoroutine (GetTexturesAsync (www.text, linesRef, filePath, textures, objData, loadError));
                    if (loadError.b == true) {
                        yield break;
                    }
                    materials = ParseMTL (linesRef.lines, standardMaterial, transparentMaterial, filePath, textures);
                    if (materials == null) {
                        useMtl = false;
                    }
                }
            }
            else {
                useMtl = false;
            }
        }

        CreateObjects (ref objFile, useMtl, materials, standardMaterial, objData, Path.GetFileNameWithoutExtension (objFilePath));
    }
Ejemplo n.º 4
0
    private IEnumerator GetTexturesAsync(string mtlFile, LinesRef linesRef, string filePath, Dictionary<string, Texture2D> textures, ObjData objData, BoolRef loadError)
    {
        Texture2D diffuseTexture = null;
        string[] lines = mtlFile.Split ('\n');

        int numberOfTextures = 0;

        // See how many textures there are (to use for progress)
        for (int i = 0; i < lines.Length; i++) {
            var line = lines[i];
            CleanLine (ref line);
            lines[i] = line;

            if (line.Length < 7 || line[0] == '#') {
                continue;
            }

            if (IsTextureLine (line) && filePath != "") {
                numberOfTextures++;
            }
        }

        float progress = .5f;
        for (int i = 0; i < lines.Length; i++) {
            if (lines[i].Length < 7 || lines[i][0] == '#') {
                continue;
            }

            // Get diffuse/bump texture
            if (IsTextureLine (lines[i]) && filePath != "") {
                var textureFilePath = GetFileName (lines[i], GetToken (lines[i]));
                if (textureFilePath != "") {
                    var completeFilePath = filePath + textureFilePath;
                    var www = new WWW(completeFilePath);
                    while (!www.isDone) {
                        objData.SetProgress (progress + (www.progress / numberOfTextures) * .5f);
                        if (objData.cancel) {
                            loadError.b = true;
                            yield break;
                        }
                        yield return null;
                    }
                    if (www.error != null) {
                        Debug.LogError ("Error loading " + completeFilePath + ": " + www.error);
                        loadError.b = true;
                        objData.SetDone();
                        yield break;
                    }
                    progress += (1.0f / numberOfTextures) * .5f;
                    diffuseTexture = new Texture2D (4, 4);
                    www.LoadImageIntoTexture (diffuseTexture);
                    if (lines[i].StartsWith ("map_bump") || lines[i].StartsWith ("bump")) {
                        ConvertToNormalmap (diffuseTexture);
                    }
                    textures[textureFilePath] = diffuseTexture;
                }
            }
        }
        linesRef.lines = lines;
    }
Ejemplo n.º 5
0
    private IEnumerator GetWWWFiles(string objFilePath, bool useMtl, Material standardMaterial, Material transparentMaterial, ObjData objData)
    {
        var www = new WWW(objFilePath);
        while (!www.isDone) {
            objData.SetProgress (www.progress * (useMtl? .5f : 1.0f));
            yield return null;
        }
        if (www.error != null) {
            Debug.Log (www.error);
            objData.SetDone();
            yield break;
        }

        string objFile = www.text;
        string filePath = "";
        Dictionary<string, Material> materials = null;

        if (useMtl) {
            string mtlFileName = GetMTLFileName (ref objFilePath, ref objFile, ref filePath);
            if (mtlFileName != "") {
                // Read in mtl file
                www = new WWW(filePath + mtlFileName);
                yield return www;
                if (www.error != null) {
                    Debug.Log (www.error);
                    objData.SetDone();
                    yield break;
                }
                if (www.text != "") {
                    // Get textures and parse MTL file
                    var linesRef = new LinesRef();
                    var textures = new Dictionary<string, Texture2D>();
                    yield return StartCoroutine (GetTexturesAsync (www.text, linesRef, filePath, textures, objData));
                    materials = ParseMTL (linesRef.lines, standardMaterial, transparentMaterial, filePath, textures);
                    if (materials == null) {
                        useMtl = false;
                    }
                }
            }
            else {
                useMtl = false;
            }
        }

        CreateObjects (ref objFile, useMtl, materials, standardMaterial, objData);
    }
Ejemplo n.º 6
0
    private IEnumerator GetTexturesAsync(string mtlFile, LinesRef linesRef, string filePath, Dictionary<string, Texture2D> textures, ObjData objData)
    {
        Texture2D diffuseTexture = null;
        string[] lines = SplitLines (ref mtlFile);
        int numberOfTextures = 0;

        // See how many textures there are (to use for progress)
        for (int i = 0; i < lines.Length; i++) {
            var line = lines[i];
            CleanLine (ref line);
            lines[i] = line;

            if (line.Length < 7 || line[0] == '#') {
                continue;
            }

            if (line.StartsWith ("map_Kd") && filePath != "") {
                numberOfTextures++;
            }
        }

        float progress = .5f;
        for (int i = 0; i < lines.Length; i++) {
            if (lines[i].Length < 7 || lines[i][0] == '#') {
                continue;
            }

            // Get diffuse texture
            if (lines[i].StartsWith ("map_Kd") && filePath != "") {
                var lineInfo = lines[i].Split (' ');
                if (lineInfo.Length > 1) {
                    var textureFilePath = filePath + lineInfo[1];
                    var www = new WWW(textureFilePath);
                    while (!www.isDone) {
                        objData.SetProgress (progress + (www.progress / numberOfTextures) * .5f);
                        yield return null;
                    }
                    if (www.error != null) {
                        Debug.Log (www.error);
                        objData.SetDone();
                        yield break;
                    }
                    progress += (1.0f / numberOfTextures) * .5f;
                    diffuseTexture = new Texture2D (4, 4);
                    www.LoadImageIntoTexture (diffuseTexture);
                    textures[lineInfo[1]] = diffuseTexture;
                }
                continue;
            }
        }
        linesRef.lines = lines;
    }