64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
|
|
namespace Artngame.SKYMASTER
|
|
{
|
|
public class TGALoader
|
|
{
|
|
public static Texture3D load3DFromTGASlices(TextAsset asset)
|
|
{
|
|
using BinaryReader binaryReader = new BinaryReader(new MemoryStream(asset.bytes));
|
|
binaryReader.BaseStream.Seek(12L, SeekOrigin.Begin);
|
|
short num = binaryReader.ReadInt16();
|
|
short num2 = binaryReader.ReadInt16();
|
|
int num3 = num2 * num2;
|
|
int num4 = binaryReader.ReadByte();
|
|
binaryReader.BaseStream.Seek(1L, SeekOrigin.Current);
|
|
Color32[] array = new Color32[num * num2];
|
|
Texture3D texture3D;
|
|
switch (num4)
|
|
{
|
|
case 32:
|
|
{
|
|
for (int j = 0; j < array.Length; j++)
|
|
{
|
|
byte a = binaryReader.ReadByte();
|
|
byte b2 = binaryReader.ReadByte();
|
|
byte g2 = binaryReader.ReadByte();
|
|
byte r2 = binaryReader.ReadByte();
|
|
int num8 = j % num % num2;
|
|
int num9 = j / num;
|
|
int num10 = j % num / num2 * num3 + num9 * num2 + num8;
|
|
array[num10] = new Color32(r2, g2, b2, a);
|
|
}
|
|
texture3D = new Texture3D(num2, num2, num2, TextureFormat.RGBA32, mipChain: true);
|
|
break;
|
|
}
|
|
case 24:
|
|
{
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
byte b = binaryReader.ReadByte();
|
|
byte g = binaryReader.ReadByte();
|
|
byte r = binaryReader.ReadByte();
|
|
int num5 = i % num % num2;
|
|
int num6 = i / num;
|
|
int num7 = i % num / num2 * num3 + num6 * num2 + num5;
|
|
array[num7] = new Color32(r, g, b, 1);
|
|
}
|
|
texture3D = new Texture3D(num2, num2, num2, TextureFormat.RGB24, mipChain: true);
|
|
break;
|
|
}
|
|
default:
|
|
throw new Exception("TGA texture had non 32/24 bit depth.");
|
|
}
|
|
texture3D.SetPixels32(array, 0);
|
|
texture3D.wrapMode = TextureWrapMode.Repeat;
|
|
texture3D.filterMode = FilterMode.Bilinear;
|
|
texture3D.Apply();
|
|
return texture3D;
|
|
}
|
|
}
|
|
}
|