internal float MaxScale() { float scale = 1; if (m_ScaleWithStroke) { if (m_Stroke != null) { scale = m_Stroke.MaxWidth(); } } else if (m_VariableScales != null && m_VariableScales.Count > 0) { if (m_VariableScales.Count == 1) { scale = m_VariableScales[0].width; } else { for (int i = 0; i < m_VariableScales.Count; i++) { if (m_VariableScales[i].width > scale) { scale = m_VariableScales[i].width; } } } } else { scale = 1; } return(scale); }
private static void RebuildStyleData(Material material, LW_MaterialBuffer materialBuffer, bool forceRebuild = false) { //Debug.Log("RebuildStyleData material: " + material.name); if (!(material != null && materialBuffer != null && materialBuffer.vertexBuffers != null && materialBuffer.isAdvancedShader && (forceRebuild || materialBuffer.isDirty))) { return; } List <LW_VertexBuffer> vertexBuffers = materialBuffer.vertexBuffers; Stack <int> emptyIndices = materialBuffer.emptyIndices; // Setup int width = s_GradientDataLength + s_GradientPrecision; int height = vertexBuffers.Count; bool hasChanged = false; bool rebuildAllRows = forceRebuild; Texture2D styleData = material.GetTexture("_StyleData") as Texture2D; // Resizing StyleData if (styleData != null) { if (styleData.height < height) { int oldHeight = styleData.height; int oldWidth = styleData.width; Color[] pixels = styleData.GetPixels(); styleData.Resize(width, height, TextureFormat.RGBAFloat, false); styleData.SetPixels(0, 0, oldWidth, oldHeight, pixels, 0); material.SetVector("_StyleDataSize", new Vector4(width, height, s_GradientDataLength, s_GradientPrecision)); hasChanged = true; } else if (styleData.height > height) { styleData = null; } } // Creating StyleData if (styleData == null) { styleData = new Texture2D(width, height, TextureFormat.RGBAFloat, false); styleData.hideFlags = HideFlags.DontSave; styleData.filterMode = FilterMode.Bilinear; styleData.wrapMode = TextureWrapMode.Clamp; styleData.anisoLevel = 0; material.SetTexture("_StyleData", styleData); material.SetVector("_StyleDataSize", new Vector4(width, height, s_GradientDataLength, s_GradientPrecision)); hasChanged = true; rebuildAllRows = true; } // Updating StyleData for (int i = 0; i < vertexBuffers.Count; i++) { int index = i; LW_VertexBuffer buffer = vertexBuffers[i]; if (buffer.isEmpty || emptyIndices.Contains(i) || (buffer.styleDataIndex == index && !buffer.style.isDirty && !rebuildAllRows)) { continue; } //Debug.Log("buffer.styleDataIndex = " + index); hasChanged = true; buffer.styleDataIndex = index; //LW_Graphic graphic = buffer.graphic; //LW_Canvas canvas = buffer.canvas; LW_PaintStyle style = buffer.style as LW_PaintStyle; float boundsMin = Mathf.Min(buffer.bounds.min.x, buffer.bounds.min.y); float boundsMax = Mathf.Max(buffer.bounds.max.x, buffer.bounds.max.y); float strokeJustification = -1; float strokeWidth = 0; float strokeLength = 0; float screenSpaceMiterLimit = 0; float caps = -1; float joins = -1; // Vertex Data if (style is LW_Stroke) { LW_Stroke stroke = style as LW_Stroke; strokeJustification = ((int)stroke.justification - 1); strokeWidth = stroke.MaxWidth(); strokeLength = buffer.totalLength; screenSpaceMiterLimit = stroke.screenSpace ? stroke.miterLimit : -stroke.miterLimit; caps = (int)stroke.linecap; joins = (int)stroke.linejoin; } if (style is LW_Fill) { strokeWidth = buffer.bounds.size.y; strokeLength = buffer.bounds.size.x; caps = (int)Linecap.Round; joins = (int)Linejoin.Round; } // Fragment Data Matrix4x4 gradTransform = style.gradientUnits == GradientUnits.objectBoundingBox ? style.BoundsToLocalMatrix(buffer.bounds) * style.gradientTransform * style.LocalToBoundsMatrix(buffer.bounds) : style.gradientTransform; Vector2 start = gradTransform.MultiplyPoint(style.gradientStart); Vector2 end = gradTransform.MultiplyPoint(style.gradientEnd); Vector2 gradientDirection = end - start; float gradAngle = Mathf.Atan2(gradientDirection.x, gradientDirection.y); float gradScale = gradientDirection.magnitude; styleData.SetPixel(0, index, new Color(screenSpaceMiterLimit, strokeJustification, strokeWidth, strokeLength)); styleData.SetPixel(1, index, new Color((int)style.uvMode, (int)style.gradientUnits, boundsMin, boundsMax)); styleData.SetPixel(2, index, new Color(start.x, start.y, (int)style.gradientSpreadMethod, (int)style.paintMode)); styleData.SetPixel(3, index, new Color(gradAngle, gradScale, caps, joins)); for (int p = 0; p < s_GradientPrecision + 1; p++) { float percentage = (float)p / (float)s_GradientPrecision; Color color = style.ColorAtPercentage(percentage); styleData.SetPixel(s_GradientDataLength + p, i, color); } } if (hasChanged) { if (!s_DirtyTextures.Contains(styleData)) { s_DirtyTextures.Push(styleData); } } materialBuffer.isDirty = false; }