Skip to content

Commit ecb6bfd

Browse files
committed
Start to convert panorama to strands
1 parent 32ed46b commit ecb6bfd

5 files changed

Lines changed: 63 additions & 20 deletions

File tree

preview/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
let env;
3333

3434
p.setup = async function () {
35-
await p.createCanvas(400, 400, p.WEBGL);
35+
await p.createCanvas(400, 400, p.WEBGPU);
3636
env = await p.loadImage('img/spheremap.jpg');
3737
font = await p.loadFont(
3838
'font/PlayfairDisplay.ttf'
@@ -118,6 +118,7 @@
118118
p.orbitControl();
119119
const t = p.millis() * 0.002;
120120
p.background(200);
121+
p.panorama(env);
121122
p.push();
122123
p.imageLight(env);
123124
p.shader(sh);

src/core/constants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,13 @@ export const IMAGE = 'image';
11481148

11491149
// WEBGL TEXTURE WRAP AND FILTERING
11501150
// LINEAR already exists above
1151+
/**
1152+
* @typedef {'linear_mipmap'} LINEAR_MIPMAP
1153+
* @property {LINEAR_MIPMAP} LINEAR_MIPMAP
1154+
* @final
1155+
* @private
1156+
*/
1157+
export const LINEAR_MIPMAP = 'linear_mipmap';
11511158
/**
11521159
* @typedef {'nearest'} NEAREST
11531160
* @property {NEAREST} NEAREST

src/core/p5.Renderer3D.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,6 +1932,50 @@ export class Renderer3D extends Renderer {
19321932
return tex;
19331933
}
19341934

1935+
_getSphereMapping(img) {
1936+
if (!this.sphereMapping) {
1937+
const p5 = this._pInst;
1938+
this.sphereMapping = this.baseFilterShader().modify(() => {
1939+
const uEnvMap = p5.uniformTexture();
1940+
const uFovY = p5.uniformFloat();
1941+
const uAspect = p5.uniformFloat();
1942+
// Hack: we don't have matrix uniforms yet; use three vectors
1943+
const uN1 = p5.uniformVec3();
1944+
const uN2 = p5.uniformVec3();
1945+
const uN3 = p5.uniformVec3();
1946+
p5.getColor((inputs) => {
1947+
const uFovX = uFovY * uAspect;
1948+
const angleY = p5.mix(uFovY/2.0, -uFovY/2.0, inputs.texCoord.y);
1949+
const angleX = p5.mix(uFovX/2.0, -uFovX/2.0, inputs.texCoord.x);
1950+
let rotatedNormal = p5.normalize([angleX, angleY, 1]);
1951+
rotatedNormal = [
1952+
p5.dot(rotatedNormal, uN1),
1953+
p5.dot(rotatedNormal, uN2),
1954+
p5.dot(rotatedNormal, uN3),
1955+
];
1956+
const temp = rotatedNormal.z;
1957+
rotatedNormal.z = rotatedNormal.x;
1958+
rotatedNormal.x = -temp;
1959+
const suv = [
1960+
0.5 + 0.5 * (-rotatedNormal.y),
1961+
p5.atan(rotatedNormal.z, rotatedNormal.x) / (2.0 * p5.PI) + 0.5
1962+
];
1963+
return p5.getTexture(uEnvMap, suv);
1964+
})
1965+
}, { p5 });
1966+
}
1967+
this.scratchMat3.inverseTranspose4x4(this.states.uViewMatrix);
1968+
this.scratchMat3.invert(this.scratchMat3); // uNMMatrix is 3x3
1969+
this.sphereMapping.setUniform("uFovY", this.states.curCamera.cameraFOV);
1970+
this.sphereMapping.setUniform("uAspect", this.states.curCamera.aspectRatio);
1971+
// this.sphereMapping.setUniform("uNewNormalMatrix", this.scratchMat3.mat3);
1972+
this.sphereMapping.setUniform("uN1", this.scratchMat3.mat3.slice(0, 3));
1973+
this.sphereMapping.setUniform("uN2", this.scratchMat3.mat3.slice(3, 6));
1974+
this.sphereMapping.setUniform("uN3", this.scratchMat3.mat3.slice(6));
1975+
this.sphereMapping.setUniform("uEnvMap", img);
1976+
return this.sphereMapping;
1977+
}
1978+
19351979
/*
19361980
* Abstract methods to be implemented by specific renderers
19371981
*/

src/webgl/p5.RendererGL.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -674,20 +674,6 @@ class RendererGL extends Renderer3D {
674674
* and the shader must be valid in that context.
675675
*/
676676

677-
// TODO move to super class
678-
_getSphereMapping(img) {
679-
if (!this.sphereMapping) {
680-
this.sphereMapping = this._pInst.createFilterShader(sphereMapping);
681-
}
682-
this.scratchMat3.inverseTranspose4x4(this.states.uViewMatrix);
683-
this.scratchMat3.invert(this.scratchMat3); // uNMMatrix is 3x3
684-
this.sphereMapping.setUniform("uFovY", this.states.curCamera.cameraFOV);
685-
this.sphereMapping.setUniform("uAspect", this.states.curCamera.aspectRatio);
686-
this.sphereMapping.setUniform("uNewNormalMatrix", this.scratchMat3.mat3);
687-
this.sphereMapping.setUniform("uEnvMap", img);
688-
return this.sphereMapping;
689-
}
690-
691677
baseMaterialShader() {
692678
if (!this._pInst._glAttributes.perPixelLighting) {
693679
throw new Error(
@@ -932,19 +918,23 @@ class RendererGL extends Renderer3D {
932918
* Create final MipmapTexture from collected ImageData for WebGL
933919
*/
934920
_finalizeMipmapTexture(mipmapData) {
935-
return new MipmapTexture(this, mipmapData.levels, {});
921+
return new MipmapTexture(this, mipmapData.levels, {
922+
minFilter: constants.LINEAR_MIPMAP,
923+
magFilter: constants.LINEAR,
924+
});
936925
}
937926

938927
createMipmapTextureHandle({ levels, format, dataType, width, height }) {
939928
const gl = this.GL;
940929
const texture = gl.createTexture();
941930

942931
gl.bindTexture(gl.TEXTURE_2D, texture);
943-
932+
944933
// Determine GL format and data type
945934
const glFormat = gl.RGBA;
946935
const glDataType = gl.UNSIGNED_BYTE;
947-
936+
937+
console.log(levels)
948938
for (let level = 0; level < levels.length; level++) {
949939
gl.texImage2D(
950940
gl.TEXTURE_2D,
@@ -961,7 +951,7 @@ class RendererGL extends Renderer3D {
961951
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
962952

963953
gl.bindTexture(gl.TEXTURE_2D, null);
964-
954+
965955
return { texture, glFormat, glDataType };
966956
}
967957

src/webgl/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ export function readPixelWebGL(gl, framebuffer, x, y, format, type, flipY) {
104104
export function setWebGLTextureParams(texture, gl, webglVersion) {
105105
texture.bindTexture();
106106
const glMinFilter =
107-
texture.minFilter === constants.NEAREST ? gl.NEAREST : gl.LINEAR;
107+
texture.minFilter === constants.NEAREST ? gl.NEAREST :
108+
texture.minFilter === constants.LINEAR_MIPMAP ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR;
108109
const glMagFilter =
109110
texture.magFilter === constants.NEAREST ? gl.NEAREST : gl.LINEAR;
110111

0 commit comments

Comments
 (0)