My level is limited, is not the place also to Danale crazy spray!
See this title, some people estimate will be questioned, this does not hurt, good PNG why to split into two AH!
If you have such doubts, so angry words, congratulations you come to the place, at the same time also small despise you, level too food. Haha, come on!
Why do you do this?
1, the main purpose in order to reduce the size of the package, while the picture quality loss. Hand Tour We all know that the smaller the package, the higher the conversion rate (under the premise of the same playability)
Implementation Details: The introduction of an alpha channel with the PS, in the PS inside a new size with the original image, the format of the bitmap (why Mughal, alpha value in the mark when the transparent is not black and white that is 0 1), copy the original alpha channel to the new layer, save as PNG , save the original image as JPG so mask and JPG are made.
Before and after size comparison:
Implementation principle:
During the rendering process, the two are restored back, the buffer RGB is the JPG texture of the rgb,a as the mask texture R or G or B (why is the r/g/b saved mask when the three values are the same don't believe you try anyway I tried it)
Shader Source:
Shader"masktest"{Properties {_maintex ("Base (RGB)", 2D) =""{} _mainmask ("Base (RGB)", 2D) =""{}} subshader {Pass { Blend srcalpha Oneminussrcalpha//must not forget this CGP Rogram#pragmaVertex vert#pragmaFragment Frag#include"Unitycg.cginc" structvert_input {fixed4 vertex:position; Fixed2 texcoord:texcoord0; }; structvert_output {fixed4 pos:sv_position; Fixed2 texcoord:texcoord0; }; Uniform sampler2d _maintex; Uniform sampler2d _mainmask; Vert_output Vert (Vert_input i) {vert_output o; O.pos=Mul (Unity_matrix_mvp,i.vertex); O.texcoord=I.texcoord; returno; } fixed4 Frag (Vert_output o): COLOR {fixed4 Color1=tex2d (_maintex,o.texcoord); Fixed4 Mask=tex2d (_mainmask,o.texcoord); color1.a=MASK.R; returnColor1; } ENDCG} }}
Implementation results:
Using textures:
This basically realizes the function, but a little trouble also calculates two textures each time, the efficiency is a little low, is not tall still
Referring to others, is to load the texture, the implementation of texture merging, in memory to generate a texture, to avoid the operation in the shader. The implementation principle is JPG RGB copy to texture RGB, the PNG r/b/g copy to texture a
The core code is as follows:
for (int i = 0; i < len; i++)
{
Dest = pngdata[srcindex];//Gets the first
Outpic[outindex] = Jpgdata[srcindex];
Outpic[outindex + 1] = Jpgdata[srcindex + 1];
Outpic[outindex + 2] = jpgdata[srcindex+2];
Outpic[outindex + 3] = dest;
Srcindex + = 3;
Outindex + = 4;
}
Original Jpg+mask Replace Transparent PNG