forked from donmccurdy/postprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepthPickingPass.js
More file actions
174 lines (127 loc) · 4.24 KB
/
DepthPickingPass.js
File metadata and controls
174 lines (127 loc) · 4.24 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
import { FloatType, RGBADepthPacking } from "three";
import { DepthCopyMode } from "../materials";
import { DepthCopyPass } from "./DepthCopyPass";
const unpackFactors = new Float32Array([
(255 / 256) / (256 ** 3),
(255 / 256) / (256 ** 2),
(255 / 256) / 256,
(255 / 256)
]);
/**
* Unpacks RGBA-encoded depth.
*
* @private
* @param {Uint8Array} packedDepth - The packed depth.
* @return {Number} The unpacked depth.
*/
function unpackRGBAToDepth(packedDepth) {
return (
packedDepth[0] * unpackFactors[0] +
packedDepth[1] * unpackFactors[1] +
packedDepth[2] * unpackFactors[2] +
packedDepth[3] * unpackFactors[3]
) / 255;
}
/**
* A depth picking pass.
*/
export class DepthPickingPass extends DepthCopyPass {
/**
* Constructs a new depth picking pass.
*
* @param {Object} [options] - The options.
* @param {DepthPackingStrategies} [options.depthPacking=RGBADepthPacking] - The depth packing.
* @param {Number} [options.mode=DepthCopyMode.SINGLE] - The depth copy mode.
*/
constructor({ depthPacking = RGBADepthPacking, mode = DepthCopyMode.SINGLE } = {}) {
super({ depthPacking });
this.name = "DepthPickingPass";
this.fullscreenMaterial.mode = mode;
/**
* An RGBA pixel buffer.
*
* @type {TypedArray}
* @private
*/
this.pixelBuffer = (depthPacking === RGBADepthPacking) ? new Uint8Array(4) : new Float32Array(4);
/**
* A callback for picking results.
*
* @type {Function}
* @private
*/
this.callback = null;
}
/**
* Reads depth at a specific screen position.
*
* Only one depth value can be picked per frame. Calling this method multiple times per frame will overwrite the
* picking coordinates. Unresolved promises will be abandoned.
*
* @example
* const ndc = new Vector3();
* const clientRect = myViewport.getBoundingClientRect();
* const clientX = pointerEvent.clientX - clientRect.left;
* const clientY = pointerEvent.clientY - clientRect.top;
* ndc.x = (clientX / myViewport.clientWidth) * 2.0 - 1.0;
* ndc.y = -(clientY / myViewport.clientHeight) * 2.0 + 1.0;
* const depth = await depthPickingPass.readDepth(ndc);
* ndc.z = depth * 2.0 - 1.0;
*
* const worldPosition = ndc.unproject(camera);
*
* @param {Vector2|Vector3} ndc - Normalized device coordinates. Only X and Y are relevant.
* @return {Promise<Number>} A promise that returns the depth on the next frame.
*/
readDepth(ndc) {
this.fullscreenMaterial.texelPosition.set(ndc.x * 0.5 + 0.5, ndc.y * 0.5 + 0.5);
return new Promise((resolve) => {
this.callback = resolve;
});
}
/**
* Copies depth and resolves depth picking promises.
*
* @param {WebGLRenderer} renderer - The renderer.
* @param {WebGLRenderTarget} inputBuffer - A frame buffer that contains the result of the previous pass.
* @param {WebGLRenderTarget} outputBuffer - A frame buffer that serves as the output render target unless this pass renders to screen.
* @param {Number} [deltaTime] - The time between the last frame and the current one in seconds.
* @param {Boolean} [stencilTest] - Indicates whether a stencil mask is active.
*/
render(renderer, inputBuffer, outputBuffer, deltaTime, stencilTest) {
const material = this.fullscreenMaterial;
const mode = material.mode;
if(mode === DepthCopyMode.FULL) {
// Always copy the full depth texture.
super.render(renderer);
}
if(this.callback !== null) {
const renderTarget = this.renderTarget;
const pixelBuffer = this.pixelBuffer;
const packed = (renderTarget.texture.type !== FloatType);
let x = 0, y = 0;
if(mode === DepthCopyMode.SINGLE) {
// Copy a specific depth value.
super.render(renderer);
} else {
const texelPosition = material.texelPosition;
x = Math.round(texelPosition.x * renderTarget.width);
y = Math.round(texelPosition.y * renderTarget.height);
}
renderer.readRenderTargetPixels(renderTarget, x, y, 1, 1, pixelBuffer);
this.callback(packed ? unpackRGBAToDepth(pixelBuffer) : pixelBuffer[0]);
this.callback = null;
}
}
/**
* Updates the size of this pass.
*
* @param {Number} width - The width.
* @param {Number} height - The height.
*/
setSize(width, height) {
if(this.fullscreenMaterial.mode === DepthCopyMode.FULL) {
super.setSize(width, height);
}
}
}