diff --git a/src/entities/Cube.ts b/src/entities/Cube.ts index ca9948a..d8ccd4b 100644 --- a/src/entities/Cube.ts +++ b/src/entities/Cube.ts @@ -8,7 +8,6 @@ import cubeFrag from '../shaders/cube.frag'; import depthFrag from '../shaders/depth.frag'; import { genCube } from '../geometries/genCube'; import { Lambda } from '../heck/components/Lambda'; -import { gl } from '../globals/canvas'; import { quadGeometry } from '../globals/quadGeometry'; import { dummyRenderTargetFourDrawBuffers, dummyRenderTargetOneDrawBuffers } from '../globals/dummyRenderTarget'; @@ -41,7 +40,7 @@ export class Cube extends Entity { geometry.count = cube.count; geometry.mode = cube.mode; - geometry.indexType = gl.UNSIGNED_SHORT; + geometry.indexType = cube.indexType; // -- materials -------------------------------------------------------------------------------- const materials = { diff --git a/src/entities/Raymarcher.ts b/src/entities/Raymarcher.ts index 95bdbce..3861d6b 100644 --- a/src/entities/Raymarcher.ts +++ b/src/entities/Raymarcher.ts @@ -1,38 +1,39 @@ import { Mesh, MeshCull } from '../heck/components/Mesh'; -import { TRIANGLE_STRIP_QUAD, Vector3 } from '@fms-cat/experimental'; -import { gl, glCat } from '../globals/canvas'; +import { Vector3 } from '@fms-cat/experimental'; import { Entity } from '../heck/Entity'; import { Geometry } from '../heck/Geometry'; import { Material } from '../heck/Material'; -import quadVert from '../shaders/quad.vert'; +import raymarchObjectVert from '../shaders/raymarch-object.vert'; import raymarcherFrag from '../shaders/raymarcher.frag'; import { Lambda } from '../heck/components/Lambda'; import { randomTexture, randomTextureStatic } from '../globals/randomTexture'; import { auto } from '../globals/automaton'; import { dummyRenderTargetFourDrawBuffers, dummyRenderTargetOneDrawBuffers } from '../globals/dummyRenderTarget'; +import { genOctahedron } from '../geometries/genOctahedron'; export class Raymarcher extends Entity { public constructor() { super(); - this.transform.position = new Vector3( [ 0.0, 0.0, 0.3 ] ); - this.transform.scale = new Vector3( [ 16.0, 9.0, 1.0 ] ).scale( 0.15 ); + this.transform.position = new Vector3( [ 0.0, 0.0, 0.0 ] ); + this.transform.scale = new Vector3( [ 1.0, 1.0, 1.0 ] ); // -- geometry --------------------------------------------------------------------------------- + const octahedron = genOctahedron( { radius: 2.0, div: 1 } ); + const geometry = new Geometry(); - const bufferPos = glCat.createBuffer(); - bufferPos.setVertexbuffer( new Float32Array( TRIANGLE_STRIP_QUAD ) ); + geometry.vao.bindVertexbuffer( octahedron.position, 0, 3 ); + geometry.vao.bindIndexbuffer( octahedron.index ); - geometry.vao.bindVertexbuffer( bufferPos, 0, 2 ); - - geometry.count = 4; - geometry.mode = gl.TRIANGLE_STRIP; + geometry.count = octahedron.count; + geometry.mode = octahedron.mode; + geometry.indexType = octahedron.indexType; // -- materials -------------------------------------------------------------------------------- const materials = { deferred: new Material( - quadVert, + raymarchObjectVert, raymarcherFrag, { defines: { 'DEFERRED': 'true' }, @@ -40,7 +41,7 @@ export class Raymarcher extends Entity { }, ), shadow: new Material( - quadVert, + raymarchObjectVert, raymarcherFrag, { defines: { 'SHADOW': 'true' }, @@ -52,8 +53,8 @@ export class Raymarcher extends Entity { if ( process.env.DEV ) { if ( module.hot ) { module.hot.accept( '../shaders/raymarcher.frag', () => { - materials.deferred.replaceShader( quadVert, raymarcherFrag ); - materials.shadow.replaceShader( quadVert, raymarcherFrag ); + materials.deferred.replaceShader( raymarchObjectVert, raymarcherFrag ); + materials.shadow.replaceShader( raymarchObjectVert, raymarcherFrag ); } ); } } @@ -77,9 +78,13 @@ export class Raymarcher extends Entity { ); material.addUniformVector( - 'inversePV', + 'inversePVM', 'Matrix4fv', - event.projectionMatrix.multiply( event.viewMatrix ).inverse!.elements + event.projectionMatrix + .multiply( event.viewMatrix ) + .multiply( this.transform.matrix ) + .inverse! + .elements ); material.addUniform( 'deformAmp', '1f', auto( 'Music/NEURO_WUB_AMP' ) ); diff --git a/src/entities/Rings.ts b/src/entities/Rings.ts index 56876be..c65708f 100644 --- a/src/entities/Rings.ts +++ b/src/entities/Rings.ts @@ -42,9 +42,9 @@ export class Rings extends Entity { geometry.vao.bindVertexbuffer( bufferInstanceId, 2, 1, 1 ); geometry.count = torus.count; - geometry.primcount = PRIMCOUNT; geometry.mode = torus.mode; - geometry.indexType = gl.UNSIGNED_SHORT; + geometry.indexType = torus.indexType; + geometry.primcount = PRIMCOUNT; // -- materials -------------------------------------------------------------------------------- const materials = { diff --git a/src/entities/SphereParticles.ts b/src/entities/SphereParticles.ts index accf755..3b797b3 100644 --- a/src/entities/SphereParticles.ts +++ b/src/entities/SphereParticles.ts @@ -67,8 +67,8 @@ export class SphereParticles extends Entity { geometryRender.count = octahedron.count; geometryRender.mode = octahedron.mode; + geometryRender.indexType = octahedron.indexType; geometryRender.primcount = PARTICLES_SQRT * PARTICLES_SQRT; - geometryRender.indexType = gl.UNSIGNED_SHORT; // -- material render -------------------------------------------------------------------------- const deferred = new Material( diff --git a/src/geometries/genCube.ts b/src/geometries/genCube.ts index 8e0817a..1aea559 100644 --- a/src/geometries/genCube.ts +++ b/src/geometries/genCube.ts @@ -7,6 +7,7 @@ interface ResultGenCube { index: GLCatBuffer; count: number; mode: GLenum; + indexType: GLenum; } export function genCube(): ResultGenCube { @@ -65,6 +66,7 @@ export function genCube(): ResultGenCube { normal, index, count: ind.length, - mode: gl.TRIANGLES + mode: gl.TRIANGLES, + indexType: gl.UNSIGNED_SHORT, }; } diff --git a/src/geometries/genOctahedron.ts b/src/geometries/genOctahedron.ts index c3ed74d..b573ca7 100644 --- a/src/geometries/genOctahedron.ts +++ b/src/geometries/genOctahedron.ts @@ -7,6 +7,7 @@ interface ResultGenOctahedron { index: GLCatBuffer; count: number; mode: GLenum; + indexType: GLenum; } export function genOctahedron( options: { @@ -132,6 +133,7 @@ export function genOctahedron( options: { normal, index, count: ind.length, - mode: gl.TRIANGLES + mode: gl.TRIANGLES, + indexType: gl.UNSIGNED_SHORT, }; } diff --git a/src/geometries/genTorus.ts b/src/geometries/genTorus.ts index 4bbe63a..ed0d78c 100644 --- a/src/geometries/genTorus.ts +++ b/src/geometries/genTorus.ts @@ -7,6 +7,7 @@ interface ResultGenTorus { index: GLCatBuffer; count: number; mode: GLenum; + indexType: GLenum; } export function genTorus( options?: { @@ -71,6 +72,7 @@ export function genTorus( options?: { normal, index, count: ind.length, - mode: gl.TRIANGLES + mode: gl.TRIANGLES, + indexType: gl.UNSIGNED_SHORT, }; } diff --git a/src/shaders/raymarch-object.vert b/src/shaders/raymarch-object.vert new file mode 100644 index 0000000..53b8270 --- /dev/null +++ b/src/shaders/raymarch-object.vert @@ -0,0 +1,21 @@ +#version 300 es + +layout (location = 0) in vec3 position; + +out vec2 vP; + +uniform vec2 resolution; +uniform mat4 projectionMatrix; +uniform mat4 viewMatrix; +uniform mat4 modelMatrix; + +// ------ + +void main() { + vec4 outPos = projectionMatrix * viewMatrix * modelMatrix * vec4( position, 1.0 ); + + vP = outPos.xy / outPos.w; + + outPos.x *= resolution.y / resolution.x; + gl_Position = outPos; +} diff --git a/src/shaders/raymarcher.frag b/src/shaders/raymarcher.frag index 7c3201d..e89c78a 100644 --- a/src/shaders/raymarcher.frag +++ b/src/shaders/raymarcher.frag @@ -11,7 +11,7 @@ const int MTL_PBR = 2; const int MTL_GRADIENT = 3; const int MTL_IRIDESCENT = 4; -in vec2 vUv; +in vec2 vP; #ifdef DEFERRED layout (location = 0) out vec4 fragPosition; @@ -33,7 +33,7 @@ uniform vec2 cameraNearFar; uniform vec3 cameraPos; uniform mat4 viewMatrix; uniform mat4 projectionMatrix; -uniform mat4 inversePV; +uniform mat4 inversePVM; uniform sampler2D samplerRandom; uniform sampler2D samplerRandomStatic; uniform sampler2D samplerCapture; @@ -87,10 +87,10 @@ vec3 normalFunc( vec3 p, float dd ) { } void main() { - vec2 p = vUv * 2.0 - 1.0; - p.x *= resolution.x / resolution.y; - vec3 rayOri = divideByW( inversePV * vec4( p, 0.0, 1.0 ) ); - vec3 farPos = divideByW( inversePV * vec4( p, 1.0, 1.0 ) ); + vec2 p = vP; + + vec3 rayOri = divideByW( inversePVM * vec4( p, 0.0, 1.0 ) ); + vec3 farPos = divideByW( inversePVM * vec4( p, 1.0, 1.0 ) ); vec3 rayDir = normalize( farPos - rayOri ); float rayLen = cameraNearFar.x; vec3 rayPos = rayOri + rayDir * rayLen;