add: Item Manager
This commit is contained in:
9
Package/embedded/Miscs/SkinDyeMaterial.tres
Normal file
9
Package/embedded/Miscs/SkinDyeMaterial.tres
Normal file
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://cegp8r6cj3jqj"]
|
||||
|
||||
[ext_resource type="Shader" path="res://embedded/Miscs/SkinDyeShader.gdshader" id="1_31kjt"]
|
||||
|
||||
[resource]
|
||||
shader = ExtResource("1_31kjt")
|
||||
shader_parameter/old_palette = null
|
||||
shader_parameter/new_palette = null
|
||||
shader_parameter/glowing = null
|
||||
50
Package/embedded/Miscs/SkinDyeShader.gdshader
Normal file
50
Package/embedded/Miscs/SkinDyeShader.gdshader
Normal file
@@ -0,0 +1,50 @@
|
||||
shader_type canvas_item;
|
||||
|
||||
uniform vec4 old_palette[32];
|
||||
uniform vec4 new_palette[32];
|
||||
uniform int glowing[32];
|
||||
const float pe = 0.004;
|
||||
|
||||
struct DyeResult{
|
||||
vec4 color;
|
||||
int index;
|
||||
};
|
||||
|
||||
DyeResult dye(vec4 color){
|
||||
for(int i = 0; i<32; i++){
|
||||
if(distance(color, old_palette[i]) < pe){
|
||||
return DyeResult(new_palette[i], i);
|
||||
}
|
||||
}
|
||||
return DyeResult(color, -1);
|
||||
}
|
||||
|
||||
vec4 rgb2hsv(vec4 inp){
|
||||
vec3 c = inp.rgb;
|
||||
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
|
||||
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
|
||||
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
|
||||
float d = q.x - min(q.w, q.y);
|
||||
float e = 1.0e-10;
|
||||
return vec4(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x, inp.a);
|
||||
}
|
||||
|
||||
vec4 hsv2rgb(vec4 inp){
|
||||
vec3 c = inp.xyz;
|
||||
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
|
||||
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
|
||||
return vec4( c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y), inp.a);
|
||||
}
|
||||
|
||||
void fragment() {
|
||||
DyeResult dy = dye(texture(TEXTURE, UV));
|
||||
if(dy.index != -1 && glowing[dy.index] == 1){
|
||||
vec4 hsv = rgb2hsv(dy.color);
|
||||
hsv.z += sin(TIME*1.5)*0.1;
|
||||
COLOR = hsv2rgb(hsv);
|
||||
}
|
||||
else{
|
||||
COLOR = dy.color;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ using Polonium.Attributes;
|
||||
using Polonium;
|
||||
using Polonium.Interfaces;
|
||||
using System.Collections.Generic;
|
||||
using Polonium.Agents;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
public static partial class GlobalRegistry
|
||||
@@ -28,21 +29,18 @@ public static partial class GlobalRegistry
|
||||
|
||||
public static class Asset<T> where T : Node
|
||||
{
|
||||
private static readonly Queue<T> Pool = new();
|
||||
// ReSharper disable once StaticMemberInGenericType
|
||||
// ReSharper disable once UnusedAutoPropertyAccessor.Global
|
||||
public static PackedScene Scene { get; set; }
|
||||
private static T Instance => Scene.Instantiate<T>();
|
||||
public static T Get() => Pool.Count > 0 ? Pool.Dequeue() : Instance;
|
||||
|
||||
public static void Return(T obj)
|
||||
{
|
||||
if(Pool.Count < 10)
|
||||
Pool.Enqueue(obj);
|
||||
else
|
||||
obj.QueueFree();
|
||||
}
|
||||
public static PackedScene Scene => PoloniumRegistry.Asset<T>.Scene;
|
||||
public static T Get() => PoloniumRegistry.Asset<T>.Get();
|
||||
public static void Return(T obj) => PoloniumRegistry.Asset<T>.Return(obj);
|
||||
}
|
||||
|
||||
public static partial class Action<TAction> where TAction : AgentAction
|
||||
{
|
||||
public static string Name => PoloniumRegistry.Action<TAction>.Name;
|
||||
}
|
||||
|
||||
public static PoloniumRegistry PoloniumRegistry => PoloniumRegistry.Instance;
|
||||
public static bool Paused { get; set; }
|
||||
public static void Prepare() => PoloniumRegistry.Prepare();
|
||||
|
||||
151
Package/embedded/Patches/SkinPaletteRegister.p.cs
Normal file
151
Package/embedded/Patches/SkinPaletteRegister.p.cs
Normal file
@@ -0,0 +1,151 @@
|
||||
using System.Reflection;
|
||||
using Godot;
|
||||
using Polonium;
|
||||
using Polonium.Attributes;
|
||||
using Polonium.SkinManagers.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Skin = Polonium.SkinManagers.Skin;
|
||||
|
||||
[AutoRegister]
|
||||
// ReSharper disable once CheckNamespace
|
||||
public static class SkinPaletteRegister
|
||||
{
|
||||
public static void Register()
|
||||
{
|
||||
Type gr = typeof(PoloniumRegistry.SkinRegistry<>);
|
||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||
IEnumerable<Type> skinTypes = assembly
|
||||
.GetTypes()
|
||||
.Where(t => t.IsSubclassOf(typeof(Skin)) && !t.IsAbstract)
|
||||
.Where(t => t.GetCustomAttributes(typeof(SkinInfo), true).Any());
|
||||
|
||||
foreach (Type skinType in skinTypes)
|
||||
{
|
||||
Type ist = gr.MakeGenericType(skinType);
|
||||
string pth = skinType.GetCustomAttribute<SkinInfo>()!.Path;
|
||||
int k = skinType.GetCustomAttribute<SkinInfo>()!.Roots;
|
||||
|
||||
PropertyInfo paletteRoots = ist.GetProperty("PaletteRoots");
|
||||
PropertyInfo paletteMap = ist.GetProperty("PaletteMap");
|
||||
Color[] roots;
|
||||
Dictionary<Color, Color[]> map;
|
||||
if (IsAutoPalette(skinType))
|
||||
(roots, map) = KMean(ExtractColors(pth), k);
|
||||
else
|
||||
(roots, map) = GetPalette(pth);
|
||||
paletteRoots!.SetValue(null, roots);
|
||||
paletteMap!.SetValue(null, map);
|
||||
}
|
||||
}
|
||||
private static bool IsAutoPalette(Type type)
|
||||
{
|
||||
Type x = type;
|
||||
while (x != typeof(object) && x != null)
|
||||
{
|
||||
if (x.IsGenericType && x.GetGenericTypeDefinition() == typeof(AutoPaletteSkin<>))
|
||||
return true;
|
||||
x = x.BaseType;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static HashSet<Color> ExtractColors(string path)
|
||||
{
|
||||
HashSet<Color> colors = new();
|
||||
CompressedTexture2D texture = ResourceLoader.Load<CompressedTexture2D>(path);
|
||||
Image image = texture.GetImage();
|
||||
for (int x = 0; x < image.GetWidth(); x++)
|
||||
for (int y = 0; y < image.GetHeight(); y++)
|
||||
{
|
||||
Color px = image.GetPixel(x, y);
|
||||
if (px.A8 > 0)
|
||||
colors.Add(px);
|
||||
}
|
||||
|
||||
return colors;
|
||||
}
|
||||
|
||||
private static (Color[], Dictionary<Color, Color[]>) GetPalette(string path)
|
||||
{
|
||||
//HashSet<Color> colors = ExtractColors(path);
|
||||
|
||||
HashSet<Color> colors = new();
|
||||
Dictionary<Color, HashSet<Color>> map = new();
|
||||
|
||||
CompressedTexture2D texture = ResourceLoader.Load<CompressedTexture2D>(path);
|
||||
Image image = texture.GetImage();
|
||||
int height = image.GetHeight();
|
||||
int width = image.GetWidth();
|
||||
for (int y = height - 32; y < height; y++)
|
||||
{
|
||||
Color px0 = image.GetPixel(width-32, y);
|
||||
if (px0.A8 == 0)
|
||||
break;
|
||||
colors.Add(px0);
|
||||
map[px0] = new HashSet<Color> { px0 };
|
||||
for (int x = width - 32; x < width; x++)
|
||||
{
|
||||
Color px = image.GetPixel(x, y);
|
||||
if (px.A8 == 0)
|
||||
break;
|
||||
map[px0].Add(px);
|
||||
}
|
||||
}
|
||||
|
||||
return (colors.ToArray(), map.ToDictionary(p => p.Key, p => p.Value.ToArray()));
|
||||
}
|
||||
private static (Color[], Dictionary<Color, Color[]>) KMean(HashSet<Color> colors, int k)
|
||||
{
|
||||
var colorPoints = colors.ToList();
|
||||
var random = new Random();
|
||||
Color[] centroids = colorPoints.OrderBy(x => random.Next()).Take(k).ToArray();
|
||||
|
||||
bool hasConverged = false;
|
||||
Dictionary<Color, List<Color>> clusters = new();
|
||||
|
||||
while (!hasConverged)
|
||||
{
|
||||
clusters = centroids.ToDictionary(c => c, c => new List<Color>());
|
||||
foreach (Color color in colorPoints)
|
||||
{
|
||||
Color nearestCentroid = centroids.OrderBy(c => c.DistanceTo(color)).First();
|
||||
clusters[nearestCentroid].Add(color);
|
||||
}
|
||||
hasConverged = true;
|
||||
for (int i = 0; i < centroids.Length; i++)
|
||||
{
|
||||
if (clusters[centroids[i]].Count == 0) continue;
|
||||
|
||||
Color newCentroid = clusters[centroids[i]].AverageColor();
|
||||
if (!newCentroid.Equals(centroids[i]))
|
||||
{
|
||||
centroids[i] = newCentroid;
|
||||
hasConverged = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Dictionary<Color, Color[]> resultMap = clusters.ToDictionary(
|
||||
kvp => kvp.Key,
|
||||
kvp => kvp.Value.ToArray()
|
||||
);
|
||||
|
||||
return (centroids, resultMap);
|
||||
}
|
||||
|
||||
private static Vector4 ToVector(this Color a) => new(a.R, a.G, a.B, a.A);
|
||||
private static double DistanceTo(this Color a, Color b) => a.ToVector().DistanceTo(b.ToVector());
|
||||
private static Color ToColor(this Vector4 a) => new Color(a.X, a.Y, a.Z, a.W);
|
||||
|
||||
private static Color AverageColor(this IEnumerable<Color> a)
|
||||
{
|
||||
Vector4[] aa = a.Select(x => x.ToVector()).ToArray();
|
||||
Vector4 res = Vector4.Zero;
|
||||
foreach (Vector4 c in aa)
|
||||
res += c;
|
||||
return (res / aa.Length).ToColor();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user