/// <summary>
        /// Constructor.
        /// </summary>
        public ThumbnailGenerator()
        {
            Debugging.Message("creating thumbnail generator");

            // Get local reference from parent.
            renderer = ThumbnailManager.Renderer;

            // Size and setting for thumbnail images: 109 x 100, doubled for anti-aliasing.
            renderer.Size           = new Vector2(109, 100) * 2f;
            renderer.CameraRotation = 35f;
        }
        /// <summary>
        /// Cleans up when finished.
        /// </summary>
        internal static void Close()
        {
            GameObject.Destroy(_renderer);
            GameObject.Destroy(gameObject);

            Debugging.Message("thumbnail renderer destroyed");

            // Let the garbage collector cleanup.
            _generator = null;
            _renderer  = null;
            gameObject = null;
        }
        /// <summary>
        /// Creates our renderer GameObject.
        /// </summary>
        internal static void Create()
        {
            try
            {
                // If no instance already set, create one.
                if (gameObject == null)
                {
                    // Give it a unique name for easy finding with ModTools.
                    gameObject = new GameObject("FindItThumbnailRenderer");
                    gameObject.transform.parent = UIView.GetAView().transform;

                    // Add our queue manager and renderer directly to the gameobject.
                    _renderer  = gameObject.AddComponent <PreviewRenderer>();
                    _generator = new ThumbnailGenerator();

                    Debugging.Message("thumbnail renderer created");
                }
            }
            catch (Exception e)
            {
                Debugging.LogException(e);
            }
        }
Beispiel #4
0
        public static bool CreateThumbnailAtlas(string name, PrefabInfo prefab)
        {
            if (name.IsNullOrWhiteSpace() || prefab == null)
            {
                return(false);
            }
            if (prefab.m_Thumbnail == name)
            {
                return(true);
            }

            if (m_previewRenderer == null)
            {
                m_previewRenderer      = new GameObject("FindItPreviewRenderer").AddComponent <PreviewRenderer>();
                m_previewRenderer.size = new Vector2(109, 100) * 2f;
            }

            m_previewRenderer.cameraRotation = 210f;
            m_previewRenderer.zoom           = 4f;

            bool rendered = false;

            BuildingInfo buildingPrefab = prefab as BuildingInfo;

            if (buildingPrefab != null)
            {
                m_previewRenderer.mesh     = buildingPrefab.m_mesh;
                m_previewRenderer.material = buildingPrefab.m_material;

                if (m_previewRenderer.mesh != null)
                {
                    if (buildingPrefab.m_useColorVariations && m_previewRenderer.material != null)
                    {
                        Color materialColor = buildingPrefab.m_material.color;
                        buildingPrefab.m_material.color = buildingPrefab.m_color0;
                        m_previewRenderer.Render();
                        buildingPrefab.m_material.color = materialColor;
                    }
                    else
                    {
                        m_previewRenderer.Render();
                    }

                    rendered = true;
                }
            }

            PropInfo propPrefab = prefab as PropInfo;

            if (propPrefab != null)
            {
                if (propPrefab.m_material != null && (propPrefab.m_material.shader == Asset.shaderBlend || propPrefab.m_material.shader == Asset.shaderSolid))
                {
                    RenderTexture active = RenderTexture.active;

                    Texture2D mainTexture = propPrefab.m_material.GetTexture("_MainTex") as Texture2D;
                    Texture2D aci         = propPrefab.m_material.GetTexture("_ACIMap") as Texture2D;

                    Texture2D texture = new Texture2D(mainTexture.width, mainTexture.height);
                    ResourceLoader.CopyTexture(mainTexture, texture);
                    Color32[] colors = texture.GetPixels32();

                    if (aci != null)
                    {
                        ResourceLoader.CopyTexture(aci, texture);
                        Color32[] aciColors = texture.GetPixels32();

                        for (int i = 0; i < colors.Length; i++)
                        {
                            colors[i].a -= aciColors[i].r;
                        }

                        texture.SetPixels32(0, 0, texture.width, texture.height, colors);
                        texture.Apply();
                    }

                    ScaleTexture2(texture, 109 - 10, 100 - 10);
                    texture.name = name;

                    prefab.m_Thumbnail = name;
                    prefab.m_Atlas     = ResourceLoader.CreateTextureAtlas("FindItThumbnails_" + name, new string[] { }, null);
                    ResourceLoader.AddTexturesInAtlas(prefab.m_Atlas, GenerateMissingThumbnailVariants(texture));

                    DebugUtils.Log("Generated thumbnails for: " + name);

                    return(true);
                }
                else
                {
                    m_previewRenderer.mesh     = propPrefab.m_mesh;
                    m_previewRenderer.material = propPrefab.m_material;

                    if (m_previewRenderer.mesh != null)
                    {
                        if (propPrefab.m_useColorVariations && m_previewRenderer.material != null)
                        {
                            Color materialColor = propPrefab.m_material.color;
                            propPrefab.m_material.color = propPrefab.m_color0;
                            m_previewRenderer.Render();
                            propPrefab.m_material.color = materialColor;
                        }
                        else
                        {
                            m_previewRenderer.Render();
                        }

                        rendered = true;
                    }
                }
            }

            TreeInfo treePrefab = prefab as TreeInfo;

            if (treePrefab != null)
            {
                m_previewRenderer.mesh     = treePrefab.m_mesh;
                m_previewRenderer.material = treePrefab.m_material;

                if (m_previewRenderer.mesh != null)
                {
                    m_previewRenderer.Render();
                    rendered = true;
                }
            }

            if (rendered)
            {
                Texture2D texture = ResourceLoader.ConvertRenderTexture(m_previewRenderer.texture);
                texture.name = name;

                prefab.m_Thumbnail = name;
                prefab.m_Atlas     = ResourceLoader.CreateTextureAtlas("FindItThumbnails_" + name, new string[] { }, null);

                ResourceLoader.ResizeTexture(texture, 109, 100);
                ResourceLoader.AddTexturesInAtlas(prefab.m_Atlas, GenerateMissingThumbnailVariants(texture));

                DebugUtils.Log("Generated thumbnails for: " + name);
            }
            else
            {
                prefab.m_Thumbnail = "ThumbnailBuildingDefault";
            }

            return(rendered);
        }