提交 750a242f 编写于 作者: A alteredq

Added vertex colors for particles.

上级 5365a11b
此差异已折叠。
此差异已折叠。
此差异已折叠。
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>three.js - particles - billboards - colors - webgl</title>
<meta charset="utf-8">
<style type="text/css">
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
font-family:Monospace;
font-size:13px;
text-align:center;
font-weight: bold;
text-align:center;
}
a {
color:#0078ff;
}
#info {
color:#fff;
position: absolute;
top: 0px; width: 100%;
padding: 5px;
z-index:100;
}
#oldie {
font-family:monospace;
font-size:13px;
text-align:center;
background:#eee;
color:#000;
padding:1em;
width:475px;
margin:5em auto 0;
display:none;
}
</style>
</head>
<body>
<div id="info">
<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl particle billboards colors example
</div>
<center>
<div id="oldie">
Sorry, your browser doesn't support <a href="http://khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">WebGL</a>
and <a href="http://www.whatwg.org/specs/web-workers/current-work/">Web Workers</a>.<br/>
<br/>
Please try in
<a href="http://www.chromium.org/getting-involved/dev-channel">Chrome 9+</a> /
<a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 4+</a> /
<a href="http://nightly.webkit.org/">Safari OSX 10.6+</a>
</div>
</center>
<script type="text/javascript" src="../build/ThreeExtras.js"></script>
<script type="text/javascript" src="js/Stats.js"></script>
<script type="text/javascript">
if ( !is_browser_compatible() ) {
document.getElementById( "oldie" ).style.display = "block";
}
var container, stats;
var camera, scene, renderer, particles, geometry, material, i, h, color, colors = [], sprite, size, x, y, z;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
init();
setInterval( loop, 1000 / 60 );
//loop();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.Camera( 55, window.innerWidth / window.innerHeight, 1, 3000 );
camera.position.z = 1000;
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0x000000, 0.001 );
geometry = new THREE.Geometry();
sprite = ImageUtils.loadTexture( "textures/sprites/circle.png" );
for ( i = 0; i < 5000; i++ ) {
x = 2000 * Math.random() - 1000;
y = 2000 * Math.random() - 1000;
z = 2000 * Math.random() - 1000;
vector = new THREE.Vector3( x, y, z );
geometry.vertices.push( new THREE.Vertex( vector ) );
colors[ i ] = new THREE.Color( 0xffffff );
colors[ i ].setHSV( (x+1000)/2000, 1.0, 1.0 );
}
geometry.colors = colors;
console.log( colors[0] );
material = new THREE.ParticleBasicMaterial( { size: 35, map: sprite, blending: THREE.BillboardBlending, vertex_colors: true } );
material.color.setHSV( 1.0, 0.2, 0.8 );
particles = new THREE.ParticleSystem( geometry, material );
particles.sortParticles = true;
particles.updateMatrix();
scene.addObject( particles );
var light = new THREE.DirectionalLight( 0xffffff );
light.position.x = 0;
light.position.y = 0;
light.position.z = 1;
scene.addLight( light );
renderer = new THREE.WebGLRenderer( { clearAlpha: 1 });
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
}
function onDocumentMouseMove( event ) {
mouseX = event.clientX - windowHalfX;
mouseY = event.clientY - windowHalfY;
}
function onDocumentTouchStart( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
mouseY = event.touches[ 0 ].pageY - windowHalfY;
}
}
function onDocumentTouchMove( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
mouseY = event.touches[ 0 ].pageY - windowHalfY;
}
}
function loop() {
var time = new Date().getTime() * 0.00005;
camera.position.x += ( mouseX - camera.position.x ) * 0.05;
camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
h = ( 360 * ( 1.0 + time ) % 360 ) / 360;
material.color.setHSV( h, 0.8, 1.0 );
renderer.render( scene, camera );
stats.update();
}
function is_browser_compatible() {
// WebGL support
try { var test = new Float32Array(1); } catch(e) { return false; }
// Web workers
return !!window.Worker;
}
</script>
</body>
</html>
......@@ -10,6 +10,7 @@ THREE.Geometry = function () {
this.faces = [];
this.uvs = [];
this.uvs2 = [];
this.colors = [];
this.boundingBox = null;
this.boundingSphere = null;
......
......@@ -16,6 +16,8 @@ THREE.ParticleBasicMaterial = function ( parameters ) {
this.opacity = 1;
this.size = 1;
this.vertex_colors = false;
this.blending = THREE.NormalBlending;
this.offset = new THREE.Vector2(); // TODO: expose to parameters
......@@ -27,6 +29,7 @@ THREE.ParticleBasicMaterial = function ( parameters ) {
if ( parameters.opacity !== undefined ) this.opacity = parameters.opacity;
if ( parameters.size !== undefined ) this.size = parameters.size;
if ( parameters.blending !== undefined ) this.blending = parameters.blending;
if ( parameters.vertex_colors !== undefined ) this.vertex_colors = parameters.vertex_colors;
}
......@@ -42,6 +45,7 @@ THREE.ParticleBasicMaterial.prototype = {
'opacity: ' + this.opacity + '<br/>' +
'size: ' + this.size + '<br/>' +
'blending: ' + this.blending + '<br/>' +
'vertex_colors: ' + this.vertex_colors + '<br/>' +
')';
}
......
......@@ -8,7 +8,6 @@ THREE.ParticleSystem = function ( geometry, materials ) {
this.geometry = geometry;
this.materials = materials instanceof Array ? materials : [ materials ];
this.colors = [];
this.sortParticles = false;
......
......@@ -197,6 +197,7 @@ THREE.WebGLRenderer = function ( parameters ) {
var nvertices = geometry.vertices.length;
geometry.__vertexArray = new Float32Array( nvertices * 3 );
geometry.__colorArray = new Float32Array( nvertices * 3 );
geometry.__particleArray = new Uint16Array( nvertices );
geometry.__sortArray = [];
......@@ -665,6 +666,9 @@ THREE.WebGLRenderer = function ( parameters ) {
}
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webGLVertexBuffer );
_gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
}
// yeah, this is silly as order of element indices is currently fixed
......@@ -678,24 +682,25 @@ THREE.WebGLRenderer = function ( parameters ) {
}
}
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webGLVertexBuffer );
_gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometry.__webGLLineBuffer );
_gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometry.__webGLLineBuffer );
_gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
}
};
this.setParticleBuffers = function( geometry, hint, object, camera ) {
var v, vertex, offset,
var v, c, vertex, offset,
vertices = geometry.vertices,
vl = vertices.length,
colors = geometry.colors,
cl = colors.length,
vertexArray = geometry.__vertexArray,
particleArray = geometry.__particleArray,
colorArray = geometry.__colorArray,
sortArray = geometry.__sortArray,
......@@ -730,9 +735,21 @@ THREE.WebGLRenderer = function ( parameters ) {
vertexArray[ offset ] = vertex.x;
vertexArray[ offset + 1 ] = vertex.y;
vertexArray[ offset + 2 ] = vertex.z;
}
for ( c = 0; c < cl; c++ ) {
offset = c * 3;
color = colors[ sortArray[c][1] ];
colorArray[ offset ] = color.r;
colorArray[ offset + 1 ] = color.g;
colorArray[ offset + 2 ] = color.b;
}
} else {
if ( dirtyVertices ) {
......@@ -750,9 +767,26 @@ THREE.WebGLRenderer = function ( parameters ) {
}
}
if ( dirtyColors ) {
for ( c = 0; c < cl; c++ ) {
color = colors[ c ];
offset = c * 3;
colorArray[ offset ] = color.r;
colorArray[ offset + 1 ] = color.g;
colorArray[ offset + 2 ] = color.b;
}
}
}
// yeah, this is silly as order of element indices is currently fixed
// though this could change if some use case arises
// (like depth-sorting of semi-opaque particles)
......@@ -764,15 +798,25 @@ THREE.WebGLRenderer = function ( parameters ) {
particleArray[ v ] = v;
}
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometry.__webGLParticleBuffer );
_gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, particleArray, hint );
}
if ( dirtyVertices || object.sortParticles ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webGLVertexBuffer );
_gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
}
if ( dirtyColors || object.sortParticles ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webGLColorBuffer );
_gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webGLVertexBuffer );
_gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometry.__webGLParticleBuffer );
_gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, particleArray, hint );
}
};
......@@ -787,7 +831,7 @@ THREE.WebGLRenderer = function ( parameters ) {
function refreshUniformsCommon( material, fog ) {
// premultiply alpha
material.uniforms.color.value.setRGB( material.color.r * material.opacity, material.color.g * material.opacity, material.color.b * material.opacity );
material.uniforms.diffuse.value.setRGB( material.color.r * material.opacity, material.color.g * material.opacity, material.color.b * material.opacity );
// pure color
//material.uniforms.color.value.setHex( material.color.hex );
......@@ -824,7 +868,7 @@ THREE.WebGLRenderer = function ( parameters ) {
function refreshUniformsLine( material, fog ) {
material.uniforms.color.value.setRGB( material.color.r * material.opacity, material.color.g * material.opacity, material.color.b * material.opacity );
material.uniforms.diffuse.value.setRGB( material.color.r * material.opacity, material.color.g * material.opacity, material.color.b * material.opacity );
material.uniforms.opacity.value = material.opacity;
if ( fog ) {
......@@ -848,7 +892,7 @@ THREE.WebGLRenderer = function ( parameters ) {
function refreshUniformsParticle( material, fog ) {
material.uniforms.color.value.setRGB( material.color.r * material.opacity, material.color.g * material.opacity, material.color.b * material.opacity );
material.uniforms.psColor.value.setRGB( material.color.r * material.opacity, material.color.g * material.opacity, material.color.b * material.opacity );
material.uniforms.opacity.value = material.opacity;
material.uniforms.size.value = material.size;
material.uniforms.map.texture = material.map;
......@@ -935,7 +979,8 @@ THREE.WebGLRenderer = function ( parameters ) {
maxLightCount = allocateLights( lights, 4 );
parameters = { fog: fog, map: material.map, env_map: material.env_map, light_map: material.light_map, maxDirLights: maxLightCount.directional, maxPointLights: maxLightCount.point };
parameters = { fog: fog, map: material.map, env_map: material.env_map, light_map: material.light_map, vertex_colors: material.vertex_colors,
maxDirLights: maxLightCount.directional, maxPointLights: maxLightCount.point };
material.program = buildProgram( material.fragment_shader, material.vertex_shader, parameters );
identifiers = [ 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition' ];
......@@ -946,7 +991,7 @@ THREE.WebGLRenderer = function ( parameters ) {
}
cacheUniformLocations( material.program, identifiers );
cacheAttributeLocations( material.program, [ "position", "normal", "uv", "uv2", "tangent" ] );
cacheAttributeLocations( material.program, [ "position", "normal", "uv", "uv2", "tangent", "color" ] );
}
......@@ -1021,6 +1066,16 @@ THREE.WebGLRenderer = function ( parameters ) {
_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
_gl.enableVertexAttribArray( attributes.position );
// colors
if ( attributes.color >= 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLColorBuffer );
_gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
_gl.enableVertexAttribArray( attributes.color );
}
// normals
if ( attributes.normal >= 0 ) {
......@@ -1588,6 +1643,7 @@ THREE.WebGLRenderer = function ( parameters ) {
parameters.map ? "#define USE_MAP" : "",
parameters.env_map ? "#define USE_ENVMAP" : "",
parameters.light_map ? "#define USE_LIGHTMAP" : "",
parameters.vertex_colors ? "#define USE_COLOR" : "",
"uniform mat4 viewMatrix;",
"uniform vec3 cameraPosition;",
......@@ -1603,6 +1659,7 @@ THREE.WebGLRenderer = function ( parameters ) {
parameters.map ? "#define USE_MAP" : "",
parameters.env_map ? "#define USE_ENVMAP" : "",
parameters.light_map ? "#define USE_LIGHTMAP" : "",
parameters.vertex_colors ? "#define USE_COLOR" : "",
"uniform mat4 objectMatrix;",
"uniform mat4 modelViewMatrix;",
......@@ -1612,6 +1669,7 @@ THREE.WebGLRenderer = function ( parameters ) {
"uniform vec3 cameraPosition;",
"attribute vec3 position;",
"attribute vec3 normal;",
"attribute vec3 color;",
"attribute vec2 uv;",
"attribute vec2 uv2;",
""
......@@ -2417,7 +2475,52 @@ THREE.Snippets = {
"totalLight += pointDiffuse + pointSpecular;",
"#endif"
].join("\n"),
// VERTEX COLORS
color_pars_fragment: [
"#ifdef USE_COLOR",
"varying vec3 vColor;",
"#endif"
].join("\n"),
color_fragment: [
"#ifdef USE_COLOR",
"vertexColor = vec4( vColor, opacity );",
"#endif"
].join("\n"),
color_pars_vertex: [
"#ifdef USE_COLOR",
"varying vec3 vColor;",
"#endif"
].join("\n"),
color_vertex: [
"#ifdef USE_COLOR",
"vColor = color;",
"#endif"
].join("\n")
};
......@@ -2425,7 +2528,7 @@ THREE.UniformsLib = {
common: {
"color" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
"diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
"opacity" : { type: "f", value: 1 },
"map" : { type: "t", value: 0, texture: null },
......@@ -2457,7 +2560,7 @@ THREE.UniformsLib = {
particle: {
"color" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
"psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
"opacity" : { type: "f", value: 1 },
"size" : { type: "f", value: 1 },
"map" : { type: "t", value: 0, texture: null },
......@@ -2544,7 +2647,7 @@ THREE.ShaderLib = {
fragment_shader: [
"uniform vec3 color;",
"uniform vec3 diffuse;",
"uniform float opacity;",
THREE.Snippets[ "map_pars_fragment" ],
......@@ -2554,7 +2657,7 @@ THREE.ShaderLib = {
"void main() {",
"vec4 mColor = vec4( color, opacity );",
"vec4 mColor = vec4( diffuse, opacity );",
"vec4 mapColor = vec4( 1.0 );",
"vec4 lightmapColor = vec4( 1.0 );",
"vec4 cubeColor = vec4( 1.0 );",
......@@ -2600,7 +2703,7 @@ THREE.ShaderLib = {
fragment_shader: [
"uniform vec3 color;",
"uniform vec3 diffuse;",
"uniform float opacity;",
"varying vec3 vLightWeighting;",
......@@ -2612,7 +2715,7 @@ THREE.ShaderLib = {
"void main() {",
"vec4 mColor = vec4( color, opacity );",
"vec4 mColor = vec4( diffuse, opacity );",
"vec4 mapColor = vec4( 1.0 );",
"vec4 lightmapColor = vec4( 1.0 );",
"vec4 cubeColor = vec4( 1.0 );",
......@@ -2672,7 +2775,7 @@ THREE.ShaderLib = {
fragment_shader: [
"uniform vec3 color;",
"uniform vec3 diffuse;",
"uniform float opacity;",
"uniform vec3 ambient;",
......@@ -2689,7 +2792,7 @@ THREE.ShaderLib = {
"void main() {",
"vec4 mColor = vec4( color, opacity );",
"vec4 mColor = vec4( diffuse, opacity );",
"vec4 mapColor = vec4( 1.0 );",
"vec4 lightmapColor = vec4( 1.0 );",
"vec4 cubeColor = vec4( 1.0 );",
......@@ -2753,20 +2856,23 @@ THREE.ShaderLib = {
fragment_shader: [
"uniform vec3 color;",
"uniform vec3 psColor;",
"uniform float opacity;",
THREE.Snippets[ "color_pars_fragment" ],
THREE.Snippets[ "map_particle_pars_fragment" ],
THREE.Snippets[ "fog_pars_fragment" ],
"void main() {",
"vec4 mColor = vec4( color, opacity );",
"vec4 mColor = vec4( psColor, opacity );",
"vec4 mapColor = vec4( 1.0 );",
"vec4 vertexColor = vec4( 1.0 );",
THREE.Snippets[ "map_particle_fragment" ],
THREE.Snippets[ "color_fragment" ],
"gl_FragColor = mColor * mapColor;",
"gl_FragColor = mColor * mapColor * vertexColor;",
THREE.Snippets[ "fog_fragment" ],
......@@ -2778,8 +2884,12 @@ THREE.ShaderLib = {
"uniform float size;",
THREE.Snippets[ "color_pars_vertex" ],
"void main() {",
THREE.Snippets[ "color_vertex" ],
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
"gl_Position = projectionMatrix * mvPosition;",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册