forked from Syomus/ProceduralToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PTUtils.cs
185 lines (173 loc) · 7.13 KB
/
PTUtils.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using ProceduralToolkit.FastNoiseLib;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace ProceduralToolkit
{
/// <summary>
/// Various useful methods and constants
/// </summary>
public static class PTUtils
{
/// <summary>
/// Lowercase letters from a to z
/// </summary>
public const string LowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
/// <summary>
/// Uppercase letters from A to Z
/// </summary>
public const string UppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/// <summary>
/// Digits from 0 to 9
/// </summary>
public const string Digits = "0123456789";
/// <summary>
/// The concatenation of the strings <see cref="LowercaseLetters"/> and <see cref="UppercaseLetters"/>
/// </summary>
public const string Letters = LowercaseLetters + UppercaseLetters;
/// <summary>
/// The concatenation of the strings <see cref="Letters"/> and <see cref="Digits"/>
/// </summary>
public const string Alphanumerics = Letters + Digits;
/// <summary>
/// Square root of 0.5
/// </summary>
public const float Sqrt05 = 0.7071067811865475244f;
/// <summary>
/// Square root of 2
/// </summary>
public const float Sqrt2 = 1.4142135623730950488f;
/// <summary>
/// Square root of 5
/// </summary>
public const float Sqrt5 = 2.2360679774997896964f;
/// <summary>
/// Golden angle in radians
/// </summary>
public const float GoldenAngle = Mathf.PI*(3 - Sqrt5);
/// <summary>
/// Swaps values of <paramref name="left"/> and <paramref name="right"/>
/// </summary>
public static void Swap<T>(ref T left, ref T right)
{
T temp = left;
left = right;
right = temp;
}
/// <summary>
/// Knapsack problem solver for items with equal value
/// </summary>
/// <typeparam name="T">Item identificator</typeparam>
/// <param name="set">
/// Set of items where key <typeparamref name="T"/> is item identificator and value is item weight</param>
/// <param name="capacity">Maximum weight</param>
/// <param name="knapsack">Pre-filled knapsack</param>
/// <returns>
/// Filled knapsack where values are number of items of type key.
/// Tends to overload knapsack: fills remainder with one smallest item.</returns>
/// <remarks>
/// https://en.wikipedia.org/wiki/Knapsack_problem
/// </remarks>
public static Dictionary<T, int> Knapsack<T>(Dictionary<T, float> set, float capacity,
Dictionary<T, int> knapsack = null)
{
var keys = new List<T>(set.Keys);
// Sort keys by their weights in descending order
keys.Sort((a, b) => -set[a].CompareTo(set[b]));
if (knapsack == null)
{
knapsack = new Dictionary<T, int>();
foreach (var key in keys)
{
knapsack[key] = 0;
}
}
return Knapsack(set, keys, capacity, knapsack, 0);
}
private static Dictionary<T, int> Knapsack<T>(Dictionary<T, float> set, List<T> keys, float remainder,
Dictionary<T, int> knapsack, int startIndex)
{
T smallestKey = keys[keys.Count - 1];
if (remainder < set[smallestKey])
{
knapsack[smallestKey] = 1;
return knapsack;
}
// Cycle through items and try to put them in knapsack
for (var i = startIndex; i < keys.Count; i++)
{
T key = keys[i];
float weight = set[key];
// Larger items won't fit, smaller items will fill as much space as they can
knapsack[key] += (int) (remainder/weight);
remainder %= weight;
}
if (remainder > 0)
{
// Throw out largest item and try again
for (var i = 0; i < keys.Count; i++)
{
T key = keys[i];
if (knapsack[key] != 0)
{
// Already tried every combination, return as is
if (key.Equals(smallestKey))
{
return knapsack;
}
knapsack[key]--;
remainder += set[key];
startIndex = i + 1;
break;
}
}
knapsack = Knapsack(set, keys, remainder, knapsack, startIndex);
}
return knapsack;
}
public static string ToString(this Vector3 vector, string format, IFormatProvider formatProvider)
{
return string.Format("({0}, {1}, {2})", vector.x.ToString(format, formatProvider), vector.y.ToString(format, formatProvider),
vector.z.ToString(format, formatProvider));
}
public static string ToString(this Quaternion quaternion, string format, IFormatProvider formatProvider)
{
return string.Format("({0}, {1}, {2}, {3})", quaternion.x.ToString(format, formatProvider), quaternion.y.ToString(format, formatProvider),
quaternion.z.ToString(format, formatProvider), quaternion.w.ToString(format, formatProvider));
}
public static void ApplyProperties(this Renderer renderer, RendererProperties properties)
{
renderer.lightProbeUsage = properties.lightProbeUsage;
renderer.lightProbeProxyVolumeOverride = properties.lightProbeProxyVolumeOverride;
renderer.reflectionProbeUsage = properties.reflectionProbeUsage;
renderer.probeAnchor = properties.probeAnchor;
renderer.shadowCastingMode = properties.shadowCastingMode;
renderer.receiveShadows = properties.receiveShadows;
renderer.motionVectorGenerationMode = properties.motionVectorGenerationMode;
}
public static MeshRenderer CreateMeshRenderer(string name, out MeshFilter meshFilter)
{
var gameObject = new GameObject(name);
meshFilter = gameObject.AddComponent<MeshFilter>();
var meshRenderer = gameObject.AddComponent<MeshRenderer>();
return meshRenderer;
}
public static Texture2D CreateTexture(int width, int height, Color clearColor)
{
var texture = new Texture2D(width, height, TextureFormat.ARGB32, false, true)
{
filterMode = FilterMode.Point
};
texture.Clear(clearColor);
texture.Apply();
return texture;
}
/// <summary>
/// Returns a noise value between 0.0 and 1.0
/// </summary>
public static float GetNoise01(this FastNoise noise, float x, float y)
{
return Mathf.Clamp01(noise.GetNoise(x, y)*0.5f + 0.5f);
}
}
}