public override Material BuildMaterialFromProperties(MWMaterialProps mp)
        {
            Material material;

            //check if the material is already cached
            if (!m_existingMaterials.TryGetValue(mp, out material))
            {
                //otherwise create a new material and cache it
                if (mp.alphaBlended)
                {
                    material = BuildMaterialBlended(mp.srcBlendMode, mp.dstBlendMode);
                }
                else if (mp.alphaTest)
                {
                    material = BuildMaterialTested(mp.alphaCutoff);
                }
                else
                {
                    material = BuildMaterial();
                }

                if (mp.textures.mainFilePath != null)
                {
                    material.mainTexture = m_textureManager.LoadTexture(mp.textures.mainFilePath);
                }

                if (mp.textures.bumpFilePath != null)
                {
                    material.SetTexture("_BumpMap", m_textureManager.LoadTexture(mp.textures.bumpFilePath));
                }

                m_existingMaterials[mp] = material;
            }
            return(material);
        }
Exemple #2
0
 public abstract Material BuildMaterialFromProperties(MWMaterialProps mp);
Exemple #3
0
        public override Material BuildMaterialFromProperties(MWMaterialProps mp)
        {
            Material material;

            //check if the material is already cached
            if (!m_existingMaterials.TryGetValue(mp, out material))
            {
                //otherwise create a new material and cache it
                if (mp.alphaBlended)
                {
                    material = BuildMaterialBlended(mp.srcBlendMode, mp.dstBlendMode);
                }
                else if (mp.alphaTest)
                {
                    material = BuildMaterialTested(mp.alphaCutoff);
                }
                else
                {
                    material = BuildMaterial();
                }

                if (mp.textures.mainFilePath != null && material.HasProperty("_MainTex"))
                {
                    material.SetTexture("_MainTex", m_textureManager.LoadTexture(mp.textures.mainFilePath));
                }

                if (mp.textures.detailFilePath != null && material.HasProperty("_DetailTex"))
                {
                    material.SetTexture("_DetailTex", m_textureManager.LoadTexture(mp.textures.detailFilePath));
                }

                if (mp.textures.darkFilePath != null && material.HasProperty("_DarkTex"))
                {
                    material.SetTexture("_DarkTex", m_textureManager.LoadTexture(mp.textures.darkFilePath));
                }

                if (mp.textures.glossFilePath != null && material.HasProperty("_GlossTex"))
                {
                    material.SetTexture("_GlossTex", m_textureManager.LoadTexture(mp.textures.glossFilePath));
                }

                if (mp.textures.glowFilePath != null && material.HasProperty("_Glowtex"))
                {
                    material.SetTexture("_Glowtex", m_textureManager.LoadTexture(mp.textures.glowFilePath));
                }

                if (mp.textures.bumpFilePath != null && material.HasProperty("_BumpTex"))
                {
                    material.SetTexture("_BumpTex", m_textureManager.LoadTexture(mp.textures.bumpFilePath));
                }

                if (material.HasProperty("_Metallic"))
                {
                    material.SetFloat("_Metallic", 0f);
                }

                if (material.HasProperty("_Glossiness"))
                {
                    material.SetFloat("_Glossiness", 0f);
                }

                m_existingMaterials[mp] = material;
            }

            return(material);
        }
 public Material BuildMaterialFromProperties(MWMaterialProps mp)
 {
     return(_mwMaterial.BuildMaterialFromProperties(mp));
 }
Exemple #5
0
        private MWMaterialProps NiAVObjectPropertiesToMWMaterialProperties(NiAVObject obj)
        {
            // Find relevant properties.
            NiTexturingProperty texturingProperty = null;
            NiMaterialProperty  materialProperty  = null;
            NiAlphaProperty     alphaProperty     = null;

            foreach (var propRef in obj.properties)
            {
                var prop = file.blocks[propRef.value];

                if (prop is NiTexturingProperty)
                {
                    texturingProperty = (NiTexturingProperty)prop;
                }
                else if (prop is NiMaterialProperty)
                {
                    materialProperty = (NiMaterialProperty)prop;
                }
                else if (prop is NiAlphaProperty)
                {
                    alphaProperty = (NiAlphaProperty)prop;
                }
            }

            // Create the material properties.
            MWMaterialProps mp = new MWMaterialProps();

            #region AlphaProperty Cheat Sheet

            /*
             * 14 bits used:
             *
             * 1 bit for alpha blend bool
             * 4 bits for src blend mode
             * 4 bits for dest blend mode
             * 1 bit for alpha test bool
             * 3 bits for alpha test mode
             * 1 bit for zwrite bool ( opposite value )
             *
             * Bit 0 : alpha blending enable
             * Bits 1-4 : source blend mode
             * Bits 5-8 : destination blend mode
             * Bit 9 : alpha test enable
             * Bit 10-12 : alpha test mode
             * Bit 13 : no sorter flag ( disables triangle sorting ) ( Unity ZWrite )
             *
             * blend modes (glBlendFunc):
             * 0000 GL_ONE
             * 0001 GL_ZERO
             * 0010 GL_SRC_COLOR
             * 0011 GL_ONE_MINUS_SRC_COLOR
             * 0100 GL_DST_COLOR
             * 0101 GL_ONE_MINUS_DST_COLOR
             * 0110 GL_SRC_ALPHA
             * 0111 GL_ONE_MINUS_SRC_ALPHA
             * 1000 GL_DST_ALPHA
             * 1001 GL_ONE_MINUS_DST_ALPHA
             * 1010 GL_SRC_ALPHA_SATURATE
             *
             * test modes (glAlphaFunc):
             * 000 GL_ALWAYS
             * 001 GL_LESS
             * 010 GL_EQUAL
             * 011 GL_LEQUAL
             * 100 GL_GREATER
             * 101 GL_NOTEQUAL
             * 110 GL_GEQUAL
             * 111 GL_NEVER
             */
            #endregion

            if (alphaProperty != null)
            {
                ushort flags    = alphaProperty.flags;
                ushort oldflags = flags;
                byte   srcbm    = (byte)(BitConverter.GetBytes(flags >> 1)[0] & 15);
                byte   dstbm    = ( byte )(BitConverter.GetBytes(flags >> 5)[0] & 15);
                mp.zWrite = BitConverter.GetBytes(flags >> 15)[0] == 1;      //smush

                if (Utils.ContainsBitFlags(flags, 0x01))                     // if flags contain the alpha blend flag at bit 0 in byte 0
                {
                    mp.alphaBlended = true;
                    mp.srcBlendMode = FigureBlendMode(srcbm);
                    mp.dstBlendMode = FigureBlendMode(dstbm);
                }

                else if (Utils.ContainsBitFlags(flags, 0x100))                  // if flags contain the alpha test flag
                {
                    mp.alphaTest   = true;
                    mp.alphaCutoff = (float)alphaProperty.threshold / 255;
                }
            }
            else
            {
                mp.alphaBlended = false;
                mp.alphaTest    = false;
            }

            // Apply textures.
            if (texturingProperty != null)
            {
                mp.textures = ConfigureTextureProperties(texturingProperty);
            }

            return(mp);
        }