Exemplo n.º 1
0
        public static SSTextureMaterial FromBlenderMtl(string basePath, SSWavefrontMTLInfo mtl)
        {
            SSTexture diffuse = null, specular = null, ambient = null, bumpMap = null;

            if (mtl.diffuseTextureResourceName != null && mtl.diffuseTextureResourceName.Length > 0)
            {
                string path = Path.Combine(basePath, mtl.diffuseTextureResourceName);
                diffuse = SSAssetManager.GetInstance <SSTextureWithAlpha> (path);
            }
            if (mtl.specularTextureResourceName != null && mtl.specularTextureResourceName.Length > 0)
            {
                string path = Path.Combine(basePath, mtl.specularTextureResourceName);
                specular = SSAssetManager.GetInstance <SSTextureWithAlpha> (path);
            }
            if (mtl.ambientTextureResourceName != null && mtl.ambientTextureResourceName.Length > 0)
            {
                string path = Path.Combine(basePath, mtl.ambientTextureResourceName);
                ambient = SSAssetManager.GetInstance <SSTextureWithAlpha> (path);
            }
            if (mtl.bumpTextureResourceName != null && mtl.bumpTextureResourceName.Length > 0)
            {
                string path = Path.Combine(basePath, mtl.bumpTextureResourceName);
                bumpMap = SSAssetManager.GetInstance <SSTextureWithAlpha> (path);
            }

            return(new SSTextureMaterial(diffuse, specular, ambient, bumpMap));
        }
Exemplo n.º 2
0
        public static SSTextureMaterial FromBlenderMtl(SSAssetManager.Context ctx, SSWavefrontMTLInfo mtl)
        {
            SSTexture diffuse = null, specular = null, ambient = null, bumpMap = null;
            if (mtl.diffuseTextureResourceName != null && mtl.diffuseTextureResourceName.Length > 0) {
                diffuse = SSAssetManager.GetInstance<SSTextureWithAlpha> (ctx, mtl.diffuseTextureResourceName);
            }
            if (mtl.specularTextureResourceName != null && mtl.specularTextureResourceName.Length > 0) {
                specular = SSAssetManager.GetInstance<SSTextureWithAlpha> (ctx, mtl.specularTextureResourceName);
            }
            if (mtl.ambientTextureResourceName != null && mtl.ambientTextureResourceName.Length > 0) {
                ambient = SSAssetManager.GetInstance<SSTextureWithAlpha> (ctx, mtl.ambientTextureResourceName);
            }
            if (mtl.bumpTextureResourceName != null && mtl.bumpTextureResourceName.Length > 0) {
                bumpMap = SSAssetManager.GetInstance<SSTextureWithAlpha> (ctx, mtl.bumpTextureResourceName);
            }

            return new SSTextureMaterial (diffuse, specular, ambient, bumpMap);
        }
Exemplo n.º 3
0
        public static SSColorMaterial fromMtl(SSWavefrontMTLInfo info)
        {
            Color4 diffuse = Color4Helper.Zero;

            if (info.hasDiffuse)
            {
                diffuse = Color4Helper.fromVector4(info.vDiffuse);
            }
            Color4 ambient = Color4Helper.Zero;

            if (info.hasAmbient)
            {
                ambient = Color4Helper.fromVector4(info.vAmbient);
            }
            Color4 specular = Color4Helper.Zero;

            if (info.hasSpecular)
            {
                specular = Color4Helper.fromVector4(info.vSpecular);
            }
            return(new SSColorMaterial(diffuse, ambient, specular,
                                       Color4Helper.Zero));
        }
