Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Unity: Fixed loading additive shader, added LogError in the default l…

…oader
  • Loading branch information...
commit a4d8f60a06888db12763db015c1faa4db8b7ccd2 1 parent 0b7176b
@splhack splhack authored
View
BIN  csharp/unity/dist/lwf.unitypackage
Binary file not shown
View
13 csharp/unity/renderer/combinedmesh/lwf_combinedmesh_factory.cs
@@ -67,6 +67,7 @@ public partial class Factory : UnityRenderer.Factory
private string textureName;
private int updateCount;
private int bitmapCount;
+ private bool usingAdditiveShader;
public Factory(Data d, GameObject gObj,
float zOff = 0, float zR = 1, int rQOff = 0, bool uAC = false,
@@ -117,6 +118,18 @@ public override void Destruct()
base.Destruct();
}
+ public override void SetBlendMode(int m)
+ {
+ base.SetBlendMode(m);
+
+ if (m == (int)Format.Constant.BLEND_MODE_ADD && !usingAdditiveShader) {
+ usingAdditiveShader = true;
+ meshRenderer.sharedMaterial.shader =
+ ResourceCache.SharedInstance().GetAdditiveShader(
+ meshRenderer.sharedMaterial.shader);
+ }
+ }
+
public void AddBitmap()
{
++bitmapCount;
View
31 csharp/unity/renderer/common/lwf_unity_resourcecache.cs
@@ -21,6 +21,7 @@
using UnityEngine;
using System;
using System.Collections.Generic;
+using System.Text.RegularExpressions;
using LWFDataLoader = System.Func<string, byte[]>;
using TextureLoader = System.Func<string, UnityEngine.Texture2D>;
@@ -129,14 +130,22 @@ private ResourceCache()
if (m_lwfDataLoader == null) {
m_lwfDataLoader = (filename) => {
- TextAsset asset = (TextAsset)Resources.Load(filename, typeof(TextAsset));
+ TextAsset asset =
+ (TextAsset)Resources.Load(filename, typeof(TextAsset));
+ if (asset == null)
+ Debug.LogError(string.Format(
+ "Resources.Load can't load [{0}]", filename));
return asset.bytes;
};
}
if (m_textureLoader == null) {
m_textureLoader = (filename) => {
- return (Texture2D)Resources.Load(filename);
+ Texture2D texture = (Texture2D)Resources.Load(filename);
+ if (texture == null)
+ Debug.LogError(string.Format(
+ "Resources.Load can't load [{0}]", filename));
+ return texture;
};
}
@@ -238,6 +247,21 @@ public void UnloadTexture(string lwfName, string filename)
}
}
+ public Shader GetAdditiveShader(Shader shader)
+ {
+ string shaderName = shader.name;
+ Regex r = new Regex("Additive");
+ if (r.IsMatch(shaderName))
+ return shader;
+
+ r = new Regex("(Normal|PreMultipliedAlpha)");
+ shaderName = r.Replace(shaderName, (match) => {
+ string s = match.ToString();
+ return s + "Additive";
+ });
+ return GetShader(shaderName);
+ }
+
public MeshContext LoadMesh(string lwfName,
Data data, Format.BitmapEx bitmapEx, int bitmapExId)
{
@@ -343,6 +367,9 @@ public Shader GetShader(string filename)
Shader shader;
if (!m_shaderCache.TryGetValue(filename, out shader)) {
shader = Shader.Find(filename);
+ if (shader == null)
+ Debug.LogError(
+ string.Format("Shader.Find can't find [{0}]", filename));
m_shaderCache[filename] = shader;
}
return shader;
View
8 csharp/unity/renderer/drawmesh/lwf_drawmesh_bitmap.cs
@@ -19,6 +19,7 @@
*/
using UnityEngine;
+
using ResourceCache = LWF.UnityRenderer.ResourceCache;
using MeshContext = LWF.UnityRenderer.MeshContext;
@@ -193,12 +194,9 @@ public override void Destruct()
if (factory.blendMode == (int)Format.Constant.BLEND_MODE_ADD) {
if (m_additiveMaterial == null) {
m_additiveMaterial = new Material(m_context.material);
- string shaderName = m_context.material.shader.name;
- shaderName += "Additive";
- if (factory.useAdditionalColor)
- shaderName += "Additional";
m_additiveMaterial.shader =
- ResourceCache.SharedInstance().GetShader(shaderName);
+ ResourceCache.SharedInstance().GetAdditiveShader(
+ m_context.material.shader);
}
Graphics.DrawMesh(m_context.mesh, m_renderMatrix,
m_additiveMaterial, factory.gameObject.layer, factory.camera, 0,
Please sign in to comment.
Something went wrong with that request. Please try again.