@drawcall/charta 0.0.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.
Files changed (71) hide show
  1. package/LICENSE +7 -0
  2. package/dist/assets/loader.d.ts +21 -0
  3. package/dist/assets/loader.d.ts.map +1 -0
  4. package/dist/assets/loader.js +113 -0
  5. package/dist/errors.d.ts +11 -0
  6. package/dist/errors.d.ts.map +1 -0
  7. package/dist/errors.js +27 -0
  8. package/dist/grammar.d.ts +29 -0
  9. package/dist/grammar.d.ts.map +1 -0
  10. package/dist/grammar.js +119 -0
  11. package/dist/grass/index.d.ts +25 -0
  12. package/dist/grass/index.d.ts.map +1 -0
  13. package/dist/grass/index.js +177 -0
  14. package/dist/grass/material.d.ts +10 -0
  15. package/dist/grass/material.d.ts.map +1 -0
  16. package/dist/grass/material.js +80 -0
  17. package/dist/index.d.ts +13 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +12 -0
  20. package/dist/interpreter.d.ts +47 -0
  21. package/dist/interpreter.d.ts.map +1 -0
  22. package/dist/interpreter.js +226 -0
  23. package/dist/locations.d.ts +16 -0
  24. package/dist/locations.d.ts.map +1 -0
  25. package/dist/locations.js +58 -0
  26. package/dist/parser.d.ts +9 -0
  27. package/dist/parser.d.ts.map +1 -0
  28. package/dist/parser.js +47 -0
  29. package/dist/pillars/index.d.ts +7 -0
  30. package/dist/pillars/index.d.ts.map +1 -0
  31. package/dist/pillars/index.js +154 -0
  32. package/dist/pillars/material.d.ts +3 -0
  33. package/dist/pillars/material.d.ts.map +1 -0
  34. package/dist/pillars/material.js +43 -0
  35. package/dist/place/index.d.ts +37 -0
  36. package/dist/place/index.d.ts.map +1 -0
  37. package/dist/place/index.js +216 -0
  38. package/dist/tiles/geometry.d.ts +46 -0
  39. package/dist/tiles/geometry.d.ts.map +1 -0
  40. package/dist/tiles/geometry.js +463 -0
  41. package/dist/tiles/index.d.ts +18 -0
  42. package/dist/tiles/index.d.ts.map +1 -0
  43. package/dist/tiles/index.js +121 -0
  44. package/dist/tiles/material.d.ts +6 -0
  45. package/dist/tiles/material.d.ts.map +1 -0
  46. package/dist/tiles/material.js +88 -0
  47. package/dist/utils/instanced-mesh-group.d.ts +17 -0
  48. package/dist/utils/instanced-mesh-group.d.ts.map +1 -0
  49. package/dist/utils/instanced-mesh-group.js +59 -0
  50. package/dist/utils/random.d.ts +4 -0
  51. package/dist/utils/random.d.ts.map +1 -0
  52. package/dist/utils/random.js +19 -0
  53. package/dist/utils/texture.d.ts +3 -0
  54. package/dist/utils/texture.d.ts.map +1 -0
  55. package/dist/utils/texture.js +30 -0
  56. package/dist/walls/index.d.ts +87 -0
  57. package/dist/walls/index.d.ts.map +1 -0
  58. package/dist/walls/index.js +376 -0
  59. package/dist/walls/material.d.ts +3 -0
  60. package/dist/walls/material.d.ts.map +1 -0
  61. package/dist/walls/material.js +67 -0
  62. package/dist/water/index.d.ts +10 -0
  63. package/dist/water/index.d.ts.map +1 -0
  64. package/dist/water/index.js +46 -0
  65. package/dist/water/material.d.ts +5 -0
  66. package/dist/water/material.d.ts.map +1 -0
  67. package/dist/water/material.js +46 -0
  68. package/dist/water/texture.d.ts +15 -0
  69. package/dist/water/texture.d.ts.map +1 -0
  70. package/dist/water/texture.js +201 -0
  71. package/package.json +39 -0
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Modifies an existing Three.js material to support texture array blending based on textureStrength
3
+ */
4
+ export function buildTilesMaterial(material, textureArray) {
5
+ material.onBeforeCompile = (shader) => {
6
+ // Add uniforms
7
+ shader.uniforms.uTextureArray = { value: textureArray };
8
+ // Vertex shader modifications
9
+ shader.vertexShader = shader.vertexShader
10
+ .replace("#include <common>", `#include <common>
11
+ attribute mat4 textureStrength;
12
+ varying mat4 vTextureStrength;
13
+ #ifndef USE_MAP
14
+ varying vec2 vUv;
15
+ #endif`)
16
+ .replace("#include <begin_vertex>", `#include <begin_vertex>
17
+ vTextureStrength = textureStrength;
18
+ #ifndef USE_MAP
19
+ vUv = uv;
20
+ #endif`);
21
+ // Fragment shader modifications
22
+ shader.fragmentShader = shader.fragmentShader.replace("#include <common>", `#include <common>
23
+ uniform sampler2DArray uTextureArray;
24
+ varying mat4 vTextureStrength;
25
+ #ifndef USE_MAP
26
+ varying vec2 vUv;
27
+ #endif
28
+
29
+ vec2 hash2( vec2 p ) {
30
+ return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453);
31
+ }
32
+
33
+ float voronoi( in vec2 x ) {
34
+ vec2 n = floor( x );
35
+ vec2 f = fract( x );
36
+ float m = 8.0;
37
+ for( int j=-1; j<=1; j++ )
38
+ for( int i=-1; i<=1; i++ ) {
39
+ vec2 g = vec2( float(i), float(j) );
40
+ vec2 o = hash2( n + g );
41
+ vec2 r = g - f + o;
42
+ float d = dot( r, r );
43
+ m = min( m, d );
44
+ }
45
+ return sqrt(m);
46
+ }`);
47
+ // Apply texture blending
48
+ shader.fragmentShader = shader.fragmentShader.replace("#include <map_fragment>", `#include <map_fragment>
49
+ vec3 blendedColor = vec3(0.0);
50
+
51
+ // Use prime number scale for noise to avoid grid alignment
52
+ float noise = voronoi(vUv * 0.6);
53
+ float mixVal = smoothstep(0.4, 0.6, noise);
54
+
55
+ float weightSum = 0.001;
56
+
57
+ // Generate a noise value for mixing layers
58
+ float layerNoise = voronoi(vUv * 0.5);
59
+
60
+ for (int i = 0; i < 4; i++) {
61
+ for (int j = 0; j < 4; j++) {
62
+ float baseWeight = vTextureStrength[i][j];
63
+
64
+ // Only process if weight is significant
65
+ if (baseWeight > 0.001) {
66
+ // Modulate weight with noise to create organic transitions between layers
67
+ // Using pow to sharpen the transition
68
+ float noiseWeight = smoothstep(0.4, 0.6, baseWeight * layerNoise + baseWeight * 0.9);
69
+ float w = baseWeight * noiseWeight;
70
+
71
+ int layerIndex = i * 4 + j;
72
+
73
+ vec3 tex1 = texture(uTextureArray, vec3(vUv, float(layerIndex))).rgb;
74
+ vec3 tex2 = texture(uTextureArray, vec3(-vUv + 0.35, float(layerIndex))).rgb;
75
+
76
+ vec3 texSample = mix(tex1, tex2, mixVal) * (voronoi(vUv * 1.5 + 12.0) * 0.2 + 0.9);
77
+
78
+ blendedColor += texSample * w;
79
+ weightSum += w;
80
+ }
81
+ }
82
+ }
83
+
84
+ diffuseColor.rgb = blendedColor / weightSum;`);
85
+ };
86
+ material.needsUpdate = true;
87
+ return material;
88
+ }
@@ -0,0 +1,17 @@
1
+ import { Group, InstancedMesh, Matrix4, Object3D } from "three";
2
+ interface InstancedMeshEntry {
3
+ mesh: InstancedMesh;
4
+ offsetMatrix: Matrix4;
5
+ }
6
+ export declare class InstancedMeshGroup extends Group {
7
+ entries: InstancedMeshEntry[];
8
+ constructor(object: Object3D, count: number, matrices?: Array<Matrix4>);
9
+ setMatrixAt(index: number, matrix: Matrix4): void;
10
+ /**
11
+ * Disposes all internal InstancedMeshes.
12
+ * Note: Does not dispose geometries or materials as they might be shared.
13
+ */
14
+ dispose(): void;
15
+ }
16
+ export {};
17
+ //# sourceMappingURL=instanced-mesh-group.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instanced-mesh-group.d.ts","sourceRoot":"","sources":["../../src/utils/instanced-mesh-group.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,OAAO,EAAQ,QAAQ,EAAS,MAAM,OAAO,CAAC;AAE7E,UAAU,kBAAkB;IAC1B,IAAI,EAAE,aAAa,CAAC;IACpB,YAAY,EAAE,OAAO,CAAC;CACvB;AAED,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,OAAO,EAAE,kBAAkB,EAAE,CAAM;gBAEvB,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC;IA6CtE,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO;IAiB1C;;;OAGG;IACH,OAAO;CAOR"}
@@ -0,0 +1,59 @@
1
+ import { Group, InstancedMesh, Matrix4 } from "three";
2
+ export class InstancedMeshGroup extends Group {
3
+ entries = [];
4
+ constructor(object, count, matrices) {
5
+ super();
6
+ // Ensure the source object has its world matrices up to date
7
+ object.updateMatrixWorld(true);
8
+ const rootInverse = new Matrix4().copy(object.matrixWorld).invert();
9
+ object.traverse((child) => {
10
+ if (child.isMesh) {
11
+ const mesh = child;
12
+ // Calculate the offset matrix relative to the root object
13
+ const offsetMatrix = new Matrix4().multiplyMatrices(rootInverse, mesh.matrixWorld);
14
+ // Create InstancedMesh with the same geometry and material
15
+ // We clone the geometry to avoid side effects if the original is modified,
16
+ // but sharing is also possible. Given we don't modify it, sharing is fine usually.
17
+ // However, InstancedMesh might modify the geometry (e.g. attribute updates?).
18
+ // Usually it's safe to share geometry.
19
+ const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, count);
20
+ // Copy shadow properties
21
+ instancedMesh.castShadow = mesh.castShadow;
22
+ instancedMesh.receiveShadow = mesh.receiveShadow;
23
+ this.add(instancedMesh);
24
+ this.entries.push({ mesh: instancedMesh, offsetMatrix });
25
+ }
26
+ });
27
+ if (matrices != null) {
28
+ for (let i = 0; i < Math.min(count, matrices.length); i++) {
29
+ this.setMatrixAt(i, matrices[i]);
30
+ }
31
+ }
32
+ }
33
+ setMatrixAt(index, matrix) {
34
+ const instanceMatrix = new Matrix4();
35
+ for (const entry of this.entries) {
36
+ // Calculate the final matrix for this part:
37
+ // PartWorld = InstanceRootWorld * PartOffset
38
+ instanceMatrix.multiplyMatrices(matrix, entry.offsetMatrix);
39
+ entry.mesh.setMatrixAt(index, instanceMatrix);
40
+ }
41
+ // Mark all as needing update
42
+ for (const entry of this.entries) {
43
+ if (entry.mesh.instanceMatrix) {
44
+ entry.mesh.instanceMatrix.needsUpdate = true;
45
+ }
46
+ }
47
+ }
48
+ /**
49
+ * Disposes all internal InstancedMeshes.
50
+ * Note: Does not dispose geometries or materials as they might be shared.
51
+ */
52
+ dispose() {
53
+ for (const entry of this.entries) {
54
+ entry.mesh.dispose();
55
+ }
56
+ this.entries = [];
57
+ this.clear();
58
+ }
59
+ }
@@ -0,0 +1,4 @@
1
+ export declare function createRng(seed: string | number): () => number;
2
+ export declare function hashStringToUint32(input: string, seed?: number): number;
3
+ export declare function hashNumbersToUint32(...nums: number[]): number;
4
+ //# sourceMappingURL=random.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../../src/utils/random.ts"],"names":[],"mappings":"AAGA,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,MAAM,CAG7D;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,GAAE,MAAU,GAAG,MAAM,CAE1E;AAED,wBAAgB,mBAAmB,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAQ7D"}
@@ -0,0 +1,19 @@
1
+ import seedrandom from "seedrandom";
2
+ import MurmurHash3 from "imurmurhash";
3
+ export function createRng(seed) {
4
+ const rng = seedrandom(String(seed));
5
+ return () => rng();
6
+ }
7
+ export function hashStringToUint32(input, seed = 0) {
8
+ return new MurmurHash3(input, seed).result() >>> 0;
9
+ }
10
+ export function hashNumbersToUint32(...nums) {
11
+ let hasher = new MurmurHash3("", 0);
12
+ for (let i = 0; i < nums.length; i++) {
13
+ // Use '|' separator to avoid ambiguities; stringify to be simple and robust
14
+ if (i > 0)
15
+ hasher = hasher.hash("|");
16
+ hasher = hasher.hash(String(nums[i] >>> 0));
17
+ }
18
+ return hasher.result() >>> 0;
19
+ }
@@ -0,0 +1,3 @@
1
+ import { Texture, DataArrayTexture } from "three";
2
+ export declare function buildTextureArrayFromAssets(textures: Texture[]): DataArrayTexture;
3
+ //# sourceMappingURL=texture.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"texture.d.ts","sourceRoot":"","sources":["../../src/utils/texture.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,gBAAgB,EAIjB,MAAM,OAAO,CAAC;AAEf,wBAAgB,2BAA2B,CACzC,QAAQ,EAAE,OAAO,EAAE,GAClB,gBAAgB,CA6BlB"}
@@ -0,0 +1,30 @@
1
+ import { DataArrayTexture, LinearMipMapLinearFilter, LinearFilter, RepeatWrapping, } from "three";
2
+ export function buildTextureArrayFromAssets(textures) {
3
+ const images = textures.map((t) => t.image);
4
+ if (images.some((image) => image == null)) {
5
+ return new DataArrayTexture();
6
+ }
7
+ const width = images[0].width;
8
+ const height = images[0].height;
9
+ const depth = images.length;
10
+ const canvas = document.createElement("canvas");
11
+ canvas.width = width;
12
+ canvas.height = height;
13
+ const ctx = canvas.getContext("2d");
14
+ const data = new Uint8Array(width * height * 4 * depth);
15
+ for (let i = 0; i < depth; i++) {
16
+ const img = images[i];
17
+ ctx.clearRect(0, 0, width, height);
18
+ ctx.drawImage(img, 0, 0, width, height);
19
+ const imgData = ctx.getImageData(0, 0, width, height).data;
20
+ data.set(imgData, i * width * height * 4);
21
+ }
22
+ const arrayTex = new DataArrayTexture(data, width, height, depth);
23
+ arrayTex.needsUpdate = true;
24
+ arrayTex.minFilter = LinearMipMapLinearFilter;
25
+ arrayTex.magFilter = LinearFilter;
26
+ arrayTex.wrapS = RepeatWrapping;
27
+ arrayTex.wrapT = RepeatWrapping;
28
+ arrayTex.generateMipmaps = true;
29
+ return arrayTex;
30
+ }
@@ -0,0 +1,87 @@
1
+ import { Material, Mesh, Vector2Tuple, Vector3Tuple } from 'three';
2
+ import { Interpreter } from '../interpreter.js';
3
+ import { coerce } from 'zod';
4
+ import { TilesGeometry } from '../tiles/geometry.js';
5
+ import { ErrorLocation } from '../errors.js';
6
+ export type WindowData = {
7
+ offsetX: number;
8
+ bottomY: number;
9
+ topY: number;
10
+ width: number;
11
+ };
12
+ export type WallData = {
13
+ x: number;
14
+ z: number;
15
+ xzSize: Vector2Tuple;
16
+ rotationY: number;
17
+ yStart: Vector3Tuple;
18
+ yEnd: Vector3Tuple;
19
+ textureId: number;
20
+ windows: WindowData[];
21
+ };
22
+ export declare const windowSchema: import("zod").ZodObject<{
23
+ offsetX: import("zod").ZodOptional<coerce.ZodCoercedNumber<unknown>>;
24
+ bottomY: import("zod").ZodOptional<coerce.ZodCoercedNumber<unknown>>;
25
+ topY: import("zod").ZodOptional<coerce.ZodCoercedNumber<unknown>>;
26
+ width: import("zod").ZodOptional<coerce.ZodCoercedNumber<unknown>>;
27
+ }, import("zod/v4/core").$strip>;
28
+ export declare const doorSchema: import("zod").ZodObject<{
29
+ offsetX: import("zod").ZodOptional<coerce.ZodCoercedNumber<unknown>>;
30
+ bottomY: import("zod").ZodOptional<coerce.ZodCoercedNumber<unknown>>;
31
+ topY: import("zod").ZodOptional<coerce.ZodCoercedNumber<unknown>>;
32
+ height: import("zod").ZodOptional<coerce.ZodCoercedNumber<unknown>>;
33
+ width: import("zod").ZodOptional<coerce.ZodCoercedNumber<unknown>>;
34
+ }, import("zod/v4/core").$strip>;
35
+ export declare const wallSchema: import("zod").ZodObject<{
36
+ dir: import("zod").ZodEnum<{
37
+ top: "top";
38
+ bottom: "bottom";
39
+ left: "left";
40
+ right: "right";
41
+ }>;
42
+ texture: import("zod").ZodString;
43
+ bottomY: import("zod").ZodOptional<coerce.ZodCoercedNumber<unknown>>;
44
+ topY: import("zod").ZodOptional<coerce.ZodCoercedNumber<unknown>>;
45
+ }, import("zod/v4/core").$strip>;
46
+ export declare const WALL_CONFIG: {
47
+ readonly top: {
48
+ readonly zOffset: -0.5;
49
+ readonly rotation: 0;
50
+ readonly axis: "z";
51
+ readonly sampleDir: 1;
52
+ };
53
+ readonly bottom: {
54
+ readonly zOffset: 0.5;
55
+ readonly rotation: 0;
56
+ readonly axis: "z";
57
+ readonly sampleDir: 1;
58
+ };
59
+ readonly left: {
60
+ readonly xOffset: -0.5;
61
+ readonly rotation: number;
62
+ readonly axis: "x";
63
+ readonly sampleDir: -1;
64
+ };
65
+ readonly right: {
66
+ readonly xOffset: 0.5;
67
+ readonly rotation: number;
68
+ readonly axis: "x";
69
+ readonly sampleDir: -1;
70
+ };
71
+ };
72
+ export declare const WALL_CONNECTION_TOLERANCE_RATIO: number;
73
+ export declare function computeWallVerticalBounds(interpreter: Interpreter, tilesGeometry: TilesGeometry, row: number, col: number, wallIdx: number, parsed: {
74
+ bottomY?: number;
75
+ topY?: number;
76
+ dir: string;
77
+ }, config: (typeof WALL_CONFIG)[keyof typeof WALL_CONFIG], loc: ErrorLocation): {
78
+ yStart: Vector3Tuple;
79
+ yEnd: Vector3Tuple;
80
+ worldWallX: number;
81
+ worldWallZ: number;
82
+ } | undefined;
83
+ export declare class WallMesh extends Mesh {
84
+ constructor(interpreter: Interpreter, material?: Material);
85
+ }
86
+ export declare function isLayerConnectedToWall(wallTopY: number, wallBotY: number, y: number, tileSize: number): boolean;
87
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/walls/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,QAAQ,EAER,IAAI,EAGJ,YAAY,EAEZ,YAAY,EAOb,MAAM,OAAO,CAAA;AAEd,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,MAAM,EAAyC,MAAM,KAAK,CAAA;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAGpD,OAAO,EAAe,aAAa,EAAE,MAAM,cAAc,CAAA;AAMzD,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;IACT,MAAM,EAAE,YAAY,CAAA;IACpB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,YAAY,CAAA;IACpB,IAAI,EAAE,YAAY,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,OAAO,EAAE,UAAU,EAAE,CAAA;CACtB,CAAA;AAED,eAAO,MAAM,YAAY;;;;;gCAKvB,CAAA;AAEF,eAAO,MAAM,UAAU;;;;;;gCAMrB,CAAA;AAEF,eAAO,MAAM,UAAU;;;;;;;;;;gCAKrB,CAAA;AAEF,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;CAed,CAAA;AAEV,eAAO,MAAM,+BAA+B,QAAQ,CAAA;AAEpD,wBAAgB,yBAAyB,CACvC,WAAW,EAAE,WAAW,EACxB,aAAa,EAAE,aAAa,EAC5B,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,MAAM,EACf,MAAM,EAAE;IAAE,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EACxD,MAAM,EAAE,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,OAAO,WAAW,CAAC,EACtD,GAAG,EAAE,aAAa;;;;;cAqInB;AAED,qBAAa,QAAS,SAAQ,IAAI;gBACpB,WAAW,EAAE,WAAW,EAAE,QAAQ,GAAE,QAAkC;CA6QnF;AAED,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,WAGrG"}