• 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. }
  • 相关阅读:
    关于Jupyter Notebook默认起始目录设置无效的解决方法
    关于二叉树中度为0与度为2节点数关系证明
    LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List
    数据结构,物理结构,存储结构,逻辑结构的区分
    条件变量 sync.Cond
    defer 的常用场景
    switch...case... 语句中的类型转换
    golang 切片和数组在for...range中的区别
    golang 并发顺序输出数字
    Golang 中哪些值是不可以寻址的
  • 原文地址:https://www.cnblogs.com/j349900963/p/4687229.html
Copyright © 2020-2023  润新知