diff --git a/index.html b/index.html index d7554fd..d8ee7df 100644 --- a/index.html +++ b/index.html @@ -1,6 +1,7 @@ + + diff --git a/scene/example.pr b/scene/example.pr index 4bd3bf6..81a3f19 100644 --- a/scene/example.pr +++ b/scene/example.pr @@ -39,14 +39,6 @@ >>>>> - - - 1.0] - - >>> - diff --git a/src/engine.ts b/src/engine.ts index 809638e..f22a0e6 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -110,7 +110,7 @@ export async function startEngine( } }; - document.body.onclick = enableVR; + canvas.onclick = enableVR; let leanBase: { position: Vector3 } | null = null; let recenterBase: { rotation: Quaternion } | null = null; diff --git a/src/index.ts b/src/index.ts index 1921c7c..cdc7dcf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,10 @@ -import { is, Dataspace, Embedded, Reader, Ref, Schemas, Sturdy } from "@syndicate-lang/core"; +import { is, Dataspace, Embedded, Reader, Ref, Schemas, Sturdy, Turn } from "@syndicate-lang/core"; import * as html from "@syndicate-lang/html"; import * as timer from "@syndicate-lang/timer"; import * as wsRelay from "@syndicate-lang/ws-relay"; import * as wakeDetector from './wake-detector.js'; import * as Shapes from './gen/shapes.js'; +import { md5 } from './md5.js'; import { Engine, @@ -70,6 +71,7 @@ function bootApp(ds: Ref, scene: Scene) { at remoteDs { during SceneHandle($sceneDs_e: Embedded) => { const id = uuid(); + const thisFacet = Turn.activeFacet; const sceneDs = sceneDs_e.embeddedValue; interpretScene(id, scene, sceneDs); @@ -77,7 +79,7 @@ function bootApp(ds: Ref, scene: Scene) { field rotation: Shapes.Vector3 = Shapes.Vector3({ x:0, y:0, z:0 }); at ds { - const refreshPeriod = Math.floor(1000 / 30); + const refreshPeriod = Math.floor(1000 / 10); on message timer.PeriodicTick(refreshPeriod) => { const camera = scene.cameras[0]; if (camera && camera instanceof TargetCamera) { @@ -95,6 +97,13 @@ function bootApp(ds: Ref, scene: Scene) { } } + field email: string = id; + const emailInput = document.getElementById('emailInput') as HTMLInputElement; + emailInput.value = id; + emailInput.addEventListener('keyup', () => thisFacet.turn(() => { + email.value = emailInput.value; + })); + at sceneDs { assert Shapes.Sprite({ name: id, @@ -105,7 +114,19 @@ function bootApp(ds: Ref, scene: Scene) { B.scale({ x: 0.4, y: 1.4, z: 0.1 }, B.box()))), B.rotate(rotation.value, - B.scale({ x: 0.15, y: 0.23, z: 0.18 }, B.box())), + B.scale({ x: 0.15, y: 0.23, z: 0.18 }, B.many([ + B.box(), + B.move({ x: 0, y: 0, z: 0.501 }, + B.texture( + Shapes.TextureSpec.uvAlpha({ + path: `https://www.gravatar.com/avatar/${md5(new TextEncoder().encode(email.value.trim()))}?s=256&d=wavatar`, + scale: Shapes.Vector2({ x:1, y:1 }), + offset: Shapes.Vector2({ x:0, y:0 }), + alpha: 1 + }), + B.rotate({ x: 0, y: Math.PI, z: 0 }, + B.plane()))), + ]))), ]))), }); } diff --git a/src/md5.ts b/src/md5.ts new file mode 100644 index 0000000..cacf7a6 --- /dev/null +++ b/src/md5.ts @@ -0,0 +1,167 @@ +/* + MD5 code originally written by Joseph Myers + (Public Domain per https://github.com/satazor/js-spark-md5/issues/45) + http://www.myersdaily.org/joseph/javascript/md5-text.html + http://www.myersdaily.org/joseph/javascript/md5.js + + Lightly cleaned up and otherwise modified by Tony Garnock-Jones + , August 2022, to work with Uint8Array and + TypeScript. +*/ + +function md5cycle(x: Uint32Array, k: Uint32Array) { + let a = x[0], b = x[1], c = x[2], d = x[3]; + + a = ff(a, b, c, d, k[0], 7, -680876936); + d = ff(d, a, b, c, k[1], 12, -389564586); + c = ff(c, d, a, b, k[2], 17, 606105819); + b = ff(b, c, d, a, k[3], 22, -1044525330); + a = ff(a, b, c, d, k[4], 7, -176418897); + d = ff(d, a, b, c, k[5], 12, 1200080426); + c = ff(c, d, a, b, k[6], 17, -1473231341); + b = ff(b, c, d, a, k[7], 22, -45705983); + a = ff(a, b, c, d, k[8], 7, 1770035416); + d = ff(d, a, b, c, k[9], 12, -1958414417); + c = ff(c, d, a, b, k[10], 17, -42063); + b = ff(b, c, d, a, k[11], 22, -1990404162); + a = ff(a, b, c, d, k[12], 7, 1804603682); + d = ff(d, a, b, c, k[13], 12, -40341101); + c = ff(c, d, a, b, k[14], 17, -1502002290); + b = ff(b, c, d, a, k[15], 22, 1236535329); + + a = gg(a, b, c, d, k[1], 5, -165796510); + d = gg(d, a, b, c, k[6], 9, -1069501632); + c = gg(c, d, a, b, k[11], 14, 643717713); + b = gg(b, c, d, a, k[0], 20, -373897302); + a = gg(a, b, c, d, k[5], 5, -701558691); + d = gg(d, a, b, c, k[10], 9, 38016083); + c = gg(c, d, a, b, k[15], 14, -660478335); + b = gg(b, c, d, a, k[4], 20, -405537848); + a = gg(a, b, c, d, k[9], 5, 568446438); + d = gg(d, a, b, c, k[14], 9, -1019803690); + c = gg(c, d, a, b, k[3], 14, -187363961); + b = gg(b, c, d, a, k[8], 20, 1163531501); + a = gg(a, b, c, d, k[13], 5, -1444681467); + d = gg(d, a, b, c, k[2], 9, -51403784); + c = gg(c, d, a, b, k[7], 14, 1735328473); + b = gg(b, c, d, a, k[12], 20, -1926607734); + + a = hh(a, b, c, d, k[5], 4, -378558); + d = hh(d, a, b, c, k[8], 11, -2022574463); + c = hh(c, d, a, b, k[11], 16, 1839030562); + b = hh(b, c, d, a, k[14], 23, -35309556); + a = hh(a, b, c, d, k[1], 4, -1530992060); + d = hh(d, a, b, c, k[4], 11, 1272893353); + c = hh(c, d, a, b, k[7], 16, -155497632); + b = hh(b, c, d, a, k[10], 23, -1094730640); + a = hh(a, b, c, d, k[13], 4, 681279174); + d = hh(d, a, b, c, k[0], 11, -358537222); + c = hh(c, d, a, b, k[3], 16, -722521979); + b = hh(b, c, d, a, k[6], 23, 76029189); + a = hh(a, b, c, d, k[9], 4, -640364487); + d = hh(d, a, b, c, k[12], 11, -421815835); + c = hh(c, d, a, b, k[15], 16, 530742520); + b = hh(b, c, d, a, k[2], 23, -995338651); + + a = ii(a, b, c, d, k[0], 6, -198630844); + d = ii(d, a, b, c, k[7], 10, 1126891415); + c = ii(c, d, a, b, k[14], 15, -1416354905); + b = ii(b, c, d, a, k[5], 21, -57434055); + a = ii(a, b, c, d, k[12], 6, 1700485571); + d = ii(d, a, b, c, k[3], 10, -1894986606); + c = ii(c, d, a, b, k[10], 15, -1051523); + b = ii(b, c, d, a, k[1], 21, -2054922799); + a = ii(a, b, c, d, k[8], 6, 1873313359); + d = ii(d, a, b, c, k[15], 10, -30611744); + c = ii(c, d, a, b, k[6], 15, -1560198380); + b = ii(b, c, d, a, k[13], 21, 1309151649); + a = ii(a, b, c, d, k[4], 6, -145523070); + d = ii(d, a, b, c, k[11], 10, -1120210379); + c = ii(c, d, a, b, k[2], 15, 718787259); + b = ii(b, c, d, a, k[9], 21, -343485551); + + x[0] = add32(a, x[0]); + x[1] = add32(b, x[1]); + x[2] = add32(c, x[2]); + x[3] = add32(d, x[3]); + +} + +function cmn(q: number, a: number, b: number, x: number, s: number, t: number): number { + a = add32(add32(a, q), add32(x, t)); + return add32((a << s) | (a >>> (32 - s)), b); +} + +function ff(a: number, b: number, c: number, d: number, x: number, s: number, t: number): number { + return cmn((b & c) | ((~b) & d), a, b, x, s, t); +} + +function gg(a: number, b: number, c: number, d: number, x: number, s: number, t: number): number { + return cmn((b & d) | (c & (~d)), a, b, x, s, t); +} + +function hh(a: number, b: number, c: number, d: number, x: number, s: number, t: number): number { + return cmn(b ^ c ^ d, a, b, x, s, t); +} + +function ii(a: number, b: number, c: number, d: number, x: number, s: number, t: number): number { + return cmn(c ^ (b | (~d)), a, b, x, s, t); +} + +function md51(s: Uint8Array): Uint8Array { + const n = s.length; + const state = Uint32Array.from([1732584193, -271733879, -1732584194, 271733878]); + + let i: number; + + for (i = 64; i <= s.length; i += 64) { + md5cycle(state, md5blk(s.subarray(i - 64, i))); + } + s = s.subarray(i - 64); + + const tail = new Uint32Array(16); + for (i = 0; i < s.length; i++) tail[i >> 2] |= s[i] << ((i % 4) << 3); + tail[i >> 2] |= 0x80 << ((i % 4) << 3); + + if (i > 55) { + md5cycle(state, tail); + for (i = 0; i < 16; i++) tail[i] = 0; + } + tail[14] = n * 8; + md5cycle(state, tail); + + return new Uint8Array(state.buffer); +} + +function md5blk(s: Uint8Array): Uint32Array { + const md5blks = new Uint32Array(16); + let j = 0; + for (let i = 0; i < md5blks.length; i++, j+=4) { + md5blks[i] = s[j] + (s[j + 1] << 8) + (s[j + 2] << 16) + (s[j + 3] << 24); + } + return md5blks; +} + +const HEX_CHR = '0123456789abcdef'; +function rhex(digits: Array, n: number): void { + digits.push(HEX_CHR[(n >> 4) & 15]); + digits.push(HEX_CHR[n & 15]); +} + +export function hex(x: Uint8Array): string { + const result: Array = []; + for (let i = 0; i < x.length; i++) rhex(result, x[i]); + return result.join(''); +} + +export function md5(s: Uint8Array): string { + return hex(md51(s)); +} + +function add32(a: number, b: number): number { + return (a + b) & 0xFFFFFFFF; +} + +if (md5(new TextEncoder().encode('hello')) != '5d41402abc4b2a76b9719d911017c592') { + throw new Error('invalid md5 result'); +} diff --git a/src/shapes.ts b/src/shapes.ts index 75be4f4..4153046 100644 --- a/src/shapes.ts +++ b/src/shapes.ts @@ -15,7 +15,7 @@ import { Vector2, Vector3, } from '@babylonjs/core/Legacy/legacy'; -import { Value, is } from "@syndicate-lang/core"; +import { KeyedDictionary, Value, is } from "@syndicate-lang/core"; import * as Shapes from './gen/shapes.js'; @@ -91,7 +91,7 @@ export class ShapeTree { } remove() { - this.node?.dispose(false, true); + this.node?.dispose(); } static empty(name: string, scene: Scene): ShapeTree { @@ -121,7 +121,20 @@ function applyCustomizer(m: ShapeTree, c: MeshCustomizer) { Object.values(c).forEach(f => f(m)); } -function buildTexture(name: string, scene: Scene, spec: Shapes.TextureSpec): Material { +type CachedTexture = { + key: Value, + referenceCount: number, + material: Material, +}; +const textureCache = new KeyedDictionary(); + +function buildTexture(name: string, scene: Scene, spec: Shapes.TextureSpec): CachedTexture { + const cacheKey = Shapes.fromTextureSpec(spec); + const entry = textureCache.get(cacheKey); + if (entry !== void 0) { + entry.referenceCount++; + return entry; + } const mat = new StandardMaterial(name, scene); const tex = new Texture(spec.path, scene); mat.diffuseTexture = tex; @@ -142,7 +155,20 @@ function buildTexture(name: string, scene: Scene, spec: Shapes.TextureSpec): Mat break; } } - return mat; + const newEntry = { + key: cacheKey, + referenceCount: 1, + material: mat, + }; + textureCache.set(cacheKey, newEntry); + return newEntry; +} + +function releaseTexture(entry: CachedTexture) { + if (--entry.referenceCount === 0) { + textureCache.delete(entry.key); + entry.material.dispose(); + } } export function build(name: string, scene: Scene, shape: Shapes.Shape, customize: MeshCustomizer): ShapeTree { @@ -218,11 +244,13 @@ export function build(name: string, scene: Scene, shape: Shapes.Shape, customize } case "Texture": { - const mat = buildTexture(name + '.texture', scene, shape.value.spec); - return build(name + '.inner', scene, shape.value.shape, { + const entry = buildTexture(name + '.texture', scene, shape.value.spec); + const t = build(name + '.inner', scene, shape.value.shape, { ... customize, - material: m => m.node.material = mat, + material: m => m.node.material = entry.material, }); + t.cleanups.push(() => releaseTexture(entry)); + return t; } case "Color": {