Created
June 29, 2023 14:30
-
-
Save hecomi/62513a24bd3b07dd67ab17d3c63dde5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class AsyncTextureCreator : MonoBehaviour | |
{ | |
public int width = 128; | |
public int height = 32; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using Unity.Burst; | |
using Unity.Jobs; | |
using Unity.Collections; | |
using Unity.Mathematics; | |
[BurstCompile] | |
internal struct CreateTextureJob : IJob | |
{ | |
[WriteOnly] public NativeArray<Color32> texColors; | |
[ReadOnly] [DeallocateOnJobCompletion] public NativeArray<float> array; | |
Color ToRGB(float hue) | |
{ | |
hue = (1f - math.cos(math.PI * hue)) * 0.5f; | |
hue = 1f - hue; | |
hue *= 5f; | |
var x = 1 - math.abs(hue % 2f - 1f); | |
return | |
hue < 1f ? new Color(1f, x, 0f) : | |
hue < 2f ? new Color(x, 1f, 0f) : | |
hue < 3f ? new Color(0f, 1f, x) : | |
hue < 4f ? new Color(0f, x, 1f) : | |
new Color(x * 0.5f, 0f, 0.5f); | |
} | |
public void Execute() | |
{ | |
for (int i = 0; i < array.Length; ++i) | |
{ | |
texColors[i] = ToRGB(array[i]); | |
} | |
} | |
} | |
[CustomEditor(typeof(AsyncTextureCreator))] | |
public class AsyncTextureCreatorEditor : Editor | |
{ | |
AsyncTextureCreator creator => target as AsyncTextureCreator; | |
Texture2D _texture; | |
JobHandle _jobHandle; | |
bool _isTextureUpdateRequested = false; | |
bool isTextureUpdateRequired => | |
!_texture || | |
_texture.width != creator.width || | |
_texture.height != creator.height; | |
void UpdateTexture() | |
{ | |
if (_isTextureUpdateRequested) | |
{ | |
_jobHandle.Complete(); | |
_texture.Apply(); | |
_isTextureUpdateRequested = false; | |
} | |
if (!isTextureUpdateRequired) return; | |
int width = creator.width; | |
int height = creator.height; | |
_texture = new Texture2D(width, height); | |
var texColors = _texture.GetPixelData<Color32>(0); | |
var array = new NativeArray<float>(width * height, Allocator.TempJob); | |
for (int y = 0; y < height; ++y) | |
{ | |
for (int x = 0; x < width; ++x) | |
{ | |
int i = y * width + x; | |
array[i] = i / 256f; | |
} | |
} | |
var job = new CreateTextureJob() | |
{ | |
texColors = texColors, | |
array = array, | |
}; | |
_jobHandle = job.Schedule(); | |
_isTextureUpdateRequested = true; | |
Repaint(); | |
} | |
public override void OnInspectorGUI() | |
{ | |
DrawDefaultInspector(); | |
EditorGUILayout.Space(); | |
UpdateTexture(); | |
var area = EditorGUILayout.GetControlRect(false, creator.height); | |
GUI.DrawTexture(area, _texture, ScaleMode.StretchToFill); | |
} | |
} |
Author
hecomi
commented
Jun 29, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment