Files
2025-05-13 00:35:44 +03:00

119 lines
3.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.TextCore.Text;
public class MusicSheetRenderer : MonoBehaviour
{
// Start is called before the first frame update
public FontAsset TextFont;
public Material TextMaterial;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public GameObject MakeGlyphObject(uint unicodeValue) {
//0x1D11E
var glyphObject = new GameObject("Glyph_" + unicodeValue.ToString("X"));
glyphObject.transform.localPosition = Vector3.zero;
var mr = glyphObject.AddComponent<MeshRenderer>();
mr.sharedMaterial = TextMaterial;
var mf = glyphObject.AddComponent<MeshFilter>();
var mesh = new Mesh();
var character = TextFont.characterLookupTable[unicodeValue];
var w = character.glyph.glyphRect.width;
var h = character.glyph.glyphRect.height;
var maxHeight = TextFont.faceInfo.ascentLine - TextFont.faceInfo.descentLine;
var fw = (float)w / maxHeight;
var fh = (float)h / maxHeight;
var vOffset = (character.glyph.metrics.horizontalBearingY - h) / maxHeight;
mesh.vertices = new Vector3[4]{
new Vector3(0,0 + vOffset,0),
new Vector3(0,fh+ vOffset,0),
new Vector3(fw,fh+ vOffset,0),
new Vector3(fw,0+ vOffset,0),
};
mesh.triangles = new int[6] {
0, 1, 2,
0, 2, 3
};
Vector2 uvXY = new Vector2((float)character.glyph.glyphRect.x / TextFont.atlasWidth, (float)character.glyph.glyphRect.y / TextFont.atlasHeight);
Vector2 uvWH = new Vector2((float)character.glyph.glyphRect.width / TextFont.atlasWidth, (float)character.glyph.glyphRect.height / TextFont.atlasHeight);
Vector2 uvMax = uvXY + uvWH;
//𝄞 1D11E
mesh.uv = new Vector2[] {
new Vector2 (uvXY.x, uvXY.y),
new Vector2 (uvXY.x,uvMax.y),
new Vector2 (uvMax.x,uvMax.y),
new Vector2 (uvMax.x,uvXY.y),
};
mf.sharedMesh = mesh;
return glyphObject;
}
public static uint[] GetUtf32CodePoints(string text) {
byte[] bytes = System.Text.Encoding.UTF32.GetBytes(text);
uint[] result = new uint[bytes.Length / 4];
Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
return result;
}
public void MakeString(string text) {
var codePoints = GetUtf32CodePoints(text);
for(int i = 0; i < codePoints.Length; i++) {
var glyph = MakeGlyphObject(codePoints[i]);
glyph.transform.localPosition += new Vector3(i, 0, 0);
}
}
}
[CustomEditor(typeof(MusicSheetRenderer))]
[CanEditMultipleObjects]
public class LookAtPointEditor : Editor {
SerializedProperty lookAtPoint;
void OnEnable() {
lookAtPoint = serializedObject.FindProperty("lookAtPoint");
//UnityEngine.TextCore.LowLevel.FontEngine.LoadFontFace((target as MusicSheetRenderer).TextFont);
//obj.TextFont.
}
public override void OnInspectorGUI() {
base.OnInspectorGUI();
if(GUILayout.Button("Test font")) {
var obj = (target as MusicSheetRenderer);
obj.MakeString("𝄢𝄚𝄞𝄀𝄆𝅗𝅥");
}
}
}