Exemplo n.º 4
0
        public static SSTextureMaterial FromBlenderMtl(SSAssetManager.Context ctx, SSWavefrontMTLInfo mtl)
        {
            SSTexture diffuse = null, specular = null, ambient = null, bumpMap = null;

            if (mtl.diffuseTextureResourceName != null && mtl.diffuseTextureResourceName.Length > 0)
            {
                diffuse = SSAssetManager.GetInstance <SSTextureWithAlpha> (ctx, mtl.diffuseTextureResourceName);
            }
            if (mtl.specularTextureResourceName != null && mtl.specularTextureResourceName.Length > 0)
            {
                specular = SSAssetManager.GetInstance <SSTextureWithAlpha> (ctx, mtl.specularTextureResourceName);
            }
            if (mtl.ambientTextureResourceName != null && mtl.ambientTextureResourceName.Length > 0)
            {
                ambient = SSAssetManager.GetInstance <SSTextureWithAlpha> (ctx, mtl.ambientTextureResourceName);
            }
            if (mtl.bumpTextureResourceName != null && mtl.bumpTextureResourceName.Length > 0)
            {
                bumpMap = SSAssetManager.GetInstance <SSTextureWithAlpha> (ctx, mtl.bumpTextureResourceName);
            }

            return(new SSTextureMaterial(diffuse, specular, ambient, bumpMap));
        }
Exemplo n.º 5
0
        public float vSpecularWeight; // Ns

        #endregion Fields

        #region Methods

        public static SSWavefrontMTLInfo[] ReadMTLs(SSAssetManager.Context ctx, string filename)
        {
            var materials = new List<SSWavefrontMTLInfo> ();
            SSWavefrontMTLInfo parseMaterial = null;

            StreamReader sr = ctx.OpenText(filename);

            //Read the first line of text
            string line = sr.ReadLine();

            //Continue to read until you reach end of file
            while (line != null) {
                string[] tokens = line.Split(" ".ToArray(), 2);
                if (tokens.Length < 2) {
                    goto next_line;
                }

                string firstToken = tokens[0];
                string lineContent = tokens[1];

                switch (firstToken) {
                case "#":
                    // Nothing to read, these are comments.
                    break;
                case "newmtl":  // create new named material
                    parseMaterial = new SSWavefrontMTLInfo();
                    materials.Add(parseMaterial);
                    parseMaterial.name = lineContent;
                    break;
                case "Ka": // ambient color
                    parseMaterial.vAmbient = WavefrontParser.readVector4(lineContent, null);
                    parseMaterial.hasAmbient = true;
                    break;
                case "Kd": // diffuse color
                    parseMaterial.vDiffuse = WavefrontParser.readVector4(lineContent, null);
                    parseMaterial.hasDiffuse = true;
                    break;
                case "Ks": // specular color (weighted by Ns)
                    parseMaterial.vSpecular = WavefrontParser.readVector4(lineContent,null);
                    parseMaterial.hasSpecular = true;
                    break;
                case "Ns": // specular color weight
                    parseMaterial.vSpecularWeight = WavefrontParser.parseFloat(lineContent);
                    break;
                case "d":
                case "Tr": // transparency / dissolve (i.e. alpha)
                    parseMaterial.fTransparency = WavefrontParser.parseFloat(lineContent);
                    parseMaterial.hasTransparency = true;
                    break;
                case "illum": // illumination mode
                    parseMaterial.hasIlluminationMode = true;
                    parseMaterial.illuminationMode = (SSWavefrontIlluminationMode) int.Parse(lineContent);
                    break;
                case "map_Kd": // diffuse color map
                    parseMaterial.diffuseTextureResourceName = lineContent;
                    break;
                case "map_Ka": // ambient color map
                    parseMaterial.ambientTextureResourceName = lineContent;
                    break;
                case "map_Ks": // specular color map
                    parseMaterial.specularTextureResourceName = lineContent;
                    break;
                case "bump":
                case "map_Bump":
                case "map_bump": // bump map
                    // bump <filename> [-bm <float intensity>]
                    // bump -bm <float intensity> <filename>
                    string[] parts = lineContent.Split(' ');
                    if (parts.Length == 1) {
                        parseMaterial.bumpTextureResourceName = parts[0];
                    } else {
                        if (parts.Length == 3) {
                            if (parts[1].Equals("-bm")) {
                                parseMaterial.bumpTextureResourceName = parts[0];
                                parseMaterial.bumpIntensity = WavefrontParser.parseFloat(parts[2]);
                            } else if (parts[0].Equals("-bm")) {
                                parseMaterial.bumpTextureResourceName = parts[3];
                                parseMaterial.bumpIntensity = WavefrontParser.parseFloat(parts[1]);
                            }
                        }
                    }

                    break;
                }

                next_line:
                //Read the next line
                line = sr.ReadLine();
            }

            //close the file
            sr.Close();

            return materials.ToArray();
        }
