how to combine jpg + separate alpha in png?
http://www.tasharen.com/forum/index.php?topic=4018.msg19784#msg19784
I have tons of large sprites, I need to reduce the build size. The ideal is to have the color image in a jpg and the alpha information in a separate 8-bit png.
I am aware, this is not how Unity works (you import the texture into a format, Unity compresses the asset package at build time etc.) Still, I found out I can load jpg and png trough Texture2D.LoadImage, but I have no idea how to put them together. I guess a shader could do that, but I have no clue how to set them up in Unity and even less how to use them in code to call something like Finale = Merge(jpg, png).
void Start () { //load the prepared image data TextAsset colortxt; TextAsset alphatxt; Texture2D colorjpg = new Texture2D(4, 4); Texture2D alphapng = new Texture2D(4, 4); colortxt = Resources.Load("1024cloak01") as TextAsset; alphatxt = Resources.Load ("1024cloak01A") as TextAsset; colorjpg.LoadImage(colortxt.bytes); alphapng.LoadImage(alphatxt.bytes); //load the prepared Shader asset Shader rgbplusa; rgbplusa = Shader.Find("RGBplusA"); //use NGUI to display the image pixel-perfect, make use of the camera setup created with NGUI > Open the UI Wizard UITexture perfect = NGUITools.AddWidget<UITexture>(GameObject.Find ("Panel")); perfect.material = new Material (rgbplusa); perfect.material.SetTexture("_MainTex", colorjpg); perfect.material.SetTexture("_Mask", alphapng); perfect.MakePixelPerfect(); }
so I figured out a shader that does what I need, see below. If there are some errors or unnecessary stuff, please let me know. It takes one RGB 24 bit Unity texture and one Alpha 8 Unity Texture (with Alpha from Greyscale ON). I tested it in the editor with a simple scene. I created a material, changed the shader to the custom one, added the textures. Then I added the material onto a plane and it works.
I also figured out how to avoid Unity importing my .jpg and .png as textures once I add them in the Resource folder, as I want them added to the build in their compressed form. I change the extension to .bytes and then use Resource.Load("name") as TextAsset and then create a texture and add the image data from the TextAsset.bytes with Texture2D.LoadImage.
So I have a shader and the two textures loaded from the image files in code. I can create a plane, but I am not sure how to scale and position it so that the image is displayed pixel perfect. Is there an easy way how to do it? Is there a way how to use the shader without a plane/mesh?
- Shader "RGBplusA" {
- Properties {
- _MainTex ("Main Texture", 2D) = "white" {}
- _Mask ("Mask Texture", 2D) = "white" {}
- }
- SubShader {
- Blend SrcAlpha OneMinusSrcAlpha
- Pass
- {
- SetTexture [_Mask] {combine texture}
- SetTexture [_MainTex] {combine texture, previous}
- }
- }
- }
how to combine jpg + separate alpha in png?的更多相关文章
- Unity3d之Shader编程:子着色器、通道与标签的写法 & 纹理混合
一.子着色器 Unity中的每一个着色器都包含一个subshader的列表,当Unity需要显示一个网格时,它能发现使用的着色器,并提取第一个能运行在当前用户的显示卡上的子着色器. 我们知道,子着色器 ...
- Unity3d Shader开发(三)Pass(Texturing )
纹理在基本的顶点光照被计算后被应用.在着色器中通过SetTexture 命令来完成. SetTexture 命令在片面程序被使用时不会生效:这种模式下像素操作被完全描述在着色器中. 材质贴图可以用 ...
- Texture Combiner
[Texture Combiner] After the basic vertex lighting has been calculated, textures are applied. In Sha ...
- 【浅墨Unity3D Shader编程】之三 光之城堡篇:子着色器、通道与标签的写法 & 纹理混合
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://hpw123.net/a/C__/kongzhitaichengxu/2014/1117/120.html 作者:毛星云 ...
- Unity3D NGUI动态生成模糊背景图
先上效果. 制作原理:模糊的部分是用UITexture,前面是一个UISprite.用主摄像机渲染出一张纹理,把这张纹理模糊处理,把这张纹理赋值给UITexture. 脚本代码 using Unity ...
- 转:SDL2源代码分析
1:初始化(SDL_Init()) SDL简介 有关SDL的简介在<最简单的视音频播放示例7:SDL2播放RGB/YUV>以及<最简单的视音频播放示例9:SDL2播放PCM>中 ...
- Unity 的“Vertex Lit Rendering path“中 shader Pass 的注意事项
"MADFINGER/Environment/Unlit (Supports Lightmap)"是 ShadowGun 示例中最简单的 shader 了,如下: // Unlit ...
- SDL2来源分析3:渲染(SDL_Renderer)
===================================================== SDL源代码分析系列文章上市: SDL2源码分析1:初始化(SDL_Init()) SDL2 ...
- SDL2源代码分析3:渲染器(SDL_Renderer)
===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...
随机推荐
- Java系列:JVM指令详解(上)(zz)
一.未归类系列A 此系列暂未归类. 指令码 助记符 说明 59:iastore 60:lload 6 //因为str ...
- (转)Python实例手册
原文地址:http://hi.baidu.com/quanzhou722/item/cf4471f8e23d3149932af2a7 实在是太好的资料了,不得不转 python实例手册 #encodi ...
- Lisp简明教程
此教程是我花了一点时间和功夫整理出来的,希望能够帮到喜欢Lisp(Common Lisp)的朋友们.本人排版很烂还望多多海涵! <Lisp简明教程>PDF格式下载 <Lisp简明教程 ...
- node的实践(项目三)
渲染前台的方式. <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" ...
- 学习之路三十二:VS调试的简单技巧
这段时间园子里讲了一些关于VS的快捷键以及一些配置技巧,挺好的,大家一起学习,一起进步. 这段时间重点看了一下关于VS调试技巧方面的书,在此记录一下学习的内容吧,主要还是一些比较浅显的知识. 1. 调 ...
- Lambda表达式和表达式树
在C# 2.0中,通过方法组转换和匿名方法,使委托的实现得到了极大的简化.但是,匿名方法仍然有些臃肿,而且当代码中充满了匿名方法的时候,可读性可能就会受到影响.C# 3.0中出现的Lambda表达式在 ...
- jQuery基础之(一)jQuery概述
1.jQuery的简介 就像上节所将到的Ajax框架一样,简单的说,jQuery是一个优秀的javascript框架,它能够让用户方便的处理html,events(冒泡)事件,动画效果,ajax交互等 ...
- UIToolbar 如何调整里面的按钮位置
可以在Bar Button Item后面填个Fixed Space Bar Button Item 或者Flexible Space Bar Button Item,然后再在后面加上下一个Bar Bu ...
- 压缩算法实现之LZ78
LZ78编码 LZ78算法,建立词典的算法. LZ78的编码思想: 不断地从字符流中提取新的缀-符串(String),通俗地理解为新"词条",然后用"代号"也就 ...
- 5.9-2比较str1和str2截取后的子串
package zfc; public class ZfcShcq { public static void main(String[] args) { // TODO Auto-generated ...