Skip to content

Instantly share code, notes, and snippets.

@splhack
Created June 13, 2014 21:04
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/db2da47cb279f9dba229 to your computer and use it in GitHub Desktop.
Save splhack/db2da47cb279f9dba229 to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2014 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.
*/
#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2
#else
using UnityEngine;
using ResourceCache = LWF.UnityRenderer.ResourceCache;
namespace LWF {
namespace SpriteRenderer {
public partial class Factory : IRendererFactory
{
private BitmapContext[] m_bitmapContexts;
private BitmapContext[] m_bitmapExContexts;
private void CreateBitmapContexts(Data data)
{
m_bitmapContexts = new BitmapContext[data.bitmaps.Length];
for (int i = 0; i < data.bitmaps.Length; ++i) {
Format.Bitmap bitmap = data.bitmaps[i];
// Ignore null texture
if (bitmap.textureFragmentId == -1)
continue;
Format.BitmapEx bitmapEx = new Format.BitmapEx();
bitmapEx.matrixId = bitmap.matrixId;
bitmapEx.textureFragmentId = bitmap.textureFragmentId;
bitmapEx.u = 0;
bitmapEx.v = 0;
bitmapEx.w = 1;
bitmapEx.h = 1;
m_bitmapContexts[i] = new BitmapContext(this, data, bitmapEx);
}
m_bitmapExContexts = new BitmapContext[data.bitmapExs.Length];
for (int i = 0; i < data.bitmapExs.Length; ++i) {
Format.BitmapEx bitmapEx = data.bitmapExs[i];
// Ignore null texture
if (bitmapEx.textureFragmentId == -1)
continue;
m_bitmapExContexts[i] = new BitmapContext(this, data, bitmapEx);
}
}
public override void Destruct()
{
for (int i = 0; i < m_bitmapContexts.Length; ++i)
if (m_bitmapContexts[i] != null)
m_bitmapContexts[i].Destruct();
for (int i = 0; i < m_bitmapExContexts.Length; ++i)
if (m_bitmapExContexts[i] != null)
m_bitmapExContexts[i].Destruct();
base.Destruct();
}
}
public class BitmapContext
{
private Factory m_factory;
private Material m_material;
private Sprite m_sprite;
private Data m_data;
private float m_height;
private string m_textureName;
private bool m_premultipliedAlpha;
public Factory factory {get {return m_factory;}}
public Material material {get {return m_material;}}
public Sprite sprite {get {return m_sprite;}}
public float height {get {return m_height;}}
public string textureName {get {return m_textureName;}}
public bool premultipliedAlpha {get {return m_premultipliedAlpha;}}
public BitmapContext(Factory factory, Data data, Format.BitmapEx bitmapEx)
{
m_factory = factory;
m_data = data;
Format.TextureFragment fragment =
data.textureFragments[bitmapEx.textureFragmentId];
Format.Texture texture = data.textures[fragment.textureId];
m_textureName = factory.texturePrefix + texture.filename;
m_premultipliedAlpha = (texture.format ==
(int)Format.Constant.TEXTUREFORMAT_PREMULTIPLIEDALPHA);
m_material = ResourceCache.SharedInstance().LoadTexture(
data.name, m_textureName, texture.format, false,
factory.useAdditionalColor, factory.textureLoader,
factory.textureUnloader);
if (factory.renderQueueOffset != 0)
m_material.renderQueue += factory.renderQueueOffset;
float tw = (float)texture.width;
float th = (float)texture.height;
float x = (float)fragment.x;
float y = - (float)fragment.y;
float u = (float)fragment.u;
float v = th - (float)fragment.v;
float w = (float)fragment.w;
float h = (float)fragment.h;
float bu = bitmapEx.u * w;
float bv = bitmapEx.v * h;
float bw = bitmapEx.w;
float bh = bitmapEx.h;
x += bu;
y += bv;
u += bu;
v += bv;
w *= bw;
h *= bh;
m_height = h / texture.scale;
float x0 = x / texture.scale;
//float y0 = y / texture.scale;
float x1 = (x + w) / texture.scale;
//float y1 = (y + h) / texture.scale;
Rect rect;
if (fragment.rotated == 0) {
float u0 = u;
float v0 = (v - h);
float u1 = (u + w);
float v1 = v;
rect = new Rect(u0, v0, u1 - u0, v1 - v0);
} else {
float u0 = u;
float v0 = (v - w);
float u1 = (u + h);
float v1 = v;
rect = new Rect(u0, v0, u1 - u0, v1 - v0);
}
m_sprite = Sprite.Create((Texture2D)m_material.mainTexture, rect,
Vector2.zero, (x1 - x0) / rect.width);
}
public void Destruct()
{
ResourceCache.SharedInstance().UnloadTexture(
m_data.name, m_textureName);
Sprite.Destroy(m_sprite);
}
}
public class BitmapRenderer : Renderer
{
BitmapContext m_context;
GameObject m_gameObject;
UnityEngine.SpriteRenderer m_renderer;
Matrix4x4 m_matrix;
UnityEngine.Color m_colorMult;
UnityEngine.Color m_colorAdd;
public BitmapRenderer(LWF lwf, BitmapContext context) : base(lwf)
{
m_context = context;
// Ignore null texture
if (m_context == null)
return;
m_gameObject = new GameObject(m_context.textureName);
m_renderer = m_gameObject.AddComponent<UnityEngine.SpriteRenderer>();
m_renderer.sprite = m_context.sprite;
m_renderer.sharedMaterial = context.material;
m_gameObject.transform.parent = context.factory.gameObject.transform;
m_matrix = new Matrix4x4();
m_colorMult = new UnityEngine.Color();
m_colorAdd = new UnityEngine.Color();
}
public override void Destruct()
{
if (m_context == null)
return;
if (Application.isEditor)
GameObject.DestroyImmediate(m_gameObject);
else
GameObject.Destroy(m_gameObject);
}
public override void Render(Matrix matrix, ColorTransform colorTransform,
int renderingIndex, int renderingCount, bool visible)
{
// Ignore null texture
if (m_context == null || !visible) {
if (m_gameObject != null)
m_gameObject.SetActive(false);
return;
}
Factory factory = m_context.factory;
factory.ConvertColorTransform(
ref m_colorMult, ref m_colorAdd, colorTransform);
if (m_colorMult.a <= 0) {
m_gameObject.SetActive(false);
return;
}
if (m_context.premultipliedAlpha) {
m_colorMult.r *= m_colorMult.a;
m_colorMult.g *= m_colorMult.a;
m_colorMult.b *= m_colorMult.a;
}
m_renderer.color = m_colorMult;
factory.ConvertMatrix(ref m_matrix, matrix,
renderingCount - renderingIndex, m_context.height);
m_gameObject.SetActive(true);
factory.ApplyMatrix(m_gameObject, ref m_matrix);
}
}
} // namespace SpriteRenderer
} // namespace LWF
#endif
/*
* Copyright (C) 2013 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.
*/
#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2
#else
using UnityEngine;
using TextureLoader = System.Func<string, UnityEngine.Texture2D>;
using TextureUnloader = System.Action<UnityEngine.Texture2D>;
namespace LWF {
namespace SpriteRenderer {
public partial class Factory : UnityRenderer.Factory
{
Vector3 vector;
Quaternion quaternion;
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 = "",
TextureLoader textureLdr = null,
TextureUnloader textureUnldr = null)
: base(gObj, zOff, zR, rQOff, uAC,
cam, texturePrfx, fontPrfx, textureLdr, textureUnldr)
{
CreateBitmapContexts(data);
vector = new Vector3();
quaternion = Quaternion.identity;
}
public override Renderer ConstructBitmap(LWF lwf,
int objectId, Bitmap bitmap)
{
return new BitmapRenderer(lwf, m_bitmapContexts[objectId]);
}
public override Renderer ConstructBitmapEx(LWF lwf,
int objectId, BitmapEx bitmapEx)
{
return new BitmapRenderer(lwf, m_bitmapExContexts[objectId]);
}
public override TextRenderer ConstructText(LWF lwf, int objectId, Text text)
{
return new UnityRenderer.UnityTextRenderer(lwf, objectId);
}
public void ApplyMatrix(GameObject gObj, ref Matrix4x4 m)
{
Transform transform = gObj.transform;
Vector3 v = vector;
v.x = m[0,3];
v.y = m[1,3];
v.z = m[2,3];
transform.localPosition = v;
Quaternion q = quaternion;
float tr = m[0,0] + m[1,1] + m[2,2];
if (tr > 0) {
float s = Mathf.Sqrt(tr + 1) * 2;
if (s != 0) {
q.w = 0.25f * s;
q.x = (m[2,1] - m[1,2]) / s;
q.y = (m[0,2] - m[2,0]) / s;
q.z = (m[1,0] - m[0,1]) / s;
}
} else if ((m[0,0] > m[1,1]) && (m[0,0] > m[2,2])) {
float s =
Mathf.Sqrt(Mathf.Max(0, 1 + m[0,0] - m[1,1] - m[2,2])) * 2;
if (s != 0) {
q.w = (m[2,1] - m[1,2]) / s;
q.x = 0.25f * s;
q.y = (m[0,1] + m[1,0]) / s;
q.z = (m[0,2] + m[2,0]) / s;
}
} else if (m[1,1] > m[2,2]) {
float s =
Mathf.Sqrt(Mathf.Max(0, 1 + m[1,1] - m[0,0] - m[2,2])) * 2;
if (s != 0) {
q.w = (m[0,2] - m[2,0]) / s;
q.x = (m[0,1] + m[1,0]) / s;
q.y = 0.25f * s;
q.z = (m[1,2] + m[2,1]) / s;
}
} else {
float s =
Mathf.Sqrt(Mathf.Max(0, 1 + m[2,2] - m[0,0] - m[1,1])) * 2;
if (s != 0) {
q.w = (m[1,0] - m[0,1]) / s;
q.x = (m[0,2] + m[2,0]) / s;
q.y = (m[1,2] + m[2,1]) / s;
q.z = 0.25f * s;
}
}
transform.localRotation = q;
v.x = m.GetColumn(0).magnitude * Mathf.Sign(m[0,0]);
v.y = m.GetColumn(1).magnitude * Mathf.Sign(m[1,1]);
v.z = m.GetColumn(2).magnitude * Mathf.Sign(m[2,2]);
transform.localScale = v;
}
}
} // namespace SpriteRenderer
} // namespace LWF
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment