Exemplo n.º 1
0
        /// <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
            if (XMLManager.XmlFileExist(archive, record))
            {
                // Customize billboard size (scale)
                Transform transform = go.GetComponent <Transform>();
                transform.localScale = XMLManager.GetScale(name, texturesPath, transform.localScale);
                summary.Size.x      *= transform.localScale.x;
                summary.Size.y      *= 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
            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;
            }
        }