/// <summary>
        /// Sets new Daggerfall material and recreates mesh.
        /// Will use an atlas if specified in DaggerfallUnity singleton.
        /// </summary>
        /// <param name="dfUnity">DaggerfallUnity singleton. Required for content readers and settings.</param>
        /// <param name="archive">Texture archive index.</param>
        /// <param name="record">Texture record index.</param>
        /// <param name="frame">Frame index.</param>
        /// <param name="dungeon">This is a dungeon billboard.</param>
        /// <returns>Material.</returns>
        public Material SetMaterial(DaggerfallUnity dfUnity, int archive, int record, int frame, bool dungeon)
        {
            Shader shader;

            //if (archive == 210 && dungeon) // Dungeon light billboards use unlit billboard shader
            if (archive == 210) // All light billboards use unlit billboard shader
            {
                shader = Shader.Find(dfUnity.MaterialReader.DefaultUnlitBillboardShaderName);
            }
            else
            {
                shader = Shader.Find(dfUnity.MaterialReader.DefaultBillboardShaderName);
            }

            // Get references
            meshRenderer = GetComponent <MeshRenderer>();

            Vector2  size;
            Mesh     mesh     = null;
            Material material = null;

            if (dfUnity.MaterialReader.AtlasTextures)
            {
                material = dfUnity.MaterialReader.GetMaterialAtlas(
                    archive,
                    0,
                    4,
                    2048,
                    out summary.AtlasRects,
                    out summary.AtlasIndices,
                    4,
                    true,
                    0,
                    false,
                    shader);
                mesh = dfUnity.MeshReader.GetBillboardMesh(
                    summary.AtlasRects[summary.AtlasIndices[record].startIndex],
                    archive,
                    record,
                    out size,
                    dungeon);
                summary.AtlasedMaterial = true;
                if (summary.AtlasIndices[record].frameCount > 1)
                {
                    summary.AnimatedMaterial = true;
                }
                else
                {
                    summary.AnimatedMaterial = false;
                }
            }
            else
            {
                material = dfUnity.MaterialReader.GetMaterial(
                    archive,
                    record,
                    frame,
                    0,
                    out summary.Rect,
                    4,
                    true,
                    shader);
                mesh = dfUnity.MeshReader.GetBillboardMesh(
                    summary.Rect,
                    archive,
                    record,
                    out size,
                    dungeon);
                summary.AtlasedMaterial  = false;
                summary.AnimatedMaterial = false;
            }

            // Set summary
            summary.InDungeon = dungeon;
            summary.FlatType  = MaterialReader.GetFlatType(archive);
            summary.Archive   = archive;
            summary.Record    = record;
            summary.Size      = size;

            // Set editor flat types
            if (summary.FlatType == FlatTypes.Editor)
            {
                summary.EditorFlatType = MaterialReader.GetEditorFlatType(summary.Record);
            }

            // Assign mesh and material
            MeshFilter meshFilter = GetComponent <MeshFilter>();
            Mesh       oldMesh    = meshFilter.sharedMesh;

            if (mesh)
            {
                meshFilter.sharedMesh       = mesh;
                meshRenderer.sharedMaterial = material;
            }
            if (oldMesh)
            {
                // The old mesh is no longer required
                DestroyImmediate(oldMesh);
            }

            return(material);
        }
        /// <summary>
        /// Sets new Daggerfall material and recreates mesh.
        /// Will use an atlas if specified in DaggerfallUnity singleton.
        /// </summary>
        /// <param name="dfUnity">DaggerfallUnity singleton. Required for content readers and settings.</param>
        /// <param name="archive">Texture archive index.</param>
        /// <param name="record">Texture record index.</param>
        /// <param name="frame">Frame index.</param>
        /// <param name="dungeon">This is a dungeon billboard.</param>
        /// <returns>Material.</returns>
        public Material SetMaterial(int archive, int record, int frame, bool dungeon)
        {
            // Get DaggerfallUnity
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            if (!dfUnity.IsReady)
            {
                return(null);
            }

            // Get references
            meshRenderer = GetComponent <MeshRenderer>();

            Vector2  size;
            Mesh     mesh     = null;
            Material material = null;

            if (dfUnity.MaterialReader.AtlasTextures)
            {
                material = dfUnity.MaterialReader.GetMaterialAtlas(
                    archive,
                    0,
                    4,
                    2048,
                    out summary.AtlasRects,
                    out summary.AtlasIndices,
                    4,
                    true,
                    0,
                    false);
                mesh = dfUnity.MeshReader.GetBillboardMesh(
                    summary.AtlasRects[summary.AtlasIndices[record].startIndex],
                    archive,
                    record,
                    out size,
                    dungeon);
                summary.AtlasedMaterial = true;
                if (summary.AtlasIndices[record].frameCount > 1)
                {
                    summary.AnimatedMaterial = true;
                }
                else
                {
                    summary.AnimatedMaterial = false;
                }
            }
            else
            {
                material = dfUnity.MaterialReader.GetMaterial(
                    archive,
                    record,
                    frame,
                    0,
                    out summary.Rect,
                    4,
                    true);
                mesh = dfUnity.MeshReader.GetBillboardMesh(
                    summary.Rect,
                    archive,
                    record,
                    out size,
                    dungeon);
                summary.AtlasedMaterial  = false;
                summary.AnimatedMaterial = false;
            }

            // Update material properties
            MaterialReader.SetBlendMode(material, MaterialReader.CustomBlendMode.Cutout);

            // Set summary
            summary.InDungeon = dungeon;
            summary.FlatType  = MaterialReader.GetFlatType(archive);
            summary.Archive   = archive;
            summary.Record    = record;
            summary.Size      = size;

            // Set editor flat types
            if (summary.FlatType == FlatTypes.Editor)
            {
                summary.EditorFlatType = MaterialReader.GetEditorFlatType(summary.Record);
            }

            // Assign mesh and material
            MeshFilter meshFilter = GetComponent <MeshFilter>();
            Mesh       oldMesh    = meshFilter.sharedMesh;

            if (mesh)
            {
                meshFilter.sharedMesh       = mesh;
                meshRenderer.sharedMaterial = material;
            }
            if (oldMesh)
            {
                // The old mesh is no longer required
                DestroyImmediate(oldMesh);
            }

            // Standalone billboards never cast shadows
            meshRenderer.shadowCastingMode = ShadowCastingMode.Off;

            return(material);
        }
