Skip to content

Commit

Permalink
feat: manual cubemap mipmap loading example
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jul 16, 2020
1 parent 212d8d5 commit 3e11038
Show file tree
Hide file tree
Showing 58 changed files with 111 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/cube/angusMipmaps/cube_m03_c00.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/cube/angusMipmaps/cube_m03_c01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/cube/angusMipmaps/cube_m03_c02.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/cube/angusMipmaps/cube_m03_c03.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/cube/angusMipmaps/cube_m03_c04.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/cube/angusMipmaps/cube_m03_c05.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/cube/angusMipmaps/cube_m04_c00.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/cube/angusMipmaps/cube_m04_c01.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m04_c02.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m04_c03.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m04_c04.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m04_c05.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m05_c00.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m05_c01.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m05_c02.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m05_c03.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m05_c04.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m05_c05.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m06_c00.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m06_c01.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m06_c02.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m06_c03.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m06_c04.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m06_c05.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m07_c00.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m07_c01.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m07_c02.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m07_c03.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m07_c04.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m07_c05.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m08_c00.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m08_c01.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m08_c02.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m08_c03.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m08_c04.jpg
Binary file added assets/textures/cube/angusMipmaps/cube_m08_c05.jpg
8 changes: 8 additions & 0 deletions src/examples/cubemaps/mipmaps/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"slug": "cubemap_mipmaps",
"en": {
"name": "Cube Map Manual Mipmap Textures",
"description": "Load manually generated mipmaps into the cubemap.",
"keywords": ["texture", "cubemap", "mipmaps"]
}
}
17 changes: 17 additions & 0 deletions src/examples/cubemaps/mipmaps/fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
precision highp float;

varying vec3 v_viewPosition;
varying vec3 v_viewNormal;
varying vec3 v_localNormal;

uniform samplerCube cubeMap;
uniform float perceptualRoughness;
uniform int mipCount;

void main() {

vec3 reflectDir = v_localNormal; //reflect( normalize( v_viewPosition ),normalize(v_viewNormal) );
float lod = clamp(perceptualRoughness * float(mipCount), 0., float(mipCount));
gl_FragColor = textureCubeLodEXT(cubeMap, reflectDir, lod);

}
67 changes: 67 additions & 0 deletions src/examples/cubemaps/mipmaps/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { icosahedronGeometry } from "../../../lib/geometry/primitives/polyhedronGeometry";
import { ShaderMaterial } from "../../../lib/materials/ShaderMaterial";
import { Euler } from "../../../lib/math/Euler";
import { Matrix4 } from "../../../lib/math/Matrix4";
import {
makeMatrix4PerspectiveFov,
makeMatrix4RotationFromEuler,
makeMatrix4Translation,
} from "../../../lib/math/Matrix4.Functions";
import { Vector3 } from "../../../lib/math/Vector3";
import { makeBufferGeometryFromGeometry } from "../../../lib/renderers/webgl/buffers/BufferGeometry";
import { DepthTestFunc, DepthTestState } from "../../../lib/renderers/webgl/DepthTestState";
import { makeProgramFromShaderMaterial } from "../../../lib/renderers/webgl/programs/Program";
import { RenderingContext } from "../../../lib/renderers/webgl/RenderingContext";
import { makeTexImage2DFromCubeTexture } from "../../../lib/renderers/webgl/textures/TexImage2D";
import { CubeMapTexture } from "../../../lib/textures/CubeTexture";
import { fetchImage } from "../../../lib/textures/loaders/Image";
import fragmentSourceCode from "./fragment.glsl";
import vertexSourceCode from "./vertex.glsl";

async function init(): Promise<null> {
const geometry = icosahedronGeometry(0.75, 4);
const material = new ShaderMaterial(vertexSourceCode, fragmentSourceCode);
const images = [];
for (let level = 0; level < 9; level++) {
for (let face = 0; face < 6; face++) {
images.push(fetchImage(`/assets/textures/cube/angusMipmaps/cube_m0${level}_c0${face}.jpg`));
}
}
const cubeTexture = new CubeMapTexture(await Promise.all(images));
cubeTexture.generateMipmaps = false;

const context = new RenderingContext(document.getElementById("framebuffer") as HTMLCanvasElement);
const canvasFramebuffer = context.canvasFramebuffer;
window.addEventListener("resize", () => canvasFramebuffer.resize());

const program = makeProgramFromShaderMaterial(context, material);
const uniforms = {
localToWorld: new Matrix4(),
worldToView: makeMatrix4Translation(new Vector3(0, 0, -3.0)),
viewToScreen: makeMatrix4PerspectiveFov(25, 0.1, 4.0, 1.0, canvasFramebuffer.aspectRatio),
cubeMap: makeTexImage2DFromCubeTexture(context, cubeTexture),
perceptualRoughness: 0,
mipCount: cubeTexture.mipCount,
};
const bufferGeometry = makeBufferGeometryFromGeometry(context, geometry);
const depthTestState = new DepthTestState(true, DepthTestFunc.Less);

function animate(): void {
requestAnimationFrame(animate);
const now = Date.now();

uniforms.localToWorld = makeMatrix4RotationFromEuler(
new Euler(now * 0.0001, now * 0.00033, now * 0.000077),
uniforms.localToWorld,
);
uniforms.perceptualRoughness = Math.sin(now * 0.005) * 0.5 + 0.5;

canvasFramebuffer.renderBufferGeometry(program, uniforms, bufferGeometry, depthTestState);
}

animate();

return null;
}

init();
19 changes: 19 additions & 0 deletions src/examples/cubemaps/mipmaps/vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
attribute vec3 position;
attribute vec3 normal;

uniform mat4 localToWorld;
uniform mat4 worldToView;
uniform mat4 viewToScreen;

varying vec3 v_viewPosition;
varying vec3 v_viewNormal;
varying vec3 v_localNormal;

void main() {

v_localNormal = normalize( position );
v_viewNormal = normalize( ( worldToView * localToWorld * vec4( normalize( position ), 0. ) ).xyz );
v_viewPosition = ( worldToView * localToWorld * vec4( position, 1. ) ).xyz;
gl_Position = viewToScreen * vec4( v_viewPosition, 1. );

}

0 comments on commit 3e11038

Please sign in to comment.