Skip to content

Commit

Permalink
Version 0.10.0 Alpha.2 [Prerelease]
Browse files Browse the repository at this point in the history
Warning: This commit hash may not be preserved!

Debug state: Some collider pairs may explode on contact due to improper contact generation when using UnitySim. However, issues with compound colliders have been resolved since the previous alpha. Calligraphics single-font functionality is intact, though multi-font pathways have limited testing.
  • Loading branch information
Dreaming381 committed Apr 3, 2024
1 parent 5329927 commit 1a82412
Show file tree
Hide file tree
Showing 48 changed files with 2,201 additions and 978 deletions.
74 changes: 50 additions & 24 deletions Calligraphics/Authoring/BakingSystems/FontSmartBlobberSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,32 @@ protected override void OnUpdate()
}).WithEntityQueryOptions(EntityQueryOptions.IncludePrefab | EntityQueryOptions.IncludeDisabledEntities).WithoutBurst().Run();
}

public static BlobAssetReference<FontBlob> BakeFont(FontAsset font, Material material)
public static unsafe BlobAssetReference<FontBlob> BakeFont(FontAsset font, Material material)
{
float materialPadding = material.GetPaddingForText(false, false);

var builder = new BlobBuilder(Allocator.Temp);
ref FontBlob FontBlobFont = ref builder.ConstructRoot<FontBlob>();
FontBlobFont.scale = font.faceInfo.scale;
FontBlobFont.pointSize = font.faceInfo.pointSize;
FontBlobFont.baseLine = font.faceInfo.baseline;
FontBlobFont.ascentLine = font.faceInfo.ascentLine;
FontBlobFont.descentLine = font.faceInfo.descentLine;
FontBlobFont.lineHeight = font.faceInfo.lineHeight;
FontBlobFont.regularStyleSpacing = font.regularStyleSpacing;
FontBlobFont.regularStyleWeight = font.regularStyleWeight;
FontBlobFont.boldStyleSpacing = font.boldStyleSpacing;
FontBlobFont.boldStyleWeight = font.boldStyleWeight;
FontBlobFont.italicsStyleSlant = font.italicStyleSlant;
FontBlobFont.capLine = font.faceInfo.capLine;
FontBlobFont.atlasWidth = font.atlasWidth;
FontBlobFont.atlasHeight = font.atlasHeight;
FontBlobFont.materialPadding = materialPadding;

var glyphPairAdjustments = font.GetGlyphPairAdjustmentRecordLookup();

BlobBuilderArray<GlyphBlob> glyphBuilder = builder.Allocate(ref FontBlobFont.characters, font.characterTable.Count);
ref FontBlob fontBlobRoot = ref builder.ConstructRoot<FontBlob>();
fontBlobRoot.scale = font.faceInfo.scale;
fontBlobRoot.pointSize = font.faceInfo.pointSize;
fontBlobRoot.baseLine = font.faceInfo.baseline;
fontBlobRoot.ascentLine = font.faceInfo.ascentLine;
fontBlobRoot.descentLine = font.faceInfo.descentLine;
fontBlobRoot.lineHeight = font.faceInfo.lineHeight;
fontBlobRoot.regularStyleSpacing = font.regularStyleSpacing;
fontBlobRoot.regularStyleWeight = font.regularStyleWeight;
fontBlobRoot.boldStyleSpacing = font.boldStyleSpacing;
fontBlobRoot.boldStyleWeight = font.boldStyleWeight;
fontBlobRoot.italicsStyleSlant = font.italicStyleSlant;
fontBlobRoot.capLine = font.faceInfo.capLine;
fontBlobRoot.atlasWidth = font.atlasWidth;
fontBlobRoot.atlasHeight = font.atlasHeight;
fontBlobRoot.materialPadding = materialPadding;

var glyphPairAdjustments = font.GetGlyphPairAdjustmentRecordLookup();
Span<int> hashCounts = stackalloc int[64];
hashCounts.Clear();
BlobBuilderArray<GlyphBlob> glyphBuilder = builder.Allocate(ref fontBlobRoot.characters, font.characterTable.Count);
for (int i = 0; i < font.characterTable.Count; i++)
{
var character = font.characterTable[i];
Expand Down Expand Up @@ -185,7 +186,7 @@ public static BlobAssetReference<FontBlob> BakeFont(FontAsset font, Material mat
}

//Get vertices and uvs
var vertices = GetVertices(character.glyph.metrics, materialPadding, font.atlasPadding / 2f, FontBlobFont.baseScale);
var vertices = GetVertices(character.glyph.metrics, materialPadding, font.atlasPadding / 2f, fontBlobRoot.baseScale);
BlobBuilderArray<float2> verticesBuilder = builder.Allocate(ref glyphBlob.vertices, vertices.Length);
for (int j = 0; j < vertices.Length; j++)
{
Expand All @@ -206,18 +207,43 @@ public static BlobAssetReference<FontBlob> BakeFont(FontAsset font, Material mat
uv2Builder[j] = uv2s[j];
}

glyphBuilder[i] = glyphBlob;
hashCounts[BlobTextMeshGlyphExtensions.GetGlyphHash(glyphBlob.unicode)]++;
}
}

var hashes = builder.Allocate(ref fontBlobRoot.glyphLookupMap, 64);
Span<HashArray> hashArrays = stackalloc HashArray[64];
for (int i = 0; i < hashes.Length; i++)
{
hashArrays[i] = new HashArray
{
hashArray = (GlyphLookup*)builder.Allocate(ref hashes[i], hashCounts[i]).GetUnsafePtr()
};
hashCounts[i] = 0;
}

for (int i = 0; i < glyphBuilder.Length; i++)
{
if (glyphBuilder[i].unicode == 0) // Is this the right way to rule out null glyphs?
continue;
var hash = BlobTextMeshGlyphExtensions.GetGlyphHash(glyphBuilder[i].unicode);
hashArrays[hash].hashArray[hashCounts[hash]] = new GlyphLookup { unicode = glyphBuilder[i].unicode, index = i };
hashCounts[hash]++;
}

var result = builder.CreateBlobAssetReference<FontBlob>(Allocator.Persistent);
builder.Dispose();

FontBlobFont = result.Value;
fontBlobRoot = result.Value;

return result;
}

