generated from M-CreativeLab/template-for-jsar-widget
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
114 lines (102 loc) · 3.51 KB
/
main.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
import { NES } from './jsnes/nes';
import { Controller } from './jsnes/controller';
import marioBin from './roms/mario.nes.bin';
const { scene } = spatialDocument;
var SCREEN_WIDTH = 256;
var SCREEN_HEIGHT = 240;
var FRAMEBUFFER_SIZE = SCREEN_WIDTH * SCREEN_HEIGHT;
let targetGameObject: BABYLON.Mesh;
const model = spatialDocument.getNodeById('model');
if (!model) {
const ground = BABYLON.MeshBuilder.CreateBox('ground', {
width: SCREEN_WIDTH,
height: SCREEN_HEIGHT,
depth: SCREEN_HEIGHT,
}, scene);
ground.rotation.x = Math.PI / 3;
{
const mat = new BABYLON.StandardMaterial('mat', scene);
mat.roughness = 1;
// mat.diffuseColor = new BABYLON.Color3(1, 1, 0);
mat.sideOrientation = BABYLON.Material.ClockWiseSideOrientation;
ground.material = mat;
}
targetGameObject = ground;
} else {
const screen = model.getChildMeshes().find(mesh => mesh.name === 'Object_16');
if (screen) {
{
const mat = new BABYLON.StandardMaterial('mat', scene);
// mat.albedoColor = new BABYLON.Color3(1, 0.3, 0.3);
mat.roughness = 1;
mat.diffuseColor = new BABYLON.Color3(1, 1, 0);
mat.sideOrientation = BABYLON.Material.ClockWiseSideOrientation;
screen.material = mat;
screen.rotate(BABYLON.Axis.X, Math.PI, BABYLON.Space.WORLD);
}
targetGameObject = screen as BABYLON.Mesh;
}
}
const groundTex = new BABYLON.DynamicTexture("dynamic texture", {
width: SCREEN_WIDTH,
height: SCREEN_HEIGHT,
}, scene, false, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, BABYLON.Engine.TEXTUREFORMAT_RGBA, false);
(targetGameObject.material as BABYLON.StandardMaterial).diffuseTexture = groundTex;
const context2d = groundTex.getContext();
context2d.fillStyle = 'red';
context2d.fillRect(0, 0, SCREEN_WIDTH, 20);
var buffer = new ArrayBuffer(FRAMEBUFFER_SIZE * 4);
var framebuffer_u8 = new Uint8ClampedArray(buffer);
var framebuffer_u32 = new Uint32Array(buffer);
var nes = new NES({
onFrame: function (framebuffer_24) {
for (var i = 0; i < FRAMEBUFFER_SIZE; i++) {
framebuffer_u32[i] = 0xFF000000 | framebuffer_24[i];
}
const imageData = new ImageData(framebuffer_u8, SCREEN_WIDTH, SCREEN_HEIGHT);
context2d.putImageData(imageData, 0, 0);
groundTex.update();
},
onAudioSample: function (left, right) {
// ... play audio sample
}
});
const romData = Buffer.from(marioBin).toString('binary');
nes.loadROM(romData);
// setInterval(() => nes.frame(), 16);
scene.registerAfterRender(function () {
nes.frame();
});
function keyboard(callback, code) {
var player = 1;
switch (code) {
case 38: // UP
callback(player, Controller.BUTTON_UP); break;
case 40: // Down
callback(player, Controller.BUTTON_DOWN); break;
case 37: // Left
callback(player, Controller.BUTTON_LEFT); break;
case 39: // Right
callback(player, Controller.BUTTON_RIGHT); break;
case 65: // 'a' - qwerty, dvorak
case 81: // 'q' - azerty
callback(player, Controller.BUTTON_A); break;
case 83: // 's' - qwerty, azerty
case 79: // 'o' - dvorak
callback(player, Controller.BUTTON_B); break;
case 9: // Tab
callback(player, Controller.BUTTON_SELECT); break;
case 13: // Return
callback(player, Controller.BUTTON_START); break;
default: break;
}
}
spatialDocument.watchInputEvent();
spatialDocument.addEventListener('mouse', (event: any) => {
const { inputData } = event;
if (inputData.Action === 'down') {
keyboard(nes.buttonDown, 13);
} else if (inputData.Action === 'up') {
keyboard(nes.buttonUp, 13);
}
});