fix shader properties initialization

This commit is contained in:
2017-08-06 16:44:51 +07:00
parent 08ac4cc73e
commit 5f987eeea8
2 changed files with 64 additions and 13 deletions

View File

@@ -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<T>
//
public static T GetComponent<T>(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 {

View File

@@ -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);
}