unsafe struct HashArray
{
public GlyphLookup* hashArray;
}

private static FixedList64Bytes<float2> GetVertices(GlyphMetrics glyphMetrics, float materialPadding, float stylePadding, float currentElementScale)
{
float2 topLeft;
Expand Down
49 changes: 27 additions & 22 deletions Calligraphics/Authoring/TextRendererAuthoring.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using Latios.Authoring;
using Latios.Calligraphics.Rendering;
using Latios.Calligraphics.Rendering.Authoring;
using Latios.Kinemation.Authoring;
using System;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Entities.Graphics;
Expand All @@ -21,14 +22,14 @@ public class TextRendererAuthoring : MonoBehaviour
[TextArea(5, 10)]
public string text;

public float fontSize = 12f;
public bool wordWrap = true;
public float maxLineWidth = float.MaxValue;
public float fontSize = 12f;
public bool wordWrap = true;
public float maxLineWidth = float.MaxValue;
public HorizontalAlignmentOptions horizontalAlignment = HorizontalAlignmentOptions.Left;
public VerticalAlignmentOptions verticalAlignment = VerticalAlignmentOptions.Top;
public bool isOrthographic;
public FontStyles fontStyle;
public FontWeight fontWeight;
public VerticalAlignmentOptions verticalAlignment = VerticalAlignmentOptions.Top;
public bool isOrthographic;
public FontStyles fontStyle;
public FontWeight fontWeight;

public Color32 color = UnityEngine.Color.white;

Expand All @@ -39,7 +40,7 @@ public class TextRendererAuthoring : MonoBehaviour
public struct FontMaterialPair
{
public FontAsset font;
public Material overrideMaterial;
public Material overrideMaterial;

public Material material => overrideMaterial == null ? font.material : overrideMaterial;
}
Expand All @@ -58,11 +59,15 @@ public override void Bake(TextRendererAuthoring authoring)
AddFontRendering(entity, authoring.fontsAndMaterials[0]);
if (authoring.fontsAndMaterials.Count > 1)
{
var additionalEntities = AddBuffer<AdditionalFontMaterialEntity>(entity).Reinterpret<Entity>();
AddComponent<TextMaterialMaskShaderIndex>(entity);
AddBuffer<RenderGlyphMask>(entity);
var additionalEntities = AddBuffer<Rendering.AdditionalFontMaterialEntity>(entity).Reinterpret<Entity>();
for (int i = 1; i < authoring.fontsAndMaterials.Count; i++)
{
var newEntity = CreateAdditionalEntity(TransformUsageFlags.Renderable);
AddFontRendering(newEntity, authoring.fontsAndMaterials[i]);
AddComponent<TextMaterialMaskShaderIndex>(newEntity);
AddBuffer<RenderGlyphMask>(newEntity);
additionalEntities.Add(newEntity);
}
}
Expand All @@ -72,14 +77,14 @@ public override void Bake(TextRendererAuthoring authoring)
calliString.Append(authoring.text);
AddComponent(entity, new TextBaseConfiguration
{
fontSize = authoring.fontSize,
color = authoring.color,
maxLineWidth = math.select(float.MaxValue, authoring.maxLineWidth, authoring.wordWrap),
fontSize = authoring.fontSize,
color = authoring.color,
maxLineWidth = math.select(float.MaxValue, authoring.maxLineWidth, authoring.wordWrap),
lineJustification = authoring.horizontalAlignment,
verticalAlignment = authoring.verticalAlignment,
isOrthographic = authoring.isOrthographic,
fontStyle = authoring.fontStyle,
fontWeight = authoring.fontWeight,
isOrthographic = authoring.isOrthographic,
fontStyle = authoring.fontStyle,
fontWeight = authoring.fontWeight,
});
}

