[OvldGenCallTarget] public static void Sphere([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, [OvldDefault(nameof(SphereRadiusSpace))] ThicknessSpace spaceRadius, Vector3 pos, [OvldDefault(nameof(SphereRadius))] float radius, [OvldDefault(nameof(Color))] Color color) { Material mat = ShapesMaterialUtils.matSphere[blendMode]; ApplyGlobalProperties(mat); mat.SetColor(ShapesMaterialUtils.propColor, color); mat.SetFloat(ShapesMaterialUtils.propRadius, radius); mat.SetInt(ShapesMaterialUtils.propRadiusSpace, (int)spaceRadius); DrawMesh(pos, Quaternion.identity, ShapesMeshUtils.SphereMesh, mat); }
[OvldGenCallTarget] public static void Cuboid([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, [OvldDefault(nameof(CuboidSizeSpace))] ThicknessSpace sizeSpace, Vector3 pos, [OvldDefault("Quaternion.identity")] Quaternion rot, Vector3 size, [OvldDefault(nameof(Color))] Color color) { Material mat = ShapesMaterialUtils.matCuboid[blendMode]; mat.SetColor(ShapesMaterialUtils.propColor, color); mat.SetVector(ShapesMaterialUtils.propSize, size); mat.SetInt(ShapesMaterialUtils.propSizeSpace, (int)sizeSpace); DrawMesh(pos, rot, ShapesMeshUtils.CuboidMesh, mat); }
static string GetShaderBlendMode(this ShapesBlendMode blendMode) { switch (blendMode) { case ShapesBlendMode.Opaque: return("One Zero"); case ShapesBlendMode.Transparent: return("SrcAlpha OneMinusSrcAlpha"); case ShapesBlendMode.Additive: return("One One"); case ShapesBlendMode.Multiplicative: return("DstColor Zero"); default: throw new ArgumentOutOfRangeException(nameof(blendMode), blendMode, null); } }
static void DiscCore(ShapesBlendMode blendMode, ThicknessSpace spaceRadius, ThicknessSpace spaceThickness, bool hollow, bool sector, Vector3 pos, Quaternion rot, float radius, float thickness, Color colorInnerStart, Color colorOuterStart, Color colorInnerEnd, Color colorOuterEnd, DashStyle dashStyle = null, float angleRadStart = 0f, float angleRadEnd = 0f, ArcEndCap arcEndCaps = ArcEndCap.None) { if (sector && Mathf.Abs(angleRadEnd - angleRadStart) < 0.0001f) { return; } Material mat = ShapesMaterialUtils.GetDiscMaterial(hollow, sector)[blendMode]; ApplyGlobalProperties(mat); mat.SetFloat(ShapesMaterialUtils.propRadius, radius); mat.SetInt(ShapesMaterialUtils.propRadiusSpace, (int)spaceRadius); mat.SetInt(ShapesMaterialUtils.propAlignment, (int)Draw.DiscGeometry); if (hollow) { mat.SetInt(ShapesMaterialUtils.propThicknessSpace, (int)spaceThickness); mat.SetFloat(ShapesMaterialUtils.propThickness, thickness); mat.SetInt(ShapesMaterialUtils.propScaleMode, (int)ScaleMode); } if (sector) { mat.SetFloat(ShapesMaterialUtils.propAngStart, angleRadStart); mat.SetFloat(ShapesMaterialUtils.propAngEnd, angleRadEnd); if (hollow) { mat.SetFloat(ShapesMaterialUtils.propRoundCaps, (int)arcEndCaps); } } mat.SetColor(ShapesMaterialUtils.propColor, colorInnerStart); mat.SetColor(ShapesMaterialUtils.propColorOuterStart, colorOuterStart); mat.SetColor(ShapesMaterialUtils.propColorInnerEnd, colorInnerEnd); mat.SetColor(ShapesMaterialUtils.propColorOuterEnd, colorOuterEnd); bool dashed = dashStyle?.size > 0f; mat.SetFloat(ShapesMaterialUtils.propDashSize, dashed ? dashStyle.GetNetAbsoluteSize(true, thickness) : 0); if (dashed) { mat.SetInt(ShapesMaterialUtils.propDashType, (int)dashStyle.type); mat.SetInt(ShapesMaterialUtils.propDashSpace, (int)dashStyle.space); mat.SetInt(ShapesMaterialUtils.propDashSnap, (int)dashStyle.snap); mat.SetFloat(ShapesMaterialUtils.propDashOffset, dashStyle.offset); mat.SetFloat(ShapesMaterialUtils.propDashSpacing, dashStyle.GetNetAbsoluteSpacing(true, thickness)); } DrawMesh(pos, rot, ShapesMeshUtils.QuadMesh, mat); }
[OvldGenCallTarget] public static void Polyline([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, PolylinePath path, [OvldDefault("false")] bool closed, [OvldDefault(nameof(PolylineGeometry))] PolylineGeometry geometry, [OvldDefault(nameof(PolylineJoins))] PolylineJoins joins, [OvldDefault(nameof(LineThickness))] float thickness, [OvldDefault(nameof(LineThicknessSpace))] ThicknessSpace thicknessSpace, [OvldDefault(nameof(Color))] Color color) { if (path.EnsureMeshIsReadyToRender(closed, joins, out Mesh mesh) == false) { return; // no points defined in the mesh } switch (path.Count) { case 0: Debug.LogWarning("Tried to draw polyline with no points"); return; case 1: Debug.LogWarning("Tried to draw polyline with only one point"); return; } Material matPolyLine = ShapesMaterialUtils.GetPolylineMat(joins)[blendMode]; ApplyGlobalProperties(matPolyLine); matPolyLine.SetFloat(ShapesMaterialUtils.propThickness, thickness); matPolyLine.SetFloat(ShapesMaterialUtils.propThicknessSpace, (int)thicknessSpace); matPolyLine.SetColor(ShapesMaterialUtils.propColor, color); matPolyLine.SetInt(ShapesMaterialUtils.propAlignment, (int)geometry); matPolyLine.SetInt(ShapesMaterialUtils.propScaleMode, (int)ScaleMode); if (joins == PolylineJoins.Miter) { DrawMesh(Vector3.zero, Quaternion.identity, mesh, matPolyLine); } else { Material matPolyLineJoins = ShapesMaterialUtils.GetPolylineJoinsMat(joins)[blendMode]; ApplyGlobalProperties(matPolyLineJoins); matPolyLineJoins.SetFloat(ShapesMaterialUtils.propThickness, thickness); matPolyLineJoins.SetFloat(ShapesMaterialUtils.propThicknessSpace, (int)thicknessSpace); matPolyLineJoins.SetColor(ShapesMaterialUtils.propColor, color); matPolyLineJoins.SetInt(ShapesMaterialUtils.propAlignment, (int)geometry); matPolyLineJoins.SetInt(ShapesMaterialUtils.propScaleMode, (int)ScaleMode); DrawTwoSubmeshes(Vector3.zero, Quaternion.identity, mesh, matPolyLine, matPolyLineJoins); } }
[OvldGenCallTarget] static void Triangle([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, Vector3 a, Vector3 b, Vector3 c, [OvldDefault(nameof(Color))] Color colorA, [OvldDefault(nameof(Color))] Color colorB, [OvldDefault(nameof(Color))] Color colorC) { using (new IMDrawer(mpbTriangle, ShapesMaterialUtils.matTriangle[blendMode], ShapesMeshUtils.TriangleMesh[0])) { mpbTriangle.a.Add(a); mpbTriangle.b.Add(b); mpbTriangle.c.Add(c); mpbTriangle.color.Add(colorA); mpbTriangle.colorB.Add(colorB); mpbTriangle.colorC.Add(colorC); } }
[OvldGenCallTarget] public static void Cone([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, [OvldDefault(nameof(ConeSizeSpace))] ThicknessSpace sizeSpace, Vector3 pos, [OvldDefault("Quaternion.identity")] Quaternion rot, float radius, float length, [OvldDefault("true")] bool fillCap, [OvldDefault(nameof(Color))] Color color) { Material mat = ShapesMaterialUtils.matCone[blendMode]; mat.SetColor(ShapesMaterialUtils.propColor, color); mat.SetFloat(ShapesMaterialUtils.propRadius, radius); mat.SetFloat(ShapesMaterialUtils.propLength, length); mat.SetInt(ShapesMaterialUtils.propSizeSpace, (int)sizeSpace); DrawMesh(pos, rot, fillCap ? ShapesMeshUtils.ConeMesh : ShapesMeshUtils.ConeMeshUncapped, mat); }
public static IEnumerable <string> GetPassRenderStates(this ShapesBlendMode blendMode) { yield return("Cull Off"); if (blendMode.ZWrite() == false) { yield return("ZWrite Off"); } if (blendMode.AlphaToMask()) { yield return("AlphaToMask On"); } if (blendMode.HasSpecialBlendMode()) { yield return($"Blend {blendMode.GetShaderBlendMode()}"); } }
[OvldGenCallTarget] public static void Triangle([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, Vector3 a, Vector3 b, Vector3 c, [OvldDefault(nameof(Color))] Color colorA, [OvldDefault(nameof(Color))] Color colorB, [OvldDefault(nameof(Color))] Color colorC) { Material mat = ShapesMaterialUtils.matTriangle[blendMode]; mat.SetVector(ShapesMaterialUtils.propA, a); mat.SetVector(ShapesMaterialUtils.propB, b); mat.SetVector(ShapesMaterialUtils.propC, c); mat.SetColor(ShapesMaterialUtils.propColor, colorA); mat.SetColor(ShapesMaterialUtils.propColorB, colorB); mat.SetColor(ShapesMaterialUtils.propColorC, colorC); DrawMesh(Vector3.zero, Quaternion.identity, ShapesMeshUtils.TriangleMesh, mat); }
[OvldGenCallTarget] static void Cone([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, [OvldDefault(nameof(ConeSizeSpace))] ThicknessSpace sizeSpace, Vector3 pos, [OvldDefault("Quaternion.identity")] Quaternion rot, float radius, float length, [OvldDefault("true")] bool fillCap, [OvldDefault(nameof(Color))] Color color) { Mesh mesh = fillCap ? ShapesMeshUtils.ConeMesh[(int)DetailLevel] : ShapesMeshUtils.ConeMeshUncapped[(int)DetailLevel]; using (new IMDrawer(mpbCone, ShapesMaterialUtils.matCone[blendMode], mesh, pos, rot)) { mpbCone.color.Add(color); mpbCone.radius.Add(radius); mpbCone.length.Add(length); mpbCone.sizeSpace.Add((float)sizeSpace); } }
[OvldGenCallTarget] public static void Rectangle([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, [OvldDefault("false")] bool hollow, [OvldDefault("Vector3.zero")] Vector3 pos, [OvldDefault("Quaternion.identity")] Quaternion rot, Rect rect, [OvldDefault(nameof(Color))] Color color, [OvldDefault("0f")] float thickness = 0f, [OvldDefault("default")] Vector4 cornerRadii = default) { bool rounded = ShapesMath.MaxComp(cornerRadii) >= 0.0001f; // positive vibes only if (rect.width < 0) { rect.x -= rect.width *= -1; } if (rect.height < 0) { rect.y -= rect.height *= -1; } if (hollow && thickness * 2 >= Mathf.Min(rect.width, rect.height)) { hollow = false; } Material mat = ShapesMaterialUtils.GetRectMaterial(hollow, rounded)[blendMode]; ApplyGlobalProperties(mat); mat.SetColor(ShapesMaterialUtils.propColor, color); mat.SetVector(ShapesMaterialUtils.propRect, rect.ToVector4()); if (rounded) { mat.SetVector(ShapesMaterialUtils.propCornerRadii, cornerRadii); } if (hollow) { mat.SetFloat(ShapesMaterialUtils.propThickness, thickness); mat.SetInt(ShapesMaterialUtils.propScaleMode, (int)ScaleMode); } DrawMesh(pos, rot, ShapesMeshUtils.QuadMesh, mat); }
[OvldGenCallTarget] static void Polyline([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, PolylinePath path, [OvldDefault("false")] bool closed, [OvldDefault(nameof(PolylineGeometry))] PolylineGeometry geometry, [OvldDefault(nameof(PolylineJoins))] PolylineJoins joins, [OvldDefault(nameof(LineThickness))] float thickness, [OvldDefault(nameof(LineThicknessSpace))] ThicknessSpace thicknessSpace, [OvldDefault(nameof(Color))] Color color) { if (path.EnsureMeshIsReadyToRender(closed, joins, out Mesh mesh) == false) { return; // no points defined in the mesh } switch (path.Count) { case 0: Debug.LogWarning("Tried to draw polyline with no points"); return; case 1: Debug.LogWarning("Tried to draw polyline with only one point"); return; } void ApplyToMpb(MpbPolyline mpb) { mpb.thickness.Add(thickness); mpb.thicknessSpace.Add((int)thicknessSpace); mpb.color.Add(color); mpb.alignment.Add((int)geometry); mpb.scaleMode.Add((int)ScaleMode); } using (new IMDrawer(mpbPolyline, ShapesMaterialUtils.GetPolylineMat(joins)[blendMode], mesh, 0)) ApplyToMpb(mpbPolyline); if (joins.HasJoinMesh()) { using (new IMDrawer(mpbPolylineJoins, ShapesMaterialUtils.GetPolylineJoinsMat(joins)[blendMode], mesh, 1)) ApplyToMpb(mpbPolylineJoins); } }
[OvldGenCallTarget] static void Quad([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, Vector3 a, Vector3 b, Vector3 c, [OvldDefault("a + ( c - b )")] Vector3 d, [OvldDefault(nameof(Color))] Color colorA, [OvldDefault(nameof(Color))] Color colorB, [OvldDefault(nameof(Color))] Color colorC, [OvldDefault(nameof(Color))] Color colorD) { using (new IMDrawer(mpbQuad, ShapesMaterialUtils.matQuad[blendMode], ShapesMeshUtils.QuadMesh[0])) { mpbQuad.a.Add(a); mpbQuad.b.Add(b); mpbQuad.c.Add(c); mpbQuad.d.Add(d); mpbQuad.color.Add(colorA); mpbQuad.colorB.Add(colorB); mpbQuad.colorC.Add(colorC); mpbQuad.colorD.Add(colorD); } }
[OvldGenCallTarget] public static void Quad([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, Vector3 a, Vector3 b, Vector3 c, [OvldDefault("a + ( c - b )")] Vector3 d, [OvldDefault(nameof(Color))] Color colorA, [OvldDefault(nameof(Color))] Color colorB, [OvldDefault(nameof(Color))] Color colorC, [OvldDefault(nameof(Color))] Color colorD) { Material mat = ShapesMaterialUtils.matQuad[blendMode]; mat.SetVector(ShapesMaterialUtils.propA, a); mat.SetVector(ShapesMaterialUtils.propB, b); mat.SetVector(ShapesMaterialUtils.propC, c); mat.SetVector(ShapesMaterialUtils.propD, d); mat.SetColor(ShapesMaterialUtils.propColor, colorA); mat.SetColor(ShapesMaterialUtils.propColorB, colorB); mat.SetColor(ShapesMaterialUtils.propColorC, colorC); mat.SetColor(ShapesMaterialUtils.propColorD, colorD); DrawMesh(Vector3.zero, Quaternion.identity, ShapesMeshUtils.QuadMesh, mat); }
[OvldGenCallTarget] public static void Line([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, [OvldDefault(nameof(LineGeometry))] LineGeometry geometry, [OvldDefault(nameof(LineEndCaps))] LineEndCap endCaps, [OvldDefault(nameof(LineThicknessSpace))] ThicknessSpace thicknessSpace, Vector3 start, Vector3 end, [OvldDefault(nameof(Color))] Color colorStart, [OvldDefault(nameof(Color))] Color colorEnd, [OvldDefault(nameof(LineThickness))] float thickness, [OvldDefault(nameof(LineDashStyle))] DashStyle dashStyle = null) { Material mat = ShapesMaterialUtils.GetLineMat(geometry, endCaps)[blendMode]; ApplyGlobalProperties(mat); mat.SetColor(ShapesMaterialUtils.propColor, colorStart); mat.SetColor(ShapesMaterialUtils.propColorEnd, colorEnd); mat.SetVector(ShapesMaterialUtils.propPointStart, start); mat.SetVector(ShapesMaterialUtils.propPointEnd, end); mat.SetFloat(ShapesMaterialUtils.propThickness, thickness); mat.SetInt(ShapesMaterialUtils.propAlignment, (int)geometry); mat.SetInt(ShapesMaterialUtils.propThicknessSpace, (int)thicknessSpace); mat.SetInt(ShapesMaterialUtils.propScaleMode, (int)ScaleMode); bool dashed = dashStyle?.size > 0f; mat.SetFloat(ShapesMaterialUtils.propDashSize, dashed ? dashStyle.GetNetAbsoluteSize(true, thickness) : 0); if (dashed) { if (geometry != LineGeometry.Volumetric3D) { mat.SetInt(ShapesMaterialUtils.propDashType, (int)dashStyle.type); } mat.SetInt(ShapesMaterialUtils.propDashSpace, (int)dashStyle.space); mat.SetInt(ShapesMaterialUtils.propDashSnap, (int)dashStyle.snap); mat.SetFloat(ShapesMaterialUtils.propDashOffset, dashStyle.offset); mat.SetFloat(ShapesMaterialUtils.propDashSpacing, dashStyle.GetNetAbsoluteSpacing(true, thickness)); } DrawMesh(Vector3.zero, Quaternion.identity, ShapesMeshUtils.GetLineMesh(geometry, endCaps), mat); }
static void DiscCore(ShapesBlendMode blendMode, ThicknessSpace spaceRadius, ThicknessSpace spaceThickness, bool hollow, bool sector, Vector3 pos, Quaternion rot, float radius, float thickness, Color colorInnerStart, Color colorOuterStart, Color colorInnerEnd, Color colorOuterEnd, DashStyle dashStyle = null, float angleRadStart = 0f, float angleRadEnd = 0f, ArcEndCap arcEndCaps = ArcEndCap.None) { if (sector && Mathf.Abs(angleRadEnd - angleRadStart) < 0.0001f) { return; } Material mat = ShapesMaterialUtils.GetDiscMaterial(hollow, sector)[blendMode]; ApplyGlobalProperties(mat); mat.SetFloat(ShapesMaterialUtils.propRadius, radius); mat.SetInt(ShapesMaterialUtils.propRadiusSpace, (int)spaceRadius); mat.SetInt(ShapesMaterialUtils.propAlignment, (int)Draw.DiscGeometry); if (hollow) { mat.SetInt(ShapesMaterialUtils.propThicknessSpace, (int)spaceThickness); mat.SetFloat(ShapesMaterialUtils.propThickness, thickness); mat.SetInt(ShapesMaterialUtils.propScaleMode, (int)ScaleMode); } if (sector) { mat.SetFloat(ShapesMaterialUtils.propAngStart, angleRadStart); mat.SetFloat(ShapesMaterialUtils.propAngEnd, angleRadEnd); if (hollow) { mat.SetFloat(ShapesMaterialUtils.propRoundCaps, (int)arcEndCaps); } } mat.SetColor(ShapesMaterialUtils.propColor, colorInnerStart); mat.SetColor(ShapesMaterialUtils.propColorOuterStart, colorOuterStart); mat.SetColor(ShapesMaterialUtils.propColorInnerEnd, colorInnerEnd); mat.SetColor(ShapesMaterialUtils.propColorOuterEnd, colorOuterEnd); ApplyDashSettings(mat, dashStyle, thickness); DrawMesh(pos, rot, ShapesMeshUtils.QuadMesh, mat); }
public ShaderBuilder(string name, ShapesBlendMode blendMode, RenderPipeline rp) { this.blendMode = blendMode; this.shaderName = name; using (Scope($"Shader \"Shapes/{name} {blendMode.ToString()}\"")) { using (Scope("Properties")) { AppendLine("[Enum(UnityEngine.Rendering.CompareFunction)] _ZTest (\"Z Test\", int) = 4"); AppendLine("_ZOffsetFactor (\"Z Offset Factor\", Float ) = 0"); AppendLine("_ZOffsetUnits (\"Z Offset Units\", int ) = 0"); AppendLine("[Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp (\"Stencil Comparison\", int) = 8"); AppendLine("[Enum(UnityEngine.Rendering.StencilOp)] _StencilOpPass (\"Stencil Operation Pass\", int) = 0"); AppendLine("_StencilID (\"Stencil ID\", int) = 0"); AppendLine("_StencilReadMask (\"Stencil Read Mask\", int) = 255"); AppendLine("_StencilWriteMask (\"Stencil Write Mask\", int) = 255"); if (name == "Texture") { AppendLine("_MainTex (\"Texture\", 2D) = \"white\" {}"); } } using (Scope("SubShader")) { using (Scope("Tags")) { // subshader tags AppendLines(rp.GetSubshaderTags()); AppendLines(blendMode.GetSubshaderTags()); } AppendPass(ShaderPassType.Render, rp); if (rp != RenderPipeline.Legacy) { AppendPass(ShaderPassType.DepthOnly, rp); } AppendPass(ShaderPassType.Picking, rp); AppendPass(ShaderPassType.Selection, rp); } } }
static void DiscCore(ShapesBlendMode blendMode, ThicknessSpace spaceRadius, ThicknessSpace spaceThickness, bool hollow, bool sector, Vector3 pos, Quaternion rot, float radius, float thickness, Color colorInnerStart, Color colorOuterStart, Color colorInnerEnd, Color colorOuterEnd, DashStyle dashStyle = null, float angleRadStart = 0f, float angleRadEnd = 0f, ArcEndCap arcEndCaps = ArcEndCap.None) { if (sector && Mathf.Abs(angleRadEnd - angleRadStart) < 0.0001f) { return; } using (new IMDrawer(mpbDisc, ShapesMaterialUtils.GetDiscMaterial(hollow, sector)[blendMode], ShapesMeshUtils.QuadMesh[0], pos, rot)) { MetaMpb.ApplyDashSettings(mpbDisc, dashStyle, thickness); mpbDisc.radius.Add(radius); mpbDisc.radiusSpace.Add((int)spaceRadius); mpbDisc.alignment.Add((int)Draw.DiscGeometry); mpbDisc.thicknessSpace.Add((int)spaceThickness); mpbDisc.thickness.Add(thickness); mpbDisc.scaleMode.Add((int)ScaleMode); mpbDisc.angStart.Add(angleRadStart); mpbDisc.angEnd.Add(angleRadEnd); mpbDisc.roundCaps.Add((int)arcEndCaps); mpbDisc.color.Add(colorInnerStart); mpbDisc.colorOuterStart.Add(colorOuterStart); mpbDisc.colorInnerEnd.Add(colorInnerEnd); mpbDisc.colorOuterEnd.Add(colorOuterEnd); } }
[OvldGenCallTarget] public static void RegularPolygon([OvldDefault(nameof(BlendMode))] ShapesBlendMode blendMode, [OvldDefault(nameof(RegularPolygonRadiusSpace))] ThicknessSpace spaceRadius, [OvldDefault(nameof(RegularPolygonThicknessSpace))] ThicknessSpace spaceThickness, Vector3 pos, [OvldDefault("Quaternion.identity")] Quaternion rot, [OvldDefault(nameof(RegularPolygonSideCount))] int sideCount, [OvldDefault(nameof(RegularPolygonRadius))] float radius, [OvldDefault(nameof(RegularPolygonThickness))] float thickness, [OvldDefault(nameof(Color))] Color color, bool hollow, [OvldDefault("0f")] float roundness, [OvldDefault("0f")] float angle, [OvldDefault(nameof(PolygonShapeFill))] ShapeFill fill) { Material mat = ShapesMaterialUtils.matRegularPolygon[blendMode]; ApplyGlobalProperties(mat); mat.SetFloat(ShapesMaterialUtils.propRadius, radius); mat.SetInt(ShapesMaterialUtils.propRadiusSpace, (int)spaceRadius); mat.SetInt(ShapesMaterialUtils.propAlignment, (int)Draw.RegularPolygonGeometry); mat.SetInt(ShapesMaterialUtils.propSides, Mathf.Max(3, sideCount)); mat.SetFloat(ShapesMaterialUtils.propAng, angle); mat.SetFloat(ShapesMaterialUtils.propRoundness, roundness); mat.SetInt(ShapesMaterialUtils.propHollow, hollow.AsInt()); if (hollow) { mat.SetInt(ShapesMaterialUtils.propThicknessSpace, (int)spaceThickness); mat.SetFloat(ShapesMaterialUtils.propThickness, thickness); mat.SetInt(ShapesMaterialUtils.propScaleMode, (int)ScaleMode); } TryApplyFillAndColor(mat, fill, color); DrawMesh(pos, rot, ShapesMeshUtils.QuadMesh, mat); }
public static void GenerateShadersAndMaterials() { int blendModeCount = System.Enum.GetNames(typeof(ShapesBlendMode)).Length; const string CORE_SUFFIX = " Core"; IEnumerable <TextAsset> shaderCores = ShapesIO.LoadAllAssets <TextAsset>(ShapesIO.CoreShaderFolder); IEnumerable <string> shaderNames = shaderCores.Select(x => x.name.Substring(0, x.name.Length - CORE_SUFFIX.Length)); // generate all shaders foreach (string name in shaderNames) { for (int i = 0; i < blendModeCount; i++) { ShapesBlendMode blendMode = (ShapesBlendMode)i; string path = $"{ShapesIO.GeneratedShadersFolder}/{name} {blendMode}.shader"; string shaderContents = new ShaderBuilder(name, blendMode).shader; File.WriteAllText(path, shaderContents); } } // reimport all assets to load newly generated shaders AssetDatabase.Refresh(ImportAssetOptions.Default); // generate all materials foreach (string name in shaderNames) { for (int i = 0; i < blendModeCount; i++) { ShapesBlendMode blendMode = (ShapesBlendMode)i; string nameWithBlendMode = ShapesMaterials.GetMaterialName(name, blendMode.ToString()); Shader shader = Shader.Find($"Shapes/{nameWithBlendMode}"); if (shader == null) { Debug.LogError("missing shader " + $"Shapes/{nameWithBlendMode}"); continue; } if (ShaderBuilder.shaderKeywords.ContainsKey(name)) { // create all permutations MultiCompile[] multis = ShaderBuilder.shaderKeywords[name]; List <string> keywordPermutations = new List <string>(); foreach (IEnumerable <string> perm in GetPermutations(multis.Select(m => m.Enumerate()))) { IEnumerable <string> validKeywords = perm.Where(p => string.IsNullOrEmpty(p) == false); string kws = $" [{string.Join( "][", validKeywords )}]"; if (kws.Contains("[]")) // this means it has no permutations { kws = ""; } TryCreateMaterial(nameWithBlendMode + kws, validKeywords); } } else { TryCreateMaterial(nameWithBlendMode); } Material TryCreateMaterial(string fullMaterialName, IEnumerable <string> keywords = null) { string savePath = $"{ShapesIO.GeneratedMaterialsFolder}/{fullMaterialName}.mat"; Material mat = AssetDatabase.LoadAssetAtPath <Material>(savePath); void TrySetKeywords() { if (keywords != null) { foreach (string keyword in keywords) { mat.EnableKeyword(keyword); } } } if (mat != null) { EditorUtility.SetDirty(mat); mat.hideFlags = HideFlags.HideInInspector; TrySetKeywords(); } else { Debug.Log("creating material " + savePath); mat = new Material(shader) { enableInstancing = true, hideFlags = HideFlags.HideInInspector }; TrySetKeywords(); AssetDatabase.CreateAsset(mat, savePath); } return(mat); } } } AssetDatabase.Refresh(ImportAssetOptions.Default); }
static string Queue(this ShapesBlendMode blendMode) => blendMode == ShapesBlendMode.Opaque ? "AlphaTest" : "Transparent";
void DrawShapes() { Vector2 center = position.size / 2; float fitRadius = Mathf.Min(position.width, position.height) / 2 - 8; // set doot positions float t = (float)EditorApplication.timeSinceStartup / 2; foreach (Doot doot in doots) { float ang = doot.angSpeed * t * ShapesMath.TAU + doot.angOffset; Vector2 dir = ShapesMath.AngToDir(ang); doot.pos = dir * (fitRadius * doot.radialOffset); } // mouse doot~ Vector2 mouseRawPos = Event.current.mousePosition - center; float maxRadius = fitRadius * DOOT_MAX_RADIUS; Vector2 mouseTargetPos = Vector2.ClampMagnitude(mouseRawPos, maxRadius); doots[0].pos = Vector2.Lerp(doots[0].pos, mouseTargetPos, mouseDootT); bool mouseOver = mouseOverWindow == this; mouseDootT = Mathf.Lerp(mouseDootT, mouseOver ? 1f : 0f, 0.05f); // save state Matrix4x4 prevMtx = Draw.Matrix; ShapesBlendMode prevBlendMode = Draw.BlendMode; ThicknessSpace prevDiscRadiusSpace = Draw.DiscRadiusSpace; ThicknessSpace prevLineThicknessSpace = Draw.LineThicknessSpace; LineGeometry prevLineGeometry = Draw.LineGeometry; ThicknessSpace prevRingThicknessSpace = Draw.RingThicknessSpace; // draw setup Draw.Matrix = Matrix4x4.TRS(new Vector3(center.x, center.y, 1f), Quaternion.identity, Vector3.one); Draw.BlendMode = ShapesBlendMode.Transparent; Draw.DiscRadiusSpace = ThicknessSpace.Meters; Draw.LineThicknessSpace = ThicknessSpace.Meters; Draw.LineGeometry = LineGeometry.Flat2D; Draw.RingThicknessSpace = ThicknessSpace.Meters; // Drawing Draw.RingGradientRadial(Vector3.zero, fitRadius, fitRadius * 0.1f, Color.black, new Color(0, 0, 0, 0)); Draw.Disc(Vector3.zero, fitRadius, Color.black); // edge noodles const int noodCount = 64; for (int i = 0; i < noodCount; i++) { float tDir = i / ((float)noodCount); float tAng = ShapesMath.TAU * tDir; Vector2 dir = ShapesMath.AngToDir(tAng); if (Mathf.Abs(dir.y) > 0.75f) { continue; } Vector2 root = dir * fitRadius; float distToNearest = float.MaxValue; for (int j = 0; j < doots.Length; j++) { distToNearest = Mathf.Min(distToNearest, Vector2.Distance(doots[j].pos, root)); } float distMod = Mathf.InverseLerp(fitRadius * 0.5f, fitRadius * 0.1f, distToNearest); float noodMaxOffset = fitRadius * (1 + 0.1f * distMod); Draw.Line(root, dir * noodMaxOffset, fitRadius * Mathf.Lerp(0.07f, 0.04f, distMod)); } // ring Draw.Ring(Vector3.zero, fitRadius, fitRadius * 0.0125f, colMain); // connecting lines for (int i = 0; i < doots.Length; i++) { Vector2 a = doots[i].pos; for (int j = i; j < doots.Length; j++) { Vector2 b = doots[j].pos; float dist = Vector2.Distance(a, b); float rangeValue = Mathf.InverseLerp(fitRadius * 1f, fitRadius * 0.02f, dist); if (rangeValue > 0) { Color col = Color.Lerp(colFade, colMain, rangeValue); Draw.Line(a, b, fitRadius * 0.015f * rangeValue, LineEndCap.Round, col); } } } // doots~ foreach (Doot doot in doots) { Draw.BlendMode = ShapesBlendMode.Transparent; Draw.Disc(doot.pos, fitRadius * 0.025f, Color.black); Draw.Disc(doot.pos, fitRadius * 0.015f, colMain); Draw.BlendMode = ShapesBlendMode.Additive; Color innerColor = colMain; innerColor.a = 0.25f; Color outerColor = Color.clear; Draw.DiscGradientRadial(doot.pos, fitRadius * 0.18f, innerColor, outerColor); } Draw.BlendMode = ShapesBlendMode.Multiplicative; Draw.DiscGradientRadial(Vector3.zero, fitRadius * 0.5f, Color.black, Color.clear); // restore state Draw.Matrix = prevMtx; Draw.BlendMode = prevBlendMode; Draw.DiscRadiusSpace = prevDiscRadiusSpace; Draw.LineThicknessSpace = prevLineThicknessSpace; Draw.LineGeometry = prevLineGeometry; Draw.RingThicknessSpace = prevRingThicknessSpace; }
static bool HasSpecialBlendMode(this ShapesBlendMode blendMode) => blendMode != ShapesBlendMode.Opaque;
public static string BlendShaderDefine(this ShapesBlendMode blendMode) => blendMode.ToString().ToUpper();
public Material this[ShapesBlendMode type] => materials[(int)type];
static string RenderType(this ShapesBlendMode blendMode) => blendMode == ShapesBlendMode.Opaque ? "TransparentCutout" : "Transparent";
static bool AlphaToMask(this ShapesBlendMode blendMode) => blendMode == ShapesBlendMode.Opaque;
static bool ZWrite(this ShapesBlendMode blendMode) => blendMode == ShapesBlendMode.Opaque;