Skip to content
Draft
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
227fe69
Extending p5.texture for cubemap textures
Garima3110 Dec 24, 2023
8e2d05c
Merge remote-tracking branch 'upstream/main' into cubeMap
Garima3110 Dec 25, 2023
317d915
Merge remote-tracking branch 'upstream/main' into cubeMap
Garima3110 Dec 28, 2023
917de2f
Added vertex and fragment shaders for cubemaps
Garima3110 Dec 28, 2023
5139c0a
Updated src/webgl/shaders/lighting.glsl file for cubemaps
Garima3110 Dec 28, 2023
45e33d5
Storing the diffuse irradiance in a cubemap texture
Garima3110 Dec 28, 2023
1d54fad
Added caching mechanism for shaders
Garima3110 Dec 29, 2023
1ff8702
Updated caching for some shaders
Garima3110 Dec 29, 2023
6cd7856
Updated p5.Texture.js
Garima3110 Dec 29, 2023
4401ebd
Updated cubeVertex and cubeFragment shaders
Garima3110 Dec 29, 2023
37751d9
renamed cubemap shader files
Garima3110 Dec 29, 2023
93b8b50
Updated p5.RendererGL.js
Garima3110 Dec 29, 2023
66dc0dc
Updated p5.Texture.js
Garima3110 Dec 29, 2023
cffda69
Updated lighting.glsl
Garima3110 Dec 29, 2023
751b16a
Added TEXTURE_CUBE
Garima3110 Dec 29, 2023
29e7a14
Added gl.SAMPLER_CUBE uniform type to p5.Shader.js
Garima3110 Dec 29, 2023
7981fdc
Merge remote-tracking branch 'upstream/main' into cubeMap
Garima3110 Dec 31, 2023
e7cd392
Minor changes in SAMPLER_CUBE uniform type
Garima3110 Dec 31, 2023
f30af0e
Minor updates on caching shaders
Garima3110 Dec 31, 2023
d51c4a8
Merge branch 'processing:main' into cubeMap
Garima3110 Jan 4, 2024
38bcaab
Merge branch 'processing:main' into cubeMap
Garima3110 Jan 8, 2024
2558755
Merge remote-tracking branch 'upstream/main' into cubeMap
Garima3110 Jan 13, 2024
773198a
Updated RendererGL.js
Garima3110 Jan 13, 2024
bbc7c72
Merge remote-tracking branch 'upstream/main' into cubeMap
Garima3110 Jan 15, 2024
ffd9097
Reordered some code in p5.RendererGL.js
Garima3110 Jan 16, 2024
cc77e7e
Minor fixes
Garima3110 Jan 20, 2024
518b0f0
Merge remote-tracking branch 'upstream/main' into cubeMap
Garima3110 Jan 20, 2024
c124738
Fragment shader error fixed
Garima3110 Jan 23, 2024
38e6e84
Merge remote-tracking branch 'upstream/main' into cubeMap
Garima3110 Jan 23, 2024
a585ef5
Minor updates
Garima3110 Jan 23, 2024
f118a3b
some fixes
Garima3110 Jan 24, 2024
2da03d1
Merge branch 'processing:main' into cubeMap
Garima3110 Jan 29, 2024
5961be8
Minor changes
Garima3110 Mar 30, 2024
4515c09
Some changes with cubemap implementation
Garima3110 Jun 20, 2024
be6d27f
Minor changes
Garima3110 Jun 24, 2024
6dc9916
fix
Garima3110 Jun 24, 2024
3133e80
Merge branch 'processing:main' into cubeMap
Garima3110 Nov 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 141 additions & 61 deletions src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ 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 = {};
const shaderCache = {}; //Shader cache object

