@@ -57,16 +57,61 @@ public partial class HsvDrawer
|
||||
}
|
||||
}
|
||||
|
||||
private Coroutine valueSliderCoroutine;
|
||||
private float pendingValue = -1f;
|
||||
|
||||
// 新增:高低分辨率参数
|
||||
public int highResTextureSize = 256;
|
||||
public int lowResTextureSize = 64;
|
||||
private bool isLowRes = false;
|
||||
|
||||
void OnValueSliderChanged(float newValue)
|
||||
{
|
||||
value = newValue;
|
||||
CreateColorWheelTexture();
|
||||
ApplyTextureToRawImage();
|
||||
pendingValue = newValue;
|
||||
|
||||
// 切换到低分辨率
|
||||
if (!isLowRes)
|
||||
{
|
||||
isLowRes = true;
|
||||
CreateColorWheelTexture(true);
|
||||
ApplyTextureToRawImage();
|
||||
}
|
||||
|
||||
if (valueSliderCoroutine != null)
|
||||
StopCoroutine(valueSliderCoroutine);
|
||||
valueSliderCoroutine = StartCoroutine(DelayedRedrawColorWheel());
|
||||
|
||||
// 刷新当前颜色
|
||||
if (pickerIndicator != null)
|
||||
UpdateCurrentColor(pickerIndicator.localPosition);
|
||||
}
|
||||
|
||||
IEnumerator DelayedRedrawColorWheel()
|
||||
{
|
||||
float waitTime = 0.01f;
|
||||
float timer = 0f;
|
||||
|
||||
while (timer < waitTime)
|
||||
{
|
||||
yield return null;
|
||||
timer += Time.unscaledDeltaTime;
|
||||
}
|
||||
// 恢复高分辨率
|
||||
if (isLowRes)
|
||||
{
|
||||
isLowRes = false;
|
||||
CreateColorWheelTexture(false);
|
||||
ApplyTextureToRawImage();
|
||||
}
|
||||
else
|
||||
{
|
||||
CreateColorWheelTexture(false);
|
||||
ApplyTextureToRawImage();
|
||||
}
|
||||
valueSliderCoroutine = null;
|
||||
}
|
||||
|
||||
void OnAlphaSliderChanged(float newAlpha)
|
||||
{
|
||||
// 只更新透明度
|
||||
@@ -82,6 +127,7 @@ public partial class HsvDrawer
|
||||
{
|
||||
StartCoroutine(DraggingColorPicker());
|
||||
}
|
||||
|
||||
}
|
||||
IEnumerator DraggingColorPicker()
|
||||
{
|
||||
@@ -112,47 +158,44 @@ public partial class HsvDrawer
|
||||
yield return null; // 等待下一帧
|
||||
}
|
||||
}
|
||||
void CreateColorWheelTexture()
|
||||
// 整合分辨率逻辑到此方法
|
||||
void CreateColorWheelTexture(bool useLowRes = false)
|
||||
{
|
||||
// 创建方形纹理
|
||||
_texture = new Texture2D(textureSize, textureSize, TextureFormat.RGBA32, false);
|
||||
int targetSize = useLowRes ? lowResTextureSize : highResTextureSize;
|
||||
textureSize = targetSize;
|
||||
|
||||
if (_texture == null || _texture.width != textureSize || _texture.height != textureSize)
|
||||
_texture = new Texture2D(textureSize, textureSize, TextureFormat.RGBA32, false);
|
||||
|
||||
_texture.wrapMode = TextureWrapMode.Clamp;
|
||||
|
||||
Vector2 center = new Vector2(textureSize / 2f, textureSize / 2f);
|
||||
float maxRadius = textureSize / 2f;
|
||||
Vector2 center = new Vector2(textureSize * 0.5f, textureSize * 0.5f);
|
||||
float maxRadius = textureSize * 0.5f;
|
||||
Color[] pixels = new Color[textureSize * textureSize];
|
||||
|
||||
// 遍历所有像素
|
||||
for (int y = 0; y < textureSize; y++)
|
||||
{
|
||||
for (int x = 0; x < textureSize; x++)
|
||||
{
|
||||
Vector2 pixelPos = new Vector2(x, y);
|
||||
Vector2 offset = pixelPos - center;
|
||||
Vector2 offset = new Vector2(x + 0.5f, y + 0.5f) - center;
|
||||
float distance = offset.magnitude;
|
||||
int idx = y * textureSize + x;
|
||||
|
||||
// 在圆形区域内绘制
|
||||
if (distance <= maxRadius)
|
||||
{
|
||||
// 计算角度(0-360°)
|
||||
float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
|
||||
if (angle < 0) angle += 360;
|
||||
|
||||
// 计算饱和度(基于半径比例)
|
||||
if (angle < 0) angle += 360f;
|
||||
float sat = Mathf.Clamp01(distance / maxRadius) * saturation;
|
||||
|
||||
// HSV转RGB
|
||||
Color color = Color.HSVToRGB(angle / 360f, sat, value);
|
||||
_texture.SetPixel(x, y, color);
|
||||
pixels[idx] = Color.HSVToRGB(angle / 360f, sat, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 圆形外透明
|
||||
_texture.SetPixel(x, y, Color.clear);
|
||||
pixels[idx] = Color.clear;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_texture.Apply(); // 应用像素更改
|
||||
_texture.SetPixels(pixels);
|
||||
_texture.Apply();
|
||||
}
|
||||
|
||||
void ApplyTextureToRawImage()
|
||||
@@ -207,7 +250,7 @@ public partial class HsvDrawer
|
||||
if (baseColorPicker != null)
|
||||
{
|
||||
// 同步到BaseColorPicker
|
||||
baseColorPicker.SliderChangeWithoutNofication(currentColor);
|
||||
baseColorPicker.SliderChangeWithoutNotification(currentColor);
|
||||
}
|
||||
ApplyParameters();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user