提交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: ee08275ceb5764e62bce73db80126854
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

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:

View File

@@ -0,0 +1,14 @@
{
"name": "FairyGUI.Runtime",
"rootNamespace": "",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: f4270d81837019d47b93f11421168dae
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

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

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: cc39210f7f4d03f4aa637689b9d90d75
folderAsset: yes
timeCreated: 1446459912
licenseType: Store
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: b5137d7a41873f9499f95f860a6cef17
folderAsset: yes
timeCreated: 1465913233
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,58 @@
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "FairyGUI/BlurFilter" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Pass {
ZTest Always
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
half2 taps[4] : TEXCOORD1;
};
sampler2D _MainTex;
half4 _MainTex_TexelSize;
half4 _BlurOffsets;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord - _BlurOffsets.xy * _MainTex_TexelSize.xy;
o.taps[0] = o.texcoord + _MainTex_TexelSize * _BlurOffsets.xy;
o.taps[1] = o.texcoord - _MainTex_TexelSize * _BlurOffsets.xy;
o.taps[2] = o.texcoord + _MainTex_TexelSize * _BlurOffsets.xy * half2(1,-1);
o.taps[3] = o.texcoord - _MainTex_TexelSize * _BlurOffsets.xy * half2(1,-1);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
half4 color = tex2D(_MainTex, i.taps[0]);
color += tex2D(_MainTex, i.taps[1]);
color += tex2D(_MainTex, i.taps[2]);
color += tex2D(_MainTex, i.taps[3]);
return color * 0.25;
}
ENDCG
}
}
Fallback off
}

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: f83d3bb1d90aaf54d8aed0783317662f
timeCreated: 1465913243
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,154 @@
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "FairyGUI/BMFont"
{
Properties
{
_MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
_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
_BlendSrcFactor ("Blend SrcFactor", Float) = 5
_BlendDstFactor ("Blend DstFactor", Float) = 10
}
SubShader
{
LOD 100
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend [_BlendSrcFactor] [_BlendDstFactor]
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma multi_compile NOT_GRAYED GRAYED
#pragma multi_compile NOT_CLIPPED CLIPPED SOFT_CLIPPED
#pragma vertex vert
#pragma fragment frag
#pragma exclude_renderers d3d9 opengl flash
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float4 texcoord : TEXCOORD0;
#ifdef CLIPPED
float2 clipPos : TEXCOORD1;
#endif
#ifdef SOFT_CLIPPED
float2 clipPos : TEXCOORD1;
#endif
};
sampler2D _MainTex;
CBUFFER_START(UnityPerMaterial)
#ifdef CLIPPED
float4 _ClipBox = float4(-2, -2, 0, 0);
#endif
#ifdef SOFT_CLIPPED
float4 _ClipBox = float4(-2, -2, 0, 0);
float4 _ClipSoftness = float4(0, 0, 0, 0);
#endif
CBUFFER_END
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
#if !defined(UNITY_COLORSPACE_GAMMA) && (UNITY_VERSION >= 550)
o.color.rgb = GammaToLinearSpace(v.color.rgb);
o.color.a = v.color.a;
#else
o.color = v.color;
#endif
#ifdef CLIPPED
o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy;
#endif
#ifdef SOFT_CLIPPED
o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy;
#endif
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = i.color;
fixed4 tcol = tex2D(_MainTex, i.texcoord);
col.a *= tcol[i.texcoord.z];//z stores channel
#ifdef GRAYED
fixed grey = dot(col.rgb, fixed3(0.299, 0.587, 0.114));
col.rgb = fixed3(grey, grey, grey);
#endif
#ifdef SOFT_CLIPPED
float2 factor = float2(0,0);
if(i.clipPos.x<0)
factor.x = (1.0-abs(i.clipPos.x)) * _ClipSoftness.x;
else
factor.x = (1.0-i.clipPos.x) * _ClipSoftness.z;
if(i.clipPos.y<0)
factor.y = (1.0-abs(i.clipPos.y)) * _ClipSoftness.w;
else
factor.y = (1.0-i.clipPos.y) * _ClipSoftness.y;
col.a *= clamp(min(factor.x, factor.y), 0.0, 1.0);
#endif
#ifdef CLIPPED
float2 factor = abs(i.clipPos);
col.a *= step(max(factor.x, factor.y), 1);
#endif
return col;
}
ENDCG
}
}
Fallback "FairyGUI/Text"
}

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: cd79153f88fa7334ea6c5564c053bdca
timeCreated: 1459224288
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,182 @@
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "FairyGUI/Image"
{
Properties
{
_MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
_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
_BlendSrcFactor ("Blend SrcFactor", Float) = 5
_BlendDstFactor ("Blend DstFactor", Float) = 10
}
SubShader
{
LOD 100
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend [_BlendSrcFactor] [_BlendDstFactor], One One
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma multi_compile NOT_COMBINED COMBINED
#pragma multi_compile NOT_GRAYED GRAYED COLOR_FILTER
#pragma multi_compile NOT_CLIPPED CLIPPED SOFT_CLIPPED ALPHA_MASK
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float4 texcoord : TEXCOORD0;
#ifdef CLIPPED
float2 clipPos : TEXCOORD1;
#endif
#ifdef SOFT_CLIPPED
float2 clipPos : TEXCOORD1;
#endif
};
sampler2D _MainTex;
#ifdef COMBINED
sampler2D _AlphaTex;
#endif
CBUFFER_START(UnityPerMaterial)
#ifdef CLIPPED
float4 _ClipBox = float4(-2, -2, 0, 0);
#endif
#ifdef SOFT_CLIPPED
float4 _ClipBox = float4(-2, -2, 0, 0);
float4 _ClipSoftness = float4(0, 0, 0, 0);
#endif
CBUFFER_END
#ifdef COLOR_FILTER
float4x4 _ColorMatrix;
float4 _ColorOffset;
float _ColorOption = 0;
#endif
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
#if !defined(UNITY_COLORSPACE_GAMMA) && (UNITY_VERSION >= 550)
o.color.rgb = GammaToLinearSpace(v.color.rgb);
o.color.a = v.color.a;
#else
o.color = v.color;
#endif
#ifdef CLIPPED
o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy;
#endif
#ifdef SOFT_CLIPPED
o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy;
#endif
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.texcoord.xy / i.texcoord.w) * i.color;
#ifdef COMBINED
col.a *= tex2D(_AlphaTex, i.texcoord.xy / i.texcoord.w).g;
#endif
#ifdef GRAYED
fixed grey = dot(col.rgb, fixed3(0.299, 0.587, 0.114));
col.rgb = fixed3(grey, grey, grey);
#endif
#ifdef SOFT_CLIPPED
float2 factor = float2(0,0);
if(i.clipPos.x<0)
factor.x = (1.0-abs(i.clipPos.x)) * _ClipSoftness.x;
else
factor.x = (1.0-i.clipPos.x) * _ClipSoftness.z;
if(i.clipPos.y<0)
factor.y = (1.0-abs(i.clipPos.y)) * _ClipSoftness.w;
else
factor.y = (1.0-i.clipPos.y) * _ClipSoftness.y;
col.a *= clamp(min(factor.x, factor.y), 0.0, 1.0);
#endif
#ifdef CLIPPED
float2 factor = abs(i.clipPos);
col.a *= step(max(factor.x, factor.y), 1);
#endif
#ifdef COLOR_FILTER
if (_ColorOption == 0)
{
fixed4 col2 = col;
col2.r = dot(col, _ColorMatrix[0]) + _ColorOffset.x;
col2.g = dot(col, _ColorMatrix[1]) + _ColorOffset.y;
col2.b = dot(col, _ColorMatrix[2]) + _ColorOffset.z;
col2.a = dot(col, _ColorMatrix[3]) + _ColorOffset.w;
col = col2;
}
else //premultiply alpha
col.rgb *= col.a;
#endif
#ifdef ALPHA_MASK
clip(col.a - 0.001);
#endif
return col;
}
ENDCG
}
}
}

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 263c97191482b3649ac7bf0810cc4f77
timeCreated: 1459224288
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,150 @@
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "FairyGUI/Text"
{
Properties
{
_MainTex ("Alpha (A)", 2D) = "white" {}
_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
_BlendSrcFactor ("Blend SrcFactor", Float) = 5
_BlendDstFactor ("Blend DstFactor", Float) = 10
}
SubShader
{
LOD 100
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend [_BlendSrcFactor] [_BlendDstFactor]
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma multi_compile NOT_GRAYED GRAYED
#pragma multi_compile NOT_CLIPPED CLIPPED SOFT_CLIPPED
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float4 texcoord : TEXCOORD0;
#ifdef CLIPPED
float2 clipPos : TEXCOORD1;
#endif
#ifdef SOFT_CLIPPED
float2 clipPos : TEXCOORD1;
#endif
};
sampler2D _MainTex;
CBUFFER_START(UnityPerMaterial)
#ifdef CLIPPED
float4 _ClipBox = float4(-2, -2, 0, 0);
#endif
#ifdef SOFT_CLIPPED
float4 _ClipBox = float4(-2, -2, 0, 0);
float4 _ClipSoftness = float4(0, 0, 0, 0);
#endif
CBUFFER_END
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
#if !defined(UNITY_COLORSPACE_GAMMA) && (UNITY_VERSION >= 550)
o.color.rgb = GammaToLinearSpace(v.color.rgb);
o.color.a = v.color.a;
#else
o.color = v.color;
#endif
#ifdef CLIPPED
o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy;
#endif
#ifdef SOFT_CLIPPED
o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy;
#endif
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = i.color;
col.a *= tex2D(_MainTex, i.texcoord).a;
#ifdef GRAYED
fixed grey = dot(col.rgb, fixed3(0.299, 0.587, 0.114));
col.rgb = fixed3(grey, grey, grey);
#endif
#ifdef SOFT_CLIPPED
float2 factor = float2(0,0);
if(i.clipPos.x<0)
factor.x = (1.0-abs(i.clipPos.x)) * _ClipSoftness.x;
else
factor.x = (1.0-i.clipPos.x) * _ClipSoftness.z;
if(i.clipPos.y<0)
factor.y = (1.0-abs(i.clipPos.y)) * _ClipSoftness.w;
else
factor.y = (1.0-i.clipPos.y) * _ClipSoftness.y;
col.a *= clamp(min(factor.x, factor.y), 0.0, 1.0);
#endif
#ifdef CLIPPED
float2 factor = abs(i.clipPos);
col.a *= step(max(factor.x, factor.y), 1);
#endif
return col;
}
ENDCG
}
}
}

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 8526777372c6fef4f8162b3a7901dcb0
timeCreated: 1459224288
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

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

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: eed3a919a48726c46a61e180a615ef7d
folderAsset: yes
timeCreated: 1460480287
licenseType: Store
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,110 @@
using UnityEngine;
using NativeBlendMode = UnityEngine.Rendering.BlendMode;
namespace FairyGUI
{
/*关于BlendMode.Off, 这种模式相当于Blend Off指令的效果。当然在着色器里使用Blend Off指令可以获得更高的效率
但因为Image着色器本身就有多个关键字复制一个这样的着色器代价太大所有为了节省Shader数量便增加了这样一种模式也是可以接受的。
*/
/// <summary>
///
/// </summary>
public enum BlendMode
{
Normal,
None,
Add,
Multiply,
Screen,
Erase,
Mask,
Below,
Off,
One_OneMinusSrcAlpha,
Custom1,
Custom2,
Custom3
}
/// <summary>
///
/// </summary>
public class BlendModeUtils
{
public class BlendFactor
{
public NativeBlendMode srcFactor;
public NativeBlendMode dstFactor;
public bool pma;
public BlendFactor(NativeBlendMode srcFactor, NativeBlendMode dstFactor, bool pma = false)
{
this.srcFactor = srcFactor;
this.dstFactor = dstFactor;
this.pma = pma;
}
}
//Source指的是被计算的颜色Destination是已经在屏幕上的颜色。
//混合结果=Source * factor1 + Destination * factor2
public static BlendFactor[] Factors = new BlendFactor[] {
//Normal
new BlendFactor(NativeBlendMode.SrcAlpha, NativeBlendMode.OneMinusSrcAlpha),
//None
new BlendFactor(NativeBlendMode.One, NativeBlendMode.One),
//Add
new BlendFactor(NativeBlendMode.SrcAlpha, NativeBlendMode.One),
//Multiply
new BlendFactor(NativeBlendMode.DstColor, NativeBlendMode.OneMinusSrcAlpha, true),
//Screen
new BlendFactor(NativeBlendMode.One, NativeBlendMode.OneMinusSrcColor, true),
//Erase
new BlendFactor(NativeBlendMode.Zero, NativeBlendMode.OneMinusSrcAlpha),
//Mask
new BlendFactor(NativeBlendMode.Zero, NativeBlendMode.SrcAlpha),
//Below
new BlendFactor(NativeBlendMode.OneMinusDstAlpha, NativeBlendMode.DstAlpha),
//Off
new BlendFactor(NativeBlendMode.One, NativeBlendMode.Zero),
//One_OneMinusSrcAlpha
new BlendFactor(NativeBlendMode.One, NativeBlendMode.OneMinusSrcAlpha),
//Custom1
new BlendFactor(NativeBlendMode.SrcAlpha, NativeBlendMode.OneMinusSrcAlpha),
//Custom2
new BlendFactor(NativeBlendMode.SrcAlpha, NativeBlendMode.OneMinusSrcAlpha),
//Custom3
new BlendFactor(NativeBlendMode.SrcAlpha, NativeBlendMode.OneMinusSrcAlpha)
};
/// <summary>
///
/// </summary>
/// <param name="mat"></param>
/// <param name="blendMode"></param>
public static void Apply(Material mat, BlendMode blendMode)
{
BlendFactor bf = Factors[(int)blendMode];
mat.SetFloat(ShaderConfig.ID_BlendSrcFactor, (float)bf.srcFactor);
mat.SetFloat(ShaderConfig.ID_BlendDstFactor, (float)bf.dstFactor);
if (bf.pma)
mat.SetFloat(ShaderConfig.ID_ColorOption, 1);
else
mat.SetFloat(ShaderConfig.ID_ColorOption, 0);
}
/// <summary>
///
/// </summary>
/// <param name="blendMode"></param>
/// <param name="srcFactor"></param>
/// <param name="dstFactor"></param>
public static void Override(BlendMode blendMode, NativeBlendMode srcFactor, NativeBlendMode dstFactor)
{
BlendFactor bf = Factors[(int)blendMode];
bf.srcFactor = srcFactor;
bf.dstFactor = dstFactor;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: ce4df113fe8d9994c83e22680544ccdb
timeCreated: 1464679834
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,202 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class CaptureCamera : MonoBehaviour
{
/// <summary>
///
/// </summary>
[System.NonSerialized]
public Transform cachedTransform;
/// <summary>
///
/// </summary>
[System.NonSerialized]
public Camera cachedCamera;
[System.NonSerialized]
static CaptureCamera _main;
[System.NonSerialized]
static int _layer = -1;
static int _hiddenLayer = -1;
public const string Name = "Capture Camera";
public const string LayerName = "VUI";
public const string HiddenLayerName = "Hidden VUI";
void OnEnable()
{
cachedCamera = this.GetComponent<Camera>();
cachedTransform = this.gameObject.transform;
if (this.gameObject.name == Name)
_main = this;
}
/// <summary>
///
/// </summary>
public static void CheckMain()
{
if (_main != null && _main.cachedCamera != null)
return;
GameObject go = GameObject.Find(Name);
if (go != null)
{
_main = go.GetComponent<CaptureCamera>();
return;
}
GameObject cameraObject = new GameObject(Name);
Camera camera = cameraObject.AddComponent<Camera>();
camera.depth = 0;
camera.cullingMask = 1 << layer;
camera.clearFlags = CameraClearFlags.SolidColor;
camera.backgroundColor = Color.clear;
camera.orthographic = true;
camera.orthographicSize = 5;
camera.nearClipPlane = -30;
camera.farClipPlane = 30;
camera.enabled = false;
#if UNITY_5_4_OR_NEWER
camera.stereoTargetEye = StereoTargetEyeMask.None;
#endif
#if UNITY_5_6_OR_NEWER
camera.allowHDR = false;
camera.allowMSAA = false;
#endif
cameraObject.AddComponent<CaptureCamera>();
}
/// <summary>
///
/// </summary>
public static int layer
{
get
{
if (_layer == -1)
{
_layer = LayerMask.NameToLayer(LayerName);
if (_layer == -1)
{
_layer = 30;
Debug.LogWarning("Please define two layers named '" + CaptureCamera.LayerName + "' and '" + CaptureCamera.HiddenLayerName + "'");
}
}
return _layer;
}
}
/// <summary>
///
/// </summary>
public static int hiddenLayer
{
get
{
if (_hiddenLayer == -1)
{
_hiddenLayer = LayerMask.NameToLayer(HiddenLayerName);
if (_hiddenLayer == -1)
{
Debug.LogWarning("Please define two layers named '" + CaptureCamera.LayerName + "' and '" + CaptureCamera.HiddenLayerName + "'");
_hiddenLayer = 31;
}
}
return _hiddenLayer;
}
}
/// <summary>
///
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="stencilSupport"></param>
/// <returns></returns>
public static RenderTexture CreateRenderTexture(int width, int height, bool stencilSupport)
{
RenderTexture texture = new RenderTexture(width, height, stencilSupport ? 24 : 0, RenderTextureFormat.ARGB32);
texture.antiAliasing = 1;
texture.filterMode = FilterMode.Bilinear;
texture.anisoLevel = 0;
texture.useMipMap = false;
texture.wrapMode = TextureWrapMode.Clamp;
texture.hideFlags = DisplayObject.hideFlags;
return texture;
}
/// <summary>
///
/// </summary>
/// <param name="target"></param>
/// <param name="texture"></param>
/// <param name="contentHeight"></param>
/// <param name="offset"></param>
public static void Capture(DisplayObject target, RenderTexture texture, float contentHeight, Vector2 offset)
{
CheckMain();
Matrix4x4 matrix = target.cachedTransform.localToWorldMatrix;
float scaleX = new Vector4(matrix.m00, matrix.m10, matrix.m20, matrix.m30).magnitude;
float scaleY = new Vector4(matrix.m01, matrix.m11, matrix.m21, matrix.m31).magnitude;
Vector3 forward;
forward.x = matrix.m02;
forward.y = matrix.m12;
forward.z = matrix.m22;
Vector3 upwards;
upwards.x = matrix.m01;
upwards.y = matrix.m11;
upwards.z = matrix.m21;
float halfHeight = contentHeight * 0.5f;
Camera camera = _main.cachedCamera;
camera.targetTexture = texture;
float aspect = (float)texture.width / texture.height;
camera.aspect = aspect * scaleX / scaleY;
camera.orthographicSize = halfHeight * scaleY;
_main.cachedTransform.localPosition = target.cachedTransform.TransformPoint(halfHeight * aspect - offset.x, -halfHeight + offset.y, 0);
if (forward != Vector3.zero)
_main.cachedTransform.localRotation = Quaternion.LookRotation(forward, upwards);
int oldLayer = 0;
if (target.graphics != null)
{
oldLayer = target.graphics.gameObject.layer;
target.graphics.gameObject.layer = CaptureCamera.layer;
}
if (target is Container)
{
oldLayer = ((Container)target).numChildren > 0 ? ((Container)target).GetChildAt(0).layer : CaptureCamera.hiddenLayer;
((Container)target).SetChildrenLayer(CaptureCamera.layer);
}
RenderTexture old = RenderTexture.active;
RenderTexture.active = texture;
GL.Clear(true, true, Color.clear);
camera.Render();
RenderTexture.active = old;
if (target.graphics != null)
target.graphics.gameObject.layer = oldLayer;
if (target is Container)
((Container)target).SetChildrenLayer(oldLayer);
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: d207151359c99fb448f4b3380bf41b0f
timeCreated: 1535374215
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 531f2c788ec31e7459700f6811410a6f
timeCreated: 1535374214
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 24d09ba8cf3faa74f8dcd1c86ad38588
timeCreated: 1535374214
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,393 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Pool;
namespace FairyGUI
{
/// <summary>
/// GoWrapper is class for wrapping common gameobject into UI display list.
/// </summary>
public class GoWrapper : DisplayObject
{
[Obsolete("No need to manually set this flag anymore, coz it will be handled automatically.")]
public bool supportStencil;
public event Action<UpdateContext> onUpdate;
public Action<Dictionary<Material, Material>> customCloneMaterials;
public Action customRecoverMaterials;
protected GameObject _wrapTarget;
protected List<RendererInfo> _renderers;
protected Dictionary<Material, Material> _materialsBackup;
protected Canvas _canvas;
protected bool _cloneMaterial;
protected bool _shouldCloneMaterial;
protected struct RendererInfo
{
public Renderer renderer;
public Material[] materials;
public int sortingOrder;
}
// protected static List<Transform> helperTransformList = new List<Transform>();
/// <summary>
///
/// </summary>
public GoWrapper()
{
// _flags |= Flags.SkipBatching;
_renderers = new List<RendererInfo>();
_materialsBackup = new Dictionary<Material, Material>();
CreateGameObject("GoWrapper");
}
/// <summary>
///
/// </summary>
/// <param name="go">包装对象。</param>
public GoWrapper(GameObject go) : this()
{
SetWrapTarget(go, false);
}
/// <summary>
/// 设置包装对象。注意如果原来有包装对象,设置新的包装对象后,原来的包装对象只会被删除引用,但不会被销毁。
/// 对象包含的所有材质不会被复制如果材质已经是公用的这可能影响到其他对象。如果希望自动复制改为使用SetWrapTarget(target, true)设置。
/// </summary>
public GameObject wrapTarget
{
get { return _wrapTarget; }
set { SetWrapTarget(value, false); }
}
[Obsolete("setWrapTarget is deprecated. Use SetWrapTarget instead.")]
public void setWrapTarget(GameObject target, bool cloneMaterial)
{
SetWrapTarget(target, cloneMaterial);
}
/// <summary>
/// 设置包装对象。注意如果原来有包装对象,设置新的包装对象后,原来的包装对象只会被删除引用,但不会被销毁。
/// </summary>
/// <param name="target"></param>
/// <param name="cloneMaterial">如果true则复制材质否则直接使用sharedMaterial。</param>
public void SetWrapTarget(GameObject target, bool cloneMaterial)
{
// set Flags.SkipBatching only target not null
if (target == null) _flags &= ~Flags.SkipBatching;
else _flags |= Flags.SkipBatching;
InvalidateBatchingState();
RecoverMaterials();
_cloneMaterial = cloneMaterial;
if (_wrapTarget != null)
_wrapTarget.transform.SetParent(null, false);
_canvas = null;
_wrapTarget = target;
_shouldCloneMaterial = false;
_renderers.Clear();
if (_wrapTarget != null)
{
_wrapTarget.transform.SetParent(this.cachedTransform, false);
_canvas = _wrapTarget.GetComponent<Canvas>();
if (_canvas != null)
{
_canvas.renderMode = RenderMode.WorldSpace;
_canvas.worldCamera = StageCamera.main;
_canvas.overrideSorting = true;
RectTransform rt = _canvas.GetComponent<RectTransform>();
rt.pivot = new Vector2(0, 1);
rt.position = new Vector3(0, 0, 0);
this.SetSize(rt.rect.width, rt.rect.height);
}
else
{
CacheRenderers();
this.SetSize(0, 0);
}
SetGoLayers(this.layer);
}
}
/// <summary>
/// GoWrapper will cache all renderers of your gameobject on constructor.
/// If your gameobject change laterly, call this function to update the cache.
/// GoWrapper会在构造函数里查询你的gameobject所有的Renderer并保存。如果你的gameobject
/// 后续发生了改变调用这个函数通知GoWrapper重新查询和保存。
/// </summary>
public void CacheRenderers()
{
if (_canvas != null)
return;
RecoverMaterials();
_renderers.Clear();
var items = ListPool<Renderer>.Get();
_wrapTarget.GetComponentsInChildren(true, items);
int cnt = items.Count;
_renderers.Capacity = cnt;
for (int i = 0; i < cnt; i++)
{
Renderer r = items[i];
Material[] mats = r.sharedMaterials;
RendererInfo ri = new RendererInfo()
{
renderer = r,
materials = mats,
sortingOrder = r.sortingOrder
};
_renderers.Add(ri);
if (!_cloneMaterial && mats != null
&& ((r is SkinnedMeshRenderer) || (r is MeshRenderer)))
{
int mcnt = mats.Length;
for (int j = 0; j < mcnt; j++)
{
Material mat = mats[j];
if (mat != null && mat.renderQueue != 3000) //Set the object rendering in Transparent Queue as UI objects
mat.renderQueue = 3000;
}
}
}
ListPool<Renderer>.Release(items);
_renderers.Sort((RendererInfo c1, RendererInfo c2) => { return c1.sortingOrder - c2.sortingOrder; });
_shouldCloneMaterial = _cloneMaterial;
}
void CloneMaterials()
{
_shouldCloneMaterial = false;
int cnt = _renderers.Count;
for (int i = 0; i < cnt; i++)
{
RendererInfo ri = _renderers[i];
Material[] mats = ri.materials;
if (mats == null)
continue;
bool shouldSetRQ = (ri.renderer is SkinnedMeshRenderer) || (ri.renderer is MeshRenderer);
int mcnt = mats.Length;
for (int j = 0; j < mcnt; j++)
{
Material mat = mats[j];
if (mat == null)
continue;
//确保相同的材质不会复制两次
Material newMat;
if (!_materialsBackup.TryGetValue(mat, out newMat))
{
newMat = new Material(mat);
_materialsBackup[mat] = newMat;
}
mats[j] = newMat;
if (shouldSetRQ && mat.renderQueue != 3000) //Set the object rendering in Transparent Queue as UI objects
newMat.renderQueue = 3000;
}
if (customCloneMaterials != null)
customCloneMaterials.Invoke(_materialsBackup);
else if (ri.renderer != null)
ri.renderer.sharedMaterials = mats;
}
}
void RecoverMaterials()
{
if (_materialsBackup.Count == 0)
return;
int cnt = _renderers.Count;
for (int i = 0; i < cnt; i++)
{
RendererInfo ri = _renderers[i];
if (ri.renderer == null)
continue;
Material[] mats = ri.materials;
if (mats == null)
continue;
int mcnt = mats.Length;
for (int j = 0; j < mcnt; j++)
{
Material mat = mats[j];
foreach (KeyValuePair<Material, Material> kv in _materialsBackup)
{
if (kv.Value == mat)
mats[j] = kv.Key;
}
}
if (customRecoverMaterials != null)
customRecoverMaterials.Invoke();
else
ri.renderer.sharedMaterials = mats;
}
foreach (KeyValuePair<Material, Material> kv in _materialsBackup)
Material.DestroyImmediate(kv.Value);
_materialsBackup.Clear();
}
public override int renderingOrder
{
get { return base.renderingOrder; }
set
{
base.renderingOrder = value;
if (_canvas != null)
_canvas.sortingOrder = value;
else
{
int cnt = _renderers.Count;
for (int i = 0; i < cnt; i++)
{
RendererInfo ri = _renderers[i];
if (ri.renderer != null)
{
if (i != 0 && _renderers[i].sortingOrder != _renderers[i - 1].sortingOrder)
value = UpdateContext.current.renderingOrder++;
ri.renderer.sortingOrder = value;
}
}
}
}
}
override protected bool SetLayer(int value, bool fromParent)
{
if (base.SetLayer(value, fromParent))
{
SetGoLayers(value);
return true;
}
else
return false;
}
protected void SetGoLayers(int layer)
{
if (_wrapTarget == null)
return;
var helperTransformList = ListPool<Transform>.Get();
_wrapTarget.GetComponentsInChildren(true, helperTransformList);
int cnt = helperTransformList.Count;
for (int i = 0; i < cnt; i++)
{
helperTransformList[i].gameObject.layer = layer;
}
ListPool<Transform>.Release(helperTransformList);
// helperTransformList.Clear();
}
override public void Update(UpdateContext context)
{
if (onUpdate != null)
onUpdate(context);
if (_shouldCloneMaterial)
CloneMaterials();
ApplyClipping(context);
base.Update(context);
}
private List<Material> helperMaterials = new List<Material>();
virtual protected void ApplyClipping(UpdateContext context)
{
#if UNITY_2018_2_OR_NEWER
int cnt = _renderers.Count;
for (int i = 0; i < cnt; i++)
{
Renderer renderer = _renderers[i].renderer;
if (renderer == null)
continue;
if (customCloneMaterials != null)
helperMaterials.AddRange(_materialsBackup.Values);
else
renderer.GetSharedMaterials(helperMaterials);
int cnt2 = helperMaterials.Count;
for (int j = 0; j < cnt2; j++)
{
Material mat = helperMaterials[j];
if (mat != null)
context.ApplyClippingProperties(mat, false);
}
helperMaterials.Clear();
}
#else
int cnt = _renderers.Count;
for (int i = 0; i < cnt; i++)
{
Material[] mats = _renderers[i].materials;
if (mats == null)
continue;
int cnt2 = mats.Length;
for (int j = 0; j < cnt2; j++)
{
Material mat = mats[j];
if (mat != null)
context.ApplyClippingProperties(mat, false);
}
}
#endif
}
public override void Dispose()
{
if ((_flags & Flags.Disposed) != 0)
return;
if (_wrapTarget != null)
{
UnityEngine.Object.Destroy(_wrapTarget);
_wrapTarget = null;
if (_materialsBackup.Count > 0)
{
//如果有备份,说明材质是复制出来的,应该删除
foreach (KeyValuePair<Material, Material> kv in _materialsBackup)
Material.DestroyImmediate(kv.Value);
}
}
_renderers = null;
_materialsBackup = null;
_canvas = null;
customCloneMaterials = null;
customRecoverMaterials = null;
base.Dispose();
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 796d0bdebe7368c47adfdf04a1abdfc6
timeCreated: 1460480288
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 736ceb6630254bd42b41568b387cbcbe
folderAsset: yes
timeCreated: 1461773297
licenseType: Store
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,33 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class ColliderHitTest : IHitTest
{
/// <summary>
///
/// </summary>
public Collider collider;
/// <summary>
///
/// </summary>
/// <param name="contentRect"></param>
/// <param name="localPoint"></param>
/// <returns></returns>
virtual public bool HitTest(Rect contentRect, Vector2 localPoint)
{
RaycastHit hit;
if (!HitTestContext.GetRaycastHitFromCache(HitTestContext.camera, out hit))
return false;
if (hit.collider != collider)
return false;
return true;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: ffed53edbd969f3439a942ca847cd43d
timeCreated: 1461773299
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,79 @@
using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class HitTestContext
{
//set before hit test
public static Vector3 screenPoint;
public static Vector3 worldPoint;
public static Vector3 direction;
public static bool forTouch;
public static Camera camera;
public static int layerMask = -1;
public static float maxDistance = Mathf.Infinity;
public static Camera cachedMainCamera;
static Dictionary<Camera, RaycastHit?> raycastHits = new Dictionary<Camera, RaycastHit?>();
/// <summary>
///
/// </summary>
/// <param name="camera"></param>
/// <param name="hit"></param>
/// <returns></returns>
public static bool GetRaycastHitFromCache(Camera camera, out RaycastHit hit)
{
RaycastHit? hitRef;
if (!raycastHits.TryGetValue(camera, out hitRef))
{
Ray ray = camera.ScreenPointToRay(screenPoint);
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
{
raycastHits[camera] = hit;
return true;
}
else
{
raycastHits[camera] = null;
return false;
}
}
else if (hitRef == null)
{
hit = new RaycastHit();
return false;
}
else
{
hit = (RaycastHit)hitRef;
return true;
}
}
/// <summary>
///
/// </summary>
/// <param name="camera"></param>
/// <param name="hit"></param>
public static void CacheRaycastHit(Camera camera, ref RaycastHit hit)
{
raycastHits[camera] = hit;
}
/// <summary>
///
/// </summary>
public static void ClearRaycastHitCache()
{
raycastHits.Clear();
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 19869d6307205b84a81aef6031ba1f33
timeCreated: 1461750571
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,27 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public enum HitTestMode
{
Default,
Raycast
}
/// <summary>
///
/// </summary>
public interface IHitTest
{
/// <summary>
///
/// </summary>
/// <param name="contentRect"></param>
/// <param name="localPoint"></param>
/// <returns></returns>
bool HitTest(Rect contentRect, Vector2 localPoint);
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 8bbadf82645501c41957c257ab020708
timeCreated: 1461853133
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,43 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class MeshColliderHitTest : ColliderHitTest
{
public Vector2 lastHit;
/// <summary>
///
/// </summary>
/// <param name="collider"></param>
public MeshColliderHitTest(MeshCollider collider)
{
this.collider = collider;
}
/// <summary>
///
/// </summary>
/// <param name="contentRect"></param>
/// <param name="localPoint"></param>
/// <returns></returns>
override public bool HitTest(Rect contentRect, Vector2 localPoint)
{
RaycastHit hit;
if (!HitTestContext.GetRaycastHitFromCache(HitTestContext.camera, out hit))
return false;
if (hit.collider != collider)
return false;
lastHit = new Vector2(hit.textureCoord.x * contentRect.width, (1 - hit.textureCoord.y) * contentRect.height);
HitTestContext.direction = Vector3.back;
HitTestContext.worldPoint = StageCamera.main.ScreenToWorldPoint(new Vector2(lastHit.x, Screen.height - lastHit.y));
return true;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 6d0a2fa0c7008814795dc7f5f3bd4b19
timeCreated: 1461750571
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,82 @@
using FairyGUI.Utils;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class PixelHitTestData
{
public int pixelWidth;
public float scale;
public byte[] pixels;
public int pixelsLength;
public int pixelsOffset;
public void Load(ByteBuffer ba)
{
ba.ReadInt();
pixelWidth = ba.ReadInt();
scale = 1.0f / ba.ReadByte();
pixels = ba.buffer;
pixelsLength = ba.ReadInt();
pixelsOffset = ba.position;
ba.Skip(pixelsLength);
}
}
/// <summary>
///
/// </summary>
public class PixelHitTest : IHitTest
{
public int offsetX;
public int offsetY;
public float sourceWidth;
public float sourceHeight;
PixelHitTestData _data;
/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <param name="offsetX"></param>
/// <param name="offsetY"></param>
public PixelHitTest(PixelHitTestData data, int offsetX, int offsetY, float sourceWidth, float sourceHeight)
{
_data = data;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.sourceWidth = sourceWidth;
this.sourceHeight = sourceHeight;
}
/// <summary>
///
/// </summary>
/// <param name="contentRect"></param>
/// <param name="localPoint"></param>
/// <returns></returns>
public bool HitTest(Rect contentRect, Vector2 localPoint)
{
if (!contentRect.Contains(localPoint))
return false;
int x = Mathf.FloorToInt((localPoint.x * sourceWidth / contentRect.width - offsetX) * _data.scale);
int y = Mathf.FloorToInt((localPoint.y * sourceHeight / contentRect.height - offsetY) * _data.scale);
if (x < 0 || y < 0 || x >= _data.pixelWidth)
return false;
int pos = y * _data.pixelWidth + x;
int pos2 = pos / 8;
int pos3 = pos % 8;
if (pos2 >= 0 && pos2 < _data.pixelsLength)
return ((_data.pixels[_data.pixelsOffset + pos2] >> pos3) & 0x1) > 0;
else
return false;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 98cb65df7f5853c4b8e3719a28d4d81f
timeCreated: 1535374215
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,26 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class RectHitTest : IHitTest
{
/// <summary>
///
/// </summary>
public Rect rect;
/// <summary>
///
/// </summary>
/// <param name="contentRect"></param>
/// <param name="localPoint"></param>
/// <returns></returns>
public bool HitTest(Rect contentRect, Vector2 localPoint)
{
return rect.Contains(localPoint);
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 92768f03d9b8dea47b1649613c4d0de7
timeCreated: 1474896442
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,44 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class ShapeHitTest : IHitTest
{
/// <summary>
///
/// </summary>
public DisplayObject shape;
public ShapeHitTest(DisplayObject obj)
{
shape = obj;
}
/// <summary>
///
/// </summary>
/// <param name="contentRect"></param>
/// <param name="localPoint"></param>
/// <returns></returns>
public bool HitTest(Rect contentRect, Vector2 localPoint)
{
if (shape.graphics == null)
return false;
if (shape.parent != null)
{
localPoint = shape.parent.TransformPoint(localPoint, shape);
contentRect.size = shape.size;
}
IHitTest ht = shape.graphics.meshFactory as IHitTest;
if (ht == null)
return false;
return ht.HitTest(contentRect, localPoint);
}
}
}

View File

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

View File

@@ -0,0 +1,436 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class Image : DisplayObject, IMeshFactory
{
protected Rect? _scale9Grid;
protected bool _scaleByTile;
protected Vector2 _textureScale;
protected int _tileGridIndice;
protected FillMesh _fillMesh;
public Image() : this(null)
{
}
/// <summary>
///
/// </summary>
/// <param name="texture"></param>
public Image(NTexture texture)
: base()
{
_flags |= Flags.TouchDisabled;
CreateGameObject("Image");
graphics = new NGraphics(gameObject);
graphics.shader = ShaderConfig.imageShader;
graphics.meshFactory = this;
_textureScale = Vector2.one;
if (texture != null)
UpdateTexture(texture);
}
/// <summary>
///
/// </summary>
public NTexture texture
{
get { return graphics.texture; }
set
{
UpdateTexture(value);
}
}
public Vector2 textureScale
{
get { return _textureScale; }
set
{
_textureScale = value;
graphics.SetMeshDirty();
}
}
/// <summary>
///
/// </summary>
public Color color
{
get
{
return graphics.color;
}
set
{
graphics.color = value;
graphics.Tint();
}
}
/// <summary>
///
/// </summary>
public FillMethod fillMethod
{
get { return _fillMesh != null ? _fillMesh.method : FillMethod.None; }
set
{
if (_fillMesh == null)
{
if (value == FillMethod.None)
return;
_fillMesh = new FillMesh();
}
if (_fillMesh.method != value)
{
_fillMesh.method = value;
graphics.SetMeshDirty();
}
}
}
/// <summary>
///
/// </summary>
public int fillOrigin
{
get { return _fillMesh != null ? _fillMesh.origin : 0; }
set
{
if (_fillMesh == null)
_fillMesh = new FillMesh();
if (_fillMesh.origin != value)
{
_fillMesh.origin = value;
graphics.SetMeshDirty();
}
}
}
/// <summary>
///
/// </summary>
public bool fillClockwise
{
get { return _fillMesh != null ? _fillMesh.clockwise : true; }
set
{
if (_fillMesh == null)
_fillMesh = new FillMesh();
if (_fillMesh.clockwise != value)
{
_fillMesh.clockwise = value;
graphics.SetMeshDirty();
}
}
}
/// <summary>
///
/// </summary>
public float fillAmount
{
get { return _fillMesh != null ? _fillMesh.amount : 0; }
set
{
if (_fillMesh == null)
_fillMesh = new FillMesh();
if (_fillMesh.amount != value)
{
_fillMesh.amount = value;
graphics.SetMeshDirty();
}
}
}
/// <summary>
///
/// </summary>
public Rect? scale9Grid
{
get { return _scale9Grid; }
set
{
if (_scale9Grid != value)
{
_scale9Grid = value;
graphics.SetMeshDirty();
}
}
}
/// <summary>
///
/// </summary>
public bool scaleByTile
{
get { return _scaleByTile; }
set
{
if (_scaleByTile != value)
{
_scaleByTile = value;
graphics.SetMeshDirty();
}
}
}
/// <summary>
///
/// </summary>
public int tileGridIndice
{
get { return _tileGridIndice; }
set
{
if (_tileGridIndice != value)
{
_tileGridIndice = value;
graphics.SetMeshDirty();
}
}
}
/// <summary>
///
/// </summary>
public void SetNativeSize()
{
if (graphics.texture != null)
SetSize(graphics.texture.width, graphics.texture.height);
else
SetSize(0, 0);
}
virtual protected void UpdateTexture(NTexture value)
{
if (value == graphics.texture)
return;
graphics.texture = value;
_textureScale = Vector2.one;
if (_contentRect.width == 0)
SetNativeSize();
InvalidateBatchingState();
}
public void OnPopulateMesh(VertexBuffer vb)
{
if (_fillMesh != null && _fillMesh.method != FillMethod.None)
{
_fillMesh.OnPopulateMesh(vb);
}
else if (_scaleByTile)
{
NTexture texture = graphics.texture;
if (texture.root == texture
&& texture.nativeTexture != null
&& texture.nativeTexture.wrapMode == TextureWrapMode.Repeat)
{
Rect uvRect = vb.uvRect;
uvRect.width *= vb.contentRect.width / texture.width * _textureScale.x;
uvRect.height *= vb.contentRect.height / texture.height * _textureScale.y;
vb.AddQuad(vb.contentRect, vb.vertexColor, uvRect);
vb.AddTriangles();
}
else
{
Rect contentRect = vb.contentRect;
contentRect.width *= _textureScale.x;
contentRect.height *= _textureScale.y;
TileFill(vb, contentRect, vb.uvRect, texture.width, texture.height);
vb.AddTriangles();
}
}
else if (_scale9Grid != null)
{
SliceFill(vb);
}
else
graphics.OnPopulateMesh(vb);
}
static int[] TRIANGLES_9_GRID = new int[] {
4,0,1,1,5,4,
5,1,2,2,6,5,
6,2,3,3,7,6,
8,4,5,5,9,8,
9,5,6,6,10,9,
10,6,7,7,11,10,
12,8,9,9,13,12,
13,9,10,10,14,13,
14,10,11,
11,15,14
};
static int[] gridTileIndice = new int[] { -1, 0, -1, 2, 4, 3, -1, 1, -1 };
static float[] gridX = new float[4];
static float[] gridY = new float[4];
static float[] gridTexX = new float[4];
static float[] gridTexY = new float[4];
public void SliceFill(VertexBuffer vb)
{
NTexture texture = graphics.texture;
Rect gridRect = (Rect)_scale9Grid;
Rect contentRect = vb.contentRect;
contentRect.width *= _textureScale.x;
contentRect.height *= _textureScale.y;
Rect uvRect = vb.uvRect;
float sourceW = texture.width;
float sourceH = texture.height;
if (graphics.flip != FlipType.None)
{
if (graphics.flip == FlipType.Horizontal || graphics.flip == FlipType.Both)
{
gridRect.x = sourceW - gridRect.xMax;
gridRect.xMax = gridRect.x + gridRect.width;
}
if (graphics.flip == FlipType.Vertical || graphics.flip == FlipType.Both)
{
gridRect.y = sourceH - gridRect.yMax;
gridRect.yMax = gridRect.y + gridRect.height;
}
}
float sx = uvRect.width / sourceW;
float sy = uvRect.height / sourceH;
float xMax = uvRect.xMax;
float yMax = uvRect.yMax;
float xMax2 = gridRect.xMax;
float yMax2 = gridRect.yMax;
gridTexX[0] = uvRect.x;
gridTexX[1] = uvRect.x + gridRect.x * sx;
gridTexX[2] = uvRect.x + xMax2 * sx;
gridTexX[3] = xMax;
gridTexY[0] = yMax;
gridTexY[1] = yMax - gridRect.y * sy;
gridTexY[2] = yMax - yMax2 * sy;
gridTexY[3] = uvRect.y;
if (contentRect.width >= (sourceW - gridRect.width))
{
gridX[1] = gridRect.x;
gridX[2] = contentRect.width - (sourceW - xMax2);
gridX[3] = contentRect.width;
}
else
{
float tmp = gridRect.x / (sourceW - xMax2);
tmp = contentRect.width * tmp / (1 + tmp);
gridX[1] = tmp;
gridX[2] = tmp;
gridX[3] = contentRect.width;
}
if (contentRect.height >= (sourceH - gridRect.height))
{
gridY[1] = gridRect.y;
gridY[2] = contentRect.height - (sourceH - yMax2);
gridY[3] = contentRect.height;
}
else
{
float tmp = gridRect.y / (sourceH - yMax2);
tmp = contentRect.height * tmp / (1 + tmp);
gridY[1] = tmp;
gridY[2] = tmp;
gridY[3] = contentRect.height;
}
if (_tileGridIndice == 0)
{
for (int cy = 0; cy < 4; cy++)
{
for (int cx = 0; cx < 4; cx++)
vb.AddVert(new Vector2(gridX[cx] / _textureScale.x, gridY[cy] / _textureScale.y), vb.vertexColor, new Vector2(gridTexX[cx], gridTexY[cy]));
}
vb.AddTriangles(TRIANGLES_9_GRID);
}
else
{
Rect drawRect;
Rect texRect;
int row, col;
int part;
for (int pi = 0; pi < 9; pi++)
{
col = pi % 3;
row = pi / 3;
part = gridTileIndice[pi];
drawRect = Rect.MinMaxRect(gridX[col], gridY[row], gridX[col + 1], gridY[row + 1]);
texRect = Rect.MinMaxRect(gridTexX[col], gridTexY[row + 1], gridTexX[col + 1], gridTexY[row]);
if (part != -1 && (_tileGridIndice & (1 << part)) != 0)
{
TileFill(vb, drawRect, texRect,
(part == 0 || part == 1 || part == 4) ? gridRect.width : drawRect.width,
(part == 2 || part == 3 || part == 4) ? gridRect.height : drawRect.height);
}
else
{
drawRect.x /= _textureScale.x;
drawRect.y /= _textureScale.y;
drawRect.width /= _textureScale.x;
drawRect.height /= _textureScale.y;
vb.AddQuad(drawRect, vb.vertexColor, texRect);
}
}
vb.AddTriangles();
}
}
void TileFill(VertexBuffer vb, Rect contentRect, Rect uvRect, float sourceW, float sourceH)
{
int hc = Mathf.CeilToInt(contentRect.width / sourceW);
int vc = Mathf.CeilToInt(contentRect.height / sourceH);
float tailWidth = contentRect.width - (hc - 1) * sourceW;
float tailHeight = contentRect.height - (vc - 1) * sourceH;
float xMax = uvRect.xMax;
float yMax = uvRect.yMax;
for (int i = 0; i < hc; i++)
{
for (int j = 0; j < vc; j++)
{
Rect uvTmp = uvRect;
if (i == hc - 1)
uvTmp.xMax = Mathf.Lerp(uvRect.x, xMax, tailWidth / sourceW);
if (j == vc - 1)
uvTmp.yMin = Mathf.Lerp(uvRect.y, yMax, 1 - tailHeight / sourceH);
Rect drawRect = new Rect(contentRect.x + i * sourceW, contentRect.y + j * sourceH,
i == (hc - 1) ? tailWidth : sourceW, j == (vc - 1) ? tailHeight : sourceH);
drawRect.x /= _textureScale.x;
drawRect.y /= _textureScale.y;
drawRect.width /= _textureScale.x;
drawRect.height /= _textureScale.y;
vb.AddQuad(drawRect, vb.vertexColor, uvTmp);
}
}
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 948ec8822e3c52942bc6e7f6265b264c
timeCreated: 1535374214
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,243 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
namespace FairyGUI
{
[Flags]
public enum MaterialFlags
{
Clipped = 1,
SoftClipped = 2,
StencilTest = 4,
AlphaMask = 8,
Grayed = 16,
ColorFilter = 32
}
/// <summary>
/// Every texture-shader combination has a MaterialManager.
/// </summary>
public class MaterialManager
{
public event Action<Material> onCreateNewMaterial;
public bool firstMaterialInFrame;
NTexture _texture;
Shader _shader;
List<string> _addKeywords;
Dictionary<int, List<MaterialRef>> _materials;
bool _combineTexture;
class MaterialRef
{
public Material material;
public int frame;
public BlendMode blendMode;
public uint group;
}
const int internalKeywordsCount = 6;
static string[] internalKeywords = new[] { "CLIPPED", "SOFT_CLIPPED", null, "ALPHA_MASK", "GRAYED", "COLOR_FILTER" };
/// <summary>
///
/// </summary>
/// <param name="texture"></param>
/// <param name="shader"></param>
internal MaterialManager(NTexture texture, Shader shader)
{
_texture = texture;
_shader = shader;
_materials = new Dictionary<int, List<MaterialRef>>();
_combineTexture = texture.alphaTexture != null;
}
/// <summary>
///
/// </summary>
/// <param name="keywords"></param>
/// <returns></returns>
public int GetFlagsByKeywords(IList<string> keywords)
{
if (_addKeywords == null)
_addKeywords = new List<string>();
int flags = 0;
for (int i = 0; i < keywords.Count; i++)
{
string s = keywords[i];
if (string.IsNullOrEmpty(s))
continue;
int j = _addKeywords.IndexOf(s);
if (j == -1)
{
j = _addKeywords.Count;
_addKeywords.Add(s);
}
flags += (1 << (j + internalKeywordsCount));
}
return flags;
}
/// <summary>
///
/// </summary>
/// <param name="flags"></param>
/// <param name="blendMode"></param>
/// <param name="group"></param>
/// <returns></returns>
public Material GetMaterial(int flags, BlendMode blendMode, uint group)
{
if (blendMode != BlendMode.Normal && BlendModeUtils.Factors[(int)blendMode].pma)
flags |= (int)MaterialFlags.ColorFilter;
List<MaterialRef> items;
if (!_materials.TryGetValue(flags, out items))
{
items = new List<MaterialRef>();
_materials[flags] = items;
}
int frameId = Time.frameCount;
int cnt = items.Count;
MaterialRef result = null;
for (int i = 0; i < cnt; i++)
{
MaterialRef item = items[i];
if (item.group == group && item.blendMode == blendMode)
{
if (item.frame != frameId)
{
firstMaterialInFrame = true;
item.frame = frameId;
}
else
firstMaterialInFrame = false;
if (_combineTexture)
item.material.SetTexture(ShaderConfig.ID_AlphaTex, _texture.alphaTexture);
return item.material;
}
else if (result == null && (item.frame > frameId || item.frame < frameId - 1)) //collect materials if it is unused in last frame
result = item;
}
if (result == null)
{
result = new MaterialRef() { material = CreateMaterial(flags) };
items.Add(result);
}
else if (_combineTexture)
result.material.SetTexture(ShaderConfig.ID_AlphaTex, _texture.alphaTexture);
if (result.blendMode != blendMode)
{
BlendModeUtils.Apply(result.material, blendMode);
result.blendMode = blendMode;
}
result.group = group;
result.frame = frameId;
firstMaterialInFrame = true;
return result.material;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
Material CreateMaterial(int flags)
{
Material mat = new Material(_shader);
mat.mainTexture = _texture.nativeTexture;
if (_texture.alphaTexture != null)
{
mat.EnableKeyword("COMBINED");
mat.SetTexture(ShaderConfig.ID_AlphaTex, _texture.alphaTexture);
}
for (int i = 0; i < internalKeywordsCount; i++)
{
if ((flags & (1 << i)) != 0)
{
string s = internalKeywords[i];
if (s != null)
mat.EnableKeyword(s);
}
}
if (_addKeywords != null)
{
int keywordCnt = _addKeywords.Count;
for (int i = 0; i < keywordCnt; i++)
{
if ((flags & (1 << (i + internalKeywordsCount))) != 0)
mat.EnableKeyword(_addKeywords[i]);
}
}
mat.hideFlags = DisplayObject.hideFlags;
if (onCreateNewMaterial != null)
onCreateNewMaterial(mat);
return mat;
}
/// <summary>
///
/// </summary>
public void DestroyMaterials()
{
var iter = _materials.GetEnumerator();
while (iter.MoveNext())
{
List<MaterialRef> items = iter.Current.Value;
if (Application.isPlaying)
{
int cnt = items.Count;
for (int j = 0; j < cnt; j++)
Object.Destroy(items[j].material);
}
else
{
int cnt = items.Count;
for (int j = 0; j < cnt; j++)
Object.DestroyImmediate(items[j].material);
}
items.Clear();
}
iter.Dispose();
}
/// <summary>
///
/// </summary>
public void RefreshMaterials()
{
_combineTexture = _texture.alphaTexture != null;
var iter = _materials.GetEnumerator();
while (iter.MoveNext())
{
List<MaterialRef> items = iter.Current.Value;
int cnt = items.Count;
for (int j = 0; j < cnt; j++)
{
Material mat = items[j].material;
mat.mainTexture = _texture.nativeTexture;
if (_combineTexture)
{
mat.EnableKeyword("COMBINED");
mat.SetTexture(ShaderConfig.ID_AlphaTex, _texture.alphaTexture);
}
}
}
iter.Dispose();
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 7223c4628e56c1e448c70e10168f5fa1
timeCreated: 1535374214
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: fca659474d209d94fbded309f407995c
folderAsset: yes
timeCreated: 1545987172
licenseType: Store
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,75 @@
using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class CompositeMesh : IMeshFactory, IHitTest
{
/// <summary>
///
/// </summary>
public readonly List<IMeshFactory> elements;
/// <summary>
/// If it is -1, means all elements are active, otherwise, only the specific element is active
/// </summary>
public int activeIndex;
public CompositeMesh()
{
elements = new List<IMeshFactory>();
activeIndex = -1;
}
public void OnPopulateMesh(VertexBuffer vb)
{
int cnt = elements.Count;
if (cnt == 1)
elements[0].OnPopulateMesh(vb);
else
{
VertexBuffer vb2 = VertexBuffer.Begin(vb);
for (int i = 0; i < cnt; i++)
{
if (activeIndex == -1 || i == activeIndex)
{
vb2.Clear();
elements[i].OnPopulateMesh(vb2);
vb.Append(vb2);
}
}
vb2.End();
}
}
public bool HitTest(Rect contentRect, Vector2 point)
{
if (!contentRect.Contains(point))
return false;
bool flag = false;
int cnt = elements.Count;
for (int i = 0; i < cnt; i++)
{
if (activeIndex == -1 || i == activeIndex)
{
IHitTest ht = elements[i] as IHitTest;
if (ht != null)
{
if (ht.HitTest(contentRect, point))
return true;
}
else
flag = true;
}
}
return flag;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: ba3d921c1f6e95f4e9105f45fd67bda5
timeCreated: 1546159121
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,200 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class EllipseMesh : IMeshFactory, IHitTest
{
/// <summary>
///
/// </summary>
public Rect? drawRect;
/// <summary>
///
/// </summary>
public float lineWidth;
/// <summary>
///
/// </summary>
public Color32 lineColor;
/// <summary>
///
/// </summary>
public Color32? centerColor;
/// <summary>
///
/// </summary>
public Color32? fillColor;
/// <summary>
///
/// </summary>
public float startDegree;
/// <summary>
///
/// </summary>
public float endDegreee;
static int[] SECTOR_CENTER_TRIANGLES = new int[] {
0, 4, 1,
0, 3, 4,
0, 2, 3,
0, 8, 5,
0, 7, 8,
0, 6, 7,
6, 5, 2,
2, 1, 6
};
public EllipseMesh()
{
lineColor = Color.black;
startDegree = 0;
endDegreee = 360;
}
public void OnPopulateMesh(VertexBuffer vb)
{
Rect rect = drawRect != null ? (Rect)drawRect : vb.contentRect;
Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
float sectionStart = Mathf.Clamp(startDegree, 0, 360);
float sectionEnd = Mathf.Clamp(endDegreee, 0, 360);
bool clipped = sectionStart > 0 || sectionEnd < 360;
sectionStart = sectionStart * Mathf.Deg2Rad;
sectionEnd = sectionEnd * Mathf.Deg2Rad;
Color32 centerColor2 = centerColor == null ? color : (Color32)centerColor;
float radiusX = rect.width / 2;
float radiusY = rect.height / 2;
int sides = Mathf.CeilToInt(Mathf.PI * (radiusX + radiusY) / 4);
sides = Mathf.Clamp(sides, 40, 800);
float angleDelta = 2 * Mathf.PI / sides;
float angle = 0;
float lineAngle = 0;
if (lineWidth > 0 && clipped)
{
lineAngle = lineWidth / Mathf.Max(radiusX, radiusY);
sectionStart += lineAngle;
sectionEnd -= lineAngle;
}
int vpos = vb.currentVertCount;
float centerX = rect.x + radiusX;
float centerY = rect.y + radiusY;
vb.AddVert(new Vector3(centerX, centerY, 0), centerColor2);
for (int i = 0; i < sides; i++)
{
if (angle < sectionStart)
angle = sectionStart;
else if (angle > sectionEnd)
angle = sectionEnd;
Vector3 vec = new Vector3(Mathf.Cos(angle) * (radiusX - lineWidth) + centerX, Mathf.Sin(angle) * (radiusY - lineWidth) + centerY, 0);
vb.AddVert(vec, color);
if (lineWidth > 0)
{
vb.AddVert(vec, lineColor);
vb.AddVert(new Vector3(Mathf.Cos(angle) * radiusX + centerX, Mathf.Sin(angle) * radiusY + centerY, 0), lineColor);
}
angle += angleDelta;
}
if (lineWidth > 0)
{
int cnt = sides * 3;
for (int i = 0; i < cnt; i += 3)
{
if (i != cnt - 3)
{
vb.AddTriangle(0, i + 1, i + 4);
vb.AddTriangle(i + 5, i + 2, i + 3);
vb.AddTriangle(i + 3, i + 6, i + 5);
}
else if (!clipped)
{
vb.AddTriangle(0, i + 1, 1);
vb.AddTriangle(2, i + 2, i + 3);
vb.AddTriangle(i + 3, 3, 2);
}
else
{
vb.AddTriangle(0, i + 1, i + 1);
vb.AddTriangle(i + 2, i + 2, i + 3);
vb.AddTriangle(i + 3, i + 3, i + 2);
}
}
}
else
{
for (int i = 0; i < sides; i++)
{
if (i != sides - 1)
vb.AddTriangle(0, i + 1, i + 2);
else if (!clipped)
vb.AddTriangle(0, i + 1, 1);
else
vb.AddTriangle(0, i + 1, i + 1);
}
}
if (lineWidth > 0 && clipped)
{
//扇形内边缘的线条
vb.AddVert(new Vector3(radiusX, radiusY, 0), lineColor);
float centerRadius = lineWidth * 0.5f;
sectionStart -= lineAngle;
angle = sectionStart + lineAngle * 0.5f + Mathf.PI * 0.5f;
vb.AddVert(new Vector3(Mathf.Cos(angle) * centerRadius + radiusX, Mathf.Sin(angle) * centerRadius + radiusY, 0), lineColor);
angle -= Mathf.PI;
vb.AddVert(new Vector3(Mathf.Cos(angle) * centerRadius + radiusX, Mathf.Sin(angle) * centerRadius + radiusY, 0), lineColor);
vb.AddVert(new Vector3(Mathf.Cos(sectionStart) * radiusX + radiusX, Mathf.Sin(sectionStart) * radiusY + radiusY, 0), lineColor);
vb.AddVert(vb.GetPosition(vpos + 3), lineColor);
sectionEnd += lineAngle;
angle = sectionEnd - lineAngle * 0.5f + Mathf.PI * 0.5f;
vb.AddVert(new Vector3(Mathf.Cos(angle) * centerRadius + radiusX, Mathf.Sin(angle) * centerRadius + radiusY, 0), lineColor);
angle -= Mathf.PI;
vb.AddVert(new Vector3(Mathf.Cos(angle) * centerRadius + radiusX, Mathf.Sin(angle) * centerRadius + radiusY, 0), lineColor);
vb.AddVert(vb.GetPosition(vpos + sides * 3), lineColor);
vb.AddVert(new Vector3(Mathf.Cos(sectionEnd) * radiusX + radiusX, Mathf.Sin(sectionEnd) * radiusY + radiusY, 0), lineColor);
vb.AddTriangles(SECTOR_CENTER_TRIANGLES, sides * 3 + 1);
}
}
public bool HitTest(Rect contentRect, Vector2 point)
{
if (!contentRect.Contains(point))
return false;
float radiusX = contentRect.width * 0.5f;
float raduisY = contentRect.height * 0.5f;
float xx = point.x - radiusX - contentRect.x;
float yy = point.y - raduisY - contentRect.y;
if (Mathf.Pow(xx / radiusX, 2) + Mathf.Pow(yy / raduisY, 2) < 1)
{
if (startDegree != 0 || endDegreee != 360)
{
float deg = Mathf.Atan2(yy, xx) * Mathf.Rad2Deg;
if (deg < 0)
deg += 360;
return deg >= startDegree && deg <= endDegreee;
}
else
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 7ea96854995120948847301fb48d1675
timeCreated: 1545987173
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,400 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class FillMesh : IMeshFactory
{
/// <summary>
///
/// </summary>
public FillMethod method;
/// <summary>
///
/// </summary>
public int origin;
/// <summary>
///
/// </summary>
public float amount;
/// <summary>
///
/// </summary>
public bool clockwise;
public FillMesh()
{
clockwise = true;
amount = 1;
}
public void OnPopulateMesh(VertexBuffer vb)
{
float amount = Mathf.Clamp01(this.amount);
switch (method)
{
case FillMethod.Horizontal:
FillHorizontal(vb, vb.contentRect, origin, amount);
break;
case FillMethod.Vertical:
FillVertical(vb, vb.contentRect, origin, amount);
break;
case FillMethod.Radial90:
FillRadial90(vb, vb.contentRect, (Origin90)origin, amount, clockwise);
break;
case FillMethod.Radial180:
FillRadial180(vb, vb.contentRect, (Origin180)origin, amount, clockwise);
break;
case FillMethod.Radial360:
FillRadial360(vb, vb.contentRect, (Origin360)origin, amount, clockwise);
break;
}
}
static void FillHorizontal(VertexBuffer vb, Rect vertRect, int origin, float amount)
{
float a = vertRect.width * amount;
if ((OriginHorizontal)origin == OriginHorizontal.Right || (OriginVertical)origin == OriginVertical.Bottom)
vertRect.x += (vertRect.width - a);
vertRect.width = a;
vb.AddQuad(vertRect);
vb.AddTriangles();
}
static void FillVertical(VertexBuffer vb, Rect vertRect, int origin, float amount)
{
float a = vertRect.height * amount;
if ((OriginHorizontal)origin == OriginHorizontal.Right || (OriginVertical)origin == OriginVertical.Bottom)
vertRect.y += (vertRect.height - a);
vertRect.height = a;
vb.AddQuad(vertRect);
vb.AddTriangles();
}
//4 vertex
static void FillRadial90(VertexBuffer vb, Rect vertRect, Origin90 origin, float amount, bool clockwise)
{
bool flipX = origin == Origin90.TopRight || origin == Origin90.BottomRight;
bool flipY = origin == Origin90.BottomLeft || origin == Origin90.BottomRight;
if (flipX != flipY)
clockwise = !clockwise;
float ratio = clockwise ? amount : (1 - amount);
float tan = Mathf.Tan(Mathf.PI * 0.5f * ratio);
bool thresold = false;
if (ratio != 1)
thresold = (vertRect.height / vertRect.width - tan) > 0;
if (!clockwise)
thresold = !thresold;
float x = vertRect.x + (ratio == 0 ? float.MaxValue : (vertRect.height / tan));
float y = vertRect.y + (ratio == 1 ? float.MaxValue : (vertRect.width * tan));
float x2 = x;
float y2 = y;
if (flipX)
x2 = vertRect.width - x;
if (flipY)
y2 = vertRect.height - y;
float xMin = flipX ? (vertRect.width - vertRect.x) : vertRect.xMin;
float yMin = flipY ? (vertRect.height - vertRect.y) : vertRect.yMin;
float xMax = flipX ? -vertRect.xMin : vertRect.xMax;
float yMax = flipY ? -vertRect.yMin : vertRect.yMax;
vb.AddVert(new Vector3(xMin, yMin, 0));
if (clockwise)
vb.AddVert(new Vector3(xMax, yMin, 0));
if (y > vertRect.yMax)
{
if (thresold)
vb.AddVert(new Vector3(x2, yMax, 0));
else
vb.AddVert(new Vector3(xMax, yMax, 0));
}
else
vb.AddVert(new Vector3(xMax, y2, 0));
if (x > vertRect.xMax)
{
if (thresold)
vb.AddVert(new Vector3(xMax, y2, 0));
else
vb.AddVert(new Vector3(xMax, yMax, 0));
}
else
vb.AddVert(new Vector3(x2, yMax, 0));
if (!clockwise)
vb.AddVert(new Vector3(xMin, yMax, 0));
if (flipX == flipY)
{
vb.AddTriangle(0, 1, 2);
vb.AddTriangle(0, 2, 3);
}
else
{
vb.AddTriangle(2, 1, 0);
vb.AddTriangle(3, 2, 0);
}
}
//8 vertex
static void FillRadial180(VertexBuffer vb, Rect vertRect, Origin180 origin, float amount, bool clockwise)
{
switch (origin)
{
case Origin180.Top:
if (amount <= 0.5f)
{
vertRect.width /= 2;
if (clockwise)
vertRect.x += vertRect.width;
FillRadial90(vb, vertRect, clockwise ? Origin90.TopLeft : Origin90.TopRight, amount / 0.5f, clockwise);
Vector3 vec = vb.GetPosition(-4);
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
vb.AddTriangles(-4);
}
else
{
vertRect.width /= 2;
if (!clockwise)
vertRect.x += vertRect.width;
FillRadial90(vb, vertRect, clockwise ? Origin90.TopRight : Origin90.TopLeft, (amount - 0.5f) / 0.5f, clockwise);
if (clockwise)
vertRect.x += vertRect.width;
else
vertRect.x -= vertRect.width;
vb.AddQuad(vertRect);
vb.AddTriangles(-4);
}
break;
case Origin180.Bottom:
if (amount <= 0.5f)
{
vertRect.width /= 2;
if (!clockwise)
vertRect.x += vertRect.width;
FillRadial90(vb, vertRect, clockwise ? Origin90.BottomRight : Origin90.BottomLeft, amount / 0.5f, clockwise);
Vector3 vec = vb.GetPosition(-4);
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
vb.AddTriangles(-4);
}
else
{
vertRect.width /= 2;
if (clockwise)
vertRect.x += vertRect.width;
FillRadial90(vb, vertRect, clockwise ? Origin90.BottomLeft : Origin90.BottomRight, (amount - 0.5f) / 0.5f, clockwise);
if (clockwise)
vertRect.x -= vertRect.width;
else
vertRect.x += vertRect.width;
vb.AddQuad(vertRect);
vb.AddTriangles(-4);
}
break;
case Origin180.Left:
if (amount <= 0.5f)
{
vertRect.height /= 2;
if (!clockwise)
vertRect.y += vertRect.height;
FillRadial90(vb, vertRect, clockwise ? Origin90.BottomLeft : Origin90.TopLeft, amount / 0.5f, clockwise);
Vector3 vec = vb.GetPosition(-4);
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
vb.AddTriangles(-4);
}
else
{
vertRect.height /= 2;
if (clockwise)
vertRect.y += vertRect.height;
FillRadial90(vb, vertRect, clockwise ? Origin90.TopLeft : Origin90.BottomLeft, (amount - 0.5f) / 0.5f, clockwise);
if (clockwise)
vertRect.y -= vertRect.height;
else
vertRect.y += vertRect.height;
vb.AddQuad(vertRect);
vb.AddTriangles(-4);
}
break;
case Origin180.Right:
if (amount <= 0.5f)
{
vertRect.height /= 2;
if (clockwise)
vertRect.y += vertRect.height;
FillRadial90(vb, vertRect, clockwise ? Origin90.TopRight : Origin90.BottomRight, amount / 0.5f, clockwise);
Vector3 vec = vb.GetPosition(-4);
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
vb.AddTriangles(-4);
}
else
{
vertRect.height /= 2;
if (!clockwise)
vertRect.y += vertRect.height;
FillRadial90(vb, vertRect, clockwise ? Origin90.BottomRight : Origin90.TopRight, (amount - 0.5f) / 0.5f, clockwise);
if (clockwise)
vertRect.y += vertRect.height;
else
vertRect.y -= vertRect.height;
vb.AddQuad(vertRect);
vb.AddTriangles(-4);
}
break;
}
}
//12 vertex
static void FillRadial360(VertexBuffer vb, Rect vertRect, Origin360 origin, float amount, bool clockwise)
{
switch (origin)
{
case Origin360.Top:
if (amount < 0.5f)
{
vertRect.width /= 2;
if (clockwise)
vertRect.x += vertRect.width;
FillRadial180(vb, vertRect, clockwise ? Origin180.Left : Origin180.Right, amount / 0.5f, clockwise);
Vector3 vec = vb.GetPosition(-8);
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
vb.AddTriangles(-4);
}
else
{
vertRect.width /= 2;
if (!clockwise)
vertRect.x += vertRect.width;
FillRadial180(vb, vertRect, clockwise ? Origin180.Right : Origin180.Left, (amount - 0.5f) / 0.5f, clockwise);
if (clockwise)
vertRect.x += vertRect.width;
else
vertRect.x -= vertRect.width;
vb.AddQuad(vertRect);
vb.AddTriangles(-4);
}
break;
case Origin360.Bottom:
if (amount < 0.5f)
{
vertRect.width /= 2;
if (!clockwise)
vertRect.x += vertRect.width;
FillRadial180(vb, vertRect, clockwise ? Origin180.Right : Origin180.Left, amount / 0.5f, clockwise);
Vector3 vec = vb.GetPosition(-8);
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
vb.AddTriangles(-4);
}
else
{
vertRect.width /= 2;
if (clockwise)
vertRect.x += vertRect.width;
FillRadial180(vb, vertRect, clockwise ? Origin180.Left : Origin180.Right, (amount - 0.5f) / 0.5f, clockwise);
if (clockwise)
vertRect.x -= vertRect.width;
else
vertRect.x += vertRect.width;
vb.AddQuad(vertRect);
vb.AddTriangles(-4);
}
break;
case Origin360.Left:
if (amount < 0.5f)
{
vertRect.height /= 2;
if (!clockwise)
vertRect.y += vertRect.height;
FillRadial180(vb, vertRect, clockwise ? Origin180.Bottom : Origin180.Top, amount / 0.5f, clockwise);
Vector3 vec = vb.GetPosition(-8);
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
vb.AddTriangles(-4);
}
else
{
vertRect.height /= 2;
if (clockwise)
vertRect.y += vertRect.height;
FillRadial180(vb, vertRect, clockwise ? Origin180.Top : Origin180.Bottom, (amount - 0.5f) / 0.5f, clockwise);
if (clockwise)
vertRect.y -= vertRect.height;
else
vertRect.y += vertRect.height;
vb.AddQuad(vertRect);
vb.AddTriangles(-4);
}
break;
case Origin360.Right:
if (amount < 0.5f)
{
vertRect.height /= 2;
if (clockwise)
vertRect.y += vertRect.height;
FillRadial180(vb, vertRect, clockwise ? Origin180.Top : Origin180.Bottom, amount / 0.5f, clockwise);
Vector3 vec = vb.GetPosition(-8);
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
vb.AddTriangles(-4);
}
else
{
vertRect.height /= 2;
if (!clockwise)
vertRect.y += vertRect.height;
FillRadial180(vb, vertRect, clockwise ? Origin180.Bottom : Origin180.Top, (amount - 0.5f) / 0.5f, clockwise);
if (clockwise)
vertRect.y += vertRect.height;
else
vertRect.y -= vertRect.height;
vb.AddQuad(vertRect);
vb.AddTriangles(-4);
}
break;
}
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 8b37d56dc1840734a8fdb4971f5bc733
timeCreated: 1545987173
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,179 @@
using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// Inspired by kim ki won (http://mypi.ruliweb.daum.net/mypi.htm?id=newtypekorea)
/// </summary>
public class LineMesh : IMeshFactory
{
/// <summary>
///
/// </summary>
public GPath path;
/// <summary>
///
/// </summary>
public float lineWidth;
/// <summary>
///
/// </summary>
public AnimationCurve lineWidthCurve;
/// <summary>
///
/// </summary>
public Gradient gradient;
/// <summary>
///
/// </summary>
public bool roundEdge;
/// <summary>
///
/// </summary>
public float fillStart;
/// <summary>
///
/// </summary>
public float fillEnd;
/// <summary>
///
/// </summary>
public float pointDensity;
/// <summary>
///
/// </summary>
public bool repeatFill;
static List<Vector3> points = new List<Vector3>();
static List<float> ts = new List<float>();
public LineMesh()
{
path = new GPath();
lineWidth = 2;
fillStart = 0;
fillEnd = 1;
pointDensity = 0.1f;
}
public void OnPopulateMesh(VertexBuffer vb)
{
Vector2 uvMin = vb.uvRect.position;
Vector2 uvMax = new Vector2(vb.uvRect.xMax, vb.uvRect.yMax);
float uvRatio = path.length / vb.textureSize.x;
int segCount = path.segmentCount;
float t = 0;
float lw = lineWidth;
float u;
for (int si = 0; si < segCount; si++)
{
float ratio = path.GetSegmentLength(si) / path.length;
float t0 = Mathf.Clamp(fillStart - t, 0, ratio) / ratio;
float t1 = Mathf.Clamp(fillEnd - t, 0, ratio) / ratio;
if (t0 >= t1)
{
t += ratio;
continue;
}
points.Clear();
ts.Clear();
path.GetPointsInSegment(si, t0, t1, points, ts, pointDensity);
int cnt = points.Count;
Color c0 = vb.vertexColor;
Color c1 = vb.vertexColor;
if (gradient != null)
c0 = gradient.Evaluate(t);
if (lineWidthCurve != null)
lw = lineWidthCurve.Evaluate(t);
if (roundEdge && si == 0 && t0 == 0)
DrawRoundEdge(vb, points[0], points[1], lw, c0, uvMin);
int vertCount = vb.currentVertCount;
for (int i = 1; i < cnt; i++)
{
Vector3 p0 = points[i - 1];
Vector3 p1 = points[i];
int k = vertCount + (i - 1) * 2;
float tc = t + ratio * ts[i];
Vector3 lineVector = p1 - p0;
Vector3 widthVector = Vector3.Cross(lineVector, new Vector3(0, 0, 1));
widthVector.Normalize();
if (i == 1)
{
if (repeatFill)
u = tc * uvRatio * uvMax.x;
else
u = Mathf.Lerp(uvMin.x, uvMax.x, t + ratio * ts[i - 1]);
vb.AddVert(p0 - widthVector * lw * 0.5f, c0, new Vector2(u, uvMax.y));
vb.AddVert(p0 + widthVector * lw * 0.5f, c0, new Vector2(u, uvMin.y));
if (si != 0) //joint
{
vb.AddTriangle(k - 2, k - 1, k + 1);
vb.AddTriangle(k - 2, k + 1, k);
}
}
if (gradient != null)
c1 = gradient.Evaluate(tc);
if (lineWidthCurve != null)
lw = lineWidthCurve.Evaluate(tc);
if (repeatFill)
u = tc * uvRatio * uvMax.x;
else
u = Mathf.Lerp(uvMin.x, uvMax.x, tc);
vb.AddVert(p1 - widthVector * lw * 0.5f, c1, new Vector2(u, uvMax.y));
vb.AddVert(p1 + widthVector * lw * 0.5f, c1, new Vector2(u, uvMin.y));
vb.AddTriangle(k, k + 1, k + 3);
vb.AddTriangle(k, k + 3, k + 2);
}
if (roundEdge && si == segCount - 1 && t1 == 1)
DrawRoundEdge(vb, points[cnt - 1], points[cnt - 2], lw, c1, uvMax);
t += ratio;
}
}
void DrawRoundEdge(VertexBuffer vb, Vector2 p0, Vector2 p1, float lw, Color32 color, Vector2 uv)
{
Vector2 widthVector = Vector3.Cross(p0 - p1, new Vector3(0, 0, 1));
widthVector.Normalize();
widthVector = widthVector * lw / 2f;
Vector2 lineVector = (p0 - p1).normalized * lw / 2f;
int sides = Mathf.CeilToInt(Mathf.PI * lw / 2);
if (sides < 6)
sides = 6;
int current = vb.currentVertCount;
float angleUnit = Mathf.PI / (sides - 1);
vb.AddVert(p0, color, uv);
vb.AddVert(p0 + widthVector, color, uv);
for (int n = 0; n < sides; n++)
{
vb.AddVert(p0 + Mathf.Cos(angleUnit * n) * widthVector + Mathf.Sin(angleUnit * n) * lineVector, color, uv);
vb.AddTriangle(current, current + 1 + n, current + 2 + n);
}
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 6d1935a924fabf74a92c72b22a042279
timeCreated: 1546519483
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,15 @@

namespace FairyGUI
{
/// <summary>
///
/// </summary>
public interface IMeshFactory
{
/// <summary>
///
/// </summary>
/// <param name="vb"></param>
void OnPopulateMesh(VertexBuffer vb);
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 274275eaa3fbbc94697cbe90a0fb52f1
timeCreated: 1545987172
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,51 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class PlaneMesh : IMeshFactory
{
public int gridSize = 30;
public void OnPopulateMesh(VertexBuffer vb)
{
float w = vb.contentRect.width;
float h = vb.contentRect.height;
float xMax = vb.contentRect.xMax;
float yMax = vb.contentRect.yMax;
int hc = (int)Mathf.Min(Mathf.CeilToInt(w / gridSize), 9);
int vc = (int)Mathf.Min(Mathf.CeilToInt(h / gridSize), 9);
int eachPartX = Mathf.FloorToInt(w / hc);
int eachPartY = Mathf.FloorToInt(h / vc);
float x, y;
for (int i = 0; i <= vc; i++)
{
if (i == vc)
y = yMax;
else
y = vb.contentRect.y + i * eachPartY;
for (int j = 0; j <= hc; j++)
{
if (j == hc)
x = xMax;
else
x = vb.contentRect.x + j * eachPartX;
vb.AddVert(new Vector3(x, y, 0));
}
}
for (int i = 0; i < vc; i++)
{
int k = i * (hc + 1);
for (int j = 1; j <= hc; j++)
{
int m = k + j;
vb.AddTriangle(m - 1, m, m + hc);
vb.AddTriangle(m, m + hc + 1, m + hc);
}
}
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 121b756a7a8240c49b63fa9de560691a
timeCreated: 1547970144
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,291 @@
using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class PolygonMesh : IMeshFactory, IHitTest
{
/// <summary>
/// points must be in clockwise order, and must start from bottom-left if stretchUV is set.
/// </summary>
public readonly List<Vector2> points;
/// <summary>
/// if you dont want to provide uv, leave it empty.
/// </summary>
public readonly List<Vector2> texcoords;
/// <summary>
///
/// </summary>
public float lineWidth;
/// <summary>
///
/// </summary>
public Color32 lineColor;
/// <summary>
///
/// </summary>
public Color32? fillColor;
/// <summary>
///
/// </summary>
public Color32[] colors;
/// <summary>
///
/// </summary>
public bool usePercentPositions;
static List<int> sRestIndices = new List<int>();
public PolygonMesh()
{
points = new List<Vector2>();
texcoords = new List<Vector2>();
}
/// <summary>
///
/// </summary>
/// <param name="point"></param>
public void Add(Vector2 point)
{
points.Add(point);
}
/// <summary>
///
/// </summary>
/// <param name="point"></param>
/// <param name="texcoord"></param>
public void Add(Vector2 point, Vector2 texcoord)
{
points.Add(point);
texcoords.Add(texcoord);
}
public void OnPopulateMesh(VertexBuffer vb)
{
int numVertices = points.Count;
if (numVertices < 3)
return;
int restIndexPos, numRestIndices;
Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
float w = vb.contentRect.width;
float h = vb.contentRect.height;
bool useTexcoords = texcoords.Count >= numVertices;
bool fullUV = true;
for (int i = 0; i < numVertices; i++)
{
Vector3 vec = new Vector3(points[i].x, points[i].y, 0);
if (usePercentPositions)
{
vec.x *= w;
vec.y *= h;
}
if (useTexcoords)
{
Vector2 uv = texcoords[i];
if (uv.x != 0 && uv.x != 1 || uv.y != 0 && uv.y != 1)
fullUV = false;
uv.x = Mathf.Lerp(vb.uvRect.x, vb.uvRect.xMax, uv.x);
uv.y = Mathf.Lerp(vb.uvRect.y, vb.uvRect.yMax, uv.y);
vb.AddVert(vec, color, uv);
}
else
vb.AddVert(vec, color);
}
if (useTexcoords && fullUV && numVertices == 4)
vb._isArbitraryQuad = true;
// Algorithm "Ear clipping method" described here:
// -> https://en.wikipedia.org/wiki/Polygon_triangulation
//
// Implementation inspired by:
// -> http://polyk.ivank.net
// -> Starling
sRestIndices.Clear();
for (int i = 0; i < numVertices; ++i)
sRestIndices.Add(i);
restIndexPos = 0;
numRestIndices = numVertices;
Vector2 a, b, c, p;
int otherIndex;
bool earFound;
int i0, i1, i2;
while (numRestIndices > 3)
{
earFound = false;
i0 = sRestIndices[restIndexPos % numRestIndices];
i1 = sRestIndices[(restIndexPos + 1) % numRestIndices];
i2 = sRestIndices[(restIndexPos + 2) % numRestIndices];
a = points[i0];
b = points[i1];
c = points[i2];
if ((a.y - b.y) * (c.x - b.x) + (b.x - a.x) * (c.y - b.y) >= 0)
{
earFound = true;
for (int i = 3; i < numRestIndices; ++i)
{
otherIndex = sRestIndices[(restIndexPos + i) % numRestIndices];
p = points[otherIndex];
if (IsPointInTriangle(ref p, ref a, ref b, ref c))
{
earFound = false;
break;
}
}
}
if (earFound)
{
vb.AddTriangle(i0, i1, i2);
sRestIndices.RemoveAt((restIndexPos + 1) % numRestIndices);
numRestIndices--;
restIndexPos = 0;
}
else
{
restIndexPos++;
if (restIndexPos == numRestIndices) break; // no more ears
}
}
vb.AddTriangle(sRestIndices[0], sRestIndices[1], sRestIndices[2]);
if (colors != null)
vb.RepeatColors(colors, 0, vb.currentVertCount);
if (lineWidth > 0)
DrawOutline(vb);
}
void DrawOutline(VertexBuffer vb)
{
int numVertices = points.Count;
int start = vb.currentVertCount - numVertices;
int k = vb.currentVertCount;
for (int i = 0; i < numVertices; i++)
{
Vector3 p0 = vb.vertices[start + i];
p0.y = -p0.y;
Vector3 p1;
if (i < numVertices - 1)
p1 = vb.vertices[start + i + 1];
else
p1 = vb.vertices[start];
p1.y = -p1.y;
Vector3 lineVector = p1 - p0;
Vector3 widthVector = Vector3.Cross(lineVector, new Vector3(0, 0, 1));
widthVector.Normalize();
vb.AddVert(p0 - widthVector * lineWidth * 0.5f, lineColor);
vb.AddVert(p0 + widthVector * lineWidth * 0.5f, lineColor);
vb.AddVert(p1 - widthVector * lineWidth * 0.5f, lineColor);
vb.AddVert(p1 + widthVector * lineWidth * 0.5f, lineColor);
k += 4;
vb.AddTriangle(k - 4, k - 3, k - 1);
vb.AddTriangle(k - 4, k - 1, k - 2);
//joint
if (i != 0)
{
vb.AddTriangle(k - 6, k - 5, k - 3);
vb.AddTriangle(k - 6, k - 3, k - 4);
}
if (i == numVertices - 1)
{
start += numVertices;
vb.AddTriangle(k - 2, k - 1, start + 1);
vb.AddTriangle(k - 2, start + 1, start);
}
}
}
bool IsPointInTriangle(ref Vector2 p, ref Vector2 a, ref Vector2 b, ref Vector2 c)
{
// From Starling
// This algorithm is described well in this article:
// http://www.blackpawn.com/texts/pointinpoly/default.html
float v0x = c.x - a.x;
float v0y = c.y - a.y;
float v1x = b.x - a.x;
float v1y = b.y - a.y;
float v2x = p.x - a.x;
float v2y = p.y - a.y;
float dot00 = v0x * v0x + v0y * v0y;
float dot01 = v0x * v1x + v0y * v1y;
float dot02 = v0x * v2x + v0y * v2y;
float dot11 = v1x * v1x + v1y * v1y;
float dot12 = v1x * v2x + v1y * v2y;
float invDen = 1.0f / (dot00 * dot11 - dot01 * dot01);
float u = (dot11 * dot02 - dot01 * dot12) * invDen;
float v = (dot00 * dot12 - dot01 * dot02) * invDen;
return (u >= 0) && (v >= 0) && (u + v < 1);
}
public bool HitTest(Rect contentRect, Vector2 point)
{
if (!contentRect.Contains(point))
return false;
// Algorithm & implementation thankfully taken from:
// -> http://alienryderflex.com/polygon/
// inspired by Starling
int len = points.Count;
int i;
int j = len - 1;
bool oddNodes = false;
float w = contentRect.width;
float h = contentRect.height;
for (i = 0; i < len; ++i)
{
float ix = points[i].x;
float iy = points[i].y;
float jx = points[j].x;
float jy = points[j].y;
if (usePercentPositions)
{
ix *= w;
iy *= h;
ix *= w;
iy *= h;
}
if ((iy < point.y && jy >= point.y || jy < point.y && iy >= point.y) && (ix <= point.x || jx <= point.x))
{
if (ix + (point.y - iy) / (jy - iy) * (jx - ix) < point.x)
oddNodes = !oddNodes;
}
j = i;
}
return oddNodes;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: db020628cd79e714fbafdbaaa2cc355f
timeCreated: 1545987173
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,85 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class RectMesh : IMeshFactory, IHitTest
{
/// <summary>
///
/// </summary>
public Rect? drawRect;
/// <summary>
///
/// </summary>
public float lineWidth;
/// <summary>
///
/// </summary>
public Color32 lineColor;
/// <summary>
///
/// </summary>
public Color32? fillColor;
/// <summary>
///
/// </summary>
public Color32[] colors;
public RectMesh()
{
lineColor = Color.black;
}
public void OnPopulateMesh(VertexBuffer vb)
{
Rect rect = drawRect != null ? (Rect)drawRect : vb.contentRect;
Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
if (lineWidth == 0)
{
if (color.a != 0)//optimized
vb.AddQuad(rect, color);
}
else
{
Rect part;
//left,right
part = new Rect(rect.x, rect.y, lineWidth, rect.height);
vb.AddQuad(part, lineColor);
part = new Rect(rect.xMax - lineWidth, rect.y, lineWidth, rect.height);
vb.AddQuad(part, lineColor);
//top, bottom
part = new Rect(rect.x + lineWidth, rect.y, rect.width - lineWidth * 2, lineWidth);
vb.AddQuad(part, lineColor);
part = new Rect(rect.x + lineWidth, rect.yMax - lineWidth, rect.width - lineWidth * 2, lineWidth);
vb.AddQuad(part, lineColor);
//middle
if (color.a != 0)//optimized
{
part = Rect.MinMaxRect(rect.x + lineWidth, rect.y + lineWidth, rect.xMax - lineWidth, rect.yMax - lineWidth);
if (part.width > 0 && part.height > 0)
vb.AddQuad(part, color);
}
}
if (colors != null)
vb.RepeatColors(colors, 0, vb.currentVertCount);
vb.AddTriangles();
}
public bool HitTest(Rect contentRect, Vector2 point)
{
return contentRect.Contains(point);
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 13a3a9d0d365d834696112114d7bf951
timeCreated: 1545987172
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,128 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class RegularPolygonMesh : IMeshFactory, IHitTest
{
/// <summary>
///
/// </summary>
public Rect? drawRect;
/// <summary>
///
/// </summary>
public int sides;
/// <summary>
///
/// </summary>
public float lineWidth;
/// <summary>
///
/// </summary>
public Color32 lineColor;
/// <summary>
///
/// </summary>
public Color32? centerColor;
/// <summary>
///
/// </summary>
public Color32? fillColor;
/// <summary>
///
/// </summary>
public float[] distances;
/// <summary>
///
/// </summary>
public float rotation;
public RegularPolygonMesh()
{
sides = 3;
lineColor = Color.black;
}
public void OnPopulateMesh(VertexBuffer vb)
{
if (distances != null && distances.Length < sides)
{
Debug.LogError("distances.Length<sides");
return;
}
Rect rect = drawRect != null ? (Rect)drawRect : vb.contentRect;
Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
float angleDelta = 2 * Mathf.PI / sides;
float angle = rotation * Mathf.Deg2Rad;
float radius = Mathf.Min(rect.width / 2, rect.height / 2);
float centerX = radius + rect.x;
float centerY = radius + rect.y;
vb.AddVert(new Vector3(centerX, centerY, 0), centerColor == null ? color : (Color32)centerColor);
for (int i = 0; i < sides; i++)
{
float r = radius;
if (distances != null)
r *= distances[i];
float xv = Mathf.Cos(angle) * (r - lineWidth);
float yv = Mathf.Sin(angle) * (r - lineWidth);
Vector3 vec = new Vector3(xv + centerX, yv + centerY, 0);
vb.AddVert(vec, color);
if (lineWidth > 0)
{
vb.AddVert(vec, lineColor);
xv = Mathf.Cos(angle) * r + centerX;
yv = Mathf.Sin(angle) * r + centerY;
vb.AddVert(new Vector3(xv, yv, 0), lineColor);
}
angle += angleDelta;
}
if (lineWidth > 0)
{
int tmp = sides * 3;
for (int i = 0; i < tmp; i += 3)
{
if (i != tmp - 3)
{
vb.AddTriangle(0, i + 1, i + 4);
vb.AddTriangle(i + 5, i + 2, i + 3);
vb.AddTriangle(i + 3, i + 6, i + 5);
}
else
{
vb.AddTriangle(0, i + 1, 1);
vb.AddTriangle(2, i + 2, i + 3);
vb.AddTriangle(i + 3, 3, 2);
}
}
}
else
{
for (int i = 0; i < sides; i++)
vb.AddTriangle(0, i + 1, (i == sides - 1) ? 1 : i + 2);
}
}
public bool HitTest(Rect contentRect, Vector2 point)
{
if (drawRect != null)
return ((Rect)drawRect).Contains(point);
else
return contentRect.Contains(point);
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: a4084bdb2f1738340a28ab5699977f3b
timeCreated: 1545987173
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,177 @@
using UnityEngine;
namespace FairyGUI
{
public class RoundedRectMesh : IMeshFactory, IHitTest
{
/// <summary>
///
/// </summary>
public Rect? drawRect;
/// <summary>
///
/// </summary>
public float lineWidth;
/// <summary>
///
/// </summary>
public Color32 lineColor;
/// <summary>
///
/// </summary>
public Color32? fillColor;
/// <summary>
///
/// </summary>
public float topLeftRadius;
/// <summary>
///
/// </summary>
public float topRightRadius;
/// <summary>
///
/// </summary>
public float bottomLeftRadius;
/// <summary>
///
/// </summary>
public float bottomRightRadius;
public RoundedRectMesh()
{
lineColor = Color.black;
}
public void OnPopulateMesh(VertexBuffer vb)
{
Rect rect = drawRect != null ? (Rect)drawRect : vb.contentRect;
Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
float radiusX = rect.width / 2;
float radiusY = rect.height / 2;
float cornerMaxRadius = Mathf.Min(radiusX, radiusY);
float centerX = radiusX + rect.x;
float centerY = radiusY + rect.y;
vb.AddVert(new Vector3(centerX, centerY, 0), color);
int cnt = vb.currentVertCount;
for (int i = 0; i < 4; i++)
{
float radius = 0;
switch (i)
{
case 0:
radius = bottomRightRadius;
break;
case 1:
radius = bottomLeftRadius;
break;
case 2:
radius = topLeftRadius;
break;
case 3:
radius = topRightRadius;
break;
}
radius = Mathf.Min(cornerMaxRadius, radius);
float offsetX = rect.x;
float offsetY = rect.y;
if (i == 0 || i == 3)
offsetX = rect.xMax - radius * 2;
if (i == 0 || i == 1)
offsetY = rect.yMax - radius * 2;
if (radius != 0)
{
int partNumSides = Mathf.Max(1, Mathf.CeilToInt(Mathf.PI * radius / 8)) + 1;
float angleDelta = Mathf.PI / 2 / partNumSides;
float angle = Mathf.PI / 2 * i;
float startAngle = angle;
for (int j = 1; j <= partNumSides; j++)
{
if (j == partNumSides) //消除精度误差带来的不对齐
angle = startAngle + Mathf.PI / 2;
Vector3 v1 = new Vector3(offsetX + Mathf.Cos(angle) * (radius - lineWidth) + radius,
offsetY + Mathf.Sin(angle) * (radius - lineWidth) + radius, 0);
vb.AddVert(v1, color);
if (lineWidth != 0)
{
vb.AddVert(v1, lineColor);
vb.AddVert(new Vector3(offsetX + Mathf.Cos(angle) * radius + radius,
offsetY + Mathf.Sin(angle) * radius + radius, 0), lineColor);
}
angle += angleDelta;
}
}
else
{
Vector3 v1 = new Vector3(offsetX, offsetY, 0);
if (lineWidth != 0)
{
if (i == 0 || i == 3)
offsetX -= lineWidth;
else
offsetX += lineWidth;
if (i == 0 || i == 1)
offsetY -= lineWidth;
else
offsetY += lineWidth;
Vector3 v2 = new Vector3(offsetX, offsetY, 0);
vb.AddVert(v2, color);
vb.AddVert(v2, lineColor);
vb.AddVert(v1, lineColor);
}
else
vb.AddVert(v1, color);
}
}
cnt = vb.currentVertCount - cnt;
if (lineWidth > 0)
{
for (int i = 0; i < cnt; i += 3)
{
if (i != cnt - 3)
{
vb.AddTriangle(0, i + 1, i + 4);
vb.AddTriangle(i + 5, i + 2, i + 3);
vb.AddTriangle(i + 3, i + 6, i + 5);
}
else
{
vb.AddTriangle(0, i + 1, 1);
vb.AddTriangle(2, i + 2, i + 3);
vb.AddTriangle(i + 3, 3, 2);
}
}
}
else
{
for (int i = 0; i < cnt; i++)
vb.AddTriangle(0, i + 1, (i == cnt - 1) ? 1 : i + 2);
}
}
public bool HitTest(Rect contentRect, Vector2 point)
{
if (drawRect != null)
return ((Rect)drawRect).Contains(point);
else
return contentRect.Contains(point);
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 2f910c4392a714c41b421c296621562b
timeCreated: 1545987172
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,90 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class StraightLineMesh : IMeshFactory
{
/// <summary>
///
/// </summary>
public Color color;
/// <summary>
///
/// </summary>
public Vector3 origin;
/// <summary>
///
/// </summary>
public Vector3 end;
/// <summary>
///
/// </summary>
public float lineWidth;
/// <summary>
///
/// </summary>
public bool repeatFill;
public StraightLineMesh()
{
color = Color.black;
lineWidth = 1;
}
/// <summary>
///
/// </summary>
/// <param name="lineWidth"></param>
/// <param name="color"></param>
/// <param name="repeatFill"></param>
public StraightLineMesh(float lineWidth, Color color, bool repeatFill)
{
this.lineWidth = lineWidth;
this.color = color;
this.repeatFill = repeatFill;
}
public void OnPopulateMesh(VertexBuffer vb)
{
if (origin == end)
return;
float length = Vector2.Distance(origin, end);
Vector3 lineVector = end - origin;
Vector3 widthVector = Vector3.Cross(lineVector, new Vector3(0, 0, 1));
widthVector.Normalize();
Vector3 v0, v1, v2, v3;
if (repeatFill)
{
float ratio = length / vb.textureSize.x;
v0 = VertexBuffer.NormalizedUV[0];
v1 = VertexBuffer.NormalizedUV[1];
v2 = new Vector2(ratio, 1);
v3 = new Vector2(ratio, 0);
}
else
{
v0 = new Vector2(vb.uvRect.xMin, vb.uvRect.yMin);
v1 = new Vector2(vb.uvRect.xMin, vb.uvRect.yMax);
v2 = new Vector2(vb.uvRect.xMax, vb.uvRect.yMax);
v3 = new Vector2(vb.uvRect.xMax, vb.uvRect.yMin);
}
vb.AddVert(origin - widthVector * lineWidth * 0.5f, color, v0);
vb.AddVert(origin + widthVector * lineWidth * 0.5f, color, v1);
vb.AddVert(end + widthVector * lineWidth * 0.5f, color, v2);
vb.AddVert(end - widthVector * lineWidth * 0.5f, color, v3);
vb.AddTriangles();
}
}
}

View File

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

View File

@@ -0,0 +1,441 @@
using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public sealed class VertexBuffer
{
/// <summary>
///
/// </summary>
public Rect contentRect;
/// <summary>
///
/// </summary>
public Rect uvRect;
/// <summary>
///
/// </summary>
public Color32 vertexColor;
/// <summary>
///
/// </summary>
public Vector2 textureSize;
/// <summary>
///
/// </summary>
public readonly List<Vector3> vertices;
/// <summary>
///
/// </summary>
public readonly List<Color32> colors;
/// <summary>
///
/// </summary>
public readonly List<Vector2> uvs;
/// <summary>
///
/// </summary>
public readonly List<Vector2> uvs2;
/// <summary>
///
/// </summary>
public readonly List<int> triangles;
static public Vector2[] NormalizedUV = new Vector2[] {
new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) };
static public Vector2[] NormalizedPosition = new Vector2[] {
new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1) };
internal bool _alphaInVertexColor;
internal bool _isArbitraryQuad;
static Stack<VertexBuffer> _pool = new Stack<VertexBuffer>();
/// <summary>
///
/// </summary>
/// <returns></returns>
public static VertexBuffer Begin()
{
if (_pool.Count > 0)
{
VertexBuffer inst = _pool.Pop();
inst.Clear();
return inst;
}
else
return new VertexBuffer();
}
/// <summary>
///
/// </summary>
/// <param name="source"></param>
public static VertexBuffer Begin(VertexBuffer source)
{
VertexBuffer vb = Begin();
vb.contentRect = source.contentRect;
vb.uvRect = source.uvRect;
vb.vertexColor = source.vertexColor;
vb.textureSize = source.textureSize;
return vb;
}
private VertexBuffer()
{
vertices = new List<Vector3>();
colors = new List<Color32>();
uvs = new List<Vector2>();
uvs2 = new List<Vector2>();
triangles = new List<int>();
}
/// <summary>
///
/// </summary>
public void End()
{
_pool.Push(this);
}
/// <summary>
///
/// </summary>
public void Clear()
{
vertices.Clear();
colors.Clear();
uvs.Clear();
uvs2.Clear();
triangles.Clear();
_isArbitraryQuad = false;
_alphaInVertexColor = false;
}
/// <summary>
///
/// </summary>
public int currentVertCount
{
get
{
return vertices.Count;
}
}
/// <summary>
///
/// </summary>
/// <param name="position"></param>
public void AddVert(Vector3 position)
{
position.y = -position.y;
vertices.Add(position);
colors.Add(vertexColor);
if (vertexColor.a != 255)
_alphaInVertexColor = true;
uvs.Add(new Vector2(
Mathf.Lerp(uvRect.xMin, uvRect.xMax, (position.x - contentRect.xMin) / contentRect.width),
Mathf.Lerp(uvRect.yMax, uvRect.yMin, (-position.y - contentRect.yMin) / contentRect.height)));
}
/// <summary>
///
/// </summary>
/// <param name="position"></param>
/// <param name="color"></param>
public void AddVert(Vector3 position, Color32 color)
{
position.y = -position.y;
vertices.Add(position);
colors.Add(color);
if (color.a != 255)
_alphaInVertexColor = true;
uvs.Add(new Vector2(
Mathf.Lerp(uvRect.xMin, uvRect.xMax, (position.x - contentRect.xMin) / contentRect.width),
Mathf.Lerp(uvRect.yMax, uvRect.yMin, (-position.y - contentRect.yMin) / contentRect.height)));
}
/// <summary>
///
/// </summary>
/// <param name="position"></param>
/// <param name="color"></param>
/// <param name="uv"></param>
public void AddVert(Vector3 position, Color32 color, Vector2 uv)
{
position.y = -position.y;
vertices.Add(position);
uvs.Add(new Vector2(uv.x, uv.y));
colors.Add(color);
if (color.a != 255)
_alphaInVertexColor = true;
}
/// <summary>
///
/// 1---2
/// | / |
/// 0---3
/// </summary>
/// <param name="vertRect"></param>
public void AddQuad(Rect vertRect)
{
AddVert(new Vector3(vertRect.xMin, vertRect.yMax, 0f));
AddVert(new Vector3(vertRect.xMin, vertRect.yMin, 0f));
AddVert(new Vector3(vertRect.xMax, vertRect.yMin, 0f));
AddVert(new Vector3(vertRect.xMax, vertRect.yMax, 0f));
}
/// <summary>
///
/// </summary>
/// <param name="vertRect"></param>
/// <param name="color"></param>
public void AddQuad(Rect vertRect, Color32 color)
{
AddVert(new Vector3(vertRect.xMin, vertRect.yMax, 0f), color);
AddVert(new Vector3(vertRect.xMin, vertRect.yMin, 0f), color);
AddVert(new Vector3(vertRect.xMax, vertRect.yMin, 0f), color);
AddVert(new Vector3(vertRect.xMax, vertRect.yMax, 0f), color);
}
/// <summary>
///
/// </summary>
/// <param name="vertRect"></param>
/// <param name="color"></param>
/// <param name="uvRect"></param>
public void AddQuad(Rect vertRect, Color32 color, Rect uvRect)
{
vertices.Add(new Vector3(vertRect.xMin, -vertRect.yMax, 0f));
vertices.Add(new Vector3(vertRect.xMin, -vertRect.yMin, 0f));
vertices.Add(new Vector3(vertRect.xMax, -vertRect.yMin, 0f));
vertices.Add(new Vector3(vertRect.xMax, -vertRect.yMax, 0f));
uvs.Add(new Vector2(uvRect.xMin, uvRect.yMin));
uvs.Add(new Vector2(uvRect.xMin, uvRect.yMax));
uvs.Add(new Vector2(uvRect.xMax, uvRect.yMax));
uvs.Add(new Vector2(uvRect.xMax, uvRect.yMin));
colors.Add(color);
colors.Add(color);
colors.Add(color);
colors.Add(color);
if (color.a != 255)
_alphaInVertexColor = true;
}
static List<Vector4> helperV4List = new List<Vector4>(4) { Vector4.zero, Vector4.zero, Vector4.zero, Vector4.zero };
internal List<Vector4> FixUVForArbitraryQuad()
{
//ref1 http://www.reedbeta.com/blog/quadrilateral-interpolation-part-1/
//ref2 https://bitlush.com/blog/arbitrary-quadrilaterals-in-opengl-es-2-0
Vector4 qq = Vector4.one;
Vector2 a = vertices[2] - vertices[0];
Vector2 b = vertices[1] - vertices[3];
Vector2 c = vertices[0] - vertices[3];
float cross = a.x * b.y - a.y * b.x;
if (cross != 0)
{
float s = (a.x * c.y - a.y * c.x) / cross;
if (s > 0 && s < 1)
{
float t = (b.x * c.y - b.y * c.x) / cross;
if (t > 0 && t < 1)
{
qq.x = 1 / (1 - t);
qq.y = 1 / s;
qq.z = 1 / t;
qq.w = 1 / (1 - s);
}
}
}
for (int i = 0; i < 4; i++)
{
Vector4 v = uvs[i];
float q = qq[i];
v.x *= q;
v.y *= q;
v.w = q;
helperV4List[i] = v;
}
return helperV4List;
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <param name="startIndex"></param>
/// <param name="count"></param>
public void RepeatColors(Color32[] value, int startIndex, int count)
{
int len = Mathf.Min(startIndex + count, vertices.Count);
int colorCount = value.Length;
int k = 0;
for (int i = startIndex; i < len; i++)
{
Color32 c = value[(k++) % colorCount];
colors[i] = c;
if (c.a != 255)
_alphaInVertexColor = true;
}
}
/// <summary>
///
/// </summary>
/// <param name="idx0"></param>
/// <param name="idx1"></param>
/// <param name="idx2"></param>
public void AddTriangle(int idx0, int idx1, int idx2)
{
triangles.Add(idx0);
triangles.Add(idx1);
triangles.Add(idx2);
}
/// <summary>
///
/// </summary>
/// <param name="idxList"></param>
/// <param name="startVertexIndex"></param>
public void AddTriangles(int[] idxList, int startVertexIndex = 0)
{
if (startVertexIndex != 0)
{
if (startVertexIndex < 0)
startVertexIndex = vertices.Count + startVertexIndex;
int cnt = idxList.Length;
for (int i = 0; i < cnt; i++)
triangles.Add(idxList[i] + startVertexIndex);
}
else
triangles.AddRange(idxList);
}
/// <summary>
///
/// </summary>
/// <param name="startVertexIndex"></param>
public void AddTriangles(int startVertexIndex = 0)
{
int cnt = vertices.Count;
if (startVertexIndex < 0)
startVertexIndex = cnt + startVertexIndex;
for (int i = startVertexIndex; i < cnt; i += 4)
{
triangles.Add(i);
triangles.Add(i + 1);
triangles.Add(i + 2);
triangles.Add(i + 2);
triangles.Add(i + 3);
triangles.Add(i);
}
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public Vector3 GetPosition(int index)
{
if (index < 0)
index = vertices.Count + index;
Vector3 vec = vertices[index];
vec.y = -vec.y;
return vec;
}
/// <summary>
///
/// </summary>
/// <param name="position"></param>
/// <param name="usePercent"></param>
/// <returns></returns>
public Vector2 GetUVAtPosition(Vector2 position, bool usePercent)
{
if (usePercent)
{
return new Vector2(Mathf.Lerp(uvRect.xMin, uvRect.xMax, position.x),
Mathf.Lerp(uvRect.yMax, uvRect.yMin, position.y));
}
else
return new Vector2(Mathf.Lerp(uvRect.xMin, uvRect.xMax, (position.x - contentRect.xMin) / contentRect.width),
Mathf.Lerp(uvRect.yMax, uvRect.yMin, (position.y - contentRect.yMin) / contentRect.height));
}
/// <summary>
///
/// </summary>
/// <param name="vb"></param>
public void Append(VertexBuffer vb)
{
int len = vertices.Count;
vertices.AddRange(vb.vertices);
uvs.AddRange(vb.uvs);
uvs2.AddRange(vb.uvs2);
colors.AddRange(vb.colors);
if (len != 0)
{
int len1 = vb.triangles.Count;
for (int i = 0; i < len1; i++)
triangles.Add(vb.triangles[i] + len);
}
else
triangles.AddRange(vb.triangles);
if (vb._alphaInVertexColor)
_alphaInVertexColor = true;
}
/// <summary>
///
/// </summary>
/// <param name="vb"></param>
public void Insert(VertexBuffer vb)
{
vertices.InsertRange(0, vb.vertices);
uvs.InsertRange(0, vb.uvs);
uvs2.InsertRange(0, vb.uvs2);
colors.InsertRange(0, vb.colors);
int len = triangles.Count;
if (len != 0)
{
int len1 = vb.vertices.Count;
for (int i = 0; i < len; i++)
triangles[i] += len1;
}
triangles.InsertRange(0, vb.triangles);
if (vb._alphaInVertexColor)
_alphaInVertexColor = true;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 8d5501e8f84e40e47835611ab3896029
timeCreated: 1545987173
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,414 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class MovieClip : Image
{
/// <summary>
///
/// </summary>
public class Frame
{
public NTexture texture;
public float addDelay;
}
/// <summary>
///
/// </summary>
public float interval;
/// <summary>
///
/// </summary>
public bool swing;
/// <summary>
///
/// </summary>
public float repeatDelay;
/// <summary>
///
/// </summary>
public float timeScale;
/// <summary>
/// Whether to ignore Unity time scale.
/// </summary>
public bool ignoreEngineTimeScale;
Frame[] _frames;
int _frameCount;
int _frame;
bool _playing;
int _start;
int _end;
int _times;
int _endAt;
int _status; //0-none, 1-next loop, 2-ending, 3-ended
float _frameElapsed; //当前帧延迟
bool _reversed;
int _repeatedCount;
TimerCallback _timerDelegate;
EventListener _onPlayEnd;
/// <summary>
///
/// </summary>
public MovieClip()
{
interval = 0.1f;
_playing = true;
_timerDelegate = OnTimer;
timeScale = 1;
ignoreEngineTimeScale = false;
if (Application.isPlaying)
{
onAddedToStage.Add(OnAddedToStage);
onRemovedFromStage.Add(OnRemoveFromStage);
}
SetPlaySettings();
}
/// <summary>
///
/// </summary>
public EventListener onPlayEnd
{
get { return _onPlayEnd ?? (_onPlayEnd = new EventListener(this, "onPlayEnd")); }
}
/// <summary>
///
/// </summary>
public Frame[] frames
{
get
{
return _frames;
}
set
{
_frames = value;
_scale9Grid = null;
_scaleByTile = false;
if (_frames == null)
{
_frameCount = 0;
graphics.texture = null;
CheckTimer();
return;
}
_frameCount = frames.Length;
if (_end == -1 || _end > _frameCount - 1)
_end = _frameCount - 1;
if (_endAt == -1 || _endAt > _frameCount - 1)
_endAt = _frameCount - 1;
if (_frame < 0 || _frame > _frameCount - 1)
_frame = _frameCount - 1;
InvalidateBatchingState();
_frameElapsed = 0;
_repeatedCount = 0;
_reversed = false;
DrawFrame();
CheckTimer();
}
}
/// <summary>
///
/// </summary>
public bool playing
{
get { return _playing; }
set
{
if (_playing != value)
{
_playing = value;
CheckTimer();
}
}
}
/// <summary>
///
/// </summary>
public int frame
{
get { return _frame; }
set
{
if (_frame != value)
{
if (_frames != null && value >= _frameCount)
value = _frameCount - 1;
_frame = value;
_frameElapsed = 0;
DrawFrame();
}
}
}
/// <summary>
///
/// </summary>
public void Rewind()
{
_frame = 0;
_frameElapsed = 0;
_reversed = false;
_repeatedCount = 0;
DrawFrame();
}
/// <summary>
///
/// </summary>
/// <param name="anotherMc"></param>
public void SyncStatus(MovieClip anotherMc)
{
_frame = anotherMc._frame;
_frameElapsed = anotherMc._frameElapsed;
_reversed = anotherMc._reversed;
_repeatedCount = anotherMc._repeatedCount;
DrawFrame();
}
/// <summary>
///
/// </summary>
/// <param name="time"></param>
public void Advance(float time)
{
int beginFrame = _frame;
bool beginReversed = _reversed;
float backupTime = time;
while (true)
{
float tt = interval + _frames[_frame].addDelay;
if (_frame == 0 && _repeatedCount > 0)
tt += repeatDelay;
if (time < tt)
{
_frameElapsed = 0;
break;
}
time -= tt;
if (swing)
{
if (_reversed)
{
_frame--;
if (_frame <= 0)
{
_frame = 0;
_repeatedCount++;
_reversed = !_reversed;
}
}
else
{
_frame++;
if (_frame > _frameCount - 1)
{
_frame = Mathf.Max(0, _frameCount - 2);
_repeatedCount++;
_reversed = !_reversed;
}
}
}
else
{
_frame++;
if (_frame > _frameCount - 1)
{
_frame = 0;
_repeatedCount++;
}
}
if (_frame == beginFrame && _reversed == beginReversed) //走了一轮了
{
float roundTime = backupTime - time; //这就是一轮需要的时间
time -= Mathf.FloorToInt(time / roundTime) * roundTime; //跳过
}
}
DrawFrame();
}
/// <summary>
///
/// </summary>
public void SetPlaySettings()
{
SetPlaySettings(0, -1, 0, -1);
}
/// <summary>
/// 从start帧开始播放到end帧-1表示结尾重复times次0表示无限循环循环结束后停止在endAt帧-1表示参数end
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="times"></param>
/// <param name="endAt"></param>
public void SetPlaySettings(int start, int end, int times, int endAt)
{
_start = start;
_end = end;
if (_end == -1 || _end > _frameCount - 1)
_end = _frameCount - 1;
_times = times;
_endAt = endAt;
if (_endAt == -1)
_endAt = _end;
_status = 0;
this.frame = start;
}
void OnAddedToStage()
{
if (_playing && _frameCount > 0)
Timers.inst.AddUpdate(_timerDelegate);
}
void OnRemoveFromStage()
{
Timers.inst.Remove(_timerDelegate);
}
void CheckTimer()
{
if (!Application.isPlaying)
return;
if (_playing && _frameCount > 0 && this.stage != null)
Timers.inst.AddUpdate(_timerDelegate);
else
Timers.inst.Remove(_timerDelegate);
}
void OnTimer(object param)
{
if (!_playing || _frameCount == 0 || _status == 3)
return;
float dt;
if (ignoreEngineTimeScale)
{
dt = Time.unscaledDeltaTime;
if (dt > 0.1f)
dt = 0.1f;
}
else
dt = Time.deltaTime;
if (timeScale != 1)
dt *= timeScale;
_frameElapsed += dt;
float tt = interval + _frames[_frame].addDelay;
if (_frame == 0 && _repeatedCount > 0)
tt += repeatDelay;
if (_frameElapsed < tt)
return;
_frameElapsed -= tt;
if (_frameElapsed > interval)
_frameElapsed = interval;
if (swing)
{
if (_reversed)
{
_frame--;
if (_frame <= 0)
{
_frame = 0;
_repeatedCount++;
_reversed = !_reversed;
}
}
else
{
_frame++;
if (_frame > _frameCount - 1)
{
_frame = Mathf.Max(0, _frameCount - 2);
_repeatedCount++;
_reversed = !_reversed;
}
}
}
else
{
_frame++;
if (_frame > _frameCount - 1)
{
_frame = 0;
_repeatedCount++;
}
}
if (_status == 1) //new loop
{
_frame = _start;
_frameElapsed = 0;
_status = 0;
DrawFrame();
}
else if (_status == 2) //ending
{
_frame = _endAt;
_frameElapsed = 0;
_status = 3; //ended
DrawFrame();
DispatchEvent("onPlayEnd", null);
}
else
{
DrawFrame();
if (_frame == _end)
{
if (_times > 0)
{
_times--;
if (_times == 0)
_status = 2; //ending
else
_status = 1; //new loop
}
else if (_start != 0)
_status = 1; //new loop
}
}
}
void DrawFrame()
{
if (_frameCount > 0)
{
Frame frame = _frames[_frame];
graphics.texture = frame.texture;
}
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 5ddeeb52259b7ed4e8a604fa8cd47897
timeCreated: 1535374214
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,67 @@
using System;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class NAudioClip
{
public static Action<AudioClip> CustomDestroyMethod;
/// <summary>
///
/// </summary>
public DestroyMethod destroyMethod;
/// <summary>
///
/// </summary>
public AudioClip nativeClip;
/// <summary>
///
/// </summary>
/// <param name="audioClip"></param>
public NAudioClip(AudioClip audioClip)
{
nativeClip = audioClip;
}
/// <summary>
///
/// </summary>
public void Unload()
{
if (nativeClip == null)
return;
if (destroyMethod == DestroyMethod.Unload)
Resources.UnloadAsset(nativeClip);
else if (destroyMethod == DestroyMethod.Destroy)
UnityEngine.Object.DestroyImmediate(nativeClip, true);
else if (destroyMethod == DestroyMethod.Custom)
{
if (CustomDestroyMethod == null)
Debug.LogWarning("NAudioClip.CustomDestroyMethod must be set to handle DestroyMethod.Custom");
else
CustomDestroyMethod(nativeClip);
}
nativeClip = null;
}
/// <summary>
///
/// </summary>
/// <param name="audioClip"></param>
public void Reload(AudioClip audioClip)
{
if (nativeClip != null && nativeClip != audioClip)
Unload();
nativeClip = audioClip;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: e081bc28928c3474194543c862fadec5
timeCreated: 1535374215
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,842 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI.Utils;
using Object = UnityEngine.Object;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class NGraphics : IMeshFactory
{
/// <summary>
///
/// </summary>
public GameObject gameObject { get; private set; }
/// <summary>
///
/// </summary>
public MeshFilter meshFilter { get; private set; }
/// <summary>
///
/// </summary>
public MeshRenderer meshRenderer { get; private set; }
/// <summary>
///
/// </summary>
public Mesh mesh { get; private set; }
/// <summary>
///
/// </summary>
public BlendMode blendMode;
/// <summary>
/// 不参与剪裁
/// </summary>
public bool dontClip;
/// <summary>
/// 当Mesh更新时触发
/// </summary>
public event Action meshModifier;
NTexture _texture;
string _shader;
Material _material;
int _customMatarial; //0-none, 1-common, 2-support internal mask, 128-owns material
MaterialManager _manager;
string[] _shaderKeywords;
int _materialFlags;
IMeshFactory _meshFactory;
float _alpha;
Color _color;
bool _meshDirty;
Rect _contentRect;
FlipType _flip;
public class VertexMatrix
{
public Vector3 cameraPos;
public Matrix4x4 matrix;
}
VertexMatrix _vertexMatrix;
bool hasAlphaBackup;
List<byte> _alphaBackup; //透明度改变需要通过修改顶点颜色实现,但顶点颜色本身可能就带有透明度,所以这里要有一个备份
internal int _maskFlag;
StencilEraser _stencilEraser;
#if !UNITY_5_6_OR_NEWER
Color32[] _colors;
#endif
MaterialPropertyBlock _propertyBlock;
bool _blockUpdated;
/// <summary>
///
/// </summary>
/// <param name="gameObject"></param>
public NGraphics(GameObject gameObject)
{
this.gameObject = gameObject;
_alpha = 1f;
_shader = ShaderConfig.imageShader;
_color = Color.white;
_meshFactory = this;
meshFilter = gameObject.AddComponent<MeshFilter>();
meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
meshRenderer.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
meshRenderer.receiveShadows = false;
mesh = new Mesh();
mesh.name = gameObject.name;
mesh.MarkDynamic();
meshFilter.mesh = mesh;
meshFilter.hideFlags = DisplayObject.hideFlags;
meshRenderer.hideFlags = DisplayObject.hideFlags;
mesh.hideFlags = DisplayObject.hideFlags;
Stats.LatestGraphicsCreation++;
}
/// <summary>
///
/// </summary>
public IMeshFactory meshFactory
{
get { return _meshFactory; }
set
{
if (_meshFactory != value)
{
_meshFactory = value;
_meshDirty = true;
}
}
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetMeshFactory<T>() where T : IMeshFactory, new()
{
if (!(_meshFactory is T))
{
_meshFactory = new T();
_meshDirty = true;
}
return (T)_meshFactory;
}
/// <summary>
///
/// </summary>
public Rect contentRect
{
get { return _contentRect; }
set
{
_contentRect = value;
_meshDirty = true;
}
}
/// <summary>
///
/// </summary>
public FlipType flip
{
get { return _flip; }
set
{
if (_flip != value)
{
_flip = value;
_meshDirty = true;
}
}
}
/// <summary>
///
/// </summary>
public NTexture texture
{
get { return _texture; }
set
{
if (_texture != value)
{
if (value != null)
value.AddRef();
if (_texture != null)
_texture.ReleaseRef();
_texture = value;
if (_customMatarial != 0 && _material != null)
_material.mainTexture = _texture != null ? _texture.nativeTexture : null;
_meshDirty = true;
UpdateManager();
}
}
}
/// <summary>
///
/// </summary>
public string shader
{
get { return _shader; }
set
{
_shader = value;
UpdateManager();
}
}
/// <summary>
///
/// </summary>
/// <param name="shader"></param>
/// <param name="texture"></param>
public void SetShaderAndTexture(string shader, NTexture texture)
{
_shader = shader;
if (_texture != texture)
this.texture = texture;
else
UpdateManager();
}
/// <summary>
///
/// </summary>
public Material material
{
get
{
if (_customMatarial == 0 && _material == null && _manager != null)
_material = _manager.GetMaterial(_materialFlags, blendMode, 0);
return _material;
}
set
{
if ((_customMatarial & 128) != 0 && _material != null)
Object.DestroyImmediate(_material);
_material = value;
if (_material != null)
{
_customMatarial = 1;
if (_material.HasProperty(ShaderConfig.ID_Stencil) || _material.HasProperty(ShaderConfig.ID_ClipBox))
_customMatarial |= 2;
meshRenderer.sharedMaterial = _material;
if (_texture != null)
_material.mainTexture = _texture.nativeTexture;
}
else
{
_customMatarial = 0;
meshRenderer.sharedMaterial = null;
}
}
}
/// <summary>
/// Same as material property except that ownership is transferred to this object.
/// </summary>
/// <param name="material"></param>
public void SetMaterial(Material material)
{
this.material = material;
_customMatarial |= 128;
}
/// <summary>
///
/// </summary>
public string[] materialKeywords
{
get { return _shaderKeywords; }
set
{
_shaderKeywords = value;
UpdateMaterialFlags();
}
}
/// <summary>
///
/// </summary>
/// <param name="keyword"></param>
/// <param name="enabled"></param>
public void ToggleKeyword(string keyword, bool enabled)
{
if (enabled)
{
if (_shaderKeywords == null)
{
_shaderKeywords = new string[] { keyword };
UpdateMaterialFlags();
}
else if (Array.IndexOf(_shaderKeywords, keyword) == -1)
{
Array.Resize(ref _shaderKeywords, _shaderKeywords.Length + 1);
_shaderKeywords[_shaderKeywords.Length - 1] = keyword;
UpdateMaterialFlags();
}
}
else
{
if (_shaderKeywords != null)
{
int i = Array.IndexOf(_shaderKeywords, keyword);
if (i != -1)
{
_shaderKeywords[i] = null;
UpdateMaterialFlags();
}
}
}
}
void UpdateManager()
{
if (_texture != null)
_manager = _texture.GetMaterialManager(_shader);
else
_manager = null;
UpdateMaterialFlags();
}
void UpdateMaterialFlags()
{
if (_customMatarial != 0)
{
if (material != null)
material.shaderKeywords = _shaderKeywords;
}
else if (_shaderKeywords != null && _manager != null)
_materialFlags = _manager.GetFlagsByKeywords(_shaderKeywords);
else
_materialFlags = 0;
}
/// <summary>
///
/// </summary>
public bool enabled
{
get { return meshRenderer.enabled; }
set { meshRenderer.enabled = value; }
}
/// <summary>
///
/// </summary>
public int sortingOrder
{
get { return meshRenderer.sortingOrder; }
set { meshRenderer.sortingOrder = value; }
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
internal void _SetStencilEraserOrder(int value)
{
_stencilEraser.meshRenderer.sortingOrder = value;
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
public Color color
{
get { return _color; }
set { _color = value; }
}
/// <summary>
///
/// </summary>
public void Tint()
{
if (_meshDirty)
return;
int vertCount = mesh.vertexCount;
if (vertCount == 0)
return;
#if !UNITY_5_6_OR_NEWER
Color32[] colors = _colors;
if (colors == null)
colors = mesh.colors32;
#else
VertexBuffer vb = VertexBuffer.Begin();
mesh.GetColors(vb.colors);
List<Color32> colors = vb.colors;
#endif
for (int i = 0; i < vertCount; i++)
{
Color32 col = _color;
col.a = (byte)(_alpha * (hasAlphaBackup ? _alphaBackup[i] : (byte)255));
colors[i] = col;
}
#if !UNITY_5_6_OR_NEWER
mesh.colors32 = colors;
#else
mesh.SetColors(vb.colors);
vb.End();
#endif
}
void ChangeAlpha(float value)
{
_alpha = value;
int vertCount = mesh.vertexCount;
if (vertCount == 0)
return;
#if !UNITY_5_6_OR_NEWER
Color32[] colors = _colors;
if (colors == null)
colors = mesh.colors32;
#else
VertexBuffer vb = VertexBuffer.Begin();
mesh.GetColors(vb.colors);
List<Color32> colors = vb.colors;
#endif
for (int i = 0; i < vertCount; i++)
{
Color32 col = colors[i];
col.a = (byte)(_alpha * (hasAlphaBackup ? _alphaBackup[i] : (byte)255));
colors[i] = col;
}
#if !UNITY_5_6_OR_NEWER
mesh.colors32 = colors;
#else
mesh.SetColors(vb.colors);
vb.End();
#endif
}
/// <summary>
///
/// </summary>
public VertexMatrix vertexMatrix
{
get { return _vertexMatrix; }
set
{
_vertexMatrix = value;
_meshDirty = true;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public MaterialPropertyBlock materialPropertyBlock
{
get
{
if (_propertyBlock == null)
_propertyBlock = new MaterialPropertyBlock();
_blockUpdated = true;
return _propertyBlock;
}
}
/// <summary>
///
/// </summary>
public void SetMeshDirty()
{
_meshDirty = true;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool UpdateMesh()
{
if (_meshDirty)
{
UpdateMeshNow();
return true;
}
else
return false;
}
/// <summary>
///
/// </summary>
public void Dispose()
{
if (mesh != null)
{
if (Application.isPlaying)
Object.Destroy(mesh);
else
Object.DestroyImmediate(mesh);
mesh = null;
}
if ((_customMatarial & 128) != 0 && _material != null)
Object.DestroyImmediate(_material);
if (_texture != null)
{
_texture.ReleaseRef();
_texture = null;
}
_manager = null;
_material = null;
meshRenderer = null;
meshFilter = null;
_stencilEraser = null;
meshModifier = null;
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="alpha"></param>
/// <param name="grayed"></param>
public void Update(UpdateContext context, float alpha, bool grayed)
{
Stats.GraphicsCount++;
if (_meshDirty)
{
_alpha = alpha;
UpdateMeshNow();
}
else if (_alpha != alpha)
ChangeAlpha(alpha);
if (_propertyBlock != null && _blockUpdated)
{
meshRenderer.SetPropertyBlock(_propertyBlock);
_blockUpdated = false;
}
if (_customMatarial != 0)
{
if ((_customMatarial & 2) != 0 && _material != null)
context.ApplyClippingProperties(_material, false);
}
else
{
if (_manager != null)
{
if (_maskFlag == 1)
{
_material = _manager.GetMaterial((int)MaterialFlags.AlphaMask | _materialFlags, BlendMode.Normal, context.clipInfo.clipId);
context.ApplyAlphaMaskProperties(_material, false);
}
else
{
int matFlags = _materialFlags;
if (grayed)
matFlags |= (int)MaterialFlags.Grayed;
if (context.clipped)
{
if (context.stencilReferenceValue > 0)
matFlags |= (int)MaterialFlags.StencilTest;
if (context.rectMaskDepth > 0)
{
if (context.clipInfo.soft)
matFlags |= (int)MaterialFlags.SoftClipped;
else
matFlags |= (int)MaterialFlags.Clipped;
}
_material = _manager.GetMaterial(matFlags, blendMode, context.clipInfo.clipId);
if (_manager.firstMaterialInFrame)
context.ApplyClippingProperties(_material, true);
}
else
_material = _manager.GetMaterial(matFlags, blendMode, 0);
}
}
else
_material = null;
if (!Material.ReferenceEquals(_material, meshRenderer.sharedMaterial))
meshRenderer.sharedMaterial = _material;
}
if (_maskFlag != 0)
{
if (_maskFlag == 1)
_maskFlag = 2;
else
{
if (_stencilEraser != null)
_stencilEraser.enabled = false;
_maskFlag = 0;
}
}
}
internal void _PreUpdateMask(UpdateContext context, uint maskId)
{
//_maskFlag: 0-new mask, 1-active mask, 2-mask complete
if (_maskFlag == 0)
{
if (_stencilEraser == null)
{
_stencilEraser = new StencilEraser(gameObject.transform);
_stencilEraser.meshFilter.mesh = mesh;
}
else
_stencilEraser.enabled = true;
}
_maskFlag = 1;
if (_manager != null)
{
//这里使用maskId而不是clipInfo.clipId是因为遮罩有两个用途一个是写入遮罩一个是擦除两个不能用同一个材质
Material mat = _manager.GetMaterial((int)MaterialFlags.AlphaMask | _materialFlags, BlendMode.Normal, maskId);
if (!Material.ReferenceEquals(mat, _stencilEraser.meshRenderer.sharedMaterial))
_stencilEraser.meshRenderer.sharedMaterial = mat;
context.ApplyAlphaMaskProperties(mat, true);
}
}
void UpdateMeshNow()
{
_meshDirty = false;
if (_texture == null || _meshFactory == null)
{
if (mesh.vertexCount > 0)
{
mesh.Clear();
if (meshModifier != null)
meshModifier();
}
return;
}
VertexBuffer vb = VertexBuffer.Begin();
vb.contentRect = _contentRect;
vb.uvRect = _texture.uvRect;
if (_texture != null)
vb.textureSize = new Vector2(_texture.width, _texture.height);
else
vb.textureSize = new Vector2(0, 0);
if (_flip != FlipType.None)
{
if (_flip == FlipType.Horizontal || _flip == FlipType.Both)
{
float tmp = vb.uvRect.xMin;
vb.uvRect.xMin = vb.uvRect.xMax;
vb.uvRect.xMax = tmp;
}
if (_flip == FlipType.Vertical || _flip == FlipType.Both)
{
float tmp = vb.uvRect.yMin;
vb.uvRect.yMin = vb.uvRect.yMax;
vb.uvRect.yMax = tmp;
}
}
vb.vertexColor = _color;
_meshFactory.OnPopulateMesh(vb);
int vertCount = vb.currentVertCount;
if (vertCount == 0)
{
if (mesh.vertexCount > 0)
{
mesh.Clear();
if (meshModifier != null)
meshModifier();
}
vb.End();
return;
}
if (_texture.rotated)
{
float xMin = _texture.uvRect.xMin;
float yMin = _texture.uvRect.yMin;
float yMax = _texture.uvRect.yMax;
float tmp;
for (int i = 0; i < vertCount; i++)
{
Vector2 vec = vb.uvs[i];
tmp = vec.y;
vec.y = yMin + vec.x - xMin;
vec.x = xMin + yMax - tmp;
vb.uvs[i] = vec;
}
}
hasAlphaBackup = vb._alphaInVertexColor;
if (hasAlphaBackup)
{
if (_alphaBackup == null)
_alphaBackup = new List<byte>();
else
_alphaBackup.Clear();
for (int i = 0; i < vertCount; i++)
{
Color32 col = vb.colors[i];
_alphaBackup.Add(col.a);
col.a = (byte)(col.a * _alpha);
vb.colors[i] = col;
}
}
else if (_alpha != 1)
{
for (int i = 0; i < vertCount; i++)
{
Color32 col = vb.colors[i];
col.a = (byte)(col.a * _alpha);
vb.colors[i] = col;
}
}
if (_vertexMatrix != null)
{
Vector3 camPos = _vertexMatrix.cameraPos;
Vector3 center = new Vector3(camPos.x, camPos.y, 0);
center -= _vertexMatrix.matrix.MultiplyPoint(center);
for (int i = 0; i < vertCount; i++)
{
Vector3 pt = vb.vertices[i];
pt = _vertexMatrix.matrix.MultiplyPoint(pt);
pt += center;
Vector3 vec = pt - camPos;
float lambda = -camPos.z / vec.z;
pt.x = camPos.x + lambda * vec.x;
pt.y = camPos.y + lambda * vec.y;
pt.z = 0;
vb.vertices[i] = pt;
}
}
mesh.Clear();
#if UNITY_5_2 || UNITY_5_3_OR_NEWER
mesh.SetVertices(vb.vertices);
if (vb._isArbitraryQuad)
mesh.SetUVs(0, vb.FixUVForArbitraryQuad());
else
mesh.SetUVs(0, vb.uvs);
mesh.SetColors(vb.colors);
mesh.SetTriangles(vb.triangles, 0);
if (vb.uvs2.Count == vb.uvs.Count)
mesh.SetUVs(1, vb.uvs2);
#if !UNITY_5_6_OR_NEWER
_colors = null;
#endif
#else
Vector3[] vertices = new Vector3[vertCount];
Vector2[] uv = new Vector2[vertCount];
_colors = new Color32[vertCount];
int[] triangles = new int[vb.triangles.Count];
vb.vertices.CopyTo(vertices);
vb.uvs.CopyTo(uv);
vb.colors.CopyTo(_colors);
vb.triangles.CopyTo(triangles);
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
mesh.colors32 = _colors;
if(vb.uvs2.Count==uv.Length)
{
uv = new Vector2[vertCount];
vb.uvs2.CopyTo(uv);
mesh.uv2 = uv;
}
#endif
vb.End();
if (meshModifier != null)
meshModifier();
}
public void OnPopulateMesh(VertexBuffer vb)
{
Rect rect = texture.GetDrawRect(vb.contentRect);
vb.AddQuad(rect, vb.vertexColor, vb.uvRect);
vb.AddTriangles();
vb._isArbitraryQuad = _vertexMatrix != null;
}
class StencilEraser
{
public GameObject gameObject;
public MeshFilter meshFilter;
public MeshRenderer meshRenderer;
public StencilEraser(Transform parent)
{
gameObject = new GameObject("StencilEraser");
gameObject.transform.SetParent(parent, false);
meshFilter = gameObject.AddComponent<MeshFilter>();
meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
meshRenderer.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
meshRenderer.receiveShadows = false;
gameObject.layer = parent.gameObject.layer;
gameObject.hideFlags = parent.gameObject.hideFlags;
meshFilter.hideFlags = parent.gameObject.hideFlags;
meshRenderer.hideFlags = parent.gameObject.hideFlags;
}
public bool enabled
{
get { return meshRenderer.enabled; }
set { meshRenderer.enabled = value; }
}
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: b84f79fb60acc974cb58c5368b257716
timeCreated: 1535374215
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,515 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public enum DestroyMethod
{
Destroy,
Unload,
None,
ReleaseTemp,
Custom
}
/// <summary>
///
/// </summary>
public class NTexture
{
/// <summary>
/// This event will trigger when a texture is destroying if its destroyMethod is Custom
/// </summary>
public static event Action<Texture> CustomDestroyMethod;
/// <summary>
///
/// </summary>
public Rect uvRect;
/// <summary>
///
/// </summary>
public bool rotated;
/// <summary>
///
/// </summary>
public int refCount;
/// <summary>
///
/// </summary>
public float lastActive;
/// <summary>
///
/// </summary>
public DestroyMethod destroyMethod;
/// <summary>
/// This event will trigger when texture reloaded and size changed.
/// </summary>
public event Action<NTexture> onSizeChanged;
/// <summary>
/// This event will trigger when ref count is zero.
/// </summary>
public event Action<NTexture> onRelease;
private Texture _nativeTexture;
private Texture _alphaTexture;
private Rect _region;
private Vector2 _offset;
private Vector2 _originalSize;
private NTexture _root;
private Dictionary<string, MaterialManager> _materialManagers;
internal static Texture2D CreateEmptyTexture()
{
var emptyTexture = new Texture2D(1, 1, TextureFormat.RGB24, false);
emptyTexture.name = "White Texture";
emptyTexture.hideFlags = DisplayObject.hideFlags;
emptyTexture.SetPixel(0, 0, Color.white);
emptyTexture.Apply();
return emptyTexture;
}
private static NTexture _empty;
/// <summary>
///
/// </summary>
public static NTexture Empty
{
get
{
if (_empty == null)
_empty = new NTexture(CreateEmptyTexture());
return _empty;
}
}
/// <summary>
///
/// </summary>
public static void DisposeEmpty()
{
if (_empty != null)
{
var tmp = _empty;
_empty = null;
tmp.Dispose();
}
}
/// <summary>
///
/// </summary>
/// <param name="texture"></param>
public NTexture(Texture texture) : this(texture, null, 1, 1)
{
}
/// <summary>
///
/// </summary>
/// <param name="texture"></param>
/// <param name="xScale"></param>
/// <param name="yScale"></param>
public NTexture(Texture texture, Texture alphaTexture, float xScale, float yScale)
{
_root = this;
_nativeTexture = texture;
_alphaTexture = alphaTexture;
uvRect = new Rect(0, 0, xScale, yScale);
if (yScale < 0)
{
uvRect.y = -yScale;
uvRect.yMax = 0;
}
if (xScale < 0)
{
uvRect.x = -xScale;
uvRect.xMax = 0;
}
if (_nativeTexture != null)
_originalSize = new Vector2(_nativeTexture.width, _nativeTexture.height);
_region = new Rect(0, 0, _originalSize.x, _originalSize.y);
}
/// <summary>
///
/// </summary>
/// <param name="texture"></param>
/// <param name="region"></param>
public NTexture(Texture texture, Rect region)
{
_root = this;
_nativeTexture = texture;
_region = region;
_originalSize = new Vector2(_region.width, _region.height);
if (_nativeTexture != null)
uvRect = new Rect(region.x / _nativeTexture.width, 1 - region.yMax / _nativeTexture.height,
region.width / _nativeTexture.width, region.height / _nativeTexture.height);
else
uvRect.Set(0, 0, 1, 1);
}
/// <summary>
///
/// </summary>
/// <param name="root"></param>
/// <param name="region"></param>
/// <param name="rotated"></param>
public NTexture(NTexture root, Rect region, bool rotated)
{
_root = root;
this.rotated = rotated;
region.x += root._region.x;
region.y += root._region.y;
uvRect = new Rect(region.x * root.uvRect.width / root.width,
1 - region.yMax * root.uvRect.height / root.height,
region.width * root.uvRect.width / root.width, region.height * root.uvRect.height / root.height);
if (rotated)
{
var tmp = region.width;
region.width = region.height;
region.height = tmp;
tmp = uvRect.width;
uvRect.width = uvRect.height;
uvRect.height = tmp;
}
_region = region;
_originalSize = _region.size;
}
/// <summary>
///
/// </summary>
/// <param name="root"></param>
/// <param name="region"></param>
/// <param name="rotated"></param>
/// <param name="originalSize"></param>
/// <param name="offset"></param>
public NTexture(NTexture root, Rect region, bool rotated, Vector2 originalSize, Vector2 offset)
: this(root, region, rotated)
{
_originalSize = originalSize;
_offset = offset;
}
/// <summary>
///
/// </summary>
/// <param name="sprite"></param>
public NTexture(Sprite sprite)
{
var rect = sprite.textureRect;
rect.y = sprite.texture.height - rect.yMax;
_root = this;
_nativeTexture = sprite.texture;
_region = rect;
_originalSize = new Vector2(_region.width, _region.height);
uvRect = new Rect(_region.x / _nativeTexture.width, 1 - _region.yMax / _nativeTexture.height,
_region.width / _nativeTexture.width, _region.height / _nativeTexture.height);
}
/// <summary>
///
/// </summary>
public int width => (int)_region.width;
/// <summary>
///
/// </summary>
public int height => (int)_region.height;
/// <summary>
/// 修改源码unity里sprite用fgui的图转换时要用
/// </summary>
public Rect region => _region;
/// <summary>
///
/// </summary>
public Vector2 offset
{
get => _offset;
set => _offset = value;
}
/// <summary>
///
/// </summary>
public Vector2 originalSize
{
get => _originalSize;
set => _originalSize = value;
}
/// <summary>
///
/// </summary>
/// <param name="drawRect"></param>
/// <returns></returns>
public Rect GetDrawRect(Rect drawRect)
{
if (_originalSize.x == _region.width && _originalSize.y == _region.height)
return drawRect;
var sx = drawRect.width / _originalSize.x;
var sy = drawRect.height / _originalSize.y;
return new Rect(_offset.x * sx, _offset.y * sy, _region.width * sx, _region.height * sy);
}
/// <summary>
///
/// </summary>
/// <param name="uv"></param>
public void GetUV(Vector2[] uv)
{
uv[0] = uvRect.position;
uv[1] = new Vector2(uvRect.xMin, uvRect.yMax);
uv[2] = new Vector2(uvRect.xMax, uvRect.yMax);
uv[3] = new Vector2(uvRect.xMax, uvRect.yMin);
if (rotated)
{
var xMin = uvRect.xMin;
var yMin = uvRect.yMin;
var yMax = uvRect.yMax;
float tmp;
for (var i = 0; i < 4; i++)
{
var m = uv[i];
tmp = m.y;
m.y = yMin + m.x - xMin;
m.x = xMin + yMax - tmp;
uv[i] = m;
}
}
}
/// <summary>
///
/// </summary>
public NTexture root => _root;
/// <summary>
///
/// </summary>
public bool disposed => _root == null;
/// <summary>
///
/// </summary>
public Texture nativeTexture => _root != null ? _root._nativeTexture : null;
/// <summary>
///
/// </summary>
public Texture alphaTexture => _root != null ? _root._alphaTexture : null;
/// <summary>
///
/// </summary>
public MaterialManager GetMaterialManager(string shaderName)
{
if (_root != this)
{
if (_root == null)
return null;
else
return _root.GetMaterialManager(shaderName);
}
if (_materialManagers == null)
_materialManagers = new Dictionary<string, MaterialManager>();
MaterialManager mm;
if (!_materialManagers.TryGetValue(shaderName, out mm))
{
mm = new MaterialManager(this, ShaderConfig.GetShader(shaderName));
_materialManagers.Add(shaderName, mm);
}
return mm;
}
/// <summary>
///
/// </summary>
public void Unload()
{
Unload(false);
}
/// <summary>
///
/// </summary>
public void Unload(bool destroyMaterials)
{
if (this == _empty)
return;
if (_root != this)
throw new Exception("Unload is not allow to call on none root NTexture.");
if (_nativeTexture != null)
{
DestroyTexture();
if (destroyMaterials)
DestroyMaterials();
else
RefreshMaterials();
}
}
/// <summary>
///
/// </summary>
/// <param name="nativeTexture"></param>
/// <param name="alphaTexture"></param>
public void Reload(Texture nativeTexture, Texture alphaTexture)
{
if (_root != this)
throw new Exception("Reload is not allow to call on none root NTexture.");
if (_nativeTexture != null && _nativeTexture != nativeTexture)
DestroyTexture();
_nativeTexture = nativeTexture;
_alphaTexture = alphaTexture;
var lastSize = _originalSize;
if (_nativeTexture != null)
_originalSize = new Vector2(_nativeTexture.width, _nativeTexture.height);
else
_originalSize = Vector2.zero;
_region = new Rect(0, 0, _originalSize.x, _originalSize.y);
RefreshMaterials();
if (onSizeChanged != null && lastSize != _originalSize)
onSizeChanged(this);
}
private void DestroyTexture()
{
switch (destroyMethod)
{
case DestroyMethod.Destroy:
Object.DestroyImmediate(_nativeTexture, true);
if (_alphaTexture != null)
Object.DestroyImmediate(_alphaTexture, true);
break;
case DestroyMethod.Unload:
Resources.UnloadAsset(_nativeTexture);
if (_alphaTexture != null)
Resources.UnloadAsset(_alphaTexture);
break;
case DestroyMethod.ReleaseTemp:
RenderTexture.ReleaseTemporary((RenderTexture)_nativeTexture);
if (_alphaTexture is RenderTexture)
RenderTexture.ReleaseTemporary((RenderTexture)_alphaTexture);
break;
case DestroyMethod.Custom:
if (CustomDestroyMethod == null)
{
Debug.LogWarning("NTexture.CustomDestroyMethod must be set to handle DestroyMethod.Custom");
}
else
{
CustomDestroyMethod(_nativeTexture);
if (_alphaTexture != null)
CustomDestroyMethod(_alphaTexture);
}
break;
}
_nativeTexture = null;
_alphaTexture = null;
}
private void RefreshMaterials()
{
if (_materialManagers != null && _materialManagers.Count > 0)
{
var iter = _materialManagers.GetEnumerator();
while (iter.MoveNext())
iter.Current.Value.RefreshMaterials();
iter.Dispose();
}
}
private void DestroyMaterials()
{
if (_materialManagers != null && _materialManagers.Count > 0)
{
var iter = _materialManagers.GetEnumerator();
while (iter.MoveNext())
iter.Current.Value.DestroyMaterials();
iter.Dispose();
}
}
public void AddRef()
{
if (_root == null) //disposed
return;
if (_root != this && refCount == 0)
_root.AddRef();
refCount++;
}
public void ReleaseRef()
{
if (_root == null) //disposed
return;
refCount--;
if (refCount == 0)
{
if (_root != this)
_root.ReleaseRef();
if (onRelease != null)
onRelease(this);
}
}
/// <summary>
///
/// </summary>
public void Dispose()
{
if (this == _empty)
return;
if (_root == this)
Unload(true);
_root = null;
onSizeChanged = null;
onRelease = null;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: cdb420f5e4ef87d419c4467d1b8f2ddf
timeCreated: 1535374215
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,95 @@
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public static class ShaderConfig
{
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public delegate Shader GetFunction(string name);
/// <summary>
///
/// </summary>
public static GetFunction Get = Shader.Find;
/// <summary>
///
/// </summary>
public static string imageShader = "FairyGUI/Image";
/// <summary>
///
/// </summary>
public static string textShader = "FairyGUI/Text";
/// <summary>
///
/// </summary>
public static string bmFontShader = "FairyGUI/BMFont";
/// <summary>
///
/// </summary>
public static string TMPFontShader = "FairyGUI/TMP";
public static int ID_ClipBox;
public static int ID_ClipSoftness;
public static int ID_AlphaTex;
public static int ID_StencilComp;
public static int ID_Stencil;
public static int ID_StencilOp;
public static int ID_StencilReadMask;
public static int ID_ColorMask;
public static int ID_ColorMatrix;
public static int ID_ColorOffset;
public static int ID_BlendSrcFactor;
public static int ID_BlendDstFactor;
public static int ID_ColorOption;
public static int ID_Stencil2;
static ShaderConfig()
{
ID_ClipBox = Shader.PropertyToID("_ClipBox");
ID_ClipSoftness = Shader.PropertyToID("_ClipSoftness");
ID_AlphaTex = Shader.PropertyToID("_AlphaTex");
ID_StencilComp = Shader.PropertyToID("_StencilComp");
ID_Stencil = Shader.PropertyToID("_Stencil");
ID_StencilOp = Shader.PropertyToID("_StencilOp");
ID_StencilReadMask = Shader.PropertyToID("_StencilReadMask");
ID_ColorMask = Shader.PropertyToID("_ColorMask");
ID_ColorMatrix = Shader.PropertyToID("_ColorMatrix");
ID_ColorOffset = Shader.PropertyToID("_ColorOffset");
ID_BlendSrcFactor = Shader.PropertyToID("_BlendSrcFactor");
ID_BlendDstFactor = Shader.PropertyToID("_BlendDstFactor");
ID_ColorOption = Shader.PropertyToID("_ColorOption");
ID_Stencil2 = Shader.PropertyToID("_StencilRef");
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static Shader GetShader(string name)
{
Shader shader = Get(name);
if (shader == null)
{
Debug.LogWarning("FairyGUI: shader not found: " + name);
shader = Shader.Find("UI/Default");
}
shader.hideFlags = DisplayObject.hideFlags;
return shader;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: c4eae4be32683c04a96ac1c4cd1834ab
timeCreated: 1460480288
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,250 @@
using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
///
/// </summary>
public class Shape : DisplayObject
{
/// <summary>
///
/// </summary>
public Shape()
{
CreateGameObject("Shape");
graphics = new NGraphics(gameObject);
graphics.texture = NTexture.Empty;
graphics.meshFactory = null;
}
/// <summary>
///
/// </summary>
public Color color
{
get
{
return graphics.color;
}
set
{
graphics.color = value;
graphics.SetMeshDirty();
}
}
/// <summary>
///
/// </summary>
/// <param name="lineSize"></param>
/// <param name="lineColor"></param>
/// <param name="fillColor"></param>
public void DrawRect(float lineSize, Color lineColor, Color fillColor)
{
RectMesh mesh = graphics.GetMeshFactory<RectMesh>();
mesh.lineWidth = lineSize;
mesh.lineColor = lineColor;
mesh.fillColor = null;
mesh.colors = null;
graphics.color = fillColor;
graphics.SetMeshDirty();
}
/// <summary>
///
/// </summary>
/// <param name="lineSize"></param>
/// <param name="colors"></param>
public void DrawRect(float lineSize, Color32[] colors)
{
RectMesh mesh = graphics.GetMeshFactory<RectMesh>();
mesh.lineWidth = lineSize;
mesh.colors = colors;
graphics.SetMeshDirty();
}
/// <summary>
///
/// </summary>
/// <param name="lineSize"></param>
/// <param name="lineColor"></param>
/// <param name="fillColor"></param>
/// <param name="topLeftRadius"></param>
/// <param name="topRightRadius"></param>
/// <param name="bottomLeftRadius"></param>
/// <param name="bottomRightRadius"></param>
public void DrawRoundRect(float lineSize, Color lineColor, Color fillColor,
float topLeftRadius, float topRightRadius, float bottomLeftRadius, float bottomRightRadius)
{
RoundedRectMesh mesh = graphics.GetMeshFactory<RoundedRectMesh>();
mesh.lineWidth = lineSize;
mesh.lineColor = lineColor;
mesh.fillColor = null;
mesh.topLeftRadius = topLeftRadius;
mesh.topRightRadius = topRightRadius;
mesh.bottomLeftRadius = bottomLeftRadius;
mesh.bottomRightRadius = bottomRightRadius;
graphics.color = fillColor;
graphics.SetMeshDirty();
}
/// <summary>
///
/// </summary>
/// <param name="fillColor"></param>
public void DrawEllipse(Color fillColor)
{
EllipseMesh mesh = graphics.GetMeshFactory<EllipseMesh>();
mesh.lineWidth = 0;
mesh.startDegree = 0;
mesh.endDegreee = 360;
mesh.fillColor = null;
mesh.centerColor = null;
graphics.color = fillColor;
graphics.SetMeshDirty();
}
/// <summary>
///
/// </summary>
/// <param name="lineSize"></param>
/// <param name="centerColor"></param>
/// <param name="lineColor"></param>
/// <param name="fillColor"></param>
/// <param name="startDegree"></param>
/// <param name="endDegree"></param>
public void DrawEllipse(float lineSize, Color centerColor, Color lineColor, Color fillColor, float startDegree, float endDegree)
{
EllipseMesh mesh = graphics.GetMeshFactory<EllipseMesh>();
mesh.lineWidth = lineSize;
if (centerColor.Equals(fillColor))
mesh.centerColor = null;
else
mesh.centerColor = centerColor;
mesh.lineColor = lineColor;
mesh.fillColor = null;
mesh.startDegree = startDegree;
mesh.endDegreee = endDegree;
graphics.color = fillColor;
graphics.SetMeshDirty();
}
/// <summary>
///
/// </summary>
/// <param name="points"></param>
/// <param name="fillColor"></param>
public void DrawPolygon(IList<Vector2> points, Color fillColor)
{
PolygonMesh mesh = graphics.GetMeshFactory<PolygonMesh>();
mesh.points.Clear();
mesh.points.AddRange(points);
mesh.fillColor = null;
mesh.colors = null;
graphics.color = fillColor;
graphics.SetMeshDirty();
}
/// <summary>
///
/// </summary>
/// <param name="points"></param>
/// <param name="colors"></param>
public void DrawPolygon(IList<Vector2> points, Color32[] colors)
{
PolygonMesh mesh = graphics.GetMeshFactory<PolygonMesh>();
mesh.points.Clear();
mesh.points.AddRange(points);
mesh.fillColor = null;
mesh.colors = colors;
graphics.SetMeshDirty();
}
/// <summary>
///
/// </summary>
/// <param name="points"></param>
/// <param name="fillColor"></param>
/// <param name="lineSize"></param>
/// <param name="lineColor"></param>
public void DrawPolygon(IList<Vector2> points, Color fillColor, float lineSize, Color lineColor)
{
PolygonMesh mesh = graphics.GetMeshFactory<PolygonMesh>();
mesh.points.Clear();
mesh.points.AddRange(points);
mesh.fillColor = null;
mesh.lineWidth = lineSize;
mesh.lineColor = lineColor;
mesh.colors = null;
graphics.color = fillColor;
graphics.SetMeshDirty();
}
/// <summary>
///
/// </summary>
/// <param name="sides"></param>
/// <param name="lineSize"></param>
/// <param name="centerColor"></param>
/// <param name="lineColor"></param>
/// <param name="fillColor"></param>
/// <param name="rotation"></param>
/// <param name="distances"></param>
public void DrawRegularPolygon(int sides, float lineSize, Color centerColor, Color lineColor, Color fillColor, float rotation, float[] distances)
{
RegularPolygonMesh mesh = graphics.GetMeshFactory<RegularPolygonMesh>();
mesh.sides = sides;
mesh.lineWidth = lineSize;
mesh.centerColor = centerColor;
mesh.lineColor = lineColor;
mesh.fillColor = null;
mesh.rotation = rotation;
mesh.distances = distances;
graphics.color = fillColor;
graphics.SetMeshDirty();
}
/// <summary>
///
/// </summary>
public void Clear()
{
graphics.meshFactory = null;
}
/// <summary>
///
/// </summary>
public bool isEmpty
{
get { return graphics.meshFactory == null; }
}
protected override DisplayObject HitTest()
{
if (graphics.meshFactory == null)
return null;
Vector2 localPoint = WorldToLocal(HitTestContext.worldPoint, HitTestContext.direction);
IHitTest ht = graphics.meshFactory as IHitTest;
if (ht != null)
return ht.HitTest(_contentRect, localPoint) ? this : null;
else if (_contentRect.Contains(localPoint))
return this;
else
return null;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: cbcf11c269a33474aa9adaafd9867711
timeCreated: 1460480288
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 6650441f8140eae4c896682b7fd5b3e6
timeCreated: 1535374214
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,196 @@
using System;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
/// Stage Camera is an orthographic camera for UI rendering.
/// </summary>
[ExecuteInEditMode]
[AddComponentMenu("FairyGUI/UI Camera")]
public class StageCamera : MonoBehaviour
{
/// <summary>
///
/// </summary>
public bool constantSize = true;
/// <summary>
///
/// </summary>
[NonSerialized]
public float unitsPerPixel = 0.02f;
[NonSerialized]
public Transform cachedTransform;
[NonSerialized]
public Camera cachedCamera;
[NonSerialized]
int screenWidth;
[NonSerialized]
int screenHeight;
[NonSerialized]
bool isMain;
[NonSerialized]
Display _display;
/// <summary>
///
/// </summary>
[NonSerialized]
public static Camera main;
/// <summary>
///
/// </summary>
[NonSerialized]
public static int screenSizeVer = 1;
public const string Name = "Stage Camera";
public const string LayerName = "UI";
public static float DefaultCameraSize = 5;
public static float DefaultUnitsPerPixel = 0.02f;
void OnEnable()
{
cachedTransform = this.transform;
cachedCamera = this.GetComponent<Camera>();
if (this.gameObject.name == Name)
{
main = cachedCamera;
isMain = true;
}
if (Display.displays.Length > 1 && cachedCamera.targetDisplay != 0 && cachedCamera.targetDisplay < Display.displays.Length)
_display = Display.displays[cachedCamera.targetDisplay];
if (_display == null)
OnScreenSizeChanged(Screen.width, Screen.height);
else
OnScreenSizeChanged(_display.renderingWidth, _display.renderingHeight);
}
void Update()
{
if (_display == null)
{
if (screenWidth != Screen.width || screenHeight != Screen.height)
OnScreenSizeChanged(Screen.width, Screen.height);
}
else
{
if (screenWidth != _display.renderingWidth || screenHeight != _display.renderingHeight)
OnScreenSizeChanged(_display.renderingWidth, _display.renderingHeight);
}
}
void OnScreenSizeChanged(int newWidth, int newHeight)
{
if (newWidth == 0 || newHeight == 0)
return;
screenWidth = newWidth;
screenHeight = newHeight;
if (constantSize)
{
cachedCamera.orthographicSize = DefaultCameraSize;
unitsPerPixel = cachedCamera.orthographicSize * 2 / screenHeight;
}
else
{
unitsPerPixel = DefaultUnitsPerPixel;
cachedCamera.orthographicSize = screenHeight / 2 * unitsPerPixel;
}
cachedTransform.localPosition = new Vector3(cachedCamera.orthographicSize * screenWidth / screenHeight, -cachedCamera.orthographicSize);
if (isMain)
{
screenSizeVer++;
if (Application.isPlaying)
Stage.inst.HandleScreenSizeChanged(screenWidth, screenHeight, unitsPerPixel);
else
{
UIContentScaler scaler = GameObject.FindObjectOfType<UIContentScaler>();
if (scaler != null)
scaler.ApplyChange();
else
UIContentScaler.scaleFactor = 1;
}
}
}
void OnRenderObject()
{
//Update和OnGUI在EditMode的调用都不那么及时OnRenderObject则比较频繁可以保证界面及时刷新。所以使用OnRenderObject
if (isMain && !Application.isPlaying)
{
EMRenderSupport.Update();
}
}
public void ApplyModifiedProperties()
{
screenWidth = 0; //force OnScreenSizeChanged called in next update
}
/// <summary>
/// Check if there is a stage camera in the scene. If none, create one.
/// </summary>
public static void CheckMainCamera()
{
if (GameObject.Find(Name) == null)
{
int layer = LayerMask.NameToLayer(LayerName);
CreateCamera(Name, 1 << layer);
}
HitTestContext.cachedMainCamera = Camera.main;
}
/// <summary>
///
/// </summary>
public static void CheckCaptureCamera()
{
if (GameObject.Find(Name) == null)
{
int layer = LayerMask.NameToLayer(LayerName);
CreateCamera(Name, 1 << layer);
}
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="cullingMask"></param>
/// <returns></returns>
public static Camera CreateCamera(string name, int cullingMask)
{
GameObject cameraObject = new GameObject(name);
Camera camera = cameraObject.AddComponent<Camera>();
camera.depth = 1;
camera.cullingMask = cullingMask;
camera.clearFlags = CameraClearFlags.Depth;
camera.orthographic = true;
camera.orthographicSize = DefaultCameraSize;
camera.nearClipPlane = -30;
camera.farClipPlane = 30;
#if UNITY_5_4_OR_NEWER
camera.stereoTargetEye = StereoTargetEyeMask.None;
#endif
#if UNITY_5_6_OR_NEWER
camera.allowHDR = false;
camera.allowMSAA = false;
#endif
cameraObject.AddComponent<StageCamera>();
return camera;
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 367e80e3fa958344491a9a196a902b72
timeCreated: 1460480287
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Some files were not shown because too many files have changed in this diff Show More