public GradientSettingsHolder(ShadingMode _mode, GradientSettings _settings, Color _color1, Color _color2, float _gradHeight, float _gradYPos, float _Rotation) { Mode = _mode; gradSettings = _settings; color1 = _color1; color2 = _color2; gradHeight = _gradHeight; gradYPos = _gradYPos; Rotation = _Rotation; }
//Helper Classes and Methods bool IsGlobal(GradientSettings settings) { if (settings == GradientSettings.UseGlobalGradientSettings) { return(true); } else { return(false); } }
public static void SaveTexture (string path, GradientSettings gradient) { var encodedMap = GenerateTexture(gradient).EncodeToPNG(); gradient.lastGradient = new Gradient (); gradient.lastGradient.alphaKeys = gradient.gradient.alphaKeys; gradient.lastGradient.colorKeys = gradient.gradient.colorKeys; File.WriteAllBytes(path, encodedMap); var savedGradient = AssetDatabase.LoadAssetAtPath<Texture2D>(path); savedGradient.wrapMode = TextureWrapMode.Clamp; AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); }
public static void SaveTexture(string path, GradientSettings gradient) { var encodedMap = GenerateTexture(gradient).EncodeToPNG(); gradient.lastGradient = new Gradient(); gradient.lastGradient.alphaKeys = gradient.gradient.alphaKeys; gradient.lastGradient.colorKeys = gradient.gradient.colorKeys; File.WriteAllBytes(path, encodedMap); var savedGradient = AssetDatabase.LoadAssetAtPath <Texture2D>(path); savedGradient.wrapMode = TextureWrapMode.Clamp; AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); }
public static Texture2D GenerateTexture (GradientSettings gradient) { int textureSize = gradient.textureSize; Texture2D outputTexture = new Texture2D ((gradient.textureType == GradientTextureType.HORIZONTAL) ? textureSize : 1, (gradient.textureType == GradientTextureType.VERTICAL) ? textureSize : 1); Color[] colors = new Color[textureSize]; for (int i = 0; i < textureSize; i++) { float gradientPercent = (float)i / (float)(textureSize - 1); colors[i] = gradient.gradient.Evaluate(gradientPercent); } outputTexture.SetPixels(colors); outputTexture.Apply(); outputTexture.wrapMode = TextureWrapMode.Clamp; return outputTexture; }
public static Texture2D GenerateTexture(GradientSettings gradient) { int textureSize = gradient.textureSize; Texture2D outputTexture = new Texture2D((gradient.textureType == GradientTextureType.HORIZONTAL) ? textureSize : 1, (gradient.textureType == GradientTextureType.VERTICAL) ? textureSize : 1); Color[] colors = new Color[textureSize]; for (int i = 0; i < textureSize; i++) { float gradientPercent = (float)i / (float)(textureSize - 1); colors[i] = gradient.gradient.Evaluate(gradientPercent); } outputTexture.SetPixels(colors); outputTexture.Apply(); outputTexture.wrapMode = TextureWrapMode.Clamp; return(outputTexture); }
VectorImageRenderInfo Register(VectorImage vi, VisualElement context) { s_MarkerRegister.Begin(); VectorImageRenderInfo renderInfo = m_RenderInfoPool.Get(); renderInfo.useCount = 1; m_Registered[vi] = renderInfo; if (vi.settings?.Length > 0) { // We first attempt to allocate into the gradient settings atlas since it supports deallocation. int gradientCount = vi.settings.Length; Alloc alloc = m_GradientSettingsAtlas.Add(gradientCount); if (alloc.size > 0) { // Then attempt to allocate in the texture atlas. // TODO: Once the atlas actually processes returns, we should call it at some point. if (m_Atlas.TryGetAtlas(context, vi.atlas, out TextureId atlasId, out RectInt uvs)) { // Remap. GradientRemap previous = null; for (int i = 0; i < gradientCount; ++i) { // Chain. GradientRemap current = m_GradientRemapPool.Get(); if (i > 0) { previous.next = current; } else { renderInfo.firstGradientRemap = current; } previous = current; // Remap the index. current.origIndex = i; current.destIndex = (int)alloc.start + i; // Remap the rect. GradientSettings gradient = vi.settings[i]; RectInt location = gradient.location; location.x += uvs.x; location.y += uvs.y; current.location = location; current.atlas = atlasId; } // Write into the previously allocated gradient settings now that we are sure to use it. m_GradientSettingsAtlas.Write(alloc, vi.settings, renderInfo.firstGradientRemap); } else { // If the texture atlas didn't fit, keep it as a standalone custom texture, only need to remap the setting indices GradientRemap previous = null; for (int i = 0; i < gradientCount; ++i) { GradientRemap current = m_GradientRemapPool.Get(); if (i > 0) { previous.next = current; } else { renderInfo.firstGradientRemap = current; } previous = current; current.origIndex = i; current.destIndex = (int)alloc.start + i; current.atlas = TextureId.invalid; } m_GradientSettingsAtlas.Write(alloc, vi.settings, null); } } else { if (!m_LoggedExhaustedSettingsAtlas) { Debug.LogError("Exhausted max gradient settings (" + m_GradientSettingsAtlas.length + ") for atlas: " + m_GradientSettingsAtlas.atlas?.name); m_LoggedExhaustedSettingsAtlas = true; } } }
public void Write(Alloc alloc, GradientSettings[] settings, GradientRemap remap) { if (disposed) { DisposeHelper.NotifyDisposedUsed(this); return; } if (m_RawAtlas.rgba == null) { m_RawAtlas = new RawTexture { rgba = new Color32[m_ElemWidth * m_Length], width = m_ElemWidth, height = m_Length }; int size = m_ElemWidth * m_Length; for (int i = 0; i < size; ++i) { m_RawAtlas.rgba[i] = Color.black; } } s_MarkerWrite.Begin(); int destY = (int)alloc.start; for (int i = 0, settingsCount = settings.Length; i < settingsCount; ++i) { int destX = 0; GradientSettings entry = settings[i]; Debug.Assert(remap == null || destY == remap.destIndex); if (entry.gradientType == GradientType.Radial) { var focus = entry.radialFocus; focus += Vector2.one; focus /= 2.0f; focus.y = 1.0f - focus.y; m_RawAtlas.WriteRawFloat4Packed((float)GradientType.Radial / 255, (float)entry.addressMode / 255, focus.x, focus.y, destX++, destY); } else if (entry.gradientType == GradientType.Linear) { m_RawAtlas.WriteRawFloat4Packed(0.0f, (float)entry.addressMode / 255, 0.0f, 0.0f, destX++, destY); } Vector2Int pos = new Vector2Int(entry.location.x, entry.location.y); var size = new Vector2(entry.location.width - 1, entry.location.height - 1); if (remap != null) { pos = new Vector2Int(remap.location.x, remap.location.y); size = new Vector2(remap.location.width - 1, remap.location.height - 1); } m_RawAtlas.WriteRawInt2Packed(pos.x, pos.y, destX++, destY); m_RawAtlas.WriteRawInt2Packed((int)size.x, (int)size.y, destX++, destY); remap = remap?.next; ++destY; } MustCommit = true; s_MarkerWrite.End(); }
private VectorImageRenderInfo Register(VectorImage vi) { VectorImageManager.s_MarkerRegister.Begin(); VectorImageRenderInfo vectorImageRenderInfo = this.m_RenderInfoPool.Get(); vectorImageRenderInfo.useCount = 1; this.m_Registered[vi] = vectorImageRenderInfo; GradientSettings[] expr_33 = vi.settings; bool flag = expr_33 != null && expr_33.Length != 0; if (flag) { int num = vi.settings.Length; Alloc alloc = this.m_GradientSettingsAtlas.Add(num); bool flag2 = alloc.size > 0u; if (flag2) { RectInt rectInt; bool flag3 = this.m_AtlasManager.TryGetLocation(vi.atlas, out rectInt); if (flag3) { GradientRemap gradientRemap = null; for (int i = 0; i < num; i++) { GradientRemap gradientRemap2 = this.m_GradientRemapPool.Get(); bool flag4 = i > 0; if (flag4) { gradientRemap.next = gradientRemap2; } else { vectorImageRenderInfo.firstGradientRemap = gradientRemap2; } gradientRemap = gradientRemap2; gradientRemap2.origIndex = i; gradientRemap2.destIndex = (int)(alloc.start + (uint)i); GradientSettings gradientSettings = vi.settings[i]; RectInt location = gradientSettings.location; location.x += rectInt.x; location.y += rectInt.y; gradientRemap2.location = location; gradientRemap2.isAtlassed = true; } this.m_GradientSettingsAtlas.Write(alloc, vi.settings, vectorImageRenderInfo.firstGradientRemap); } else { GradientRemap gradientRemap3 = null; for (int j = 0; j < num; j++) { GradientRemap gradientRemap4 = this.m_GradientRemapPool.Get(); bool flag5 = j > 0; if (flag5) { gradientRemap3.next = gradientRemap4; } else { vectorImageRenderInfo.firstGradientRemap = gradientRemap4; } gradientRemap3 = gradientRemap4; gradientRemap4.origIndex = j; gradientRemap4.destIndex = (int)(alloc.start + (uint)j); gradientRemap4.isAtlassed = false; } this.m_GradientSettingsAtlas.Write(alloc, vi.settings, null); } } else { bool flag6 = !this.m_LoggedExhaustedSettingsAtlas; if (flag6) { string arg_233_0 = "Exhausted max gradient settings ("; string arg_233_1 = this.m_GradientSettingsAtlas.length.ToString(); string arg_233_2 = ") for atlas: "; Texture2D expr_227 = this.m_GradientSettingsAtlas.atlas; Debug.LogError(arg_233_0 + arg_233_1 + arg_233_2 + ((expr_227 != null) ? expr_227.name : null)); this.m_LoggedExhaustedSettingsAtlas = true; } } } VectorImageManager.s_MarkerRegister.End(); return(vectorImageRenderInfo); }
private void CustomShadingModule( ref bool ShowShading, ref MaterialProperty Show, string ShadingSide, ref ShadingMode ShadingType, ref MaterialProperty ShadeMode, ref MaterialProperty Color1, ref MaterialProperty Color2, ref GradientSettings GradSettings, ref MaterialProperty GradientSettings, ref MaterialProperty GradHeight, ref MaterialProperty GradPivot, ref MaterialProperty Rotation, string shaderKeywordG, string shaderKeywordS, MaterialProperty Gizmopos) { EditorGUILayout.BeginVertical(); { ShowShading = EditorGUILayout.Foldout(ShowShading, ShadingSide); } EditorGUILayout.EndVertical(); if (ShowShading) { Show.floatValue = 1; ShadingType = (ShadingMode)EditorGUILayout.EnumPopup("Shading Mode", ShadingType); ShadeMode.floatValue = (float)ShadingType; if (ShadingType == ShadingMode.VertexColor) { targetMat.DisableKeyword(shaderKeywordS); } else if (ShadingType == ShadingMode.SolidColor) { ColorProperty(Color1, "Color"); targetMat.EnableKeyword(shaderKeywordS); } else if (ShadingType == ShadingMode.Gradient_ProOnly) { EditorGUI.BeginDisabledGroup(true); EditorGUILayout.BeginHorizontal("Box"); { EditorGUILayout.BeginVertical(GUILayout.Width(50)); { Color1.colorValue = EditorGUILayout.ColorField(Color1.colorValue); Color2.colorValue = EditorGUILayout.ColorField(Color2.colorValue); if (GUILayout.Button("Swap")) { } Rect R = EditorGUILayout.GetControlRect(GUILayout.Height(50), GUILayout.Width(50)); if (ShadingSide == TOP || ShadingSide == DOWN) { GUI.DrawTexture(R, GetTexture(Color1.colorValue, Color2.colorValue, true)); } else { GUI.DrawTexture(R, GetTexture(Color1.colorValue, Color2.colorValue, false)); } } EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(GUILayout.Width(Screen.width - 112)); { GradSettings = (GradientSettings)EditorGUILayout.EnumPopup("", GradSettings, GUILayout.Width(Screen.width - 110)); GradientSettings.floatValue = (float)GradSettings; EditorGUI.BeginDisabledGroup(IsGlobal(GradSettings)); { EditorGUILayout.BeginHorizontal(); { EditorGUILayout.BeginVertical(GUILayout.Width(Screen.width - 142)); { if (IsGlobal(GradSettings)) { GradHeight.floatValue = CEditor.FloatField("Falloff", 70, _GradientHeight_G.floatValue); EditorGUILayout.LabelField("Pivot", GUILayout.Width(60)); GradPivot.vectorValue = EditorGUILayout.Vector2Field("", _GradPivot_G.vectorValue, GUILayout.Width(Screen.width - 142)); EditorGUILayout.LabelField("Rotation", GUILayout.Width(60)); Rotation.floatValue = EditorGUILayout.Slider(_Rotation_G.floatValue, 0f, 360f, GUILayout.Width(Screen.width - 142)); } else { GradHeight.floatValue = CEditor.FloatField("Falloff", 70, GradHeight.floatValue); EditorGUILayout.LabelField("Pivot", GUILayout.Width(60)); GradPivot.vectorValue = EditorGUILayout.Vector2Field("", GradPivot.vectorValue, GUILayout.Width(Screen.width - 142)); EditorGUILayout.LabelField("Rotation", GUILayout.Width(60)); Rotation.floatValue = EditorGUILayout.Slider(Rotation.floatValue, 0f, 360f, GUILayout.Width(Screen.width - 142)); } } EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(); { EditorGUI.BeginDisabledGroup(!isAnythingSelected()); { if (GUILayout.Button(EditorGUIUtility.IconContent("EditCollider", "Edit in Scene"), GUILayout.Height(28))) { } if (GUILayout.Button(EditorGUIUtility.IconContent("TreeEditor.Duplicate"), GUILayout.Height(28))) { } if (GUILayout.Button(EditorGUIUtility.IconContent("Clipboard", "Paste"), GUILayout.Height(28))) { } } EditorGUI.EndDisabledGroup(); } EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); } EditorGUI.EndDisabledGroup(); } EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); EditorGUI.EndDisabledGroup(); } else { Color1.colorValue = Color.white; Color2.colorValue = Color.white; } } else { Show.floatValue = 0; } }
private void InitializeHelperVars() { if (_ShowFront.floatValue == 0) { showFrontShading = false; } else if (_ShowFront.floatValue == 1) { showFrontShading = true; } if (_ShowBack.floatValue == 0) { showBackShading = false; } else if (_ShowBack.floatValue == 1) { showBackShading = true; } if (_ShowLeft.floatValue == 0) { showLeftShading = false; } else if (_ShowLeft.floatValue == 1) { showLeftShading = true; } if (_ShowRight.floatValue == 0) { showRightShading = false; } else if (_ShowRight.floatValue == 1) { showRightShading = true; } if (_ShowTop.floatValue == 0) { showTopShading = false; } else if (_ShowTop.floatValue == 1) { showTopShading = true; } if (_ShowBottom.floatValue == 0) { showBottomShading = false; } else if (_ShowBottom.floatValue == 1) { showBottomShading = true; } frontShadingMode = (ShadingMode)_Shading_F.floatValue; backShadingMode = (ShadingMode)_Shading_B.floatValue; leftShadingMode = (ShadingMode)_Shading_L.floatValue; rightShadingMode = (ShadingMode)_Shading_R.floatValue; topShadingMode = (ShadingMode)_Shading_T.floatValue; bottomShadingMode = (ShadingMode)_Shading_D.floatValue; frontGradientSettings = (GradientSettings)_GradSettings_F.floatValue; backGradientSettings = (GradientSettings)_GradSettings_B.floatValue; leftGradientSettings = (GradientSettings)_GradSettings_L.floatValue; rightGradientSettings = (GradientSettings)_GradSettings_R.floatValue; topGradientSettings = (GradientSettings)_GradSettings_T.floatValue; bottomGradientSettings = (GradientSettings)_GradSettings_D.floatValue; }
public void Write(Alloc alloc, GradientSettings[] settings, GradientRemap remap) { bool disposed = this.disposed; if (disposed) { DisposeHelper.NotifyDisposedUsed(this); } else { bool flag = this.m_RawAtlas.rgba == null; if (flag) { this.m_RawAtlas = new GradientSettingsAtlas.RawTexture { rgba = new Color32[this.m_ElemWidth * this.m_Length], width = this.m_ElemWidth, height = this.m_Length }; int num = this.m_ElemWidth * this.m_Length; for (int i = 0; i < num; i++) { this.m_RawAtlas.rgba[i] = Color.black; } } GradientSettingsAtlas.s_MarkerWrite.Begin(); int num2 = (int)alloc.start; int j = 0; int num3 = settings.Length; while (j < num3) { int num4 = 0; GradientSettings gradientSettings = settings[j]; Debug.Assert(remap == null || num2 == remap.destIndex); bool flag2 = gradientSettings.gradientType == GradientType.Radial; if (flag2) { Vector2 vector = gradientSettings.radialFocus; vector += Vector2.one; vector /= 2f; vector.y = 1f - vector.y; this.m_RawAtlas.WriteRawFloat4Packed(0.003921569f, (float)gradientSettings.addressMode / 255f, vector.x, vector.y, num4++, num2); } else { bool flag3 = gradientSettings.gradientType == GradientType.Linear; if (flag3) { this.m_RawAtlas.WriteRawFloat4Packed(0f, (float)gradientSettings.addressMode / 255f, 0f, 0f, num4++, num2); } } Vector2Int vector2Int = new Vector2Int(gradientSettings.location.x, gradientSettings.location.y); Vector2 vector2 = new Vector2((float)(gradientSettings.location.width - 1), (float)(gradientSettings.location.height - 1)); bool flag4 = remap != null; if (flag4) { vector2Int = new Vector2Int(remap.location.x, remap.location.y); vector2 = new Vector2((float)(remap.location.width - 1), (float)(remap.location.height - 1)); } this.m_RawAtlas.WriteRawInt2Packed(vector2Int.x, vector2Int.y, num4++, num2); this.m_RawAtlas.WriteRawInt2Packed((int)vector2.x, (int)vector2.y, num4++, num2); remap = ((remap != null) ? remap.next : null); num2++; j++; } this.MustCommit = true; GradientSettingsAtlas.s_MarkerWrite.End(); } }
VectorImageRenderInfo Register(VectorImage vi) { k_RegisterSampler.Begin(); VectorImageRenderInfo renderInfo = m_RenderInfoPool.Get(); renderInfo.useCount = 1; m_Registered[vi] = renderInfo; if (vi.settings?.Length > 0) { // We first attempt to allocate into the gradient settings atlas since it supports deallocation. int gradientCount = vi.settings.Length; Alloc alloc = m_GradientSettingsAtlas.Add(gradientCount); if (alloc.size > 0) { // Then attempt to allocate in the texture atlas. RectInt uvs; if (m_AtlasManager.TryGetLocation(vi.atlas, out uvs)) { // Remap. GradientRemap previous = null; for (int i = 0; i < gradientCount; ++i) { // Chain. GradientRemap current = m_GradientRemapPool.Get(); if (i > 0) { previous.next = current; } else { renderInfo.firstGradientRemap = current; } previous = current; // Remap the index. current.origIndex = i; current.destIndex = (int)alloc.start + i; // Remap the rect. GradientSettings gradient = vi.settings[i]; RectInt location = gradient.location; location.x += uvs.x; location.y += uvs.y; current.location = location; current.isAtlassed = true; } // Write into the previously allocated gradient settings now that we are sure to use it. m_GradientSettingsAtlas.Write(alloc, vi.settings, renderInfo.firstGradientRemap); } else { // If the texture atlas didn't fit, keep it as a standalone custom texture, only need to remap the setting indices GradientRemap previous = null; for (int i = 0; i < gradientCount; ++i) { GradientRemap current = m_GradientRemapPool.Get(); if (i > 0) { previous.next = current; } else { renderInfo.firstGradientRemap = current; } previous = current; current.origIndex = i; current.destIndex = (int)alloc.start + i; current.isAtlassed = false; } m_GradientSettingsAtlas.Write(alloc, vi.settings, null); } } else { if (!m_LoggedExhaustedSettingsAtlas) { Debug.LogError("Exhausted max gradient settings (" + m_GradientSettingsAtlas.length + ") for atlas: " + m_GradientSettingsAtlas.atlas?.name); m_LoggedExhaustedSettingsAtlas = true; } } } k_RegisterSampler.End(); return(renderInfo); }