let lineDefs = '';
const defineStrokeCapEnum = function (key, val) {
lineDefs += `#define STROKE_CAP_${key} ${val}\n`;
Expand All @@ -32,77 +34,114 @@ defineStrokeJoinEnum('ROUND', 0);
defineStrokeJoinEnum('MITER', 1);
defineStrokeJoinEnum('BEVEL', 2);

const lightingShader = readFileSync(
const getCachedShader= (shaderKey, shaderSource)=>{
Copy link
Contributor

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 on p5.RendererGL, as each instance of a renderer will need its own shaders. We just want the ability to share those shaders within the renderer.

if(!shaderCache[shaderKey]){
shaderCache[shaderKey]=p5.createShader(shaderSource);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shaders also have both a vertex and a fragment shader, not a single shader source, so you'd need to add two parameters to the function and pass both into createShader.

That said, your _getCubemapShader implementation on its own does all the caching we need, so we might not need this in addition to that.

}
return shaderCache[shaderKey];
};

const lightingShader = getCachedShader('lightingShader',readFileSync(
join(__dirname, '/shaders/lighting.glsl'),
'utf-8'
);
const webgl2CompatibilityShader = readFileSync(
));
const webgl2CompatibilityShader = getCachedShader('webgl2CompatibilityShader',readFileSync(
Copy link
Contributor

@davepagurek davepagurek Dec 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to wrap all of these in getCachedShader -- these are not full shaders, just pieces of shader source that will combine with other pieces later to turn into a full shader.

join(__dirname, '/shaders/webgl2Compatibility.glsl'),
'utf-8'
);
));

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
Expand Down Expand Up @@ -1840,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
Expand Down Expand Up @@ -1902,6 +1954,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
});
Expand All @@ -1913,23 +1967,49 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
defaultShaders.imageLightDiffusedFrag
);
}
// Create a shader for cubemap conversion
const cubemapShader = this._pInst.createShader(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just above this, we create and cache a shader so that we only ever need one copy. Can we add that caching here too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have applied caching mechanism to possibly all the shaders. I am not sure whether this aligns with what you were suggesting through this @davepagurek .PLease have a look into the changes I've made.
Thanks.!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't want to wrap all the existing shader sources in a caching function, since those are just the sources for one part of a shader (e.g. just the vertex or fragment source, and often not the full source either, since we need to add a prefix to it.) A caching function would be good for this createShader call (since you're still creating a new shader every time this line is hit, currently.) A few lines up, this.diffusedShader already does caching, but can be refactored to use the same caching system you use here if you're factoring that out into a common pattern.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh actually I see that you've added that in _getCubemapShader. I think rather than making a new shader here, we can just use const cubemapShader = this._getCubemapShader() instead.

defaultShaders.cubemapVertexShader,
defaultShaders.cubemapFragmentShader);

// 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);
Copy link
Contributor

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 use faces[i] = newFramebuffer.get() instead of this._pInst.get(...).

});
}
// Use the diffusedShader for rendering
newFramebuffer.draw(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not the cause of the error you're seeing, but I think we probably no longer need this newFramebuffer.draw(...) block since we are no longer using this framebuffer as the texture. (I think that means we actually need to call newFramebuffer.free() here to avoid a small memory leak?)

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
Expand Down Expand Up @@ -2087,7 +2167,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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, diffusedLight is obtained from getDiffusedTexture, which returns a CubemapTexture object. Since environmentMapDiffusedCubemap in the shader expects a cubemap texture (samplerCube), so I think diffusedLight is being correctly assigned to the environmentMapDiffusedCubemap uniform in the shader, which is of type samplerCube.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh...sorry. I overlooked this, previously we had diffusedLight with sampler2D texture so I got confused. I see you have updated getDiffusedTexture()

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]
Expand Down
8 changes: 7 additions & 1 deletion src/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -548,6 +548,12 @@ p5.Shader = class {
uniform.texture.src._animateGif(this._renderer._pInst);
}
break;
case gl.SAMPLER_CUBE:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this block the same as the one above? If so, we could do:

case gl.SAMPLER_2D:
case gl.SAMPLER_CUBE:
        gl.activeTexture(gl.TEXTURE0 + uniform.samplerIndex);
        uniform.texture =
          data instanceof p5.Texture ? data : this._renderer.getTexture(data);
        // ...

...to handle both cases with the same code

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;
Expand Down
57 changes: 55 additions & 2 deletions src/webgl/p5.Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ p5.Texture = class Texture {
/**
* Checks if the source data for this texture has changed (if it's
* easy to do so) and reuploads the texture if necessary. If it's not
* possible or to expensive to do a calculation to determine wheter or
* possible or too expensive to do a calculation to determine whether or
* not the data has occurred, this method simply re-uploads the texture.
* @method update
*/
Expand Down Expand Up @@ -510,12 +510,65 @@ export function checkWebGLCapabilities({ GL, webglVersion }) {
: gl.getExtension('OES_texture_half_float');
const supportsHalfFloatLinear = supportsHalfFloat &&
gl.getExtension('OES_texture_half_float_linear');
const supportsCubemap = (webglVersion === constants.WEBGL2)
|| (gl.getExtension('OES_texture_cube_map'));
return {
float: supportsFloat,
floatLinear: supportsFloatLinear,
halfFloat: supportsHalfFloat,
halfFloatLinear: supportsHalfFloatLinear
halfFloatLinear: supportsHalfFloatLinear,
cubemap: supportsCubemap
};
}

export class CubemapTexture extends p5.Texture {
constructor(renderer, faces, settings) {
super(renderer, faces, settings);
}

glFilter(_filter) {
const gl = this._renderer.GL;
// TODO: Support other filters if needed
return gl.LINEAR;
}

_getTextureDataFromSource() {
return this.src;
}

init(faces) {
const gl = this._renderer.GL;
this.glTex = gl.createTexture();

this.bindTexture();
for (let faceIndex = 0; faceIndex < faces.length; faceIndex++) {
// Set up each face of the cubemap
gl.texImage2D(
gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex,
0,
this.glFormat,
this.glFormat,
this.glDataType,
faces[faceIndex]
);
}

// Set parameters for the cubemap
gl.texParameteri(gl.TEXTURE_CUBE_MAP
, gl.TEXTURE_MAG_FILTER, this.glMagFilter);
gl.texParameteri(gl.TEXTURE_CUBE_MAP
, gl.TEXTURE_MIN_FILTER, this.glMinFilter);
gl.texParameteri(gl.TEXTURE_CUBE_MAP
, gl.TEXTURE_WRAP_S, this.glWrapS);
gl.texParameteri(gl.TEXTURE_CUBE_MAP
, gl.TEXTURE_WRAP_T, this.glWrapT);

this.unbindTexture();
}

update() {
// Custom update logic, if needed
}
}

export default p5.Texture;
22 changes: 22 additions & 0 deletions src/webgl/shaders/cubeFragment.frag
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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 macros instead resolved the errors , I again tried doing what u have suggested today, but strangely that gives in more errors! So I think we should stick to using these macros only :-)
You can refer to Dave's suggestion here in this comment:
#6665 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might need to use the TEXTURE macro here so that this will be translated to either texture() or texture2d() based on the environment it's being run in.

Copy link
Collaborator

@perminder-17 perminder-17 Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like, texture() works on webgl-2 and texture2D() works on webgl-1. You may use TEXTURE() to run on both the modes.

Copy link
Member Author

@Garima3110 Garima3110 Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had tried that earlier, tried that again today as per your suggestion but both give the same results.
#6665 (comment)
So i think we should go with texture only (not super sure however) :-)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm...what kind of errors? If you want your code to run on both environments I think TEXTURE shouldn't give errors. I don't know why? We can debug it by first writing the code for webgl-1 (changing it to GL ES 100 version I mentioned above, let me know if texture2D works there), similarly change it to version 300 and let me know if texture() works there?

Sorry if it seems a long approach for you, But I seriously don't know why TEXTURE() is giving you errors. Also, can you share me the screenshots of the errors you are getting.


OUT_COLOR = vec4(color, 1.0);
}
Loading