Skip to content

Commit

Permalink
add UGUI_ETC1_Alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoqingqing committed Sep 2, 2016
1 parent 397cf4f commit 140ec6f
Show file tree
Hide file tree
Showing 63 changed files with 1,117 additions and 0 deletions.
5 changes: 5 additions & 0 deletions unity_ugui/UGUI_ETC1_Alpha/Assets/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

178 changes: 178 additions & 0 deletions unity_ugui/UGUI_ETC1_Alpha/Assets/Editor/Tool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
using System.IO;
using UnityEditor;
using UnityEngine;
public class Encoder {
static bool MakeTextureAnAtlas(string path, bool force, bool alphaTransparency)
{
if (string.IsNullOrEmpty(path)) return false;
TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;
if (ti == null) return false;

TextureImporterSettings settings = new TextureImporterSettings();
ti.ReadTextureSettings(settings);

if (force ||
settings.readable ||
settings.maxTextureSize < 4096 ||
settings.wrapMode != TextureWrapMode.Clamp ||
settings.npotScale != TextureImporterNPOTScale.ToNearest)
{
settings.readable = false;
settings.maxTextureSize = 4096;
settings.wrapMode = TextureWrapMode.Clamp;
settings.npotScale = TextureImporterNPOTScale.None;

settings.textureFormat = TextureImporterFormat.ARGB32;
settings.filterMode = FilterMode.Trilinear;

settings.aniso = 4;
settings.alphaIsTransparency = alphaTransparency;
ti.SetTextureSettings(settings);
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
}
return true;
}

static public bool MakeTextureReadable(string path, bool force)
{
if (string.IsNullOrEmpty(path)) return false;
TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;
if (ti == null) return false;

TextureImporterSettings settings = new TextureImporterSettings();
ti.ReadTextureSettings(settings);

if (force || !settings.readable || settings.npotScale != TextureImporterNPOTScale.None || settings.alphaIsTransparency)
{
settings.readable = true;
settings.textureFormat = TextureImporterFormat.AutomaticTruecolor;
settings.npotScale = TextureImporterNPOTScale.None;
settings.alphaIsTransparency = false;
ti.SetTextureSettings(settings);
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
}
return true;
}

static public Texture2D ImportTexture(string path, bool forInput, bool force, bool alphaTransparency)
{
if (!string.IsNullOrEmpty(path))
{
if (forInput) { if (!MakeTextureReadable(path, force)) return null; }
else if (!MakeTextureAnAtlas(path, force, alphaTransparency)) return null;
//return AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;

Texture2D tex = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;
AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
return tex;
}
return null;
}

static void CreateAlphaMask(Texture2D tex)
{
string oldPath = AssetDatabase.GetAssetPath(tex.GetInstanceID());
tex = ImportTexture(oldPath, true, false, true);

//Texture2D rgbTex = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false);
Texture2D aTex = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false);

Color newColor = new Color(0f, 0f, 0f);
for (int i = 0; i < tex.width; ++i) {
for (int j = 0; j < tex.height; ++j) {
newColor = tex.GetPixel(i, j);
//rgbTex.SetPixel(i, j, newColor);

newColor.r = newColor.a;
newColor.g = 0;
newColor.b = 0;
aTex.SetPixel(i, j, newColor);
}
}
string path = oldPath.Substring(0, oldPath.LastIndexOf("."));

byte[] bytes = aTex.EncodeToPNG();
File.WriteAllBytes(path + "_ETC1_A.png", bytes);

bytes = null;
aTex = null;

AssetDatabase.Refresh();
tex = ImportTexture(oldPath, false, false, true);
}

static void CreateMaterial(string materialPath) {
string pngPath = materialPath.Replace(AppPath, "Assets");
Texture2D pngSprite = AssetDatabase.LoadAssetAtPath<Texture2D>(pngPath);

string apngPath = pngPath.Replace(".png", "_ETC1_A.png");
Texture2D maskSprite = AssetDatabase.LoadAssetAtPath<Texture2D>(apngPath);

materialPath = materialPath.Replace(".png", ".mat");

string path = Application.dataPath + "/Material/SpriteMaterial.mat";
string name = Path.GetFileNameWithoutExtension(materialPath);
File.Copy(path, materialPath, true);
AssetDatabase.Refresh();

string matPath = materialPath.Replace(AppPath, "Assets");
Material material = AssetDatabase.LoadAssetAtPath<Material>(matPath);

material.SetTexture("_MainTex", pngSprite);
material.SetTexture("_AlphaTex", maskSprite);
}

