-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Addresses issue #6587 #6665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Addresses issue #6587 #6665
Changes from 25 commits
227fe69
8e2d05c
317d915
917de2f
5139c0a
45e33d5
1d54fad
1ff8702
6cd7856
4401ebd
37751d9
93b8b50
66dc0dc
cffda69
751b16a
29e7a14
7981fdc
e7cd392
f30af0e
d51c4a8
38bcaab
2558755
773198a
bbc7c72
ffd9097
cc77e7e
518b0f0
c124738
38e6e84
a585ef5
f118a3b
2da03d1
5961be8
4515c09
be6d27f
6dc9916
3133e80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,10 +9,11 @@ import './p5.Matrix'; | |
| import './p5.Framebuffer'; | ||
| import { readFileSync } from 'fs'; | ||
| import { join } from 'path'; | ||
| import { MipmapTexture } from './p5.Texture'; | ||
| import { CubemapTexture, MipmapTexture } from './p5.Texture'; | ||
|
|
||
| const STROKE_CAP_ENUM = {}; | ||
| const STROKE_JOIN_ENUM = {}; | ||
|
|
||
| let lineDefs = ''; | ||
| const defineStrokeCapEnum = function (key, val) { | ||
| lineDefs += `#define STROKE_CAP_${key} ${val}\n`; | ||
|
|
@@ -32,77 +33,7 @@ defineStrokeJoinEnum('ROUND', 0); | |
| defineStrokeJoinEnum('MITER', 1); | ||
| defineStrokeJoinEnum('BEVEL', 2); | ||
|
|
||
| const lightingShader = readFileSync( | ||
| join(__dirname, '/shaders/lighting.glsl'), | ||
| 'utf-8' | ||
| ); | ||
| const webgl2CompatibilityShader = readFileSync( | ||
| join(__dirname, '/shaders/webgl2Compatibility.glsl'), | ||
| 'utf-8' | ||
| ); | ||
|
|
||
| const defaultShaders = { | ||
| immediateVert: readFileSync( | ||
| join(__dirname, '/shaders/immediate.vert'), | ||
| 'utf-8' | ||
| ), | ||
| vertexColorVert: readFileSync( | ||
| join(__dirname, '/shaders/vertexColor.vert'), | ||
| 'utf-8' | ||
| ), | ||
| vertexColorFrag: readFileSync( | ||
| join(__dirname, '/shaders/vertexColor.frag'), | ||
| 'utf-8' | ||
| ), | ||
| normalVert: readFileSync(join(__dirname, '/shaders/normal.vert'), 'utf-8'), | ||
| normalFrag: readFileSync(join(__dirname, '/shaders/normal.frag'), 'utf-8'), | ||
| basicFrag: readFileSync(join(__dirname, '/shaders/basic.frag'), 'utf-8'), | ||
| lightVert: | ||
| lightingShader + | ||
| readFileSync(join(__dirname, '/shaders/light.vert'), 'utf-8'), | ||
| lightTextureFrag: readFileSync( | ||
| join(__dirname, '/shaders/light_texture.frag'), | ||
| 'utf-8' | ||
| ), | ||
| phongVert: readFileSync(join(__dirname, '/shaders/phong.vert'), 'utf-8'), | ||
| phongFrag: | ||
| lightingShader + | ||
| readFileSync(join(__dirname, '/shaders/phong.frag'), 'utf-8'), | ||
| fontVert: readFileSync(join(__dirname, '/shaders/font.vert'), 'utf-8'), | ||
| fontFrag: readFileSync(join(__dirname, '/shaders/font.frag'), 'utf-8'), | ||
| lineVert: | ||
| lineDefs + readFileSync(join(__dirname, '/shaders/line.vert'), 'utf-8'), | ||
| lineFrag: | ||
| lineDefs + readFileSync(join(__dirname, '/shaders/line.frag'), 'utf-8'), | ||
| pointVert: readFileSync(join(__dirname, '/shaders/point.vert'), 'utf-8'), | ||
| pointFrag: readFileSync(join(__dirname, '/shaders/point.frag'), 'utf-8'), | ||
| imageLightVert: readFileSync(join(__dirname, '/shaders/imageLight.vert'), 'utf-8'), | ||
| imageLightDiffusedFrag: readFileSync(join(__dirname, '/shaders/imageLightDiffused.frag'), 'utf-8'), | ||
| imageLightSpecularFrag: readFileSync(join(__dirname, '/shaders/imageLightSpecular.frag'), 'utf-8') | ||
| }; | ||
| for (const key in defaultShaders) { | ||
| defaultShaders[key] = webgl2CompatibilityShader + defaultShaders[key]; | ||
| } | ||
|
|
||
| const filterShaderFrags = { | ||
| [constants.GRAY]: | ||
| readFileSync(join(__dirname, '/shaders/filters/gray.frag'), 'utf-8'), | ||
| [constants.ERODE]: | ||
| readFileSync(join(__dirname, '/shaders/filters/erode.frag'), 'utf-8'), | ||
| [constants.DILATE]: | ||
| readFileSync(join(__dirname, '/shaders/filters/dilate.frag'), 'utf-8'), | ||
| [constants.BLUR]: | ||
| readFileSync(join(__dirname, '/shaders/filters/blur.frag'), 'utf-8'), | ||
| [constants.POSTERIZE]: | ||
| readFileSync(join(__dirname, '/shaders/filters/posterize.frag'), 'utf-8'), | ||
| [constants.OPAQUE]: | ||
| readFileSync(join(__dirname, '/shaders/filters/opaque.frag'), 'utf-8'), | ||
| [constants.INVERT]: | ||
| readFileSync(join(__dirname, '/shaders/filters/invert.frag'), 'utf-8'), | ||
| [constants.THRESHOLD]: | ||
| readFileSync(join(__dirname, '/shaders/filters/threshold.frag'), 'utf-8') | ||
| }; | ||
| const filterShaderVert = readFileSync(join(__dirname, '/shaders/filters/default.vert'), 'utf-8'); | ||
| let defaultShaders={}; | ||
|
|
||
| /** | ||
| * @module Rendering | ||
|
|
@@ -444,6 +375,24 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
| this.GL = this.drawingContext; | ||
| this._pInst._setProperty('drawingContext', this.drawingContext); | ||
|
|
||
| this._pInst.shaderCache={}; | ||
| this.getCachedShader = | ||
| function (shaderKey, vertexShaderSource, fragmentShaderSource) { | ||
| if (!this._pInst.shaderCache) { | ||
| this._pInst.shaderCache = {}; // ensures a unique shaderCache for each instance of p5.RendererGL | ||
| } | ||
| if (!this._pInst.shaderCache[shaderKey]) { | ||
| const vertexShader = this._pInst.createShader( | ||
| this._pInst.VERTEX_SHADER, vertexShaderSource); | ||
| const fragmentShader = this._pInst.createShader( | ||
| this._pInst.FRAGMENT_SHADER, fragmentShaderSource); | ||
|
|
||
| this._pInst.shaderCache[shaderKey] = this._pInst.createShaderProgram( | ||
| vertexShader, fragmentShader); | ||
| } | ||
| return this._pInst.shaderCache[shaderKey]; | ||
| }; | ||
|
|
||
| // erasing | ||
| this._isErasing = false; | ||
|
|
||
|
|
@@ -564,6 +513,7 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
| this._defaultNormalShader = undefined; | ||
| this._defaultColorShader = undefined; | ||
| this._defaultPointShader = undefined; | ||
| this._defaultCubemapShader=undefined; | ||
|
|
||
| this.userFillShader = undefined; | ||
| this.userStrokeShader = undefined; | ||
|
|
@@ -830,6 +780,122 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
| this._pInst, | ||
| !isPGraphics | ||
| ); | ||
|
|
||
|
|
||
| const lightingShader = renderer.getCachedShader('lightingShader',readFileSync( | ||
| join(__dirname, '/shaders/lighting.glsl'), | ||
| 'utf-8' | ||
| )); | ||
| const webgl2CompatibilityShader = readFileSync( | ||
| join(__dirname, '/shaders/webgl2Compatibility.glsl'), | ||
| 'utf-8' | ||
| ); | ||
|
|
||
| defaultShaders = { | ||
| immediateVert: renderer.getCachedShader('immediateVert',readFileSync( | ||
| join(__dirname, '/shaders/immediate.vert'), | ||
| 'utf-8' | ||
| )), | ||
| vertexColorVert: renderer.getCachedShader('vertexColorVert',readFileSync( | ||
| join(__dirname, '/shaders/vertexColor.vert'), | ||
| 'utf-8' | ||
| )), | ||
| vertexColorFrag: renderer.getCachedShader('vertexColorFrag',readFileSync( | ||
| join(__dirname, '/shaders/vertexColor.frag'), | ||
| 'utf-8' | ||
| )), | ||
| normalVert: renderer.getCachedShader('normalVert',readFileSync( | ||
| join(__dirname, '/shaders/normal.vert'), 'utf-8' | ||
| )), | ||
| normalFrag: renderer.getCachedShader('normalFrag',readFileSync( | ||
| join(__dirname, '/shaders/normal.frag'), 'utf-8' | ||
| )), | ||
| basicFrag: renderer.getCachedShader('basicFrag',readFileSync( | ||
| join(__dirname, '/shaders/basic.frag'), 'utf-8' | ||
| )), | ||
| lightVert: renderer.getCachedShader('lightVert',lightingShader + readFileSync( | ||
| join(__dirname, '/shaders/light.vert'), 'utf-8' | ||
| )), | ||
| lightTextureFrag:renderer.getCachedShader('lightTextureFrag', readFileSync( | ||
| join(__dirname, '/shaders/light_texture.frag'),'utf-8' | ||
| )), | ||
| phongVert: renderer.getCachedShader('phongVert',readFileSync( | ||
| join(__dirname, '/shaders/phong.vert'), 'utf-8' | ||
| )), | ||
| phongFrag: renderer.getCachedShader('phongFrag', lightingShader + readFileSync( | ||
| join(__dirname, '/shaders/phong.frag'), 'utf-8' | ||
| )), | ||
| fontVert: renderer.getCachedShader('fontVert',readFileSync( | ||
| join(__dirname, '/shaders/font.vert'), 'utf-8' | ||
| )), | ||
| fontFrag: renderer.getCachedShader('fontFrag',readFileSync( | ||
| join(__dirname, '/shaders/font.frag'), 'utf-8' | ||
| )), | ||
| lineVert: renderer.getCachedShader('lineVert',lineDefs + readFileSync( | ||
| join(__dirname, '/shaders/line.vert'), 'utf-8' | ||
| )), | ||
| lineFrag : renderer.getCachedShader('lineFrag',lineDefs + readFileSync( | ||
| join(__dirname, '/shaders/line.frag'), 'utf-8' | ||
| )), | ||
| pointVert:renderer.getCachedShader('pointVert', readFileSync( | ||
| join(__dirname, '/shaders/point.vert'), 'utf-8' | ||
| )), | ||
| pointFrag:renderer.getCachedShader('pointFrag', readFileSync( | ||
| join(__dirname, '/shaders/point.frag'), 'utf-8' | ||
| )), | ||
| imageLightVert:renderer.getCachedShader('imageLightVert', readFileSync( | ||
| join(__dirname, '/shaders/imageLight.vert'), 'utf-8' | ||
| )), | ||
| imageLightDiffusedFrag:renderer.getCachedShader('imageLightDiffusedFrag', readFileSync( | ||
| join(__dirname, '/shaders/imageLightDiffused.frag'), 'utf-8' | ||
| )), | ||
| imageLightSpecularFrag:renderer.getCachedShader('imageLightSpecularFrag', readFileSync( | ||
| join(__dirname, '/shaders/imageLightSpecular.frag'), 'utf-8' | ||
| )), | ||
| cubemapVertexShader:renderer.getCachedShader('cubemapVertexShader',readFileSync( | ||
| join(__dirname,'/shaders/cubeVertex.vert'),'utf8' | ||
| )), | ||
| cubemapFragmentShader:renderer.getCachedShader('cubemapFragmentShader',readFileSync( | ||
| join(__dirname,'/shaders/cubeFragment.frag'),'utf8' | ||
| )) | ||
| }; | ||
| for (const key in defaultShaders) { | ||
| defaultShaders[key] = webgl2CompatibilityShader + defaultShaders[key]; | ||
| } | ||
|
|
||
| const filterShaderFrags = { | ||
| [constants.GRAY]:renderer.getCachedShader( | ||
| constants.GRAY,readFileSync(join( | ||
| __dirname, '/shaders/filters/gray.frag'), 'utf-8')), | ||
| [constants.ERODE]:renderer.getCachedShader( | ||
| constants.ERODE,readFileSync(join( | ||
| __dirname, '/shaders/filters/erode.frag'), 'utf-8')), | ||
| [constants.DILATE]:renderer.getCachedShader( | ||
| constants.DILATE,readFileSync(join( | ||
| __dirname, '/shaders/filters/dilate.frag'), 'utf-8')), | ||
| [constants.BLUR]:renderer.getCachedShader( | ||
| constants.BLUR,readFileSync(join( | ||
| __dirname, '/shaders/filters/blur.frag'), 'utf-8')), | ||
| [constants.POSTERIZE]:renderer.getCachedShader( | ||
| constants.POSTERIZE,readFileSync(join( | ||
| __dirname, '/shaders/filters/posterize.frag'), 'utf-8')), | ||
| [constants.OPAQUE]:renderer.getCachedShader( | ||
| constants.OPAQUE,readFileSync(join( | ||
| __dirname, '/shaders/filters/opaque.frag'), 'utf-8')), | ||
| [constants.INVERT]:renderer.getCachedShader( | ||
| constants.INVERT,readFileSync(join( | ||
| __dirname, '/shaders/filters/invert.frag'), 'utf-8')), | ||
| [constants.THRESHOLD]:renderer.getCachedShader( | ||
| constants.THRESHOLD,readFileSync(join( | ||
| __dirname, '/shaders/filters/threshold.frag'), 'utf-8')) | ||
| }; | ||
|
|
||
| const filterShaderVert = renderer.getCachedShader( | ||
| 'filterShaderVert',readFileSync(join( | ||
| __dirname, '/shaders/filters/default.vert'), 'utf-8')); | ||
|
|
||
|
|
||
|
|
||
| this._pInst._setProperty('_renderer', renderer); | ||
| renderer.resize(w, h); | ||
| renderer._applyDefaults(); | ||
|
|
@@ -1846,6 +1912,19 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
| return this._defaultFontShader; | ||
| } | ||
|
|
||
| _getCubemapShader() { | ||
| if (!this._defaultCubemapShader) { | ||
| this._defaultCubemapShader = new p5.Shader( | ||
| this, | ||
| this._webGL2CompatibilityPrefix('vert', 'mediump') + | ||
| defaultShaders.cubemapVertexShader, | ||
| this._webGL2CompatibilityPrefix('frag', 'mediump') + | ||
| defaultShaders.cubemapFragmentShader | ||
| ); | ||
| } | ||
| return this._defaultCubemapShader; | ||
| } | ||
|
|
||
| _webGL2CompatibilityPrefix( | ||
| shaderType, | ||
| floatPrecision | ||
|
|
@@ -1908,6 +1987,8 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
| let smallWidth = 200; | ||
| let width = smallWidth; | ||
| let height = Math.floor(smallWidth * (input.height / input.width)); | ||
| let cubemapTexture; | ||
| const faces = []; | ||
| newFramebuffer = this._pInst.createFramebuffer({ | ||
| width, height, density: 1 | ||
| }); | ||
|
|
@@ -1919,23 +2000,47 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
| defaultShaders.imageLightDiffusedFrag | ||
| ); | ||
| } | ||
| // Create a shader for cubemap conversion | ||
| const cubemapShader = this._getCubemapShader(); | ||
|
|
||
| // Render each face of the cubemap | ||
| for (let i = 0; i < 6; ++i) { | ||
| newFramebuffer.draw(() => { | ||
| cubemapShader.use(); | ||
| cubemapShader.setUniform('equirectangularMap', input); | ||
| cubemapShader.setUniform('projection', captureProjection); | ||
| cubemapShader.setUniform('view', captureViews[i]); | ||
|
|
||
| this._pInst.noStroke(); | ||
| this._pInst.rectMode(constants.CENTER); | ||
| this._pInst.noLights(); | ||
| this._pInst.rect(0, 0, width, height); | ||
|
|
||
| // Capture the rendered face and store it in the faces array | ||
| faces[i] = this._pInst.get(0, 0, width, height); | ||
|
||
| }); | ||
| } | ||
| // Use the diffusedShader for rendering | ||
| newFramebuffer.draw(() => { | ||
|
||
| this._pInst.shader(this.diffusedShader); | ||
| this.diffusedShader.setUniform('environmentMap', input); | ||
| this._pInst.noStroke(); | ||
| this._pInst.rectMode(constants.CENTER); | ||
| this._pInst.noLights(); | ||
| this._pInst.rect(0, 0, width, height); | ||
| // Render the cubemap using the stored faces | ||
| for (let i = 0; i < 6; ++i) { | ||
| this._pInst.image(faces[i], 0, 0, width, height); | ||
| } | ||
| }); | ||
| this.diffusedTextures.set(input, newFramebuffer); | ||
| return newFramebuffer; | ||
| // Initialize CubemapTexture class with faces | ||
| cubemapTexture=new CubemapTexture(this._pInst,faces, {}); | ||
| cubemapTexture.init(faces); | ||
| this.diffusedTextures.set(input, cubemapTexture); | ||
| return cubemapTexture; | ||
| } | ||
|
|
||
| /* | ||
| * used in imageLight, | ||
| * To create a texture from the input non blurry image, if it doesn't already exist | ||
| * Creating 8 different levels of textures according to different | ||
| * sizes and atoring them in `levels` array | ||
| * sizes and storing them in `levels` array | ||
| * Creating a new Mipmap texture with that `levels` array | ||
| * Storing the texture for input image in map called `specularTextures` | ||
| * maps the input p5.Image to a p5.MipmapTexture | ||
|
|
@@ -2112,7 +2217,7 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
| // this.activeImageLight has image as a key | ||
| // look up the texture from the diffusedTexture map | ||
| let diffusedLight = this.getDiffusedTexture(this.activeImageLight); | ||
| shader.setUniform('environmentMapDiffused', diffusedLight); | ||
| shader.setUniform('environmentMapDiffusedCubemap', diffusedLight); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure but I feel diffusedLight is also a sampler2D texture, could it be passed as a uniform with samplerCube texture?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, diffusedLight is obtained from
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oooh...sorry. I overlooked this, previously we had diffusedLight with |
||
| let specularLight = this.getSpecularTexture(this.activeImageLight); | ||
| // In p5js the range of shininess is >= 1, | ||
| // Therefore roughness range will be ([0,1]*8)*20 or [0, 160] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One issue with this current structure is that we're calling
getCachedShaderin thein_resetContext`, so on load, it will try to make a bunch of shaders.We maybe want to keep the shader source code in objects outside the constructor as it was before, and then we only call
getCachedShaderwhen we need to use the shader. e.g.: