From 5f987eeea8806dfde0e237c6ae4866141660aa2f Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sun, 6 Aug 2017 16:44:51 +0700 Subject: [PATCH] fix shader properties initialization --- .../Scripts/FTRuntime/Internal/SwfUtils.cs | 60 ++++++++++++++++++- .../FlashTools/Scripts/FTRuntime/SwfClip.cs | 17 ++---- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/Assets/FlashTools/Scripts/FTRuntime/Internal/SwfUtils.cs b/Assets/FlashTools/Scripts/FTRuntime/Internal/SwfUtils.cs index b567899..df65a6e 100644 --- a/Assets/FlashTools/Scripts/FTRuntime/Internal/SwfUtils.cs +++ b/Assets/FlashTools/Scripts/FTRuntime/Internal/SwfUtils.cs @@ -4,6 +4,10 @@ using System.Collections.Generic; namespace FTRuntime.Internal { static class SwfUtils { + // + // UnpackUV/UnpackFColorFromUInts + // + const ushort UShortMax = ushort.MaxValue; const float FColorPrecision = 1.0f / 512.0f; const float InvFColorPrecision = 1.0f / FColorPrecision; @@ -24,7 +28,39 @@ namespace FTRuntime.Internal { } // + // Shader properties // + + public static int TintShaderProp { + get { + ShaderPropsCache.LazyInitialize(); + return ShaderPropsCache.TintShaderProp; + } + } + + public static int MainTexShaderProp { + get { + ShaderPropsCache.LazyInitialize(); + return ShaderPropsCache.MainTexShaderProp; + } + } + + public static int AlphaTexShaderProp { + get { + ShaderPropsCache.LazyInitialize(); + return ShaderPropsCache.AlphaTexShaderProp; + } + } + + public static int ExternalAlphaShaderProp { + get { + ShaderPropsCache.LazyInitialize(); + return ShaderPropsCache.ExternalAlphaShaderProp; + } + } + + // + // GetComponent // public static T GetComponent(GameObject obj, bool allow_create) where T : Component { @@ -36,7 +72,7 @@ namespace FTRuntime.Internal { } // - // + // FillGeneratedMesh // public static void FillGeneratedMesh(Mesh mesh, SwfClipAsset.MeshData mesh_data) { @@ -64,8 +100,28 @@ namespace FTRuntime.Internal { } } + // ShaderPropsCache + + static class ShaderPropsCache { + public static int TintShaderProp = 0; + public static int MainTexShaderProp = 0; + public static int AlphaTexShaderProp = 0; + public static int ExternalAlphaShaderProp = 0; + + static bool _initialized = false; + public static void LazyInitialize() { + if ( !_initialized ) { + _initialized = true; + TintShaderProp = Shader.PropertyToID("_Tint"); + MainTexShaderProp = Shader.PropertyToID("_MainTex"); + AlphaTexShaderProp = Shader.PropertyToID("_AlphaTex"); + ExternalAlphaShaderProp = Shader.PropertyToID("_ExternalAlpha"); + } + } + } + // - // + // GeneratedMeshCache // static class GeneratedMeshCache { diff --git a/Assets/FlashTools/Scripts/FTRuntime/SwfClip.cs b/Assets/FlashTools/Scripts/FTRuntime/SwfClip.cs index 48af064..863bd17 100644 --- a/Assets/FlashTools/Scripts/FTRuntime/SwfClip.cs +++ b/Assets/FlashTools/Scripts/FTRuntime/SwfClip.cs @@ -21,11 +21,6 @@ namespace FTRuntime { SwfClipAsset.Sequence _curSequence = null; MaterialPropertyBlock _curPropBlock = null; - static readonly int _tintShaderProp = Shader.PropertyToID("_Tint"); - static readonly int _mainTexShaderProp = Shader.PropertyToID("_MainTex"); - static readonly int _alphaTexShaderProp = Shader.PropertyToID("_AlphaTex"); - static readonly int _externalAlphaShaderProp = Shader.PropertyToID("_ExternalAlpha"); - // --------------------------------------------------------------------- // // Events @@ -394,19 +389,19 @@ namespace FTRuntime { _curPropBlock = new MaterialPropertyBlock(); } _meshRenderer.GetPropertyBlock(_curPropBlock); - _curPropBlock.SetColor(_tintShaderProp, tint); + _curPropBlock.SetColor(SwfUtils.TintShaderProp, tint); var sprite = clip ? clip.Sprite : null; var atlas = sprite && sprite.texture ? sprite.texture : Texture2D.whiteTexture; var atlasA = sprite ? sprite.associatedAlphaSplitTexture : null; _curPropBlock.SetTexture( - _mainTexShaderProp, + SwfUtils.MainTexShaderProp, atlas ? atlas : Texture2D.whiteTexture); if ( atlasA ) { - _curPropBlock.SetTexture(_alphaTexShaderProp, atlasA); - _curPropBlock.SetFloat(_externalAlphaShaderProp, 1.0f); + _curPropBlock.SetTexture(SwfUtils.AlphaTexShaderProp, atlasA); + _curPropBlock.SetFloat(SwfUtils.ExternalAlphaShaderProp, 1.0f); } else { - _curPropBlock.SetTexture(_alphaTexShaderProp, Texture2D.whiteTexture); - _curPropBlock.SetFloat(_externalAlphaShaderProp, 0.0f); + _curPropBlock.SetTexture(SwfUtils.AlphaTexShaderProp, Texture2D.whiteTexture); + _curPropBlock.SetFloat(SwfUtils.ExternalAlphaShaderProp, 0.0f); } _meshRenderer.SetPropertyBlock(_curPropBlock); }