@@ -83,27 +83,73 @@ int countLiveNeighbors(ivec2 pos, int targetType) {
83
83
}
84
84
85
85
int determineNewType(ivec2 pos) {
86
- int greenNeighbors = countLiveNeighbors(pos, 1); // Зелёные
87
- int redNeighbors = countLiveNeighbors(pos, 2); // Красные
88
- int blueNeighbors = countLiveNeighbors(pos, 3); // Синие
89
-
90
- if (greenNeighbors > redNeighbors && greenNeighbors > blueNeighbors) {
91
- return 1; // Зелёный
92
- } else if (redNeighbors > greenNeighbors && redNeighbors > blueNeighbors) {
93
- return 2; // Красный
94
- } else if (blueNeighbors > greenNeighbors && blueNeighbors > redNeighbors) {
95
- return 3; // Синий
96
- } else if (greenNeighbors == redNeighbors && greenNeighbors > blueNeighbors) {
97
- return 4; // Жёлтый (зелёный = красный)
98
- } else if (greenNeighbors == blueNeighbors && greenNeighbors > redNeighbors) {
99
- return 5; // Оранжевый (зелёный = синий)
100
- } else if (redNeighbors == blueNeighbors && redNeighbors > greenNeighbors) {
101
- return 6; // Фиолетовый (красный = синий)
102
- } else { // Все три равны
103
- return 7; // Белый (зелёный = красный = синий)
86
+ int neighborsCount[8];
87
+ for (int i = 1; i <= 7; i++) {
88
+ neighborsCount[i] = countLiveNeighbors(pos, i);
89
+ }
90
+
91
+ int maxCount = 0;
92
+ int equalTypes[8];
93
+ int equalCount = 0;
94
+ for (int i = 1; i <= 7; i++) {
95
+ if (neighborsCount[i] > maxCount) {
96
+ maxCount = neighborsCount[i];
97
+ equalCount = 0;
98
+ equalTypes[equalCount] = i; // Сохраняем новый макс. тип
99
+ equalCount++;
100
+ } else if (neighborsCount[i] == maxCount && maxCount != 0) {
101
+ equalTypes[equalCount] = i; // Сохраняем равный тип
102
+ equalCount++;
103
+ }
104
+ }
105
+ if(maxCount == 0) return 0; //если нет соседей, то умирает
106
+
107
+ if (equalCount > 1) {
108
+ int randomIndex = int(float(equalCount) * fract(sin(dot(pos, vec2(12.9898, 78.233))) * 43758.5453));
109
+ return equalTypes[randomIndex]; // Возвращаем случайный тип из равных
110
+ } else {
111
+ return equalTypes[0]; // Возвращаем единственный максимальный тип
104
112
}
105
113
}
106
114
115
+ // Метод для генерации цвета на основе типа клетки.
116
+ vec4 getColorByType(int type, int currentState) {
117
+ if (type == 0) {
118
+ return vec4(0.05, 0.05, 0.08, 0.0); // Мертвая клетка
119
+ }
120
+ if (type < 0) {
121
+ return vec4(0.0, 0.0, 0.0, 0.0); // Пустая клетка
122
+ }
123
+
124
+
125
+ vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
126
+ float increase = 0.02;
127
+
128
+ int t = type;
129
+ int r = (t / 4) % 2;
130
+ int g = (t / 2) % 2;
131
+ int b = (t / 1) % 2;
132
+
133
+ color.r = float(r);
134
+ color.g = float(g);
135
+ color.b = float(b);
136
+
137
+ if(currentState > 0 && currentState == type){
138
+ vec4 currentColor = colors[gl_GlobalInvocationID.y * gridSize.x + gl_GlobalInvocationID.x];
139
+ color.r = min(currentColor.r + increase, color.r);
140
+ color.g = min(currentColor.g + increase, color.g);
141
+ color.b = min(currentColor.b + increase, color.b);
142
+ }
143
+ if (type > 7){
144
+ color = vec4(1.0,1.0,1.0,0.0);
145
+ }
146
+ if(type > 0){
147
+ color = clamp(color,vec4(0,0,0,0),vec4(1,1,1,0));
148
+ }
149
+ return color;
150
+ }
151
+
152
+
107
153
void main() {
108
154
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
109
155
if (pos.x >= gridSize.x || pos.y >= gridSize.y) return;
@@ -129,103 +175,15 @@ void main() {
129
175
} else {
130
176
nextState = 0; // Умирает
131
177
}
132
- } else if (currentState = = 0) { // Мёртвая клетка
178
+ } else if (currentState < = 0) { // Мёртвая клетка
133
179
if (neighbors == birth) {
134
180
nextState = determineNewType(pos); // Оживает с типом
135
181
}
136
182
}
137
183
}
138
184
139
185
next[index] = nextState;
140
-
141
- // Обновляем цвета в зависимости от типа
142
- if (nextState == 1) { // Зелёный
143
- if (currentState == 1) {
144
- vec4 currentColor = colors[index];
145
- colors[index] = vec4(
146
- min(currentColor.r + 0.02, 0.02),
147
- min(currentColor.g + 0.02, 1.0),
148
- min(currentColor.b + 0.02, 0.02),
149
- 1.0
150
- );
151
- } else {
152
- colors[index] = vec4(0.0, 0.5, 0.0, 1.0); // Начальный зелёный
153
- }
154
- } else if (nextState == 2) { // Красный
155
- if (currentState == 2) {
156
- vec4 currentColor = colors[index];
157
- colors[index] = vec4(
158
- min(currentColor.r + 0.02, 1.0),
159
- min(currentColor.g + 0.02, 0.02),
160
- min(currentColor.b + 0.02, 0.02),
161
- 1.0
162
- );
163
- } else {
164
- colors[index] = vec4(0.5, 0.0, 0.0, 1.0); // Начальный красный
165
- }
166
- } else if (nextState == 3) { // Синий
167
- if (currentState == 3) {
168
- vec4 currentColor = colors[index];
169
- colors[index] = vec4(
170
- min(currentColor.r + 0.02, 0.02),
171
- min(currentColor.g + 0.02, 0.02),
172
- min(currentColor.b + 0.02, 1.0),
173
- 1.0
174
- );
175
- } else {
176
- colors[index] = vec4(0.0, 0.0, 0.5, 1.0); // Начальный синий
177
- }
178
- } else if (nextState == 4) { // Жёлтый (зелёный = красный)
179
- if (currentState == 4) {
180
- vec4 currentColor = colors[index];
181
- colors[index] = vec4(
182
- min(currentColor.r + 0.02, 1.0),
183
- min(currentColor.g + 0.02, 1.0),
184
- min(currentColor.b + 0.02, 0.02),
185
- 1.0
186
- );
187
- } else {
188
- colors[index] = vec4(0.5, 0.5, 0.0, 1.0); // Начальный жёлтый
189
- }
190
- } else if (nextState == 5) { // Оранжевый (зелёный = синий)
191
- if (currentState == 5) {
192
- vec4 currentColor = colors[index];
193
- colors[index] = vec4(
194
- min(currentColor.r + 0.02, 1.0),
195
- min(currentColor.g + 0.02, 0.02),
196
- min(currentColor.b + 0.02, 1.0),
197
- 1.0
198
- );
199
- } else {
200
- colors[index] = vec4(0.5, 0.0, 0.5, 1.0); // Начальный оранжевый
201
- }
202
- } else if (nextState == 6) { // Фиолетовый (красный = синий)
203
- if (currentState == 6) {
204
- vec4 currentColor = colors[index];
205
- colors[index] = vec4(
206
- min(currentColor.r + 0.02, 1.0),
207
- min(currentColor.g + 0.02, 0.02),
208
- min(currentColor.b + 0.02, 1.0),
209
- 1.0
210
- );
211
- } else {
212
- colors[index] = vec4(0.0, 0.5, 0.5, 1.0); // Начальный фиолетовый
213
- }
214
- } else if (nextState == 7) { // Белый (зелёный = красный = синий)
215
- if (currentState == 7) {
216
- vec4 currentColor = colors[index];
217
- colors[index] = vec4(
218
- min(currentColor.r + 0.02, 1.0),
219
- min(currentColor.g + 0.02, 1.0),
220
- min(currentColor.b + 0.02, 1.0),
221
- 1.0
222
- );
223
- } else {
224
- colors[index] = vec4(1.0, 1.0, 1.0, 1.0); // Начальный белый
225
- }
226
- } else if (currentState > 0) { // Клетка умерла
227
- colors[index] = vec4(0.05, 0.05, 0.08, 0.0);
228
- }
186
+ colors[index] = getColorByType(nextState, currentState);
229
187
}
230
188
231
189
)" ;
@@ -365,7 +323,7 @@ void main() {
365
323
colors[index] = vec4(0.9, 0.9, 0.9, 1.0);
366
324
}
367
325
} else {
368
- cells[index] = 0 ; // Мёртвая клетка
326
+ cells[index] = -1 ; // пустая клетка
369
327
colors[index] = vec4(0.0, 0.0, 0.0, 0.0); // Чёрный
370
328
}
371
329
}
@@ -474,11 +432,6 @@ void GPUAutomaton::SetCellState(int x, int y, int state) {
474
432
std::cerr << " SetCellState: Invalid coordinates (" << x << " , " << y << " )" << std::endl;
475
433
return ;
476
434
}
477
- if (state != 0 && state != 1 ) {
478
- std::cerr << " SetCellState: Invalid state value (" << state << " ). Use 0 (dead) or 1 (alive)." << std::endl;
479
- return ;
480
- }
481
-
482
435
int index = y * gridWidth + x;
483
436
int stateData = state;
484
437
// glFinish();
0 commit comments