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 ...
随机推荐
- 查询EBS请求日志的位置和名称
select * from FND_CONCURRENT_PROGRAMS_VL fcp where fcp.USER_CONCURRENT_PROGRAM_NAME like '%CUX%XXXX% ...
- JS调用Silverlight方法拾遗
在最近做的物联网项目中,需要利用封装过的Silverlight刻度控件显示温度,湿度,二氧化碳浓度等值.由于最新的数据是通过js ajax获取的,所以需要把这些数据传递给silverlight显示,这 ...
- 【.NET】传智播客第【19】期就业班视频(高清无加密)
[.NET]传智播客第[19]期就业班视频(高清无加密) 下载地址:http://fu83.cn/thread-85-1-1.html
- ViewController与outlet绑定
ViewController的作用 ViewController与XIB一一对应,用于分离独立出可重用组件单元,如单个组件.复合组件.界面片段.整个界面等. 通常继承 UIViewController ...
- Django1.8教程——从零开始搭建一个完整django博客(三)
这一节主要介绍对数据库的访问操作:通过管理器(manage),对对象进行检索.修改.删除等操作,详细介绍了如何针对不同的模型自定义管理器. 查询和管理工作 现在,我们已经有了一个功能完善的Django ...
- web安全——简介
简介 不对外提供服务是最安全的. 安全是基于信任.如果信任失败了,则没有安全.比如你给一个ip加白名单,结果这个ip对你发动了安全攻击. 在非常明确需要提供服务的时候才对外提供服务,即白名单.其他的全 ...
- VC6.0中MFC界面换肤简例
利用VC中的MFC进行界面设计时,发现界面上的各控件无法简易地进行调整,比如字体大小.颜色.格式等. 为了改变外观,小小地美化一下,今天决定动手一试. 网上提供的库和方法不计其数,我选择了SkinMa ...
- 初探JAVA中I/O流(二)
1.缓冲输入文件 FileReader BufferedReader FileReader可以直接对文件进行读操作.但是简化编程,加快读取速度,我们加入了缓冲机制,使用了BufferedReader. ...
- loaded the "XXXView" nib but the view outlet was not set 解决方案
'-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "XXXView" nib but the view o ...
- nginx 出现413 Request Entity Too Large问题的解决方法
nginx 出现413 Request Entity Too Large问题的解决方法 使用php上传图片(大小1.9M),出现 nginx: 413 Request Entity Too Large ...