-
Notifications
You must be signed in to change notification settings - Fork 47
/
index4.html
261 lines (197 loc) · 8.09 KB
/
index4.html
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #fff;
color: #000;
margin: 0px;
overflow: hidden;
}
#info {
color: #000;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
display:block;
}
#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
.desktop canvas {
transform: scale(-1, 1);
}
</style>
<script type="text/javascript" src="build/nes.js"></script>
<script type="text/javascript" src="https://rawgit.com/mrdoob/three.js/r87/build/three.min.js"></script>
<script type="text/javascript" src="https://rawgit.com/mrdoob/three.js/r87/examples/js/controls/OrbitControls.js"></script>
<script type="text/javascript" src="https://rawgit.com/artoolkit/jsartoolkit5/master/build/artoolkit.min.js"></script>
<script type="text/javascript" src="https://rawgit.com/artoolkit/jsartoolkit5/master/js/artoolkit.api.js"></script>
<script type="text/javascript" src="https://rawgit.com/artoolkit/jsartoolkit5/master/js/artoolkit.three.js"></script>
<!-- CRT effect shader from https://github.com/hizzlekizzle/quark-shaders/tree/master/CRT-Simple.shader -->
<script id="vertexShader" type="x-shader/x-vertex">
uniform vec2 tDiffuseSize;
varying vec2 vUv;
varying vec2 one;
varying float mod_factor;
#define PI 3.141592653589
void main() {
vUv = uv;
one = 1.0 / tDiffuseSize.xy;
mod_factor = uv.x * uv.x;
vec3 pos = position;
pos.z += sin(uv.x * PI) * 0.5 + sin(uv.y * PI) * 0.5;
gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform sampler2D tDiffuse;
uniform vec2 tDiffuseSize;
varying vec2 vUv;
varying vec2 one;
varying float mod_factor;
#define inputGamma 2.4
#define outputGamma 2.2
#define TEX2D(c) pow(texture2D(tDiffuse, (c)), vec4(inputGamma))
vec4 scanlineWeights(float distance, vec4 color) {
vec4 wid = 2.0 + 2.0 * pow(color, vec4(4.0));
vec4 weights = vec4(distance / 0.3);
return 1.4 * exp(-pow(weights * inversesqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid);
}
void main() {
// flip x for fliped canvas
vec2 xy = vec2( 1.0 - vUv.x, vUv.y );
vec2 ratio_scale = xy * tDiffuseSize.xy - vec2(0.5);
vec2 uv_ratio = fract(ratio_scale);
xy.y = (floor(ratio_scale.y) + 0.5) / tDiffuseSize.y;
vec4 col = TEX2D(xy);
vec4 col2 = TEX2D(xy + vec2(0.0, one.y));
vec4 weights = scanlineWeights(uv_ratio.y, col);
vec4 weights2 = scanlineWeights(1.0 - uv_ratio.y, col2);
vec3 mul_res = (col * weights + col2 * weights2).rgb;
vec3 dotMaskWeights = mix(
vec3(1.0, 0.7, 1.0),
vec3(0.7, 1.0, 0.7),
floor(mod(mod_factor, 2.0))
);
mul_res *= dotMaskWeights;
gl_FragColor = vec4(pow(mul_res, vec3(1.0 / outputGamma)), 1.0);
}
</script>
<script type="text/javascript">
var nes;
/**
*
*/
function init() {
var url = './roms/Sgt. Helmet - Training Day (2013)(The Mojon Twins)[!].nes';
var request = new XMLHttpRequest();
request.responseType = 'arraybuffer';
request.onload = function() {
initNes(request.response);
};
request.onerror = function(e) {
};
request.open('GET', url, true);
request.send(null);
}
/**
*
*/
function initNes(buffer) {
var canvas = document.createElement('canvas')
nes = new NesJs.Nes();
nes.setRom(new NesJs.Rom(buffer));
nes.setDisplay(new NesJs.Display(canvas));
nes.setAudio(new NesJs.Audio());
window.onkeydown = function(e) { nes.handleKeyDown(e); };
window.onkeyup = function(e) { nes.handleKeyUp(e); };
nes.bootup();
nes.run();
start(canvas);
}
/**
*
*/
function start( canvas ) {
canvas.width = canvas.height = 256;
var group, scene, camera, screen, renderer;
function initAR() {
ARController.getUserMediaThreeScene( {
maxARVideoSize: 320,
cameraParam: 'https://rawgit.com/artoolkit/jsartoolkit5/master/examples/Data/camera_para-iPhone 5 rear 640x480 1.0m.dat',
onSuccess: function( arScene, arController, arCamera ) {
document.body.className = arController.orientation;
arController.setPatternDetectionMode( artoolkit.AR_MATRIX_CODE_DETECTION );
var renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( arController.videoWidth, arController.videoHeight );
document.body.className += ' desktop';
document.body.insertBefore( renderer.domElement, document.body.firstChild );
group = new THREE.Group();
group.scale.multiplyScalar( 0.15 );
group.rotation.x = 60 * Math.PI / 180;
// See /doc/patterns/Matrix code 3x3 (72dpi)/20.png
var markerRoot = arController.createThreeBarcodeMarker( 20 );
markerRoot.add( group );
arScene.scene.add( markerRoot );
var light = new THREE.DirectionalLight( 0xffffff );
light.position.set( -20, -20, -20 );
arScene.scene.add( light );
function initScene() {
var geometry = new THREE.PlaneBufferGeometry( 16, 15, 50, 50 );
var material = new THREE.ShaderMaterial( {
uniforms: {
tDiffuse: { value: new THREE.Texture( canvas ) },
tDiffuseSize: { value: new THREE.Vector2( canvas.width, canvas.height ) }
},
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent
} );
material.uniforms.tDiffuse.value.needsUpdate = true;
screen = new THREE.Mesh( geometry, material );
group.add( screen );
var box = new THREE.Mesh(
new THREE.BoxBufferGeometry( 19, 19, 5 ),
new THREE.MeshStandardMaterial( { color: 0xFFFFFF, roughness: 0.8, metalness: 0.0 } ),
);
box.position.z = -3.5;
var box2 = new THREE.Mesh(
new THREE.BoxBufferGeometry( 18, 18, 10 ),
new THREE.MeshStandardMaterial( { color: 0xDDDDDD, roughness: 0.9, metalness: 0.0 } ),
);
box2.position.z = -10;
group.add( box );
group.add( box2 );
}
function animate() {
requestAnimationFrame( animate );
screen.material.uniforms.tDiffuse.value.needsUpdate = true;
render()
}
function render() {
arScene.process();
arScene.renderOn( renderer );
}
initScene();
animate();
}
} );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
initAR();
window.addEventListener( 'resize', onWindowResize, false );
}
</script>
</head>
<body onload="init()">
<p>Show <a href="https://github.com/artoolkit/artoolkit5/blob/master/doc/patterns/Matrix%20code%203x3%20(72dpi)/20.png">3x3 marker id 20</a> to camera to display NES with CRT monitor.
<p>
<a href="https://github.com/takahirox/nes-js">Source code at GitHub</a>
</p>
</body>
</html>