Expand All @@ -92,16 +97,16 @@ void AddFontRendering(Entity entity, FontMaterialPair fontMaterialPair)
var layer = GetLayer();
this.BakeTextBackendMeshAndMaterial(new MeshRendererBakeSettings
{
targetEntity = entity,
targetEntity = entity,
renderMeshDescription = new RenderMeshDescription
{
FilterSettings = new RenderFilterSettings
{
Layer = layer,
Layer = layer,
RenderingLayerMask = (uint)(1 << layer),
ShadowCastingMode = ShadowCastingMode.Off,
ReceiveShadows = false,
MotionMode = MotionVectorGenerationMode.Object,
ShadowCastingMode = ShadowCastingMode.Off,
ReceiveShadows = false,
MotionMode = MotionVectorGenerationMode.Object,
StaticShadowCaster = false,
},
LightProbeUsage = LightProbeUsage.Off,
Expand Down
54 changes: 34 additions & 20 deletions Calligraphics/Components/FontBlob.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine.TextCore.LowLevel;
Expand All @@ -7,12 +8,13 @@ namespace Latios.Calligraphics
//TODO: Underlay, Bold, Smallcaps
public struct FontBlob
{
public BlobArray<GlyphBlob> characters;
public float ascentLine;
public float descentLine;
public float lineHeight;
public float pointSize;
public float scale;
public BlobArray<GlyphBlob> characters;
public BlobArray<BlobArray<GlyphLookup> > glyphLookupMap;
public float ascentLine;
public float descentLine;
public float lineHeight;
public float pointSize;
public float scale;

public float baseLine;
public float atlasWidth;
Expand All @@ -22,7 +24,7 @@ public struct FontBlob
public float regularStyleWeight;
public float boldStyleSpacing;
public float boldStyleWeight;
public byte italicsStyleSlant;
public byte italicsStyleSlant;

public float capLine;

Expand All @@ -40,14 +42,14 @@ public struct FontBlob

public float baseScale => 1f / pointSize * scale * .1f;

public const float smallCapsScaleMultiplier = .8f;
public const float smallCapsScaleMultiplier = .8f;
public const float orthographicScaleMultiplier = 10f;
}

public struct GlyphBlob
{
public uint glyphIndex;
public uint unicode;
public uint glyphIndex;
public uint unicode;
public BlobArray<float2> vertices;
public BlobArray<float2> uv;
public BlobArray<float2> uv2;
Expand Down Expand Up @@ -84,13 +86,13 @@ public struct GlyphBlob
public struct AdjustmentPair
{
public FontFeatureLookupFlags fontFeatureLookupFlags;
public GlyphAdjustment firstAdjustment;
public GlyphAdjustment secondAdjustment;
public GlyphAdjustment firstAdjustment;
public GlyphAdjustment secondAdjustment;
}

public struct GlyphAdjustment
{
public uint glyphUnicode;
public uint glyphUnicode;
public float xPlacement;
public float yPlacement;
public float xAdvance;
Expand All @@ -100,29 +102,41 @@ public struct GlyphAdjustment
{
xPlacement = a.xPlacement + b.xPlacement,
yPlacement = a.yPlacement + b.yPlacement,
xAdvance = a.xAdvance + b.xAdvance,
yAdvance = a.yAdvance + b.yAdvance,
xAdvance = a.xAdvance + b.xAdvance,
yAdvance = a.yAdvance + b.yAdvance,
};
}
}

public struct GlyphLookup
{
public uint unicode;
public int index;
}

public static class BlobTextMeshGlyphExtensions
{
public static bool TryGetGlyph(ref this BlobArray<GlyphBlob> glyphs, uint character, out int index)
public static bool TryGetGlyphIndex(ref this FontBlob font, uint character, out int index)
{
index = -1;
ref var hashArray = ref font.glyphLookupMap[GetGlyphHash(character)];
index = -1;

for (int i = 0; i < glyphs.Length; i++)
for (int i = 0; i < hashArray.Length; i++)
{
if (glyphs[i].unicode == character)
if (hashArray[i].unicode == character)
{
index = i;
index = hashArray[i].index;
return true;
}
}

return false;
}

public static int GetGlyphHash(uint unicode)
{
return (int)(unicode & 0x3f);
}
}
}

Loading

0 comments on commit 1a82412

Please sign in to comment.