Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StratifiedTexture: Add a stable noise support #648

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions example/skinnedMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const params = {
autoPause: true,
pause: false,
continuous: false,
stableNoise: false,
...getScaledSettings(),

};
Expand Down Expand Up @@ -155,6 +156,11 @@ async function init() {

params.autoPause = false;

} );
gui.add( params, 'stableNoise' ).onChange( v => {

pathTracer.stableNoise = v;

} );

animate();
Expand Down
2 changes: 2 additions & 0 deletions src/core/PathTracingRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,11 @@ export class PathTracingRenderer {
this.samples = 0;
this._task = null;

this.material.stratifiedTexture.stableNoise = this.stableNoise;
if ( this.stableNoise ) {

this.material.seed = 0;
this.material.stratifiedTexture.reset();

}

Expand Down
12 changes: 12 additions & 0 deletions src/core/WebGLPathTracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ export class WebGLPathTracer {

}

get stableNoise() {

return this._pathTracer.stableNoise;

}

set stableNoise( v ) {

this._pathTracer.stableNoise = v;

}

constructor( renderer ) {

// members
Expand Down
60 changes: 57 additions & 3 deletions src/uniforms/StratifiedSamplesTexture.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import { DataTexture, FloatType, NearestFilter, RGBAFormat } from 'three';
import { StratifiedSamplerCombined } from './stratified/StratifiedSamplerCombined.js';

// https://stackoverflow.com/questions/424292/seedable-javascript-random-number-generator
class RandomGenerator {

constructor( seed = 0 ) {

// LCG using GCC's constants
this.m = 0x80000000; // 2**31;
this.a = 1103515245;
this.c = 12345;

this.seed = seed;

}

nextInt() {

this.seed = ( this.a * this.seed + this.c ) % this.m;
return this.seed;

}

nextFloat() {

// returns in range [0,1]
return this.nextInt() / ( this.m - 1 );

}

}

export class StratifiedSamplesTexture extends DataTexture {

constructor( count = 1, depth = 1, strata = 8 ) {
Expand All @@ -11,22 +41,37 @@ export class StratifiedSamplesTexture extends DataTexture {

this.strata = strata;
this.sampler = null;
this.generator = new RandomGenerator();
this.stableNoise = false;
this.random = () => {

if ( this.stableNoise ) {

return this.generator.nextFloat();

} else {

return Math.random();

}

};

this.init( count, depth, strata );

}

init( count, depth, strata = this.strata ) {
init( count = this.image.height, depth = this.image.width, strata = this.strata ) {

const { image } = this;
if ( image.width === depth && image.height === count ) {
if ( image.width === depth && image.height === count && this.sampler !== null ) {

return;

}

const dimensions = new Array( count * depth ).fill( 4 );
const sampler = new StratifiedSamplerCombined( strata, dimensions );
const sampler = new StratifiedSamplerCombined( strata, dimensions, this.random );

image.width = depth;
image.height = count;
Expand All @@ -46,4 +91,13 @@ export class StratifiedSamplesTexture extends DataTexture {

}

reset() {

this.sampler = null;
this.init();
this.sampler.restart();
this.generator.seed = 0;

}

}
10 changes: 5 additions & 5 deletions src/uniforms/stratified/StratifiedSampler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// - https://github.com/hoverinc/ray-tracing-renderer
// - http://www.pbr-book.org/3ed-2018/Sampling_and_Reconstruction/Stratified_Sampling.html

export function shuffle( arr ) {
export function shuffle( arr, random = Math.random() ) {

for ( let i = arr.length - 1; i > 0; i -- ) {

const j = Math.floor( Math.random() * ( i + 1 ) );
const j = Math.floor( random() * ( i + 1 ) );
const x = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = x;
Expand All @@ -21,7 +21,7 @@ export function shuffle( arr ) {
// dimensions : The number of dimensions to generate stratified values for
export class StratifiedSampler {

constructor( strataCount, dimensions ) {
constructor( strataCount, dimensions, random = Math.random ) {

const l = strataCount ** dimensions;
const strata = new Uint16Array( l );
Expand Down Expand Up @@ -50,7 +50,7 @@ export class StratifiedSampler {

if ( index >= strata.length ) {

shuffle( strata );
shuffle( strata, random );
this.restart();

}
Expand All @@ -59,7 +59,7 @@ export class StratifiedSampler {

for ( let i = 0; i < dimensions; i ++ ) {

samples[ i ] = ( stratum % strataCount + Math.random() ) / strataCount;
samples[ i ] = ( stratum % strataCount + random() ) / strataCount;
stratum = Math.floor( stratum / strataCount );

}
Expand Down
4 changes: 2 additions & 2 deletions src/uniforms/stratified/StratifiedSamplerCombined.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { StratifiedSampler } from './StratifiedSampler.js';
// Stratified set of data with each tuple stratified separately and combined
export class StratifiedSamplerCombined {

constructor( strataCount, listOfDimensions ) {
constructor( strataCount, listOfDimensions, random = Math.random ) {

let totalDim = 0;
for ( const dim of listOfDimensions ) {
Expand All @@ -21,7 +21,7 @@ export class StratifiedSamplerCombined {
let offset = 0;
for ( const dim of listOfDimensions ) {

const sampler = new StratifiedSampler( strataCount, dim );
const sampler = new StratifiedSampler( strataCount, dim, random );
sampler.samples = new Float32Array( combined.buffer, offset, sampler.samples.length );
offset += sampler.samples.length * 4;
strataObjs.push( sampler );
Expand Down
Loading