[b86468]: / v3 / js / libs / three / postprocessing / SSAOPass.js

Download this file

158 lines (120 with data), 4.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
/**
* Screen-space ambient occlusion pass.
*
* Has the following parameters
* - radius
* - Ambient occlusion shadow radius (numeric value).
* - onlyAO
* - Display only ambient occlusion result (boolean value).
* - aoClamp
* - Ambient occlusion clamp (numeric value).
* - lumInfluence
* - Pixel luminosity influence in AO calculation (numeric value).
*
* To output to screen set renderToScreens true
*
* @author alteredq / http://alteredqualia.com/
* @author tentone
* @class SSAOPass
*/
THREE.SSAOPass = function ( scene, camera, width, height ) {
if ( THREE.SSAOShader === undefined) {
console.warn( 'THREE.SSAOPass depends on THREE.SSAOShader' );
return new THREE.ShaderPass();
}
THREE.ShaderPass.call( this, THREE.SSAOShader );
this.width = ( width !== undefined ) ? width : 512;
this.height = ( height !== undefined ) ? height : 256;
this.renderToScreen = false;
this.camera2 = camera;
this.scene2 = scene;
//Depth material
this.depthMaterial = new THREE.MeshDepthMaterial();
this.depthMaterial.depthPacking = THREE.RGBADepthPacking;
this.depthMaterial.blending = THREE.NoBlending;
//Depth render target
this.depthRenderTarget = new THREE.WebGLRenderTarget( this.width, this.height, { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter } );
//this.depthRenderTarget.texture.name = 'SSAOShader.rt';
//Shader uniforms
this.uniforms[ 'tDepth' ].value = this.depthRenderTarget.texture;
this.uniforms[ 'size' ].value.set( this.width, this.height );
this.uniforms[ 'cameraNear' ].value = this.camera2.near;
this.uniforms[ 'cameraFar' ].value = this.camera2.far;
this.uniforms[ 'radius' ].value = 4;
this.uniforms[ 'onlyAO' ].value = false;
this.uniforms[ 'aoClamp' ].value = 0.25;
this.uniforms[ 'lumInfluence' ].value = 0.7;
//Setters and getters for uniforms
var self = this;
Object.defineProperties(this, {
radius: {
get: function() { return this.uniforms[ 'radius' ].value; },
set: function( value ) { this.uniforms[ 'radius' ].value = value; }
},
onlyAO: {
get: function() { return this.uniforms[ 'onlyAO' ].value; },
set: function( value ) { this.uniforms[ 'onlyAO' ].value = value; }
},
aoClamp: {
get: function() { return this.uniforms[ 'aoClamp' ].value; },
set: function( value ) { this.uniforms[ 'aoClamp' ].value = value; }
},
lumInfluence: {
get: function() { return this.uniforms[ 'lumInfluence' ].value; },
set: function( value ) { this.uniforms[ 'lumInfluence' ].value = value; }
},
});
}
THREE.SSAOPass.prototype = Object.create( THREE.ShaderPass.prototype );
/**
* Render using this pass.
*
* @method render
* @param {WebGLRenderer} renderer
* @param {WebGLRenderTarget} writeBuffer Buffer to write output.
* @param {WebGLRenderTarget} readBuffer Input buffer.
* @param {Number} delta Delta time in milliseconds.
* @param {Boolean} maskActive Not used in this pass.
*/
THREE.SSAOPass.prototype.render = function( renderer, writeBuffer, readBuffer, delta, maskActive ) {
//Render depth into depthRenderTarget
this.scene2.overrideMaterial = this.depthMaterial;
renderer.render( this.scene2, this.camera2, this.depthRenderTarget, true );
this.scene2.overrideMaterial = null;
//SSAO shaderPass
THREE.ShaderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, delta, maskActive );
};
/**
* Change scene to be renderer by this render pass.
*
* @method setScene
* @param {Scene} scene
*/
THREE.SSAOPass.prototype.setScene = function(scene) {
this.scene2 = scene;
};
/**
* Set camera used by this render pass.
*
* @method setCamera
* @param {Camera} camera
*/
THREE.SSAOPass.prototype.setCamera = function( camera ) {
this.camera2 = camera;
this.uniforms[ 'cameraNear' ].value = this.camera2.near;
this.uniforms[ 'cameraFar' ].value = this.camera2.far;
};
/**
* Set resolution of this render pass.
*
* @method setSize
* @param {Number} width
* @param {Number} height
*/
THREE.SSAOPass.prototype.setSize = function( width, height ) {
this.width = width;
this.height = height;
this.uniforms[ 'size' ].value.set( this.width, this.height );
this.depthRenderTarget.setSize( this.width, this.height );
};