-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebComponent.js
More file actions
453 lines (380 loc) · 10.9 KB
/
WebComponent.js
File metadata and controls
453 lines (380 loc) · 10.9 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
// pick a better name! quick!
const ElementName = `popengine-canvas`;
export default ElementName;
import * as Pop from './PopEngine.js'
import Camera_t from './Camera.js'
import AssetManager from './AssetManager.js'
import {CompileShader} from './AssetManager.js'
import {CreatePromise} from './PromiseQueue.js'
export class Renderer_t
{
constructor(Canvas,GetRenderCommands,XrOnWaitForCallback)
{
// content
this.Camera = new Camera_t();
this.Camera.LookAt = [0,1.5,0.0];
this.Camera.FovVertical = 80;
const Distance = 3.0;
this.Camera.Position = [-Distance,2.0,-Distance];
// control
this.Running = true;
this.RenderingPromise = this.RenderThread( Canvas, GetRenderCommands, XrOnWaitForCallback );
}
GetRenderContext()
{
return this.RenderContext;
}
Free()
{
this.Running = false;
}
async RenderThread(Canvas,GetRenderCommands,XrOnWaitForCallback)
{
this.RenderView = new Pop.Gui.RenderView(null,Canvas);
this.RenderContext = new Pop.Opengl.Context(this.RenderView);
this.BindMouseCameraControls( this.RenderView );
// nicest way atm to pause rendering whilst xr is rendering
let LastXrRenderTime = null;
const GetXrRenderCommands = (RenderContext,Camera)=>
{
LastXrRenderTime = GetTimeNowMs();
//const ScreenRect = this.RenderView.GetScreenRect();
const ScreenRect = [0,0,100,100];
//await this.UpdateAssets(RenderContext);
//const RenderCommands = this.RenderContent.GetRenderCommands(RenderContext,Camera,ScreenRect);
const RenderCommands = GetRenderCommands( RenderContext,Camera,ScreenRect);
return RenderCommands;
};
async function XrLoop(XrOnWaitForCallback)
{
while ( this.Running )
{
try
{
console.log(`Waiting for xr device...`);
const Device = await Pop.Xr.CreateDevice( this.RenderContext, GetXrRenderCommands, XrOnWaitForCallback );
// should wait to finish now...
await Device.WaitForEnd();
}
catch(e)
{
console.error(`Failed to create xr ${e}`);
await Pop.Yield(1*1000);
}
}
}
// xr works, but desktop is reporting it has some XR support when it doesnt... (so button first appears)
//XrLoop.call(this,this.XrOnWaitForCallback.bind(this));
while ( this.Running )
{
//await this.RenderContent.UpdateAssets(this.RenderContext);
if ( LastXrRenderTime )
{
const TimeSinceXrRender = GetTimeNowMs() - LastXrRenderTime;
if ( TimeSinceXrRender < 3*1000 )
{
await Pop.Yield(1000);
continue;
}
}
const ScreenRect = this.RenderView.GetScreenRect();
const Camera = this.Camera;
const RenderCommands = GetRenderCommands( this.RenderContext,Camera,ScreenRect);
//const RenderCommands = this.RenderContent.GetRenderCommands(this.RenderContext,Camera,ScreenRect);
await this.RenderContext.Render(RenderCommands);
}
}
GetCameraUniforms(Camera,ScreenRect)
{
const w = ScreenRect[2];
const h = ScreenRect[3];
const Viewport = [0,0,w/w,h/w];
const Uniforms = {};
Uniforms.CameraToViewTransform = Camera.GetProjectionMatrix(Viewport);
Uniforms.WorldToCameraTransform = Camera.GetWorldToCameraMatrix();
return Uniforms;
}
BindMouseCameraControls(RenderView)
{
const Camera = this.Camera;
let MoveScalar = 10;
RenderView.OnMouseDown = function(x,y,Button,FirstDown=true)
{
if ( Button == 'Left' )
Camera.OnCameraOrbit( x, y, 0, FirstDown!=false );
if ( Button == 'Right' )
{
x *= MoveScalar;
y *= MoveScalar;
Camera.OnCameraPanLocal( x, y, 0, FirstDown!=false );
}
}
RenderView.OnMouseMove = function(x,y,Button)
{
RenderView.OnMouseDown( x, y, Button, false );
}
RenderView.OnMouseScroll = function(x,y,Button,Delta)
{
x *= MoveScalar;
y *= MoveScalar;
Delta[1] *= MoveScalar;
// zoom clamps to lookat, panlocalz moves lookat
Camera.OnCameraPanLocal( x, y, 0, true );
Camera.OnCameraPanLocal( x, y, -Delta[1] * 10.0, false );
//Camera.OnCameraZoom( -Delta[1] * 0.1 );
}.bind(this);
}
}
export class PopEngineCanvas extends HTMLElement
{
constructor()
{
super();
// the pop assetmanager is still a singleton atm
this.AssetManager = AssetManager;
this.DomEvents = {}; // cached dom events eg. 'load'(onload) 'dataevent'(ondataevent)
this.Renderer = null;
this.LoadedPromise = CreatePromise();
}
static ElementName() { return ElementName; }
ElementName() { return PopEngineCanvas.ElementName(); }
static get observedAttributes()
{
return ['css'];
}
// url for css file to include in <style>
get css() { return this.getAttribute('css'); }
GetCssContent()
{
const ImportCss = this.css ? `@import "${this.css}";` : '';
const Css = `
${ImportCss}
/* these can all be overriden by host */
:host
{
background: #333;
position: relative;
display: block;
min-width: 20px;
min-height: 20px;
}
canvas
{
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
width: 100%;
height: 100%;
}
#StartXr
{
visibility: hidden;
padding: 0.5em;
opacity: 0.6;
xbackground: rgba(0,0,0,0.5);
xcolor: #eee;
position: absolute;
font-size: 200%;
top: 1em;
left: 1em;
height: 3.0em;
width: 8em;
}
`;
return Css;
}
attributeChangedCallback(name, oldValue, newValue)
{
// todo: only update style if some css relative variables change
if ( this.Style )
this.Style.textContent = this.GetCssContent();
}
connectedCallback()
{
// move any children in the html onto our dom
// Create a shadow root
this.Shadow = this.attachShadow({mode: 'open'});
this.CreateDom(this.Shadow);
// initialise
this.attributeChangedCallback();
// this is for DOM load reporting
// not part of HTMLMediaPlayer
this.OnLoad(this);
}
disconnectedCallback()
{
this.FreePlayer();
}
CreateDom(Parent)
{
this.CanvasElement = document.createElement('canvas');
this.Style = document.createElement('style');
this.StartXrButton = document.createElement('button');
this.StartXrButton.id = 'StartXr';
this.StartXrButton.innerHTML = 'Start XR';
// attach the created elements to the shadow dom
Parent.appendChild(this.Style);
Parent.appendChild(this.CanvasElement);
Parent.appendChild(this.StartXrButton);
// start render thread now we have a canvas
this.RenderThreadPromise = this.RenderThread().catch(this.OnError.bind(this));
}
OnDebug(Debug)
{
console.log(`${this.ElementName()}; ${Debug}`);
}
CallDomEvent(DomEventName,Arguments=[])
{
// cache ondataevent attribute into a functor
if ( !this.DomEvents[DomEventName] && this.hasAttribute(`on${DomEventName}`) )
{
const EventFunctionString = this.getAttribute(`on${DomEventName}`);
this.DomEvents[DomEventName] = window.Function(EventFunctionString);
}
//if ( !this.DomEvents[DomEventName] && this.hasOwnProperty(`on${DomEventName}`) )
if ( !this.DomEvents[DomEventName] && this[`on${DomEventName}`] )
{
this.DomEvents[DomEventName] = this[`on${DomEventName}`];
}
// todo: dispatch event for addListener support
//this.dispatchEvent( new CustomEvent(DomEventName) )
const Event = this.DomEvents[DomEventName];
if ( Event )
{
try
{
return Event(...Arguments);
}
catch(e)
{
console.error(`on${DomEventName} exception; ${e}`);
}
}
return null;
}
OnError(Error)
{
Error = `${Error}`;
console.error(Error);
this.CallDomEvent('error', arguments );
}
OnLoad()
{
this.CallDomEvent('load', arguments );
}
FreePlayer()
{
if ( this.Renderer )
{
this.Renderer.Free();
this.Renderer = null;
}
}
XrOnWaitForCallback(OnClickedStart)
{
const Button = this.StartXrButton;
// enable xr button, wait for click
function OnClick()
{
Button.style.visibility = 'hidden';
OnClickedStart();
}
Button.style.visibility = 'visible';
Button.onclick = OnClick;
}
UpdateRenderCommands(Commands,RenderContext)
{
// convert new rendercommands (names for assets)
// to old system (assets)
// we will move this at some point to the engine
// when we figure out how to deal with images in the same way
function UpdateRenderCommand(Command)
{
if ( !Command ) return;
if ( Command[0] == 'Draw' )
{
// geo
Command[1] = this.AssetManager.GetAsset(Command[1],RenderContext);
// shader
Command[2] = this.AssetManager.GetAsset(Command[2],RenderContext);
}
return Command;
}
return Commands.map( UpdateRenderCommand.bind(this) );
}
async RenderCommands(Commands)
{
const RenderContext = this.Renderer.GetRenderContext();
Commands = this.UpdateRenderCommands( Commands, RenderContext );
await RenderContext.Render(Commands);
}
async RenderThread()
{
function GetRenderCommands(RenderContext,Camera,ScreenRect)
{
try
{
// dont send render context to external commands any more
const GetRenderCommandsArgs = [Camera,ScreenRect];
const ExternalCommands = this.CallDomEvent('getrendercommands',GetRenderCommandsArgs);
if ( !ExternalCommands )
throw `No external commands returned from event`;
// gr; should this make a copy?
const Commands = this.UpdateRenderCommands( ExternalCommands, RenderContext );
return Commands;
//ExternalCommands.forEach( UpdateRenderCommand.bind(this) );
//return ExternalCommands;
}
catch(e)
{
console.error(e);
this.SomeCounter = (this.SomeCounter||0)+1;
let Green = (this.SomeCounter % 60) / 60;
const Clear = ['SetRenderTarget',null,[1,Green,0]];
//const Clear = ['SetRenderTarget',null,ClearColour];
return [Clear];
}
}
// this has its own render thread
this.Renderer = new Renderer_t(this.CanvasElement, GetRenderCommands.bind(this) );
this.LoadedPromise.Resolve();
}
// todo: turn this into a more generic RegisterAsset() and assume the user uses the right names in the right places
RegisterGeometry(Name,GetGeometryAsync)
{
async function FetchTriangleBuffer(RenderContext)
{
const Geometry = await GetGeometryAsync();
const Indexes = Geometry.Indexes;
// as we remove indexes, do a shallow copy
const GeometryNoIndexes = Object.assign({}, Geometry);
delete GeometryNoIndexes.Indexes;
const TriangleBuffer = await RenderContext.CreateGeometry( GeometryNoIndexes, Indexes );
return TriangleBuffer;
}
this.AssetManager.RegisterAssetAsyncFetchFunction(Name,FetchTriangleBuffer);
}
RegisterShader(Name,GetShaderSourceAsync)
{
async function FetchShader(RenderContext)
{
const [VertSource,FragSource,Macros] = await GetShaderSourceAsync();
const Shader = await CompileShader( RenderContext, Name, VertSource, FragSource, Macros );
return Shader;
}
this.AssetManager.RegisterAssetAsyncFetchFunction(Name,FetchShader);
}
async WaitForLoad()
{
return this.LoadedPromise;
}
GetRenderContext()
{
if ( !this.Renderer )
return null;
return this.Renderer.GetRenderContext();
}
}
window.customElements.define( PopEngineCanvas.ElementName(), PopEngineCanvas );