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?

  1. Shader "RGBplusA" {
  2. Properties {
  3. _MainTex ("Main Texture", 2D) = "white" {}
  4. _Mask ("Mask Texture", 2D) = "white" {}
  5. }
  6. SubShader {
  7. Blend SrcAlpha OneMinusSrcAlpha
  8. Pass
  9. {
  10. SetTexture [_Mask] {combine texture}
  11. SetTexture [_MainTex] {combine texture, previous}
  12. }
  13. }
  14. }
时间: 2024-12-19 01:49:33

how to combine jpg + separate alpha in png?的相关文章

【浅墨Unity3D Shader编程】之三 光之城堡篇:子着色器、通道与标签的写法 &amp; 纹理混合

本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/41175585 作者:毛星云(浅墨)    微博:http://weibo.com/u/1723155442 邮箱: [email protected] 本文介绍了Unity中子着色器.通道和标签相关的详细概念与写法,以及纹理的设置方法,基本的纹理混合写法,写了5个Shader作为本文Shader讲解的实战内容,最后创建了一个梦幻的光之

Texture Combiner

[Texture Combiner] After the basic vertex lighting has been calculated, textures are applied. In ShaderLab this is done using SetTexture command. SetTexture commands have no effect when fragment programs are used; as in that case pixel operations are

Unity3d之Shader编程:子着色器、通道与标签的写法 &amp; 纹理混合

一.子着色器 Unity中的每一个着色器都包含一个subshader的列表,当Unity需要显示一个网格时,它能发现使用的着色器,并提取第一个能运行在当前用户的显示卡上的子着色器. 我们知道,子着色器定义了一个渲染通道的列表,并可选是否为所有通道初始化所需要的通用状态.子着色器的写法如下: Subshader{ [Tags] [CommonState] Passdef [Passdef ...] } 也就是通过可选标签,通用状态 和 一个Pass 定义的列表构成了子着色器. 当Unity选择用于

SDL2来源分析3:渲染(SDL_Renderer)

===================================================== SDL源代码分析系列文章上市: SDL2源码分析1:初始化(SDL_Init()) SDL2源码分析2:窗体(SDL_Window) SDL2源码分析3:渲染器(SDL_Renderer) SDL2源码分析4:纹理(SDL_Texture) SDL2源码分析5:更新纹理(SDL_UpdateTexture()) SDL2源码分析6:拷贝到渲染器(SDL_RenderCopy()) SDL2

SDL2源代码分析3:渲染器(SDL_Renderer)

上一篇文章分析了SDL中创建窗口的函数SDL_CreateWindow().这篇文章继续分析SDL的源代码.本文分析SDL的渲染器(SDL_Renderer). SDL播放视频的代码流程如下所示. 初始化:  SDL_Init(): 初始化SDL. SDL_CreateWindow(): 创建窗口(Window). SDL_CreateRenderer(): 基于窗口创建渲染器(Render). SDL_CreateTexture(): 创建纹理(Texture). 循环渲染数据:  SDL_U

Unity3D NGUI动态生成模糊背景图

先上效果. 制作原理:模糊的部分是用UITexture,前面是一个UISprite.用主摄像机渲染出一张纹理,把这张纹理模糊处理,把这张纹理赋值给UITexture. 脚本代码 using UnityEngine; using System.Collections; [RequireComponent(typeof(UITexture))] public class BlurTextureMaker : MonoBehaviour { public int iterations = 3; pub

转:SDL2源代码分析

1:初始化(SDL_Init()) SDL简介 有关SDL的简介在<最简单的视音频播放示例7:SDL2播放RGB/YUV>以及<最简单的视音频播放示例9:SDL2播放PCM>中已经叙述过了,不再重复.这两篇文章中也提到了一张SDL的原理图,如下所示: 从这个图中可以看出,SDL根据系统的不同调用不同的API完成相应的功能.至于它是如何实现的,将会在后文中详细叙述.下面不再罗嗦,直接进入正题. 使用SDL播放一个视频代码流程大体如下 初始化: SDL_Init(): 初始化SDL. 

【转】Transparency (or Translucency) Rendering

From:https://developer.nvidia.com/content/transparency-or-translucency-rendering Like many other visual effects, games attempt to mimic transparent (or translucent as it's often synonymously referred to in the games industry) objects as closely as po

openGL+VS2010的例程--特效(三维)

效果图如上: 步骤:略. 实现代码如下: main.cpp 1 /********************************************************************** 2 3 Particle Engine / Billboarding 4 5 October, 21st, 2002 6 7 This tutorial was written by Philipp Crocoll 8 Contact: 9 [email protected] 10 www.