public static void AssignAnimateTextureComponent(CachedMaterial[] cachedMaterials, GameObject go) { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; // Look for any animated textures in this material set for (int i = 0; i < cachedMaterials.Length; i++) { CachedMaterial cm = cachedMaterials[i]; int frameCount = cm.recordFrameCount; if (frameCount > 1) { // Add texture animation component Demo.AnimateTexture c = go.AddComponent <Demo.AnimateTexture>(); // Get texture for each frame Texture[] textures = new Texture[frameCount]; for (int frame = 0; frame < frameCount; frame++) { int archiveOut, recordOut, frameOut; MaterialReader.ReverseTextureKey(cm.key, out archiveOut, out recordOut, out frameOut, cm.keyGroup); textures[frame] = dfUnity.MaterialReader.GetMaterial(archiveOut, recordOut, frame).mainTexture; } // Assign animation properties c.TargetMaterial = cm.material; c.TextureArray = textures; } } }
public static void AssignAnimatedMaterialComponent(CachedMaterial[] cachedMaterials, GameObject go) { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; // Look for any animated textures in this material set for (int i = 0; i < cachedMaterials.Length; i++) { CachedMaterial cm = cachedMaterials[i]; int frameCount = cm.singleFrameCount; if (frameCount > 1) { // Add texture animation component AnimatedMaterial c = go.AddComponent<AnimatedMaterial>(); // Store material for each frame CachedMaterial[] materials = new CachedMaterial[frameCount]; for (int frame = 0; frame < frameCount; frame++) { int archiveOut, recordOut, frameOut; MaterialReader.ReverseTextureKey(cm.key, out archiveOut, out recordOut, out frameOut, cm.keyGroup); dfUnity.MaterialReader.GetCachedMaterial(archiveOut, recordOut, frame, out materials[frame]); } // Assign animation properties c.TargetMaterial = cm.material; c.AnimationFrames = materials; } } }
public static void AssignAnimatedMaterialComponent(CachedMaterial[] cachedMaterials, GameObject go) { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; // Look for any animated textures in this material set for (int i = 0; i < cachedMaterials.Length; i++) { CachedMaterial cm = cachedMaterials[i]; int frameCount = cm.singleFrameCount; if (frameCount > 1) { // Add texture animation component AnimatedMaterial c = go.AddComponent <AnimatedMaterial>(); // Store material for each frame CachedMaterial[] materials = new CachedMaterial[frameCount]; for (int frame = 0; frame < frameCount; frame++) { int archiveOut, recordOut, frameOut; MaterialReader.ReverseTextureKey(cm.key, out archiveOut, out recordOut, out frameOut, cm.keyGroup); dfUnity.MaterialReader.GetCachedMaterial(archiveOut, recordOut, frame, out materials[frame]); } // Assign animation properties c.TargetMaterial = cm.material; c.AnimationFrames = materials; } } }
public static Material[] GetMaterialArray(CachedMaterial[] cachedMaterials) { // Extract a material array from cached material array Material[] materials = new Material[cachedMaterials.Length]; for (int i = 0; i < cachedMaterials.Length; i++) { materials[i] = cachedMaterials[i].material; } return materials; }
/// <summary> /// Import and set custom material on Enemy unit. /// </summary> /// <param name="go">Enemy Mobile Unit.</param> /// <param name="archive">Archive which contains all textures except the dead enemy.</param> /// <param name="textures">All textures for this enemy (except dead texture).</param> static public void SetupCustomEnemyMaterial(GameObject go, int archive, out List <List <Texture2D> > textures) { var meshRenderer = go.GetComponent <MeshRenderer>(); MaterialReader materialReader = DaggerfallUnity.Instance.MaterialReader; int record = enemyDefaultRecord, frame = enemyDefaultFrame; // Update UV map SetUv(go.GetComponent <MeshFilter>()); // Get default texture from cache or import from disk meshRenderer.sharedMaterial = MaterialReader.CreateStandardMaterial(MaterialReader.CustomBlendMode.Cutout); CachedMaterial cachedMaterialOut; if (materialReader.GetCachedMaterialCustomBillboard(archive, record, frame, out cachedMaterialOut)) { meshRenderer.material.mainTexture = cachedMaterialOut.albedoMap; } else { // Get texture for default frame Texture2D albedoTexture = LoadCustomTexture(archive, record, frame); albedoTexture.filterMode = (FilterMode)DaggerfallUnity.Settings.MainFilterMode; // Set texture for default frame meshRenderer.material.mainTexture = albedoTexture; // Save in cache CachedMaterial newcm = new CachedMaterial() { albedoMap = albedoTexture, material = meshRenderer.material }; materialReader.SetCachedMaterialCustomBillboard(archive, record, frame, newcm); } // Import textures for all records and frames record = 0; textures = new List <List <Texture2D> >(); while (CustomTextureExist(archive, record)) { frame = 0; List <Texture2D> frameTextures = new List <Texture2D>(); while (CustomTextureExist(archive, record, frame)) { if (materialReader.GetCachedMaterialCustomBillboard(archive, record, frame, out cachedMaterialOut)) { frameTextures.Add(cachedMaterialOut.albedoMap); } else { // Get texture Texture2D tex = LoadCustomTexture(archive, record, frame); tex.filterMode = (FilterMode)DaggerfallUnity.Settings.MainFilterMode; frameTextures.Add(tex); // Save in cache CachedMaterial newcm = new CachedMaterial() { albedoMap = tex }; materialReader.SetCachedMaterialCustomBillboard(archive, record, frame, newcm); } frame++; } textures.Add(frameTextures); record++; } }
/// <summary> /// Set custom material on billboard gameobject. /// </summary> /// <paran name="go">Billboard gameobject.</param> /// <param name="archive">Archive index.</param> /// <param name="record">Record index.</param> static public void SetBillboardCustomMaterial(GameObject go, ref DaggerfallBillboard.BillboardSummary summary) { // Variables int numberOfFrames; int archive = summary.Archive; int record = summary.Record; //string name = GetName(archive, record); var meshRenderer = go.GetComponent <MeshRenderer>(); Texture2D albedoTexture, emissionMap; // Check if billboard is emissive bool isEmissive = meshRenderer.material.GetTexture("_EmissionMap"); // UVs Vector2 uv = Vector2.zero; // Get properties from Xml string path = Path.Combine(texturesPath, GetName(archive, record)); if (XMLManager.XmlFileExists(path)) { var xml = new XMLManager(path); // Set billboard scale Transform transform = go.GetComponent <Transform>(); transform.localScale = xml.GetVector3("scaleX", "scaleY", transform.localScale); summary.Size.x *= transform.localScale.x; summary.Size.y *= transform.localScale.y; // Get UV uv = xml.GetVector2("uvX", "uvY", uv); } // Update UV SetUv(go.GetComponent <MeshFilter>(), uv.x, uv.y); // Get material from cache or import from disk MaterialReader materialReader = DaggerfallUnity.Instance.MaterialReader; CachedMaterial cachedMaterialOut; if (materialReader.GetCachedMaterialCustomBillboard(archive, record, 0, out cachedMaterialOut)) { // Get and set material meshRenderer.material = cachedMaterialOut.material; // Get other properties numberOfFrames = cachedMaterialOut.singleFrameCount; albedoTexture = cachedMaterialOut.albedoMap; emissionMap = cachedMaterialOut.emissionMap; } else { // Get textures from disk LoadCustomBillboardFrameTexture(isEmissive, out albedoTexture, out emissionMap, archive, record); // Main texture meshRenderer.material.SetTexture("_MainTex", albedoTexture); // Emission maps for lights if (isEmissive) { meshRenderer.material.SetTexture("_EmissionMap", emissionMap); } // Get number of frames on disk numberOfFrames = NumberOfAvailableFrames(archive, record); // Save material in cache CachedMaterial newcm = new CachedMaterial() { albedoMap = albedoTexture, emissionMap = emissionMap, material = meshRenderer.material, singleFrameCount = numberOfFrames }; materialReader.SetCachedMaterialCustomBillboard(archive, record, 0, newcm); } // Import textures for each frame if billboard is animated summary.CustomBillboard = new CustomBillboard(); summary.CustomBillboard.isCustomAnimated = numberOfFrames > 1; if (summary.CustomBillboard.isCustomAnimated) { List <Texture2D> albedoTextures = new List <Texture2D>(); List <Texture2D> emissionmaps = new List <Texture2D>(); // Frame zero albedoTextures.Add(albedoTexture); if (isEmissive) { emissionmaps.Add(emissionMap); } // Other frames for (int frame = 1; frame < numberOfFrames; frame++) { if (materialReader.GetCachedMaterialCustomBillboard(archive, record, frame, out cachedMaterialOut)) { // Get textures from cache albedoTexture = cachedMaterialOut.albedoMap; emissionMap = cachedMaterialOut.emissionMap; } else { // Get textures from disk LoadCustomBillboardFrameTexture(isEmissive, out albedoTexture, out emissionMap, archive, record, frame); // Save textures in cache CachedMaterial newcm = new CachedMaterial() { albedoMap = albedoTexture, emissionMap = emissionMap, }; materialReader.SetCachedMaterialCustomBillboard(archive, record, frame, newcm); } albedoTextures.Add(albedoTexture); if (isEmissive) { emissionmaps.Add(emissionMap); } } // Set textures and properties summary.CustomBillboard.MainTexture = albedoTextures; summary.CustomBillboard.EmissionMap = emissionmaps; summary.CustomBillboard.NumberOfFrames = numberOfFrames; summary.CustomBillboard.isEmissive = isEmissive; } }
/// <summary> /// Import and set custom material on Enemy unit. /// </summary> /// <param name="go">Enemy Mobile Unit.</param> /// <param name="archive">Archive which contains all textures except the dead enemy.</param> /// <param name="textures">All textures for this enemy (except dead texture).</param> static public void SetupCustomEnemyMaterial(GameObject go, int archive, out List <List <Texture2D> > textures) { // This is the first texture set on enemy. Enemies use all custom textures or all vanilla. // If (archive, defaultRecord, defaultFrame) is present on disk all other textures are // considered required, otherwise vanilla textures are used. int record = enemyDefaultRecord, frame = enemyDefaultFrame; // Update UV map UpdateUV(go.GetComponent <MeshFilter>()); // Get default texture from cache or import from disk var meshRenderer = go.GetComponent <MeshRenderer>(); MaterialReader materialReader = DaggerfallUnity.Instance.MaterialReader; CachedMaterial cachedMaterialOut; if (materialReader.GetCachedMaterialCustomBillboard(archive, record, frame, out cachedMaterialOut)) { meshRenderer.material.mainTexture = cachedMaterialOut.albedoMap; } else { // Get texture for default frame Texture2D albedoTexture = LoadCustomTexture(archive, record, frame); albedoTexture.filterMode = (FilterMode)DaggerfallUnity.Settings.MainFilterMode; // Set texture for default frame meshRenderer.material.mainTexture = albedoTexture; // Save in cache CachedMaterial newcm = new CachedMaterial() { albedoMap = albedoTexture, material = meshRenderer.material }; materialReader.SetCachedMaterialCustomBillboard(archive, record, frame, newcm); } // Import textures for all records and frames record = 0; textures = new List <List <Texture2D> >(); while (CustomTextureExist(archive, record)) { frame = 0; List <Texture2D> frameTextures = new List <Texture2D>(); while (CustomTextureExist(archive, record, frame)) { if (materialReader.GetCachedMaterialCustomBillboard(archive, record, frame, out cachedMaterialOut)) { frameTextures.Add(cachedMaterialOut.albedoMap); } else { // Get texture Texture2D tex = LoadCustomTexture(archive, record, frame); tex.filterMode = (FilterMode)DaggerfallUnity.Settings.MainFilterMode; frameTextures.Add(tex); // Save in cache CachedMaterial newcm = new CachedMaterial() { albedoMap = tex }; materialReader.SetCachedMaterialCustomBillboard(archive, record, frame, newcm); } frame++; } textures.Add(frameTextures); record++; } }
/// <summary> /// Set custom material on billboard gameobject. /// </summary> /// <paran name="go">Billboard gameobject.</param> /// <param name="archive">Archive index.</param> /// <param name="record">Record index.</param> static public void SetBillboardCustomMaterial(GameObject go, int archive, int record) { int numberOfFrames; string name = GetName(archive, record); var meshRenderer = go.GetComponent <MeshRenderer>(); var daggerfallBillboard = go.GetComponent <DaggerfallBillboard>(); Texture2D albedoTexture, emissionMap; // Check if billboard is emissive bool isEmissive = false; if (meshRenderer.material.GetTexture("_EmissionMap") != null) { isEmissive = true; } // UVs Vector2 uv = Vector2.zero; // Get properties from Xml if (XMLManager.XmlFileExist(archive, record)) { // Customize billboard size (scale) Transform transform = go.GetComponent <Transform>(); transform.localScale = XMLManager.GetScale(name, texturesPath, transform.localScale); daggerfallBillboard.SetCustomSize(archive, record, transform.localScale.y); // Get UV uv = XMLManager.GetUv(name, texturesPath, uv.x, uv.y); } // Update UV UpdateUV(go.GetComponent <MeshFilter>(), uv.x, uv.y); // Get material from cache or import from disk MaterialReader materialReader = DaggerfallUnity.Instance.MaterialReader; CachedMaterial cachedMaterialOut; if (materialReader.GetCachedMaterialCustomBillboard(archive, record, 0, out cachedMaterialOut)) { // Get and set material meshRenderer.material = cachedMaterialOut.material; // Get other properties numberOfFrames = cachedMaterialOut.singleFrameCount; albedoTexture = cachedMaterialOut.albedoMap; emissionMap = cachedMaterialOut.emissionMap; } else { // Get textures from disk LoadCustomBillboardFrameTexture(isEmissive, out albedoTexture, out emissionMap, archive, record); // Main texture meshRenderer.material.SetTexture("_MainTex", albedoTexture); // Emission maps for lights if (isEmissive) { meshRenderer.material.SetTexture("_EmissionMap", emissionMap); } // Get number of frames on disk numberOfFrames = NumberOfAvailableFrames(archive, record); // Save material in cache CachedMaterial newcm = new CachedMaterial() { albedoMap = albedoTexture, emissionMap = emissionMap, material = meshRenderer.material, singleFrameCount = numberOfFrames }; materialReader.SetCachedMaterialCustomBillboard(archive, record, 0, newcm); } // Import textures for each frame if billboard is animated if (numberOfFrames > 1) { List <Texture2D> albedoTextures = new List <Texture2D>(); List <Texture2D> emissionmaps = new List <Texture2D>(); // Frame zero albedoTextures.Add(albedoTexture); if (isEmissive) { emissionmaps.Add(emissionMap); } // Other frames for (int frame = 1; frame < numberOfFrames; frame++) { if (materialReader.GetCachedMaterialCustomBillboard(archive, record, frame, out cachedMaterialOut)) { // Get textures from cache albedoTexture = cachedMaterialOut.albedoMap; emissionMap = cachedMaterialOut.emissionMap; } else { // Get textures from disk LoadCustomBillboardFrameTexture(isEmissive, out albedoTexture, out emissionMap, archive, record, frame); // Save textures in cache CachedMaterial newcm = new CachedMaterial() { albedoMap = albedoTexture, emissionMap = emissionMap, }; materialReader.SetCachedMaterialCustomBillboard(archive, record, frame, newcm); } albedoTextures.Add(albedoTexture); if (isEmissive) { emissionmaps.Add(emissionMap); } } // Set textures and properties CustomBillboard customBillboard = new CustomBillboard() { MainTexture = albedoTextures, EmissionMap = emissionmaps, NumberOfFrames = numberOfFrames, isEmissive = isEmissive }; daggerfallBillboard.SetCustomMaterial(customBillboard); } }