-
Notifications
You must be signed in to change notification settings - Fork 14
/
LoadTextureAsyncFromDDS.cs
40 lines (37 loc) · 1.37 KB
/
LoadTextureAsyncFromDDS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using UnityEngine;
using System;
using System.IO;
public class LoadTextureAsyncFromDDS : MonoBehaviour
{
async void Load (Material material, string property, string filepath)
{
FileStream stream = File.Open(filepath, FileMode.Open);
long length = stream.Length;
byte[] header = new byte[128];
await stream.ReadAsync(header, 0, 128);
int height = header[13] * 256 + header[12];
int width = header[17] * 256 + header[16];
bool mipmaps = header[28] > 0;
TextureFormat textureFormat = header[87] == 49 ? TextureFormat.DXT1 : TextureFormat.DXT5;
byte[] source = new byte[Convert.ToInt32(length) - 128];
await stream.ReadAsync(source, 0, Convert.ToInt32(length) - 128);
stream.Close();
Texture2D texture = new Texture2D(width, height, textureFormat, mipmaps);
texture.LoadRawTextureData(source);
texture.name = Path.GetFileName(filepath);
texture.Apply(false, true);
material.SetTexture(property, texture);
}
void Delete (Material material, string property)
{
Destroy(material.GetTexture(property));
material.SetTexture(property, null);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.L))
Load(GetComponent<Renderer>().material, "_MainTex", System.IO.Path.Combine(Application.streamingAssetsPath, "plasma.dds"));
if (Input.GetKeyDown(KeyCode.Space))
Delete(GetComponent<Renderer>().material, "_MainTex");
}
}