forked from ica25-origins/origins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
1536 lines (1352 loc) · 45.4 KB
/
index.php
File metadata and controls
1536 lines (1352 loc) · 45.4 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>ICA25</title>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.172.0/build/three.module.js",
"gsap": "https://cdn.jsdelivr.net/npm/gsap@3.12.2/+esm",
"cannon-es": "https://cdn.jsdelivr.net/npm/cannon-es@0.20.0/dist/cannon-es.js"
}
}
</script>
</head>
<body style="overflow:hidden; font-family:sans-serif">
<div style="position:fixed; top:1vh; left:1vw; width:6vw; background-color:transparent; z-index:10">
<img src="keyst.png" style="width:100%">
</div>
<div style="position:fixed; top:1vh; right:1vw; background-color:white; color:#222; border:1px solid #222; padding:5px; z-index:99; text-align:center">
<b>Information</b><br><br>
<div id="info"></div>
<br>
</div>
<div id="headdiv" style="position:fixed; top:3px; left:0px; width:100vw; color:yellow; font-weight:bold; text-align:center; font-size:2em; z-index:99"></div>
<script type="module">
import * as THREE from 'three';
import * as CANNON from "cannon-es";
import * as BufferGeometryUtils from 'https://cdn.jsdelivr.net/npm/three@0.172.0/examples/jsm/utils/BufferGeometryUtils.js';
const world = new CANNON.World({
gravity: new CANNON.Vec3(0, -9.82, 0)
});
class TerrainGenerator {
constructor(seed = 12345) {
this.seed = seed;
}
hash(x, z) {
let h = (x * 374761393 + z * 668265263 + this.seed) % 2147483647;
h = (h ^ (h >> 13)) * 1274126177;
return ((h ^ (h >> 16)) % 2147483647) / 2147483647;
}
// Smooth interpolation between values
smoothstep(t) {
return t * t * (3 - 2 * t);
}
// 2D noise function
noise(x, z) {
const ix = Math.floor(x);
const iz = Math.floor(z);
const fx = x - ix;
const fz = z - iz;
// Get corner values
const a = this.hash(ix, iz);
const b = this.hash(ix + 1, iz);
const c = this.hash(ix, iz + 1);
const d = this.hash(ix + 1, iz + 1);
// Interpolate
const i1 = a + this.smoothstep(fx) * (b - a);
const i2 = c + this.smoothstep(fx) * (d - c);
return i1 + this.smoothstep(fz) * (i2 - i1);
}
getHeight(x, z) {
let height = 0;
let amplitude = 100; // Max height variation
let frequency = 0.01; // How spread out features are
// Layer multiple octaves for natural terrain
for (let i = 0; i < 4; i++) {
height += this.noise(x * frequency, z * frequency) * amplitude;
amplitude *= 0.5; // Each octave contributes less
frequency *= 2; // Each octave has finer detail
}
return height/6;
}
}
const terrain = new TerrainGenerator(Math.random()*100);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
45, window.innerWidth / window.innerHeight, 0.1, 2000);
camera.position.set(6, 36, 73);
camera.lookAt(0, 0, 0);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor('#66B3FF');
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
let tcounter = 0;
let dcounter=0;
let goodbad=0; // 0 = good, 1 = bad
let gods=['Agnostic','Gemini','Claude','Amazon-Nova','GPT'];
let gcol=[0,60,300,240];
let glast = ['','','','',''];
const size = 128;
const segments = 512;
const geometry = new THREE.PlaneGeometry(size, size, segments, segments);
geometry.rotateX(-Math.PI / 2); // rotate plane horizontal (XZ plane)
const positionAttribute = geometry.attributes.position;
for (let i = 0; i < positionAttribute.count; i++) {
const x = positionAttribute.getX(i);
const z = positionAttribute.getZ(i);
positionAttribute.setY(i,terrain.getHeight(x, z));
}
geometry.computeBoundingSphere();
geometry.attributes.position.needsUpdate = true;
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
color: 0x55bb2f,
wireframe: false,
flatShading: true,
side: THREE.DoubleSide
});
const ground = new THREE.Mesh(geometry, material);
scene.add(ground);
ground.receiveShadow = true;
const ambientLight = new THREE.AmbientLight(0xffffff, 0.3);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(50, 100, 50);
scene.add(directionalLight);
directionalLight.castShadow = true;
const stem = new THREE.CylinderGeometry(0.04, 0.04, 1, 6);
stem.translate(0, 0.5, 0);
const stem2 = new THREE.CylinderGeometry(0.04,0.04,0.7071,6);
stem2.rotateX(Math.PI/4);
stem2.translate(0,1.25,0.25);
const stem3 = new THREE.ConeGeometry(0.2,0.5,8,1,true,0,6.283185);
stem3.translate(0,0.2,0);
const leaf1 = new THREE.SphereGeometry(0.2, 8, 8);
leaf1.scale(1.5, 0.15, 0.5); // flatten vertically
leaf1.rotateZ(-Math.PI / 4); // tilt upward to the left
leaf1.translate(-0.2,1.2,0);
const leaf2 = new THREE.SphereGeometry(0.2, 8, 8);
leaf2.scale(1.5, 0.15, 0.5);
leaf2.rotateZ(Math.PI / 4); // tilt upward to the right
leaf2.translate(0.2, 1.2, 0);
const center = new THREE.SphereGeometry(0.15, 8, 8);
center.translate(0, 1.6, 0.6);
const petalCount = 12;
const petal = new THREE.ConeGeometry(0.05, 0.3, 6);
petal.rotateZ(Math.PI / 2);
petal.translate(0.15, 0, 0);
const petalGeos = [];
for (let i = 0; i < petalCount; i++) {
const a = (i / petalCount) * Math.PI * 2;
const g = petal.clone();
// rotate around Y to make a ring
g.applyMatrix4(
new THREE.Matrix4()
.makeRotationY(a)
);
petalGeos.push(g);
}
const mergedPetals = BufferGeometryUtils.mergeGeometries(petalGeos);
mergedPetals.applyMatrix4(new THREE.Matrix4().makeRotationX(Math.PI / 2.2));
mergedPetals.applyMatrix4(new THREE.Matrix4().makeTranslation(0, 1.6, 0.65));
const merged = BufferGeometryUtils.mergeGeometries([stem, stem2, stem3, leaf1, leaf2, center, mergedPetals]);
function tagGeometry(geometry, tagValue) {
const count = geometry.attributes.position.count;
const tags = new Float32Array(count).fill(tagValue);
geometry.setAttribute('isPetal', new THREE.InstancedBufferAttribute(tags, 1));
return geometry;
}
const stemGeoTagged = tagGeometry(stem, 0.0);
const stem2GeoTagged = tagGeometry(stem2, 0.0);
const stem3GeoTagged = tagGeometry(stem3, 0.0);
const leaf1GeoTagged = tagGeometry(leaf1, 0.0);
const leaf2GeoTagged = tagGeometry(leaf2, 0.0);
const headGeoTagged = tagGeometry(center, 2.0);
const petalGeoTagged = tagGeometry(mergedPetals, 1.0);
const sunflowerGeo = BufferGeometryUtils.mergeGeometries([
stemGeoTagged,
stem2GeoTagged,
stem3GeoTagged,
leaf1GeoTagged,
leaf2GeoTagged,
headGeoTagged,
petalGeoTagged
]);
const sunflowerMat = new THREE.ShaderMaterial({
uniforms: {
stemColor: { value: new THREE.Color('#5baa5b') } // base green
},
vertexShader: `
attribute float isPetal;
varying float vIsPetal;
varying vec3 vColor;
void main() {
vIsPetal = isPetal;
vColor = instanceColor; // vertex color (for later use)
gl_Position = projectionMatrix * modelViewMatrix * instanceMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform vec3 stemColor;
varying float vIsPetal;
varying vec3 vColor;
void main() {
vec3 col = mix(stemColor, mix(vColor,vColor * 0.8,step(1.5,vIsPetal)), step(0.5, vIsPetal));
gl_FragColor = vec4(col, 1.0);
}
`,
});
const count = 2900;
const sunmesh = new THREE.InstancedMesh(sunflowerGeo, sunflowerMat, count);
sunmesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
const dummy = new THREE.Object3D();
for (let i = 0; i < count; i++) {
let a = Math.random()*6.283;
let v = Math.sqrt(Math.random())*63.5;
let x = v * Math.cos(a);
let z = v * Math.sin(a);
dummy.scale.set(0.42,0.42,0.42);
dummy.position.set(x,terrain.getHeight(x,z)-0.1,z);
dummy.updateMatrix();
sunmesh.setMatrixAt(i, dummy.matrix);
const color = new THREE.Color();
color.setHSL(0.7+Math.sin(terrain.getHeight(x*.3,z*.32)*2)*0.25,0.75,0.7);
sunmesh.setColorAt(i, color);
}
sunmesh.instanceColor.needsUpdate = true;
scene.add(sunmesh);
const skyGeo = new THREE.CylinderGeometry(64, 64, 200, 64, 1, true);
skyGeo.scale(-1, 1, 1); // Flip normals inward
// Simple cloud shader
const skyMat = new THREE.ShaderMaterial({
side: THREE.DoubleSide,
uniforms: {
time: { value: 0.0 },
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float time;
varying vec2 vUv;
// simple 2D noise
float hash(vec2 p) {
return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}
float noise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
float a = hash(i);
float b = hash(i + vec2(1.0, 0.0));
float c = hash(i + vec2(0.0, 1.0));
float d = hash(i + vec2(1.0, 1.0));
vec2 u = f*f*(3.0-2.0*f);
return mix(a, b, u.x) + (c - a)*u.y*(1.0 - u.x) + (d - b)*u.x*u.y;
}
void main() {
vec2 uv = (vUv.x < 0.5 ? vUv : vec2(1.0-vUv.x,vUv.y)) * vec2(20.0, 24.0); // repeat clouds horizontally
uv.x += time * 0.2; // drift clouds
float n = (noise(uv) + noise(uv.yx))*0.5;
float clouds = smoothstep(0.6, 0.9, n);
vec3 sky = mix(vec3(0.4,0.7,1.), vec3(1.0), clouds);
gl_FragColor = vec4(sky, 1.0);
}
`
});
const sky = new THREE.Mesh(skyGeo, skyMat);
scene.add(sky);
class RevLight {
constructor(god,x,y) {
this.god=god;
const beamColor = new THREE.Color(0xffffff);
const rgb = hsvToRgb(gcol[god], 0.9, 1, 1);
beamColor.setRGB(rgb[0], rgb[1], rgb[2]);
const geom = new THREE.ConeGeometry(5, 40, 32, 1, true);
const mat = new THREE.MeshBasicMaterial({
color: beamColor,
transparent: true,
opacity: 0.25,
depthWrite: false,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending
});
this.beam = new THREE.Mesh(geom, mat);
this.beam.position.set(x, 30, y);
this.intensity=0.5;
this.startTime = Date.now();
scene.add(this.beam);
}
}
let revlights = [];
const elementSize = size / segments;
const heightMatrix = [];
for (let i = 0; i <= segments; i++) { // rows (z direction)
const row = [];
for (let j = 0; j <= segments; j++) { // columns (x direction)
const idx = (segments-j) * (segments + 1) + i;
row.push(positionAttribute.getY(idx));
}
heightMatrix.push(row);
}
const shape = new CANNON.Heightfield(heightMatrix, {
elementSize: elementSize
});
const pgbody = new CANNON.Body({ mass: 0 }); // static body
pgbody.addShape(shape);
pgbody.position.set(-size / 2, 0, size / 2); // adjust for centering/rotation
pgbody.quaternion.setFromEuler(-Math.PI / 2, 0, 0, "XYZ"); // rotate to XZ plane
world.addBody(pgbody);
class SpriteCreature extends THREE.Group {
constructor(size = 1, x = 0, z = 0, faith = 1) {
super();
this.creatureSize = size;
this.canvas = null;
this.bodyTexture = null;
this.bodyMaterial = null;
this.staticMaterials = {};
this.position.set(x,0,z);
this.createMaterials();
this.createCreature();
this.scale.setScalar(size);
this.cooldown = 0;
this.lockedTarget = 0;
this.currentProg = null;
this.health = 100;
this.faith = faith;
this.mode = 0;
this.birthdate = Date.now();
const cylinderShape = new CANNON.Cylinder(0.2, 0.6, 1.3, 16);
this.halo.material.color = new THREE.Color(hsvToRgb(gcol[this.faith],1,1,0));
this.body = new CANNON.Body({ mass: 5 });
this.body.addShape(cylinderShape);
this.body.position.set(x, 0, z);
this.body.angularDamping = 0.9;
this.body.linearDamping = 0.5;
world.addBody(this.body);
}
createMaterials() {
this.canvas = document.createElement('canvas');
this.canvas.width = 256;
this.canvas.height = 256;
const ctx = this.canvas.getContext('2d');
this.drawBodyTexture(ctx);
this.bodyTexture = new THREE.CanvasTexture(this.canvas);
this.bodyTexture.generateMipmaps = false;
this.bodyTexture.minFilter = THREE.LinearFilter;
this.bodyTexture.magFilter = THREE.LinearFilter;
this.bodyMaterial = new THREE.MeshLambertMaterial({
map: this.bodyTexture,
transparent: true,
side: THREE.DoubleSide
});
this.staticMaterials = {
head: new THREE.MeshLambertMaterial({
color: 0xffb347, // Orange
side: THREE.DoubleSide
}),
limbs: new THREE.MeshLambertMaterial({
color: 0x8e44ad, // Purple
side: THREE.DoubleSide
}),
hands: new THREE.MeshLambertMaterial({
color: 0xffb347, // Same as head
side: THREE.DoubleSide
}),
feet: new THREE.MeshLambertMaterial({
color: 0x8e44ad, // Same as limbs
side: THREE.DoubleSide
})
};
}
drawBodyTexture(ctx) {
ctx.clearRect(0, 0, 256, 256);
ctx.fillStyle='white';
ctx.fillRect(0,0,256,256);
if (this.bodyTexture)
this.bodyTexture.needsUpdate = true;
}
createCreature() {
const bodyGeometry = new THREE.CylinderGeometry(0.2, 0.6, 1.3, 32);
const bodyMesh = new THREE.Mesh(bodyGeometry, this.bodyMaterial);
bodyMesh.position.set(0, 0, 0);
bodyMesh.name = 'body';
bodyMesh.castShadow = true;
this.add(bodyMesh);
const headGeometry = new THREE.SphereGeometry(0.5, 12, 8);
const headMesh = new THREE.Mesh(headGeometry, this.staticMaterials.head);
headMesh.position.set(0, 1.1, 0);
headMesh.name = 'head';
headMesh.castShadow = true;
this.add(headMesh);
const haloGeometry = new THREE.TorusGeometry(0.4,0.05);
const haloMaterial = new THREE.MeshStandardMaterial({color: 0xFFFFFF});
this.halo = new THREE.Mesh(haloGeometry, haloMaterial);
this.halo.rotation.x = Math.PI / 2;
this.halo.position.y = 1.6;
this.add(this.halo);
this.addFaceFeatures(headMesh);
const armGeometry = new THREE.CylinderGeometry(0.08, 0.08, 0.8, 6);
const leftArm = new THREE.Mesh(armGeometry, this.staticMaterials.limbs);
leftArm.position.set(-0.8, 0.2, 0);
leftArm.rotation.z = -Math.PI * 0.4;
leftArm.name = 'leftArm';
leftArm.castShadow = true;
this.add(leftArm);
const rightArm = new THREE.Mesh(armGeometry, this.staticMaterials.limbs);
rightArm.position.set(0.8, 0.2, 0);
rightArm.rotation.z = Math.PI * 0.4;
rightArm.name = 'rightArm';
rightArm.castShadow = true;
this.add(rightArm);
// Hands (small spheres) - attached to arms
const handGeometry = new THREE.SphereGeometry(0.12, 8, 6);
const leftHand = new THREE.Mesh(handGeometry, this.staticMaterials.hands);
leftHand.position.set(0, -0.5, 0); // Relative to arm center
leftHand.name = 'leftHand';
leftHand.castShadow = true;
leftArm.add(leftHand); // Make hand a child of arm
const rightHand = new THREE.Mesh(handGeometry, this.staticMaterials.hands);
rightHand.position.set(0, -0.5, 0); // Relative to arm center
rightHand.name = 'rightHand';
rightHand.castShadow = true;
rightArm.add(rightHand); // Make hand a child of arm
// Legs (thin cylinders) - SOLID COLOR
const legGeometry = new THREE.CylinderGeometry(0.1, 0.08, 1.0, 6);
const leftLeg = new THREE.Mesh(legGeometry, this.staticMaterials.limbs);
leftLeg.position.set(-0.3, -0.9, 0);
leftLeg.name = 'leftLeg';
leftLeg.castShadow = true;
this.add(leftLeg);
const rightLeg = new THREE.Mesh(legGeometry, this.staticMaterials.limbs);
rightLeg.position.set(0.3, -0.9, 0);
rightLeg.name = 'rightLeg';
rightLeg.castShadow = true;
this.add(rightLeg);
// Feet (small spheres) - attached to legs
const footGeometry = new THREE.SphereGeometry(0.15, 8, 6);
const leftFoot = new THREE.Mesh(footGeometry, this.staticMaterials.feet);
leftFoot.position.set(0, -0.6, 0); // Relative to leg center
leftFoot.name = 'leftFoot';
leftFoot.castShadow = true;
leftLeg.add(leftFoot); // Make foot a child of leg
const rightFoot = new THREE.Mesh(footGeometry, this.staticMaterials.feet);
rightFoot.position.set(0, -0.6, 0); // Relative to leg center
rightFoot.name = 'rightFoot';
rightFoot.castShadow = true;
rightLeg.add(rightFoot); // Make foot a child of leg
}
addFaceFeatures(headMesh) {
const eyeGeometry = new THREE.SphereGeometry(0.05, 6, 4);
const eyeMaterial = new THREE.MeshLambertMaterial({ color: 0x2c3e50, side: THREE.DoubleSide });
const leftEye = new THREE.Mesh(eyeGeometry, eyeMaterial);
leftEye.position.set(-0.15, 0.15, 0.4);
headMesh.add(leftEye);
const rightEye = new THREE.Mesh(eyeGeometry, eyeMaterial);
rightEye.position.set(0.15, 0.15, 0.4);
headMesh.add(rightEye);
// Eye highlights
const highlightGeometry = new THREE.SphereGeometry(0.02, 4, 3);
const highlightMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff, side: THREE.DoubleSide });
const leftHighlight = new THREE.Mesh(highlightGeometry, highlightMaterial);
leftHighlight.position.set(-0.12, 0.18, 0.42);
headMesh.add(leftHighlight);
const rightHighlight = new THREE.Mesh(highlightGeometry, highlightMaterial);
rightHighlight.position.set(0.18, 0.18, 0.42);
headMesh.add(rightHighlight);
// Mouth (small cylinder)
const mouthGeometry = new THREE.CylinderGeometry(0.03, 0.03, 0.15, 6);
const mouthMaterial = new THREE.MeshLambertMaterial({ color: 0x2c3e50, side: THREE.DoubleSide });
const mouth = new THREE.Mesh(mouthGeometry, mouthMaterial);
mouth.position.set(0, 0.05, 0.4);
mouth.rotation.z = Math.PI / 2;
headMesh.add(mouth);
}
// Method to update only the body texture (clothing)
updateBodyTexture() {
const ctx = this.canvas.getContext('2d');
this.drawBodyTexture(ctx);
}
// Method to draw custom clothing/patterns on the body
drawCustomBodyPattern(drawFunction) {
const ctx = this.canvas.getContext('2d');
ctx.clearRect(0, 0, 256, 256);
// Call the custom drawing function with the context
drawFunction(ctx);
this.bodyTexture.needsUpdate = true;
}
setPartColor(partName, color) {
if (this.staticMaterials[partName]) {
this.staticMaterials[partName].color.setHex(color);
}
}
setSize(newSize) {
this.creatureSize = newSize;
this.scale.setScalar(newSize);
}
dispose() {
this.traverse(child => {
if (child.geometry) {
child.geometry.dispose();
}
});
Object.values(this.staticMaterials).forEach(material => {
material.dispose();
});
if (this.bodyMaterial) {
this.bodyMaterial.dispose();
}
if (this.bodyTexture) {
this.bodyTexture.dispose();
}
}
moveToward(x, z, speed) {
const dx = x - this.body.position.x;
const dz = z - this.body.position.z;
let ang = Math.atan2(dz,dx);
this.body.position.x += speed * Math.cos(ang);
this.body.position.z += speed * Math.sin(ang);
}
moveAway(x, z, speed) {
const dx = x - this.body.position.x;
const dz = z - this.body.position.z;
let ang = Math.atan2(dz,dx);
this.body.position.x -= speed * Math.cos(ang);
this.body.position.z -= speed * Math.sin(ang);
}
}
let creatures = [];
let me;
for (me=0; me<6; me++) {
let c=new SpriteCreature(1.4, Math.random()*10-5, Math.random()*8 - 4, 1+Math.floor(me/2));
creatures.push(c);
c.castShadow = true;
scene.add(c);
}
const gbox = new THREE.BoxGeometry(1, 1, 1);
const mbox = new THREE.MeshStandardMaterial({
color: 0xD3AF37,
side: THREE.DoubleSide
});
const mbox2 = new THREE.MeshStandardMaterial({
color: 0x6A452C,
side: THREE.DoubleSide
});
const mbox3 = new THREE.MeshStandardMaterial({
color: 0xEEEEFF,
side: THREE.DoubleSide
});
const pgroundMaterial = new CANNON.Material('ground');
const pboxMaterial = new CANNON.Material('box');
const dboxMaterial = new CANNON.Material('danger');
class Target {
constructor(x,y) {
this.box = new THREE.Mesh(gbox,mbox);
this.box.castShadow = true;
this.box.receiveShadow = true;
this.box.position.set(x,35,y);
this.number = ++tcounter;
this.restingplace = [0,0,0];
this.goresting = 0;
this.birthdate = Date.now();
this.captured=0;
scene.add(this.box);
const halfExtents = new CANNON.Vec3(0.5, 0.5, 0.5); // Adjust if your box is different size
this.body = new CANNON.Body({
mass: 10, // make it dynamic
shape: new CANNON.Box(halfExtents)
});
this.body.position.set(x, 35, y);
this.body.material = pboxMaterial;
world.addBody(this.body);
}
}
pgbody.material = pgroundMaterial;
const contactMaterial = new CANNON.ContactMaterial(pgroundMaterial, pboxMaterial, {
friction: 0.1,
restitution: 0.5
});
world.addContactMaterial(contactMaterial);
const gdan = new THREE.TetrahedronGeometry(1, 0);
const mdan = new THREE.MeshStandardMaterial({
color: 0xff0000,
emissive: 0x330000,
emissiveIntensity: 0.5,
metalness: 0.3,
roughness: 0.4,
side: THREE.DoubleSide,
transparent: true,
opacity:1
});
const positions = gdan.attributes.position.array;
const rawVerts = [];
for (let i = 0; i < positions.length; i += 3) {
rawVerts.push(new CANNON.Vec3(positions[i], positions[i+1], positions[i+2]));
}
const uniqueVerts = [];
const vertMap = [];
const threshold = 1e-6; // tolerance for floating-point comparison
function isEqual(v1, v2) {
return v1.distanceTo(v2) < threshold;
}
rawVerts.forEach((v, idx) => {
let foundIndex = uniqueVerts.findIndex(u => isEqual(u, v));
if (foundIndex === -1) {
uniqueVerts.push(v);
vertMap[idx] = uniqueVerts.length - 1;
} else {
vertMap[idx] = foundIndex;
}
});
const faces = [];
for (let i = 0; i < rawVerts.length; i += 3) {
faces.push([
vertMap[i],
vertMap[i+1],
vertMap[i+2]
]);
}
const tetraShape = new CANNON.ConvexPolyhedron({ vertices: uniqueVerts, faces });
class Danger {
constructor(x,y) {
this.box = new THREE.Mesh(gdan,mdan);
this.box.castShadow = true;
this.box.receiveShadow = true;
this.box.position.set(x,35,y);
this.number = ++dcounter;
this.strength = 1;
this.body = new CANNON.Body({mass: 10});
this.body.addShape(tetraShape);
this.body.position.set(x, 35, y);
this.body.material = dboxMaterial;
world.addBody(this.body);
scene.add(this.box);
}
}
let targets=new Map();
let dangers =new Map();
for (let e=0; e<3; e++) {
let t = new Target(Math.random()*40-20,Math.random()*40-20);
targets.set(t.number,t);
}
const contactMaterial2 = new CANNON.ContactMaterial(pgroundMaterial, dboxMaterial, {
friction: 0.1,
restitution: 0.5 // increase for bouncier impacts
});
world.addContactMaterial(contactMaterial2);
var keydown=[0,0,0,0,0,0,0,0,0,0,0,0];
let twopi = Math.PI * 2;
let ninety = Math.PI * 0.5;
var theta = -1.63;
var stheta = theta;
var left=0, ltop=0, mousepressed=0;
var yang = -0.27;
var syang = yang;
var spd = 0.6, spd2=0.25;
var wiw = window.innerWidth;
var wih = window.innerHeight;
var overscroll = 0;
function nearest(limit) {
let mn = limit;
let index=-1;
for (var e=0; e< creatures.length; e++)
if (!(e==me)) {
let dst=Math.hypot(creatures[e].body.position.x - creatures[me].body.position.x,creatures[e].body.position.z - creatures[me].body.position.z);
if (dst<mn) {
index=e;
mn=dst;
}
}
return index;
}
function evade() {
let ind = nearest(5);
if (ind>=0) {
creatures[me].moveAway(creatures[ind].body.position.x,creatures[ind].body.position.z,0.1);
}
}
function pursue() {
let c = creatures[me];
if (!c.lockedTarget) c.lockedTarget=findTarget(0);
if (c.lockedTarget) {
let t = targets.get(c.lockedTarget);
if (t === undefined)
c.lockedTarget=0;
else {
c.moveToward(t.box.position.x,t.box.position.z,0.1);
if (targets.get(c.lockedTarget).captured>0) c.lockedTarget=0;
else {
if (Math.hypot(c.body.position.x - t.box.position.x,c.body.position.z - t.box.position.z)<1)
if (t.box.position.y - c.body.position.y < 2) {
c.health+=20;
t.captured=1;
c.lockedTarget=0;
c.currentProg = getProgram();
t.box.material = mbox2;
}
}
}
}
}
function findblock() {
let c = creatures[me];
if (!c.lockedTarget) c.lockedTarget=findTarget(1);
if (c.lockedTarget) {
let t = targets.get(c.lockedTarget);
if (t === undefined)
c.lockedTarget=0;
else {
c.moveToward(t.box.position.x,t.box.position.z,0.1);
if (targets.get(c.lockedTarget).captured>1) c.lockedTarget=0;
else {
if (Math.hypot(c.body.position.x - t.box.position.x,c.body.position.z - t.box.position.z)<1) {
t.captured=2;
t.box.material = mbox3;
world.removeBody(t.body);
c.currentProg = placeblock;
}
}
}
}
else c.currentProg = getProgram();
}
function placeblock() {
let c = creatures[me];
let tmp = Temples[c.faith];
c.moveToward(tmp[0],tmp[1],0.1);
if (c.lockedTarget > 0) {
let t = targets.get(c.lockedTarget);
try {
t.body.position.set(c.body.position.x + 0.5, c.body.position.y, c.body.position.z + 0.5);
} catch (error) {
console.error('Error setting target body position:', error);
console.error('t.body:', t);
console.error('c.body:', c);
}
}
let h = Math.hypot(c.body.position.x - tmp[0],c.body.position.z - tmp[1]);
if (h<9) {
let t = targets.get(c.lockedTarget);
t.captured=3;
c.currentProg = getProgram();
let tmp = Temples[creatures[me].faith];
let index = tmp[2];
let lvl = Math.floor(index/9);
index-=lvl*9;
let yy = Math.floor(index/3);
index-=yy*3;
let x = tmp[0]-1+index;
let z = tmp[1]-1+yy;
let y = terrain.getHeight(tmp[0],tmp[1])+3+lvl;
tmp[2]++;
t.body.position.y+=5;
const p0 = new CANNON.Vec3(t.body.position.x,t.body.position.y,t.body.position.z);
const pT = new CANNON.Vec3(x,y,z);
const dx = pT.x - p0.x;
const dz = pT.z - p0.z;
const dy = pT.y - p0.y;
const d = Math.sqrt(dx * dx + dz * dz);
const angle = Math.PI/4;
const denom = 2 * Math.cos(angle) ** 2 * (d * Math.tan(angle) - dy);
const v0 = Math.sqrt((9.82 * d * d) / denom);
const dir = new CANNON.Vec3(dx / d, 0, dz / d);
const vel = new CANNON.Vec3(
v0 * Math.cos(angle) * dir.x,
v0 * Math.sin(angle),
v0 * Math.cos(angle) * dir.z
);
world.addBody(t.body);
t.body.velocity.copy(vel);
t.goresting=1;
t.restingplace = [x,y,z];
}
}
function findTarget(lvl) {
let mn = 9999999;
let index=0;
targets.forEach((target, key) => {
if (target.captured == lvl) {
let dst=Math.hypot(target.box.position.x - creatures[me].body.position.x,target.box.position.z - creatures[me].body.position.z);
if (dst<mn) {
index=key;
mn=dst;
}
}
});
return index;
}
function haverest() {
}
function getProgram() {
let r = Math.random();
creatures[me].lockedTarget = 0;
creatures[me].cooldown=250+Math.random()*200;
if (creatures[me].health<25) r+=0.4;
if (r<0.2) {
creatures[me].cooldown = 200;
return haverest;
}
if (r<0.4) return evade;
if (r<0.7) {
creatures[me].cooldown = 99999;
return findblock;
}
return pursue;
}
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
renderer.domElement.addEventListener('dblclick', function(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObject(ground);
if (intersects.length > 0) {
const p = intersects[0].point;
if (goodbad) {
let t = new Danger(p.x,p.z);
dangers.set(t.number,t);
}
else {
let t = new Target(p.x,p.z);
targets.set(t.number,t);
}
}
});
document.addEventListener('keydown', (e) => {
let keynum=0;
if(window.event) keynum = e.keyCode;
else keynum = e.which;
switch (keynum) {
case 65 : keydown[0]=1; break;
case 68 : keydown[1]=1; break;
case 87 : keydown[2]=1; break;
case 83 : keydown[3]=1; break;
case 81 : keydown[4]=1; break;
case 90 : keydown[5]=1; break;
case 32 : goodbad = 1-goodbad; break;
case 84 :
for (let i=0; i<creatures.length; i++) {
console.log('pos='+creatures[i].body.position.x.toFixed(1)+','+creatures[i].body.position.z.toFixed(1)+' health='+creatures[i].healath);
};
break;
default : console.log(camera.position);
console.log('theta='+theta);
console.log('yang='+yang);
}
},false);
document.addEventListener('keyup', (e) => {
let keynum=0;
if(window.event) keynum = e.keyCode;
else keynum = e.which;
switch (keynum) {
case 65 : keydown[0]=0; break;
case 68 : keydown[1]=0; break;
case 32 :
case 87 : keydown[2]=0; break;
case 83 : keydown[3]=0; break;
case 81 : keydown[4]=0; break;
case 90 : keydown[5]=0; break;
}
},false);
if (document.addEventListener) {
// For modern browsers
renderer.domElement.addEventListener("wheel", handleScroll);
} else {
// For old IE browsers
renderer.domElement.attachEvent("onmousewheel", handleScroll);
}
function handleScroll(event) {