Exemplo n.º 6
0
        public static SSWavefrontMTLInfo[] ReadMTLs(SSAssetManager.Context ctx, string filename)
        {
            var materials = new List <SSWavefrontMTLInfo> ();
            SSWavefrontMTLInfo parseMaterial = null;

            StreamReader sr = ctx.OpenText(filename);

            //Read the first line of text
            string line = sr.ReadLine();

            //Continue to read until you reach end of file
            while (line != null)
            {
                string[] tokens = line.Split(" ".ToArray(), 2);
                if (tokens.Length < 2)
                {
                    goto next_line;
                }

                string firstToken  = tokens[0];
                string lineContent = tokens[1];

                switch (firstToken)
                {
                case "#":
                    // Nothing to read, these are comments.
                    break;

                case "newmtl":                  // create new named material
                    parseMaterial = new SSWavefrontMTLInfo();
                    materials.Add(parseMaterial);
                    parseMaterial.name = lineContent;
                    break;

                case "Ka":                 // ambient color
                    parseMaterial.vAmbient   = WavefrontParser.readVector4(lineContent, null);
                    parseMaterial.hasAmbient = true;
                    break;

                case "Kd":                 // diffuse color
                    parseMaterial.vDiffuse   = WavefrontParser.readVector4(lineContent, null);
                    parseMaterial.hasDiffuse = true;
                    break;

                case "Ks":                 // specular color (weighted by Ns)
                    parseMaterial.vSpecular   = WavefrontParser.readVector4(lineContent, null);
                    parseMaterial.hasSpecular = true;
                    break;

                case "Ns":                 // specular color weight
                    parseMaterial.vSpecularWeight = WavefrontParser.parseFloat(lineContent);
                    break;

                case "d":
                case "Tr":                 // transparency / dissolve (i.e. alpha)
                    parseMaterial.fTransparency   = WavefrontParser.parseFloat(lineContent);
                    parseMaterial.hasTransparency = true;
                    break;

                case "illum":                 // illumination mode
                    parseMaterial.hasIlluminationMode = true;
                    parseMaterial.illuminationMode    = (SSWavefrontIlluminationMode)int.Parse(lineContent);
                    break;

                case "map_Kd":                 // diffuse color map
                    parseMaterial.diffuseTextureResourceName = lineContent;
                    break;

                case "map_Ka":                 // ambient color map
                    parseMaterial.ambientTextureResourceName = lineContent;
                    break;

                case "map_Ks":                 // specular color map
                    parseMaterial.specularTextureResourceName = lineContent;
                    break;

                case "bump":
                case "map_Bump":
                case "map_bump":                 // bump map
                    // bump <filename> [-bm <float intensity>]
                    // bump -bm <float intensity> <filename>
                    string[] parts = lineContent.Split(' ');
                    if (parts.Length == 1)
                    {
                        parseMaterial.bumpTextureResourceName = parts[0];
                    }
                    else
                    {
                        if (parts.Length == 3)
                        {
                            if (parts[1].Equals("-bm"))
                            {
                                parseMaterial.bumpTextureResourceName = parts[0];
                                parseMaterial.bumpIntensity           = WavefrontParser.parseFloat(parts[2]);
                            }
                            else if (parts[0].Equals("-bm"))
                            {
                                parseMaterial.bumpTextureResourceName = parts[3];
                                parseMaterial.bumpIntensity           = WavefrontParser.parseFloat(parts[1]);
                            }
                        }
                    }


                    break;
                }

next_line:
                //Read the next line
                line = sr.ReadLine();
            }

            //close the file
            sr.Close();

            return(materials.ToArray());
        }
