Skip to content
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
236 changes: 117 additions & 119 deletions examples/js/cameras/CinematicCamera.js
Original file line number Diff line number Diff line change
@@ -1,163 +1,161 @@
( function () {

var CinematicCamera = function ( fov, aspect, near, far ) {

THREE.PerspectiveCamera.call( this, fov, aspect, near, far );
this.type = 'CinematicCamera';
this.postprocessing = {
enabled: true
};
this.shaderSettings = {
rings: 3,
samples: 4
};
var depthShader = THREE.BokehDepthShader;
this.materialDepth = new THREE.ShaderMaterial( {
uniforms: depthShader.uniforms,
vertexShader: depthShader.vertexShader,
fragmentShader: depthShader.fragmentShader
} );
this.materialDepth.uniforms[ 'mNear' ].value = near;
this.materialDepth.uniforms[ 'mFar' ].value = far; // In case of cinematicCamera, having a default lens set is important

this.setLens();
this.initPostProcessing();

};

CinematicCamera.prototype = Object.create( THREE.PerspectiveCamera.prototype );
CinematicCamera.prototype.constructor = CinematicCamera; // providing fnumber and coc(Circle of Confusion) as extra arguments

CinematicCamera.prototype.setLens = function ( focalLength, filmGauge, fNumber, coc ) {
class CinematicCamera extends THREE.PerspectiveCamera {

constructor( fov, aspect, near, far ) {

super( fov, aspect, near, far );
this.type = 'CinematicCamera';
this.postprocessing = {
enabled: true
};
this.shaderSettings = {
rings: 3,
samples: 4
};
const depthShader = THREE.BokehDepthShader;
this.materialDepth = new THREE.ShaderMaterial( {
uniforms: depthShader.uniforms,
vertexShader: depthShader.vertexShader,
fragmentShader: depthShader.fragmentShader
} );
this.materialDepth.uniforms[ 'mNear' ].value = near;
this.materialDepth.uniforms[ 'mFar' ].value = far; // In case of cinematicCamera, having a default lens set is important

this.setLens();
this.initPostProcessing();

} // providing fnumber and coc(Circle of Confusion) as extra arguments
// In case of cinematicCamera, having a default lens set is important
if ( focalLength === undefined ) focalLength = 35;
if ( filmGauge !== undefined ) this.filmGauge = filmGauge;
this.setFocalLength( focalLength ); // if fnumber and coc are not provided, cinematicCamera tries to act as a basic THREE.PerspectiveCamera
// if fnumber and coc are not provided, cinematicCamera tries to act as a basic THREE.PerspectiveCamera

if ( fNumber === undefined ) fNumber = 8;
if ( coc === undefined ) coc = 0.019;
this.fNumber = fNumber;
this.coc = coc; // fNumber is focalLength by aperture

this.aperture = focalLength / this.fNumber; // hyperFocal is required to calculate depthOfField when a lens tries to focus at a distance with given fNumber and focalLength
setLens( focalLength = 35, filmGauge = 35, fNumber = 8, coc = 0.019 ) {

this.hyperFocal = focalLength * focalLength / ( this.aperture * this.coc );
this.filmGauge = filmGauge;
this.setFocalLength( focalLength );
this.fNumber = fNumber;
this.coc = coc; // fNumber is focalLength by aperture

};
this.aperture = focalLength / this.fNumber; // hyperFocal is required to calculate depthOfField when a lens tries to focus at a distance with given fNumber and focalLength

CinematicCamera.prototype.linearize = function ( depth ) {
this.hyperFocal = focalLength * focalLength / ( this.aperture * this.coc );

var zfar = this.far;
var znear = this.near;
return - zfar * znear / ( depth * ( zfar - znear ) - zfar );
}

};
linearize( depth ) {

CinematicCamera.prototype.smoothstep = function ( near, far, depth ) {
const zfar = this.far;
const znear = this.near;
return - zfar * znear / ( depth * ( zfar - znear ) - zfar );

var x = this.saturate( ( depth - near ) / ( far - near ) );
return x * x * ( 3 - 2 * x );
}

};
smoothstep( near, far, depth ) {

CinematicCamera.prototype.saturate = function ( x ) {
const x = this.saturate( ( depth - near ) / ( far - near ) );
return x * x * ( 3 - 2 * x );

return Math.max( 0, Math.min( 1, x ) );
}

}; // function for focusing at a distance from the camera
saturate( x ) {

return Math.max( 0, Math.min( 1, x ) );

CinematicCamera.prototype.focusAt = function ( focusDistance ) {
} // function for focusing at a distance from the camera

if ( focusDistance === undefined ) focusDistance = 20;
var focalLength = this.getFocalLength(); // distance from the camera (normal to frustrum) to focus on

this.focus = focusDistance; // the nearest point from the camera which is in focus (unused)
focusAt( focusDistance = 20 ) {

this.nearPoint = this.hyperFocal * this.focus / ( this.hyperFocal + ( this.focus - focalLength ) ); // the farthest point from the camera which is in focus (unused)
const focalLength = this.getFocalLength(); // distance from the camera (normal to frustrum) to focus on

this.farPoint = this.hyperFocal * this.focus / ( this.hyperFocal - ( this.focus - focalLength ) ); // the gap or width of the space in which is everything is in focus (unused)
this.focus = focusDistance; // the nearest point from the camera which is in focus (unused)

this.depthOfField = this.farPoint - this.nearPoint; // Considering minimum distance of focus for a standard lens (unused)
this.nearPoint = this.hyperFocal * this.focus / ( this.hyperFocal + ( this.focus - focalLength ) ); // the farthest point from the camera which is in focus (unused)

if ( this.depthOfField < 0 ) this.depthOfField = 0;
this.sdistance = this.smoothstep( this.near, this.far, this.focus );
this.ldistance = this.linearize( 1 - this.sdistance );
this.postprocessing.bokeh_uniforms[ 'focalDepth' ].value = this.ldistance;
this.farPoint = this.hyperFocal * this.focus / ( this.hyperFocal - ( this.focus - focalLength ) ); // the gap or width of the space in which is everything is in focus (unused)

};
this.depthOfField = this.farPoint - this.nearPoint; // Considering minimum distance of focus for a standard lens (unused)

CinematicCamera.prototype.initPostProcessing = function () {
if ( this.depthOfField < 0 ) this.depthOfField = 0;
this.sdistance = this.smoothstep( this.near, this.far, this.focus );
this.ldistance = this.linearize( 1 - this.sdistance );
this.postprocessing.bokeh_uniforms[ 'focalDepth' ].value = this.ldistance;

if ( this.postprocessing.enabled ) {
}

this.postprocessing.scene = new THREE.Scene();
this.postprocessing.camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10000, 10000 );
this.postprocessing.scene.add( this.postprocessing.camera );
var pars = {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBFormat
};
this.postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
this.postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
var bokeh_shader = THREE.BokehShader;
this.postprocessing.bokeh_uniforms = THREE.UniformsUtils.clone( bokeh_shader.uniforms );
this.postprocessing.bokeh_uniforms[ 'tColor' ].value = this.postprocessing.rtTextureColor.texture;
this.postprocessing.bokeh_uniforms[ 'tDepth' ].value = this.postprocessing.rtTextureDepth.texture;
this.postprocessing.bokeh_uniforms[ 'manualdof' ].value = 0;
this.postprocessing.bokeh_uniforms[ 'shaderFocus' ].value = 0;
this.postprocessing.bokeh_uniforms[ 'fstop' ].value = 2.8;
this.postprocessing.bokeh_uniforms[ 'showFocus' ].value = 1;
this.postprocessing.bokeh_uniforms[ 'focalDepth' ].value = 0.1; //console.log( this.postprocessing.bokeh_uniforms[ "focalDepth" ].value );

this.postprocessing.bokeh_uniforms[ 'znear' ].value = this.near;
this.postprocessing.bokeh_uniforms[ 'zfar' ].value = this.near;
this.postprocessing.bokeh_uniforms[ 'textureWidth' ].value = window.innerWidth;
this.postprocessing.bokeh_uniforms[ 'textureHeight' ].value = window.innerHeight;
this.postprocessing.materialBokeh = new THREE.ShaderMaterial( {
uniforms: this.postprocessing.bokeh_uniforms,
vertexShader: bokeh_shader.vertexShader,
fragmentShader: bokeh_shader.fragmentShader,
defines: {
RINGS: this.shaderSettings.rings,
SAMPLES: this.shaderSettings.samples,
DEPTH_PACKING: 1
}
} );
this.postprocessing.quad = new THREE.Mesh( new THREE.PlaneGeometry( window.innerWidth, window.innerHeight ), this.postprocessing.materialBokeh );
this.postprocessing.quad.position.z = - 500;
this.postprocessing.scene.add( this.postprocessing.quad );
initPostProcessing() {

if ( this.postprocessing.enabled ) {

this.postprocessing.scene = new THREE.Scene();
this.postprocessing.camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10000, 10000 );
this.postprocessing.scene.add( this.postprocessing.camera );
const pars = {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBFormat
};
this.postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
this.postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
const bokeh_shader = THREE.BokehShader;
this.postprocessing.bokeh_uniforms = THREE.UniformsUtils.clone( bokeh_shader.uniforms );
this.postprocessing.bokeh_uniforms[ 'tColor' ].value = this.postprocessing.rtTextureColor.texture;
this.postprocessing.bokeh_uniforms[ 'tDepth' ].value = this.postprocessing.rtTextureDepth.texture;
this.postprocessing.bokeh_uniforms[ 'manualdof' ].value = 0;
this.postprocessing.bokeh_uniforms[ 'shaderFocus' ].value = 0;
this.postprocessing.bokeh_uniforms[ 'fstop' ].value = 2.8;
this.postprocessing.bokeh_uniforms[ 'showFocus' ].value = 1;
this.postprocessing.bokeh_uniforms[ 'focalDepth' ].value = 0.1; //console.log( this.postprocessing.bokeh_uniforms[ "focalDepth" ].value );

this.postprocessing.bokeh_uniforms[ 'znear' ].value = this.near;
this.postprocessing.bokeh_uniforms[ 'zfar' ].value = this.near;
this.postprocessing.bokeh_uniforms[ 'textureWidth' ].value = window.innerWidth;
this.postprocessing.bokeh_uniforms[ 'textureHeight' ].value = window.innerHeight;
this.postprocessing.materialBokeh = new THREE.ShaderMaterial( {
uniforms: this.postprocessing.bokeh_uniforms,
vertexShader: bokeh_shader.vertexShader,
fragmentShader: bokeh_shader.fragmentShader,
defines: {
RINGS: this.shaderSettings.rings,
SAMPLES: this.shaderSettings.samples,
DEPTH_PACKING: 1
}
} );
this.postprocessing.quad = new THREE.Mesh( new THREE.PlaneGeometry( window.innerWidth, window.innerHeight ), this.postprocessing.materialBokeh );
this.postprocessing.quad.position.z = - 500;
this.postprocessing.scene.add( this.postprocessing.quad );

}

}

};
renderCinematic( scene, renderer ) {

CinematicCamera.prototype.renderCinematic = function ( scene, renderer ) {
if ( this.postprocessing.enabled ) {

if ( this.postprocessing.enabled ) {
const currentRenderTarget = renderer.getRenderTarget();
renderer.clear(); // Render scene into texture

var currentRenderTarget = renderer.getRenderTarget();
renderer.clear(); // Render scene into texture
scene.overrideMaterial = null;
renderer.setRenderTarget( this.postprocessing.rtTextureColor );
renderer.clear();
renderer.render( scene, this ); // Render depth into texture

scene.overrideMaterial = null;
renderer.setRenderTarget( this.postprocessing.rtTextureColor );
renderer.clear();
renderer.render( scene, this ); // Render depth into texture
scene.overrideMaterial = this.materialDepth;
renderer.setRenderTarget( this.postprocessing.rtTextureDepth );
renderer.clear();
renderer.render( scene, this ); // Render bokeh composite

scene.overrideMaterial = this.materialDepth;
renderer.setRenderTarget( this.postprocessing.rtTextureDepth );
renderer.clear();
renderer.render( scene, this ); // Render bokeh composite
renderer.setRenderTarget( null );
renderer.render( this.postprocessing.scene, this.postprocessing.camera );
renderer.setRenderTarget( currentRenderTarget );

renderer.setRenderTarget( null );
renderer.render( this.postprocessing.scene, this.postprocessing.camera );
renderer.setRenderTarget( currentRenderTarget );
}

}

};
}

THREE.CinematicCamera = CinematicCamera;

Expand Down
90 changes: 44 additions & 46 deletions examples/js/geometries/BoxLineGeometry.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,59 @@
( function () {

var BoxLineGeometry = function ( width, height, depth, widthSegments, heightSegments, depthSegments ) {

THREE.BufferGeometry.call( this );
width = width || 1;
height = height || 1;
depth = depth || 1;
widthSegments = Math.floor( widthSegments ) || 1;
heightSegments = Math.floor( heightSegments ) || 1;
depthSegments = Math.floor( depthSegments ) || 1;
var widthHalf = width / 2;
var heightHalf = height / 2;
var depthHalf = depth / 2;
var segmentWidth = width / widthSegments;
var segmentHeight = height / heightSegments;
var segmentDepth = depth / depthSegments;
var vertices = [];
var x = - widthHalf,
y = - heightHalf,
z = - depthHalf;

for ( var i = 0; i <= widthSegments; i ++ ) {

vertices.push( x, - heightHalf, - depthHalf, x, heightHalf, - depthHalf );
vertices.push( x, heightHalf, - depthHalf, x, heightHalf, depthHalf );
vertices.push( x, heightHalf, depthHalf, x, - heightHalf, depthHalf );
vertices.push( x, - heightHalf, depthHalf, x, - heightHalf, - depthHalf );
x += segmentWidth;
class BoxLineGeometry extends THREE.BufferGeometry {

}
constructor( width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1 ) {

for ( var i = 0; i <= heightSegments; i ++ ) {
super();
widthSegments = Math.floor( widthSegments );
heightSegments = Math.floor( heightSegments );
depthSegments = Math.floor( depthSegments );
const widthHalf = width / 2;
const heightHalf = height / 2;
const depthHalf = depth / 2;
const segmentWidth = width / widthSegments;
const segmentHeight = height / heightSegments;
const segmentDepth = depth / depthSegments;
const vertices = [];
let x = - widthHalf;
let y = - heightHalf;
let z = - depthHalf;

vertices.push( - widthHalf, y, - depthHalf, widthHalf, y, - depthHalf );
vertices.push( widthHalf, y, - depthHalf, widthHalf, y, depthHalf );
vertices.push( widthHalf, y, depthHalf, - widthHalf, y, depthHalf );
vertices.push( - widthHalf, y, depthHalf, - widthHalf, y, - depthHalf );
y += segmentHeight;
for ( let i = 0; i <= widthSegments; i ++ ) {

}
vertices.push( x, - heightHalf, - depthHalf, x, heightHalf, - depthHalf );
vertices.push( x, heightHalf, - depthHalf, x, heightHalf, depthHalf );
vertices.push( x, heightHalf, depthHalf, x, - heightHalf, depthHalf );
vertices.push( x, - heightHalf, depthHalf, x, - heightHalf, - depthHalf );
x += segmentWidth;

for ( var i = 0; i <= depthSegments; i ++ ) {
}

vertices.push( - widthHalf, - heightHalf, z, - widthHalf, heightHalf, z );
vertices.push( - widthHalf, heightHalf, z, widthHalf, heightHalf, z );
vertices.push( widthHalf, heightHalf, z, widthHalf, - heightHalf, z );
vertices.push( widthHalf, - heightHalf, z, - widthHalf, - heightHalf, z );
z += segmentDepth;
for ( let i = 0; i <= heightSegments; i ++ ) {

}
vertices.push( - widthHalf, y, - depthHalf, widthHalf, y, - depthHalf );
vertices.push( widthHalf, y, - depthHalf, widthHalf, y, depthHalf );
vertices.push( widthHalf, y, depthHalf, - widthHalf, y, depthHalf );
vertices.push( - widthHalf, y, depthHalf, - widthHalf, y, - depthHalf );
y += segmentHeight;

}

this.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
for ( let i = 0; i <= depthSegments; i ++ ) {

};
vertices.push( - widthHalf, - heightHalf, z, - widthHalf, heightHalf, z );
vertices.push( - widthHalf, heightHalf, z, widthHalf, heightHalf, z );
vertices.push( widthHalf, heightHalf, z, widthHalf, - heightHalf, z );
vertices.push( widthHalf, - heightHalf, z, - widthHalf, - heightHalf, z );
z += segmentDepth;

}

this.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );

}

BoxLineGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
BoxLineGeometry.prototype.constructor = BoxLineGeometry;
}

THREE.BoxLineGeometry = BoxLineGeometry;

Expand Down
Loading