Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Backwards RGB conversion

Discussion in 'Scripting' started by RiokuTheSlayer, May 12, 2015.

  1. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Hey all, Rioku here again. But of a question for some conversion stuff that goes on.

    http://www.giawa.com/magicavoxel-c-importer/

    I'm using this script as a base for my voxel engine I'm building, and had a bit of a question. I'd like to know a way to convert the values in voxColors back into RGB values.

    I assume there's a way to reverse this line: "colors = (ushort)(((r & 0x1f) << 10) | ((g & 0x1f) << 5) | (b & 0x1f));"(This line is used to convert the custom RGB value in custom palettes into ushorts) , but I've never done something like this before. So if anyone could show me how, it would be greatly appreciated.

    I don't actually know how or why you would use ushorts instead of Colors for actual colors, either. Any info on that would be nice.
     
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
  3. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    I honestly have no idea what I'm doing XD


    Code (CSharp):
    1. ushort[] buffer = VoxelReader.voxColors;
    2.             byte[] bytes = new byte[4];
    3.             bytes[0] = (byte)(buffer[1] & 0xFF);
    4.             bytes[1] = (byte)(buffer[1] >> 8);
    5.             bytes[2] = (byte)(buffer[0] & 0xFF);
    6.             bytes[3] = (byte)(buffer[0] >> 8);
    7.  
    8.             Debug.Log( BitConverter.ToSingle(bytes,0) );
    9.  
    This comes up with NaN. VoxelReader.voxColors is the same ushort array as in that script. Doesn't seem to want to work. Unless I'm doing it wrong?

    EDIT: Just a note, the RGB isn't save as 3 floats from 0 to 1. It's saving them as bytes from 0 to 255
     
  4. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I have no idea why you're using ushort to store color data. I didn't look at the link. I probably should because there is more going on than what I'm seeing.
     
  5. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Yeah. I don't know why that's what he decided to do either.