@adatechnology/game-terrain-three 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +5 -1
- package/dist/index.global.js +143 -4
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +103 -4
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ interface TerrainRenderer {
|
|
|
26
26
|
readonly pristineChunks: number;
|
|
27
27
|
readonly marksDrawn: number;
|
|
28
28
|
readonly debrisDrawn: number;
|
|
29
|
+
readonly crackQuads: number;
|
|
29
30
|
};
|
|
30
31
|
dispose(): void;
|
|
31
32
|
}
|
|
@@ -47,4 +48,7 @@ declare function createTerrainTestModule(options?: {
|
|
|
47
48
|
readonly locale?: string;
|
|
48
49
|
}): TestableModule;
|
|
49
50
|
|
|
50
|
-
|
|
51
|
+
/** The name of a ground profile in the locale (`en` when the locale is unknown); a profile a game added without a name answers its id. */
|
|
52
|
+
declare function terrainProfileName(profileId: string, localeId: string): string;
|
|
53
|
+
|
|
54
|
+
export { TERRAIN_AREA_GROUND, TERRAIN_STRIPS, type TerrainRenderer, type TerrainRendererOptions, createSurfaceMaterial, createTerrainRenderer, createTerrainTestModule, terrainProfileName };
|
package/dist/index.global.js
CHANGED
|
@@ -25,7 +25,8 @@ var GameTerrainThree = (() => {
|
|
|
25
25
|
TERRAIN_STRIPS: () => TERRAIN_STRIPS,
|
|
26
26
|
createSurfaceMaterial: () => createSurfaceMaterial,
|
|
27
27
|
createTerrainRenderer: () => createTerrainRenderer,
|
|
28
|
-
createTerrainTestModule: () => createTerrainTestModule
|
|
28
|
+
createTerrainTestModule: () => createTerrainTestModule,
|
|
29
|
+
terrainProfileName: () => terrainProfileName
|
|
29
30
|
});
|
|
30
31
|
|
|
31
32
|
// ../game-contracts/dist/index.js
|
|
@@ -38,6 +39,47 @@ var GameTerrainThree = (() => {
|
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
// ../game-terrain/dist/index.js
|
|
42
|
+
function createRandom(seed) {
|
|
43
|
+
let state = seed | 0;
|
|
44
|
+
return () => {
|
|
45
|
+
state = state + 1831565813 | 0;
|
|
46
|
+
let mixed = Math.imul(state ^ state >>> 15, 1 | state);
|
|
47
|
+
mixed = mixed + Math.imul(mixed ^ mixed >>> 7, 61 | mixed) ^ mixed;
|
|
48
|
+
return ((mixed ^ mixed >>> 14) >>> 0) / 4294967296;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
var HAIRLINE = 15e-4;
|
|
52
|
+
var SEGMENTS = 4;
|
|
53
|
+
var BRANCH_CHANCE = 0.45;
|
|
54
|
+
function crackLines(crack) {
|
|
55
|
+
const random = createRandom(crack.seed);
|
|
56
|
+
const segments = [];
|
|
57
|
+
const widthAt = (distance) => Math.max(HAIRLINE, crack.width * (1 - distance / crack.reach));
|
|
58
|
+
for (let ray = 0; ray < crack.rays; ray++) {
|
|
59
|
+
let angle = ray / crack.rays * Math.PI * 2 + (random() - 0.5) * 0.6;
|
|
60
|
+
const length = crack.reach * (0.45 + 0.55 * random());
|
|
61
|
+
let x = crack.x;
|
|
62
|
+
let z = crack.z;
|
|
63
|
+
let run = 0;
|
|
64
|
+
for (let segment = 0; segment < SEGMENTS; segment++) {
|
|
65
|
+
const step = length / SEGMENTS * (0.75 + 0.5 * random());
|
|
66
|
+
angle += (random() - 0.5) * 0.5;
|
|
67
|
+
const nx = x + Math.cos(angle) * step;
|
|
68
|
+
const nz = z + Math.sin(angle) * step;
|
|
69
|
+
segments.push({ x0: x, z0: z, x1: nx, z1: nz, w0: widthAt(run), w1: widthAt(run + step) });
|
|
70
|
+
if (segment > 0 && random() < BRANCH_CHANCE) {
|
|
71
|
+
const side = angle + (random() < 0.5 ? -1 : 1) * (0.45 + random() * 0.5);
|
|
72
|
+
const branch = step * (0.5 + 0.6 * random());
|
|
73
|
+
const w = widthAt(run + step) * 0.6;
|
|
74
|
+
segments.push({ x0: nx, z0: nz, x1: nx + Math.cos(side) * branch, z1: nz + Math.sin(side) * branch, w0: Math.max(HAIRLINE, w), w1: HAIRLINE });
|
|
75
|
+
}
|
|
76
|
+
run += step;
|
|
77
|
+
x = nx;
|
|
78
|
+
z = nz;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return segments;
|
|
82
|
+
}
|
|
41
83
|
var TERRAIN_PATTERNS = ["plain", "grass", "soil", "sand", "asphalt", "concrete", "tile", "planks", "stone"];
|
|
42
84
|
|
|
43
85
|
// src/surface-material.ts
|
|
@@ -409,6 +451,92 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
|
|
|
409
451
|
}
|
|
410
452
|
};
|
|
411
453
|
}
|
|
454
|
+
var CRACK_LIFT = 4e-3;
|
|
455
|
+
var MAX_CRACK_QUADS = 8192;
|
|
456
|
+
var CRACK_SHADE = 0.22;
|
|
457
|
+
var DARK_SURFACE = 0.1;
|
|
458
|
+
var DUST_LIFT = 2;
|
|
459
|
+
var DUST_GREY = 0.12;
|
|
460
|
+
function createCrackLayer(THREE, terrain) {
|
|
461
|
+
const positions = new Float32Array(MAX_CRACK_QUADS * 4 * 3);
|
|
462
|
+
const colours = new Float32Array(MAX_CRACK_QUADS * 4 * 3);
|
|
463
|
+
const indices = new Uint32Array(MAX_CRACK_QUADS * 6);
|
|
464
|
+
for (let quad = 0; quad < MAX_CRACK_QUADS; quad++) {
|
|
465
|
+
const first = quad * 4;
|
|
466
|
+
indices.set([first, first + 2, first + 1, first + 1, first + 2, first + 3], quad * 6);
|
|
467
|
+
}
|
|
468
|
+
const geometry = new THREE.BufferGeometry();
|
|
469
|
+
const position = new THREE.BufferAttribute(positions, 3);
|
|
470
|
+
const colour = new THREE.BufferAttribute(colours, 3);
|
|
471
|
+
position.setUsage(THREE.DynamicDrawUsage);
|
|
472
|
+
colour.setUsage(THREE.DynamicDrawUsage);
|
|
473
|
+
geometry.setAttribute("position", position);
|
|
474
|
+
geometry.setAttribute("color", colour);
|
|
475
|
+
geometry.setIndex(new THREE.BufferAttribute(indices, 1));
|
|
476
|
+
geometry.setDrawRange(0, 0);
|
|
477
|
+
const material = new THREE.MeshBasicMaterial({ vertexColors: true, transparent: true, opacity: 0.9, depthWrite: false, polygonOffset: true, polygonOffsetFactor: -3, polygonOffsetUnits: -3 });
|
|
478
|
+
const mesh = new THREE.Mesh(geometry, material);
|
|
479
|
+
mesh.name = "terrain-cracks";
|
|
480
|
+
mesh.frustumCulled = false;
|
|
481
|
+
mesh.renderOrder = 2;
|
|
482
|
+
let seen = -1;
|
|
483
|
+
let groundMoved = false;
|
|
484
|
+
const stopListening = terrain.onDeform(() => {
|
|
485
|
+
groundMoved = true;
|
|
486
|
+
});
|
|
487
|
+
const lift = (x, z) => terrain.heightAt(x, z) + CRACK_LIFT;
|
|
488
|
+
return {
|
|
489
|
+
mesh,
|
|
490
|
+
sync() {
|
|
491
|
+
const cracks = terrain.cracks();
|
|
492
|
+
if (seen === terrain.cracksVersion && !(groundMoved && cracks.length > 0)) return;
|
|
493
|
+
seen = terrain.cracksVersion;
|
|
494
|
+
groundMoved = false;
|
|
495
|
+
let quad = 0;
|
|
496
|
+
for (let n = cracks.length - 1; n >= 0 && quad < MAX_CRACK_QUADS; n--) {
|
|
497
|
+
const crack = cracks[n];
|
|
498
|
+
const tone = terrain.material(crack.material).color;
|
|
499
|
+
const dark = tone[0] * 0.3 + tone[1] * 0.59 + tone[2] * 0.11 < DARK_SURFACE;
|
|
500
|
+
const k = dark ? DUST_LIFT : CRACK_SHADE;
|
|
501
|
+
const r = dark ? Math.min(1, tone[0] * k + DUST_GREY) : tone[0] * k;
|
|
502
|
+
const g = dark ? Math.min(1, tone[1] * k + DUST_GREY) : tone[1] * k;
|
|
503
|
+
const b = dark ? Math.min(1, tone[2] * k + DUST_GREY) : tone[2] * k;
|
|
504
|
+
for (const s of crackLines(crack)) {
|
|
505
|
+
if (quad >= MAX_CRACK_QUADS) break;
|
|
506
|
+
if (!terrain.contains(s.x0, s.z0) || !terrain.contains(s.x1, s.z1)) continue;
|
|
507
|
+
const length = Math.hypot(s.x1 - s.x0, s.z1 - s.z0);
|
|
508
|
+
if (length < 1e-5) continue;
|
|
509
|
+
const ax = -(s.z1 - s.z0) / length;
|
|
510
|
+
const az = (s.x1 - s.x0) / length;
|
|
511
|
+
const corners = [
|
|
512
|
+
[s.x0 + ax * s.w0 * 0.5, s.z0 + az * s.w0 * 0.5],
|
|
513
|
+
[s.x0 - ax * s.w0 * 0.5, s.z0 - az * s.w0 * 0.5],
|
|
514
|
+
[s.x1 + ax * s.w1 * 0.5, s.z1 + az * s.w1 * 0.5],
|
|
515
|
+
[s.x1 - ax * s.w1 * 0.5, s.z1 - az * s.w1 * 0.5]
|
|
516
|
+
];
|
|
517
|
+
corners.forEach(([x, z], corner) => {
|
|
518
|
+
const at = (quad * 4 + corner) * 3;
|
|
519
|
+
positions[at] = x;
|
|
520
|
+
positions[at + 1] = lift(x, z);
|
|
521
|
+
positions[at + 2] = z;
|
|
522
|
+
colours[at] = r;
|
|
523
|
+
colours[at + 1] = g;
|
|
524
|
+
colours[at + 2] = b;
|
|
525
|
+
});
|
|
526
|
+
quad++;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
position.needsUpdate = true;
|
|
530
|
+
colour.needsUpdate = true;
|
|
531
|
+
geometry.setDrawRange(0, quad * 6);
|
|
532
|
+
},
|
|
533
|
+
dispose() {
|
|
534
|
+
stopListening();
|
|
535
|
+
geometry.dispose();
|
|
536
|
+
material.dispose();
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
}
|
|
412
540
|
|
|
413
541
|
// src/rebar.ts
|
|
414
542
|
var BAR_RADIUS = 6e-3;
|
|
@@ -527,7 +655,8 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
|
|
|
527
655
|
const drawn = /* @__PURE__ */ new Map();
|
|
528
656
|
const marks = createMarkLayer(THREE, terrain, options.markDistance ?? DEFAULT_MARK_DISTANCE);
|
|
529
657
|
const debris = createDebrisLayer(THREE, terrain);
|
|
530
|
-
|
|
658
|
+
const cracks = createCrackLayer(THREE, terrain);
|
|
659
|
+
group.add(marks.mesh, cracks.mesh, debris.mesh);
|
|
531
660
|
let pristineStale = true;
|
|
532
661
|
let pristineBuilt = false;
|
|
533
662
|
let pristineCount = 0;
|
|
@@ -613,7 +742,8 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
|
|
|
613
742
|
return {
|
|
614
743
|
group,
|
|
615
744
|
sync(camera) {
|
|
616
|
-
const
|
|
745
|
+
const laidAnew = terrain.dirtyChunkCount >= terrain.chunksX * terrain.chunksZ;
|
|
746
|
+
const limit = firstSync || laidAnew ? Infinity : void 0;
|
|
617
747
|
firstSync = false;
|
|
618
748
|
for (const chunk of terrain.drainDirtyChunks(limit)) {
|
|
619
749
|
if (chunk.pristine) {
|
|
@@ -634,9 +764,10 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
|
|
|
634
764
|
}
|
|
635
765
|
}
|
|
636
766
|
marks.sync(camera);
|
|
767
|
+
cracks.sync();
|
|
637
768
|
debris.sync();
|
|
638
769
|
},
|
|
639
|
-
stats: () => ({ chunkMeshes: drawn.size, pristineChunks: pristineCount, marksDrawn: marks.mesh.count, debrisDrawn: debris.mesh.count }),
|
|
770
|
+
stats: () => ({ chunkMeshes: drawn.size, pristineChunks: pristineCount, marksDrawn: marks.mesh.count, debrisDrawn: debris.mesh.count, crackQuads: cracks.mesh.geometry.drawRange.count / 6 }),
|
|
640
771
|
dispose() {
|
|
641
772
|
for (const index of [...drawn.keys()]) drop(index);
|
|
642
773
|
for (const geometry of spareGeometries) geometry.dispose();
|
|
@@ -644,6 +775,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
|
|
|
644
775
|
surface.dispose();
|
|
645
776
|
rebarMaterial.dispose();
|
|
646
777
|
marks.dispose();
|
|
778
|
+
cracks.dispose();
|
|
647
779
|
debris.dispose();
|
|
648
780
|
group.removeFromParent();
|
|
649
781
|
}
|
|
@@ -651,6 +783,13 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
|
|
|
651
783
|
}
|
|
652
784
|
|
|
653
785
|
// src/locale.ts
|
|
786
|
+
var PROFILE_NAMES = {
|
|
787
|
+
en: { lawn: "Lawn", dirt: "Dirt", sand: "Sand", road: "Asphalt road", concrete_slab: "Concrete slab", suspended_slab: "Slab over a void", tile_floor: "Tile floor", wood_floor: "Wood floor", stone_paving: "Stone paving" },
|
|
788
|
+
"pt-BR": { lawn: "Grama", dirt: "Terra", sand: "Areia", road: "Asfalto", concrete_slab: "Laje de concreto", suspended_slab: "Laje sobre v\xE3o", tile_floor: "Piso cer\xE2mico", wood_floor: "Assoalho", stone_paving: "Cal\xE7amento de pedra" }
|
|
789
|
+
};
|
|
790
|
+
function terrainProfileName(profileId, localeId) {
|
|
791
|
+
return (PROFILE_NAMES[localeId] ?? PROFILE_NAMES.en)[profileId] ?? profileId;
|
|
792
|
+
}
|
|
654
793
|
var LOCALES = {
|
|
655
794
|
en: {
|
|
656
795
|
name: "Terrain",
|