Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- THREE.Assert = function( mustBeTrue, message ) {
- if( ! mustBeTrue ) throw new Error( message | "Assertion failure." );
- }
- THREE.Descriptor = function ( elementType, itemSize, isArray, isIndex ) {
- THREE.Descriptor.AssertElementType( elementType );
- this.itemSize = itemSize;
- this.elementType = elementType;
- this.isArray = isArray;
- this.isIndex = isIndex;
- };
- THREE.Descriptor.prototype = {
- constructor: THREE.Descriptor,
- createArray: function( numItems ) {
- return THREE.Descriptor.CreateArray( this.elementType, numberItems );
- },
- isTexture: function() {
- // TODO: Add more types here.
- return ( this.elementType == gl.TEXTURE_2D || this.elementType == gl.TEXTURE_2D_ARRAY || this.elementType == gl.TEXTURE_CUBE_MAP );
- },
- equals: function ( d ) {
- return ( d.itemSize === this.itemSize ) && ( d.elementType === this.elementType ) && ( d.isArray == this.isArray ) && ( d.isIndex == this.isIndex );
- },
- };
- THREE.Descriptor.AssertElementType = function( elementType ) {
- switch( elementType ) {
- case gl.FLOAT:
- case gl.UNSIGNED_BYTE:
- case gl.UNSIGNED_SHORT:
- case gl.UNSIGNED_INT:
- case gl.SHORT:
- case gl.INT:
- case gl.TEXTURE_2D:
- case gl.TEXTURE_2D_ARRAY:
- case gl.TEXTURE_CUBE_MAP:
- return true;
- default:
- throw new Error( "AttributeName elementType not supported: " + elementType );
- };
- }
- THREE.Descriptor.GetArrayType = function( elementType ) {
- switch( elementType ) {
- case gl.FLOAT:
- return Float32Array;
- case gl.UNSIGNED_BYTE:
- return Uint8Array;
- case gl.UNSIGNED_SHORT:
- return Uint16Array;
- case gl.UNSIGNED_INT:
- return Uint32Array;
- case gl.SHORT:
- return Int16Array;
- case gl.INT:
- return Int32Array;
- default:
- throw new Error( "AttributeName elementType not supported: " + this.elementType );
- }
- }
- THREE.Descriptor.CreateArray = function( elementType, itemSize, optionalTarget ) {
- var arrayType = THREE.Descriptor.GetArrayType( elementType );
- var newLength = numberItems * this.itemSize;
- if( ( optionalTarget instanceof arrayType ) && optionalTarget.length === newLength ) {
- return optionalTarget;
- }
- return new arrayType( newLength );
- }
- THREE.Descriptor.NewFloat32Array = function( newLength, optionalTarget ) {
- if( ( optionalTarget instanceof Float32Array ) && optionalTarget.length === newLength ) {
- return optionalTarget;
- }
- return new Float32Array( newLength );
- }
- THREE.Descriptor.NewUint16Array = function( newLength, optionalTarget ) {
- if( ( optionalTarget instanceof Uint16Array ) && optionalTarget.length === newLength ) {
- return optionalTarget;
- }
- return new Uint16Array( newLength );
- }
- THREE.Descriptor.AssertPrimitiveType = function( primitiveType ) {
- switch( elementType ) {
- case gl.POINTS:
- case gl.LINES:
- case gl.LINE_LOOP:
- case gl.LINE_STRIP:
- case gl.TRIANGLES:
- case gl.TRIANGLE_STRIP:
- case gl.TRIANGLE_FAN:
- return true;
- default:
- throw new Error( "AttributeName primitiveType not supported: " + elementType );
- };
- }
- // TODO: move to MathUtils or something else in the ThreeJS math subdirectory?
- THREE.Descriptor.ToFlatFloat32Array = function( array, optionalTarget ) {
- if( ! array ) {
- return THREE.Descriptor.NewFloat32Array( 0, optionalTarget );
- }
- // no conversion necessary
- if( array instanceof Float32Array ) {
- return array;
- }
- // convert individual items into arrays
- if( ! ( array instanceof Array ) ) {
- array = [ array ];
- }
- if( array.length === 0 ) {
- return THREE.Descriptor.NewFloat32Array( 0, optionalTarget );
- }
- var firstItem = array[0];
- if( typeof firstItem == "number" ) {
- var result = THREE.Descriptor.NewFloat32Array( array.length, optionalTarget );
- for( var i = 0, il = array.length; i < il; i ++ ) {
- result[i] = array[i];
- }
- return result;
- }
- // the following checks are ordered in decreasing frequency of usage
- // TODO: Is there a faster check than repeated "instanceof"? But this is likely not a performance critical check
- if( firstItem instanceof THREE.Vector3 ) {
- var result = THREE.Descriptor.NewFloat32Array( array.length * 3, optionalTarget );
- for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
- result[j ++ ] = array[i].x;
- result[j ++ ] = array[i].y;
- result[j ++ ] = array[i].z;
- }
- return result;
- }
- if( firstItem instanceof THREE.Color ) {
- var result = THREE.Descriptor.NewFloat32Array( array.length * 3, optionalTarget );
- for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
- result[j ++ ] = array[i].r;
- result[j ++ ] = array[i].g;
- result[j ++ ] = array[i].b;
- }
- return result;
- }
- if( firstItem instanceof THREE.Vector2 ) {
- var result = THREE.Descriptor.NewFloat32Array( array.length * 2, optionalTarget );
- for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
- result[j ++ ] = array[i].x;
- result[j ++ ] = array[i].y;
- }
- return result;
- }
- if( ( firstItem instanceof THREE.Vector4 ) || ( firstItem instanceof THREE.Quaternion ) ) {
- var result = THREE.Descriptor.NewFloat32Array( array.length * 4, optionalTarget );
- for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
- result[j ++ ] = array[i].x;
- result[j ++ ] = array[i].y;
- result[j ++ ] = array[i].z;
- result[j ++ ] = array[i].w;
- }
- return result;
- }
- if( firstItem instanceof THREE.Matrix3 ) {
- var result = THREE.Descriptor.NewFloat32Array( array.length * 9, optionalTarget );
- for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
- var e = array[i].elements;
- result[j ++ ] = e[0];
- result[j ++ ] = e[1];
- result[j ++ ] = e[2];
- result[j ++ ] = e[3];
- result[j ++ ] = e[4];
- result[j ++ ] = e[5];
- result[j ++ ] = e[6];
- result[j ++ ] = e[7];
- result[j ++ ] = e[8];
- }
- return result;
- }
- if( firstItem instanceof THREE.Matrix4 ) {
- var result = THREE.Descriptor.NewFloat32Array( array.length * 16, optionalTarget );
- for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
- var e = array[i].elements;
- result[j ++ ] = e[0];
- result[j ++ ] = e[1];
- result[j ++ ] = e[2];
- result[j ++ ] = e[3];
- result[j ++ ] = e[4];
- result[j ++ ] = e[5];
- result[j ++ ] = e[6];
- result[j ++ ] = e[7];
- result[j ++ ] = e[8];
- result[j ++ ] = e[9];
- result[j ++ ] = e[10];
- result[j ++ ] = e[11];
- result[j ++ ] = e[12];
- result[j ++ ] = e[13];
- result[j ++ ] = e[14];
- result[j ++ ] = e[15];
- }
- return result;
- }
- if( firstItem instanceof THREE.Plane ) {
- var result = THREE.Descriptor.NewFloat32Array( array.length * 4, optionalTarget );
- for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
- result[j ++ ] = array[i].normal.x;
- result[j ++ ] = array[i].normal.y;
- result[j ++ ] = array[i].normal.z;
- result[j ++ ] = array[i].constant;
- }
- return result;
- }
- if( firstItem instanceof THREE.Line3 ) {
- var result = THREE.Descriptor.NewFloat32Array( array.length * 6, optionalTarget );
- for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
- result[j ++ ] = array[i].start.x;
- result[j ++ ] = array[i].start.y;
- result[j ++ ] = array[i].start.z;
- result[j ++ ] = array[i].end.x;
- result[j ++ ] = array[i].end.y;
- result[j ++ ] = array[i].end.z;
- }
- return result;
- }
- if( firstItem instanceof THREE.Triangle3 ) {
- var result = THREE.Descriptor.NewFloat32Array( array.length * 9, optionalTarget );
- for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
- result[j ++ ] = array[i].a.x;
- result[j ++ ] = array[i].a.y;
- result[j ++ ] = array[i].a.z;
- result[j ++ ] = array[i].b.x;
- result[j ++ ] = array[i].b.y;
- result[j ++ ] = array[i].b.z;
- result[j ++ ] = array[i].c.x;
- result[j ++ ] = array[i].c.y;
- result[j ++ ] = array[i].c.z;
- }
- return result;
- }
- throw new Error( "Unhandled type in ToFlatFloat32Array, type is: " + firstItem.constructor.toString() )
- };
- THREE.Descriptor.ToFlatUint16Array = function( array, optionalTarget ) {
- if( ! array ) {
- return THREE.Descriptor.NewUint16Array( 0, optionalTarget );
- }
- // no conversion necessary
- if( array instanceof Uint16Array ) {
- return array;
- }
- // convert individual items into arrays
- if( ! ( array instanceof Array ) ) {
- array = [ array ];
- }
- if( array.length === 0 ) {
- return THREE.Descriptor.NewUint16Array( 0, optionalTarget );
- }
- var firstItem = array[0];
- if( typeof firstItem == "number" ) {
- var result = THREE.Descriptor.NewUint16Array( array.length, optionalTarget );
- for( var i = 0; i < array.length; i ++ ) {
- result[i] = array[i];
- }
- return result;
- }
- throw new Error( "Unhandled type in ToFlatUint16Array, type is: " + firstItem.constructor.toString() )
- };
- THREE.Descriptor.ToFlatArray = function( elementType, array, optionalTarget ) {
- switch( elementType ) {
- case gl.FLOAT:
- return THREE.Descriptor.ToFlatFloat32Array( array, optionalTarget );
- case gl.UNSIGNED_BYTE:
- return THREE.Descriptor.ToFlatUint8Array( array, optionalTarget );
- case gl.UNSIGNED_SHORT:
- return THREE.Descriptor.ToFlatUint16Array( array, optionalTarget );
- case gl.UNSIGNED_INT:
- return THREE.Descriptor.ToFlatUint32Array( array, optionalTarget );
- case gl.SHORT:
- return THREE.Descriptor.ToFlatInt16Array( array, optionalTarget );
- case gl.INT:
- return THREE.Descriptor.ToFlatInt32Array( array, optionalTarget );
- default:
- throw new Error( "elementType not supported: " + elementType );
- };
- }
- THREE.UniformName = function( name, descriptor, defaultValue ) {
- this.name = name;
- this.descriptor = descriptor;
- this.defaultValue = defaultValue;
- }
- THREE.UniformValue = function( descriptor, optionalValue ) {
- this.descriptor = descriptor;
- this.value = optionalValue;
- }
- THREE.UniformValue.updateMap = function( nameToUniformValues, uniformName, value ) {
- var uniformValue = nameToUniformValues[uniformName.name] || new THREE.UniformValue( uniformName.descriptor );
- uniformValue.value = value;
- nameToUniformValues[uniformName.name] = uniformValue;
- };
- THREE.AttributeName = function ( name, descriptor, defaultValue ) {
- this.name = name;
- this.descriptor = descriptor;
- this.defaultValue = defaultValue;
- };
- THREE.AttributeValue = function( descriptor, optionalArray ) {
- this.descriptor = descriptor;
- this.glBuffer = null;
- this.glBufferLength = -1;
- if( optionalArray ) {
- this.set( optionalArray );
- }
- }
- THREE.AttributeValue.prototype = {
- constructor: THREE.AttributeValue,
- set: function( array ) {
- if( ! this.glBuffer ) {
- this.glBuffer = gl.createBuffer();
- }
- var arrayType = ( this.descriptor.isIndex ) ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
- var flatArray = THREE.Descriptor.ToFlatArray( this.descriptor.elementType, array );
- gl.bindBuffer( arrayType, this.glBuffer );
- gl.bufferData( arrayType, flatArray, gl.STATIC_DRAW );
- this.glBufferLength = flatArray.length;
- },
- dispose: function() {
- if( this.glBuffer ) {
- gl.deleteBuffer( this.glBuffer );
- this.glBufferVersion = -1;
- this.glBuffer = null;
- }
- }
- };
- THREE.AttributeValue.updateMap = function( nameToAttributeValues, attributeName, array ) {
- var attributeValue = nameToAttributeValues[attributeName.name] || new THREE.AttributeValue( attributeName.descriptor );
- attributeValue.set( array );
- nameToAttributeValues[attributeName.name] = attributeValue;
- };
- THREE.BatchItem = function ( primitiveType, program, attributes, localUniforms, materialUniforms ) {
- this.program = program;
- this.primitiveType = primitiveType;
- this.attributes = attributes;
- this.localUniforms = localUniforms;
- this.materialUniforms = materialUniforms;
- };
- THREE.BatchItem.prototype = {
- constructor: THREE.BatchItem,
- };
- THREE.ProgramUniform = function( program, uniformName, failUponMissingLocation ) {
- this.program = program;
- this.uniformName = uniformName;
- this.location = gl.getUniformLocation( this.program.glProgram, this.uniformName.name );
- this.textureUnits = null;
- if( ! this.location && failUponMissingLocation ) throw new Error( "can not bind against uniform: " + this.uniformName.name );
- };
- THREE.ProgramUniform.prototype = {
- constructor: THREE.ProgramUniform,
- set: function( value ) {
- var value = value.value || this.uniformName.defaultValue;
- var descriptor = this.uniformName.descriptor;
- if( descriptor.isTexture() ) {
- this.textureUnits = this.textureUnits || [];
- // if this is the first time we load up the texture, we need to set its units.
- while( this.textureUnits.length < value.length ) {
- this.textureUnits.push( program.nextAvailableTextureUnit ++ );
- }
- if( ! descriptor.isArray ) {
- glUniform1i( this.location, this.textureUnits[i] ); // Texture unit 0 is for base images. ???
- gl.activeTexture( gl.TEXTURE0 + this.textureUnits[i] );
- gl.bindTexture( descriptor.elementType, value );
- }
- else {
- _gl.uniform1iv( this.location, this.textureUnits );
- for( var i = 0; i < this.value.length; i ++ ) {
- gl.activeTexture( gl.TEXTURE0 + this.textureUnits[i] );
- gl.bindTexture( descriptor.elementType, value[i] );
- }
- }
- }
- else {
- this._cachedFlatArray = THREE.Descriptor.ToFlatArray( descriptor.elementType, value, this._cachedFlatArray );
- // special handling for matrices, whether arrays or not.
- if( descriptor.elementType === gl.FLOAT && descriptor.itemSize === 9 ) {
- gl.uniformMatrix3fv( this.location, false, this._cachedFlatArray );
- return;
- }
- if( descriptor.elementType === gl.FLOAT && descriptor.itemSize === 16 ) {
- gl.uniformMatrix4fv( this.location, false, this._cachedFlatArray );
- return;
- }
- var uniformFunctionName = 'uniform' + descriptor.itemSize;
- switch( descriptor.elementType ) {
- case gl.FLOAT:
- uniformFunctionName += 'f';
- break;
- case gl.INT:
- uniformFunctionName += 'i';
- break;
- default:
- throw new Error( "elementType is not supported:" + descriptor.elementType );
- };
- if( descriptor.isArray ) {
- gl[uniformFunctionName + 'v']( this.location, this._cachedFlatArray );
- }
- else {
- switch( descriptor.itemSize ) {
- case 1:
- gl[ uniformFunctionName ]( this.location, this._cachedFlatArray[0] );
- break;
- case 2:
- gl[ uniformFunctionName ]( this.location, this._cachedFlatArray[0], this._cachedFlatArray[1] );
- break;
- case 3:
- gl[ uniformFunctionName ]( this.location, this._cachedFlatArray[0], this._cachedFlatArray[1], this._cachedFlatArray[2] );
- break;
- case 4:
- gl[ uniformFunctionName ]( this.location, this._cachedFlatArray[0], this._cachedFlatArray[1], this._cachedFlatArray[2], this._cachedFlatArray[3] );
- break;
- default:
- throw new Error( "itemSize is not supported: " + descriptor.itemSize );
- }
- }
- this.previousValue = value;
- }
- }
- };
- THREE.ProgramAttribute = function( program, attributeName, failUponMissingLocation ) {
- this.program = program;
- this.attributeName = attributeName;
- this.location = gl.getAttribLocation( this.program.glProgram, this.attributeName.name );
- if( ( this.location < 0 ) && failUponMissingLocation ) throw new Error( "can not bind against attribute: " + this.attributeName.name );
- };
- THREE.ProgramAttribute.prototype = {
- constructor: THREE.ProgramAttribute,
- bind: function( attributeValue ) {
- // can not bind buffers that do not exist in the program
- if( this.location < 0 ) return;
- var descriptor = this.attributeName.descriptor;
- var name = descriptor.name;
- if( descriptor.name !== attributeValue.descriptor.name ) throw new Error( "ProgramAttribute: names do not match: " + descriptor.name + ", " + attributeValue.descriptor.name );
- // index buffers are used exclusively for drawElement calls, thus should not be bound in this fashion.
- if( name === 'index' ) return;
- if( ! attributeValue ) throw new Error( "Program: can not bind attribute to attribute, no matching attribute: " + name );
- if( ! attributeValue.glBuffer ) throw new Error( "Program: Can not draw attribute as no WebGL attribute exists for: " + name );
- gl.enableVertexAttribArray( this.location );
- gl.bindBuffer( gl.ARRAY_BUFFER, attributeValue.glBuffer );
- gl.vertexAttribPointer( this.location, descriptor.itemSize, descriptor.elementType, false, 0, 0);
- }
- };
- THREE.Program = function( glProgram, uniformNames, attributeNames, failUponMissingLocation ) {
- failUponMissingLocation = failUponMissingLocation || false;
- this.glProgram = glProgram;
- this.nextAvailableTextureUnit = 0;
- this.nameToProgramUniforms = {};
- if( uniformNames ) {
- for( var i = 0; i < uniformNames.length; i ++ ) {
- var uniformName = uniformNames[i];
- this.nameToProgramUniforms[ uniformName.name ] = new THREE.ProgramUniform( this, uniformName, failUponMissingLocation );
- }
- }
- this.nameToProgramAttributes = {};
- if( attributeNames ) {
- for( var i = 0; i < attributeNames.length; i ++ ) {
- var attributeName = attributeNames[i];
- this.nameToProgramAttributes[ attributeName.name ] = new THREE.ProgramAttribute( this, attributeName, failUponMissingLocation );
- }
- }
- };
- THREE.Program.prototype = {
- constructor: THREE.Program,
- bindAttributes: function( nameToAttributeValues ) {
- // bind all attributes that have well defined locations, error if no matching attribute can be found.
- for( var name in this.nameToProgramAttributes ) {
- if ( this.nameToProgramAttributes.hasOwnProperty( name ) ) {
- var programAttribute = this.nameToProgramAttributes[ name ];
- var attributeValue = nameToAttributeValues[ name ];
- if( attributeValue ) {
- console.log( "binding attribute: " + name + " attributeValue.length: " + attributeValue.glBufferLength );
- programAttribute.bind( attributeValue );
- }
- }
- }
- },
- setUniforms: function( nameToUniformValues ) {
- for( var name in this.nameToProgramUniforms ) {
- if ( this.nameToProgramUniforms.hasOwnProperty( name ) ) {
- var programUniform = this.nameToProgramUniforms[ name ];
- var uniformValue = nameToUniformValues[ name ];
- if( uniformValue ) {
- programUniform.set( uniformValue );
- }
- }
- }
- },
- drawPrimitives: function( primitiveType, nameToAttributeValues ) {
- var positionAttributeValue = nameToAttributeValues[ 'position' ];
- var indexAttributeValue = nameToAttributeValues[ 'index' ];
- if( ! indexAttributeValue ) {
- console.log( primitiveType, positionAttributeValue );
- var itemCount = positionAttributeValue.glBufferLength;
- switch( primitiveType ) {
- case gl.TRIANGLES:
- itemCount = itemCount / 3;
- break;
- case gl.TRIANGLE_STRIP:
- case gl.TRIANGLE_FAN:
- itemCount = itemCount - 2;
- break;
- case gl.POINTS:
- break;
- case gl.LINES:
- itemCount = itemCount / 2;
- break;
- case gl.LINE_LOOP:
- break;
- case gl.LINE_STRIP:
- itemCount = itemCount - 1;
- break;
- default:
- throw new Error( "should not get here, unknown primitiveType: " + primitiveType );
- }
- gl.drawArrays( primitiveType, 0, itemCount );
- }
- else {
- var indexDescriptor = indexAttributeValue.descriptor;
- if( ! indexAttributeValue.glBuffer ) throw new Error( "Can not draw indexed attribute as no WebGL attribute exists for: index" );
- gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, indexAttributeValue.glBuffer );
- gl.drawElements( primitiveType, indexAttributeValue.glBufferLength, indexDescriptor.elementType, 0 );
- }
- },
- dispose: function() {
- if( this.program ) {
- gl.deleteProgram( this.program );
- this.program = null;
- }
- }
- };
- THREE.DepthRenderer = function() {
- this.program = new THREE.Program( depthShaderProgram, attributeNamesToLocations, uniformNamesToLocations );
- };
- THREE.DepthRenderer.prototype = {
- constructor: THREE.DepthRenderer,
- render: function( batchItems, uniformSet, renderTarget ) {
- gl.useProgram( this.program.glProgram );
- this.program.bindUniforms( uniformSet );
- for( var i = 0; i < batchItems.length; i ++ ) {
- var batchItem = batchItems[i];
- // update uniforms
- batchItem.bufferSet.update();
- this.program.bindAttributes( batchItem.buffers );
- this.program.setUniforms( batchItem.localUniforms );
- batchItem.bufferSet.draw();
- }
- }
- };
- THREE.SimpleRenderer = function() {
- };
- THREE.SimpleRenderer.prototype = {
- constructor: THREE.SimpleRenderer,
- render: function( batchItems, globalUniformValues, renderTarget ) {
- for( var i = 0; i < batchItems.length; i ++ ) {
- var batchItem = batchItems[i];
- var program = batchItem.program;
- gl.useProgram( program.glProgram );
- program.setUniforms( globalUniformValues );
- program.bindAttributes( batchItem.attributeValues );
- if( batchItem.localUniformValues ) {
- program.setUniforms( batchItem.localUniformValues );
- }
- if( batchItem.materialUniformValues ) {
- program.setUniforms( batchItem.materialUniformValues );
- }
- program.drawPrimitives( batchItem.primitiveType, batchItem.attributeValues );
- }
- }
- };
- THREE.DescriptorLibrary = {};
- THREE.UniformLibrary = {};
- THREE.AttributeLibrary = {};
- THREE.LibraryUtils = function() {
- };
- THREE.LibraryUtils.addToNamedMap = function( map, array ) {
- map = map || {};
- for( var i = 0; i < array.length; i ++ ) {
- map[ array[i].name ] = array[i];
- }
- return map;
- };
- THREE.LibraryUtils.resolveDescriptors = function( map, nameArray, throwOnNotFound ) {
- var descriptorArray = [];
- for( var i = 0; i < nameArray.length; i ++ ) {
- var descriptor = map[ nameArray[i] ];
- if( ! descriptor ) {
- if( throwOnNotFound ) {
- throw new Error( "can not find a descriptor named: " + nameArray[i] );
- }
- }
- else {
- descriptorArray.push( descriptor );
- }
- }
- return descriptorArray;
- };
- THREE.LibraryUtils.initSharedLibraries = function( gl ) {
- THREE.DescriptorLibrary.Float1 = new THREE.Descriptor( gl.FLOAT, 1, false );
- THREE.DescriptorLibrary.Float2 = new THREE.Descriptor( gl.FLOAT, 2, false );
- THREE.DescriptorLibrary.Float3 = new THREE.Descriptor( gl.FLOAT, 3, false, false );
- THREE.DescriptorLibrary.Float4 = new THREE.Descriptor( gl.FLOAT, 4, false, false );
- THREE.DescriptorLibrary.Float9 = new THREE.Descriptor( gl.FLOAT, 9, false, false );
- THREE.DescriptorLibrary.Float16 = new THREE.Descriptor( gl.FLOAT, 16, false, false );
- THREE.DescriptorLibrary.Float1Array = new THREE.Descriptor( gl.FLOAT, 1, true, false );
- THREE.DescriptorLibrary.Float2Array = new THREE.Descriptor( gl.FLOAT, 2, true, false );
- THREE.DescriptorLibrary.Float3Array = new THREE.Descriptor( gl.FLOAT, 3, true, false );
- THREE.DescriptorLibrary.Float4Array = new THREE.Descriptor( gl.FLOAT, 4, true, false );
- THREE.DescriptorLibrary.Float4Array = new THREE.Descriptor( gl.FLOAT, 16, true, false );
- THREE.DescriptorLibrary.Int1 = new THREE.Descriptor( gl.INT, 1, false, false );
- THREE.DescriptorLibrary.Int2 = new THREE.Descriptor( gl.INT, 2, false, false );
- THREE.DescriptorLibrary.Int3 = new THREE.Descriptor( gl.INT, 3, false, false );
- THREE.DescriptorLibrary.Int1Array = new THREE.Descriptor( gl.INT, 1, true, false );
- THREE.DescriptorLibrary.Int1Index = new THREE.Descriptor( gl.INT, 1, true, true );
- THREE.DescriptorLibrary.Ushort1Index = new THREE.Descriptor( gl.UNSIGNED_SHORT, 1, true, true );
- THREE.LibraryUtils.addToNamedMap( THREE.UniformLibrary, [
- new THREE.UniformName( 'projectionMatrix', THREE.DescriptorLibrary.Float16, new THREE.Matrix4() ),
- new THREE.UniformName( 'viewMatrix', THREE.DescriptorLibrary.Float16, new THREE.Matrix4() ),
- new THREE.UniformName( 'modelMatrix', THREE.DescriptorLibrary.Float16, new THREE.Matrix4() ),
- new THREE.UniformName( 'modelViewMatrix', THREE.DescriptorLibrary.Float16, new THREE.Matrix4() ),
- new THREE.UniformName( 'normalMatrix', THREE.DescriptorLibrary.Float9, new THREE.Matrix3() ),
- new THREE.UniformName( 'cameraPosition', THREE.DescriptorLibrary.Float3, new THREE.Vector3() ),
- new THREE.UniformName( 'fogColor', THREE.DescriptorLibrary.Float3, new THREE.Vector3( 1, 1, 1 ) ),
- new THREE.UniformName( 'fogDensity', THREE.DescriptorLibrary.Float1, 0.1 ),
- new THREE.UniformName( 'fogNear', THREE.DescriptorLibrary.Float1, 0.1 ),
- new THREE.UniformName( 'fogFar', THREE.DescriptorLibrary.Float1, 10000 ),
- new THREE.UniformName( 'reflectivity', THREE.DescriptorLibrary.Float1, 0.3 ),
- new THREE.UniformName( 'flipEnvMap', THREE.DescriptorLibrary.Float1, 1.0 ),
- new THREE.UniformName( 'combine', THREE.DescriptorLibrary.Int1, 1 ),
- new THREE.UniformName( 'useRefract', THREE.DescriptorLibrary.Int1, 1 ),
- new THREE.UniformName( 'refractionRatio', THREE.DescriptorLibrary.Float1, 0.3 ),
- new THREE.UniformName( 'opacity', THREE.DescriptorLibrary.Float1, 1.0 ),
- new THREE.UniformName( 'diffuse', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
- new THREE.UniformName( 'ambient', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
- new THREE.UniformName( 'emissive', THREE.DescriptorLibrary.Float3, new THREE.Vector3( 0, 0, 0 ) ),
- new THREE.UniformName( 'specular', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
- new THREE.UniformName( 'shininess', THREE.DescriptorLibrary.Float1, 50 ),
- new THREE.UniformName( 'size', THREE.DescriptorLibrary.Float1, 1.0 ),
- new THREE.UniformName( 'scalar', THREE.DescriptorLibrary.Float1, 1.0 ),
- new THREE.UniformName( 'psColor', THREE.DescriptorLibrary.Float3, new THREE.Vector3( 1, 1, 1 ) ),
- new THREE.UniformName( 'dashSize', THREE.DescriptorLibrary.Float1, 1.0 ),
- new THREE.UniformName( 'scale', THREE.DescriptorLibrary.Float1, 1.0 ),
- new THREE.UniformName( 'totalSize', THREE.DescriptorLibrary.Float1, 10.0 ),
- new THREE.UniformName( 'mNear', THREE.DescriptorLibrary.Float1, 1.0 ),
- new THREE.UniformName( 'mFar', THREE.DescriptorLibrary.Float1, 2000.0 ),
- new THREE.UniformName( 'scale', THREE.DescriptorLibrary.Float1, 1.0 ),
- new THREE.UniformName( 'enableAO', THREE.DescriptorLibrary.Int1, 0 ),
- new THREE.UniformName( 'enableDiffuse', THREE.DescriptorLibrary.Int1, 0 ),
- new THREE.UniformName( 'enableSpecular', THREE.DescriptorLibrary.Int1, 0 ),
- new THREE.UniformName( 'enableReflection', THREE.DescriptorLibrary.Int1, 0 ),
- new THREE.UniformName( 'enableDisplacement', THREE.DescriptorLibrary.Int1, 0 ),
- new THREE.UniformName( 'uNormalScale', THREE.DescriptorLibrary.Float2, new THREE.Vector2( 1, 1 ) ),
- new THREE.UniformName( 'uDisplacementBias', THREE.DescriptorLibrary.Float1, 0.0 ),
- new THREE.UniformName( 'uDisplacementScale', THREE.DescriptorLibrary.Float1, 1.0 ),
- new THREE.UniformName( 'uDiffuseColor', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
- new THREE.UniformName( 'uSpecularColor', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
- new THREE.UniformName( 'uAmbientColor', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
- new THREE.UniformName( 'uShininess', THREE.DescriptorLibrary.Float1, 30 ),
- new THREE.UniformName( 'uOpacity', THREE.DescriptorLibrary.Float1, 30 ),
- new THREE.UniformName( 'directionalLightDirection', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 1, 0, 0 ) ),
- new THREE.UniformName( 'directionalLightColor', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 0xffffff ) ),
- new THREE.UniformName( 'spotLightPosition', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 0, 0, 0 ) ),
- new THREE.UniformName( 'spotLightDirection', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 1, 0, 0 ) ),
- new THREE.UniformName( 'spotLightColor', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 0xffffff ) ),
- new THREE.UniformName( 'spotLightDistance', THREE.DescriptorLibrary.Float1Array, 10000 ),
- new THREE.UniformName( 'spotLightExponent', THREE.DescriptorLibrary.Float1Array, 10000 ),
- new THREE.UniformName( 'spotLightAngleCos', THREE.DescriptorLibrary.Float1Array, 10000 ),
- new THREE.UniformName( 'pointLightPosition', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 0, 0, 0 ) ),
- new THREE.UniformName( 'pointLightDistance', THREE.DescriptorLibrary.Float1Array, 10000 ),
- new THREE.UniformName( 'pointLightColor', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 0xffffff ) ),
- new THREE.UniformName( 'hemisphereLightDirection', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 1, 0, 0 ) ),
- new THREE.UniformName( 'hemisphereLightSkyColor', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 0xffffff ) ),
- new THREE.UniformName( 'hemisphereLightGroundColor', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 0xffffff ) ),
- new THREE.UniformName( 'map', THREE.DescriptorLibrary.Texture2D ),
- new THREE.UniformName( 'specularMap', THREE.DescriptorLibrary.Texture2D ),
- new THREE.UniformName( 'bumpMap', THREE.DescriptorLibrary.Texture2D ),
- new THREE.UniformName( 'normalMap', THREE.DescriptorLibrary.Texture2D ),
- new THREE.UniformName( 'lightMap', THREE.DescriptorLibrary.Texture2D ),
- new THREE.UniformName( 'shadowMap', THREE.DescriptorLibrary.Texture2DArray ),
- new THREE.UniformName( 'boneTexture', THREE.DescriptorLibrary.Texture2D ),
- new THREE.UniformName( 'envMap', THREE.DescriptorLibrary.TextureCubeMap )
- ] );
- THREE.LibraryUtils.addToNamedMap( THREE.AttributeLibrary, [
- new THREE.AttributeName( 'index', THREE.DescriptorLibrary.Ushort1Index, 0 ),
- new THREE.AttributeName( 'position', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
- new THREE.AttributeName( 'normal', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 1, 0, 0 ) ),
- new THREE.AttributeName( 'uv', THREE.DescriptorLibrary.Float2Array, new THREE.Vector2() ),
- new THREE.AttributeName( 'uv2', THREE.DescriptorLibrary.Float2Array, new THREE.Vector2() ),
- new THREE.AttributeName( 'color', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 1, 1, 1 ) ),
- new THREE.AttributeName( 'morphTarget0', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
- new THREE.AttributeName( 'morphTarget1', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
- new THREE.AttributeName( 'morphTarget2', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
- new THREE.AttributeName( 'morphTarget3', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
- new THREE.AttributeName( 'morphNormal0', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
- new THREE.AttributeName( 'morphNormal1', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
- new THREE.AttributeName( 'morphNormal2', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
- new THREE.AttributeName( 'morphNormal3', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
- new THREE.AttributeName( 'skinIndex', THREE.DescriptorLibrary.Float4Array, new THREE.Vector3() ),
- new THREE.AttributeName( 'skinWeight', THREE.DescriptorLibrary.Float4Array, new THREE.Vector3() )
- ] );
- };
- THREE.ShaderLibrary = function() {
- this.shaderNameToContents = {};
- }
- THREE.ShaderLibrary.addShader = function( shaderName, contents ) {
- this.shaderNameToContents[shaderName] = contents;
- }
- THREE.ShaderLibrary.combineShader = function( orderedShaderNames ) {
- var result = "";
- for( var i = 0; i < orderedShaderNames.length; i ++ ) {
- var contents = this.shaderNameToContents[ orderedShaderNames[i] ];
- if( ! contents ) {
- throw new Error( "no shader named: " + orderedShaderNames[i] );
- }
- result += contents + '\n';
- }
- return result;
- }
- THREE.SceneTraversalInOrder = function() {
- var nodeStack = [];
- return function( scene, visitorFunction ) {
- nodeStack.push( scene );
- while( nodeStack.length > 0 ) {
- var node = nodeStack.pop();
- for( var i = 0; i < node.children.length; i ++ ) {
- nodeStack.push( node.children[i] );
- }
- visitorFunction( node );
- }
- }
- }();
- THREE.SceneTraversalPreOrder = function() {
- var nodeStack = [];
- return function( scene, visitorFunction ) {
- nodeStack.push( scene );
- while( nodeStack.length > 0 ) {
- var node = nodeStack.pop();
- visitorFunction( node );
- for( var i = 0; i < node.children.length; i ++ ) {
- nodeStack.push( node.children[i] );
- }
- }
- }
- }();
- var getRenderData = function( sceneItem ) {
- sceneItem.__renderData = sceneItem.__renderData || {};
- return sceneItem.__renderData;
- };
- THREE.SceneUpdater = function( traversal ) {
- this.traversal = traversal || THREE.SceneTraversalInOrder;
- };
- THREE.SceneUpdater.prototype = {
- constructor: THREE.SceneUpdater,
- updateCamera: function( camera ) {
- camera.updateProjectionMatrix();
- camera.updateMatrix();
- camera.updateMatrixWorld();
- var projectionMatrix = camera.projectionMatrix;
- var worldMatrixInverse = new THREE.Matrix4().getInverse( camera.matrixWorld, true );
- projectionMatrix.multiplyMatrices( projectionMatrix, worldMatrixInverse );
- var renderData = getRenderData( camera );
- var uniformValues = ( renderData.uniformValues = renderData.uniformValues || {} );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'projectionMatrix' ], projectionMatrix );
- },
- updateLights: function( scene ) {
- var dirDirections = [];
- var dirColors = [];
- var spotPositions = [];
- var spotDirections = [];
- var spotColors = [];
- var spotDistances = [];
- var spotExponents = [];
- var spotAngleCos = [];
- var pointPositions = [];
- var pointDistances = [];
- var pointColors = [];
- var hemiDirections = [];
- var hemiSkyColor = [];
- var hemiGroundColor = [];
- this.traversal( scene, function( node ) {
- if( node instanceof THREE.Light ) {
- if ( ! node.visible ) return;
- if( node instanceof THREE.DirectionalLight ) {
- var direction = node.getWorldDirection();
- if( direction.x == 0 && direction.y == 0 && direction.z == 0 ) {
- return;
- }
- dirDirections.push( direction );
- dirColors.push( node.color.clone().multiplyScalar( node.intensity ) );
- }
- else if( node instanceof THREE.SpotLight ) {
- var direction = node.getWorldDirection();
- if( direction.x == 0 && direction.y == 0 && direction.z == 0 ) {
- return;
- }
- spotPositions.push( new THREE.Vector3().setFromMatrixPosition( node.matrixWorld ) );
- spotDirections.push( direction );
- spotColors.push( node.color.clone().multiplyScalar( node.intensity ) );
- spotDistances.push( node.distance );
- spotExponents.push( node.exponent );
- spotCosAngle.push( Math.cos( THREE.Math.degToRad( node.angle ) ) );
- }
- else if( node instanceof THREE.PointLight ) {
- pointPositions.push( new THREE.Vector3().setFromMatrixPosition( node.matrixWorld ) );
- pointDistances.push( node.distance );
- pointColors.push( node.color.clone().multiplyScalar( node.intensity ) );
- } else if( node instanceof THREE.HemisphereLight ) {
- hemiDirections.push( node.getWorldDirection() );
- hemiSkyColors.push( node.color.clone().multiplyScalar( node.intensity ) );
- hemiGroundColors.push( node.groundColor.clone().multiplyScalar( node.intensity ) );
- }
- }
- });
- var renderData = getRenderData( scene );
- var uniformValues = renderData.uniformValues = renderData.uniformValues || {};
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'directionalLightDirection' ], dirColors );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'directionalLightColor' ], dirColors );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightPosition' ], spotPositions );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightDirection' ], spotDirections );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightColor' ], spotColors );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightDistance' ], spotDistances );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightExponent' ], spotExponents );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightAngleCos' ], spotAngleCos );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'pointLightPosition' ], pointPositions );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'pointLightDistance' ], pointDistances );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'pointLightColor' ], pointColors );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'hemisphereLightDirection' ], hemiDirections );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'hemisphereLightSkyColor' ], hemiSkyColor );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'hemisphereLightGroundColor' ], hemiGroundColor );
- // TODO handle previously used lights that are no lnoger visible.
- },
- // TODO: Handle programs separately from conversion of meshes into attributes and uniforms if possible.
- updateMeshes: function( scene ) {
- // TODO: optmize this by getting rid of intermediate arrays
- var flattenGeometry = function( attributeValues, geometry ) {
- var positions = [];
- var vertexNormals = [];
- var vertexColors = [];
- var vertexUvs = [];
- for( var i = 0, il = geometry.faces.length; i < il; i ++ ) {
- var f = geometry.faces[i];
- positions.push( geometry.vertices[f.a], geometry.vertices[f.b], geometry.vertices[f.c] );
- if( f.vertexNormals && f.vertexNormals.length === 3 ) {
- vertexNormals.push( f.vertexNormals[0], f.vertexNormals[1], f.vertexNormals[2] );
- }
- else if( f.normal ) {
- vertexNormals.push( f.normal, f.normal, f.normal );
- }
- if( f.vertexColors && f.vertexColors.length === 3 ) {
- vertexColors.push( f.vertexColors[0], f.vertexColors[1], f.vertexColors[2] )
- }
- else if( f.normal ) {
- vertexColors.push( f.color, f.color, f.color );
- }
- if( geometry.faceVertexUvs[0] ) {
- var faceUvs = geometry.faceVertexUvs[0][i];
- vertexUvs.push( faceUvs[0], faceUvs[1], faceUvs[2] );
- }
- }
- THREE.AttributeValue.updateMap( attributeValues, THREE.AttributeLibrary[ 'position' ], positions );
- if( vertexNormals.length = positions.length ) {
- THREE.AttributeValue.updateMap( attributeValues, THREE.AttributeLibrary[ 'normal' ], vertexNormals );
- }
- if( vertexColors.length = positions.length ) {
- THREE.AttributeValue.updateMap( attributeValues, THREE.AttributeLibrary[ 'color' ], vertexColors );
- }
- if( vertexUvs.length = positions.length ) {
- THREE.AttributeValue.updateMap( attributeValues, THREE.AttributeLibrary[ 'uv' ], vertexUvs );
- }
- }
- this.traversal( scene, function( node ) {
- if( node instanceof THREE.Mesh ) {
- var renderData = getRenderData( node );
- // update uniforms
- node.updateMatrix();
- node.updateMatrixWorld();
- var uniformValues = renderData.uniformValues = renderData.uniformValues || {};
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'modelViewMatrix' ], node.matrixWorld );
- var normalMatrix = new THREE.Matrix3().getNormalMatrix( node.matrixWorld );
- THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'normalMatrix' ], normalMatrix );
- // update attributes
- var attributeValues = renderData.attributeValues = renderData.attributeValues || {};
- if( node.geometry instanceof THREE.Geometry ) {
- flattenGeometry( attributeValues, node.geometry );
- }
- else if( node.geometry instanceof THREE.BufferGeometry ) {
- for( var attributeName in THREE.AttributeLibrary ) {
- if ( THREE.AttributeLibrary.hasOwnProperty( attributeName ) ) {
- if( node.geometry.attributes[ attributeName ] ) {
- THREE.AttributeValue.updateMap( attributeValues, THREE.AttributeLibrary[ attributeName ], node.geometry.attributes[ attributeName ].array );
- }
- }
- }
- }
- else {
- throw new Error( "should not get here, unrecognized Geometry type" );
- }
- }
- });
- },
- update: function( scene, camera ) {
- this.updateCamera( camera );
- this.updateLights( scene );
- this.updateMeshes( scene );
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement