Search Unity

Color Array to unlit Shader in Editor

Discussion in 'Shaders' started by ZenRumiko, Mar 2, 2017.

  1. ZenRumiko

    ZenRumiko

    Joined:
    Jan 7, 2014
    Posts:
    2
    Hello all.
    I'm very new to sharers in general so I'm wondering if I could get a bit of help in writing a shader that'll turn a color array into a view able texture for me to use with Graphics.DrawTexture in the Editor.
    I've done a bit of searching but haven't come up with anything yet. I'm assuming this method will be quicker than creating a new texture every time I update the array.

    The method I'm using currently to get an x/y position in the array is "y * width + x" if that helps.

    I'm guessing you'd also need to pass the texture width into the shader.

    Best of luck, I'm hoping that this is an easy one to anybody with a knowledge of shaders.
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Hey there,

    I'm not quite sure if using shaders is the right usecase for that. If you want to create textures, I guess using a script will be a more viable solution. I made a simple example on how such a script could look like (Using scriptable objects to keep the settings for different textures nicely coupled):
    Code (CSharp):
    1. // --------------------------------------------------------------------------------------------------------------------
    2. // <copyright file="TextureCreator.cs" company="Supyrb">
    3. //   Copyright (c) 2017 Supyrb. All rights reserved.
    4. // </copyright>
    5. // <author>
    6. //   Johannes Deml
    7. //   send@johannesdeml.com
    8. // </author>
    9. // --------------------------------------------------------------------------------------------------------------------
    10.  
    11. using System.IO;
    12. using UnityEngine;
    13. using UnityEditor;
    14.  
    15. namespace Supyrb
    16. {
    17.    
    18.     public class TextureCreator : ScriptableObject
    19.     {
    20.         public string FilePath = "GeneratedTexture.png";
    21.         public int Width = 8;
    22.         public int Height = 1;
    23.  
    24.         public Color[] Colors = new Color[8];
    25.        
    26.  
    27.         [ContextMenu("Create Texture")]
    28.         public void CreateTexture()
    29.         {
    30.             var texture = new Texture2D(Width, Height, TextureFormat.ARGB32, false);
    31.  
    32.             for (int y = 0; y < texture.height; y++)
    33.             {
    34.                 for (int x = 0; x < texture.width; x++)
    35.                 {
    36.                     texture.SetPixel(x, y, GetColorValue(x, y));
    37.                 }
    38.             }
    39.             texture.Apply();
    40.  
    41.  
    42.             // Encode texture into PNG
    43.             byte[] bytes = texture.EncodeToPNG();
    44.  
    45.             File.WriteAllBytes(FilePath, bytes);
    46.  
    47.             AssetDatabase.Refresh();
    48.         }
    49.  
    50.         public Color GetColorValue(int x, int y)
    51.         {
    52.             var index = Width*y + x;
    53.             if (index >= Colors.Length)
    54.             {
    55.                 return Color.black;
    56.             }
    57.             return Colors[index];
    58.         }
    59.  
    60.         void Reset()
    61.         {
    62.             FilePath = Application.dataPath + "/GeneratedTexture.png";
    63.         }
    64.     }
    65. }
    66.  
    This is the resulting scriptable object. When you click create texture a png will be saved to the defined file path.



    This is rather simple, but I guess it gives you an idea on where to go with creating textures.
     
  3. ZenRumiko

    ZenRumiko

    Joined:
    Jan 7, 2014
    Posts:
    2
    Nice script, but I actually do need a shader so that I can render the colors on screen without any of the overhead of creating textures. Thanks for the help though.