提交FairyGUI

This commit is contained in:
PC-20230316NUNE\Administrator
2024-10-15 20:37:54 +08:00
parent ef1d3dfb2f
commit 9cd469b811
409 changed files with 66228 additions and 4076 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2d9a58836214e4e58b41c998e14a4239
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,109 @@
#if FAIRYGUI_DRAGONBONES
using UnityEngine;
using DragonBones;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public partial class GLoader3D : GObject
{
UnityArmatureComponent _armatureComponent;
/// <summary>
///
/// </summary>
/// <value></value>
public UnityArmatureComponent armatureComponent
{
get { return _armatureComponent; }
}
/// <summary>
///
/// </summary>
/// <param name="asset"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="anchor"></param>
public void SetDragonBones(DragonBonesData asset, int width, int height, Vector2 anchor)
{
if (_armatureComponent != null)
FreeDragonBones();
_armatureComponent = UnityFactory.factory.BuildArmatureComponent(asset.armatureNames[0], asset.name, null, asset.name);
_armatureComponent.gameObject.transform.localScale = new Vector3(100, 100, 1);
_armatureComponent.gameObject.transform.localPosition = new Vector3(anchor.x, -anchor.y, 0);
SetWrapTarget(_armatureComponent.gameObject, true, width, height);
var ct = _armatureComponent.color;
ct.redMultiplier = _color.r;
ct.greenMultiplier = _color.g;
ct.blueMultiplier = _color.b;
_armatureComponent.color = ct;
OnChangeDragonBones(null);
}
protected void LoadDragonBones()
{
DragonBonesData asset = (DragonBonesData)_contentItem.skeletonAsset;
if (asset == null)
return;
SetDragonBones(asset, _contentItem.width, _contentItem.height, _contentItem.skeletonAnchor);
}
protected void OnChangeDragonBones(string propertyName)
{
if (_armatureComponent == null)
return;
if (propertyName == "color")
{
var ct = _armatureComponent.color;
ct.redMultiplier = _color.r;
ct.greenMultiplier = _color.g;
ct.blueMultiplier = _color.b;
_armatureComponent.color = ct;
return;
}
if (!string.IsNullOrEmpty(_animationName))
{
if (_playing)
_armatureComponent.animation.Play(_animationName, _loop ? 0 : 1);
else
_armatureComponent.animation.GotoAndStopByFrame(_animationName, (uint)_frame);
}
else
_armatureComponent.animation.Reset();
}
protected void FreeDragonBones()
{
if (_armatureComponent != null)
{
_armatureComponent.Dispose();
if (Application.isPlaying)
GameObject.Destroy(_armatureComponent.gameObject);
else
GameObject.DestroyImmediate(_armatureComponent.gameObject);
}
}
protected void OnUpdateDragonBones(UpdateContext context)
{
if (_armatureComponent != null)
{
var ct = _armatureComponent.color;
ct.alphaMultiplier = context.alpha * _content.alpha;
_armatureComponent.color = ct;
}
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9d48ba38d2e2e420085e6c98407dc1fb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d55093b745d3d452699bdfe7bd8ac88e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,160 @@
#if FAIRYGUI_SPINE
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public partial class GLoader3D : GObject
{
SkeletonAnimation _spineAnimation;
/// <summary>
///
/// </summary>
/// <value></value>
public SkeletonAnimation spineAnimation
{
get { return _spineAnimation; }
}
/// <summary>
///
/// </summary>
/// <param name="asset"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="anchor"></param>
public void SetSpine(SkeletonDataAsset asset, int width, int height, Vector2 anchor)
{
SetSpine(asset, width, height, anchor, true);
}
/// <summary>
///
/// </summary>
/// <param name="asset"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="anchor"></param>
/// <param name="cloneMaterial"></param>
public void SetSpine(SkeletonDataAsset asset, int width, int height, Vector2 anchor, bool cloneMaterial)
{
if (_spineAnimation != null)
FreeSpine();
_content.customCloneMaterials = MaterialOverride;
_content.customRecoverMaterials = CleanMaterialOverride;
_spineAnimation = SkeletonRenderer.NewSpineGameObject<SkeletonAnimation>(asset);
_spineAnimation.gameObject.name = asset.name;
Spine.SkeletonData dat = asset.GetSkeletonData(false);
_spineAnimation.gameObject.transform.localScale = new Vector3(1 / asset.scale, 1 / asset.scale, 1);
_spineAnimation.gameObject.transform.localPosition = new Vector3(anchor.x, -anchor.y, 0);
SetWrapTarget(_spineAnimation.gameObject, cloneMaterial, width, height);
_spineAnimation.skeleton.R = _color.r;
_spineAnimation.skeleton.G = _color.g;
_spineAnimation.skeleton.B = _color.b;
OnChangeSpine(null);
}
protected void LoadSpine()
{
SkeletonDataAsset asset = (SkeletonDataAsset)_contentItem.skeletonAsset;
if (asset == null)
return;
SetSpine(asset, _contentItem.width, _contentItem.height, _contentItem.skeletonAnchor);
}
protected void OnChangeSpine(string propertyName)
{
if (_spineAnimation == null)
return;
if (propertyName == "color")
{
_spineAnimation.skeleton.R = _color.r;
_spineAnimation.skeleton.G = _color.g;
_spineAnimation.skeleton.B = _color.b;
return;
}
var skeletonData = _spineAnimation.skeleton.Data;
var state = _spineAnimation.AnimationState;
Spine.Animation animationToUse = !string.IsNullOrEmpty(_animationName) ? skeletonData.FindAnimation(_animationName) : null;
if (animationToUse != null)
{
var trackEntry = state.GetCurrent(0);
if (trackEntry == null || trackEntry.Animation.Name != _animationName || trackEntry.IsComplete && !trackEntry.Loop)
trackEntry = state.SetAnimation(0, animationToUse, _loop);
else
trackEntry.Loop = _loop;
if (_playing)
trackEntry.TimeScale = 1;
else
{
trackEntry.TimeScale = 0;
trackEntry.TrackTime = Mathf.Lerp(0, trackEntry.AnimationEnd - trackEntry.AnimationStart, _frame / 100f);
}
}
else
state.ClearTrack(0);
var skin = !string.IsNullOrEmpty(skinName) ? skeletonData.FindSkin(skinName) : skeletonData.DefaultSkin;
if (skin == null && skeletonData.Skins.Count > 0)
skin = skeletonData.Skins.Items[0];
if (_spineAnimation.skeleton.Skin != skin)
{
_spineAnimation.skeleton.SetSkin(skin);
_spineAnimation.skeleton.SetSlotsToSetupPose();
}
}
protected void FreeSpine()
{
if (_spineAnimation != null)
{
if (Application.isPlaying)
GameObject.Destroy(_spineAnimation.gameObject);
else
GameObject.DestroyImmediate(_spineAnimation.gameObject);
_content.customCloneMaterials = null;
_content.customRecoverMaterials = null;
}
}
protected void OnUpdateSpine(UpdateContext context)
{
if (_spineAnimation != null)
_spineAnimation.skeleton.A = context.alpha * _content.alpha;
}
private void MaterialOverride(Dictionary<Material, Material> materials)
{
if (_spineAnimation != null)
{
foreach (var kv in materials)
{
_spineAnimation.CustomMaterialOverride[kv.Key] = kv.Value;
}
}
}
private void CleanMaterialOverride()
{
if (_spineAnimation != null)
_spineAnimation.CustomMaterialOverride.Clear();
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e3757a6f06ad143439bb00cd7190c6d1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1de100836593c444995397fa368017d5
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e302db5f01fdb46b5b45f44c25164846
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,338 @@
Shader "FairyGUI/TextMeshPro/Distance Field" {
Properties {
_FaceTex ("Face Texture", 2D) = "white" {}
_FaceUVSpeedX ("Face UV Speed X", Range(-5, 5)) = 0.0
_FaceUVSpeedY ("Face UV Speed Y", Range(-5, 5)) = 0.0
_FaceColor ("Face Color", Color) = (1,1,1,1)
_FaceDilate ("Face Dilate", Range(-1,1)) = 0
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_OutlineTex ("Outline Texture", 2D) = "white" {}
_OutlineUVSpeedX ("Outline UV Speed X", Range(-5, 5)) = 0.0
_OutlineUVSpeedY ("Outline UV Speed Y", Range(-5, 5)) = 0.0
_OutlineWidth ("Outline Thickness", Range(0, 1)) = 0
_OutlineSoftness ("Outline Softness", Range(0,1)) = 0
_Bevel ("Bevel", Range(0,1)) = 0.5
_BevelOffset ("Bevel Offset", Range(-0.5,0.5)) = 0
_BevelWidth ("Bevel Width", Range(-.5,0.5)) = 0
_BevelClamp ("Bevel Clamp", Range(0,1)) = 0
_BevelRoundness ("Bevel Roundness", Range(0,1)) = 0
_LightAngle ("Light Angle", Range(0.0, 6.2831853)) = 3.1416
_SpecularColor ("Specular", Color) = (1,1,1,1)
_SpecularPower ("Specular", Range(0,4)) = 2.0
_Reflectivity ("Reflectivity", Range(5.0,15.0)) = 10
_Diffuse ("Diffuse", Range(0,1)) = 0.5
_Ambient ("Ambient", Range(1,0)) = 0.5
_BumpMap ("Normal map", 2D) = "bump" {}
_BumpOutline ("Bump Outline", Range(0,1)) = 0
_BumpFace ("Bump Face", Range(0,1)) = 0
_ReflectFaceColor ("Reflection Color", Color) = (0,0,0,1)
_ReflectOutlineColor("Reflection Color", Color) = (0,0,0,1)
_Cube ("Reflection Cubemap", Cube) = "black" { /* TexGen CubeReflect */ }
_EnvMatrixRotation ("Texture Rotation", vector) = (0, 0, 0, 0)
_UnderlayColor ("Border Color", Color) = (0,0,0, 0.5)
_UnderlayOffsetX ("Border OffsetX", Range(-1,1)) = 0
_UnderlayOffsetY ("Border OffsetY", Range(-1,1)) = 0
_UnderlayDilate ("Border Dilate", Range(-1,1)) = 0
_UnderlaySoftness ("Border Softness", Range(0,1)) = 0
_GlowColor ("Color", Color) = (0, 1, 0, 0.5)
_GlowOffset ("Offset", Range(-1,1)) = 0
_GlowInner ("Inner", Range(0,1)) = 0.05
_GlowOuter ("Outer", Range(0,1)) = 0.05
_GlowPower ("Falloff", Range(1, 0)) = 0.75
_WeightNormal ("Weight Normal", float) = 0
_WeightBold ("Weight Bold", float) = 0.5
_ShaderFlags ("Flags", float) = 0
_ScaleRatioA ("Scale RatioA", float) = 1
_ScaleRatioB ("Scale RatioB", float) = 1
_ScaleRatioC ("Scale RatioC", float) = 1
_MainTex ("Font Atlas", 2D) = "white" {}
_TextureWidth ("Texture Width", float) = 512
_TextureHeight ("Texture Height", float) = 512
_GradientScale ("Gradient Scale", float) = 5.0
_ScaleX ("Scale X", float) = 1.0
_ScaleY ("Scale Y", float) = 1.0
_PerspectiveFilter ("Perspective Correction", Range(0, 1)) = 0.875
_Sharpness ("Sharpness", Range(-1,1)) = 0
_VertexOffsetX ("Vertex OffsetX", float) = 0
_VertexOffsetY ("Vertex OffsetY", float) = 0
_MaskCoord ("Mask Coordinates", vector) = (0, 0, 32767, 32767)
_ClipRect ("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
_MaskSoftnessX ("Mask SoftnessX", float) = 0
_MaskSoftnessY ("Mask SoftnessY", float) = 0
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
}
SubShader {
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull [_CullMode]
ZWrite Off
Lighting Off
Fog { Mode Off }
ZTest [unity_GUIZTestMode]
Blend One OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass {
CGPROGRAM
#pragma target 3.0
#pragma vertex VertShader
#pragma fragment PixShader
#pragma shader_feature __ BEVEL_ON
#pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
#pragma shader_feature __ GLOW_ON
//#pragma multi_compile __ UNITY_UI_CLIP_RECT
//#pragma multi_compile __ UNITY_UI_ALPHACLIP
#pragma multi_compile NOT_CLIPPED CLIPPED
#include "UnityCG.cginc"
#include "UnityUI.cginc"
//#include "Assets/TextMesh Pro/Resources/Shaders/TMPro_Properties.cginc"
//#include "Assets/TextMesh Pro/Resources/Shaders/TMPro.cginc"
#include "Assets/TextMesh Pro/Shaders/TMPro_Properties.cginc"
#include "Assets/TextMesh Pro/Shaders/TMPro.cginc"
struct vertex_t {
UNITY_VERTEX_INPUT_INSTANCE_ID
float4 position : POSITION;
float3 normal : NORMAL;
fixed4 color : COLOR;
float2 texcoord0 : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
struct pixel_t {
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
float4 position : SV_POSITION;
fixed4 color : COLOR;
float2 atlas : TEXCOORD0; // Atlas
float4 param : TEXCOORD1; // alphaClip, scale, bias, weight
float2 mask : TEXCOORD2; // Position in object space(xy), pixel Size(zw)
float3 viewDir : TEXCOORD3;
#if (UNDERLAY_ON || UNDERLAY_INNER)
float4 texcoord2 : TEXCOORD4; // u,v, scale, bias
fixed4 underlayColor : COLOR1;
#endif
float4 textures : TEXCOORD5;
};
// Used by Unity internally to handle Texture Tiling and Offset.
float4 _FaceTex_ST;
float4 _OutlineTex_ST;
CBUFFER_START(UnityPerMaterial)
#ifdef CLIPPED
float4 _ClipBox = float4(-2, -2, 0, 0);
#endif
CBUFFER_END
pixel_t VertShader(vertex_t input)
{
pixel_t output;
UNITY_INITIALIZE_OUTPUT(pixel_t, output);
UNITY_SETUP_INSTANCE_ID(input);
UNITY_TRANSFER_INSTANCE_ID(input,output);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
float bold = step(input.texcoord1.y, 0);
float4 vert = input.position;
vert.x += _VertexOffsetX;
vert.y += _VertexOffsetY;
float4 vPosition = UnityObjectToClipPos(vert);
float2 pixelSize = vPosition.w;
pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
float scale = rsqrt(dot(pixelSize, pixelSize));
scale *= abs(input.texcoord1.y) * _GradientScale * (_Sharpness + 1);
if (UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
float bias =(.5 - weight) + (.5 / scale);
float alphaClip = (1.0 - _OutlineWidth * _ScaleRatioA - _OutlineSoftness * _ScaleRatioA);
#if GLOW_ON
alphaClip = min(alphaClip, 1.0 - _GlowOffset * _ScaleRatioB - _GlowOuter * _ScaleRatioB);
#endif
alphaClip = alphaClip / 2.0 - ( .5 / scale) - weight;
#if (UNDERLAY_ON || UNDERLAY_INNER)
float4 underlayColor = _UnderlayColor;
underlayColor.rgb *= underlayColor.a;
float bScale = scale;
bScale /= 1 + ((_UnderlaySoftness*_ScaleRatioC) * bScale);
float bBias = (0.5 - weight) * bScale - 0.5 - ((_UnderlayDilate * _ScaleRatioC) * 0.5 * bScale);
float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
float2 bOffset = float2(x, y);
#endif
// Generate UV for the Masking Texture
// float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
// float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
// Support for texture tiling and offset
float2 textureUV = UnpackUV(input.texcoord1.x);
float2 faceUV = TRANSFORM_TEX(textureUV, _FaceTex);
float2 outlineUV = TRANSFORM_TEX(textureUV, _OutlineTex);
output.position = vPosition;
#if !defined(UNITY_COLORSPACE_GAMMA) && (UNITY_VERSION >= 550)
output.color.rgb = GammaToLinearSpace(input.color.rgb);
output.color.a = input.color.a;
#else
output.color = input.color;
#endif
output.atlas = input.texcoord0;
output.param = float4(alphaClip, scale, bias, weight);
//output.mask = half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy));
output.viewDir = mul((float3x3)_EnvMatrix, _WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, vert).xyz);
#if (UNDERLAY_ON || UNDERLAY_INNER)
output.texcoord2 = float4(input.texcoord0 + bOffset, bScale, bBias);
output.underlayColor = underlayColor;
#endif
output.textures = float4(faceUV, outlineUV);
#ifdef CLIPPED
output.mask = mul(unity_ObjectToWorld, input.position).xy * _ClipBox.zw + _ClipBox.xy;
#endif
return output;
}
fixed4 PixShader(pixel_t input) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(input);
float c = tex2D(_MainTex, input.atlas).a;
#ifndef UNDERLAY_ON
clip(c - input.param.x);
#endif
float scale = input.param.y;
float bias = input.param.z;
float weight = input.param.w;
float sd = (bias - c) * scale;
float outline = (_OutlineWidth * _ScaleRatioA) * scale;
float softness = (_OutlineSoftness * _ScaleRatioA) * scale;
half4 faceColor = _FaceColor;
half4 outlineColor = _OutlineColor;
faceColor.rgb *= input.color.rgb;
faceColor *= tex2D(_FaceTex, input.textures.xy + float2(_FaceUVSpeedX, _FaceUVSpeedY) * _Time.y);
outlineColor *= tex2D(_OutlineTex, input.textures.zw + float2(_OutlineUVSpeedX, _OutlineUVSpeedY) * _Time.y);
faceColor = GetColor(sd, faceColor, outlineColor, outline, softness);
#if BEVEL_ON
float3 dxy = float3(0.5 / _TextureWidth, 0.5 / _TextureHeight, 0);
float3 n = GetSurfaceNormal(input.atlas, weight, dxy);
float3 bump = UnpackNormal(tex2D(_BumpMap, input.textures.xy + float2(_FaceUVSpeedX, _FaceUVSpeedY) * _Time.y)).xyz;
bump *= lerp(_BumpFace, _BumpOutline, saturate(sd + outline * 0.5));
n = normalize(n- bump);
float3 light = normalize(float3(sin(_LightAngle), cos(_LightAngle), -1.0));
float3 col = GetSpecular(n, light);
faceColor.rgb += col*faceColor.a;
faceColor.rgb *= 1-(dot(n, light)*_Diffuse);
faceColor.rgb *= lerp(_Ambient, 1, n.z*n.z);
fixed4 reflcol = texCUBE(_Cube, reflect(input.viewDir, -n));
faceColor.rgb += reflcol.rgb * lerp(_ReflectFaceColor.rgb, _ReflectOutlineColor.rgb, saturate(sd + outline * 0.5)) * faceColor.a;
#endif
#if UNDERLAY_ON
float d = tex2D(_MainTex, input.texcoord2.xy).a * input.texcoord2.z;
faceColor += input.underlayColor * saturate(d - input.texcoord2.w) * (1 - faceColor.a);
#endif
#if UNDERLAY_INNER
float d = tex2D(_MainTex, input.texcoord2.xy).a * input.texcoord2.z;
faceColor += input.underlayColor * (1 - saturate(d - input.texcoord2.w)) * saturate(1 - sd) * (1 - faceColor.a);
#endif
#if GLOW_ON
float4 glowColor = GetGlowColor(sd, scale);
faceColor.rgb += glowColor.rgb * glowColor.a;
#endif
// Alternative implementation to UnityGet2DClipping with support for softness.
// #if UNITY_UI_CLIP_RECT
// half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
// faceColor *= m.x * m.y;
// #endif
// #if UNITY_UI_ALPHACLIP
// clip(faceColor.a - 0.001);
// #endif
#ifdef CLIPPED
float2 factor = abs(input.mask);
clip(1-max(factor.x, factor.y));
#endif
return faceColor * input.color.a;
}
ENDCG
}
}
Fallback "TextMeshPro/Mobile/Distance Field"
CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
}

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: de10f84640569464799f0e0bfa1ab7b0
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,542 @@
#if FAIRYGUI_TMPRO
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.TextCore;
using TMPro;
namespace FairyGUI
{
/// <summary>
/// TextMeshPro text adapter for FairyGUI. Most of codes were taken from TextMeshPro!
/// Note that not all of TextMeshPro features are supported.
/// </summary>
public class TMPFont : BaseFont
{
protected TMP_FontAsset _fontAsset;
FontStyles _style;
float _scale;
float _padding;
float _stylePadding;
float _ascent;
float _lineHeight;
float _boldMultiplier;
FontWeight _defaultFontWeight;
FontWeight _fontWeight;
TextFormat _format;
TMP_Character _char;
TMP_Character _lineChar;
Material _material;
MaterialManager _manager;
public TMPFont()
{
this.canTint = true;
this.shader = "FairyGUI/TextMeshPro/Distance Field";
this.keepCrisp = true;
_defaultFontWeight = FontWeight.Medium;
}
override public void Dispose()
{
Release();
}
public TMP_FontAsset fontAsset
{
get { return _fontAsset; }
set
{
_fontAsset = value;
Init();
}
}
public FontWeight fontWeight
{
get { return _defaultFontWeight; }
set { _defaultFontWeight = value; }
}
void Release()
{
if (_manager != null)
{
_manager.onCreateNewMaterial -= OnCreateNewMaterial;
_manager = null;
}
if (mainTexture != null)
{
mainTexture.Dispose();
mainTexture = null;
}
if (_material != null)
{
Material.DestroyImmediate(_material);
_material = null;
}
}
void Init()
{
Release();
mainTexture = new NTexture(_fontAsset.atlasTexture);
mainTexture.destroyMethod = DestroyMethod.None;
_manager = mainTexture.GetMaterialManager(this.shader);
_manager.onCreateNewMaterial += OnCreateNewMaterial;
_material = new Material(_fontAsset.material); //copy
_material.SetFloat(ShaderUtilities.ID_TextureWidth, mainTexture.width);
_material.SetFloat(ShaderUtilities.ID_TextureHeight, mainTexture.height);
_material.SetFloat(ShaderUtilities.ID_GradientScale, fontAsset.atlasPadding + 1);
_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
// _ascent = _fontAsset.faceInfo.ascentLine;
// _lineHeight = _fontAsset.faceInfo.lineHeight;
_ascent = _fontAsset.faceInfo.pointSize;
_lineHeight = _fontAsset.faceInfo.pointSize * 1.25f;
_lineChar = GetCharacterFromFontAsset('_', FontStyles.Normal);
}
void OnCreateNewMaterial(Material mat)
{
mat.SetFloat(ShaderUtilities.ID_TextureWidth, mainTexture.width);
mat.SetFloat(ShaderUtilities.ID_TextureHeight, mainTexture.height);
mat.SetFloat(ShaderUtilities.ID_GradientScale, fontAsset.atlasPadding + 1);
mat.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
mat.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
}
override public void UpdateGraphics(NGraphics graphics)
{
MaterialPropertyBlock block = graphics.materialPropertyBlock;
if (_format.outline > 0)
{
graphics.ToggleKeyword("OUTLINE_ON", true);
block.SetFloat(ShaderUtilities.ID_OutlineWidth, _format.outline);
block.SetColor(ShaderUtilities.ID_OutlineColor, _format.outlineColor);
}
else
{
graphics.ToggleKeyword("OUTLINE_ON", false);
block.SetFloat(ShaderUtilities.ID_OutlineWidth, 0);
}
if (_format.shadowOffset.x != 0 || _format.shadowOffset.y != 0)
{
graphics.ToggleKeyword("UNDERLAY_ON", true);
block.SetColor(ShaderUtilities.ID_UnderlayColor, _format.shadowColor);
block.SetFloat(ShaderUtilities.ID_UnderlayOffsetX, _format.shadowOffset.x);
block.SetFloat(ShaderUtilities.ID_UnderlayOffsetY, -_format.shadowOffset.y);
block.SetFloat(ShaderUtilities.ID_UnderlaySoftness, _format.underlaySoftness);
}
else
{
graphics.ToggleKeyword("UNDERLAY_ON", false);
block.SetFloat(ShaderUtilities.ID_UnderlayOffsetX, 0);
block.SetFloat(ShaderUtilities.ID_UnderlayOffsetY, 0);
block.SetFloat(ShaderUtilities.ID_UnderlaySoftness, 0);
}
block.SetFloat(ShaderUtilities.ID_FaceDilate, _format.faceDilate);
block.SetFloat(ShaderUtilities.ID_OutlineSoftness, _format.outlineSoftness);
if (_material.HasProperty(ShaderUtilities.ID_ScaleRatio_A))
{
//ShaderUtilities.GetPadding does not support handle materialproperyblock, we have to use a temp material
_material.SetFloat(ShaderUtilities.ID_OutlineWidth, block.GetFloat(ShaderUtilities.ID_OutlineWidth));
_material.SetFloat(ShaderUtilities.ID_UnderlayOffsetX, block.GetFloat(ShaderUtilities.ID_UnderlayOffsetX));
_material.SetFloat(ShaderUtilities.ID_UnderlayOffsetY, block.GetFloat(ShaderUtilities.ID_UnderlayOffsetY));
_material.SetFloat(ShaderUtilities.ID_UnderlaySoftness, block.GetFloat(ShaderUtilities.ID_UnderlaySoftness));
_material.SetFloat(ShaderUtilities.ID_FaceDilate, block.GetFloat(ShaderUtilities.ID_FaceDilate));
_material.SetFloat(ShaderUtilities.ID_OutlineSoftness, block.GetFloat(ShaderUtilities.ID_OutlineSoftness));
_padding = ShaderUtilities.GetPadding(_material, false, false);
//and then set back the properteis
block.SetFloat(ShaderUtilities.ID_ScaleRatio_A, _material.GetFloat(ShaderUtilities.ID_ScaleRatio_A));
block.SetFloat(ShaderUtilities.ID_ScaleRatio_B, _material.GetFloat(ShaderUtilities.ID_ScaleRatio_B));
block.SetFloat(ShaderUtilities.ID_ScaleRatio_C, _material.GetFloat(ShaderUtilities.ID_ScaleRatio_C));
}
// Set Padding based on selected font style
#region Handle Style Padding
if (((_style & FontStyles.Bold) == FontStyles.Bold)) // Checks for any combination of Bold Style.
{
if (_material.HasProperty(ShaderUtilities.ID_GradientScale))
{
float gradientScale = _material.GetFloat(ShaderUtilities.ID_GradientScale);
_stylePadding = _fontAsset.boldStyle / 4.0f * gradientScale * _material.GetFloat(ShaderUtilities.ID_ScaleRatio_A);
// Clamp overall padding to Gradient Scale size.
if (_stylePadding + _padding > gradientScale)
_padding = gradientScale - _stylePadding;
}
else
_stylePadding = 0;
}
else
{
if (_material.HasProperty(ShaderUtilities.ID_GradientScale))
{
float gradientScale = _material.GetFloat(ShaderUtilities.ID_GradientScale);
_stylePadding = _fontAsset.normalStyle / 4.0f * gradientScale * _material.GetFloat(ShaderUtilities.ID_ScaleRatio_A);
// Clamp overall padding to Gradient Scale size.
if (_stylePadding + _padding > gradientScale)
_padding = gradientScale - _stylePadding;
}
else
_stylePadding = 0;
}
#endregion Handle Style Padding
}
override public void SetFormat(TextFormat format, float fontSizeScale)
{
_format = format;
float size = format.size * fontSizeScale;
if (_format.specialStyle == TextFormat.SpecialStyle.Subscript || _format.specialStyle == TextFormat.SpecialStyle.Superscript)
size *= SupScale;
_scale = size / _fontAsset.faceInfo.pointSize * _fontAsset.faceInfo.scale;
_style = FontStyles.Normal;
if (format.bold)
{
_style |= FontStyles.Bold;
_fontWeight = FontWeight.Bold;
_boldMultiplier = 1 + _fontAsset.boldSpacing * 0.01f;
}
else
{
_fontWeight = _defaultFontWeight;
_boldMultiplier = 1.0f;
}
if (format.italic)
_style |= FontStyles.Italic;
format.FillVertexColors(vertexColors);
}
override public bool GetGlyph(char ch, out float width, out float height, out float baseline)
{
_char = GetCharacterFromFontAsset(ch, _style);
if (_char != null)
{
width = _char.glyph.metrics.horizontalAdvance * _boldMultiplier * _scale;
height = _lineHeight * _scale;
baseline = _ascent * _scale;
if (_format.specialStyle == TextFormat.SpecialStyle.Subscript)
{
height /= SupScale;
baseline /= SupScale;
}
else if (_format.specialStyle == TextFormat.SpecialStyle.Superscript)
{
height = height / SupScale + baseline * SupOffset;
baseline *= (SupOffset + 1 / SupScale);
}
height = Mathf.RoundToInt(height);
baseline = Mathf.RoundToInt(baseline);
return true;
}
else
{
width = 0;
height = 0;
baseline = 0;
return false;
}
}
TMP_Character GetCharacterFromFontAsset(uint unicode, FontStyles fontStyle)
{
bool isAlternativeTypeface;
#pragma warning disable
TMP_FontAsset actualAsset;
#pragma warning restore
return TMP_FontAssetUtilities.GetCharacterFromFontAsset(unicode, _fontAsset, true, fontStyle, _fontWeight,
out isAlternativeTypeface
//,out actualAsset //old TMP version need this line
);
}
static Vector3 bottomLeft;
static Vector3 topLeft;
static Vector3 topRight;
static Vector3 bottomRight;
static Vector4 uvBottomLeft;
static Vector4 uvTopLeft;
static Vector4 uvTopRight;
static Vector4 uvBottomRight;
static Vector4 uv2BottomLeft;
static Vector4 uv2TopLeft;
static Vector4 uv2TopRight;
static Vector4 uv2BottomRight;
static Color32[] vertexColors = new Color32[4];
override public int DrawGlyph(float x, float y,
List<Vector3> vertList, List<Vector2> uvList, List<Vector2> uv2List, List<Color32> colList)
{
GlyphMetrics metrics = _char.glyph.metrics;
GlyphRect rect = _char.glyph.glyphRect;
if (_format.specialStyle == TextFormat.SpecialStyle.Subscript)
y = y - Mathf.RoundToInt(_ascent * _scale * SupOffset);
else if (_format.specialStyle == TextFormat.SpecialStyle.Superscript)
y = y + Mathf.RoundToInt(_ascent * _scale * (1 / SupScale - 1 + SupOffset));
topLeft.x = x + (metrics.horizontalBearingX - _padding - _stylePadding) * _scale;
topLeft.y = y + (metrics.horizontalBearingY + _padding) * _scale;
bottomRight.x = topLeft.x + (metrics.width + _padding * 2 + _stylePadding * 2) * _scale;
bottomRight.y = topLeft.y - (metrics.height + _padding * 2) * _scale;
topRight.x = bottomRight.x;
topRight.y = topLeft.y;
bottomLeft.x = topLeft.x;
bottomLeft.y = bottomRight.y;
#region Handle Italic & Shearing
if (((_style & FontStyles.Italic) == FontStyles.Italic))
{
// Shift Top vertices forward by half (Shear Value * height of character) and Bottom vertices back by same amount.
float shear_value = _fontAsset.italicStyle * 0.01f;
Vector3 topShear = new Vector3(shear_value * ((metrics.horizontalBearingY + _padding + _stylePadding) * _scale), 0, 0);
Vector3 bottomShear = new Vector3(shear_value * (((metrics.horizontalBearingY - metrics.height - _padding - _stylePadding)) * _scale), 0, 0);
topLeft += topShear;
bottomLeft += bottomShear;
topRight += topShear;
bottomRight += bottomShear;
}
#endregion Handle Italics & Shearing
vertList.Add(bottomLeft);
vertList.Add(topLeft);
vertList.Add(topRight);
vertList.Add(bottomRight);
float u = (rect.x - _padding - _stylePadding) / _fontAsset.atlasWidth;
float v = (rect.y - _padding - _stylePadding) / _fontAsset.atlasHeight;
float uw = (rect.width + _padding * 2 + _stylePadding * 2) / _fontAsset.atlasWidth;
float vw = (rect.height + _padding * 2 + _stylePadding * 2) / _fontAsset.atlasHeight;
uvBottomLeft = new Vector2(u, v);
uvTopLeft = new Vector2(u, v + vw);
uvTopRight = new Vector2(u + uw, v + vw);
uvBottomRight = new Vector2(u + uw, v);
float xScale = _scale * 0.01f;
if (_format.bold)
xScale *= -1;
uv2BottomLeft = new Vector2(0, xScale);
uv2TopLeft = new Vector2(511, xScale);
uv2TopRight = new Vector2(2093567, xScale);
uv2BottomRight = new Vector2(2093056, xScale);
uvList.Add(uvBottomLeft);
uvList.Add(uvTopLeft);
uvList.Add(uvTopRight);
uvList.Add(uvBottomRight);
uv2List.Add(uv2BottomLeft);
uv2List.Add(uv2TopLeft);
uv2List.Add(uv2TopRight);
uv2List.Add(uv2BottomRight);
colList.Add(vertexColors[0]);
colList.Add(vertexColors[1]);
colList.Add(vertexColors[2]);
colList.Add(vertexColors[3]);
return 4;
}
override public int DrawLine(float x, float y, float width, int fontSize, int type,
List<Vector3> vertList, List<Vector2> uvList, List<Vector2> uv2List, List<Color32> colList)
{
if (_lineChar == null)
return 0;
float thickness;
float offset;
if (type == 0)
{
thickness = _fontAsset.faceInfo.underlineThickness;
offset = _fontAsset.faceInfo.underlineOffset;
}
else
{
thickness = _fontAsset.faceInfo.strikethroughThickness;
offset = _fontAsset.faceInfo.strikethroughOffset;
}
float scale = (float)fontSize / _fontAsset.faceInfo.pointSize * _fontAsset.faceInfo.scale;
float segmentWidth = _lineChar.glyph.metrics.width / 2 * scale;
if (width < _lineChar.glyph.metrics.width * scale)
segmentWidth = width / 2f;
// UNDERLINE VERTICES FOR (3) LINE SEGMENTS
#region UNDERLINE VERTICES
thickness = thickness * scale;
if (thickness < 1)
thickness = 1;
offset = Mathf.RoundToInt(offset * scale);
// Front Part of the Underline
y += offset;
topLeft.x = x;
topLeft.y = y + _padding * scale;
bottomRight.x = x + segmentWidth;
bottomRight.y = y - thickness - _padding * scale;
topRight.x = bottomRight.x;
topRight.y = topLeft.y;
bottomLeft.x = topLeft.x;
bottomLeft.y = bottomRight.y;
vertList.Add(bottomLeft);
vertList.Add(topLeft);
vertList.Add(topRight);
vertList.Add(bottomRight);
// Middle Part of the Underline
topLeft = topRight;
bottomLeft = bottomRight;
topRight.x = x + width - segmentWidth;
bottomRight.x = topRight.x;
vertList.Add(bottomLeft);
vertList.Add(topLeft);
vertList.Add(topRight);
vertList.Add(bottomRight);
// End Part of the Underline
topLeft = topRight;
bottomLeft = bottomRight;
topRight.x = x + width;
bottomRight.x = topRight.x;
vertList.Add(bottomLeft);
vertList.Add(topLeft);
vertList.Add(topRight);
vertList.Add(bottomRight);
#endregion
// UNDERLINE UV0
#region HANDLE UV0
// Calculate UV required to setup the 3 Quads for the Underline.
Vector2 uv0 = new Vector2((_lineChar.glyph.glyphRect.x - _padding) / _fontAsset.atlasWidth, (_lineChar.glyph.glyphRect.y - _padding) / _fontAsset.atlasHeight); // bottom left
Vector2 uv1 = new Vector2(uv0.x, (_lineChar.glyph.glyphRect.y + _lineChar.glyph.glyphRect.height + _padding) / _fontAsset.atlasHeight); // top left
Vector2 uv2 = new Vector2((_lineChar.glyph.glyphRect.x - _padding + (float)_lineChar.glyph.glyphRect.width / 2) / _fontAsset.atlasWidth, uv1.y); // Mid Top Left
Vector2 uv3 = new Vector2(uv2.x, uv0.y); // Mid Bottom Left
Vector2 uv4 = new Vector2((_lineChar.glyph.glyphRect.x + _padding + (float)_lineChar.glyph.glyphRect.width / 2) / _fontAsset.atlasWidth, uv1.y); // Mid Top Right
Vector2 uv5 = new Vector2(uv4.x, uv0.y); // Mid Bottom right
Vector2 uv6 = new Vector2((_lineChar.glyph.glyphRect.x + _padding + _lineChar.glyph.glyphRect.width) / _fontAsset.atlasWidth, uv1.y); // End Part - Bottom Right
Vector2 uv7 = new Vector2(uv6.x, uv0.y); // End Part - Top Right
uvList.Add(uv0);
uvList.Add(uv1);
uvList.Add(uv2);
uvList.Add(uv3);
// Middle Part of the Underline
uvList.Add(new Vector2(uv2.x - uv2.x * 0.001f, uv0.y));
uvList.Add(new Vector2(uv2.x - uv2.x * 0.001f, uv1.y));
uvList.Add(new Vector2(uv2.x + uv2.x * 0.001f, uv1.y));
uvList.Add(new Vector2(uv2.x + uv2.x * 0.001f, uv0.y));
// Right Part of the Underline
uvList.Add(uv5);
uvList.Add(uv4);
uvList.Add(uv6);
uvList.Add(uv7);
#endregion
// UNDERLINE UV2
#region HANDLE UV2 - SDF SCALE
// UV1 contains Face / Border UV layout.
float segUv1 = segmentWidth / width;
float segUv2 = 1 - segUv1;
//Calculate the xScale or how much the UV's are getting stretched on the X axis for the middle section of the underline.
float xScale = scale * 0.01f;
uv2List.Add(PackUV(0, 0, xScale));
uv2List.Add(PackUV(0, 1, xScale));
uv2List.Add(PackUV(segUv1, 1, xScale));
uv2List.Add(PackUV(segUv1, 0, xScale));
uv2List.Add(PackUV(segUv1, 0, xScale));
uv2List.Add(PackUV(segUv1, 1, xScale));
uv2List.Add(PackUV(segUv2, 1, xScale));
uv2List.Add(PackUV(segUv2, 0, xScale));
uv2List.Add(PackUV(segUv2, 0, xScale));
uv2List.Add(PackUV(segUv2, 1, xScale));
uv2List.Add(PackUV(1, 1, xScale));
uv2List.Add(PackUV(1, 0, xScale));
#endregion
// UNDERLINE VERTEX COLORS
#region
// Alpha is the lower of the vertex color or tag color alpha used.
for (int i = 0; i < 12; i++)
colList.Add(vertexColors[0]);
#endregion
return 12;
}
Vector2 PackUV(float x, float y, float xScale)
{
double x0 = (int)(x * 511);
double y0 = (int)(y * 511);
return new Vector2((float)((x0 * 4096) + y0), xScale);
}
override public bool HasCharacter(char ch)
{
return _fontAsset.HasCharacter(ch);
}
override public int GetLineHeight(int size)
{
return Mathf.RoundToInt(_lineHeight * ((float)size / _fontAsset.faceInfo.pointSize * _fontAsset.faceInfo.scale));
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d7b7ade279883464288671135d22476f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,16 @@
#if FAIRYGUI_TMPRO
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public partial class TextFormat
{
public float faceDilate;
public float outlineSoftness;
public float underlaySoftness;
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6dd33b417fbb245f887d32166c8acc1d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: