提交 a28e942f 编写于 作者: M Mr.doob

Merge @zz85/buffer_attributes into dev

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - buffer geometry custom attributes [particles]</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 {
color: #ffffff;
font-family:Monospace;
font-size:13px;
text-align:center;
font-weight: bold;
background-color: #000000;
margin: 0px;
overflow: hidden;
}
#info {
color: #fff;
background-color: rgba( 0, 0, 0, 0.75 );
position: relative;
top: 0px; width: 100%;
padding: 5px;
z-index:100;
width:33em;
margin:0 auto -2em;
}
a { color: #ff0000 }
</style>
</head>
<body>
<div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - buffergeometry custom attributes example - particles</div>
<div id="container"></div>
<script src="../build/three.min.js"></script>
<script src="../src/renderers/WebGLRenderer.js"></script>
<script src="js/Detector.js"></script>
<script src="js/libs/stats.min.js"></script>
<script type="x-shader/x-vertex" id="vertexshader">
uniform float amplitude;
attribute float size;
attribute vec3 customColor;
varying vec3 vColor;
void main() {
vColor = customColor;
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
//gl_PointSize = size;
gl_PointSize = size * ( 300.0 / length( mvPosition.xyz ) );
gl_Position = projectionMatrix * mvPosition;
}
</script>
<script type="x-shader/x-fragment" id="fragmentshader">
uniform vec3 color;
uniform sampler2D texture;
varying vec3 vColor;
void main() {
gl_FragColor = vec4( color * vColor, 1.0 );
gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );
}
</script>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var renderer, scene, camera, stats;
var sphere, uniforms, geometry;
var noise = [];
var values_size;
//500000
var particles = 100000;
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 40, WIDTH / HEIGHT, 1, 10000 );
camera.position.z = 300;
scene = new THREE.Scene();
var attributes = {
size: { type: 'f', value: null },
customColor: { type: 'c', value: null }
};
uniforms = {
amplitude: { type: "f", value: 1.0 },
color: { type: "c", value: new THREE.Color( 0xffffff ) },
texture: { type: "t", value: THREE.ImageUtils.loadTexture( "textures/sprites/spark1.png" ) },
};
var shaderMaterial = new THREE.ShaderMaterial( {
uniforms: uniforms,
attributes: attributes,
vertexShader: document.getElementById( 'vertexshader' ).textContent,
fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
blending: THREE.AdditiveBlending,
depthTest: false,
transparent: true
});
var radius = 200;
geometry = new THREE.BufferGeometry();
// geometry.dynamic = true;
geometry.attributes = {
position: {
itemSize: 3,
array: new Float32Array( particles * 3 ),
numItems: particles * 3
},
customColor: {
itemSize: 3,
array: new Float32Array( particles * 3 ),
numItems: particles * 3
},
size: {
itemSize: 1,
array: new Float32Array( particles ),
numItems: particles * 1,
dynamic: true
},
}
values_size = geometry.attributes.size.array;
var positions = geometry.attributes.position.array;
var values_color = geometry.attributes.customColor.array;
sphere = new THREE.ParticleSystem( geometry, shaderMaterial );
//sphere.sortParticles = true;
var color = new THREE.Color( 0xffaa00 );;
for( var v = 0; v < particles; v++ ) {
values_size[ v ] = 10;
positions[ v * 3 + 0 ] = (Math.random() * 2 - 1) * radius;
positions[ v * 3 + 1 ] = (Math.random() * 2 - 1) * radius;
positions[ v * 3 + 2 ] = (Math.random() * 2 - 1) * radius;
if ( positions[ v * 3 + 0 ] < 0 )
color.setHSL( 0.5 + 0.1 * ( v / particles ), 0.7, 0.1 );
else
color.setHSL( 0.0 + 0.1 * ( v / particles ), 0.9, 0.1 );
values_color[ v * 3 + 0 ] = color.r;
values_color[ v * 3 + 1 ] = color.g;
values_color[ v * 3 + 2 ] = color.b;
}
scene.add( sphere );
renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1 } );
renderer.setSize( WIDTH, HEIGHT );
var container = document.getElementById( 'container' );
container.appendChild( renderer.domElement );
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
var time = Date.now() * 0.005;
sphere.rotation.z = 0.01 * time;
for( var i = 0; i < particles; i++ ) {
values_size[ i ] = 14 + 13 * Math.sin( 0.1 * i + time );
}
geometry.attributes.size.needsUpdate = true;
renderer.render( scene, camera );
}
</script>
</body>
</html>
此差异已折叠。
......@@ -105,7 +105,7 @@ THREE.WebGLRenderer = function ( parameters ) {
_usedTextureUnits = 0,
// GL state
// GL state
_viewportX = 0,
_viewportY = 0,
......@@ -486,54 +486,31 @@ THREE.WebGLRenderer = function ( parameters ) {
var attributes = geometry.attributes;
var index = attributes[ "index" ];
var position = attributes[ "position" ];
var normal = attributes[ "normal" ];
var uv = attributes[ "uv" ];
var color = attributes[ "color" ];
var tangent = attributes[ "tangent" ];
var attributeName, attributeItem;
if ( geometry.elementsNeedUpdate && index !== undefined ) {
for ( attributeName in attributes ) {
renderer.setDynamicIndexBuffer( index.buffer, index.array);
attributeItem = attributes[ attributeName ];
}
if ( geometry.verticesNeedUpdate && position !== undefined ) {
renderer.setDynamicArrayBuffer( position.buffer, position.array);
}
if ( geometry.normalsNeedUpdate && normal !== undefined ) {
renderer.setDynamicArrayBuffer( normal.buffer, normal.array);
}
if ( attributeItem.needsUpdate ) {
if ( geometry.uvsNeedUpdate && uv !== undefined ) {
if ( attributeName === 'index' ) {
renderer.setDynamicArrayBuffer( uv.buffer, uv.array);
renderer.setDynamicIndexBuffer( attributeItem.buffer, attributeItem.array );
}
if ( geometry.colorsNeedUpdate && color !== undefined ) {
renderer.setDynamicArrayBuffer( color.buffer, color.array);
}
} else {
if ( geometry.tangentsNeedUpdate && tangent !== undefined ) {
renderer.setDynamicArrayBuffer( attributeItem.buffer, attributeItem.array );
renderer.setDynamicArrayBuffer( tangent.buffer, tangent.array);
}
}
attributeItem.needsUpdate = false;
if ( dispose ) {
}
for ( var i in geometry.attributes ) {
if ( dispose && ! attributeItem.dynamic ) {
delete geometry.attributes[ i ].array;
delete attributeItem.array;
}
......@@ -601,7 +578,7 @@ THREE.WebGLRenderer = function ( parameters ) {
}
}
renderer.setDynamicArrayBuffer( object.__webglNormalBuffer, object.normalArray);
renderer.setFloatAttribute(program.attributes.normal, object.__webglNormalBuffer, 3, 0);
......@@ -630,11 +607,13 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( material.visible === false ) return;
var program, attributes, linewidth, primitives, a, attribute;
var program, programAttributes, linewidth, primitives, a, attribute, geometryAttributes;
var attributeItem, attributeName, attributePointer, attributeSize;
program = setProgram( camera, lights, fog, material, object );
attributes = program.attributes;
programAttributes = program.attributes;
geometryAttributes = geometry.attributes;
var updateBuffers = false,
wireframeBit = material.wireframe ? 1 : 0,
......@@ -657,7 +636,7 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( object instanceof THREE.Mesh ) {
var index = geometry.attributes[ "index" ];
var index = geometryAttributes[ "index" ];
// indexed triangles
......@@ -677,60 +656,25 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( updateBuffers ) {
// vertices
var position = geometry.attributes[ "position" ];
var positionSize = position.itemSize;
renderer.setFloatAttribute(attributes.position , position.buffer, positionSize, startIndex * positionSize * 4);
// normals
var normal = geometry.attributes[ "normal" ];
if ( attributes.normal >= 0 && normal ) {
var normalSize = normal.itemSize;
renderer.setFloatAttribute(attributes.normal , normal.buffer, normalSize, startIndex * normalSize * 4);
}
// uvs
var uv = geometry.attributes[ "uv" ];
if ( attributes.uv >= 0 && uv ) {
var uvSize = uv.itemSize;
renderer.setFloatAttribute(attributes.uv , uv.buffer, uvSize, startIndex * uvSize * 4);
}
// colors
var color = geometry.attributes[ "color" ];
if ( attributes.color >= 0 && color ) {
for ( attributeName in geometryAttributes ) {
var colorSize = color.itemSize;
renderer.setFloatAttribute(attributes.color , color.buffer, colorSize, startIndex * colorSize * 4);
if ( attributeName === 'index' ) continue;
}
// tangents
attributePointer = programAttributes[ attributeName ];
attributeItem = geometryAttributes[ attributeName ];
attributeSize = attributeItem.itemSize;
var tangent = geometry.attributes[ "tangent" ];
if ( attributePointer >= 0 ) {
if ( attributes.tangent >= 0 && tangent ) {
renderer.setFloatAttribute( attributePointer , attributeItem.buffer, attributeSize, startIndex * attributeSize * 4 );
var tangentSize = tangent.itemSize;
renderer.setFloatAttribute(attributes.tangent , tangent.buffer, tangentSize, startIndex * tangentSize * 4);
}
}
}
// render indexed triangles
renderer.drawTriangleElements(index.buffer, offsets[ i ].count, offsets[ i ].start * 2);
_this.info.render.calls ++;
......@@ -745,59 +689,26 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( updateBuffers ) {
// vertices
var position = geometry.attributes[ "position" ];
var positionSize = position.itemSize;
renderer.setFloatAttribute(attributes.position , position.buffer, positionSize, 0);
// normals
var normal = geometry.attributes[ "normal" ];
if ( attributes.normal >= 0 && normal ) {
var normalSize = normal.itemSize;
renderer.setFloatAttribute(attributes.normal , normal.buffer, normalSize, 0);
}
// uvs
var uv = geometry.attributes[ "uv" ];
if ( attributes.uv >= 0 && uv ) {
var uvSize = uv.itemSize;
renderer.setFloatAttribute(attributes.uv , uv.buffer, uvSize, 0);
}
// colors
for ( attributeName in geometryAttributes ) {
var color = geometry.attributes[ "color" ];
attributePointer = programAttributes[ attributeName ];
attributeItem = geometryAttributes[ attributeName ];
attributeSize = attributeItem.itemSize;
if ( attributes.color >= 0 && color ) {
if ( attributePointer >= 0 ) {
var colorSize = color.itemSize;
renderer.setFloatAttribute(attributes.color , color.buffer, colorSize, 0);
renderer.setFloatAttribute( attributePointer , attributeItem.buffer, attributeSize, 0 );
}
// tangents
var tangent = geometry.attributes[ "tangent" ];
if ( attributes.tangent >= 0 && tangent ) {
var tangentSize = tangent.itemSize;
renderer.setFloatAttribute(attributes.tangent , tangent.buffer, tangentSize, 0);
}
}
}
var position = geometry.attributes[ "position" ];
// render non-indexed triangles
renderer.drawTriangles( position.numItems / 3)
_this.info.render.calls ++;
......@@ -812,23 +723,22 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( updateBuffers ) {
// vertices
for ( attributeName in geometryAttributes ) {
var position = geometry.attributes[ "position" ];
var positionSize = position.itemSize;
renderer.setFloatAttribute(attributes.position , position.buffer, positionSize, 0);
// colors
attributePointer = programAttributes[ attributeName ];
attributeItem = geometryAttributes[ attributeName ];
attributeSize = attributeItem.itemSize;
var color = geometry.attributes[ "color" ];
if ( attributePointer >= 0 ) {
if ( attributes.color >= 0 && color ) {
renderer.setFloatAttribute( attributePointer , attributeItem.buffer, attributeSize, 0 );
var colorSize = color.itemSize;
renderer.setFloatAttribute(attributes.color , color.buffer, colorSize, 0);
}
}
var position = geometryAttributes[ "position" ];
// render particles
renderer.drawPoints(position.numItems / 3);
......@@ -841,23 +751,22 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( updateBuffers ) {
// vertices
for ( attributeName in geometryAttributes ) {
var position = geometry.attributes[ "position" ];
var positionSize = position.itemSize;
renderer.setFloatAttribute(attributes.position , position.buffer, positionSize, 0);
attributePointer = programAttributes[ attributeName ];
attributeItem = geometryAttributes[ attributeName ];
attributeSize = attributeItem.itemSize;
// colors
if ( attributePointer >= 0 ) {
var color = geometry.attributes[ "color" ];
renderer.setFloatAttribute( attributePointer , attributeItem.buffer, attributeSize, 0 );
if ( attributes.color >= 0 && color ) {
var colorSize = color.itemSize;
renderer.setFloatAttribute(attributes.color , color.buffer, colorSize, 0);
}
}
var position = geometryAttributes[ "position" ];
// render lines
renderer.setLineWidth( material.linewidth );
renderer.drawLineStrip(position.numItems / 3);
......@@ -3111,7 +3020,7 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( ! light.visible ) continue;
_direction.getPositionFromMatrix( light.matrixWorld );
_vector3.getPositionFromMatrix( light.target.matrixWorld );
_vector3.getPositionFromMatrix( light.target.matrixWorld );
_direction.sub( _vector3 );
_direction.normalize();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册