- Home /
solved: how to combine jpg + separate alpha in png?
Hello,
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).
I am open to any ideas, and hope someone solved this already :) Thank you for your time.
jpg?! the format that adds more noise than a vuvuzela concert?
oh, i found a thing that might be helpful (using jpg is generally not going to have good results unless you spend too much time making it look nice): http://u3d.as/content/anomalous-underdog/build-report-tool/4u2
Answer by Andrej Vojtas · Apr 30, 2013 at 12:16 AM
Ok,
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}
}
}
}
There are multiple ways of getting something "pixel perfect." You might take a look at Graphics.DrawTexture() to see if it is what you want. It takes a material as a parameter, so you can use your shader.
I ended up using NGUI from code to display the result pixel perfect, for a complete code example see: http://www.tasharen.com/forum/index.php?topic=4018.msg19784#msg19784
Your answer
![](https://koobas.hobune.stream/wayback/20220613101201im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Remove white edges on texture help 2 Answers
Glass Shader with Strumpy Shader Editor 1 Answer
About shader! 0 Answers
Chromakey shader for movie textures? 2 Answers
Fade Shader- One texture fades in, the other fades out 2 Answers