/// <summary> /// Makes texture results for given material. /// </summary> /// <param name="material">Unity material.</param> /// <param name="archive">Texture archive.</param> /// <param name="record">Record index.</param> /// <returns>Results for the given material.</returns> public static GetTextureResults MakeResults(Material material, int archive, int record) { string path = Path.Combine(DaggerfallUnity.Instance.MaterialReader.TextureReader.Arena2Path, TextureFile.IndexToFileName(archive)); TextureFile textureFile = new TextureFile(path, FileUsage.UseMemory, true); return(new GetTextureResults() { albedoMap = GetTextureOrDefault(material, Uniforms.MainTex), normalMap = GetTextureOrDefault(material, Uniforms.BumpMap), emissionMap = GetTextureOrDefault(material, Uniforms.EmissionMap), singleRect = new Rect(0, 0, 1, 1), isWindow = ClimateSwaps.IsExteriorWindow(archive, record), isEmissive = material.HasProperty(Uniforms.EmissionMap), textureFile = textureFile }); }
/// <summary> /// Gets Unity Material from Daggerfall texture with more options. /// </summary> /// <param name="archive">Archive index.</param> /// <param name="record">Record index.</param> /// <param name="frame">Frame index.</param> /// <param name="rectOut">Receives UV rect for texture inside border.</param> /// <param name="border">Number of pixels internal border around each texture.</param> /// <param name="dilate">Blend texture into surrounding empty pixels.</param> /// <param name="shader">Shader for material. If null, DefaultShaderName will be applied.</param> /// <returns>Material or null.</returns> public Material GetMaterial( int archive, int record, int frame, int alphaIndex, out Rect rectOut, int border = 0, bool dilate = false, Shader shader = null) { // Ready check if (!IsReady) { rectOut = new Rect(); return(null); } // HACK: Override shader for unlit textures // TODO: Find a better way to do this if (archive == 356 && (record == 0 || record == 2 || record == 3) || archive == 87 && record == 0) { // Only override if not specified if (shader == null) { shader = Shader.Find(dfUnity.MaterialReader.DefaultUnlitTextureShaderName); } } int key = MakeTextureKey((short)archive, (byte)record, (byte)frame); if (materialDict.ContainsKey(key)) { CachedMaterial cm = materialDict[key]; if (cm.filterMode == MainFilterMode) { // Properties are the same rectOut = cm.singleRect; return(cm.material); } else { // Properties don't match, remove material and reload materialDict.Remove(key); } } if (shader == null) { shader = Shader.Find(DefaultShaderName); } Material material = new Material(shader); material.name = FormatName(archive, record); Texture2D texture = textureReader.GetTexture2D(archive, record, frame, alphaIndex, out rectOut, border, dilate); material.mainTexture = texture; material.mainTexture.filterMode = MainFilterMode; DFSize size = textureReader.TextureFile.GetSize(record); DFSize scale = textureReader.TextureFile.GetScale(record); DFSize offset = textureReader.TextureFile.GetOffset(record); Vector2[] recordSizes = new Vector2[1] { new Vector2(size.Width, size.Height) }; Vector2[] recordScales = new Vector2[1] { new Vector2(scale.Width, scale.Height) }; Vector2[] recordOffsets = new Vector2[1] { new Vector2(offset.Width, offset.Height) }; CachedMaterial newcm = new CachedMaterial() { key = key, keyGroup = 0, singleRect = rectOut, material = material, filterMode = MainFilterMode, isWindow = ClimateSwaps.IsExteriorWindow(archive, record), recordSizes = recordSizes, recordScales = recordScales, recordOffsets = recordOffsets, recordFrameCount = textureReader.TextureFile.GetFrameCount(record), }; materialDict.Add(key, newcm); return(material); }