public void Push(Material material)
 {
     Materials.Add(material);
 }
 private void PushMaterial(string materialName)
 {
     _currentMaterial = new Material(materialName);
     _materialLibrary.Push(_currentMaterial);
 }
        private void LoadMaterial()
        {
            var data = Encoding.ASCII.GetBytes(MaterialLibrary);
            var materialStream = new MemoryStream(data);

            _materialLibraryLoader.Load(materialStream);
            _firstMaterial = _materialLibrarySpy.Materials.First();
            _secondMaterial = _materialLibrarySpy.Materials.ElementAt(1);
        }
Example #4
0
        public UnityEngine.Material Create(ObjLoader.Loader.Data.Material material)
        {
            var HasDiffuse           = ValidStr(material.DiffuseTextureMap);
            var HasBump              = ValidStr(material.BumpMap);
            var HasParallax          = ValidStr(material.DisplacementMap);
            var HasSpecular_Color    = ValidStr(material.SpecularTextureMap);          // Specular color I think
            var HasSpecular_Hardness = ValidStr(material.SpecularHighlightTextureMap); // Specular INTENSITY map
            var HasEmission          = ValidStr(material.EmissiveTextureMap);
            var HasAmbientOcclusion  = ValidStr(material.AmbientTextureMap);
            var HasTransparency      = ValidStr(material.AlphaTextureMap);
            var HasDecals            = ValidStr(material.StencilDecalMap);// Ehh, Someone else will implement support for decals eventually... yeah right...

            // Here we will just go ahead and set any of these flags TRUE if we can already tell they are going to be needed.
            // This ensures we don't get bad materials when bad moeling programs don't write the correct IlluminationMode into the .mtl files.

            var _specularity  = (HasSpecular_Color || HasSpecular_Hardness); //does the shader we are using support specularity parameters
            var _lighting     = HasBump;                                     // Does the material receive lighting from the environment?
            var _transparency = HasTransparency;                             // Does this material need to allow use of the alpha channel when drawing textures?
            var _reflection   = false;
            var _refraction   = false;
            var _shadows      = false;// Does this material cast shadows?


            // First let's instantiate a new Unity material using the appropriate shader.
            UnityEngine.Material mat = null;
            if (material.IlluminationModel >= 1)
            {
                _lighting = true;
            }
            if (material.IlluminationModel >= 2)
            {
                _specularity = true;
            }
            if (material.IlluminationModel >= 3)
            {
                _reflection = true;
            }
            if (material.IlluminationModel >= 4)
            {
                _transparency = true;
            }


            switch (material.IlluminationModel)
            {
            case 0:    // Diffuse
                break;

            case 1:    // Diffuse + Ambient
                break;

            case 2:    // Diffuse + Ambient + Specularity
                break;

            case 3:    // Reflection on and Ray trace on
                _reflection = true;
                break;

            case 4:    // Transparency: Glass on, Reflection: Ray trace on
                _reflection = true;
                break;

            case 6:    // Transparency: Refraction on, Reflection: Fresnel off and Ray trace on
                _reflection = true;
                break;

            case 7:    // Transparency: Refraction on, Reflection: Fresnel on and Ray trace on
                _reflection = true;
                _refraction = true;
                break;

            case 9:    // Transparency: Glass on, Reflection: Ray trace off
                break;

            case 10:    // Casts shadows onto invisible surfaces
                break;
            }

            // Instantiate a material
            if (mat == null && _refraction && _transparency)
            {
            }                                                   // Fallthrough to the transparent/diffuse shader for now as we presently have no refraction capable shaders.
            if (mat == null && _transparency)
            {
                mat = new UnityEngine.Material(Shader.Find("Transparent/Diffuse"));
            }

            //if (mat == null && _lighting) mat = new UnityEngine.Material(Shader.Find("Diffuse"));
            if (mat == null && !_lighting)
            {
                mat = new UnityEngine.Material(Shader.Find("VertexLit"));
            }
            if (mat == null)
            {
                mat = new UnityEngine.Material(Shader.Find("Standard"));
            }
            // Name it
            mat.name = material.Name;

            // Set any necessarry shader params
            var albedo = toUnityColor(material.DiffuseColor);

            albedo.a = material.Transparency;
            mat.SetColor(Color, albedo);

            if (_lighting)
            {
                mat.SetColor(EmissionColor, toUnityColor(material.AmbientColor));
            }
            if (_specularity)
            {
                mat.SetColor(SpecColor, toUnityColor(material.SpecularColor));
            }
            //mat.SetFloat("_Glossiness", );// We might actually want this to be set to 1.0 because .mlt specs don't define a specularIntensity. instead the specular color is multiplied by the intensity before modeling programs export the material file. so intensity is already encoded in the color...

            // Now load all of the textures our material needs...
            if (HasDiffuse)
            {
                this.ApplyTexture(mat, "_MainTex", material.DiffuseTextureMap);            // mat.SetTexture("_MainTex", LoadTexture(material.DiffuseTextureMap)); //mat.mainTexture = LoadTexture(material.DiffuseTextureMap);
            }
            if (HasBump)
            {
                this.ApplyTexture(mat, "_BumpMap", material.BumpMap);         // mat.SetTexture("_BumpMap", LoadTexture(material.BumpMap));
            }
            if (HasParallax)
            {
                this.ApplyTexture(mat, "_ParallaxMap", material.DisplacementMap);             // mat.SetTexture("_ParallaxMap", LoadTexture(material.DisplacementMap));
            }
            //if (HasSpecular_Color) this.ApplyTexture(mat, "", material.SpecularTextureMap);// mat.SetTexture("", LoadTexture(material.SpecularTextureMap));
            if (HasSpecular_Hardness)
            {
                this.ApplyTexture(mat, "_MetallicGlossMap", material.SpecularHighlightTextureMap);                      // mat.SetTexture("_MetallicGlossMap", LoadTexture(material.SpecularHighlightTextureMap));
            }
            if (HasAmbientOcclusion)
            {
                this.ApplyTexture(mat, "_OcclusionMap", material.AmbientTextureMap);                     // mat.SetTexture("_OcclusionMap", LoadTexture(material.AmbientTextureMap));
            }
            if (HasEmission)
            {
                this.ApplyTexture(mat, "_EmissionMap", material.EmissiveTextureMap);             // mat.SetTexture("_EmissionMap", LoadTexture(material.EmissiveTextureMap));
            }
            return(mat);
        }