-
Notifications
You must be signed in to change notification settings - Fork 2
/
car.html
201 lines (161 loc) · 5.81 KB
/
car.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="mobile-web-app-capable" content="yes">
<title>mapMD</title>
</head>
<script src="pixi.js"></script>
<script src="js/tools/tools.js"></script>
<script src="js/tools/ColorMapFilter.js"></script>
<script src="js/tools/animation.js"></script>
<script src="js/tools/tween.js"></script>
<script src="js/vision-filter.js"></script>
<body>
<style>* {
padding: 0;
margin: 0;
height: 100%;
}
</style>
<script type="text/javascript">
class BarrelFilter extends PIXI.Filter {
constructor(power = 1.2, offset = { x: 0, y: 0 }) {
super(undefined, BarrelFilter.fragment);
this.uniforms.dimensions = { x: 0, y: 0 };
this.uniforms.filterArea = { x: 0, y: 0, z: 0, w: 0 };
this.uniforms.power = power;
this.uniforms.offset = offset;
this.uniforms.nssm = new PIXI.Matrix();
this.uniforms.inssm = new PIXI.Matrix();
}
apply(filterManager, input, output) {
this.uniforms.dimensions.x = input.sourceFrame.width;
this.uniforms.dimensions.y = input.sourceFrame.height;
this.uniforms.filterArea.x = output.size.width;
this.uniforms.filterArea.y = output.size.height;
this.uniforms.filterArea.z = input.sourceFrame.x;
this.uniforms.filterArea.w = input.sourceFrame.y;
filterManager.calculateNormalizedScreenSpaceMatrix(this.uniforms.nssm);
filterManager.calculateNormalizedScreenSpaceMatrix(this.uniforms.inssm);
this.uniforms.inssm.invert();
filterManager.applyFilter(this, input, output);
}
}
BarrelFilter.fragment = `
precision highp float;
varying vec2 vTextureCoord;
varying vec2 vFilterCoord;
uniform sampler2D uSampler;
uniform vec2 dimensions;
uniform vec4 filterArea;
uniform float power;
uniform vec2 offset;
uniform mat3 nssm;
uniform mat3 inssm;
vec2 mapCoord( vec2 coord ){
return (vec3(coord, 1.0) * nssm).xy;
}
vec2 unmapCoord( vec2 coord ){
return (vec3(coord, 1.0) * inssm).xy;
}
// Given a vec2 in [-1,+1], generate a texture coord in [0,+1]
vec2 barrelDistortion(vec2 p)
{
float theta = atan(p.y, p.x);
float radius = length(p);
radius = pow(radius, power);
p.x = radius * cos(theta);
p.y = radius * sin(theta);
return 0.5 * (p + 1.0);
}
void main(){
vec2 uv = mapCoord(vTextureCoord);
uv = barrelDistortion(uv*2.0 - 1.0 + offset);
gl_FragColor = texture2D(uSampler, unmapCoord(uv));
}
`;
let ri = 0.15; let ro = 0.3; let c = {x:0.48, y:0.42};
let visionMapFilterLeft = new VisionMapFilter(ri, ro, c);
let visionMapFilterRight = new VisionMapFilter(ri, ro, c);
//Create the renderer
var stage = new PIXI.Container(),
renderer = PIXI.autoDetectRenderer(0, 0);
document.body.appendChild(renderer.view);
renderer.view.style.position = "absolute";
renderer.view.style.display = "block";
renderer.autoResize = true;
renderer.resize(window.innerWidth, window.innerHeight);
//Use Pixi's built-in `loader` object to load an image
// PIXI.loader
// .add("img_fjords.jpg")
// .load(setup);
//This `setup` function will run when the image has loaded
function setup() {
// //Create the `cat` sprite from the texture
// var cat = new PIXI.Sprite(
// PIXI.loader.resources["img_fjords.jpg"].texture
// );
//
// //Add the cat to the stage
// stage.addChild(cat);
//Render the stage
renderer.render(stage);
}
let texture;
let sprite;
var video = document.createElement("video");
video.preload = "auto";
video.loop = true; // enable looping
video.src = 'car-approaching.mp4';
texture = new PIXI.Texture.fromVideo(video);
sprite = new PIXI.Sprite(texture);
sprite.x = 0;
sprite.y = 0;
sprite.height = renderer.height;
sprite.width = renderer.width/2;
sprite.filters = [visionMapFilterLeft, new BarrelFilter(1.2)];
stage.addChild(sprite);
sprite = new PIXI.Sprite(texture);
sprite.x = renderer.width/2;
sprite.y = 0;
sprite.height = renderer.height;
sprite.width = renderer.width/2;
sprite.filters = [visionMapFilterRight,new BarrelFilter(1.2)];
stage.addChild(sprite);
renderer.render(stage);
loop();
function loop() {
setTimeout(loop, 70);
sprite.texture.update();
renderer.render(stage);
}
var transitionId = null;
var mode = 1;
function toggleMode(){
window.cancelAnimationFrame(transitionId);
mode = (mode + 1) % 2;
if (mode === 0) {
visionMapFilterLeft.uniforms.warp = 0;
visionMapFilterLeft.uniforms.darkening = 0;
visionMapFilterLeft.uniforms.innerWarp = 0;
visionMapFilterRight.uniforms.warp = 0;
visionMapFilterRight.uniforms.darkening = 0;
visionMapFilterRight.uniforms.innerWarp = 0;
} else {
visionMapFilterLeft.uniforms.warp = 0;
visionMapFilterLeft.uniforms.darkening = 1;
visionMapFilterLeft.uniforms.innerWarp = 1;
visionMapFilterRight.uniforms.warp = 1;
visionMapFilterRight.uniforms.darkening = 1;
visionMapFilterRight.uniforms.innerWarp = 0;
}
}
toggleMode();
stage.interactive = true;
stage.on('tap', toggleMode);
stage.on('click', toggleMode);
setup();
</script>
</body>
</html>