-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
PickDepth.js
200 lines (171 loc) · 5.61 KB
/
PickDepth.js
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
import Cartesian4 from "../Core/Cartesian4.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import PixelFormat from "../Core/PixelFormat.js";
import Framebuffer from "../Renderer/Framebuffer.js";
import PixelDatatype from "../Renderer/PixelDatatype.js";
import RenderState from "../Renderer/RenderState.js";
import ShaderSource from "../Renderer/ShaderSource.js";
import Texture from "../Renderer/Texture.js";
/**
* @private
*/
function PickDepth() {
this._framebuffer = undefined;
this._depthTexture = undefined;
this._textureToCopy = undefined;
this._copyDepthCommand = undefined;
this._useLogDepth = undefined;
this._debugPickDepthViewportCommand = undefined;
}
function executeDebugPickDepth(pickDepth, context, passState, useLogDepth) {
if (
!defined(pickDepth._debugPickDepthViewportCommand) ||
useLogDepth !== pickDepth._useLogDepth
) {
var fsSource =
"uniform sampler2D u_texture;\n" +
"varying vec2 v_textureCoordinates;\n" +
"void main()\n" +
"{\n" +
" float z_window = czm_unpackDepth(texture2D(u_texture, v_textureCoordinates));\n" +
" z_window = czm_reverseLogDepth(z_window); \n" +
" float n_range = czm_depthRange.near;\n" +
" float f_range = czm_depthRange.far;\n" +
" float z_ndc = (2.0 * z_window - n_range - f_range) / (f_range - n_range);\n" +
" float scale = pow(z_ndc * 0.5 + 0.5, 8.0);\n" +
" gl_FragColor = vec4(mix(vec3(0.0), vec3(1.0), scale), 1.0);\n" +
"}\n";
var fs = new ShaderSource({
defines: [useLogDepth ? "LOG_DEPTH" : ""],
sources: [fsSource],
});
pickDepth._debugPickDepthViewportCommand = context.createViewportQuadCommand(
fs,
{
uniformMap: {
u_texture: function () {
return pickDepth._depthTexture;
},
},
owner: pickDepth,
}
);
pickDepth._useLogDepth = useLogDepth;
}
pickDepth._debugPickDepthViewportCommand.execute(context, passState);
}
function destroyTextures(pickDepth) {
pickDepth._depthTexture =
pickDepth._depthTexture &&
!pickDepth._depthTexture.isDestroyed() &&
pickDepth._depthTexture.destroy();
}
function destroyFramebuffers(pickDepth) {
pickDepth._framebuffer =
pickDepth._framebuffer &&
!pickDepth._framebuffer.isDestroyed() &&
pickDepth._framebuffer.destroy();
}
function createTextures(pickDepth, context, width, height) {
pickDepth._depthTexture = new Texture({
context: context,
width: width,
height: height,
pixelFormat: PixelFormat.RGBA,
pixelDatatype: PixelDatatype.UNSIGNED_BYTE,
});
}
function createFramebuffers(pickDepth, context, width, height) {
destroyTextures(pickDepth);
destroyFramebuffers(pickDepth);
createTextures(pickDepth, context, width, height);
pickDepth._framebuffer = new Framebuffer({
context: context,
colorTextures: [pickDepth._depthTexture],
destroyAttachments: false,
});
}
function updateFramebuffers(pickDepth, context, depthTexture) {
var width = depthTexture.width;
var height = depthTexture.height;
var texture = pickDepth._depthTexture;
var textureChanged =
!defined(texture) || texture.width !== width || texture.height !== height;
if (!defined(pickDepth._framebuffer) || textureChanged) {
createFramebuffers(pickDepth, context, width, height);
}
}
function updateCopyCommands(pickDepth, context, depthTexture) {
if (!defined(pickDepth._copyDepthCommand)) {
var fs =
"uniform sampler2D u_texture;\n" +
"varying vec2 v_textureCoordinates;\n" +
"void main()\n" +
"{\n" +
" gl_FragColor = czm_packDepth(texture2D(u_texture, v_textureCoordinates).r);\n" +
"}\n";
pickDepth._copyDepthCommand = context.createViewportQuadCommand(fs, {
renderState: RenderState.fromCache(),
uniformMap: {
u_texture: function () {
return pickDepth._textureToCopy;
},
},
owner: pickDepth,
});
}
pickDepth._textureToCopy = depthTexture;
pickDepth._copyDepthCommand.framebuffer = pickDepth._framebuffer;
}
PickDepth.prototype.executeDebugPickDepth = function (
context,
passState,
useLogDepth
) {
executeDebugPickDepth(this, context, passState, useLogDepth);
};
PickDepth.prototype.update = function (context, depthTexture) {
updateFramebuffers(this, context, depthTexture);
updateCopyCommands(this, context, depthTexture);
};
var scratchPackedDepth = new Cartesian4();
var packedDepthScale = new Cartesian4(
1.0,
1.0 / 255.0,
1.0 / 65025.0,
1.0 / 16581375.0
);
PickDepth.prototype.getDepth = function (context, x, y) {
// If this function is called before the framebuffer is created, the depth is undefined.
if (!defined(this._framebuffer)) {
return undefined;
}
var pixels = context.readPixels({
x: x,
y: y,
width: 1,
height: 1,
framebuffer: this._framebuffer,
});
var packedDepth = Cartesian4.unpack(pixels, 0, scratchPackedDepth);
Cartesian4.divideByScalar(packedDepth, 255.0, packedDepth);
return Cartesian4.dot(packedDepth, packedDepthScale);
};
PickDepth.prototype.executeCopyDepth = function (context, passState) {
this._copyDepthCommand.execute(context, passState);
};
PickDepth.prototype.isDestroyed = function () {
return false;
};
PickDepth.prototype.destroy = function () {
destroyTextures(this);
destroyFramebuffers(this);
if (defined(this._copyDepthCommand)) {
this._copyDepthCommand.shaderProgram =
defined(this._copyDepthCommand.shaderProgram) &&
this._copyDepthCommand.shaderProgram.destroy();
}
return destroyObject(this);
};
export default PickDepth;