performance: limit raymarcher range

This commit is contained in:
FMS-Cat
2021-03-25 00:48:57 +09:00
parent 57bb3d2448
commit cb012552b2
9 changed files with 62 additions and 31 deletions

View File

@@ -8,7 +8,6 @@ import cubeFrag from '../shaders/cube.frag';
import depthFrag from '../shaders/depth.frag'; import depthFrag from '../shaders/depth.frag';
import { genCube } from '../geometries/genCube'; import { genCube } from '../geometries/genCube';
import { Lambda } from '../heck/components/Lambda'; import { Lambda } from '../heck/components/Lambda';
import { gl } from '../globals/canvas';
import { quadGeometry } from '../globals/quadGeometry'; import { quadGeometry } from '../globals/quadGeometry';
import { dummyRenderTargetFourDrawBuffers, dummyRenderTargetOneDrawBuffers } from '../globals/dummyRenderTarget'; import { dummyRenderTargetFourDrawBuffers, dummyRenderTargetOneDrawBuffers } from '../globals/dummyRenderTarget';
@@ -41,7 +40,7 @@ export class Cube extends Entity {
geometry.count = cube.count; geometry.count = cube.count;
geometry.mode = cube.mode; geometry.mode = cube.mode;
geometry.indexType = gl.UNSIGNED_SHORT; geometry.indexType = cube.indexType;
// -- materials -------------------------------------------------------------------------------- // -- materials --------------------------------------------------------------------------------
const materials = { const materials = {

View File

@@ -1,38 +1,39 @@
import { Mesh, MeshCull } from '../heck/components/Mesh'; import { Mesh, MeshCull } from '../heck/components/Mesh';
import { TRIANGLE_STRIP_QUAD, Vector3 } from '@fms-cat/experimental'; import { Vector3 } from '@fms-cat/experimental';
import { gl, glCat } from '../globals/canvas';
import { Entity } from '../heck/Entity'; import { Entity } from '../heck/Entity';
import { Geometry } from '../heck/Geometry'; import { Geometry } from '../heck/Geometry';
import { Material } from '../heck/Material'; 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 raymarcherFrag from '../shaders/raymarcher.frag';
import { Lambda } from '../heck/components/Lambda'; import { Lambda } from '../heck/components/Lambda';
import { randomTexture, randomTextureStatic } from '../globals/randomTexture'; import { randomTexture, randomTextureStatic } from '../globals/randomTexture';
import { auto } from '../globals/automaton'; import { auto } from '../globals/automaton';
import { dummyRenderTargetFourDrawBuffers, dummyRenderTargetOneDrawBuffers } from '../globals/dummyRenderTarget'; import { dummyRenderTargetFourDrawBuffers, dummyRenderTargetOneDrawBuffers } from '../globals/dummyRenderTarget';
import { genOctahedron } from '../geometries/genOctahedron';
export class Raymarcher extends Entity { export class Raymarcher extends Entity {
public constructor() { public constructor() {
super(); super();
this.transform.position = new Vector3( [ 0.0, 0.0, 0.3 ] ); this.transform.position = new Vector3( [ 0.0, 0.0, 0.0 ] );
this.transform.scale = new Vector3( [ 16.0, 9.0, 1.0 ] ).scale( 0.15 ); this.transform.scale = new Vector3( [ 1.0, 1.0, 1.0 ] );
// -- geometry --------------------------------------------------------------------------------- // -- geometry ---------------------------------------------------------------------------------
const octahedron = genOctahedron( { radius: 2.0, div: 1 } );
const geometry = new Geometry(); const geometry = new Geometry();
const bufferPos = glCat.createBuffer(); geometry.vao.bindVertexbuffer( octahedron.position, 0, 3 );
bufferPos.setVertexbuffer( new Float32Array( TRIANGLE_STRIP_QUAD ) ); geometry.vao.bindIndexbuffer( octahedron.index );
geometry.vao.bindVertexbuffer( bufferPos, 0, 2 ); geometry.count = octahedron.count;
geometry.mode = octahedron.mode;
geometry.count = 4; geometry.indexType = octahedron.indexType;
geometry.mode = gl.TRIANGLE_STRIP;
// -- materials -------------------------------------------------------------------------------- // -- materials --------------------------------------------------------------------------------
const materials = { const materials = {
deferred: new Material( deferred: new Material(
quadVert, raymarchObjectVert,
raymarcherFrag, raymarcherFrag,
{ {
defines: { 'DEFERRED': 'true' }, defines: { 'DEFERRED': 'true' },
@@ -40,7 +41,7 @@ export class Raymarcher extends Entity {
}, },
), ),
shadow: new Material( shadow: new Material(
quadVert, raymarchObjectVert,
raymarcherFrag, raymarcherFrag,
{ {
defines: { 'SHADOW': 'true' }, defines: { 'SHADOW': 'true' },
@@ -52,8 +53,8 @@ export class Raymarcher extends Entity {
if ( process.env.DEV ) { if ( process.env.DEV ) {
if ( module.hot ) { if ( module.hot ) {
module.hot.accept( '../shaders/raymarcher.frag', () => { module.hot.accept( '../shaders/raymarcher.frag', () => {
materials.deferred.replaceShader( quadVert, raymarcherFrag ); materials.deferred.replaceShader( raymarchObjectVert, raymarcherFrag );
materials.shadow.replaceShader( quadVert, raymarcherFrag ); materials.shadow.replaceShader( raymarchObjectVert, raymarcherFrag );
} ); } );
} }
} }
@@ -77,9 +78,13 @@ export class Raymarcher extends Entity {
); );
material.addUniformVector( material.addUniformVector(
'inversePV', 'inversePVM',
'Matrix4fv', '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' ) ); material.addUniform( 'deformAmp', '1f', auto( 'Music/NEURO_WUB_AMP' ) );

View File

@@ -42,9 +42,9 @@ export class Rings extends Entity {
geometry.vao.bindVertexbuffer( bufferInstanceId, 2, 1, 1 ); geometry.vao.bindVertexbuffer( bufferInstanceId, 2, 1, 1 );
geometry.count = torus.count; geometry.count = torus.count;
geometry.primcount = PRIMCOUNT;
geometry.mode = torus.mode; geometry.mode = torus.mode;
geometry.indexType = gl.UNSIGNED_SHORT; geometry.indexType = torus.indexType;
geometry.primcount = PRIMCOUNT;
// -- materials -------------------------------------------------------------------------------- // -- materials --------------------------------------------------------------------------------
const materials = { const materials = {

View File

@@ -67,8 +67,8 @@ export class SphereParticles extends Entity {
geometryRender.count = octahedron.count; geometryRender.count = octahedron.count;
geometryRender.mode = octahedron.mode; geometryRender.mode = octahedron.mode;
geometryRender.indexType = octahedron.indexType;
geometryRender.primcount = PARTICLES_SQRT * PARTICLES_SQRT; geometryRender.primcount = PARTICLES_SQRT * PARTICLES_SQRT;
geometryRender.indexType = gl.UNSIGNED_SHORT;
// -- material render -------------------------------------------------------------------------- // -- material render --------------------------------------------------------------------------
const deferred = new Material( const deferred = new Material(

View File

@@ -7,6 +7,7 @@ interface ResultGenCube {
index: GLCatBuffer; index: GLCatBuffer;
count: number; count: number;
mode: GLenum; mode: GLenum;
indexType: GLenum;
} }
export function genCube(): ResultGenCube { export function genCube(): ResultGenCube {
@@ -65,6 +66,7 @@ export function genCube(): ResultGenCube {
normal, normal,
index, index,
count: ind.length, count: ind.length,
mode: gl.TRIANGLES mode: gl.TRIANGLES,
indexType: gl.UNSIGNED_SHORT,
}; };
} }

View File

@@ -7,6 +7,7 @@ interface ResultGenOctahedron {
index: GLCatBuffer; index: GLCatBuffer;
count: number; count: number;
mode: GLenum; mode: GLenum;
indexType: GLenum;
} }
export function genOctahedron( options: { export function genOctahedron( options: {
@@ -132,6 +133,7 @@ export function genOctahedron( options: {
normal, normal,
index, index,
count: ind.length, count: ind.length,
mode: gl.TRIANGLES mode: gl.TRIANGLES,
indexType: gl.UNSIGNED_SHORT,
}; };
} }

View File

@@ -7,6 +7,7 @@ interface ResultGenTorus {
index: GLCatBuffer; index: GLCatBuffer;
count: number; count: number;
mode: GLenum; mode: GLenum;
indexType: GLenum;
} }
export function genTorus( options?: { export function genTorus( options?: {
@@ -71,6 +72,7 @@ export function genTorus( options?: {
normal, normal,
index, index,
count: ind.length, count: ind.length,
mode: gl.TRIANGLES mode: gl.TRIANGLES,
indexType: gl.UNSIGNED_SHORT,
}; };
} }

View File

@@ -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;
}

View File

@@ -11,7 +11,7 @@ const int MTL_PBR = 2;
const int MTL_GRADIENT = 3; const int MTL_GRADIENT = 3;
const int MTL_IRIDESCENT = 4; const int MTL_IRIDESCENT = 4;
in vec2 vUv; in vec2 vP;
#ifdef DEFERRED #ifdef DEFERRED
layout (location = 0) out vec4 fragPosition; layout (location = 0) out vec4 fragPosition;
@@ -33,7 +33,7 @@ uniform vec2 cameraNearFar;
uniform vec3 cameraPos; uniform vec3 cameraPos;
uniform mat4 viewMatrix; uniform mat4 viewMatrix;
uniform mat4 projectionMatrix; uniform mat4 projectionMatrix;
uniform mat4 inversePV; uniform mat4 inversePVM;
uniform sampler2D samplerRandom; uniform sampler2D samplerRandom;
uniform sampler2D samplerRandomStatic; uniform sampler2D samplerRandomStatic;
uniform sampler2D samplerCapture; uniform sampler2D samplerCapture;
@@ -87,10 +87,10 @@ vec3 normalFunc( vec3 p, float dd ) {
} }
void main() { void main() {
vec2 p = vUv * 2.0 - 1.0; vec2 p = vP;
p.x *= resolution.x / resolution.y;
vec3 rayOri = divideByW( inversePV * vec4( p, 0.0, 1.0 ) ); vec3 rayOri = divideByW( inversePVM * vec4( p, 0.0, 1.0 ) );
vec3 farPos = divideByW( inversePV * vec4( p, 1.0, 1.0 ) ); vec3 farPos = divideByW( inversePVM * vec4( p, 1.0, 1.0 ) );
vec3 rayDir = normalize( farPos - rayOri ); vec3 rayDir = normalize( farPos - rayOri );
float rayLen = cameraNearFar.x; float rayLen = cameraNearFar.x;
vec3 rayPos = rayOri + rayDir * rayLen; vec3 rayPos = rayOri + rayDir * rayLen;