@@ -3,6 +3,7 @@ import { vec3 } from "gl-matrix";
3
3
4
4
5
5
export class Framebuffer {
6
+ public static glContextStencilBits = 0 ; //class static variable
6
7
public framebuffer : WebGLFramebuffer ;
7
8
public renderbuffer : WebGLRenderbuffer ;
8
9
public texture : WebGLTexture ;
@@ -70,10 +71,22 @@ export class Framebuffer {
70
71
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_MIN_FILTER , gl . NEAREST ) ;
71
72
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_S , gl . CLAMP_TO_EDGE ) ;
72
73
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_T , gl . CLAMP_TO_EDGE ) ;
73
- if ( this . _glVersion === 1 ) {
74
+
75
+ //If the stencil bit is 8 bits or more, set to record the depth texture with DEPTH24_STENCIL8 if available (24+8 expands to a total of 32 bits to record depth => needs confirmation)
76
+ if ( Framebuffer . glContextStencilBits >= 8 ) {
77
+ if ( this . _glVersion === 1 ) {
78
+ //32bit(24+8) precision
79
+ gl . texImage2D ( gl . TEXTURE_2D , 0 , gl [ "UNSIGNED_INT_24_8" ] , width , height , 0 , gl . DEPTH_STENCIL , gl [ "UNSIGNED_INT_24_8" ] , null ) ;
80
+ } else {
81
+ //32bit(24+8) precision
82
+ gl . texImage2D ( gl . TEXTURE_2D , 0 , gl [ "DEPTH24_STENCIL8" ] , width , height , 0 , gl . DEPTH_STENCIL , gl [ "UNSIGNED_INT_24_8" ] , null ) ;
83
+ }
84
+ } else if ( this . _glVersion === 1 ) {
85
+ //8bit precision
74
86
gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . DEPTH_COMPONENT , width , height , 0 , gl . DEPTH_COMPONENT , gl . UNSIGNED_SHORT , null ) ;
75
87
} else {
76
- gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . DEPTH_COMPONENT16 , width , height , 0 , gl . DEPTH_COMPONENT , gl . UNSIGNED_SHORT , null ) ;
88
+ //16bit precision
89
+ gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . DEPTH_COMPONENT16 , width , height , 0 , gl . DEPTH_COMPONENT , gl . UNSIGNED_INT , null ) ;
77
90
}
78
91
}
79
92
@@ -109,6 +122,23 @@ export class Framebuffer {
109
122
return result ;
110
123
}
111
124
125
+ public getPixelAsFloat ( x : number , y : number ) : number {
126
+ if ( ! this . isReady )
127
+ return null ;
128
+ var result = new Uint8Array ( 4 ) ;
129
+ this . gl . readPixels ( x , y , 1 , 1 , this . gl . RGBA , this . gl . UNSIGNED_BYTE , result ) ;
130
+ return this . decodeFloatFromRGBA ( result ) ;
131
+ }
132
+
133
+ public decodeFloatFromRGBA ( rgba : Uint8Array ) :number {
134
+ const rightVec4 = [ 1.0 , 1 / 255.0 , 1 / 65025.0 , 1 / 16581375.0 ] ;
135
+ const dot = ( rgba [ 0 ] / 255.0 ) * rightVec4 [ 0 ]
136
+ + ( rgba [ 1 ] / 255.0 ) * rightVec4 [ 1 ]
137
+ + ( rgba [ 2 ] / 255.0 ) * rightVec4 [ 2 ]
138
+ + ( rgba [ 3 ] / 255.0 ) * rightVec4 [ 3 ] ;
139
+ return dot ;
140
+ }
141
+
112
142
public getDepth ( x : number , y : number ) : number {
113
143
if ( ! this . isReady )
114
144
return null ;
@@ -135,15 +165,12 @@ export class Framebuffer {
135
165
const depths = this . getDepths ( points ) ;
136
166
137
167
return depths . map ( ( depth , i ) => {
138
- if ( depth === 255 ) { // infinity (= nothing, no value)
139
- return null ;
140
- }
141
168
142
169
// convert values to clip space where x and y are [-1, 1] and z is [0, 1]
143
170
const point = points [ i ] ;
144
171
const xc = point . x / this . width * 2.0 - 1.0 ;
145
172
const yc = point . y / this . height * 2.0 - 1.0 ;
146
- const zc = ( depth / 255.0 - 0.5 ) * 2.0 ;
173
+ const zc = ( depth - 0.5 ) * 2.0 ;
147
174
148
175
return vec3 . fromValues ( xc , yc , zc ) ;
149
176
} ) ;
@@ -177,20 +204,13 @@ export class Framebuffer {
177
204
}
178
205
179
206
const depth = this . getDepth ( x , y ) ;
180
- if ( depth === 255 ) { // infinity
181
- return null ;
182
- }
183
207
184
208
// convert values to clip space where x and y are [-1, 1] and z is [0, 1]
185
209
const xc = x / this . width * 2.0 - 1.0 ;
186
210
const yc = y / this . height * 2.0 - 1.0 ;
187
- const zc = ( depth / 255.0 - 0.5 ) * 2.0 ;
188
-
189
- const depthNear = Math . max ( depth - 1 , 0 ) ;
190
- const zcn = ( depthNear / 255.0 - 0.5 ) * 2.0 ;
191
-
192
- const depthFar = Math . min ( depth + 1 , 255 ) ;
193
- const zcf = ( depthFar / 255.0 - 0.5 ) * 2.0 ;
211
+ const zc = 0.5 ;
212
+ const zcn = 0.0 ;
213
+ const zcf = 1.0 ;
194
214
195
215
return {
196
216
far : vec3 . fromValues ( xc , yc , zcf ) ,
0 commit comments