-
-
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 26 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`; | ||
|
|
@@ -78,7 +79,9 @@ const defaultShaders = { | |
| 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') | ||
| imageLightSpecularFrag: readFileSync(join(__dirname, '/shaders/imageLightSpecular.frag'), 'utf-8'), | ||
| cubemapVertexShader: readFileSync(join(__dirname, '/shaders/cubeVertex.vert'), 'utf-8'), | ||
| cubemapFragmentShader: readFileSync(join(__dirname, '/shaders/cubeFragment.frag'), 'utf-8') | ||
| }; | ||
| for (const key in defaultShaders) { | ||
| defaultShaders[key] = webgl2CompatibilityShader + defaultShaders[key]; | ||
|
|
@@ -104,6 +107,7 @@ const filterShaderFrags = { | |
| }; | ||
| const filterShaderVert = readFileSync(join(__dirname, '/shaders/filters/default.vert'), 'utf-8'); | ||
|
|
||
|
|
||
| /** | ||
| * @module Rendering | ||
| * @submodule Rendering | ||
|
|
@@ -444,6 +448,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 +586,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 +853,7 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
| this._pInst, | ||
| !isPGraphics | ||
| ); | ||
|
|
||
| this._pInst._setProperty('_renderer', renderer); | ||
| renderer.resize(w, h); | ||
| renderer._applyDefaults(); | ||
|
|
@@ -1846,6 +1870,15 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
| return this._defaultFontShader; | ||
| } | ||
|
|
||
| _getCubemapShader() { | ||
| return this.getCachedShader('_defaultCubemapShader', | ||
| this._webGL2CompatibilityPrefix('vert', 'mediump') + | ||
| defaultShaders.cubemapVertexShader, | ||
| this._webGL2CompatibilityPrefix('frag', 'mediump') + | ||
| defaultShaders.cubemapFragmentShader | ||
| ); | ||
| } | ||
|
|
||
| _webGL2CompatibilityPrefix( | ||
| shaderType, | ||
| floatPrecision | ||
|
|
@@ -1908,6 +1941,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 +1954,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 +2171,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] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // 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. |
||
|
|
||
| const vec2 invAtan = vec2(0.1591, 0.3183); | ||
|
|
||
| vec2 SampleSphericalMap(vec3 v) | ||
| { | ||
| vec2 uv = vec2(atan(v.z, v.x), asin(v.y)); | ||
| uv *= invAtan; | ||
| uv += 0.5; | ||
| return uv; | ||
| } | ||
|
|
||
| void main() | ||
| { | ||
| vec2 uv = SampleSphericalMap(normalize(localPos)); | ||
| vec3 color = texture(equirectangularMap, uv).rgb; | ||
|
||
|
|
||
| OUT_COLOR = vec4(color, 1.0); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Adapted from: https://learnopengl.com/PBR/IBL/Diffuse-irradiance | ||
| layout (location = 0) in vec3 aPos; | ||
|
||
|
|
||
| OUT vec3 localPos; | ||
|
|
||
| uniform mat4 projection; | ||
| uniform mat4 view; | ||
|
||
|
|
||
| void main() | ||
| { | ||
| localPos = aPos; | ||
| gl_Position = projection * view * vec4(localPos, 1.0); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,7 @@ uniform float uQuadraticAttenuation; | |
| // boolean to initiate the calculateImageDiffuse and calculateImageSpecular | ||
| uniform bool uUseImageLight; | ||
| // texture for use in calculateImageDiffuse | ||
| uniform sampler2D environmentMapDiffused; | ||
| uniform samplerCube environmentMapDiffusedCubemap; | ||
| // texture for use in calculateImageSpecular | ||
| uniform sampler2D environmentMapSpecular; | ||
| // roughness for use in calculateImageSpecular | ||
|
|
@@ -114,7 +114,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 = TEXTURE( environmentMapDiffused, 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 mix(smoothstep(vec3(0.0), vec3(1.0), texture.xyz), vec3(0.0), metallic); | ||
|
|
||
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.
Also probably not the source of the error, but we might need to put this outside of the
draw(...)block, and usefaces[i] = newFramebuffer.get()instead ofthis._pInst.get(...).