refactor: globalize random textures

This commit is contained in:
FMS-Cat
2021-03-15 12:40:16 +09:00
parent 42a0a52c66
commit 5bb0fd6470
7 changed files with 34 additions and 52 deletions

View File

@@ -12,6 +12,7 @@ import aoFrag from '../shaders/ao.frag';
import quadVert from '../shaders/quad.vert';
import shadingFrag from '../shaders/shading.frag';
import { gl } from '../heck/canvas';
import { randomTexture } from '../utils/RandomTexture';
export interface CameraEntityOptions {
root: Entity;
@@ -19,7 +20,6 @@ export interface CameraEntityOptions {
lights: LightEntity[];
textureIBLLUT: GLCatTexture<WebGL2RenderingContext>;
textureEnv: GLCatTexture<WebGL2RenderingContext>;
textureRandom: GLCatTexture<WebGL2RenderingContext>;
}
export class CameraEntity {
@@ -95,7 +95,7 @@ export class CameraEntity {
);
}
aoMaterial.addUniformTexture( 'samplerRandom', options.textureRandom );
aoMaterial.addUniformTexture( 'samplerRandom', randomTexture.texture );
const aoQuad = new Quad( {
material: aoMaterial,
@@ -189,7 +189,7 @@ export class CameraEntity {
shadingMaterial.addUniformTexture( 'samplerShadow', light.shadowMap.texture );
shadingMaterial.addUniformTexture( 'samplerIBLLUT', options.textureIBLLUT );
shadingMaterial.addUniformTexture( 'samplerEnv', options.textureEnv );
shadingMaterial.addUniformTexture( 'samplerRandom', options.textureRandom );
shadingMaterial.addUniformTexture( 'samplerRandom', randomTexture.texture );
const shadingQuad = new Quad( {
material: shadingMaterial,

View File

@@ -10,11 +10,10 @@ import flickyParticleRenderFrag from '../shaders/flicky-particles-render.frag';
import flickyParticleRenderVert from '../shaders/flicky-particles-render.vert';
import { TRIANGLE_STRIP_QUAD } from '@fms-cat/experimental';
import { gl, glCat } from '../heck/canvas';
import { randomTexture, randomTextureStatic } from '../utils/RandomTexture';
export interface FlickyParticlesOptions {
particlesSqrt: number;
textureRandom: GLCatTexture<WebGL2RenderingContext>;
textureRandomStatic: GLCatTexture<WebGL2RenderingContext>;
}
export class FlickyParticles {
@@ -51,7 +50,7 @@ export class FlickyParticles {
const material = new Material( quadVert, flickyParticleComputeFrag );
material.addUniform( 'particlesSqrt', '1f', particlesSqrt );
material.addUniform( 'particles', '1f', particles );
material.addUniformTexture( 'samplerRandom', options.textureRandom );
material.addUniformTexture( 'samplerRandom', randomTexture.texture );
if ( process.env.DEV ) {
if ( module.hot ) {
@@ -114,7 +113,7 @@ export class FlickyParticles {
flickyParticleRenderFrag,
);
material.addUniform( 'colorVar', '1f', 0.1 );
material.addUniformTexture( 'samplerRandomStatic', options.textureRandomStatic );
material.addUniformTexture( 'samplerRandomStatic', randomTextureStatic.texture );
if ( process.env.DEV ) {
if ( module.hot ) {

View File

@@ -8,6 +8,7 @@ import { Material } from '../heck/Material';
import quadVert from '../shaders/quad.vert';
import raymarcherFrag from '../shaders/raymarcher.frag';
import { Lambda } from '../heck/components/Lambda';
import { randomTexture, randomTextureStatic } from '../utils/RandomTexture';
export class Raymarcher {
private __mesh: Mesh;
@@ -25,10 +26,7 @@ export class Raymarcher {
return this.__entity;
}
public constructor( options: {
textureRandom: GLCatTexture<WebGL2RenderingContext>;
textureRandomStatic: GLCatTexture<WebGL2RenderingContext>;
} ) {
public constructor() {
this.__entity = new Entity();
this.__entity.transform.position = new Vector3( [ 0.0, 0.0, 0.3 ] );
this.__entity.transform.scale = new Vector3( [ 16.0, 9.0, 1.0 ] ).scale( 0.15 );
@@ -38,8 +36,8 @@ export class Raymarcher {
this.__material.addUniform( 'range', '4f', -1.0, -1.0, 1.0, 1.0 );
this.__material.addUniformTexture( 'samplerRandom', options.textureRandom );
this.__material.addUniformTexture( 'samplerRandomStatic', options.textureRandomStatic );
this.__material.addUniformTexture( 'samplerRandom', randomTexture.texture );
this.__material.addUniformTexture( 'samplerRandomStatic', randomTextureStatic.texture );
this.__entity.components.push( new Lambda( {
onDraw: ( event ) => {

View File

@@ -1,4 +1,3 @@
import { GLCatTexture } from '@fms-cat/glcat-ts';
import { Entity } from '../heck/Entity';
import { GPUParticles } from './GPUParticles';
import { Geometry } from '../heck/Geometry';
@@ -10,11 +9,10 @@ import sphereParticleComputeFrag from '../shaders/sphere-particles-compute.frag'
import sphereParticleRenderFrag from '../shaders/sphere-particles-render.frag';
import sphereParticleRenderVert from '../shaders/sphere-particles-render.vert';
import { gl, glCat } from '../heck/canvas';
import { randomTexture, randomTextureStatic } from '../utils/RandomTexture';
export interface SphereParticlesOptions {
particlesSqrt: number;
textureRandom: GLCatTexture<WebGL2RenderingContext>;
textureRandomStatic: GLCatTexture<WebGL2RenderingContext>;
}
export class SphereParticles {
@@ -51,7 +49,7 @@ export class SphereParticles {
const material = new Material( quadVert, sphereParticleComputeFrag );
material.addUniform( 'particlesSqrt', '1f', particlesSqrt );
material.addUniform( 'particles', '1f', particles );
material.addUniformTexture( 'samplerRandom', options.textureRandom );
material.addUniformTexture( 'samplerRandom', randomTexture.texture );
if ( process.env.DEV ) {
if ( module.hot ) {
@@ -117,7 +115,7 @@ export class SphereParticles {
},
);
material.addUniform( 'colorVar', '1f', 0.1 );
material.addUniformTexture( 'samplerRandomStatic', options.textureRandomStatic );
material.addUniformTexture( 'samplerRandomStatic', randomTextureStatic.texture );
if ( process.env.DEV ) {
if ( module.hot ) {

View File

@@ -1,4 +1,3 @@
import { GLCatTexture } from '@fms-cat/glcat-ts';
import { Entity } from '../heck/Entity';
import { GPUParticles } from './GPUParticles';
import { Geometry } from '../heck/Geometry';
@@ -9,12 +8,11 @@ import trailsComputeFrag from '../shaders/trails-compute.frag';
import trailsRenderFrag from '../shaders/trails-render.frag';
import trailsRenderVert from '../shaders/trails-render.vert';
import { gl, glCat } from '../heck/canvas';
import { randomTexture, randomTextureStatic } from '../utils/RandomTexture';
export interface TrailsOptions {
trails: number;
trailLength: number;
textureRandom: GLCatTexture<WebGL2RenderingContext>;
textureRandomStatic: GLCatTexture<WebGL2RenderingContext>;
}
export class Trails {
@@ -48,7 +46,7 @@ export class Trails {
const material = new Material( quadVert, trailsComputeFrag );
material.addUniform( 'trails', '1f', options.trails );
material.addUniform( 'trailLength', '1f', options.trailLength );
material.addUniformTexture( 'samplerRandom', options.textureRandom );
material.addUniformTexture( 'samplerRandom', randomTexture.texture );
if ( process.env.DEV ) {
if ( module.hot ) {
@@ -149,7 +147,7 @@ export class Trails {
trailsRenderVert,
trailsRenderFrag,
);
material.addUniformTexture( 'samplerRandomStatic', options.textureRandomStatic );
material.addUniformTexture( 'samplerRandomStatic', randomTextureStatic.texture );
if ( process.env.DEV ) {
if ( module.hot ) {

View File

@@ -7,8 +7,7 @@ import { canvas, glCat } from './heck/canvas';
import { Dog } from './heck/Dog';
import { CanvasRenderTarget } from './heck/CanvasRenderTarget';
import { Lambda } from './heck/components/Lambda';
import { RandomTexture } from './utils/RandomTexture';
import { RANDOM_RESOLUTION, STATIC_RANDOM_RESOLUTION } from './config';
import { randomTexture } from './utils/RandomTexture';
import { SphereParticles } from './entities/SphereParticles';
import { Swap, Vector3 } from '@fms-cat/experimental';
import { BufferRenderTarget } from './heck/BufferRenderTarget';
@@ -163,21 +162,6 @@ if ( process.env.DEV ) {
);
}
// == textures =====================================================================================
const randomTexture = new RandomTexture(
glCat,
RANDOM_RESOLUTION[ 0 ],
RANDOM_RESOLUTION[ 1 ]
);
randomTexture.update();
const randomTextureStatic = new RandomTexture(
glCat,
STATIC_RANDOM_RESOLUTION[ 0 ],
STATIC_RANDOM_RESOLUTION[ 1 ]
);
randomTextureStatic.update();
// == scene ========================================================================================
const dog = new Dog( music );
@@ -206,16 +190,12 @@ dog.root.children.push( environmentMap.entity );
// -- "objects" ------------------------------------------------------------------------------------
const sphereParticles = new SphereParticles( {
particlesSqrt: 256,
textureRandom: randomTexture.texture,
textureRandomStatic: randomTextureStatic.texture
} );
dog.root.children.push( sphereParticles.entity );
const trails = new Trails( {
trails: 4096,
trailLength: 64,
textureRandom: randomTexture.texture,
textureRandomStatic: randomTextureStatic.texture
} );
dog.root.children.push( trails.entity );
@@ -227,15 +207,10 @@ dog.root.children.push( cube.entity );
const flickyParticles = new FlickyParticles( {
particlesSqrt: 8,
textureRandom: randomTexture.texture,
textureRandomStatic: randomTextureStatic.texture,
} );
dog.root.children.push( flickyParticles.entity );
const raymarcher = new Raymarcher( {
textureRandom: randomTexture.texture,
textureRandomStatic: randomTextureStatic.texture
} );
const raymarcher = new Raymarcher();
dog.root.children.push( raymarcher.entity );
// -- things that is not an "object" ---------------------------------------------------------------
@@ -286,7 +261,6 @@ const camera = new CameraEntity( {
],
textureIBLLUT: ibllut.texture,
textureEnv: environmentMap.texture,
textureRandom: randomTexture.texture
} );
camera.camera.clear = [ 0.0, 0.0, 0.0, 0.0 ];
camera.entity.components.unshift( new Lambda( {

View File

@@ -1,6 +1,7 @@
import { GLCat, GLCatTexture } from '@fms-cat/glcat-ts';
import { Xorshift } from '@fms-cat/experimental';
import { gl } from '../heck/canvas';
import { gl, glCat } from '../heck/canvas';
import { RANDOM_RESOLUTION, STATIC_RANDOM_RESOLUTION } from '../config';
export class RandomTexture {
private __texture: GLCatTexture<WebGL2RenderingContext>;
@@ -52,3 +53,17 @@ export class RandomTexture {
);
}
}
export const randomTexture = new RandomTexture(
glCat,
RANDOM_RESOLUTION[ 0 ],
RANDOM_RESOLUTION[ 1 ]
);
randomTexture.update();
export const randomTextureStatic = new RandomTexture(
glCat,
STATIC_RANDOM_RESOLUTION[ 0 ],
STATIC_RANDOM_RESOLUTION[ 1 ]
);
randomTextureStatic.update();