Skip to content

Instantly share code, notes, and snippets.

@splhack
Created June 11, 2014 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save splhack/992233254922771ee748 to your computer and use it in GitHub Desktop.
Save splhack/992233254922771ee748 to your computer and use it in GitHub Desktop.
diff --git a/csharp/unity/renderer/drawmesh/lwf_drawmesh_bitmap.cs b/csharp/unity/renderer/drawmesh/lwf_drawmesh_bitmap.cs
index a63e0ef..3fabd77 100644
--- a/csharp/unity/renderer/drawmesh/lwf_drawmesh_bitmap.cs
+++ b/csharp/unity/renderer/drawmesh/lwf_drawmesh_bitmap.cs
@@ -61,7 +61,7 @@ public partial class Factory : IRendererFactory
}
}
- public override void Destruct()
+ public void DestructBitmapContexts()
{
for (int i = 0; i < m_bitmapContexts.Length; ++i)
if (m_bitmapContexts[i] != null)
@@ -69,7 +69,6 @@ public partial class Factory : IRendererFactory
for (int i = 0; i < m_bitmapExContexts.Length; ++i)
if (m_bitmapExContexts[i] != null)
m_bitmapExContexts[i].Destruct();
- base.Destruct();
}
}
diff --git a/csharp/unity/renderer/drawmesh/lwf_drawmesh_factory.cs b/csharp/unity/renderer/drawmesh/lwf_drawmesh_factory.cs
index f8bb279..8e74eaf 100644
--- a/csharp/unity/renderer/drawmesh/lwf_drawmesh_factory.cs
+++ b/csharp/unity/renderer/drawmesh/lwf_drawmesh_factory.cs
@@ -20,6 +20,8 @@
using UnityEngine;
+using Constant = LWF.Format.Constant;
+using ResourceCache = LWF.UnityRenderer.ResourceCache;
using TextureLoader = System.Func<string, UnityEngine.Texture2D>;
using TextureUnloader = System.Action<UnityEngine.Texture2D>;
@@ -28,6 +30,14 @@ namespace DrawMeshRenderer {
public partial class Factory : UnityRenderer.Factory
{
+ private RenderTexture currentTargetTexture;
+ private RenderTexture maskTexture;
+ private RenderTexture layerTexture;
+ private Material maskMaterial;
+ private Material eraseMaterial;
+ private Material blitMaterial;
+ private int masked;
+
public Factory(Data data, GameObject gObj,
float zOff = 0, float zR = 1, int rQOff = 0, bool uAC = false,
Camera cam = null, string texturePrfx = "", string fontPrfx = "",
@@ -37,6 +47,29 @@ public partial class Factory : UnityRenderer.Factory
cam, texturePrfx, fontPrfx, textureLdr, textureUnldr)
{
CreateBitmapContexts(data);
+
+ maskMaterial = new Material(
+ ResourceCache.SharedInstance().GetShader("LWF/Mask"));
+ eraseMaterial = new Material(
+ ResourceCache.SharedInstance().GetShader("LWF/Erase"));
+ blitMaterial = new Material(
+ ResourceCache.SharedInstance().GetShader("LWF/Blit"));
+ }
+
+ public override void Destruct()
+ {
+ DeleteMask();
+ if (!Application.isEditor) {
+ Material.Destroy(blitMaterial);
+ Material.Destroy(eraseMaterial);
+ Material.Destroy(maskMaterial);
+ }
+ blitMaterial = null;
+ eraseMaterial = null;
+ maskMaterial = null;
+
+ DestructBitmapContexts();
+ base.Destruct();
}
public override Renderer ConstructBitmap(LWF lwf,
@@ -55,6 +88,103 @@ public partial class Factory : UnityRenderer.Factory
{
return new UnityRenderer.UnityTextRenderer(lwf, objectId);
}
+
+ public override void BeginRender(LWF lwf)
+ {
+ base.BeginRender(lwf);
+
+ maskMode = (int)Constant.BLEND_MODE_NORMAL;
+ masked = (int)Constant.BLEND_MODE_NORMAL;
+ }
+
+ public override void EndRender(LWF lwf)
+ {
+ base.EndRender(lwf);
+
+ if (maskMode != (int)Constant.BLEND_MODE_NORMAL) {
+ if (maskMode == (int)Constant.BLEND_MODE_LAYER &&
+ masked != (int)Constant.BLEND_MODE_NORMAL) {
+ RenderMask();
+ } else {
+ ResetMask();
+ }
+ }
+ }
+
+ private void GenerateMask()
+ {
+ if (maskTexture != null)
+ return;
+
+ int w = (int)camera.pixelWidth;
+ int h = (int)camera.pixelHeight;
+
+ maskTexture = RenderTexture.GetTemporary(w, h);
+ layerTexture = RenderTexture.GetTemporary(w, h);
+
+ currentTargetTexture = camera.targetTexture;
+ }
+
+ private void DeleteMask()
+ {
+ if (maskTexture == null)
+ return;
+
+ RenderTexture.ReleaseTemporary(maskTexture);
+ RenderTexture.ReleaseTemporary(layerTexture);
+ maskTexture = null;
+ layerTexture = null;
+ currentTargetTexture = null;
+ }
+
+ private void RenderMask()
+ {
+ Graphics.SetRenderTarget(currentTargetTexture);
+ Graphics.Blit(layerTexture, maskTexture,
+ masked == (int)Constant.BLEND_MODE_MASK ?
+ maskMaterial : eraseMaterial);
+ Graphics.Blit(maskTexture, currentTargetTexture, blitMaterial);
+ }
+
+ private void ResetMask()
+ {
+ Graphics.SetRenderTarget(currentTargetTexture);
+ }
+
+ public override void SetMaskMode(int m)
+ {
+ GenerateMask();
+ switch (m) {
+ case (int)Constant.BLEND_MODE_ERASE:
+ case (int)Constant.BLEND_MODE_MASK:
+ if (maskMode == (int)Constant.BLEND_MODE_LAYER &&
+ masked != (int)Constant.BLEND_MODE_NORMAL) {
+ RenderMask();
+ }
+ masked = m;
+ Graphics.SetRenderTarget(maskTexture);
+ break;
+
+ case (int)Constant.BLEND_MODE_LAYER:
+ if (masked != (int)Constant.BLEND_MODE_NORMAL) {
+ Graphics.SetRenderTarget(layerTexture);
+ } else {
+ ResetMask();
+ }
+ break;
+
+ default:
+ if (maskMode == (int)Constant.BLEND_MODE_LAYER &&
+ masked != (int)Constant.BLEND_MODE_NORMAL) {
+ RenderMask();
+ } else {
+ ResetMask();
+ }
+ break;
+ }
+
+ base.SetMaskMode(m);
+ }
}
} // namespace DrawMeshRenderer
diff --git a/csharp/unity/shaders/LWFBlit.shader b/csharp/unity/shaders/LWFBlit.shader
new file mode 100644
index 0000000..9985f2e
--- /dev/null
+++ b/csharp/unity/shaders/LWFBlit.shader
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2012 GREE, Inc.
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
+Shader "LWF/Blit" {
+ Properties {
+ _MainTex ("Texture", 2D) = "white" {}
+ }
+
+ SubShader {
+ Tags {
+ "Queue" = "Transparent"
+ "IgnoreProjector" = "True"
+ }
+ Cull Off
+ ZWrite Off
+ Blend One OneMinusSrcAlpha
+ Pass {
+ CGPROGRAM
+ #pragma vertex vert
+ #pragma fragment frag
+ #include "UnityCG.cginc"
+ sampler2D _MainTex;
+ half4 _MainTex_ST;
+ struct v2f {
+ half4 pos: SV_POSITION;
+ half2 uv: TEXCOORD0;
+ fixed4 color: COLOR;
+ };
+ v2f vert(appdata_full v)
+ {
+ v2f o;
+ o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
+ o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
+ o.color = v.color;
+ return o;
+ }
+ fixed4 frag(v2f i): COLOR
+ {
+ return tex2D(_MainTex, i.uv.xy) * i.color;
+ }
+ ENDCG
+ }
+ }
+}
diff --git a/csharp/unity/shaders/LWFErase.shader b/csharp/unity/shaders/LWFErase.shader
new file mode 100644
index 0000000..0ef5a6e
--- /dev/null
+++ b/csharp/unity/shaders/LWFErase.shader
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2012 GREE, Inc.
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
+Shader "LWF/Erase" {
+ Properties {
+ _MainTex ("Texture", 2D) = "white" {}
+ }
+
+ SubShader {
+ Tags {
+ "Queue" = "Transparent"
+ "IgnoreProjector" = "True"
+ }
+ Cull Off
+ ZWrite Off
+ Blend OneMinusDstAlpha Zero
+ Pass {
+ CGPROGRAM
+ #pragma vertex vert
+ #pragma fragment frag
+ #include "UnityCG.cginc"
+ sampler2D _MainTex;
+ half4 _MainTex_ST;
+ struct v2f {
+ half4 pos: SV_POSITION;
+ half2 uv: TEXCOORD0;
+ fixed4 color: COLOR;
+ };
+ v2f vert(appdata_full v)
+ {
+ v2f o;
+ o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
+ o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
+ o.color = v.color;
+ return o;
+ }
+ fixed4 frag(v2f i): COLOR
+ {
+ return tex2D(_MainTex, i.uv.xy) * i.color;
+ }
+ ENDCG
+ }
+ }
+}
diff --git a/csharp/unity/shaders/LWFMask.shader b/csharp/unity/shaders/LWFMask.shader
new file mode 100644
index 0000000..f143978
--- /dev/null
+++ b/csharp/unity/shaders/LWFMask.shader
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2012 GREE, Inc.
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
+Shader "LWF/Mask" {
+ Properties {
+ _MainTex ("Texture", 2D) = "white" {}
+ }
+
+ SubShader {
+ Tags {
+ "Queue" = "Transparent"
+ "IgnoreProjector" = "True"
+ }
+ Cull Off
+ ZWrite Off
+ Blend DstAlpha Zero
+ Pass {
+ CGPROGRAM
+ #pragma vertex vert
+ #pragma fragment frag
+ #include "UnityCG.cginc"
+ sampler2D _MainTex;
+ half4 _MainTex_ST;
+ struct v2f {
+ half4 pos: SV_POSITION;
+ half2 uv: TEXCOORD0;
+ fixed4 color: COLOR;
+ };
+ v2f vert(appdata_full v)
+ {
+ v2f o;
+ o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
+ o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
+ o.color = v.color;
+ return o;
+ }
+ fixed4 frag(v2f i): COLOR
+ {
+ return tex2D(_MainTex, i.uv.xy) * i.color;
+ }
+ ENDCG
+ }
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment