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

WebGLRenderer: Moved initAttributes/enableAttribute/disableUnusedAttributes to WebGLState.

上级 3ab982bc
......@@ -125,9 +125,6 @@ THREE.WebGLRenderer = function ( parameters ) {
_currentWidth = 0,
_currentHeight = 0,
_newAttributes = new Uint8Array( 16 ),
_enabledAttributes = new Uint8Array( 16 ),
// frustum
_frustum = new THREE.Frustum(),
......@@ -280,12 +277,6 @@ THREE.WebGLRenderer = function ( parameters ) {
_lightsNeedUpdate = true;
for ( var i = 0; i < _enabledAttributes.length; i ++ ) {
_enabledAttributes[ i ] = 0;
}
state.reset();
};
......@@ -2330,7 +2321,7 @@ THREE.WebGLRenderer = function ( parameters ) {
this.renderBufferImmediate = function ( object, program, material ) {
initAttributes();
state.initAttributes();
if ( object.hasPositions && ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer();
if ( object.hasNormals && ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer();
......@@ -2341,7 +2332,9 @@ THREE.WebGLRenderer = function ( parameters ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer );
_gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
enableAttribute( program.attributes.position );
state.enableAttribute( program.attributes.position );
_gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
}
......@@ -2394,7 +2387,9 @@ THREE.WebGLRenderer = function ( parameters ) {
}
_gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
enableAttribute( program.attributes.normal );
state.enableAttribute( program.attributes.normal );
_gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
}
......@@ -2403,7 +2398,9 @@ THREE.WebGLRenderer = function ( parameters ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglUvBuffer );
_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
enableAttribute( program.attributes.uv );
state.enableAttribute( program.attributes.uv );
_gl.vertexAttribPointer( program.attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
}
......@@ -2412,12 +2409,14 @@ THREE.WebGLRenderer = function ( parameters ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglColorBuffer );
_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
enableAttribute( program.attributes.color );
state.enableAttribute( program.attributes.color );
_gl.vertexAttribPointer( program.attributes.color, 3, _gl.FLOAT, false, 0, 0 );
}
disableUnusedAttributes();
state.disableUnusedAttributes();
_gl.drawArrays( _gl.TRIANGLES, 0, object.count );
......@@ -2447,7 +2446,7 @@ THREE.WebGLRenderer = function ( parameters ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryAttribute.buffer );
enableAttribute( programAttribute );
state.enableAttribute( programAttribute );
_gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, 0, startIndex * size * 4 ); // 4 bytes per Float32
......@@ -2469,7 +2468,7 @@ THREE.WebGLRenderer = function ( parameters ) {
}
disableUnusedAttributes();
state.disableUnusedAttributes();
}
......@@ -2494,7 +2493,7 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( updateBuffers ) {
initAttributes();
state.initAttributes();
}
......@@ -2835,7 +2834,7 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( updateBuffers ) {
initAttributes();
state.initAttributes();
}
......@@ -2846,7 +2845,9 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( updateBuffers ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
enableAttribute( attributes.position );
state.enableAttribute( attributes.position );
_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
}
......@@ -2877,7 +2878,9 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( attributes[ attribute.buffer.belongsToAttribute ] >= 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, attribute.buffer );
enableAttribute( attributes[ attribute.buffer.belongsToAttribute ] );
state.enableAttribute( attributes[ attribute.buffer.belongsToAttribute ] );
_gl.vertexAttribPointer( attributes[ attribute.buffer.belongsToAttribute ], attribute.size, _gl.FLOAT, false, 0, 0 );
}
......@@ -2894,7 +2897,9 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( object.geometry.colors.length > 0 || object.geometry.faces.length > 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
enableAttribute( attributes.color );
state.enableAttribute( attributes.color );
_gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
} else if ( material.defaultAttributeValues !== undefined ) {
......@@ -2911,7 +2916,9 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( attributes.normal >= 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
enableAttribute( attributes.normal );
state.enableAttribute( attributes.normal );
_gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
}
......@@ -2921,7 +2928,9 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( attributes.tangent >= 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
enableAttribute( attributes.tangent );
state.enableAttribute( attributes.tangent );
_gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
}
......@@ -2933,7 +2942,9 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( object.geometry.faceVertexUvs[ 0 ] ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
enableAttribute( attributes.uv );
state.enableAttribute( attributes.uv );
_gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
} else if ( material.defaultAttributeValues !== undefined ) {
......@@ -2950,7 +2961,9 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( object.geometry.faceVertexUvs[ 1 ] ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
enableAttribute( attributes.uv2 );
state.enableAttribute( attributes.uv2 );
_gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
} else if ( material.defaultAttributeValues !== undefined ) {
......@@ -2966,11 +2979,15 @@ THREE.WebGLRenderer = function ( parameters ) {
attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
enableAttribute( attributes.skinIndex );
state.enableAttribute( attributes.skinIndex );
_gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
enableAttribute( attributes.skinWeight );
state.enableAttribute( attributes.skinWeight );
_gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
}
......@@ -2980,14 +2997,16 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( attributes.lineDistance >= 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglLineDistanceBuffer );
enableAttribute( attributes.lineDistance );
state.enableAttribute( attributes.lineDistance );
_gl.vertexAttribPointer( attributes.lineDistance, 1, _gl.FLOAT, false, 0, 0 );
}
}
disableUnusedAttributes();
state.disableUnusedAttributes();
// render mesh
......@@ -3042,44 +3061,6 @@ THREE.WebGLRenderer = function ( parameters ) {
};
function initAttributes() {
for ( var i = 0, l = _newAttributes.length; i < l; i ++ ) {
_newAttributes[ i ] = 0;
}
}
function enableAttribute( attribute ) {
_newAttributes[ attribute ] = 1;
if ( _enabledAttributes[ attribute ] === 0 ) {
_gl.enableVertexAttribArray( attribute );
_enabledAttributes[ attribute ] = 1;
}
}
function disableUnusedAttributes() {
for ( var i = 0, l = _enabledAttributes.length; i < l; i ++ ) {
if ( _enabledAttributes[ i ] !== _newAttributes[ i ] ) {
_gl.disableVertexAttribArray( i );
_enabledAttributes[ i ] = 0;
}
}
}
function setupMorphTargets ( material, geometryGroup, object ) {
// set base
......@@ -3089,13 +3070,17 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( object.morphTargetBase !== - 1 && attributes.position >= 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] );
enableAttribute( attributes.position );
state.enableAttribute( attributes.position );
_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
} else if ( attributes.position >= 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
enableAttribute( attributes.position );
state.enableAttribute( attributes.position );
_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
}
......@@ -3117,7 +3102,9 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( attribute >= 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] );
enableAttribute( attribute );
state.enableAttribute( attribute );
_gl.vertexAttribPointer( attribute, 3, _gl.FLOAT, false, 0, 0 );
}
......@@ -3127,7 +3114,9 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( attribute >= 0 && material.morphNormals ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ] );
enableAttribute( attribute );
state.enableAttribute( attribute );
_gl.vertexAttribPointer( attribute, 3, _gl.FLOAT, false, 0, 0 );
}
......@@ -3189,7 +3178,9 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( attribute >= 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ influenceIndex ] );
enableAttribute( attribute );
state.enableAttribute( attribute );
_gl.vertexAttribPointer( attribute, 3, _gl.FLOAT, false, 0, 0 );
}
......@@ -3199,9 +3190,10 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( attribute >= 0 && material.morphNormals ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ influenceIndex ] );
enableAttribute( attribute );
_gl.vertexAttribPointer( attribute, 3, _gl.FLOAT, false, 0, 0 );
state.enableAttribute( attribute );
_gl.vertexAttribPointer( attribute, 3, _gl.FLOAT, false, 0, 0 );
}
......
......@@ -4,6 +4,9 @@
THREE.WebGLState = function ( gl, paramThreeToGL ) {
var newAttributes = new Uint8Array( 16 );
var enabledAttributes = new Uint8Array( 16 );
var currentBlending = - 1;
var currentBlendEquation = - 1;
var currentBlendSrc = - 1;
......@@ -24,6 +27,44 @@ THREE.WebGLState = function ( gl, paramThreeToGL ) {
var currentPolygonOffsetFactor = null;
var currentPolygonOffsetUnits = null;
this.initAttributes = function () {
for ( var i = 0, l = newAttributes.length; i < l; i ++ ) {
newAttributes[ i ] = 0;
}
};
this.enableAttribute = function ( attribute ) {
newAttributes[ attribute ] = 1;
if ( enabledAttributes[ attribute ] === 0 ) {
gl.enableVertexAttribArray( attribute );
enabledAttributes[ attribute ] = 1;
}
};
this.disableUnusedAttributes = function () {
for ( var i = 0, l = enabledAttributes.length; i < l; i ++ ) {
if ( enabledAttributes[ i ] !== newAttributes[ i ] ) {
gl.disableVertexAttribArray( i );
enabledAttributes[ i ] = 0;
}
}
};
this.setBlending = function ( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha ) {
if ( blending !== currentBlending ) {
......@@ -221,6 +262,12 @@ THREE.WebGLState = function ( gl, paramThreeToGL ) {
this.reset = function () {
for ( var i = 0; i < enabledAttributes.length; i ++ ) {
enabledAttributes[ i ] = 0;
}
currentBlending = - 1;
currentDepthTest = - 1;
currentDepthWrite = - 1;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册