提交 0c1f71e5 编写于 作者: M Michal Polko

Added SMAA

上级 6798fb5c
/**
* @author mpk / http://polko.me/
*/
THREE.SMAAPass = function ( width, height, areaTexture ) {
// render targets
this.edgesRT = new THREE.WebGLRenderTarget( width, height, {
depthBuffer: false,
stencilBuffer: false,
generateMipmaps: false,
minFilter: THREE.LinearFilter,
format: THREE.RGBFormat
} );
this.weightsRT = new THREE.WebGLRenderTarget( width, height, {
depthBuffer: false,
stencilBuffer: false,
generateMipmaps: false,
minFilter: THREE.LinearFilter,
format: THREE.RGBAFormat
} );
// textures
if ( areaTexture === undefined ) {
console.error( "THREE.SMAAPass relies on smaa-area.png texture. You can find it in examples/textures/." );
}
this.areaTexture = areaTexture;
this.areaTexture.format = THREE.RGBFormat;
this.areaTexture.minFilter = THREE.LinearFilter;
this.areaTexture.generateMipmaps = false;
this.areaTexture.flipY = false;
this.searchTexture = new THREE.DataTexture( this.getSearchTextureBytes(), 66, 33, THREE.AlphaFormat );
this.searchTexture.magFilter = THREE.NearestFilter;
this.searchTexture.minFilter = THREE.NearestFilter;
this.searchTexture.generateMipmaps = false;
this.searchTexture.unpackAlignment = 1;
this.searchTexture.needsUpdate = true;
this.searchTexture.flipY = false;
// materials - pass 1
if ( THREE.SMAAShader === undefined ) {
console.error( "THREE.SMAAPass relies on THREE.SMAAShader" );
}
this.uniformsEdges = THREE.UniformsUtils.clone( THREE.SMAAShader[0].uniforms );
this.uniformsEdges[ "resolution" ].value.set( 1 / width, 1 / height );
this.materialEdges = new THREE.ShaderMaterial( {
defines: THREE.SMAAShader[0].defines,
uniforms: this.uniformsEdges,
vertexShader: THREE.SMAAShader[0].vertexShader,
fragmentShader: THREE.SMAAShader[0].fragmentShader
} );
// materials - pass 2
this.uniformsWeights = THREE.UniformsUtils.clone( THREE.SMAAShader[1].uniforms );
this.uniformsWeights[ "resolution" ].value.set( 1 / width, 1 / height );
this.uniformsWeights[ "tDiffuse" ].value = this.edgesRT;
this.uniformsWeights[ "tArea" ].value = this.areaTexture;
this.uniformsWeights[ "tSearch" ].value = this.searchTexture;
this.materialWeights = new THREE.ShaderMaterial( {
defines: THREE.SMAAShader[1].defines,
uniforms: this.uniformsWeights,
vertexShader: THREE.SMAAShader[1].vertexShader,
fragmentShader: THREE.SMAAShader[1].fragmentShader
} );
// materials - pass 3
this.uniformsBlend = THREE.UniformsUtils.clone( THREE.SMAAShader[2].uniforms );
this.uniformsBlend[ "resolution" ].value.set( 1 / width, 1 / height );
this.uniformsBlend[ "tDiffuse" ].value = this.weightsRT;
this.materialBlend = new THREE.ShaderMaterial( {
uniforms: this.uniformsBlend,
vertexShader: THREE.SMAAShader[2].vertexShader,
fragmentShader: THREE.SMAAShader[2].fragmentShader
} );
//
this.renderToScreen = false;
this.enabled = true;
this.needsSwap = false;
this.clear = false;
this.camera = new THREE.OrthographicCamera( -1, 1, 1, -1, 0, 1 );
this.scene = new THREE.Scene();
this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
this.scene.add( this.quad );
};
THREE.SMAAPass.prototype = {
render: function ( renderer, writeBuffer, readBuffer, delta ) {
// pass 1
this.uniformsEdges[ "tDiffuse" ].value = readBuffer;
this.quad.material = this.materialEdges;
renderer.render( this.scene, this.camera, this.edgesRT, this.clear );
// pass 2
this.quad.material = this.materialWeights;
renderer.render( this.scene, this.camera, this.weightsRT, this.clear );
// pass 3
this.uniformsBlend[ "tColor" ].value = readBuffer;
this.quad.material = this.materialBlend;
if ( this.renderToScreen ) {
renderer.render( this.scene, this.camera );
} else {
renderer.render( this.scene, this.camera, writeBuffer, this.clear );
}
},
setSize: function ( width, height ) {
this.edgesRT.setSize( width, height );
this.weightsRT.setSize( width, height );
this.materialEdges.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
this.materialWeights.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
this.materialBlend.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
},
getSearchTextureBytes: function () {
return new Uint8Array( [ 0,0,0,0,0,0,0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0,0,0,0,0,0,0,0,0,0,0,0,2,2,
0,1,1,0,0,2,2,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,1,1,0,1,1,2,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,1,1,0,0,2,2,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,1,1,0,1,1,2,
1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ] );
}
};
/**
* @author mpk / http://polko.me/
*
* WebGL port of Subpixel Morphological Antialiasing (SMAA) v2.8
* Preset: SMAA 1x Medium (with color edge detection)
* https://github.com/iryoku/smaa/releases/tag/v2.8
*/
THREE.SMAAShader = [ {
defines: {
"SMAA_THRESHOLD": "0.1"
},
uniforms: {
"tDiffuse": { type: "t", value: null },
"resolution": { type: "v2", value: new THREE.Vector2( 1 / 1024, 1 / 512 ) }
},
vertexShader: [
"uniform vec2 resolution;",
"varying vec2 vUv;",
"varying vec4 vOffset[ 3 ];",
"void SMAAEdgeDetectionVS( vec2 texcoord ) {",
"vOffset[ 0 ] = texcoord.xyxy + resolution.xyxy * vec4( -1.0, 0.0, 0.0, 1.0 );", // WebGL port note: Changed sign in W component
"vOffset[ 1 ] = texcoord.xyxy + resolution.xyxy * vec4( 1.0, 0.0, 0.0, -1.0 );", // WebGL port note: Changed sign in W component
"vOffset[ 2 ] = texcoord.xyxy + resolution.xyxy * vec4( -2.0, 0.0, 0.0, 2.0 );", // WebGL port note: Changed sign in W component
"}",
"void main() {",
"vUv = uv;",
"SMAAEdgeDetectionVS( vUv );",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"uniform sampler2D tDiffuse;",
"varying vec2 vUv;",
"varying vec4 vOffset[ 3 ];",
"vec4 SMAAColorEdgeDetectionPS( vec2 texcoord, vec4 offset[3], sampler2D colorTex ) {",
"vec2 threshold = vec2( SMAA_THRESHOLD, SMAA_THRESHOLD );",
// Calculate color deltas:
"vec4 delta;",
"vec3 C = texture2D( colorTex, texcoord ).rgb;",
"vec3 Cleft = texture2D( colorTex, offset[0].xy ).rgb;",
"vec3 t = abs( C - Cleft );",
"delta.x = max( max( t.r, t.g ), t.b );",
"vec3 Ctop = texture2D( colorTex, offset[0].zw ).rgb;",
"t = abs( C - Ctop );",
"delta.y = max( max( t.r, t.g ), t.b );",
// We do the usual threshold:
"vec2 edges = step( threshold, delta.xy );",
// Then discard if there is no edge:
"if ( dot( edges, vec2( 1.0, 1.0 ) ) == 0.0 )",
"discard;",
// Calculate right and bottom deltas:
"vec3 Cright = texture2D( colorTex, offset[1].xy ).rgb;",
"t = abs( C - Cright );",
"delta.z = max( max( t.r, t.g ), t.b );",
"vec3 Cbottom = texture2D( colorTex, offset[1].zw ).rgb;",
"t = abs( C - Cbottom );",
"delta.w = max( max( t.r, t.g ), t.b );",
// Calculate the maximum delta in the direct neighborhood:
"float maxDelta = max( max( max( delta.x, delta.y ), delta.z ), delta.w );",
// Calculate left-left and top-top deltas:
"vec3 Cleftleft = texture2D( colorTex, offset[2].xy ).rgb;",
"t = abs( C - Cleftleft );",
"delta.z = max( max( t.r, t.g ), t.b );",
"vec3 Ctoptop = texture2D( colorTex, offset[2].zw ).rgb;",
"t = abs( C - Ctoptop );",
"delta.w = max( max( t.r, t.g ), t.b );",
// Calculate the final maximum delta:
"maxDelta = max( max( maxDelta, delta.z ), delta.w );",
// Local contrast adaptation in action:
"edges.xy *= step( 0.5 * maxDelta, delta.xy );",
"return vec4( edges, 0.0, 0.0 );",
"}",
"void main() {",
"gl_FragColor = SMAAColorEdgeDetectionPS( vUv, vOffset, tDiffuse );",
"}"
].join("\n")
}, {
defines: {
"SMAA_MAX_SEARCH_STEPS": "8",
"SMAA_AREATEX_MAX_DISTANCE": "16",
"SMAA_AREATEX_PIXEL_SIZE": "( 1.0 / vec2( 160.0, 560.0 ) )",
"SMAA_AREATEX_SUBTEX_SIZE": "( 1.0 / 7.0 )"
},
uniforms: {
"tDiffuse": { type: "t", value: null },
"tArea": { type: "t", value: null },
"tSearch": { type: "t", value: null },
"resolution": { type: "v2", value: new THREE.Vector2( 1 / 1024, 1 / 512 ) }
},
vertexShader: [
"uniform vec2 resolution;",
"varying vec2 vUv;",
"varying vec4 vOffset[ 3 ];",
"varying vec2 vPixcoord;",
"void SMAABlendingWeightCalculationVS( vec2 texcoord ) {",
"vPixcoord = texcoord / resolution;",
// We will use these offsets for the searches later on (see @PSEUDO_GATHER4):
"vOffset[ 0 ] = texcoord.xyxy + resolution.xyxy * vec4( -0.25, 0.125, 1.25, 0.125 );", // WebGL port note: Changed sign in Y and W components
"vOffset[ 1 ] = texcoord.xyxy + resolution.xyxy * vec4( -0.125, 0.25, -0.125, -1.25 );", // WebGL port note: Changed sign in Y and W components
// And these for the searches, they indicate the ends of the loops:
"vOffset[ 2 ] = vec4( vOffset[ 0 ].xz, vOffset[ 1 ].yw ) + vec4( -2.0, 2.0, -2.0, 2.0 ) * resolution.xxyy * float( SMAA_MAX_SEARCH_STEPS );",
"}",
"void main() {",
"vUv = uv;",
"SMAABlendingWeightCalculationVS( vUv );",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"#define SMAASampleLevelZeroOffset( tex, coord, offset ) texture2D( tex, coord + float( offset ) * resolution, 0.0 )",
"uniform sampler2D tDiffuse;",
"uniform sampler2D tArea;",
"uniform sampler2D tSearch;",
"uniform vec2 resolution;",
"varying vec2 vUv;",
"varying vec4 vOffset[3];",
"varying vec2 vPixcoord;",
"vec2 round( vec2 x ) {",
"return sign( x ) * floor( abs( x ) + 0.5 );",
"}",
"float SMAASearchLength( sampler2D searchTex, vec2 e, float bias, float scale ) {",
// Not required if searchTex accesses are set to point:
// float2 SEARCH_TEX_PIXEL_SIZE = 1.0 / float2(66.0, 33.0);
// e = float2(bias, 0.0) + 0.5 * SEARCH_TEX_PIXEL_SIZE +
// e * float2(scale, 1.0) * float2(64.0, 32.0) * SEARCH_TEX_PIXEL_SIZE;
"e.r = bias + e.r * scale;",
"return 255.0 * texture2D( searchTex, e, 0.0 ).a;",
"}",
"float SMAASearchXLeft( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {",
/**
* @PSEUDO_GATHER4
* This texcoord has been offset by (-0.25, -0.125) in the vertex shader to
* sample between edge, thus fetching four edges in a row.
* Sampling with different offsets in each direction allows to disambiguate
* which edges are active from the four fetched ones.
*/
"vec2 e = vec2( 0.0, 1.0 );",
"for ( int i = 0; i < SMAA_MAX_SEARCH_STEPS; i ++ ) {", // WebGL port note: Changed while to for
"e = texture2D( edgesTex, texcoord, 0.0 ).rg;",
"texcoord -= vec2( 2.0, 0.0 ) * resolution;",
"if ( ! ( texcoord.x > end && e.g > 0.8281 && e.r == 0.0 ) ) break;",
"}",
// We correct the previous (-0.25, -0.125) offset we applied:
"texcoord.x += 0.25 * resolution.x;",
// The searches are bias by 1, so adjust the coords accordingly:
"texcoord.x += resolution.x;",
// Disambiguate the length added by the last step:
"texcoord.x += 2.0 * resolution.x;", // Undo last step
"texcoord.x -= resolution.x * SMAASearchLength(searchTex, e, 0.0, 0.5);",
"return texcoord.x;",
"}",
"float SMAASearchXRight( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {",
"vec2 e = vec2( 0.0, 1.0 );",
"for ( int i = 0; i < SMAA_MAX_SEARCH_STEPS; i ++ ) {", // WebGL port note: Changed while to for
"e = texture2D( edgesTex, texcoord, 0.0 ).rg;",
"texcoord += vec2( 2.0, 0.0 ) * resolution;",
"if ( ! ( texcoord.x < end && e.g > 0.8281 && e.r == 0.0 ) ) break;",
"}",
"texcoord.x -= 0.25 * resolution.x;",
"texcoord.x -= resolution.x;",
"texcoord.x -= 2.0 * resolution.x;",
"texcoord.x += resolution.x * SMAASearchLength( searchTex, e, 0.5, 0.5 );",
"return texcoord.x;",
"}",
"float SMAASearchYUp( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {",
"vec2 e = vec2( 1.0, 0.0 );",
"for ( int i = 0; i < SMAA_MAX_SEARCH_STEPS; i ++ ) {", // WebGL port note: Changed while to for
"e = texture2D( edgesTex, texcoord, 0.0 ).rg;",
"texcoord += vec2( 0.0, 2.0 ) * resolution;", // WebGL port note: Changed sign
"if ( ! ( texcoord.y > end && e.r > 0.8281 && e.g == 0.0 ) ) break;",
"}",
"texcoord.y -= 0.25 * resolution.y;", // WebGL port note: Changed sign
"texcoord.y -= resolution.y;", // WebGL port note: Changed sign
"texcoord.y -= 2.0 * resolution.y;", // WebGL port note: Changed sign
"texcoord.y += resolution.y * SMAASearchLength( searchTex, e.gr, 0.0, 0.5 );", // WebGL port note: Changed sign
"return texcoord.y;",
"}",
"float SMAASearchYDown( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {",
"vec2 e = vec2( 1.0, 0.0 );",
"for ( int i = 0; i < SMAA_MAX_SEARCH_STEPS; i ++ ) {", // WebGL port note: Changed while to for
"e = texture2D( edgesTex, texcoord, 0.0 ).rg;",
"texcoord -= vec2( 0.0, 2.0 ) * resolution;", // WebGL port note: Changed sign
"if ( ! ( texcoord.y < end && e.r > 0.8281 && e.g == 0.0 ) ) break;",
"}",
"texcoord.y += 0.25 * resolution.y;", // WebGL port note: Changed sign
"texcoord.y += resolution.y;", // WebGL port note: Changed sign
"texcoord.y += 2.0 * resolution.y;", // WebGL port note: Changed sign
"texcoord.y -= resolution.y * SMAASearchLength( searchTex, e.gr, 0.5, 0.5 );", // WebGL port note: Changed sign
"return texcoord.y;",
"}",
"vec2 SMAAArea( sampler2D areaTex, vec2 dist, float e1, float e2, float offset ) {",
// Rounding prevents precision errors of bilinear filtering:
"vec2 texcoord = float( SMAA_AREATEX_MAX_DISTANCE ) * round( 4.0 * vec2( e1, e2 ) ) + dist;",
// We do a scale and bias for mapping to texel space:
"texcoord = SMAA_AREATEX_PIXEL_SIZE * texcoord + ( 0.5 * SMAA_AREATEX_PIXEL_SIZE );",
// Move to proper place, according to the subpixel offset:
"texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;",
"return texture2D( areaTex, texcoord, 0.0 ).rg;",
"}",
"vec4 SMAABlendingWeightCalculationPS( vec2 texcoord, vec2 pixcoord, vec4 offset[ 3 ], sampler2D edgesTex, sampler2D areaTex, sampler2D searchTex, ivec4 subsampleIndices ) {",
"vec4 weights = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec2 e = texture2D( edgesTex, texcoord ).rg;",
"if ( e.g > 0.0 ) {", // Edge at north
"vec2 d;",
// Find the distance to the left:
"vec2 coords;",
"coords.x = SMAASearchXLeft( edgesTex, searchTex, offset[ 0 ].xy, offset[ 2 ].x );",
"coords.y = offset[ 1 ].y;", // offset[1].y = texcoord.y - 0.25 * resolution.y (@CROSSING_OFFSET)
"d.x = coords.x;",
// Now fetch the left crossing edges, two at a time using bilinear
// filtering. Sampling at -0.25 (see @CROSSING_OFFSET) enables to
// discern what value each edge has:
"float e1 = texture2D( edgesTex, coords, 0.0 ).r;",
// Find the distance to the right:
"coords.x = SMAASearchXRight( edgesTex, searchTex, offset[ 0 ].zw, offset[ 2 ].y );",
"d.y = coords.x;",
// We want the distances to be in pixel units (doing this here allow to
// better interleave arithmetic and memory accesses):
"d = d / resolution.x - pixcoord.x;",
// SMAAArea below needs a sqrt, as the areas texture is compressed
// quadratically:
"vec2 sqrt_d = sqrt( abs( d ) );",
// Fetch the right crossing edges:
"coords.y -= 1.0 * resolution.y;", // WebGL port note: Added
"float e2 = SMAASampleLevelZeroOffset( edgesTex, coords, ivec2( 1, 0 ) ).r;",
// Ok, we know how this pattern looks like, now it is time for getting
// the actual area:
"weights.rg = SMAAArea( areaTex, sqrt_d, e1, e2, float( subsampleIndices.y ) );",
"}",
"if ( e.r > 0.0 ) {", // Edge at west
"vec2 d;",
// Find the distance to the top:
"vec2 coords;",
"coords.y = SMAASearchYUp( edgesTex, searchTex, offset[ 1 ].xy, offset[ 2 ].z );",
"coords.x = offset[ 0 ].x;", // offset[1].x = texcoord.x - 0.25 * resolution.x;
"d.x = coords.y;",
// Fetch the top crossing edges:
"float e1 = texture2D( edgesTex, coords, 0.0 ).g;",
// Find the distance to the bottom:
"coords.y = SMAASearchYDown( edgesTex, searchTex, offset[ 1 ].zw, offset[ 2 ].w );",
"d.y = coords.y;",
// We want the distances to be in pixel units:
"d = d / resolution.y - pixcoord.y;",
// SMAAArea below needs a sqrt, as the areas texture is compressed
// quadratically:
"vec2 sqrt_d = sqrt( abs( d ) );",
// Fetch the bottom crossing edges:
"coords.y -= 1.0 * resolution.y;", // WebGL port note: Added
"float e2 = SMAASampleLevelZeroOffset( edgesTex, coords, ivec2( 0, 1 ) ).g;",
// Get the area for this direction:
"weights.ba = SMAAArea( areaTex, sqrt_d, e1, e2, float( subsampleIndices.x ) );",
"}",
"return weights;",
"}",
"void main() {",
"gl_FragColor = SMAABlendingWeightCalculationPS( vUv, vPixcoord, vOffset, tDiffuse, tArea, tSearch, ivec4( 0.0 ) );",
"}"
].join("\n")
}, {
uniforms: {
"tDiffuse": { type: "t", value: null },
"tColor": { type: "t", value: null },
"resolution": { type: "v2", value: new THREE.Vector2( 1 / 1024, 1 / 512 ) }
},
vertexShader: [
"uniform vec2 resolution;",
"varying vec2 vUv;",
"varying vec4 vOffset[ 2 ];",
"void SMAANeighborhoodBlendingVS( vec2 texcoord ) {",
"vOffset[ 0 ] = texcoord.xyxy + resolution.xyxy * vec4( -1.0, 0.0, 0.0, 1.0 );", // WebGL port note: Changed sign in W component
"vOffset[ 1 ] = texcoord.xyxy + resolution.xyxy * vec4( 1.0, 0.0, 0.0, -1.0 );", // WebGL port note: Changed sign in W component
"}",
"void main() {",
"vUv = uv;",
"SMAANeighborhoodBlendingVS( vUv );",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"uniform sampler2D tDiffuse;",
"uniform sampler2D tColor;",
"uniform vec2 resolution;",
"varying vec2 vUv;",
"varying vec4 vOffset[ 2 ];",
"vec4 SMAANeighborhoodBlendingPS( vec2 texcoord, vec4 offset[ 2 ], sampler2D colorTex, sampler2D blendTex ) {",
// Fetch the blending weights for current pixel:
"vec4 a;",
"a.xz = texture2D( blendTex, texcoord ).xz;",
"a.y = texture2D( blendTex, offset[ 1 ].zw ).g;",
"a.w = texture2D( blendTex, offset[ 1 ].xy ).a;",
// Is there any blending weight with a value greater than 0.0?
"if ( dot(a, vec4( 1.0, 1.0, 1.0, 1.0 )) < 1e-5 ) {",
"return texture2D( colorTex, texcoord, 0.0 );",
"} else {",
// Up to 4 lines can be crossing a pixel (one through each edge). We
// favor blending by choosing the line with the maximum weight for each
// direction:
"vec2 offset;",
"offset.x = a.a > a.b ? a.a : -a.b;", // left vs. right
"offset.y = a.g > a.r ? -a.g : a.r;", // top vs. bottom // WebGL port note: Changed signs
// Then we go in the direction that has the maximum weight:
"if ( abs( offset.x ) > abs( offset.y )) {", // horizontal vs. vertical
"offset.y = 0.0;",
"} else {",
"offset.x = 0.0;",
"}",
// Fetch the opposite color and lerp by hand:
"vec4 C = texture2D( colorTex, texcoord, 0.0 );",
"texcoord += sign( offset ) * resolution;",
"vec4 Cop = texture2D( colorTex, texcoord, 0.0 );",
"float s = abs( offset.x ) > abs( offset.y ) ? abs( offset.x ) : abs( offset.y );",
// WebGL port note: Added gamma correction
"C.xyz = pow(C.xyz, vec3(2.2));",
"Cop.xyz = pow(Cop.xyz, vec3(2.2));",
"vec4 mixed = mix(C, Cop, s);",
"mixed.xyz = pow(mixed.xyz, vec3(1.0 / 2.2));",
"return mixed;",
"}",
"}",
"void main() {",
"gl_FragColor = SMAANeighborhoodBlendingPS( vUv, vOffset, tColor, tDiffuse );",
"}"
].join("\n")
} ];
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - postprocessing smaa</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
margin: 0px;
background-color: #000000;
overflow: hidden;
}
</style>
</head>
<body>
<script src="../build/three.min.js"></script>
<script src="js/postprocessing/SMAAPass.js"></script>
<script src="js/shaders/CopyShader.js"></script>
<script src="js/shaders/SMAAShader.js"></script>
<script src="js/postprocessing/EffectComposer.js"></script>
<script src="js/postprocessing/RenderPass.js"></script>
<script src="js/postprocessing/MaskPass.js"></script>
<script src="js/postprocessing/ShaderPass.js"></script>
<script>
var camera, scene, renderer, composer, pass;
var geometry, material, object;
var textureLoader = new THREE.TextureLoader();
textureLoader.load( "textures/smaa-area.png", function( areaTexture ) {
textureLoader.load( "textures/brick_diffuse.jpg", function( meshTexture ) {
init( areaTexture, meshTexture );
animate();
});
} );
function init( areaTexture, meshTexture ) {
renderer = new THREE.WebGLRenderer( { antialias: false } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.z = 400;
scene = new THREE.Scene();
geometry = new THREE.BoxGeometry( 200, 200, 200 );
material = new THREE.MeshBasicMaterial( { map: meshTexture } );
meshTexture.anisotropy = 4;
object = new THREE.Mesh( geometry, material );
scene.add( object );
// postprocessing
composer = new THREE.EffectComposer( renderer );
composer.addPass( new THREE.RenderPass( scene, camera ) );
pass = new THREE.SMAAPass( window.innerWidth, window.innerHeight, areaTexture );
pass.renderToScreen = true;
composer.addPass( pass );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
var width = window.innerWidth;
var height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
var pixelRatio = renderer.getPixelRatio();
var newWidth = Math.floor( width / pixelRatio ) || 1;
var newHeight = Math.floor( height / pixelRatio ) || 1;
composer.setSize( newWidth, newHeight );
pass.setSize( newWidth, newHeight );
}
function animate() {
requestAnimationFrame( animate );
object.rotation.x += 0.005;
object.rotation.y += 0.01;
composer.render();
}
</script>
</body>
</html>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册