public static void Render(IntPtr configPtr, NodeInfo *nodeInfo, UIPassState *statePtr, UIVertexData *vertexDataPtr, UIContextData *context) { IntPtr configSource = configPtr + nodeInfo->configOffset; TextConfig * textConfig = (TextConfig *)UIConfigUtility.GetConfig(nodeInfo->configurationMask, UIConfigLayoutTable.TextConfig, configSource).ToPointer(); BackgroundConfig *backgroundConfig = (BackgroundConfig *)UIConfigUtility.GetConfig(nodeInfo->configurationMask, UIConfigLayoutTable.BackgroundConfig, configSource).ToPointer(); FontConfig * fontConfig = (FontConfig *)UIConfigUtility.GetConfig(nodeInfo->configurationMask, UIConfigLayoutTable.FontConfig, configSource).ToPointer(); BorderConfig * borderConfig = (BorderConfig *)UIConfigUtility.GetConfig(nodeInfo->configurationMask, UIConfigLayoutTable.BorderConfig, configSource).ToPointer(); UIRenderBoxWriters.WriteCharRenderBoxes(statePtr, configPtr + nodeInfo->nodeOffset, backgroundConfig, borderConfig, fontConfig, textConfig, vertexDataPtr, context); }
private static void SizeText(UIPassState *selfPtr, IntPtr configSource, TextConfig *textConfig, FontConfig *fontConfig, UIContextData *context) { var totalWidth = 0f; var height = fontConfig->size.Normalize(*context); var fontSizeScale = (height / textConfig->fontInfo.lineHeight) * textConfig->fontInfo.scale; for (int i = 0; i < textConfig->text.length; i++) { var charInfo = textConfig->GetCharInfo(configSource, i); totalWidth += (charInfo.metrics.horizontalBearingX + charInfo.metrics.horizontalAdvance) * fontSizeScale; } selfPtr->size = new float2(totalWidth + selfPtr->localBox.x + selfPtr->localBox.z, height + selfPtr->localBox.y + selfPtr->localBox.w); }
public static void Layout(int childIndex, IntPtr configPtr, NodeInfo *nodeInfo, IntPtr statePtr, UIContextData *context) { if (childIndex < 0) { UIPassState * selfPtr = (UIPassState *)(statePtr + (UnsafeUtility.SizeOf <UIPassState>() * nodeInfo->index)).ToPointer(); IntPtr configSource = configPtr + nodeInfo->configOffset; TextConfig * textConfig = (TextConfig *)UIConfigUtility.GetConfig(nodeInfo->configurationMask, UIConfigLayoutTable.TextConfig, configSource).ToPointer(); BackgroundConfig *backgroundConfig = (BackgroundConfig *)UIConfigUtility.GetConfig(nodeInfo->configurationMask, UIConfigLayoutTable.BackgroundConfig, configSource).ToPointer(); FontConfig * fontConfig = (FontConfig *)UIConfigUtility.GetConfig(nodeInfo->configurationMask, UIConfigLayoutTable.FontConfig, configSource).ToPointer(); BorderConfig * borderConfig = (BorderConfig *)UIConfigUtility.GetConfig(nodeInfo->configurationMask, UIConfigLayoutTable.BorderConfig, configSource).ToPointer(); SizeText(selfPtr, configPtr + nodeInfo->nodeOffset, textConfig, fontConfig, context); } }
public override unsafe byte PostInit(TextConfig *value, IntPtr config, ulong mask, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { TMP_FontAsset fontAsset = null; var fontConfigPtr = UIConfigUtility.GetConfig(mask, UIConfigLayoutTable.FontConfig, config); if (fontConfigPtr == IntPtr.Zero) { return(0); } FontConfig *fontConfig = ((FontConfig *)fontConfigPtr); var guid = fontConfig->asset.ToHex(); #if UNITY_EDITOR fontAsset = UnityEditor.AssetDatabase.LoadAssetAtPath <TMP_FontAsset>(UnityEditor.AssetDatabase.GUIDToAssetPath(guid)); #else //var task = Addressables.LoadAssetAsync<TMP_FontAsset>(guid); //fontAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(AssetDatabase.GUIDToAssetPath(guid)); #endif var index = context.group.GetAtlasIndex(guid); if (fontAsset != null && index >= 0) { value->fontInfo = new FontInfo(fontAsset); value->charInfoOffset = extraBytesStream.Length; for (int charIndex = 0; charIndex < value->text.length; charIndex++) { UnsafeUtility.CopyPtrToStructure(new IntPtr(((IntPtr)extraBytesStream.Data).ToInt64() + (value->text.offset - extraByteStreamOffset) + (charIndex * 2)).ToPointer(), out char character); var charInfo = fontAsset.characterLookupTable[character]; var charInfoValue = new CharInfo { uvs = new float4(charInfo.glyph.glyphRect.x / (float)fontAsset.atlasWidth, charInfo.glyph.glyphRect.y / (float)fontAsset.atlasHeight, charInfo.glyph.glyphRect.width / (float)fontAsset.atlasWidth, charInfo.glyph.glyphRect.height / (float)fontAsset.atlasHeight), metrics = charInfo.glyph.metrics, index = (byte)index, unicode = charInfo.unicode }; extraBytesStream.WriteBytes(UnsafeUtility.AddressOf(ref charInfoValue), UnsafeUtility.SizeOf <CharInfo>()); } value->charInfoLength = value->text.length; //value->charInfoLength = (int)((extraBytesStream.Length - value->charInfoOffset) / UnsafeUtility.SizeOf<CharInfo>()); value->charInfoOffset += extraByteStreamOffset; } return(0); }
public static void WriteCharRenderBoxes(UIPassState *state, IntPtr config, BackgroundConfig *backgroundConfig, BorderConfig *borderConfig, FontConfig *fontConfig, TextConfig *textConfig, UIVertexData *vertexPtr, UIContextData *context) { var height = fontConfig->size.Normalize(*context); var fontSizeScale = (height / textConfig->fontInfo.lineHeight) * textConfig->fontInfo.scale; float offsetX = state->globalBox.x; float offsetY = state->globalBox.y; var nullBg = new float3(float.NaN, float.NaN, float.NaN); float width = 0f; for (int i = 1; i < textConfig->text.length + 1; i++) { var charInfo = textConfig->GetCharInfo(config, i - 1); UnsafeUtility.WriteArrayElement(vertexPtr, i * 4, new UIVertexData { position = new float3(offsetX + (charInfo.metrics.horizontalBearingX * fontSizeScale), offsetY + (-textConfig->fontInfo.descentLine * fontSizeScale) + ((charInfo.metrics.horizontalBearingY - charInfo.metrics.height) * fontSizeScale), -0.002f), normals = new float3(0, 0, -1), background = nullBg, foreground = new float3(charInfo.uvs.x, charInfo.uvs.y, charInfo.index), color = fontConfig->color }); UnsafeUtility.WriteArrayElement(vertexPtr, (i * 4) + 1, new UIVertexData { position = new float3(offsetX + ((charInfo.metrics.horizontalBearingX + charInfo.metrics.width) * fontSizeScale), offsetY + (-textConfig->fontInfo.descentLine * fontSizeScale) + ((charInfo.metrics.horizontalBearingY - charInfo.metrics.height) * fontSizeScale), -0.002f), normals = new float3(0, 0, -1), background = nullBg, foreground = new float3(charInfo.uvs.x + charInfo.uvs.z, charInfo.uvs.y, charInfo.index), color = fontConfig->color }); UnsafeUtility.WriteArrayElement(vertexPtr, (i * 4) + 2, new UIVertexData { position = new float3(offsetX + (charInfo.metrics.horizontalBearingX * fontSizeScale), offsetY + (-textConfig->fontInfo.descentLine * fontSizeScale) + ((charInfo.metrics.horizontalBearingY - charInfo.metrics.height) * fontSizeScale) + (charInfo.metrics.height * fontSizeScale), -0.002f), normals = new float3(0, 0, -1), background = nullBg, foreground = new float3(charInfo.uvs.x, charInfo.uvs.y + charInfo.uvs.w, charInfo.index), color = fontConfig->color }); UnsafeUtility.WriteArrayElement(vertexPtr, (i * 4) + 3, new UIVertexData { position = new float3(offsetX + ((charInfo.metrics.horizontalBearingX + charInfo.metrics.width) * fontSizeScale), offsetY + (-textConfig->fontInfo.descentLine * fontSizeScale) + ((charInfo.metrics.horizontalBearingY - charInfo.metrics.height) * fontSizeScale) + (charInfo.metrics.height * fontSizeScale), -0.002f), normals = new float3(0, 0, -1), background = nullBg, foreground = new float3(charInfo.uvs.x + charInfo.uvs.z, charInfo.uvs.y + charInfo.uvs.w, charInfo.index), color = fontConfig->color }); offsetX += (charInfo.metrics.horizontalBearingX + charInfo.metrics.horizontalAdvance) * fontSizeScale; width += (charInfo.metrics.horizontalBearingX + charInfo.metrics.horizontalAdvance) * fontSizeScale; } //Write BG Box based on box defined by chars UnsafeUtility.WriteArrayElement <UIVertexData>(vertexPtr, 0, new UIVertexData { background = new float3(backgroundConfig->image.value.x, backgroundConfig->image.value.y - backgroundConfig->image.value.w, 0f), color = backgroundConfig->color, position = new float3(state->globalBox.x, state->globalBox.y, -0.001f), normals = new float3(0, 0, -1), border = new float3(math.atan2(borderConfig->width.top.Normalize(*context), borderConfig->width.left.Normalize(*context)), math.distance(float2.zero, new float2(borderConfig->width.left.Normalize(*context), borderConfig->width.top.Normalize(*context))), borderConfig->radius.topLeft.Normalize(*context)), }); UnsafeUtility.WriteArrayElement <UIVertexData>(vertexPtr, 1, new UIVertexData { background = new float3(backgroundConfig->image.value.x + backgroundConfig->image.value.z, backgroundConfig->image.value.y - backgroundConfig->image.value.w, 0f), color = backgroundConfig->color, position = new float3(state->globalBox.x + width, state->globalBox.y, -0.001f), normals = new float3(0, 0, -1), border = new float3(math.atan2(borderConfig->width.top.Normalize(*context), borderConfig->width.right.Normalize(*context)), math.distance(float2.zero, new float2(borderConfig->width.right.Normalize(*context), borderConfig->width.top.Normalize(*context))), borderConfig->radius.topRight.Normalize(*context)), }); UnsafeUtility.WriteArrayElement <UIVertexData>(vertexPtr, 2, new UIVertexData { background = new float3(backgroundConfig->image.value.x, backgroundConfig->image.value.y, 0f), color = backgroundConfig->color, position = new float3(state->globalBox.x, state->globalBox.y + height, -0.001f), normals = new float3(0, 0, -1), border = new float3(math.atan2(borderConfig->width.bottom.Normalize(*context), borderConfig->width.left.Normalize(*context)), math.distance(float2.zero, new float2(borderConfig->width.left.Normalize(*context), borderConfig->width.bottom.Normalize(*context))), borderConfig->radius.bottomLeft.Normalize(*context)), }); UnsafeUtility.WriteArrayElement <UIVertexData>(vertexPtr, 3, new UIVertexData { background = new float3(backgroundConfig->image.value.x + backgroundConfig->image.value.z, backgroundConfig->image.value.y, 0f), color = backgroundConfig->color, position = new float3(state->globalBox.x + width, state->globalBox.y + height, -0.001f), normals = new float3(0, 0, -1), border = new float3(math.atan2(borderConfig->width.bottom.Normalize(*context), borderConfig->width.right.Normalize(*context)), math.distance(float2.zero, new float2(borderConfig->width.right.Normalize(*context), borderConfig->width.bottom.Normalize(*context))), borderConfig->radius.bottomRight.Normalize(*context)), }); }
public static int RenderBoxCount(IntPtr configPtr, NodeInfo *nodeInfo) { TextConfig *config = (TextConfig *)UIConfigUtility.GetConfig(nodeInfo->configurationMask, UIConfigLayoutTable.TextConfig, configPtr + nodeInfo->configOffset).ToPointer(); return(config->text.length + 1); }
public override unsafe byte PreInit(TextConfig *value, ulong mask, ref UIPropertyWriterContext context) { return(0); }