Created
April 8, 2012 15:40
-
-
Save davidedc/2338030 to your computer and use it in GitHub Desktop.
Example for issue of blend losing the background transparency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title>three.js webgl - lines - cubes - colors</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 { | |
background-color: #FF0000; | |
margin: 0px; | |
overflow: hidden; | |
} | |
a { | |
color:#0078ff; | |
} | |
#info { | |
position: absolute; | |
top: 10px; width: 100%; | |
color: #ffffff; | |
padding: 5px; | |
font-family: Monospace; | |
font-size: 13px; | |
text-align: center; | |
z-index:100; | |
} | |
a { | |
color: orange; | |
text-decoration: none; | |
} | |
a:hover { | |
color: #0080ff; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="info"> | |
<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - color lines WebGL demo | |
[<a href="http://en.wikipedia.org/wiki/Hilbert_curve">Hilbert curve</a> thanks to <a href="http://www.openprocessing.org/visuals/?visualID=15599">Thomas Diewald</a>] | |
</div> | |
<script src="../build/Three.js"></script> | |
<script src="js/ShaderExtras.js"></script> | |
<script src="js/postprocessing/EffectComposer.js"></script> | |
<script src="js/postprocessing/MaskPass.js"></script> | |
<script src="js/postprocessing/RenderPass.js"></script> | |
<script src="js/postprocessing/ShaderPass.js"></script> | |
<script src="js/postprocessing/BloomPass.js"></script> | |
<script src="js/postprocessing/SavePass.js"></script> | |
<script src="js/Detector.js"></script> | |
<script src="js/Stats.js"></script> | |
<script> | |
if ( ! Detector.webgl ) Detector.addGetWebGLMessage(); | |
var mouseX = 0, mouseY = 0, | |
windowHalfX = window.innerWidth / 2, | |
windowHalfY = window.innerHeight / 2, | |
camera, scene, renderer, material, composer; | |
init(); | |
animate(); | |
function init() { | |
var i, container; | |
container = document.createElement( 'div' ); | |
document.body.appendChild( container ); | |
scene = new THREE.Scene(); | |
camera = new THREE.PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 1, 10000 ); | |
camera.position.z = 700; | |
scene.add( camera ); | |
renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 0, antialias: true } ); | |
renderer.setSize( window.innerWidth, window.innerHeight ); | |
renderer.autoClear = false; | |
container.appendChild( renderer.domElement ); | |
var geometry = new THREE.Geometry(), | |
geometry2 = new THREE.Geometry(), | |
geometry3 = new THREE.Geometry(), | |
points = hilbert3D( new THREE.Vector3( 0,0,0 ), 200.0, 2, 0, 1, 2, 3, 4, 5, 6, 7 ), | |
colors = [], colors2 = [], colors3 = []; | |
for ( i = 0; i < points.length; i ++ ) { | |
geometry.vertices.push( new THREE.Vertex( points[ i ] ) ); | |
colors[ i ] = new THREE.Color( 0xffffff ); | |
colors[ i ].setHSV( 0.6, ( 200 + points[ i ].x ) / 400, 1.0 ); | |
colors2[ i ] = new THREE.Color( 0xffffff ); | |
colors2[ i ].setHSV( 0.3, 1.0, ( 200 + points[ i ].x ) / 400 ); | |
colors3[ i ] = new THREE.Color( 0xffffff ); | |
colors3[ i ].setHSV( i / points.length, 1.0, 1.0 ); | |
} | |
geometry2.vertices = geometry3.vertices = geometry.vertices; | |
geometry.colors = colors; | |
geometry2.colors = colors2; | |
geometry3.colors = colors3; | |
// lines | |
material = new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 1, linewidth: 3 } ); | |
var line, p, scale = 0.3, d = 225, | |
parameters = [ [ material, scale*1.5, [-d,0,0], geometry ], | |
[ material, scale*1.5, [0,0,0], geometry2 ], | |
[ material, scale*1.5, [d,0,0], geometry3 ] ]; | |
material.vertexColors = true; | |
for ( i = 0; i < parameters.length; ++i ) { | |
p = parameters[ i ]; | |
line = new THREE.Line( p[ 3 ], p[ 0 ] ); | |
line.scale.x = line.scale.y = line.scale.z = p[ 1 ]; | |
line.position.x = p[ 2 ][ 0 ]; | |
line.position.y = p[ 2 ][ 1 ]; | |
line.position.z = p[ 2 ][ 2 ]; | |
scene.add( line ); | |
} | |
// | |
stats = new Stats(); | |
stats.domElement.style.position = 'absolute'; | |
stats.domElement.style.top = '0px'; | |
//container.appendChild( stats.domElement ); | |
// | |
renderTargetParameters = { format: THREE.RGBAFormat, stencilBuffer: true }; | |
renderTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, renderTargetParameters ); | |
effectSaveTarget = new THREE.SavePass( new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, renderTargetParameters ) ); | |
effectSaveTarget.clear = false; | |
effectBlend = new THREE.ShaderPass( THREE.ShaderExtras[ "blend" ], "tDiffuse1" ); | |
screenPass = new THREE.ShaderPass( THREE.ShaderExtras[ "screen" ] ); | |
// motion blur | |
effectBlend.uniforms[ 'tDiffuse2' ].texture = effectSaveTarget.renderTarget; | |
effectBlend.uniforms[ 'mixRatio' ].value = 0.25; | |
var renderModel = new THREE.RenderPass( scene, camera ); | |
composer = new THREE.EffectComposer( renderer, renderTarget ); | |
composer.addPass( renderModel ); | |
// if I uncomment these two, the motion blur kicks-in but I lose the | |
// transparent background. | |
composer.addPass( effectBlend ); //*** | |
composer.addPass( effectSaveTarget ); //*** | |
composer.addPass( screenPass ); | |
screenPass.renderToScreen = true; | |
} | |
// port of Processing Java code by Thomas Diewald | |
// http://www.openprocessing.org/visuals/?visualID=15599 | |
function hilbert3D( center, side, iterations, v0, v1, v2, v3, v4, v5, v6, v7 ) { | |
var half = side / 2, | |
vec_s = [ | |
new THREE.Vector3( center.x - half, center.y + half, center.z - half ), | |
new THREE.Vector3( center.x - half, center.y + half, center.z + half ), | |
new THREE.Vector3( center.x - half, center.y - half, center.z + half ), | |
new THREE.Vector3( center.x - half, center.y - half, center.z - half ), | |
new THREE.Vector3( center.x + half, center.y - half, center.z - half ), | |
new THREE.Vector3( center.x + half, center.y - half, center.z + half ), | |
new THREE.Vector3( center.x + half, center.y + half, center.z + half ), | |
new THREE.Vector3( center.x + half, center.y + half, center.z - half ) | |
], | |
vec = [ vec_s[ v0 ], vec_s[ v1 ], vec_s[ v2 ], vec_s[ v3 ], vec_s[ v4 ], vec_s[ v5 ], vec_s[ v6 ], vec_s[ v7 ] ]; | |
if( --iterations >= 0 ) { | |
var tmp = []; | |
Array.prototype.push.apply( tmp, hilbert3D ( vec[ 0 ], half, iterations, v0, v3, v4, v7, v6, v5, v2, v1 ) ); | |
Array.prototype.push.apply( tmp, hilbert3D ( vec[ 1 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) ); | |
Array.prototype.push.apply( tmp, hilbert3D ( vec[ 2 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) ); | |
Array.prototype.push.apply( tmp, hilbert3D ( vec[ 3 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) ); | |
Array.prototype.push.apply( tmp, hilbert3D ( vec[ 4 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) ); | |
Array.prototype.push.apply( tmp, hilbert3D ( vec[ 5 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) ); | |
Array.prototype.push.apply( tmp, hilbert3D ( vec[ 6 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) ); | |
Array.prototype.push.apply( tmp, hilbert3D ( vec[ 7 ], half, iterations, v6, v5, v2, v1, v0, v3, v4, v7 ) ); | |
return tmp; | |
} | |
return vec; | |
} | |
// | |
function onDocumentMouseMove( event ) { | |
} | |
function onDocumentTouchStart( event ) { | |
} | |
function onDocumentTouchMove( event ) { | |
} | |
// | |
function animate() { | |
requestAnimationFrame( animate ); | |
render(); | |
stats.update(); | |
} | |
function render() { | |
camera.position.x += ( mouseX - camera.position.x ) * .05; | |
camera.position.y += ( - mouseY + 200 - camera.position.y ) * .05; | |
camera.lookAt( scene.position ); | |
var time = Date.now() * 0.005; | |
for( var i = 0; i < scene.objects.length; i ++ ) { | |
scene.objects[ i ].rotation.y = time * ( i % 2 ? 1 : -1 ); | |
} | |
renderer.clear(); | |
//renderer.render(scene, camera); | |
composer.render(); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment