-
-
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 10 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 |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ import { CubemapTexture, MipmapTexture } from './p5.Texture'; | |
|
|
||
| const STROKE_CAP_ENUM = {}; | ||
| const STROKE_JOIN_ENUM = {}; | ||
| const shaderCache = {}; //Shader cache object | ||
|
|
||
| let lineDefs = ''; | ||
| const defineStrokeCapEnum = function (key, val) { | ||
| lineDefs += `#define STROKE_CAP_${key} ${val}\n`; | ||
|
|
@@ -32,85 +34,114 @@ defineStrokeJoinEnum('ROUND', 0); | |
| defineStrokeJoinEnum('MITER', 1); | ||
| defineStrokeJoinEnum('BEVEL', 2); | ||
|
|
||
| const lightingShader = readFileSync( | ||
| const getCachedShader= (shaderKey, shaderSource)=>{ | ||
| if(!shaderCache[shaderKey]){ | ||
| shaderCache[shaderKey]=p5.createShader(shaderSource); | ||
|
||
| } | ||
| return shaderCache[shaderKey]; | ||
| }; | ||
|
|
||
| const lightingShader = getCachedShader('lightingShader',readFileSync( | ||
| join(__dirname, '/shaders/lighting.glsl'), | ||
| 'utf-8' | ||
| ); | ||
| const webgl2CompatibilityShader = readFileSync( | ||
| )); | ||
| const webgl2CompatibilityShader = getCachedShader('webgl2CompatibilityShader',readFileSync( | ||
|
||
| join(__dirname, '/shaders/webgl2Compatibility.glsl'), | ||
| 'utf-8' | ||
| ); | ||
| const cubemapFragmentShader=readFileSync( | ||
| join(__dirname,'/shaders/cubeFragment.glsl'), | ||
| 'utf8' | ||
| ); | ||
| const cubemapVertexShader=readFileSync( | ||
| join(__dirname,'/shaders/cubeVertex.glsl'), | ||
| 'utf8' | ||
| ); | ||
| )); | ||
|
|
||
| const defaultShaders = { | ||
| immediateVert: readFileSync( | ||
| immediateVert: getCachedShader('immediateVert',readFileSync( | ||
| join(__dirname, '/shaders/immediate.vert'), | ||
| 'utf-8' | ||
| ), | ||
| vertexColorVert: readFileSync( | ||
| )), | ||
| vertexColorVert: getCachedShader('vertexColorVert',readFileSync( | ||
| join(__dirname, '/shaders/vertexColor.vert'), | ||
| 'utf-8' | ||
| ), | ||
| vertexColorFrag: readFileSync( | ||
| )), | ||
| vertexColorFrag: getCachedShader('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') | ||
| )), | ||
| normalVert: getCachedShader('normalVert',readFileSync( | ||
| join(__dirname, '/shaders/normal.vert'), 'utf-8' | ||
| )), | ||
| normalFrag: getCachedShader('normalFrag',readFileSync( | ||
| join(__dirname, '/shaders/normal.frag'), 'utf-8' | ||
| )), | ||
| basicFrag: getCachedShader('basicFrag',readFileSync( | ||
| join(__dirname, '/shaders/basic.frag'), 'utf-8' | ||
| )), | ||
| lightVert: getCachedShader('lightVert',lightingShader + readFileSync( | ||
| join(__dirname, '/shaders/light.vert'), 'utf-8' | ||
| )), | ||
| lightTextureFrag:getCachedShader('lightTextureFrag', readFileSync( | ||
| join(__dirname, '/shaders/light_texture.frag'),'utf-8' | ||
| )), | ||
| phongVert: getCachedShader('phongVert',readFileSync( | ||
| join(__dirname, '/shaders/phong.vert'), 'utf-8' | ||
| )), | ||
| phongFrag: getCachedShader('phongFrag', lightingShader + readFileSync( | ||
| join(__dirname, '/shaders/phong.frag'), 'utf-8' | ||
| )), | ||
| fontVert: getCachedShader('fontVert',readFileSync( | ||
| join(__dirname, '/shaders/font.vert'), 'utf-8' | ||
| )), | ||
| fontFrag: getCachedShader('fontFrag',readFileSync( | ||
| join(__dirname, '/shaders/font.frag'), 'utf-8' | ||
| )), | ||
| lineVert: getCachedShader('lineVert',lineDefs + readFileSync( | ||
| join(__dirname, '/shaders/line.vert'), 'utf-8' | ||
| )), | ||
| lineFrag : getCachedShader('lineFrag',lineDefs + readFileSync( | ||
| join(__dirname, '/shaders/line.frag'), 'utf-8' | ||
| )), | ||
| pointVert:getCachedShader('pointVert', readFileSync( | ||
| join(__dirname, '/shaders/point.vert'), 'utf-8' | ||
| )), | ||
| pointFrag:getCachedShader('pointFrag', readFileSync( | ||
| join(__dirname, '/shaders/point.frag'), 'utf-8' | ||
| )), | ||
| imageLightVert:getCachedShader('imageLightVert', readFileSync( | ||
| join(__dirname, '/shaders/imageLight.vert'), 'utf-8' | ||
| )), | ||
| imageLightDiffusedFrag:getCachedShader('imageLightDiffusedFrag', readFileSync( | ||
| join(__dirname, '/shaders/imageLightDiffused.frag'), 'utf-8' | ||
| )), | ||
| imageLightSpecularFrag:getCachedShader('imageLightSpecularFrag', readFileSync( | ||
| join(__dirname, '/shaders/imageLightSpecular.frag'), 'utf-8' | ||
| )), | ||
| cubemapVertexShader:getCachedShader('cubemapVertexShader',readFileSync( | ||
| join(__dirname,'/shaders/cubeVertex.vert'),'utf8' | ||
| )), | ||
| cubemapFragmentShader:getCachedShader('cubemapFragmentShader',readFileSync( | ||
| join(__dirname,'/shaders/cubeFragment.frag'),'utf8' | ||
| )) | ||
| }; | ||
| 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') | ||
| [constants.GRAY]:getCachedShader(constants.GRAY, | ||
| readFileSync(join(__dirname, '/shaders/filters/gray.frag'), 'utf-8')), | ||
| [constants.ERODE]:getCachedShader(constants.ERODE, | ||
| readFileSync(join(__dirname, '/shaders/filters/erode.frag'), 'utf-8')), | ||
| [constants.DILATE]:getCachedShader(constants.DILATE, | ||
| readFileSync(join(__dirname, '/shaders/filters/dilate.frag'), 'utf-8')), | ||
| [constants.BLUR]:getCachedShader(constants.BLUR, | ||
| readFileSync(join(__dirname, '/shaders/filters/blur.frag'), 'utf-8')), | ||
| [constants.POSTERIZE]:getCachedShader(constants.POSTERIZE, | ||
| readFileSync(join(__dirname, '/shaders/filters/posterize.frag'), 'utf-8')), | ||
| [constants.OPAQUE]:getCachedShader(constants.OPAQUE, | ||
| readFileSync(join(__dirname, '/shaders/filters/opaque.frag'), 'utf-8')), | ||
| [constants.INVERT]:getCachedShader(constants.INVERT, | ||
| readFileSync(join(__dirname, '/shaders/filters/invert.frag'), 'utf-8')), | ||
| [constants.THRESHOLD]:getCachedShader(constants.THRESHOLD, | ||
| readFileSync(join(__dirname, '/shaders/filters/threshold.frag'), 'utf-8')) | ||
| }; | ||
| const filterShaderVert = readFileSync(join(__dirname, '/shaders/filters/default.vert'), 'utf-8'); | ||
| const filterShaderVert = getCachedShader('filterShaderVert',readFileSync( | ||
| join(__dirname, '/shaders/filters/default.vert'), 'utf-8')); | ||
|
|
||
| /** | ||
| * @module Rendering | ||
|
|
@@ -1848,6 +1879,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 | ||
|
|
@@ -1925,8 +1969,8 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
| } | ||
| // Create a shader for cubemap conversion | ||
| const cubemapShader = this._pInst.createShader( | ||
|
||
| cubemapVertexShader, | ||
| cubemapFragmentShader); | ||
| defaultShaders.cubemapVertexShader, | ||
| defaultShaders.cubemapFragmentShader); | ||
|
|
||
| // Render each face of the cubemap | ||
| for (let i = 0; i < 6; ++i) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -225,7 +225,7 @@ p5.Shader = class { | |
| uniform.name = uniformName; | ||
| uniform.type = uniformInfo.type; | ||
| uniform._cachedData = undefined; | ||
| if (uniform.type === gl.SAMPLER_2D) { | ||
| if (uniform.type === gl.SAMPLER_2D || uniform.type === gl.SAMPLER_CUBE) { | ||
| uniform.samplerIndex = samplerIndex; | ||
| samplerIndex++; | ||
| this.samplers.push(uniform); | ||
|
|
@@ -548,6 +548,12 @@ p5.Shader = class { | |
| uniform.texture.src._animateGif(this._renderer._pInst); | ||
| } | ||
| break; | ||
| case gl.SAMPLER_CUBE: | ||
|
||
| gl.activeTexture(gl.TEXTURE0 + uniform.samplerIndex); | ||
| uniform.texture = | ||
| data instanceof p5.Texture ? data : this._renderer.getTexture(data); | ||
| gl.uniform1i(location, uniform.samplerIndex); | ||
| break; | ||
| //@todo complete all types | ||
| } | ||
| return this; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| #version 330 core | ||
| out vec4 FragColor; | ||
| in vec3 localPos; | ||
| // Adapted from: https://learnopengl.com/PBR/IBL/Diffuse-irradiance | ||
| IN vec3 localPos; | ||
|
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. In my opinion I feel for maximum compatibility, we can do this one in GL ES 100 instead of 300? Using attribute instead of IN, using the spacial variable gl_FragColor instead of defining an OUT_COLOR, and using texture2D() instead of texture()? What you feel?
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. Well I started from that only , and was getting a lot of errors and from Dave's suggestion of using the
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. Hmmm...It looks like you need to use both webgl-1 and webgl-2 modes on shaders. |
||
|
|
||
| uniform sampler2D equirectangularMap; | ||
|
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 think you have hard-coded the value of equirectangularMap to "0" which won't work or its actually a float value. |
||
|
|
||
|
|
@@ -19,5 +18,5 @@ void main() | |
| vec2 uv = SampleSphericalMap(normalize(localPos)); | ||
| vec3 color = texture(equirectangularMap, uv).rgb; | ||
|
||
|
|
||
| FragColor = vec4(color, 1.0); | ||
| OUT_COLOR = vec4(color, 1.0); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| #version 330 core | ||
| // Adapted from: https://learnopengl.com/PBR/IBL/Diffuse-irradiance | ||
| layout (location = 0) in vec3 aPos; | ||
|
||
|
|
||
| out vec3 localPos; | ||
| OUT vec3 localPos; | ||
|
|
||
| uniform mat4 projection; | ||
| uniform mat4 view; | ||
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,7 +39,7 @@ uniform float uQuadraticAttenuation; | |
| // boolean to initiate the calculateImageDiffuse and calculateImageSpecular | ||
| uniform bool uUseImageLight; | ||
| // texture for use in calculateImageDiffuse | ||
| uniform sampler2D environmentMapDiffusedCubemap; | ||
| uniform samplerCube environmentMapDiffusedCubemap; | ||
| // texture for use in calculateImageSpecular | ||
| uniform sampler2D environmentMapSpecular; | ||
| // roughness for use in calculateImageSpecular | ||
|
|
@@ -111,7 +111,7 @@ vec3 calculateImageDiffuse( vec3 vNormal, vec3 vViewPosition ){ | |
| vec3 worldCameraPosition = vec3(0.0, 0.0, 0.0); // hardcoded world camera position | ||
| vec3 worldNormal = normalize(vNormal); | ||
| vec2 newTexCoor = mapTextureToNormal( worldNormal ); | ||
| vec4 texture = textureCube( environmentMapDiffusedCubemap, newTexCoor ); | ||
| vec4 texture = TEXTURE_CUBE( environmentMapDiffusedCubemap, newTexCoor ); | ||
|
||
| // this is to make the darker sections more dark | ||
| // png and jpg usually flatten the brightness so it is to reverse that | ||
| return smoothstep(vec3(0.0), vec3(0.8), texture.xyz); | ||
|
|
||
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.
I think this would need to be not just a
const, but a property onp5.RendererGL, as each instance of a renderer will need its own shaders. We just want the ability to share those shaders within the renderer.