/// <summary>
/// ѹËõÎÆÀí
/// </summary>
static void CompressTexture(string path) {
string aPath = path.Replace(".png", "_ETC1_A.png");
string[] paths = { path, aPath };
foreach (var newPath in paths) {
TextureImporter ti = AssetImporter.GetAtPath(newPath) as TextureImporter;
if (ti == null) return;

bool isMask = newPath.Contains("_ETC1_A");

TextureImporterSettings settings = new TextureImporterSettings();
ti.ReadTextureSettings(settings);

settings.readable = false;
settings.mipmapEnabled = false;
settings.maxTextureSize = 1024;
settings.wrapMode = TextureWrapMode.Clamp;
settings.npotScale = TextureImporterNPOTScale.None;

settings.textureFormat = TextureImporterFormat.ETC_RGB4;
settings.filterMode = FilterMode.Trilinear;
settings.spriteMode = (int)(isMask ? SpriteImportMode.None : SpriteImportMode.Multiple);

settings.aniso = 4;
settings.alphaIsTransparency = true;
ti.SetTextureSettings(settings);
AssetDatabase.ImportAsset(newPath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
}
}

/// <summary>
/// ´´½¨
/// </summary>
[MenuItem("Assets/Create ETC1 Mask")]
static void CreateEtc1Mask() {
Texture2D tex = Selection.activeObject as Texture2D;
if (null == tex) return;

UnityEngine.Object obj = Selection.activeObject;
var file = AssetDatabase.GetAssetPath(obj);
if (!file.EndsWith(".png")) return;

CreateAlphaMask(tex);
CreateMaterial(AppPath.Replace("Assets", "") + file);
CompressTexture(file);
AssetDatabase.Refresh();
}

static string AppPath {
get { return Application.dataPath; }
}
}
8 changes: 8 additions & 0 deletions unity_ugui/UGUI_ETC1_Alpha/Assets/Editor/Tool.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions unity_ugui/UGUI_ETC1_Alpha/Assets/Material.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions unity_ugui/UGUI_ETC1_Alpha/Assets/MonoVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using UnityEngine;
using System;
using System.Reflection;

public class MonoVersion : MonoBehaviour {
void Start()
{
Type type = Type.GetType("Mono.Runtime");
if (type != null)
{
MethodInfo info = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);

if (info != null)
Debug.Log(info.Invoke(null, null));
}
//Debug.Log(System.Enviroment.Version);//net version?
}
}
12 changes: 12 additions & 0 deletions unity_ugui/UGUI_ETC1_Alpha/Assets/MonoVersion.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions unity_ugui/UGUI_ETC1_Alpha/Assets/Shaders.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 115 additions & 0 deletions unity_ugui/UGUI_ETC1_Alpha/Assets/Shaders/UI-Default.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
Shader "UI/Default(Public)"
{
Properties
{
_MainTex ("Sprite Texture", 2D) = "white" {}
_AlphaTex ("Alpha Texture", 2D) = "white" {}

_Color ("Tint", Color) = (1,1,1,1)

_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255

_ColorMask ("Color Mask", Float) = 15
}

SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}

Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}

Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"
#include "UnityUI.cginc"

struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};

struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
};

fixed4 _Color;
fixed4 _TextureSampleAdd;

bool _UseClipRect;
float4 _ClipRect;

bool _UseAlphaClip;

v2f vert(appdata_t IN)
{
v2f OUT;
OUT.worldPosition = IN.vertex;
OUT.vertex = mul(UNITY_MATRIX_MVP, OUT.worldPosition);

OUT.texcoord = IN.texcoord;

#ifdef UNITY_HALF_TEXEL_OFFSET
OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
#endif

OUT.color = IN.color * _Color;
return OUT;
}

sampler2D _MainTex;
sampler2D _AlphaTex;

fixed4 frag(v2f IN) : SV_Target
{
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
fixed4 alpha = tex2D(_AlphaTex, IN.texcoord);

if (_UseClipRect)
color *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);

if (_UseAlphaClip)
clip (color.a - 0.001);

color.a = color.a * alpha.r;

return color;
}
ENDCG
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions unity_ugui/UGUI_ETC1_Alpha/Assets/Shared.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
8 changes: 8 additions & 0 deletions unity_ugui/UGUI_ETC1_Alpha/Assets/Shared/Shared.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 140ec6f

Please sign in to comment.