private string printMaterial(NiMaterialProperty material, NiTexturingProperty texture)
        {
            if (mtlExport.Count > 0)
            {
                mtlExport.Add(Environment.NewLine);
            }

            string name = material.Name.ToString();

            mtlExport.Add("newmtl " + name);

            // Ambient Color
            mtlExport.Add(string.Format("Ka {0} {1} {2}", material.AmbientColor.Red, material.AmbientColor.Green, material.AmbientColor.Blue));
            // Diffuse Color
            mtlExport.Add(string.Format("Kd {0} {1} {2}", material.DiffuseColor.Red, material.DiffuseColor.Green, material.DiffuseColor.Blue));
            // Specular Color
            mtlExport.Add(string.Format("Ks {0} {1} {2}", material.SpecularColor.Red, material.SpecularColor.Green, material.SpecularColor.Blue));
            // Transparency
            mtlExport.Add(string.Format("d {0}", material.Alpha));
            //mtlExport.Add(string.Format("Tr {0}", material.Alpha));

            printTexture(texture);

            string export = "# Material" + Environment.NewLine;

            export += "usemtl " + name + Environment.NewLine;
            return(export);
        }
        MaterialTextures ConfigureTextureProperties(NiTexturingProperty ntp)
        {
            var tp = new MaterialTextures();

            if (ntp.TextureCount < 1)
            {
                return(tp);
            }
            if (ntp.BaseTexture != null)
            {
                var src = (NiSourceTexture)_obj.Blocks[ntp.BaseTexture.source.Value]; tp.MainFilePath = src.FileName;
            }
            if (ntp.DarkTexture != null)
            {
                var src = (NiSourceTexture)_obj.Blocks[ntp.DarkTexture.source.Value]; tp.DarkFilePath = src.FileName;
            }
            if (ntp.DetailTexture != null)
            {
                var src = (NiSourceTexture)_obj.Blocks[ntp.DetailTexture.source.Value]; tp.DetailFilePath = src.FileName;
            }
            if (ntp.GlossTexture != null)
            {
                var src = (NiSourceTexture)_obj.Blocks[ntp.GlossTexture.source.Value]; tp.GlossFilePath = src.FileName;
            }
            if (ntp.GlowTexture != null)
            {
                var src = (NiSourceTexture)_obj.Blocks[ntp.GlowTexture.source.Value]; tp.GlowFilePath = src.FileName;
            }
            if (ntp.BumpMapTexture != null)
            {
                var src = (NiSourceTexture)_obj.Blocks[ntp.BumpMapTexture.source.Value]; tp.BumpFilePath = src.FileName;
            }
            return(tp);
        }
Beispiel #3
0
 public ShapeDesc()
 {
     this.name                = "";
     this.staticName          = "";
     this.staticModel         = "";
     this.geometry            = null;
     this.shaderType          = "";
     this.effectShader        = null;
     this.lightingShader      = null;
     this.texturingProperty   = null;
     this.materialProperty    = null;
     this.sourceTextureBase   = null;
     this.sourceTextureDetail = null;
     this.sourceTextureGlow   = null;
     this.sourceTextureBump   = null;
     this.shaderHash          = "";
     this.shapeHash           = "";
     this.material            = "";
     this.textures            = new string[10] {
         "", "", "", "", "", "", "", "", "", ""
     };
     this.textures[0]      = "textures\\default.dds";
     this.textures[1]      = "textures\\default_n.dds";
     this.isPassThru       = false;
     this.isGroup          = false;
     this.isHighDetail     = false;
     this.hasVertexColor   = false;
     this.allWhite         = false;
     this.isDoubleSided    = false;
     this.isAlpha          = false;
     this.isDecal          = false;
     this.enableParent     = 0;
     this.TextureClampMode = 0;
     this.boundingBox      = new BBox();
     this.x           = new float();
     this.y           = new float();
     this.segments    = new List <SegmentDesc>();
     this.translation = new Vector3(0f, 0f, 0f);
     this.rotation    = new Matrix33(true);
     this.scale       = 1f;
 }
        private void printTexture(NiTexturingProperty texture)
        {
            NiSourceTexture source = texture.File.ObjectsByRef.Where(o => o.Key == texture.BaseTexture.Source.RefId).First().Value as NiSourceTexture;

            string fileName = source.FileName.ToString().ToLower();

            string offset = string.Format("-o {0} {1}", texture.BaseTexture.CenterOffset.X, texture.BaseTexture.CenterOffset.Y);

            mtlExport.Add("# original file " + fileName);
            mtlExport.Add(string.Format("map_Ka {0}.tga", Path.GetFileNameWithoutExtension(fileName), offset));
            mtlExport.Add(string.Format("map_Kd {0}.tga", Path.GetFileNameWithoutExtension(fileName), offset));
            //mtlExport.Add(string.Format("map_Ks {0}.tga", Path.GetFileNameWithoutExtension(fileName), offset));
            textures.Add(fileName);

            if (texture.BumpMapTexture != null)
            {
                NiSourceTexture bumbTexture = texture.File.ObjectsByRef.Where(o => o.Key == texture.BumpMapTexture.Source.RefId).First().Value as NiSourceTexture;
                mtlExport.Add(string.Format("map_bump {0}.tga", Path.GetFileNameWithoutExtension(bumbTexture.FileName.ToString().ToLower())));
                textures.Add(bumbTexture.FileName.ToString());
            }
        }
Beispiel #5
0
        private MWMaterialTextures ConfigureTextureProperties(NiTexturingProperty ntp)
        {
            MWMaterialTextures tp = new MWMaterialTextures();

            if (ntp.textureCount < 1)
            {
                return(tp);
            }
            if (ntp.hasBaseTexture)
            {
                NiSourceTexture src = ( NiSourceTexture )file.blocks[ntp.baseTexture.source.value];
                tp.mainFilePath = src.fileName;
            }
            if (ntp.hasDarkTexture)
            {
                NiSourceTexture src = ( NiSourceTexture )file.blocks[ntp.darkTexture.source.value];
                tp.darkFilePath = src.fileName;
            }
            if (ntp.hasDetailTexture)
            {
                NiSourceTexture src = ( NiSourceTexture )file.blocks[ntp.detailTexture.source.value];
                tp.detailFilePath = src.fileName;
            }
            if (ntp.hasGlossTexture)
            {
                NiSourceTexture src = ( NiSourceTexture )file.blocks[ntp.glossTexture.source.value];
                tp.glossFilePath = src.fileName;
            }
            if (ntp.hasGlowTexture)
            {
                NiSourceTexture src = ( NiSourceTexture )file.blocks[ntp.glowTexture.source.value];
                tp.glowFilePath = src.fileName;
            }
            if (ntp.hasBumpMapTexture)
            {
                NiSourceTexture src = ( NiSourceTexture )file.blocks[ntp.bumpMapTexture.source.value];
                tp.bumpFilePath = src.fileName;
            }
            return(tp);
        }
Beispiel #6
0
    static private String[] getTextureIds(NIFFile nf, NiMesh mesh)
    {
        String[]            textureType;
        NiTexturingProperty texturingProperty = mesh.getTexturingProperty(nf);

        if (texturingProperty != null)
        {
            //Debug.Log("found texturing property for mesh " + mesh.name);
            textureType = new String[texturingProperty.shaderMapList.Count];
            foreach (int extraID in mesh.extraDataIDs)
            {
                NIFObject ni = nf.getObject(extraID);
                if (ni is NiIntegerExtraData)
                {
                    NiIntegerExtraData nied = (NiIntegerExtraData)ni;
                    if (nied.extraDataString != null)
                    {
                        if (nied.extraDataString.Contains("Texture"))
                        {
                            if (nied.intExtraData >= 0 && nied.intExtraData < textureType.Length)
                            {
                                textureType[nied.intExtraData] = nied.extraDataString;
                            }
                            else
                            {
                                //                                Debug.LogWarning("nied.intExtraData out of range:" + nied.intExtraData + " => " + textureType.Length);
                            }
                        }
                    }
                }
            }
        }
        else
        {
            textureType = new String[255];
        }
        return(textureType);
    }
Beispiel #7
0
 public ShapeDesc(ShapeDesc shapeDesc)
 {
     this.name                = shapeDesc.name;
     this.staticName          = shapeDesc.name;
     this.staticModel         = shapeDesc.name;
     this.geometry            = null;
     this.shaderType          = shapeDesc.shaderType;
     this.effectShader        = shapeDesc.effectShader;
     this.lightingShader      = shapeDesc.lightingShader;
     this.texturingProperty   = shapeDesc.texturingProperty;
     this.materialProperty    = shapeDesc.materialProperty;
     this.sourceTextureBase   = shapeDesc.sourceTextureBase;
     this.sourceTextureDetail = shapeDesc.sourceTextureDetail;
     this.sourceTextureGlow   = shapeDesc.sourceTextureGlow;
     this.sourceTextureBump   = shapeDesc.sourceTextureBump;
     this.shaderHash          = shapeDesc.shaderHash;
     this.shapeHash           = shapeDesc.shapeHash;
     this.material            = shapeDesc.material;
     this.textures            = shapeDesc.textures;
     this.isPassThru          = shapeDesc.isPassThru;
     this.isGroup             = shapeDesc.isGroup;
     this.isHighDetail        = shapeDesc.isHighDetail;
     this.hasVertexColor      = shapeDesc.hasVertexColor;
     this.allWhite            = shapeDesc.allWhite;
     this.isDoubleSided       = shapeDesc.isDoubleSided;
     this.isAlpha             = shapeDesc.isAlpha;
     this.isDecal             = shapeDesc.isDecal;
     this.enableParent        = shapeDesc.enableParent;
     this.TextureClampMode    = shapeDesc.TextureClampMode;
     this.boundingBox         = new BBox();
     this.x           = shapeDesc.x;
     this.y           = shapeDesc.y;
     this.segments    = new List <SegmentDesc>();
     this.translation = shapeDesc.translation;
     this.rotation    = shapeDesc.rotation;
     this.scale       = shapeDesc.scale;
 }
        MaterialProps NiAVObjectPropertiesToMaterialProperties(NiAVObject obj)
        {
            // Find relevant properties.
            NiTexturingProperty texturingProperty = null;
            //NiMaterialProperty materialProperty = null;
            NiAlphaProperty alphaProperty = null;

            foreach (var propRef in obj.Properties)
            {
                var prop = _obj.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.
            var mp = new MaterialProps();

            if (alphaProperty != null)
            {
                #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
                var flags    = alphaProperty.Flags;
                var oldflags = flags;
                var srcbm    = (byte)(BitConverter.GetBytes(flags >> 1)[0] & 15);
                var 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);
        }
Beispiel #9
0
        public ShapeDesc(String gameDir, NiFile file, NiTriBasedGeom geom, StaticDesc stat, int quadIndex, StringList PassThruMeshList, bool skyblivionTexPath, bool useOptimizer, bool fixTangents, bool useDecalFlag, bool terrain, bool verbose, LogFile logFile)
        {
            this.name = "";
            if (geom.GetNameIndex() != -1)
            {
                this.name = file.GetStringAtIndex(geom.GetNameIndex());
            }
            else
            {
                this.name = geom.GetName();
            }
            if (this.name == null)
            {
                this.name = "";
            }
            this.staticName = stat.staticName;
            if (stat.staticModels != null && quadIndex < stat.staticModels.Count())
            {
                this.staticModel = stat.staticModels[quadIndex].ToLower(CultureInfo.InvariantCulture);
            }
            else
            {
                this.staticModel = "";
            }
            this.geometry            = null;
            this.shaderType          = "";
            this.effectShader        = null;
            this.lightingShader      = null;
            this.texturingProperty   = null;
            this.materialProperty    = null;
            this.sourceTextureBase   = null;
            this.sourceTextureDetail = null;
            this.sourceTextureGlow   = null;
            this.sourceTextureBump   = null;
            this.shaderHash          = "";
            this.shapeHash           = "";
            if (stat.materialName != null)
            {
                this.material = stat.materialName;
            }
            else
            {
                this.material = "";
            }
            if (quadIndex != 0 && this.material.ToLower().Contains("largeref"))
            {
                this.material = Regex.Replace(this.material, "-largeref", "", RegexOptions.IgnoreCase);
            }
            this.textures = new string[10] {
                "", "", "", "", "", "", "", "", "", ""
            };
            this.textures[0] = "textures\\default.dds";
            this.textures[1] = "textures\\default_n.dds";

            if (this.name == "LODGenPassThru" || this.material.ToLower(CultureInfo.InvariantCulture) == "passthru" || (PassThruMeshList != null && staticModel != "" && PassThruMeshList.Any(staticModel.Contains)))
            {
                this.isPassThru = true;
                this.material   = "passthru";
            }
            else
            {
                this.isPassThru = false;
            }
            if ((stat.staticFlags & 1) == 1)
            {
                this.isGroup = true;
            }
            this.isHighDetail     = false;
            this.hasVertexColor   = false;
            this.allWhite         = false;
            this.isDoubleSided    = false;
            this.isAlpha          = false;
            this.isDecal          = false;
            this.enableParent     = 0;
            this.TextureClampMode = 0;
            this.boundingBox      = new BBox();
            this.x           = new float();
            this.y           = new float();
            this.segments    = new List <SegmentDesc>();
            this.translation = new Vector3(stat.x, stat.y, stat.z);

            this.scale = stat.scale;

            Matrix33 matrix33_1 = new Matrix33(true);
            Matrix33 matrix33_2 = new Matrix33(true);
            Matrix33 matrix33_3 = new Matrix33(true);

            matrix33_1.SetRotationX(Utils.ToRadians(-stat.rotX));
            matrix33_2.SetRotationY(Utils.ToRadians(-stat.rotY));
            matrix33_3.SetRotationZ(Utils.ToRadians(-stat.rotZ));
            this.rotation = new Matrix33(true) * matrix33_1 * matrix33_2 * matrix33_3;

            try
            {
                if (geom.GetClassName() == "NiTriStrips")
                {
                    int index = geom.GetData();
                    if (index == -1)
                    {
                        geometry = new Geometry();
                    }
                    else
                    {
                        geometry = new Geometry(new NiTriShapeData((NiTriStripsData)file.GetBlockAtIndex(index)));
                    }
                    List <int> extradatalist = geom.GetExtraData();
                    if (extradatalist.Count == 1)
                    {
                        NiBinaryExtraData extradata = (NiBinaryExtraData)file.GetBlockAtIndex(extradatalist[0]);
                        this.geometry.SetTangents(extradata.GetTangents());
                        this.geometry.SetBitangents(extradata.GetBitangents());
                    }
                }
                else
                {
                    int index = geom.GetData();
                    if (index == -1)
                    {
                        geometry = new Geometry();
                    }
                    else
                    {
                        if (file.GetBlockAtIndex(index).IsDerivedType("BSTriShape"))
                        {
                            BSTriShape bsts = (BSTriShape)file.GetBlockAtIndex(index);
                            geometry = bsts.GetGeom();
                        }
                        else
                        {
                            geometry = new Geometry((NiTriShapeData)file.GetBlockAtIndex(index));
                        }
                    }
                }
            }
            catch
            {
                logFile.WriteLog("Skipping non supported data " + this.staticModel + " " + this.name);
                geometry = new Geometry();
                return;
            }

            if (terrain)
            {
                return;
            }

            if (geometry.uvcoords.Count == 0)
            {
                logFile.WriteLog("Skipping no UV " + this.staticModel + " " + this.name);
                geometry = new Geometry();
                return;
            }

            if (geometry.HasVertexColors())
            {
                this.allWhite = true;
                List <Color4> vertexColors = geometry.GetVertexColors();
                for (int index = 0; index < vertexColors.Count; index++)
                {
                    float r = vertexColors[index][0];
                    float g = vertexColors[index][1];
                    float b = vertexColors[index][2];
                    float a = vertexColors[index][3];
                    if (r < 0.9f || b < 0.9f || g < 0.9f)
                    {
                        this.allWhite = false;
                    }
                    if (this.isPassThru && Game.Mode != "merge5")
                    {
                        // if neither LOD flag is set, alpha is used on/off at 0.5f, nobody wants that
                        // HD snow shader uses alpha for something else
                        a = 1f;
                    }
                    vertexColors[index] = new Color4(r, g, b, a);
                }
                if (this.allWhite)
                {
                    geometry.SetVertexColors(new List <Color4>());
                }
                else if (this.isPassThru && Game.Mode != "merge5")
                {
                    geometry.SetVertexColors(vertexColors);
                }
            }
            this.hasVertexColor = geometry.HasVertexColors();
            try
            {
                this.shapeHash = Utils.GetHash(Utils.ObjectToByteArray(geometry.vertices));
            }
            catch
            {
                logFile.WriteLog("Can not get hash for vertices in " + this.staticModel + " block " + this.name);
                logFile.Close();
                System.Environment.Exit(3003);
            }
            if ((file.GetVersion() > 335544325U) && (file.GetUserVersion() > 11U))
            {
                for (int index = 0; index < 2; index++)
                {
                    if (geom.GetBSProperty(index) != -1)
                    {
                        string type = file.GetBlockAtIndex(geom.GetBSProperty(index)).GetClassName().ToLower(CultureInfo.InvariantCulture);
                        if (type == "nialphaproperty")
                        {
                            if (Game.Mode == "fo4" || Game.Mode == "merge5")
                            {
                                this.isAlpha = true;
                                NiAlphaProperty alphaProperty = (NiAlphaProperty)file.GetBlockAtIndex(geom.GetBSProperty(index));
                                this.alphaThreshold = alphaProperty.GetThreshold();
                            }
                        }
                        else if (type == "bseffectshaderproperty")
                        {
                            this.shaderType = type;
                            BSEffectShaderProperty shader = (BSEffectShaderProperty)file.GetBlockAtIndex(geom.GetBSProperty(index));
                            this.textures[0] = shader.GetSourceTexture().ToLower(CultureInfo.InvariantCulture);
                            // disable non supported flags
                            if (Game.Mode != "merge5")
                            {
                                shader.SetShaderFlags1(shader.GetShaderFlags1() & 3724541045);
                                shader.SetShaderFlags2(shader.GetShaderFlags2() & 3758063615);
                            }
                            shader.SetSourceTexture("");
                            this.effectShader = shader;
                            if (this.hasVertexColor)
                            {
                                shader.SetShaderFlags2(shader.GetShaderFlags2() | 32);
                            }
                            else
                            {
                                shader.SetShaderFlags2(shader.GetShaderFlags2() & 4294967263);
                            }
                            this.isDoubleSided    = (shader.GetShaderFlags2() & 16) == 16;
                            this.TextureClampMode = shader.GetTextureClampMode();
                            shader.SetTextureClampMode(3);
                            try
                            {
                                this.shaderHash = Utils.GetHash(Utils.ObjectToByteArray(shader));
                            }
                            catch
                            {
                                logFile.WriteLog("Can not get hash for shader in " + this.staticModel + " block " + this.name);
                                logFile.Close();
                                System.Environment.Exit(3004);
                            }
                        }
                        else if (type == "bslightingshaderproperty")
                        {
                            this.shaderType = type;
                            BSLightingShaderProperty shader = (BSLightingShaderProperty)file.GetBlockAtIndex(geom.GetBSProperty(index));
                            // disable non supported flags
                            if (Game.Mode != "merge5")
                            {
                                //enviroment shader
                                if (shader.GetShaderType() == 1)
                                {
                                    if ((shader.GetShaderFlags2() & 64) == 64)
                                    {
                                        //glow shader
                                        shader.SetShaderType(2);
                                    }
                                    shader.SetShaderType(0);
                                }
                                //3695270121
                                shader.SetShaderFlags1(shader.GetShaderFlags1() & 3724541045);
                                shader.SetShaderFlags2(shader.GetShaderFlags2() & 3758063615);
                            }
                            this.TextureClampMode = shader.GetTextureClampMode();
                            shader.SetTextureClampMode(3);
                            this.isDoubleSided = (shader.GetShaderFlags2() & 16) == 16;
                            if ((Game.Mode == "tes5" || Game.Mode == "sse") && useDecalFlag && !this.isPassThru && ((shader.GetShaderFlags1() & 67108864) == 67108864 || (shader.GetShaderFlags1() & 134217728) == 134217728))
                            {
                                this.isDecal = true;
                                // SLSF1_Decal
                                shader.SetShaderFlags1(shader.GetShaderFlags1() | 67108864);
                                // SLSF1_Dynamic_Decal
                                shader.SetShaderFlags1(shader.GetShaderFlags1() | 134217728);
                                // SLSF2_No_Fade
                                shader.SetShaderFlags2(shader.GetShaderFlags2() | 8);
                            }
                            if (this.hasVertexColor)
                            {
                                shader.SetShaderFlags2(shader.GetShaderFlags2() | 32);
                            }
                            else
                            {
                                shader.SetShaderFlags2(shader.GetShaderFlags2() & 4294967263);
                            }
                            // no specular flag -> reset specular strength, color, glossiness
                            //if ((shader.GetShaderFlags1() & 1) == 0 || shader.GetSpecularStrength() == 0f || (shader.GetGlossiness() == 80f && shader.GetSpecularColor() == new Color3(0f, 0f, 0f) && shader.GetSpecularStrength() == 1f))
                            //{
                            shader.SetShaderFlags1(shader.GetShaderFlags1() & 4294967294);
                            shader.SetGlossiness(80f);
                            shader.SetSpecularColor(new Color3(0f, 0f, 0f));
                            shader.SetSpecularStrength(1f);
                            //}
                            // no soft/rim/back/effect lighting -> reset effect lighting 1 and 2
                            if ((shader.GetShaderFlags2() & 1308622848) == 0 || (shader.GetLightingEffect1() < 0.1f && shader.GetLightingEffect2() < 0.1f))
                            {
                                shader.SetShaderFlags2(shader.GetShaderFlags2() & 2986344447);
                                shader.SetLightingEffect1(0f);
                                shader.SetLightingEffect2(0f);
                            }
                            // no own emit -> reset own emit, emissive color, muliplier
                            if ((shader.GetShaderFlags1() & 4194304) == 0 || (shader.GetEmissiveColor()[0] < 0.1f && shader.GetEmissiveColor()[1] < 0.1f && shader.GetEmissiveColor()[2] < 0.1f) || shader.GetEmissiveMultiple() == 0f)
                            {
                                shader.SetShaderFlags1(shader.GetShaderFlags1() & 4290772991);
                                shader.SetEmissiveColor(new Color3(0f, 0f, 0f));
                                shader.SetEmissiveMultiple(1f);
                            }
                            this.backlightPower = shader.GetBacklightPower();
                            if (shader.GetTextureSet() != -1 && this.textures[0] == "textures\\default.dds")
                            {
                                BSShaderTextureSet shaderTextureSet = (BSShaderTextureSet)file.GetBlockAtIndex(shader.GetTextureSet());
                                for (int index2 = 0; index2 < shaderTextureSet.GetNumTextures(); ++index2)
                                {
                                    this.textures[index2] = shaderTextureSet.GetTexture(index2).ToLower(CultureInfo.InvariantCulture);
                                }
                            }
                            shader.SetTextureSet(-1);
                            //BGSM takes priority and overwrite everything in nif
                            if (shader.GetNameIndex() != -1)
                            {
                                string bgsmFileName = file.GetStringAtIndex(shader.GetNameIndex()).ToLower(CultureInfo.InvariantCulture);
                                if (bgsmFileName.Contains(".bgsm"))
                                {
                                    int i = bgsmFileName.IndexOf("\\data\\");
                                    if (i > 0)
                                    {
                                        i           += 6;
                                        bgsmFileName = bgsmFileName.Substring(i, bgsmFileName.Length - i);
                                    }
                                    if (stat.materialSwap.ContainsKey(bgsmFileName))
                                    {
                                        bgsmFileName = stat.materialSwap[bgsmFileName];
                                    }
                                    BGSMFile bgsmdata = new BGSMFile();
                                    bgsmdata.Read(gameDir, bgsmFileName, logFile);
                                    if (bgsmdata.textures[0] != "")
                                    {
                                        this.textures[0]      = "textures\\" + bgsmdata.textures[0];
                                        this.textures[1]      = "textures\\" + bgsmdata.textures[1];
                                        this.textures[7]      = "textures\\" + bgsmdata.textures[2];
                                        this.TextureClampMode = bgsmdata.textureClampMode;
                                        shader.SetTextureClampMode(this.TextureClampMode);
                                        this.isAlpha       = Convert.ToBoolean(bgsmdata.alphaFlag);
                                        this.isDoubleSided = Convert.ToBoolean(bgsmdata.doubleSided);
                                        shader.SetShaderFlags2(shader.GetShaderFlags2() | 16);
                                        this.alphaThreshold = bgsmdata.alphaThreshold;
                                        this.backlightPower = bgsmdata.backlightPower;
                                        shader.SetBacklightPower(this.backlightPower);
                                    }
                                }
                            }
                            try
                            {
                                this.shaderHash = Utils.GetHash(Utils.ObjectToByteArray(shader));
                            }
                            catch
                            {
                                logFile.WriteLog("Can not get hash for shader in " + this.staticModel + " block " + this.name);
                                logFile.Close();
                                System.Environment.Exit(3005);
                            }
                            this.lightingShader = shader;
                        }
                        else
                        {
                            if (this.shaderType == "")
                            {
                                this.shaderType = type;
                            }
                        }
                    }
                }
            }
            else
            {
                this.shaderType = "none";
                for (int index = 0; index < geom.GetNumProperties(); ++index)
                {
                    NiProperty niProperty = (NiProperty)file.GetBlockAtIndex(geom.GetProperty(index));
                    string     type       = niProperty.GetClassName().ToLower(CultureInfo.InvariantCulture);
                    if (niProperty.GetType() == typeof(BSShaderPPLightingProperty))
                    {
                        BSShaderPPLightingProperty shader           = (BSShaderPPLightingProperty)file.GetBlockAtIndex(geom.GetProperty(index));
                        BSShaderTextureSet         shaderTextureSet = (BSShaderTextureSet)file.GetBlockAtIndex(shader.GetTextureSet());
                        for (int index2 = 0; index2 < shaderTextureSet.GetNumTextures(); ++index2)
                        {
                            this.textures[index2] = shaderTextureSet.GetTexture(index2).ToLower(CultureInfo.InvariantCulture);
                        }
                        //this.hasVertexColor = (shader.GetShaderFlags2() & 32) == 32;
                        //this.isDoubleSided = (shader.GetShaderFlags2() & 16) == 16;
                        //this.TextureClampMode = shader.GetTextureClampMode();
                        this.shaderType = type;
                        break;
                    }
                    if (niProperty.GetType() == typeof(NiMaterialProperty))
                    {
                        this.materialProperty = (NiMaterialProperty)niProperty;
                        this.hasVertexColor   = this.geometry.HasVertexColors();
                    }
                    if (niProperty.GetType() == typeof(NiTexturingProperty))
                    {
                        this.texturingProperty = (NiTexturingProperty)niProperty;
                        string str1 = "textures\\defaultdiffuse.dds";
                        string str2 = "textures\\default_n.dds";
                        if (this.texturingProperty != null)
                        {
                            TexDesc baseTexture = this.texturingProperty.GetBaseTexture();
                            if (this.texturingProperty.HasBaseTexture() && this.texturingProperty.GetBaseTexture().source != -1)
                            {
                                this.sourceTextureBase = (NiSourceTexture)file.GetBlockAtIndex(this.texturingProperty.GetBaseTexture().source);
                                str1 = this.sourceTextureBase.GetFileName().ToLower(CultureInfo.InvariantCulture);
                            }
                            if (this.texturingProperty.HasDarkTexture() && this.texturingProperty.GetDarkTexture().source != -1)
                            {
                                if (verbose)
                                {
                                    logFile.WriteLog("dark texture " + this.staticName);
                                }
                            }
                            if (this.texturingProperty.HasDetailTexture() && this.texturingProperty.GetDetailTexture().source != -1)
                            {
                                if (verbose)
                                {
                                    logFile.WriteLog("detail texture " + this.staticName);
                                }
                                this.sourceTextureDetail = (NiSourceTexture)file.GetBlockAtIndex(this.texturingProperty.GetDetailTexture().source);
                                str1 = this.sourceTextureDetail.GetFileName().ToLower(CultureInfo.InvariantCulture);
                            }
                            if (this.texturingProperty.HasGlossTexture() && this.texturingProperty.GetGlossTexture().source != -1)
                            {
                                if (verbose)
                                {
                                    logFile.WriteLog("gloss texture " + this.staticName);
                                }
                            }
                            if (this.texturingProperty.HasGlowTexture() && this.texturingProperty.GetGlowTexture().source != -1)
                            {
                                if (verbose)
                                {
                                    logFile.WriteLog("glow texture " + this.staticName);
                                }
                                this.sourceTextureGlow = (NiSourceTexture)file.GetBlockAtIndex(this.texturingProperty.GetGlowTexture().source);
                            }
                            if (this.texturingProperty.HasBumpMapTexture() && this.texturingProperty.GetBumpMapTexture().source != -1)
                            {
                                if (verbose)
                                {
                                    logFile.WriteLog("bump texture " + this.staticName);
                                }
                                this.sourceTextureBump = (NiSourceTexture)file.GetBlockAtIndex(this.texturingProperty.GetBumpMapTexture().source);
                            }
                            if (this.texturingProperty.HasDecalTexture0() && this.texturingProperty.GetDecalTexture0().source != -1)
                            {
                                if (verbose)
                                {
                                    logFile.WriteLog("decal texture " + this.staticName);
                                }
                            }
                            str2 = Utils.GetNormalTextureName(str1);
                            if (skyblivionTexPath && !str1.Contains("textures\\tes4"))
                            {
                                str1 = str1.ToLower().Replace("textures", "textures\\tes4");
                                str2 = str2.ToLower().Replace("textures", "textures\\tes4");
                            }
                        }
                        this.textures[0] = str1.ToLower(CultureInfo.InvariantCulture);
                        this.textures[1] = str2.ToLower(CultureInfo.InvariantCulture);
                        if (this.texturingProperty.HasBaseTexture())
                        {
                            this.TextureClampMode = this.texturingProperty.GetBaseTexture().clampMode;
                        }
                        this.shaderType = type;
                        break;
                    }
                }
            }

            for (int index = 0; index < this.textures.Length; index++)
            {
                this.textures[index] = this.textures[index].Trim();
                if (this.textures[index].Contains(".dds") && !this.textures[index].Contains("textures\\"))
                {
                    this.textures[index] = Path.Combine("textures\\", this.textures[index]);
                }
            }

            if (((Game.Mode == "convert4" || Game.Mode == "convert5") && useOptimizer && AtlasList.Contains(this.textures[0])) || this.textures[0] == "textures\\grid.dds")
            {
                string texture = this.textures[0];
                geometry      = geometry.ReUV(this, texture, logFile, verbose);
                this.textures = new string[10] {
                    "", "", "", "", "", "", "", "", "", ""
                };
                this.textures[0] = AtlasList.Get(texture).AtlasTexture;
                this.textures[1] = AtlasList.Get(texture).AtlasTextureN;
                if (this.textures[2] != "")
                {
                    this.textures[2] = AtlasList.Get(texture).AtlasTexture;
                }
                if (Game.Mode == "fo4")
                {
                    this.textures[7] = AtlasList.Get(texture).AtlasTextureS;
                }
                this.TextureClampMode = 0U;
                this.isHighDetail     = false;
            }
            else if ((Game.Mode == "convert4" || Game.Mode == "convert5") && useOptimizer && verbose)
            {
                logFile.WriteLog(this.staticModel + " " + this.name + " " + this.textures[0] + " not in atlas file");
            }
            if (!geometry.HasNormals())
            {
                geometry.FaceNormals();
                geometry.SmoothNormals(60f, 0.001f);
            }
            if (fixTangents && !geometry.HasTangents())
            {
                geometry.UpdateTangents(fixTangents);
            }
        }
        private string ParseShape(NiTriShape shape)
        {
            if (!shape.Data.IsValid())
            {
                return("");
            }

            NiTriShapeData geometry = (NiTriShapeData)shape.Data.Object;

            // The final text
            List <string> export = new List <string>();

            Matrix transformationMatrix = ComputeWorldMatrix(shape);

            // Set Object name
            export.Add("g Shape " + shape.Name + Environment.NewLine);

            NiMaterialProperty  material = null;
            NiTexturingProperty texture  = null;

            foreach (NiRef <NiProperty> property in shape.Properties)
            {
                if (property.Object is NiMaterialProperty)
                {
                    material = property.Object as NiMaterialProperty;
                }
                if (property.Object is NiTexturingProperty)
                {
                    texture = property.Object as NiTexturingProperty;
                }
            }

            if (material != null && texture != null)
            {
                export.Add(printMaterial(material, texture));
            }

            // Verticles (v)
            if (geometry.HasVertices && geometry.NumVertices >= 3)
            {
                export.Add(printVertices(geometry.Vertices, transformationMatrix));
            }

            // Texture coordinates (vt)
            if (geometry.UVSets.Length > 0)
            {
                export.Add(printUvSets(geometry.UVSets));
            }

            // Normals (vn)
            if (geometry.HasNormals)
            {
                export.Add(printNormals(geometry.Normals, transformationMatrix));
            }

            // Parameter space vertices (vp)

            // Face Definitions (f)
            export.Add(printTriangles(geometry.Triangles, (geometry.UVSets.Length > 0)));

            return(string.Join(Environment.NewLine, export));
        }
Beispiel #11
0
    static Material doMaterials(NIFFile nf, NiMesh mesh, GameObject go)
    {
        StringBuilder strB = new StringBuilder(20);

        if (standardShader == null)
        {
            standardShader = Shader.Find("Standard");
        }

        bool     IS_TERRAIN     = (nf.getStringTable().Contains("terrainL1"));
        bool     animated       = false;
        bool     presetMaterial = false;
        string   materialName   = null;
        Material mat            = null;


        Material mat2 = null;

        if (mesh.materialNames.Count > 0)
        {
            strB.Length = 0;
            strB.Append("materials/");
            strB.Append(mesh.materialNames[0]);
            mat2 = Resources.Load <Material>(strB.ToString());
            if (mat2 != null)
            {
                mat = Material.Instantiate <Material>(mat2);
            }
        }
        else
        {
            Debug.LogWarning("No mesh materials found in mesh :" + mesh.name);
        }

        if (mat == null)
        {
            // do materials/textures

            if (IS_TERRAIN)
            {
                materialName = "terrainmat";
            }

            if (mesh.materialNames.Contains("Ocean_Water_Shader") || mesh.materialNames.Contains("Flow_Water") || mesh.name.Contains("water_UP") || mesh.name.Contains("water_DOWN"))
            {
                materialName = "WaterMaterial";
            }

            bool alpha = (mesh.materialNames.Contains("TwoSided_Alpha_Specular") || mesh.materialNames.Contains("Lava_Flow_Decal"));
            foreach (string n in mesh.materialNames)
            {
                if (n.ToLower().Contains("alpha"))
                {
                    alpha = true;
                }
            }
            if (alpha)
            {
                materialName = "2sidedtransmat_fade";
            }

            // handle some simple animated "scrolling" textures
            animated = (mesh.materialNames.Contains("Additive_UVScroll_Distort") || mesh.materialNames.Contains("Lava_Flow_Decal") || mesh.materialNames.Contains("Local_Cloud_Flat") ||
                        mesh.materialNames.Contains("Alpha_UVScroll_Overlay_Foggy_Waterfall") || mesh.materialNames.Contains("Fat_spike12_m") || mesh.materialNames.Contains("pPlane1_m"));


            if (animated)
            {
                materialName = "2sidedtransmat_fade";
            }



            foreach (int eid in mesh.extraDataIDs)
            {
                NIFObject obj = nf.getObject(eid);
                if (obj is NiBooleanExtraData)
                {
                    NiBooleanExtraData fExtra = (NiBooleanExtraData)obj;
                    switch (fExtra.extraDataString)
                    {
                    case "doAlphaTest":
                        if (fExtra.booleanData)
                        {
                            materialName = "2sidedtransmat";
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            if (materialName == null)
            {
                if (standardMaterial == null)
                {
                    standardMaterial = new Material(standardShader);
                }
                mat          = Material.Instantiate(standardMaterial);
                materialName = standardMaterial.name;
            }
            else
            {
                mat = Material.Instantiate(Resources.Load <Material>(materialName));
            }
            //Debug.Log("Using guessed material[" + materialName + "] for " + mesh.name + " from list of materials: " + string.Join(",", mesh.materialNames.ToArray()), go);
        }
        else if (mat2 != null)
        {
            materialName   = mat2.name;
            presetMaterial = true;
            //Debug.Log("Using actual material[" + materialName + "] for " + mesh.name + " from list of materials: " + string.Join(",", mesh.materialNames.ToArray()), go);
        }
        else
        {
            Debug.LogWarning("No material found!?");
        }
#if UNITY_EDITOR
        MeshOriginalMaterial mom = go.AddComponent <MeshOriginalMaterial>();
        if (mesh.materialNames.Count > 0)
        {
            mom.materialName = mesh.materialNames[0];
        }
#endif

        if (presetMaterial)
        {
            foreach (int extraId in mesh.extraDataIDs)
            {
                NIFObject obj = nf.getObject(extraId);
                setMaterialProperty(mat, obj);
            }

            if (mat.HasProperty("doAlphaTest"))
            {
                if (mat.GetInt("doAlphaTest") == 0)
                {
                    strB.Length = 0;
                    strB.Append("materials/");
                    strB.Append(mat2.name);
                    strB.Append("_shader_opaque");

                    string shaderName = strB.ToString();
                    //Debug.Log("loading opaque shader:" + shaderName, go);
                    Shader shader = Resources.Load <Shader>(shaderName);
                    if (shader != null)
                    {
                        mat.shader = shader;
                    }
                }
            }
        }

        mat.enableInstancing = true;
        mat.EnableKeyword("_SPECULARHIGHLIGHTS_OFF");



        if (animated)
        {
            NiFloatsExtraData extra = getFloatsExtraData(nf, mesh, "tex0ScrollRate");
            if (extra != null)
            {
                UVScroll scroller = go.AddComponent <UVScroll>();
                scroller.material = mat;

                scroller.xRate = extra.floatData[0];
                scroller.yRate = extra.floatData[1];
            }
        }

        foreach (int eid in mesh.extraDataIDs)
        {
            NIFObject obj = nf.getObject(eid);
            if (obj is NiFloatExtraData)
            {
                NiFloatExtraData fExtra = (NiFloatExtraData)obj;
                switch (fExtra.extraDataString)
                {
                case "scaleY":
                    if (mat.HasProperty("_MainTex"))
                    {
                        mat.mainTextureScale = new Vector2(mat.mainTextureScale.x, fExtra.floatData);
                    }
                    else
                    {
                        if (mat.HasProperty("scaleY"))
                        {
                            mat.SetFloat("scaleY", fExtra.floatData);
                        }
                        else
                        {
                            Debug.LogWarning("While trying to set scaleY, material[" + mat.name + "][" + materialName + "] doesn't have an appropriate texture property");
                        }
                    }
                    break;

                case "scale":
                    if (mat.HasProperty("_MainTex"))
                    {
                        mat.mainTextureScale = new Vector2(fExtra.floatData, mat.mainTextureScale.y);
                    }
                    else
                    {
                        if (mat.HasProperty("scale"))
                        {
                            mat.SetFloat("scale", fExtra.floatData);
                        }
                        else
                        {
                            Debug.LogWarning("While trying to set scale, material[" + mat.name + "][" + materialName + "] doesn't have an appropriate texture property");
                        }
                    }
                    break;

                default:
                    break;
                }
            }
        }

        string[] textureNameIds = getTextureIds(nf, mesh);

        if (presetMaterial)
        {
            foreach (int extraId in mesh.extraDataIDs)
            {
                NIFObject obj = nf.getObject(extraId);
                setMaterialProperty(mat, obj);
            }
        }

        if (mat.HasProperty("alphaTestRef"))
        {
            mat.SetFloat("alphaTestRef", 1.0f - mat.GetFloat("alphaTestRef"));
        }

        List <int> propIDs = mesh.nodePropertyIDs;
        foreach (int propID in propIDs)
        {
            NIFObject obj = nf.getObject(propID);

            if (obj is NiTexturingProperty)
            {
                NiTexturingProperty propObj = (NiTexturingProperty)obj;
                foreach (NifTexMap tex in propObj.texList)
                {
                    if (tex != null)
                    {
                        Debug.Log("\t" + tex.sourceTexLinkID);
                    }
                }

                int i = 0;
                foreach (NifTexMap tex in propObj.shaderMapList)
                {
                    string texName = "";
                    if (tex != null)
                    {
                        int sourceTexID = tex.sourceTexLinkID;
                        if (sourceTexID != -1)
                        {
                            NiSourceTexture sourceTex = (NiSourceTexture)nf.getObject(sourceTexID);
                            texName = sourceTex.texFilename;
                            if (presetMaterial)
                            {
                                strB.Length = 0;
                                if (IS_TERRAIN)
                                {
                                    strB.Append("_terrain");
                                    strB.Append(i);
                                }
                                else
                                {
                                    strB.Append("_");
                                    strB.Append(textureNameIds[i]);
                                }

                                // Debug.Log("attempt to set texture property :" + propertyName + " with texure:" + texName);

                                enqueSetTexture(mat, strB.ToString(), nf, texName);
                                //mat.SetTexture(propertyName, loadTexture(nf, texName));
                            }
                            else if (IS_TERRAIN)
                            {
                                strB.Length = 0;
                                strB.Append("_terrain");
                                strB.Append(i);
                                string param = strB.ToString();
                                //"_terrain" + i;
                                //Debug.Log("set " + param + " to " + texName + " mat:" + mat.name);
                                enqueSetTexture(mat, param, nf, texName);
                                //mat.SetTexture(param, loadTexture(nf, texName));
                            }
                            else
                            {
                                //Debug.Log("texName[" + texName + "]: id:" + textureNameIds[i]);
                                try
                                {
                                    switch (textureNameIds[i])
                                    {
                                    case "skyGradientTexture0":
                                    case "diffuseTexture":
                                    case "diffuseTextureXZ":
                                        enqueSetTexture(mat, "_MainTex", nf, texName);
                                        //mat.SetTexture("_MainTex", loadTexture(nf, texName));
                                        break;

                                    case "decalNormalTexture":
                                        enqueSetTexture(mat, "_DetailNormalMap", nf, texName);
                                        //mat.SetTexture("_DetailNormalMap", loadTexture(nf, texName));
                                        break;

                                    case "normalTexture":
                                        enqueSetTexture(mat, "_BumpMap", nf, texName);
                                        //mat.SetTexture("_BumpMap", loadTexture(nf, texName));
                                        break;

                                    case "glowTexture":
                                        mat.EnableKeyword("_EMISSION");
                                        if (mesh.materialNames.Contains("Lava_Flow_Decal"))
                                        {
                                            mat.SetColor("_EmissionColor", Color.red);
                                        }
                                        else
                                        {
                                            mat.SetColor("_EmissionColor", Color.white * 0.5f);
                                        }
                                        enqueSetTexture(mat, "_EmissionMap", nf, texName);
                                        //mat.SetTexture("_EmissionMap", loadTexture(nf, texName));
                                        break;

                                    case "glossTexture":
                                        enqueSetTexture(mat, "_MetallicGlossMap", nf, texName);
                                        //mat.SetTexture("_MetallicGlossMap", loadTexture(nf, texName));
                                        break;

                                    case "decalTexture":
                                    case "starMapTexture0":
                                        enqueSetTexture(mat, "_DetailAlbedoMap", nf, texName);
                                        //mat.SetTexture("_DetailAlbedoMap", loadTexture(nf, texName));
                                        break;

                                    default:
                                        //Debug.LogWarning("No shader material property for " + textureNameIds[i]);
                                        break;
                                    }
                                }catch (ArgumentOutOfRangeException ex)
                                {
                                    Debug.LogWarning("Texture id[" + i + "] was out of range of the texture name ids: " + textureNameIds.ToList());
                                    //mat.SetTexture("_MainTex", loadTexture(nf, texName));
                                    enqueSetTexture(mat, "_MainTex", nf, texName);
                                }
                            }
                        }
                    }
                    i++;
                }
            }
        }
        return(mat);
    }