forked from M-CreativeLab/jsar-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimpl-babylonjs.ts
203 lines (185 loc) · 6.21 KB
/
impl-babylonjs.ts
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
/// <reference path="node_modules/@types/wicg-file-system-access/index.d.ts" />
import {
SpatialDocumentImpl,
DOMParser,
NativeDocument,
NativeEngine,
RequestManager,
ResourceLoader,
UserAgent,
UserAgentInit,
JSARDOM,
} from '../src';
import 'babylonjs';
import { canParseURL } from '../src/living/helpers/url';
interface EngineOnBabylonjs extends BABYLON.Engine, EventTarget { }
class EngineOnBabylonjs extends BABYLON.Engine implements NativeEngine {
// TODO
}
class HeadlessResourceLoader implements ResourceLoader {
fetch(url: string, options: { accept?: string; cookieJar?: any; referrer?: string; }, returnsAs: 'string'): Promise<string>;
fetch(url: string, options: { accept?: string; cookieJar?: any; referrer?: string; }, returnsAs: 'json'): Promise<object>;
fetch(url: string, options: { accept?: string; cookieJar?: any; referrer?: string; }, returnsAs: 'arraybuffer'): Promise<ArrayBuffer>;
fetch<T = string | object | ArrayBuffer>(url: string, options: { accept?: string; cookieJar?: any; referrer?: string; }, returnsAs?: 'string' | 'json' | 'arraybuffer'): Promise<T>;
fetch(url: string, options: { accept?: string; cookieJar?: any; referrer?: string; }, returnsAs?: 'string' | 'json' | 'arraybuffer'): Promise<object> | Promise<ArrayBuffer> | Promise<string> {
if (!canParseURL(url)) {
throw new TypeError('Invalid URL');
}
const urlObj = new URL(url);
if (urlObj.protocol === 'file:') {
throw new TypeError('file: protocol is not supported');
} else {
return fetch(url, options)
.then((resp) => {
if (returnsAs === 'string') {
return resp.text();
} else if (returnsAs === 'json') {
return resp.json();
} else if (returnsAs === 'arraybuffer') {
return resp.arrayBuffer();
}
});
}
}
}
class UserAgentOnBabylonjs implements UserAgent {
versionString: string = '1.0';
vendor: string = '';
vendorSub: string = '';
language: string = 'zh-CN';
languages: readonly string[] = [
'zh-CN',
'en-US',
];
defaultStylesheet: string;
devicePixelRatio: number;
domParser: DOMParser;
resourceLoader: ResourceLoader;
requestManager: RequestManager;
constructor(init: UserAgentInit) {
this.defaultStylesheet = init.defaultStylesheet;
this.devicePixelRatio = init.devicePixelRatio;
this.resourceLoader = new HeadlessResourceLoader();
// this.requestManager = null;
}
alert(message?: string): void {
throw new Error('Method not implemented.');
}
confirm(message?: string): boolean {
throw new Error('Method not implemented.');
}
prompt(message?: string, defaultValue?: string): string {
throw new Error('Method not implemented.');
}
}
class NativeDocumentOnBabylonjs extends EventTarget implements NativeDocument {
engine: NativeEngine;
userAgent: UserAgent;
baseURI: string;
console: Console;
attachedDocument: SpatialDocumentImpl;
closed: boolean = false;
private _scene: BABYLON.Scene;
private _preloadMeshes: Map<string, Array<BABYLON.AbstractMesh | BABYLON.TransformNode>> = new Map();
private _preloadAnimationGroups: Map<string, BABYLON.AnimationGroup[]> = new Map();
constructor(canvas: HTMLCanvasElement) {
super();
this.engine = new EngineOnBabylonjs(canvas, true);
this.userAgent = new UserAgentOnBabylonjs({
defaultStylesheet: '',
devicePixelRatio: 1,
});
this.console = globalThis.console;
this._scene = new BABYLON.Scene(this.engine);
this._scene.clearColor = new BABYLON.Color4(0.5, 0.5, 0.5, 1);
this._scene.debugLayer.show({
showInspector: true,
globalRoot: document.getElementById('babylonjs-root') as HTMLDivElement,
});
const hdrTexture = BABYLON.CubeTexture.CreateFromPrefilteredData(
'https://assets.babylonjs.com/environments/environmentSpecular.env', this._scene);
this._scene.environmentTexture = hdrTexture;
// create camera and targets
const camera = new BABYLON.ArcRotateCamera(
'camera',
Math.PI / 2,
Math.PI / 2,
5,
BABYLON.Vector3.Zero(),
this._scene
);
camera.setPosition(new BABYLON.Vector3(0, 0, -10));
camera.setTarget(BABYLON.Vector3.Zero());
camera.attachControl(canvas, true);
const light = new BABYLON.HemisphericLight('light', new BABYLON.Vector3(0, 1, 0), this._scene);
light.intensity = 0.7;
this.engine.runRenderLoop(() => {
this._scene.render();
});
window.addEventListener('resize', () => {
this.engine.resize();
});
}
getNativeScene(): BABYLON.Scene {
return this._scene;
}
getContainerPose(): XRPose {
throw new Error('Method not implemented.');
}
getPreloadedMeshes(): Map<string, Array<BABYLON.AbstractMesh | BABYLON.TransformNode>> {
return this._preloadMeshes;
}
getPreloadedAnimationGroups(): Map<string, BABYLON.AnimationGroup[]> {
return this._preloadAnimationGroups;
}
observeInputEvent(name?: string): void {
throw new Error('Method not implemented.');
}
createBoundTransformNode(nameOrId: string): BABYLON.TransformNode {
throw new Error('Method not implemented.');
}
stop(): void {
// TODO
}
close(): void {
this.engine.stopRenderLoop();
this.engine.dispose();
this._scene.dispose();
}
}
let currentDom: JSARDOM;
document.addEventListener('DOMContentLoaded', async () => {
const canvas = document.getElementById('renderCanvas');
const urlInput = document.getElementById('url-input') as HTMLInputElement;
const selectBtn = document.getElementById('run-btn');
selectBtn?.addEventListener('click', async () => {
const entryXsmlCode = await (await fetch(urlInput?.value)).text();
await load(entryXsmlCode, urlInput?.value);
});
load(`
<xsml>
<head>
<style>
cube {
rotation: 0 45 30;
}
</style>
</head>
<space>
<cube />
</space>
</xsml>
`);
async function load(code: string, urlBase: string = 'https://example.com/') {
if (currentDom) {
await currentDom.unload();
}
const nativeDocument = new NativeDocumentOnBabylonjs(canvas as HTMLCanvasElement);
currentDom = new JSARDOM(code, {
url: urlBase,
nativeDocument,
});
await currentDom.load();
console.log(currentDom);
}
});