static public bool TextureHasAlpha(Texture2D inTex) { if (inTex != null) { return(GraphicsFormatUtility.HasAlphaChannel(GraphicsFormatUtility.GetGraphicsFormat(inTex.format, true))); } return(false); }
/// <summary> /// Specifies whether the Texture has an alpha channel or not. Returns true if it does and false otherwise. /// </summary> /// <param name="tex">The Texture for this function to check.</param> /// <returns></returns> public static bool TextureHasAlpha(Texture2D tex) { if (tex == null) { return(false); } return(GraphicsFormatUtility.HasAlphaChannel(tex.graphicsFormat)); }
public override void OnPreviewSettings() { // Do not allow changing of pages when multiple atlases is selected. if (targets.Length == 1 && m_OptionDisplays != null && m_OptionValues != null && m_TotalPages > 1) { m_PreviewPage = EditorGUILayout.IntPopup(m_PreviewPage, m_OptionDisplays, m_OptionValues, styles.preDropDown, GUILayout.MaxWidth(50)); } else { m_PreviewPage = 0; } if (m_PreviewTextures != null) { m_PreviewPage = Mathf.Min(m_PreviewPage, m_PreviewTextures.Length - 1); Texture2D t = m_PreviewTextures[m_PreviewPage]; if (t == null) { return; } if (GraphicsFormatUtility.HasAlphaChannel(t.format) || (m_PreviewAlphaTextures != null && m_PreviewAlphaTextures.Length > 0)) { m_ShowAlpha = GUILayout.Toggle(m_ShowAlpha, m_ShowAlpha ? styles.alphaIcon : styles.RGBIcon, styles.previewButton); } int mipCount = Mathf.Max(1, TextureUtil.GetMipmapCount(t)); if (mipCount > 1) { GUILayout.Box(styles.smallZoom, styles.previewLabel); m_MipLevel = Mathf.Round(GUILayout.HorizontalSlider(m_MipLevel, mipCount - 1, 0, styles.previewSlider, styles.previewSliderThumb, GUILayout.MaxWidth(64))); GUILayout.Box(styles.largeZoom, styles.previewLabel); } } }
public override void OnPreviewSettings() { // TextureInspector code is reused for RenderTexture and Cubemap inspectors. // Make sure we can handle the situation where target is just a Texture and // not a Texture2D. It's also used for large popups for mini texture fields, // and while it's being shown the actual texture object might disappear -- // make sure to handle null targets. Texture tex = target as Texture; bool alphaOnly = true; bool hasAlpha = false; bool needsExposureControl = false; int mipCount = 1; foreach (Texture t in targets) { if (t == null) // texture might have disappeared while we're showing this in a preview popup { continue; } mipCount = Mathf.Max(mipCount, TextureUtil.GetMipmapCount(t)); TextureUsageMode mode = TextureUtil.GetUsageMode(t); if (!GraphicsFormatUtility.IsAlphaOnlyFormat(t.graphicsFormat)) { alphaOnly = false; } if (GraphicsFormatUtility.HasAlphaChannel(t.graphicsFormat)) { if (mode == TextureUsageMode.Default) // all other texture usage modes don't displayable alpha { hasAlpha = true; } } // 3D texture previewer doesn't support an exposure value. if (t.dimension != TextureDimension.Tex3D && NeedsExposureControl(t)) { needsExposureControl = true; } } if (needsExposureControl) { OnExposureSlider(); } if (IsCubemap()) { m_CubemapPreview.OnPreviewSettings(targets, mipCount, alphaOnly, hasAlpha); return; } if (IsTexture3D()) { m_Texture3DPreview.OnPreviewSettings(targets); return; } if (IsTexture2DArray() && !SystemInfo.supports2DArrayTextures) { return; } if (s_Styles == null) { s_Styles = new Styles(); } List <PreviewMode> previewCandidates = new List <PreviewMode>(5); previewCandidates.Add(PreviewMode.RGB); previewCandidates.Add(PreviewMode.R); previewCandidates.Add(PreviewMode.G); previewCandidates.Add(PreviewMode.B); previewCandidates.Add(PreviewMode.A); if (alphaOnly) { previewCandidates.Clear(); previewCandidates.Add(PreviewMode.A); m_PreviewMode = PreviewMode.A; } else if (!hasAlpha) { previewCandidates.Remove(PreviewMode.A); } if (previewCandidates.Count > 1 && tex != null && !IsNormalMap(tex)) { int selectedIndex = previewCandidates.IndexOf(m_PreviewMode); if (selectedIndex == -1) { selectedIndex = 0; } if (previewCandidates.Contains(PreviewMode.RGB)) { m_PreviewMode = GUILayout.Toggle(m_PreviewMode == PreviewMode.RGB, s_Styles.previewButtonContents[0], s_Styles.toolbarButton) ? PreviewMode.RGB : m_PreviewMode; } if (previewCandidates.Contains(PreviewMode.R)) { m_PreviewMode = GUILayout.Toggle(m_PreviewMode == PreviewMode.R, s_Styles.previewButtonContents[1], s_Styles.toolbarButton) ? PreviewMode.R : m_PreviewMode; } if (previewCandidates.Contains(PreviewMode.G)) { m_PreviewMode = GUILayout.Toggle(m_PreviewMode == PreviewMode.G, s_Styles.previewButtonContents[2], s_Styles.toolbarButton) ? PreviewMode.G : m_PreviewMode; } if (previewCandidates.Contains(PreviewMode.B)) { m_PreviewMode = GUILayout.Toggle(m_PreviewMode == PreviewMode.B, s_Styles.previewButtonContents[3], s_Styles.toolbarButton) ? PreviewMode.B : m_PreviewMode; } if (previewCandidates.Contains(PreviewMode.A)) { m_PreviewMode = GUILayout.Toggle(m_PreviewMode == PreviewMode.A, s_Styles.previewButtonContents[4], s_Styles.toolbarButton) ? PreviewMode.A : m_PreviewMode; } } if (IsTexture2DArray()) { m_Texture2DArrayPreview.OnPreviewSettings(tex); } if (mipCount > 1) { GUILayout.Box(s_Styles.smallZoom, s_Styles.previewLabel); GUI.changed = false; m_MipLevel = Mathf.Round(GUILayout.HorizontalSlider(m_MipLevel, mipCount - 1, 0, s_Styles.previewSlider, s_Styles.previewSliderThumb, GUILayout.MaxWidth(64))); //For now, we don't have mipmaps smaller than the tile size when using VT. if (EditorGUI.UseVTMaterial(tex)) { int numMipsOfTile = (int)Mathf.Log(VirtualTexturing.EditorHelpers.tileSize, 2) + 1; m_MipLevel = Mathf.Min(m_MipLevel, Mathf.Max(mipCount - numMipsOfTile, 0)); } GUILayout.Box(s_Styles.largeZoom, s_Styles.previewLabel); } }
public void DrawGUI(TerrainLayer terrainLayer) { bool disabledByDiffuseAlpha = false; if (this.variable == TerrainLayerVariable.smoothness) { bool diffuseHasAlpha = terrainLayer.diffuseTexture != null && GraphicsFormatUtility.HasAlphaChannel(terrainLayer.diffuseTexture.graphicsFormat); if (diffuseHasAlpha) { disabledByDiffuseAlpha = true; } } if (disabledByDiffuseAlpha) { EditorGUI.BeginDisabledGroup(true); } switch (type) { case ShaderPropertyType.Color: { colorValue = EditorGUILayout.ColorField(TCP2_GUI.TempContent(label), colorValue, true, true, hdr); break; } case ShaderPropertyType.Float: { if (this.isVector4) { // Hack: this defines the 4 Floats mode if (showR) { vectorValue.x = EditorGUILayout.FloatField(TCP2_GUI.TempContent(labelR), vectorValue.x); } if (showG) { vectorValue.y = EditorGUILayout.FloatField(TCP2_GUI.TempContent(labelG), vectorValue.y); } if (showB) { vectorValue.z = EditorGUILayout.FloatField(TCP2_GUI.TempContent(labelB), vectorValue.z); } if (showA) { vectorValue.w = EditorGUILayout.FloatField(TCP2_GUI.TempContent(labelAlpha), vectorValue.w); } } else { floatValue = EditorGUILayout.FloatField(TCP2_GUI.TempContent(label), disabledByDiffuseAlpha ? 1f : floatValue); } break; } case ShaderPropertyType.Range: if (this.isVector4) { // Hack: this defines the Color RGB + Float mode colorValue = EditorGUILayout.ColorField(TCP2_GUI.TempContent(label), colorValue, true, false, true); floatValue = EditorGUILayout.FloatField(TCP2_GUI.TempContent(labelAlpha), floatValue); } else { floatValue = EditorGUILayout.Slider(TCP2_GUI.TempContent(label), disabledByDiffuseAlpha ? 1f : floatValue, rangeLimits.x, rangeLimits.y); } break; case ShaderPropertyType.Vector: { vectorValue = EditorGUILayout.Vector4Field(TCP2_GUI.TempContent(label), vectorValue); break; } case ShaderPropertyType.Texture: textureValue = EditorGUILayout.ObjectField(TCP2_GUI.TempContent(label), textureValue, typeof(Texture2D), false) as Texture2D; break; } if (disabledByDiffuseAlpha) { EditorGUI.EndDisabledGroup(); TCP2_GUI.HelpBoxLayout("The Albedo texture has an alpha channel, so this value is <b>forced at 1.0</b> (this is a limitation of Unity's terrain system).\nPlease either use a <b>texture without alpha</b>, or use <b>another terrain layer data slot</b> in the <b>Shader Properties</b> tab of the <b>Shader Generator 2</b>.", MessageType.Warning); } }
static bool HasAlpha(Texture2D texture) { return(GraphicsFormatUtility.HasAlphaChannel(GraphicsFormatUtility.GetGraphicsFormat(texture.format, false))); }