Skip to content

Commit bf7a885

Browse files
committed
Add specular.
1 parent 8e9a929 commit bf7a885

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

examples/jsm/postprocessing/SSRrPass.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ var SSRrPass = function ( { renderer, scene, camera, width, height, selects, enc
142142
depthBuffer: true
143143
} );
144144

145+
this.beautyRenderTargetBunny = new WebGLRenderTarget( this.width, this.height, { // TODO: Can merge with metalnessRenderTarget?
146+
minFilter: LinearFilter,
147+
magFilter: LinearFilter,
148+
format: RGBAFormat,
149+
} );
150+
145151
// normal render target
146152

147153
this.normalRenderTarget = new WebGLRenderTarget( this.width, this.height, {
@@ -192,6 +198,7 @@ var SSRrPass = function ( { renderer, scene, camera, width, height, selects, enc
192198
}
193199

194200
this.ssrrMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
201+
this.ssrrMaterial.uniforms[ 'tDiffuseBunny' ].value = this.beautyRenderTargetBunny.texture;
195202
this.ssrrMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
196203
// if (this.isSelective) {
197204
this.ssrrMaterial.defines.isSelective = this.isSelective;
@@ -270,6 +277,7 @@ SSRrPass.prototype = Object.assign( Object.create( Pass.prototype ), {
270277
// dispose render targets
271278

272279
this.beautyRenderTarget.dispose();
280+
this.beautyRenderTargetBunny.dispose();
273281
this.normalRenderTarget.dispose();
274282
this.metalnessRenderTarget.dispose();
275283
this.ssrrRenderTarget.dispose();
@@ -317,7 +325,17 @@ SSRrPass.prototype = Object.assign( Object.create( Pass.prototype ), {
317325
mesh_box.visible=true
318326
mesh_cone.visible=true
319327
mesh_plane.visible=true
320-
renderer.render( this.scene, this.camera );
328+
renderer.render(this.scene, this.camera);
329+
330+
renderer.setRenderTarget( this.beautyRenderTargetBunny );
331+
renderer.clear();
332+
mesh_bunny.visible=true
333+
mesh_sphere.visible=false
334+
mesh_box.visible=false
335+
mesh_cone.visible=false
336+
mesh_plane.visible=false
337+
renderer.render(this.scene, this.camera);
338+
321339

322340
// render normals
323341

@@ -352,6 +370,7 @@ SSRrPass.prototype = Object.assign( Object.create( Pass.prototype ), {
352370
this.ssrrMaterial.uniforms[ 'ior' ].value = this.ior;
353371
this.ssrrMaterial.uniforms[ 'surfDist' ].value = this.surfDist;
354372
this.ssrrMaterial.uniforms[ 'thickTolerance' ].value = this.thickTolerance;
373+
this.ssrrMaterial.uniforms[ 'tDiffuseBunny' ].value = this.beautyRenderTargetBunny.texture;
355374
this.renderPass( renderer, this.ssrrMaterial, this.ssrrRenderTarget );
356375

357376
// output result to screen
@@ -517,10 +536,13 @@ SSRrPass.prototype = Object.assign( Object.create( Pass.prototype ), {
517536
}
518537

519538
});
539+
this.scene._background=this.scene._background
540+
this.scene.background=null
520541
this.scene._fog=this.scene.fog // TODO: Formal writing.
521542
this.scene.fog=null
522543
renderer.render(this.scene, this.camera);
523544
this.scene.fog=this.scene._fog
545+
this.scene.background=this.scene._background
524546
this.scene.traverse( child => {
525547

526548
child.material = child._SSRPassMaterialBack;
@@ -543,6 +565,7 @@ SSRrPass.prototype = Object.assign( Object.create( Pass.prototype ), {
543565
this.ssrrMaterial.defines.MAX_STEP = Math.sqrt( width * width + height * height );
544566
this.ssrrMaterial.needsUpdate = true;
545567
this.beautyRenderTarget.setSize( width, height );
568+
this.beautyRenderTargetBunny.setSize( width, height );
546569
this.ssrrRenderTarget.setSize( width, height );
547570
this.normalRenderTarget.setSize( width, height );
548571

examples/jsm/shaders/SSRrShader.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var SSRrShader = {
1717
uniforms: {
1818

1919
"tDiffuse": { value: null },
20+
"tDiffuseBunny": { value: null },
2021
"tNormal": { value: null },
2122
"tMetalness": { value: null },
2223
"tDepth": { value: null },
@@ -56,6 +57,7 @@ var SSRrShader = {
5657
uniform sampler2D tNormal;
5758
uniform sampler2D tMetalness;
5859
uniform sampler2D tDiffuse;
60+
uniform sampler2D tDiffuseBunny;
5961
uniform float cameraRange;
6062
uniform vec2 resolution;
6163
uniform float cameraNear;
@@ -115,7 +117,7 @@ var SSRrShader = {
115117
}
116118
void main(){
117119
float metalness=texture2D(tMetalness,vUv).r;
118-
if(metalness==0.) return;
120+
if(metalness<=0.) return;
119121
120122
// TODO: Will if(ior===0.) return; improve performance?
121123
// gl_FragColor=vec4(0,0,.5,1);return;
@@ -186,7 +188,9 @@ var SSRrShader = {
186188
187189
if(viewRefractRayZ<vZ){
188190
vec4 refractColor=texture2D(tDiffuse,uv);
189-
gl_FragColor.xyz=refractColor.xyz;
191+
vec4 diffuseBunnyColor=texture2D(tDiffuseBunny,vUv);
192+
gl_FragColor.xyz=max(refractColor.xyz,diffuseBunnyColor.r); // TODO: Convert gray to alpha to improve display.
193+
// gl_FragColor.xyz=refractColor.xyz*(1.+diffuseBunnyColor.r*3.);
190194
gl_FragColor.a=1.;
191195
break;
192196
}

examples/webgl_postprocessing_ssrr.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@
104104

105105
geometry.computeVertexNormals();
106106

107-
const material = new THREE.MeshStandardMaterial({ color: 0x606060 });
107+
const material = new THREE.MeshStandardMaterial({
108+
color: 'black',
109+
metalness: 0,
110+
roughness: .2,
111+
});
112+
window.bunnyMaterial=material
108113
const mesh = new THREE.Mesh(geometry, material);
109114
mesh.position.y = - 0.0365;
110115
mesh.name='bunny'
@@ -195,6 +200,8 @@
195200
gui.add(params, 'enableSSRr').name('Enable SSRr');
196201
ssrrPass.ior = .05;
197202
gui.add(ssrrPass, 'ior').name('IOR').min(0).max(.2).step(.00001)
203+
gui.add(bunnyMaterial, 'metalness').min(0).max(1).step(.01)
204+
gui.add(bunnyMaterial, 'roughness').min(0).max(1).step(.01)
198205
gui.add(params, 'autoRotate').onChange(() => {
199206

200207
controls.enabled = !params.autoRotate;

0 commit comments

Comments
 (0)