/
reprojection.html
281 lines (235 loc) · 8.38 KB
/
reprojection.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!doctype html>
<html>
<head>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<script src="three.js"></script>
<script src="OrbitControls.js"></script>
<script>
window.ScreenQuad = (() => {
const defaultQuad = new THREE.PlaneBufferGeometry(2, 2, window.innerWidth, window.innerHeight);
const vertexShader = `\
varying vec3 vViewPosition;
varying vec2 vUv;
varying float vZDiff;
uniform sampler2D dTexture;
uniform mat4 initialProjectionMatrixInverse;
uniform mat4 initialProjectionMatrix;
uniform vec4 plane;
void main() {
vec2 xy = (position.xy + 1.0) / 2.0; //in [0,1] range
float z_b = texture2D(dTexture, xy).r;
float z_n = 2.0 * z_b - 1.0;
float zNear = initialProjectionMatrix[3][2] / (initialProjectionMatrix[2][2] - 1.0);
float zFar = initialProjectionMatrix[3][2] / (initialProjectionMatrix[2][2] + 1.0);
float z_e = -(2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear)));
float zOrig = z_e;
vec4 offset = initialProjectionMatrix * vec4(0.0, 0.0, z_e, 1.0);
vec4 worldPosOrig = initialProjectionMatrixInverse * vec4(position.xy, offset.z/offset.w, 1.0);
float zNew = worldPosOrig.z/worldPosOrig.w;
vec4 worldPosNew = modelMatrix * worldPosOrig;
vec4 viewPosNew = viewMatrix * worldPosNew;
gl_Position = projectionMatrix * viewPosNew;
vViewPosition = -worldPosNew.xyz;
vUv = uv;
vZDiff = abs(zNew - zOrig);
}
`;
const fragmentShader = `\
varying vec3 vViewPosition;
varying vec2 vUv;
varying float vZDiff;
uniform float numTextures;
uniform sampler2D uTexture;
uniform sampler2D dTexture;
uniform vec4 plane;
void main() {
if (vZDiff < 0.0001 && dot(vViewPosition, plane.xyz) <= plane.w) {
/* vec4 c = texture2D(uTexture, vUv);
float dc = (1.0 - texture2D(dTexture, vUv).r) * 20.0;
gl_FragColor = vec4(vec3(dc) * c.rgb, c.a); */
gl_FragColor = texture2D(uTexture, vUv);
} else {
discard;
}
}
`;
function ScreenQuad( params ){
params = params || {};
THREE.Mesh.apply( this, [ defaultQuad , new THREE.ShaderMaterial({
uniforms:{
numTextures: {
type: 'f',
value: undefined !== params.numTextures ? params.numTextures : 1
},
uTexture: {
type:'t',
value: undefined !== params.texture ? params.texture : null
},
dTexture: {
type:'t',
value: undefined !== params.depthTexture ? params.depthTexture : null
},
initialProjectionMatrixInverse: {
type:'m4',
value: undefined !== params.initialProjectionMatrixInverse ? params.initialProjectionMatrixInverse : null
},
initialProjectionMatrix: {
type:'m4',
value: undefined !== params.initialProjectionMatrix ? params.initialProjectionMatrix : null
},
plane: {
type:'v4',
value: undefined !== params.plane ? params.plane : null
},
},
vertexShader,
fragmentShader,
transparent: true,
})]);
this.frustumCulled = false;
this.renderOrder = -1;
//end mesh setup
}
ScreenQuad.prototype = Object.create( THREE.Mesh.prototype );
ScreenQuad.constructor = ScreenQuad;
return ScreenQuad
})();
</script>
<script>
let renderer, scene, hiddenScene, camera, hiddenCamera, iframe, boxMesh, boxMesh2, renderTarget, screenQuad;
const localVector = new THREE.Vector3();
const localVector2 = new THREE.Vector3();
const localVector3 = new THREE.Vector3();
const localCoord = new THREE.Vector2();
const localPlane = new THREE.Plane();
const localLine = new THREE.Line3();
const localLine2 = new THREE.Line3();
const planeWorldWidth = 0.9;
const planeWorldHeight = 0.9;
const boxMeshSize = 1;
const planeWidth = 1280;
const planeHeight = 1024;
function init() {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
// window.browser.magicleap.RequestDepthPopulation(true);
// renderer.autoClear = false;
document.body.appendChild(renderer.domElement);
scene = new THREE.Scene();
scene.matrixAutoUpdate = false;
hiddenScene = new THREE.Scene();
hiddenScene.matrixAutoUpdate = false;
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 1;
scene.add(camera);
new THREE.OrbitControls(camera);
hiddenCamera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
hiddenCamera.position.copy(camera.position);
hiddenScene.add(hiddenCamera);
hiddenCamera.updateMatrixWorld();
hiddenCamera.updateProjectionMatrix();
hiddenCamera.initialProjectionMatrixInverse = new THREE.Matrix4();
hiddenCamera.initialProjectionMatrix = new THREE.Matrix4();
hiddenCamera.plane = new THREE.Vector4(0, 1, 0, 0);
{
const ambientLight = new THREE.AmbientLight(0x808080);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xFFFFFF, 1);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
}
{
const ambientLight = new THREE.AmbientLight(0x808080);
hiddenScene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xFFFFFF, 1);
directionalLight.position.set(1, 1, 1);
hiddenScene.add(directionalLight);
}
const vertexShader = [
'attribute vec2 uv;',
'varying vec2 vUv;',
'void main() {',
'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
'vUv = uv;',
'}'
].join('\n');
const fragmentShader = [
'sampler2D t;',
'varying vec2 vUv;',
'void main() {',
'gl_FragColor = texture2D(t, vUv);',
'}'
].join('\n');
const _makeBoxMesh = () => {
const geometry = new THREE.BoxBufferGeometry(boxMeshSize, boxMeshSize, boxMeshSize);
const material = new THREE.MeshPhongMaterial({
color: 0xFF0000,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = Math.PI / 4;
mesh.rotation.y = Math.PI / 4;
mesh.rotation.z = Math.PI / 4;
mesh.rotation.order = 'YXZ';
return mesh;
};
boxMesh = _makeBoxMesh();
boxMesh.position.z = -2;
scene.add(boxMesh);
boxMesh2 = _makeBoxMesh();
boxMesh2.position.z = -2;
hiddenScene.add(boxMesh2);
renderTarget = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight, {
format: THREE.RGBAFormat,
});
// renderTarget.transparent = false;
renderTarget.depthTexture = new THREE.DepthTexture(window.innerWidth, window.innerHeight);
screenQuad = new ScreenQuad({
texture: renderTarget.texture,
depthTexture: renderTarget.depthTexture,
plane: hiddenCamera.plane,
initialProjectionMatrixInverse: hiddenCamera.initialProjectionMatrixInverse,
initialProjectionMatrix: hiddenCamera.initialProjectionMatrix,
});
scene.add(screenQuad);
}
init();
const _renderFrame = () => {
hiddenCamera.position.copy(camera.position);
hiddenCamera.quaternion.copy(camera.quaternion);
hiddenCamera.scale.copy(camera.scale);
hiddenCamera.updateMatrixWorld();
hiddenCamera.updateProjectionMatrix();
screenQuad.position.copy(camera.position);
screenQuad.quaternion.copy(camera.quaternion);
screenQuad.scale.copy(camera.scale);
screenQuad.updateMatrixWorld();
hiddenCamera.initialProjectionMatrixInverse.copy(hiddenCamera.projectionMatrixInverse);
hiddenCamera.initialProjectionMatrix.copy(hiddenCamera.projectionMatrix);
renderer.render(hiddenScene, hiddenCamera, renderTarget);
renderer.setRenderTarget(null);
setTimeout(_renderFrame, 2000);
};
_renderFrame();
function animate(timestamp, frame) {
boxMesh.rotation.x += 0.01;
boxMesh.rotation.y += 0.01;
boxMesh.rotation.z += 0.01;
boxMesh2.position.copy(boxMesh.position);
boxMesh2.quaternion.copy(boxMesh.quaternion);
boxMesh2.scale.copy(boxMesh.scale);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>