Exemple #3
0
        /// <summary>
        /// Sets new Daggerfall material and recreates mesh.
        /// Will use an atlas if specified in DaggerfallUnity singleton.
        /// </summary>
        /// <param name="dfUnity">DaggerfallUnity singleton. Required for content readers and settings.</param>
        /// <param name="archive">Texture archive index.</param>
        /// <param name="record">Texture record index.</param>
        /// <param name="frame">Frame index.</param>
        /// <returns>Material.</returns>
        public Material SetMaterial(int archive, int record, int frame = 0)
        {
            // Get DaggerfallUnity
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            if (!dfUnity.IsReady)
            {
                return(null);
            }

            // Get references
            meshRenderer = GetComponent <MeshRenderer>();

            Vector2  size;
            Vector2  scale;
            Mesh     mesh     = null;
            Material material = null;

            if (material = TextureReplacement.GetStaticBillboardMaterial(gameObject, archive, record, ref summary, out scale))
            {
                mesh  = dfUnity.MeshReader.GetBillboardMesh(summary.Rect, archive, record, out size);
                size *= scale;
                summary.AtlasedMaterial  = false;
                summary.AnimatedMaterial = summary.ImportedTextures.FrameCount > 1;
            }
            else if (dfUnity.MaterialReader.AtlasTextures)
            {
                material = dfUnity.MaterialReader.GetMaterialAtlas(
                    archive,
                    0,
                    4,
                    2048,
                    out summary.AtlasRects,
                    out summary.AtlasIndices,
                    4,
                    true,
                    0,
                    false,
                    true);
                mesh = dfUnity.MeshReader.GetBillboardMesh(
                    summary.AtlasRects[summary.AtlasIndices[record].startIndex],
                    archive,
                    record,
                    out size);
                summary.AtlasedMaterial = true;
                if (summary.AtlasIndices[record].frameCount > 1)
                {
                    summary.AnimatedMaterial = true;
                }
                else
                {
                    summary.AnimatedMaterial = false;
                }
            }
            else
            {
                material = dfUnity.MaterialReader.GetMaterial(
                    archive,
                    record,
                    frame,
                    0,
                    out summary.Rect,
                    4,
                    true,
                    true);
                mesh = dfUnity.MeshReader.GetBillboardMesh(
                    summary.Rect,
                    archive,
                    record,
                    out size);
                summary.AtlasedMaterial  = false;
                summary.AnimatedMaterial = false;
            }

            // Set summary
            summary.FlatType = MaterialReader.GetFlatType(archive);
            summary.Archive  = archive;
            summary.Record   = record;
            summary.Size     = size;

            // Set editor flat types
            if (summary.FlatType == FlatTypes.Editor)
            {
                summary.EditorFlatType = MaterialReader.GetEditorFlatType(summary.Record);
            }

            // Set NPC flat type based on archive
            if (RDBLayout.IsNPCFlat(summary.Archive))
            {
                summary.FlatType = FlatTypes.NPC;
            }

            // Assign mesh and material
            MeshFilter meshFilter = GetComponent <MeshFilter>();
            Mesh       oldMesh    = meshFilter.sharedMesh;

            if (mesh)
            {
                meshFilter.sharedMesh       = mesh;
                meshRenderer.sharedMaterial = material;
            }
            if (oldMesh)
            {
                // The old mesh is no longer required
#if UNITY_EDITOR
                DestroyImmediate(oldMesh);
#else
                Destroy(oldMesh);
#endif
            }

            // General billboard shadows if enabled
            bool isLightArchive = (archive == TextureReader.LightsTextureArchive);
            meshRenderer.shadowCastingMode = (DaggerfallUnity.Settings.GeneralBillboardShadows && !isLightArchive) ? ShadowCastingMode.TwoSided : ShadowCastingMode.Off;

            // Add NPC trigger collider
            if (summary.FlatType == FlatTypes.NPC)
            {
                Collider col = gameObject.AddComponent <BoxCollider>();
                col.isTrigger = true;
            }

            return(material);
        }
