Guest User

Untitled

a guest
Sep 11th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. [StructLayout(LayoutKind.Sequential)]
  2. public struct DefaultMaterialProperties
  3. {
  4.     public Vector3 AmbientColor;
  5.     public Vector3 DiffuseColor;
  6.     public Vector3 SpecularColor;
  7.  
  8.     public unsafe static implicit operator byte[] (DefaultMaterialProperties data)
  9.     {
  10.         var myBuffer = new byte[3 * 4 * sizeof(float)];
  11.  
  12.         fixed (byte* ptr = myBuffer)
  13.         {
  14.             // Ambient color
  15.             *((byte*)(ptr + 0)) = (byte)((UInt32)data.AmbientColor.X & 0xff000000 >> 24);
  16.             *((byte*)(ptr + 1)) = (byte)((UInt32)data.AmbientColor.X & 0x00ff0000 >> 16);
  17.             *((byte*)(ptr + 2)) = (byte)((UInt32)data.AmbientColor.X & 0x0000ff00 >> 8);
  18.             *((byte*)(ptr + 3)) = (byte)((UInt32)data.AmbientColor.X & 0x000000ff >> 0);
  19.             *((byte*)(ptr + 4)) = (byte)((UInt32)data.AmbientColor.Y & 0xff000000 >> 24);
  20.             *((byte*)(ptr + 5)) = (byte)((UInt32)data.AmbientColor.Y & 0x00ff0000 >> 16);
  21.             *((byte*)(ptr + 6)) = (byte)((UInt32)data.AmbientColor.Y & 0x0000ff00 >> 8);
  22.             *((byte*)(ptr + 7)) = (byte)((UInt32)data.AmbientColor.Y & 0x000000ff >> 0);
  23.             *((byte*)(ptr + 8)) = (byte)((UInt32)data.AmbientColor.Z & 0xff000000 >> 24);
  24.             *((byte*)(ptr + 9)) = (byte)((UInt32)data.AmbientColor.Z & 0x00ff0000 >> 16);
  25.             *((byte*)(ptr + 10)) = (byte)((UInt32)data.AmbientColor.Z & 0x0000ff00 >> 8);
  26.             *((byte*)(ptr + 11)) = (byte)((UInt32)data.AmbientColor.Z & 0x000000ff >> 0);
  27.  
  28.             // Diffuse color
  29.             *((byte*)(ptr + 16)) = (byte)((UInt32)data.DiffuseColor.X & 0xff000000 >> 24);
  30.             *((byte*)(ptr + 17)) = (byte)((UInt32)data.DiffuseColor.X & 0x00ff0000 >> 16);
  31.             *((byte*)(ptr + 18)) = (byte)((UInt32)data.DiffuseColor.X & 0x0000ff00 >> 8);
  32.             *((byte*)(ptr + 19)) = (byte)((UInt32)data.DiffuseColor.X & 0x000000ff >> 0);
  33.             *((byte*)(ptr + 20)) = (byte)((UInt32)data.DiffuseColor.Y & 0xff000000 >> 24);
  34.             *((byte*)(ptr + 21)) = (byte)((UInt32)data.DiffuseColor.Y & 0x00ff0000 >> 16);
  35.             *((byte*)(ptr + 22)) = (byte)((UInt32)data.DiffuseColor.Y & 0x0000ff00 >> 8);
  36.             *((byte*)(ptr + 23)) = (byte)((UInt32)data.DiffuseColor.Y & 0x000000ff >> 0);
  37.             *((byte*)(ptr + 24)) = (byte)((UInt32)data.DiffuseColor.Z & 0xff000000 >> 24);
  38.             *((byte*)(ptr + 25)) = (byte)((UInt32)data.DiffuseColor.Z & 0x00ff0000 >> 16);
  39.             *((byte*)(ptr + 26)) = (byte)((UInt32)data.DiffuseColor.Z & 0x0000ff00 >> 8);
  40.             *((byte*)(ptr + 27)) = (byte)((UInt32)data.DiffuseColor.Z & 0x000000ff >> 0);
  41.  
  42.             // Specular color
  43.             *((byte*)(ptr + 32)) = (byte)((UInt32)data.SpecularColor.X & 0xff000000 >> 24);
  44.             *((byte*)(ptr + 33)) = (byte)((UInt32)data.SpecularColor.X & 0x00ff0000 >> 16);
  45.             *((byte*)(ptr + 34)) = (byte)((UInt32)data.SpecularColor.X & 0x0000ff00 >> 8);
  46.             *((byte*)(ptr + 35)) = (byte)((UInt32)data.SpecularColor.X & 0x000000ff >> 0);
  47.             *((byte*)(ptr + 36)) = (byte)((UInt32)data.SpecularColor.Y & 0xff000000 >> 24);
  48.             *((byte*)(ptr + 37)) = (byte)((UInt32)data.SpecularColor.Y & 0x00ff0000 >> 16);
  49.             *((byte*)(ptr + 38)) = (byte)((UInt32)data.SpecularColor.Y & 0x0000ff00 >> 8);
  50.             *((byte*)(ptr + 39)) = (byte)((UInt32)data.SpecularColor.Y & 0x000000ff >> 0);
  51.             *((byte*)(ptr + 40)) = (byte)((UInt32)data.SpecularColor.Z & 0xff000000 >> 24);
  52.             *((byte*)(ptr + 41)) = (byte)((UInt32)data.SpecularColor.Z & 0x00ff0000 >> 16);
  53.             *((byte*)(ptr + 42)) = (byte)((UInt32)data.SpecularColor.Z & 0x0000ff00 >> 8);
  54.             *((byte*)(ptr + 43)) = (byte)((UInt32)data.SpecularColor.Z & 0x000000ff >> 0);
  55.         }
  56.  
  57.         return myBuffer;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment