123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using UnityEngine;
- using System.Collections;
- using System;
- namespace TerrainComposer2
- {
- [Serializable]
- public class TexturePreview
- {
- public bool edit;
- public Texture2D tex;
- [NonSerialized]
- public byte[] bytes;
- float x, y;
- public void Init()
- {
- // Debug.Log("Create");
- int resolution = TC_Area2D.current.previewResolution;
- if (bytes == null) bytes = new byte[resolution * resolution * 4];
- if (tex == null)
- {
- tex = new Texture2D(resolution, resolution, TextureFormat.RGBA32, false);
- tex.hideFlags = HideFlags.DontSave;
- tex.name = "texPreview";
- }
- }
- public void Init(int resolution)
- {
- bool create = false;
- if (bytes == null) bytes = new byte[resolution * resolution * 4];
- if (tex == null) create = true;
- else if (tex.width != resolution)
- {
- tex.Resize(resolution, resolution);
- return;
- }
- if (create)
- {
- tex = new Texture2D(resolution, resolution, TextureFormat.RGBA32, false);
- tex.hideFlags = HideFlags.DontSave;
- tex.name = "texPreview";
- }
- }
- public void ReCreate(int resolution)
- {
- // Debug.Log("recreate preview tex");
- bytes = new byte[resolution * resolution * 4];
- tex = new Texture2D(resolution, resolution, TextureFormat.RGBA32, false);
- tex.hideFlags = HideFlags.DontSave;
- tex.name = "texPreview";
- }
- public void UploadTexture()
- {
- tex.LoadRawTextureData(bytes);
- tex.Apply(false);
- }
- public void SetPixel(float v)
- {
- x = TC_Area2D.current.previewPos.x;
- y = TC_Area2D.current.previewPos.y;
- int px = (int)x;
- int py = (int)y;
- if (px > 127 || px < 0) return;
- if (py > 127 || py < 0) return;
- py *= 512;
- px *= 4;
- Color32 color = Color.white * v;
- if (v > 1) color = Color.Lerp(Color.red, new Color(1, 0, 1), Mathw.Clamp01(v - 1));
- else if (v < 0) color = Color.Lerp(Color.cyan, Color.blue, Mathw.Clamp01(v * -1));
- bytes[px + py] = (byte)(color.r);
- bytes[px + py + 1] = (byte)(color.g);
- bytes[px + py + 2] = (byte)(color.b);
- bytes[px + py + 3] = 1;
- }
- public void SetPixelColor(Color color)
- {
- // if (tex == null) Create(128);
- x = TC_Area2D.current.previewPos.x;
- y = TC_Area2D.current.previewPos.y;
- int px = (int)x;
- int py = (int)y;
- if (px > 127 || px < 0) return;
- if (py > 127 || py < 0) return;
- py *= 512;
- px *= 4;
- bytes[px + py] = (byte)(color.r * 255);
- bytes[px + py + 1] = (byte)(color.g * 255);
- bytes[px + py + 2] = (byte)(color.b * 255);
- bytes[px + py + 3] = 1;
- }
- public void SetPixelColor(int px, int py, Color color)
- {
- py *= tex.width * 4;
- px *= 4;
- bytes[px + py] = (byte)(color.r * 255);
- bytes[px + py + 1] = (byte)(color.g * 255);
- bytes[px + py + 2] = (byte)(color.b * 255);
- bytes[px + py + 3] = 1;
- }
- }
- }
|