1- const _createFramebuffer = function ( ) {
2- const fb = new Framebuffer ( this )
1+ const _createFramebuffer = function ( options ) {
2+ const fb = new Framebuffer ( this , options )
33
44 // Extend the old resize handler to also update the size of the framebuffer
55 const oldResize = this . _renderer . resize
@@ -14,7 +14,7 @@ p5.prototype.createFramebuffer = _createFramebuffer
1414p5 . Graphics . prototype . createFramebuffer = _createFramebuffer
1515
1616const parentGetTexture = p5 . RendererGL . prototype . getTexture
17- p5 . RendererGL . prototype . getTexture = function ( imgOrTexture ) {
17+ p5 . RendererGL . prototype . getTexture = function ( imgOrTexture ) {
1818 if ( imgOrTexture instanceof p5 . Texture ) {
1919 return imgOrTexture
2020 } else {
@@ -31,12 +31,7 @@ p5.RendererGL.prototype.getTexture = function(imgOrTexture) {
3131// that looks like a p5 texture but that never tries to update
3232// data in order to use framebuffer textures inside p5.
3333class RawTextureWrapper extends p5 . Texture {
34- constructor (
35- renderer ,
36- obj ,
37- w ,
38- h ,
39- ) {
34+ constructor ( renderer , obj , w , h ) {
4035 super ( renderer , obj )
4136 this . width = w
4237 this . height = h
@@ -64,15 +59,33 @@ class RawTextureWrapper extends p5.Texture {
6459}
6560
6661class Framebuffer {
67- constructor ( canvas ) {
62+ constructor ( canvas , options = { } ) {
6863 this . _renderer = canvas . _renderer
69-
7064 const gl = this . _renderer . GL
71- const ext = gl . getExtension ( 'WEBGL_depth_texture' )
72- if ( ! ext ) {
65+ if ( ! gl . getExtension ( 'WEBGL_depth_texture' ) ) {
7366 throw new Error ( 'Unable to create depth textures in this environment' )
7467 }
7568
69+ this . colorFormat = this . glColorFormat ( options . colorFormat )
70+ this . depthFormat = this . glDepthFormat ( options . depthFormat )
71+ if (
72+ ( options . colorFormat === 'float' || options . depthFormat === 'float' ) &&
73+ ( ! gl . getExtension ( 'OES_texture_float' ) ||
74+ ! gl . getExtension ( 'OES_texture_float_linear' ) ||
75+ ! gl . getExtension ( 'WEBGL_color_buffer_float' ) )
76+ ) {
77+ // Reset to default
78+ if ( options . colorFormat === 'float' ) {
79+ this . colorFormat = this . glColorFormat ( )
80+ }
81+ if ( options . depthFormat === 'float' ) {
82+ this . depthFormat = this . glDepthFormat ( )
83+ }
84+ console . warn (
85+ 'Warning: Unable to create floating point textures in this environment. Falling back to integers' ,
86+ )
87+ }
88+
7689 const framebuffer = gl . createFramebuffer ( )
7790 if ( ! framebuffer ) {
7891 throw new Error ( 'Unable to create a framebuffer' )
@@ -81,6 +94,21 @@ class Framebuffer {
8194 this . recreateTextures ( )
8295 }
8396
97+ glColorFormat ( format ) {
98+ const gl = this . _renderer . GL
99+ if ( format === 'float' ) {
100+ return gl . FLOAT
101+ }
102+ return gl . UNSIGNED_BYTE
103+ }
104+ glDepthFormat ( format ) {
105+ const gl = this . _renderer . GL
106+ if ( format === 'float' ) {
107+ return gl . FLOAT
108+ }
109+ return gl . UNSIGNED_SHORT
110+ }
111+
84112 handleResize ( ) {
85113 const oldColor = this . colorTexture
86114 const oldDepth = this . depthTexture
@@ -117,7 +145,7 @@ class Framebuffer {
117145 height * density ,
118146 0 ,
119147 hasAlpha ? gl . RGBA : gl . RGB ,
120- gl . UNSIGNED_BYTE ,
148+ this . colorFormat ,
121149 null ,
122150 )
123151
@@ -140,7 +168,7 @@ class Framebuffer {
140168 height * density ,
141169 0 ,
142170 gl . DEPTH_COMPONENT ,
143- gl . UNSIGNED_SHORT ,
171+ this . depthFormat ,
144172 null ,
145173 )
146174
0 commit comments