/// <summary> /// Generates a building thumbnail. /// </summary> /// <param name="building">BuildingInfo target</param> /// <returns>True if rendering occured successfully, false otherwise</returns> private bool BuildingThumbnail(BuildingInfo building) { // Safety first! if (building?.m_mesh == null || building.m_material == null) { return(false); } // Set mesh and material for render. renderer.Mesh = building.m_mesh; renderer.Material = building.m_material; renderer.IsPropFenceShader = false; // If the selected building has colour variations, temporarily set the colour to the default for rendering. if (building.m_useColorVariations) { Color originalColor = building.m_material.color; building.m_material.color = building.m_color0; renderer.Render(); building.m_material.color = originalColor; } else { // No temporary colour change needed. renderer.Render(); } // If we made it this far, all good. return(true); }
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); }