Exemple #4
0
        /// <summary>
        /// Sets new Daggerfall material and recreates mesh.
        /// Will use an atlas if specified in DaggerfallUnity singleton.
        /// </summary>
        /// <param name="dfUnity">DaggerfallUnity singleton. Required for content readers and settings.</param>
        /// <param name="archive">Texture archive index.</param>
        /// <param name="record">Texture record index.</param>
        /// <param name="frame">Frame index.</param>
        /// <returns>Material.</returns>
        public Material SetMaterial(int archive, int record, int frame = 0)
        {
            // Get DaggerfallUnity
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            if (!dfUnity.IsReady)
            {
                return(null);
            }

            // Get references
            meshRenderer = GetComponent <MeshRenderer>();

            Vector2  size;
            Mesh     mesh     = null;
            Material material = null;

            if (dfUnity.MaterialReader.AtlasTextures)
            {
                material = dfUnity.MaterialReader.GetMaterialAtlas(
                    archive,
                    0,
                    4,
                    2048,
                    out summary.AtlasRects,
                    out summary.AtlasIndices,
                    4,
                    true,
                    0,
                    false,
                    true);
                mesh = dfUnity.MeshReader.GetBillboardMesh(
                    summary.AtlasRects[summary.AtlasIndices[record].startIndex],
                    archive,
                    record,
                    out size);
                summary.AtlasedMaterial = true;
                if (summary.AtlasIndices[record].frameCount > 1)
                {
                    summary.AnimatedMaterial = true;
                }
                else
                {
                    summary.AnimatedMaterial = false;
                }
            }
            else
            {
                material = dfUnity.MaterialReader.GetMaterial(
                    archive,
                    record,
                    frame,
                    0,
                    out summary.Rect,
                    4,
                    true,
                    true);
                mesh = dfUnity.MeshReader.GetBillboardMesh(
                    summary.Rect,
                    archive,
                    record,
                    out size);
                summary.AtlasedMaterial  = false;
                summary.AnimatedMaterial = false;
            }

            // Set summary
            summary.FlatType = MaterialReader.GetFlatType(archive);
            summary.Archive  = archive;
            summary.Record   = record;
            summary.Size     = size;

            // Set editor flat types
            if (summary.FlatType == FlatTypes.Editor)
            {
                summary.EditorFlatType = MaterialReader.GetEditorFlatType(summary.Record);
            }

            // Set NPC flat type based on archive
            // This is just a hack for now while performing
            // more research into NPC names
            if (summary.Archive == 334 ||                               // Daggerfall people
                summary.Archive == 346 ||                               // Wayrest people
                summary.Archive == 357 ||                               // Sentinel people
                summary.Archive >= 175 && summary.Archive <= 184)       // Other people
            {
                summary.FlatType = FlatTypes.NPC;
            }

            // Assign mesh and material
            MeshFilter meshFilter = GetComponent <MeshFilter>();
            Mesh       oldMesh    = meshFilter.sharedMesh;

            if (mesh)
            {
                meshFilter.sharedMesh       = mesh;
                meshRenderer.sharedMaterial = material;
            }
            if (oldMesh)
            {
                // The old mesh is no longer required
#if UNITY_EDITOR
                DestroyImmediate(oldMesh);
#else
                Destroy(oldMesh);
#endif
            }

            // Standalone billboards never cast shadows
            meshRenderer.shadowCastingMode = ShadowCastingMode.Off;

            // Add NPC trigger collider
            if (summary.FlatType == FlatTypes.NPC)
            {
                Collider col = gameObject.AddComponent <BoxCollider>();
                col.isTrigger = true;
            }

            return(material);
        }