-
Notifications
You must be signed in to change notification settings - Fork 42
/
OverlapWFC.cs
176 lines (163 loc) · 4.83 KB
/
OverlapWFC.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
using System;
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
[ExecuteInEditMode]
class OverlapWFC : MonoBehaviour{
public Training training = null;
public int gridsize = 1;
public int width = 20;
public int depth = 20;
public int seed = 0;
//[HideInInspector]
public int N = 2;
public bool periodicInput = false;
public bool periodicOutput = false;
public int symmetry = 1;
public int foundation = 0;
public int iterations = 0;
public bool incremental = false;
public OverlappingModel model = null;
public GameObject[,] rendering;
public GameObject output;
private Transform group;
private bool undrawn = true;
public static bool IsPrefabRef(UnityEngine.Object o){
#if UNITY_EDITOR
return PrefabUtility.GetOutermostPrefabInstanceRoot(o) != null;
#else
return true;
#endif
}
static GameObject CreatePrefab(UnityEngine.Object fab, Vector3 pos, Quaternion rot) {
#if UNITY_EDITOR
GameObject e = PrefabUtility.InstantiatePrefab(fab as GameObject) as GameObject;
e.transform.position = pos;
e.transform.rotation = rot;
return e;
#else
GameObject o = GameObject.Instantiate(fab as GameObject) as GameObject;
o.transform.position = pos;
o.transform.rotation = rot;
return o;
#endif
}
public void Clear(){
if (group != null){
if (Application.isPlaying){Destroy(group.gameObject);} else {
DestroyImmediate(group.gameObject);
}
group = null;
}
}
void Awake(){}
void Start(){
Generate();
}
void Update(){
if (incremental){
Run();
}
}
public void Generate() {
if (training == null){Debug.Log("Can't Generate: no designated Training component");}
if (IsPrefabRef(training.gameObject)){
GameObject o = CreatePrefab(training.gameObject, new Vector3(0,99999f,0f), Quaternion.identity);
training = o.GetComponent<Training>();
}
if (training.sample == null){
training.Compile();
}
if (output == null){
Transform ot = transform.Find("output-overlap");
if (ot != null){output = ot.gameObject;}}
if (output == null){
output = new GameObject("output-overlap");
output.transform.parent = transform;
output.transform.position = this.gameObject.transform.position;
output.transform.rotation = this.gameObject.transform.rotation;}
for (int i = 0; i < output.transform.childCount; i++){
GameObject go = output.transform.GetChild(i).gameObject;
if (Application.isPlaying){Destroy(go);} else {DestroyImmediate(go);}
}
group = new GameObject(training.gameObject.name).transform;
group.parent = output.transform;
group.position = output.transform.position;
group.rotation = output.transform.rotation;
group.localScale = new Vector3(1f, 1f, 1f);
rendering = new GameObject[width, depth];
model = new OverlappingModel(training.sample, N, width, depth, periodicInput, periodicOutput, symmetry, foundation);
undrawn = true;
}
void OnDrawGizmos(){
Gizmos.color = Color.cyan;
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawWireCube(new Vector3(width*gridsize/2f-gridsize*0.5f, depth*gridsize/2f-gridsize*0.5f, 0f),
new Vector3(width*gridsize, depth*gridsize, gridsize));
}
public void Run(){
if (model == null){return;}
if (undrawn == false) { return; }
if (model.Run(seed, iterations)){
Draw();
}
}
public GameObject GetTile(int x, int y){
return rendering[x,y];
}
public void Draw(){
if (output == null){return;}
if (group == null){return;}
undrawn = false;
try{
for (int y = 0; y < depth; y++){
for (int x = 0; x < width; x++){
if (rendering[x,y] == null){
int v = (int)model.Sample(x, y);
if (v != 99 && v < training.tiles.Length){
Vector3 pos = new Vector3(x*gridsize, y*gridsize, 0f);
int rot = (int)training.RS[v];
GameObject fab = training.tiles[v] as GameObject;
if (fab != null){
GameObject tile = (GameObject)Instantiate(fab, new Vector3() , Quaternion.identity);
Vector3 fscale = tile.transform.localScale;
tile.transform.parent = group;
tile.transform.localPosition = pos;
tile.transform.localEulerAngles = new Vector3(0, 0, 360 - (rot * 90));
tile.transform.localScale = fscale;
rendering[x,y] = tile;
}
} else
{
undrawn = true;
}
}
}
}
} catch (IndexOutOfRangeException) {
model = null;
return;
}
}
}
#if UNITY_EDITOR
[CustomEditor (typeof(OverlapWFC))]
public class WFCGeneratorEditor : Editor {
public override void OnInspectorGUI () {
OverlapWFC generator = (OverlapWFC)target;
if (generator.training != null){
if(GUILayout.Button("generate")){
generator.Generate();
}
if (generator.model != null){
if(GUILayout.Button("RUN")){
generator.Run();
}
}
}
DrawDefaultInspector ();
}
}
#endif