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 ...
随机推荐
- 基于Microsoft Azure、ASP.NET Core和Docker的博客系统
欢迎阅读daxnet的新博客:一个基于Microsoft Azure.ASP.NET Core和Docker的博客系统 2008年11月,我在博客园开通了个人帐号,并在博客园发表了自己的第一篇博客 ...
- MySQL server has gone away的解决方法
用Python写了一个http服务,需要从mysql读数据库,第一天还好好的,第二天突然不行了.报错如下: pymysql.err.OperationalError: (2006, 'MySQL se ...
- react-native 的微信SDK辅助包,支持微信登录、微信分享、微信支付
微信SDK集成示例,现已完成微信授权登录,之后将陆续包装分享等其他功能. ReactNative高级交流群 127482131 或访问 http://blog.1ygowu.com ReactNat ...
- 后盾网VIP美团网开发(基于HDPHP)(全套38课)
教程简介 本教程由后盾网讲解,共40节,主要介绍了美团网的开发,从需求分析出发,对商铺的建立.购物流程的构建及订单处理等都做了详细的介绍,非常适合做电子商务开发的朋友和同学参考学习使用,完整教程可以在 ...
- 在opencv3中的机器学习算法
在opencv3.0中,提供了一个ml.cpp的文件,这里面全是机器学习的算法,共提供了这么几种: 1.正态贝叶斯:normal Bayessian classifier 我已在另外一篇博文中介 ...
- 把字符串添加到HashMap中
&ZhuoTai_Name=205&NoSongDanDish=0&OrderZhuoTai_ID=aca87b77797e4c859a53c228471a2636&Z ...
- HTML5 文件异步上传 — h5uploader.js
原文地址:http://imziv.com/blog/article/read.htm?id=62 之前写过一篇H5异步文件上传的文章, 但是很多朋友看着我的这个教程还是出现很多问题,文章写的不是很好 ...
- 关于git托管的一些心得
GIT托管的一些心得 熟练运用软件进行GIT托管的好处 在上一周的学习中,我提出来了一个疑惑,就是为什么一定要用软件托管而不选择web托管,在这周的学习中,我通过实践体会到了一些运用软件托管的好处: ...
- 『片段』OracleHelper (支持 多条SQL语句)
C# 调用 Oracle 是如此尴尬 >System.Data.OracleClient.dll —— .Net 自带的 已经 过时作废. >要链接 Oracle 服务器,必须在 本机安装 ...
- 《深入了解 Linq to SQL》之对象的增删改 —— 麦叔叔呕心呖血之作
你的程序里,是否到处充斥着这种代码: db.Customers.InsertOnSubmit(customer); db.SubmitChange(); 如果某一天,因为 Customers 表的数据 ...