Exemplo n.º 7
0
        public static SSTextureMaterial FromMaterialString(SSAssetManager.Context ctx, string materialString)
        {
            string existingFilename = null;

            SSAssetManager.Context existingCtx = null;

            if (ctx != null && SSAssetManager.ResourceExists(ctx, materialString))
            {
                existingCtx      = ctx;
                existingFilename = materialString;
            }
            else if (SSAssetManager.ResourceExists(SSAssetManager.Context.Root, materialString))
            {
                existingCtx      = SSAssetManager.Context.Root;
                existingFilename = materialString;
            }
            else
            {
                SSAssetManager.Context[] ctxs = { ctx, SSAssetManager.Context.Root };
                var extensions = new List <string> (SSTexture.commonImageExtensions);
                extensions.Insert(0, ".mtl");                  // check mtl first

                foreach (var context in ctxs)
                {
                    // for each context (current vs root directory)...
                    foreach (string extension in extensions)
                    {
                        // for each extension of interest...
                        string filename = materialString + extension;
                        if (SSAssetManager.ResourceExists(context, filename))
                        {
                            existingCtx      = context;
                            existingFilename = filename;
                            break;
                        }
                    }
                }
            }

            if (existingFilename != null)
            {
                // try loading a material
                try {
                    SSWavefrontMTLInfo[] mtls = SSWavefrontMTLInfo.ReadMTLs(existingCtx, existingFilename);
                    if (mtls.Length < 0)
                    {
                        throw new Exception("No MTLs available in a file");
                    }
                    return(SSTextureMaterial.FromBlenderMtl(existingCtx, mtls[0]));
                } catch {
                    // try loading an image
                    try {
                        SSTexture diffTex = SSAssetManager.GetInstance <SSTextureWithAlpha> (existingCtx, existingFilename);
                        return(new SSTextureMaterial(diffTex));
                    } catch {
                    }
                }
            }

            string errMsg = "could not load texture material: " + materialString;

            System.Console.WriteLine(errMsg);
            throw new Exception(errMsg);
        }
Exemplo n.º 8
0
        public static SSTextureMaterial FromMaterialString(string basePath, string materialString)
        {
            string existingPath = null;

            string combined = Path.Combine(basePath, materialString);

            if (SSAssetManager.ResourceExists(combined))
            {
                existingPath = combined;
            }
            else if (SSAssetManager.ResourceExists(materialString))
            {
                existingPath = materialString;
            }
            else
            {
                string[] basePaths  = { "", basePath }; // search in root as well as supplied base path
                var      extensions = new List <string> (SSTexture.commonImageExtensions);
                extensions.Insert(0, ".mtl");           // check mtl first

                foreach (var bp in basePaths)
                {
                    // for each context (current vs root directory)...
                    foreach (string extension in extensions)
                    {
                        // for each extension of interest...
                        string fullPath = Path.Combine(bp, materialString + extension);
                        if (SSAssetManager.ResourceExists(fullPath))
                        {
                            existingPath = fullPath;
                            break;
                        }
                    }
                }
            }

            if (existingPath != null)
            {
                // try loading a material
                try {
                    SSWavefrontMTLInfo[] mtls = SSWavefrontMTLInfo.ReadMTLs(existingPath);
                    if (mtls.Length <= 0)
                    {
                        throw new Exception("No MTLs available in a file");
                    }
                    string baseDir = Path.GetDirectoryName(existingPath);
                    return(SSTextureMaterial.FromBlenderMtl(baseDir, mtls[0]));
                } catch {
                    // try loading an image
                    try {
                        SSTexture diffTex = SSAssetManager.GetInstance <SSTextureWithAlpha> (existingPath);
                        return(new SSTextureMaterial(diffTex));
                    } catch {
                    }
                }
            }

            string errMsg = "could not load texture material: " + materialString;

            System.Console.WriteLine(errMsg);
            throw new Exception(errMsg);
        }