Advertisement
Guest User

Untitled

a guest
May 24th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.97 KB | None | 0 0
  1. THREE.Assert = function( mustBeTrue, message ) {
  2. if( ! mustBeTrue ) throw new Error( message | "Assertion failure." );
  3. }
  4.  
  5. THREE.Descriptor = function ( elementType, itemSize, isArray, isIndex ) {
  6.  
  7. THREE.Descriptor.AssertElementType( elementType );
  8.  
  9. this.itemSize = itemSize;
  10. this.elementType = elementType;
  11. this.isArray = isArray;
  12. this.isIndex = isIndex;
  13.  
  14. };
  15.  
  16. THREE.Descriptor.prototype = {
  17.  
  18. constructor: THREE.Descriptor,
  19.  
  20. createArray: function( numItems ) {
  21.  
  22. return THREE.Descriptor.CreateArray( this.elementType, numberItems );
  23.  
  24. },
  25.  
  26. isTexture: function() {
  27.  
  28. // TODO: Add more types here.
  29. return ( this.elementType == gl.TEXTURE_2D || this.elementType == gl.TEXTURE_2D_ARRAY || this.elementType == gl.TEXTURE_CUBE_MAP );
  30.  
  31. },
  32.  
  33. equals: function ( d ) {
  34.  
  35. return ( d.itemSize === this.itemSize ) && ( d.elementType === this.elementType ) && ( d.isArray == this.isArray ) && ( d.isIndex == this.isIndex );
  36.  
  37. },
  38. };
  39.  
  40.  
  41. THREE.Descriptor.AssertElementType = function( elementType ) {
  42.  
  43. switch( elementType ) {
  44.  
  45. case gl.FLOAT:
  46. case gl.UNSIGNED_BYTE:
  47. case gl.UNSIGNED_SHORT:
  48. case gl.UNSIGNED_INT:
  49. case gl.SHORT:
  50. case gl.INT:
  51. case gl.TEXTURE_2D:
  52. case gl.TEXTURE_2D_ARRAY:
  53. case gl.TEXTURE_CUBE_MAP:
  54. return true;
  55.  
  56. default:
  57. throw new Error( "AttributeName elementType not supported: " + elementType );
  58. };
  59.  
  60. }
  61. THREE.Descriptor.GetArrayType = function( elementType ) {
  62. switch( elementType ) {
  63. case gl.FLOAT:
  64. return Float32Array;
  65. case gl.UNSIGNED_BYTE:
  66. return Uint8Array;
  67. case gl.UNSIGNED_SHORT:
  68. return Uint16Array;
  69. case gl.UNSIGNED_INT:
  70. return Uint32Array;
  71. case gl.SHORT:
  72. return Int16Array;
  73. case gl.INT:
  74. return Int32Array;
  75. default:
  76. throw new Error( "AttributeName elementType not supported: " + this.elementType );
  77. }
  78. }
  79.  
  80. THREE.Descriptor.CreateArray = function( elementType, itemSize, optionalTarget ) {
  81.  
  82. var arrayType = THREE.Descriptor.GetArrayType( elementType );
  83. var newLength = numberItems * this.itemSize;
  84.  
  85. if( ( optionalTarget instanceof arrayType ) && optionalTarget.length === newLength ) {
  86. return optionalTarget;
  87. }
  88.  
  89. return new arrayType( newLength );
  90. }
  91.  
  92. THREE.Descriptor.NewFloat32Array = function( newLength, optionalTarget ) {
  93.  
  94. if( ( optionalTarget instanceof Float32Array ) && optionalTarget.length === newLength ) {
  95. return optionalTarget;
  96. }
  97.  
  98. return new Float32Array( newLength );
  99. }
  100.  
  101. THREE.Descriptor.NewUint16Array = function( newLength, optionalTarget ) {
  102.  
  103. if( ( optionalTarget instanceof Uint16Array ) && optionalTarget.length === newLength ) {
  104. return optionalTarget;
  105. }
  106.  
  107. return new Uint16Array( newLength );
  108. }
  109.  
  110. THREE.Descriptor.AssertPrimitiveType = function( primitiveType ) {
  111.  
  112. switch( elementType ) {
  113.  
  114. case gl.POINTS:
  115. case gl.LINES:
  116. case gl.LINE_LOOP:
  117. case gl.LINE_STRIP:
  118. case gl.TRIANGLES:
  119. case gl.TRIANGLE_STRIP:
  120. case gl.TRIANGLE_FAN:
  121. return true;
  122.  
  123. default:
  124. throw new Error( "AttributeName primitiveType not supported: " + elementType );
  125. };
  126.  
  127. }
  128.  
  129. // TODO: move to MathUtils or something else in the ThreeJS math subdirectory?
  130. THREE.Descriptor.ToFlatFloat32Array = function( array, optionalTarget ) {
  131.  
  132. if( ! array ) {
  133.  
  134. return THREE.Descriptor.NewFloat32Array( 0, optionalTarget );
  135.  
  136. }
  137.  
  138. // no conversion necessary
  139. if( array instanceof Float32Array ) {
  140.  
  141. return array;
  142.  
  143. }
  144.  
  145. // convert individual items into arrays
  146. if( ! ( array instanceof Array ) ) {
  147.  
  148. array = [ array ];
  149.  
  150. }
  151.  
  152. if( array.length === 0 ) {
  153.  
  154. return THREE.Descriptor.NewFloat32Array( 0, optionalTarget );
  155.  
  156. }
  157.  
  158. var firstItem = array[0];
  159.  
  160. if( typeof firstItem == "number" ) {
  161.  
  162. var result = THREE.Descriptor.NewFloat32Array( array.length, optionalTarget );
  163.  
  164. for( var i = 0, il = array.length; i < il; i ++ ) {
  165. result[i] = array[i];
  166. }
  167.  
  168. return result;
  169. }
  170.  
  171. // the following checks are ordered in decreasing frequency of usage
  172.  
  173. // TODO: Is there a faster check than repeated "instanceof"? But this is likely not a performance critical check
  174. if( firstItem instanceof THREE.Vector3 ) {
  175.  
  176. var result = THREE.Descriptor.NewFloat32Array( array.length * 3, optionalTarget );
  177.  
  178. for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
  179. result[j ++ ] = array[i].x;
  180. result[j ++ ] = array[i].y;
  181. result[j ++ ] = array[i].z;
  182. }
  183.  
  184. return result;
  185.  
  186. }
  187.  
  188. if( firstItem instanceof THREE.Color ) {
  189.  
  190. var result = THREE.Descriptor.NewFloat32Array( array.length * 3, optionalTarget );
  191.  
  192. for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
  193. result[j ++ ] = array[i].r;
  194. result[j ++ ] = array[i].g;
  195. result[j ++ ] = array[i].b;
  196. }
  197.  
  198. return result;
  199.  
  200. }
  201.  
  202. if( firstItem instanceof THREE.Vector2 ) {
  203.  
  204. var result = THREE.Descriptor.NewFloat32Array( array.length * 2, optionalTarget );
  205.  
  206. for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
  207. result[j ++ ] = array[i].x;
  208. result[j ++ ] = array[i].y;
  209. }
  210.  
  211. return result;
  212. }
  213.  
  214. if( ( firstItem instanceof THREE.Vector4 ) || ( firstItem instanceof THREE.Quaternion ) ) {
  215.  
  216. var result = THREE.Descriptor.NewFloat32Array( array.length * 4, optionalTarget );
  217.  
  218. for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
  219. result[j ++ ] = array[i].x;
  220. result[j ++ ] = array[i].y;
  221. result[j ++ ] = array[i].z;
  222. result[j ++ ] = array[i].w;
  223. }
  224.  
  225. return result;
  226.  
  227. }
  228.  
  229. if( firstItem instanceof THREE.Matrix3 ) {
  230.  
  231. var result = THREE.Descriptor.NewFloat32Array( array.length * 9, optionalTarget );
  232.  
  233. for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
  234. var e = array[i].elements;
  235. result[j ++ ] = e[0];
  236. result[j ++ ] = e[1];
  237. result[j ++ ] = e[2];
  238. result[j ++ ] = e[3];
  239. result[j ++ ] = e[4];
  240. result[j ++ ] = e[5];
  241. result[j ++ ] = e[6];
  242. result[j ++ ] = e[7];
  243. result[j ++ ] = e[8];
  244. }
  245.  
  246. return result;
  247.  
  248. }
  249.  
  250. if( firstItem instanceof THREE.Matrix4 ) {
  251.  
  252. var result = THREE.Descriptor.NewFloat32Array( array.length * 16, optionalTarget );
  253.  
  254. for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
  255. var e = array[i].elements;
  256. result[j ++ ] = e[0];
  257. result[j ++ ] = e[1];
  258. result[j ++ ] = e[2];
  259. result[j ++ ] = e[3];
  260. result[j ++ ] = e[4];
  261. result[j ++ ] = e[5];
  262. result[j ++ ] = e[6];
  263. result[j ++ ] = e[7];
  264. result[j ++ ] = e[8];
  265. result[j ++ ] = e[9];
  266. result[j ++ ] = e[10];
  267. result[j ++ ] = e[11];
  268. result[j ++ ] = e[12];
  269. result[j ++ ] = e[13];
  270. result[j ++ ] = e[14];
  271. result[j ++ ] = e[15];
  272. }
  273.  
  274. return result;
  275.  
  276. }
  277.  
  278. if( firstItem instanceof THREE.Plane ) {
  279.  
  280. var result = THREE.Descriptor.NewFloat32Array( array.length * 4, optionalTarget );
  281.  
  282. for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
  283. result[j ++ ] = array[i].normal.x;
  284. result[j ++ ] = array[i].normal.y;
  285. result[j ++ ] = array[i].normal.z;
  286. result[j ++ ] = array[i].constant;
  287. }
  288.  
  289. return result;
  290.  
  291. }
  292.  
  293. if( firstItem instanceof THREE.Line3 ) {
  294.  
  295. var result = THREE.Descriptor.NewFloat32Array( array.length * 6, optionalTarget );
  296.  
  297. for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
  298. result[j ++ ] = array[i].start.x;
  299. result[j ++ ] = array[i].start.y;
  300. result[j ++ ] = array[i].start.z;
  301. result[j ++ ] = array[i].end.x;
  302. result[j ++ ] = array[i].end.y;
  303. result[j ++ ] = array[i].end.z;
  304. }
  305.  
  306. return result;
  307.  
  308. }
  309.  
  310. if( firstItem instanceof THREE.Triangle3 ) {
  311.  
  312. var result = THREE.Descriptor.NewFloat32Array( array.length * 9, optionalTarget );
  313.  
  314. for( var i = 0, il = array.length, j = 0; i < il; i ++ ) {
  315. result[j ++ ] = array[i].a.x;
  316. result[j ++ ] = array[i].a.y;
  317. result[j ++ ] = array[i].a.z;
  318. result[j ++ ] = array[i].b.x;
  319. result[j ++ ] = array[i].b.y;
  320. result[j ++ ] = array[i].b.z;
  321. result[j ++ ] = array[i].c.x;
  322. result[j ++ ] = array[i].c.y;
  323. result[j ++ ] = array[i].c.z;
  324. }
  325.  
  326. return result;
  327.  
  328. }
  329.  
  330. throw new Error( "Unhandled type in ToFlatFloat32Array, type is: " + firstItem.constructor.toString() )
  331. };
  332.  
  333. THREE.Descriptor.ToFlatUint16Array = function( array, optionalTarget ) {
  334.  
  335. if( ! array ) {
  336.  
  337. return THREE.Descriptor.NewUint16Array( 0, optionalTarget );
  338.  
  339. }
  340.  
  341. // no conversion necessary
  342. if( array instanceof Uint16Array ) {
  343.  
  344. return array;
  345.  
  346. }
  347.  
  348. // convert individual items into arrays
  349. if( ! ( array instanceof Array ) ) {
  350.  
  351. array = [ array ];
  352.  
  353. }
  354.  
  355. if( array.length === 0 ) {
  356.  
  357. return THREE.Descriptor.NewUint16Array( 0, optionalTarget );
  358.  
  359. }
  360.  
  361.  
  362. var firstItem = array[0];
  363.  
  364. if( typeof firstItem == "number" ) {
  365.  
  366. var result = THREE.Descriptor.NewUint16Array( array.length, optionalTarget );
  367.  
  368. for( var i = 0; i < array.length; i ++ ) {
  369. result[i] = array[i];
  370. }
  371.  
  372. return result;
  373. }
  374.  
  375. throw new Error( "Unhandled type in ToFlatUint16Array, type is: " + firstItem.constructor.toString() )
  376.  
  377. };
  378.  
  379. THREE.Descriptor.ToFlatArray = function( elementType, array, optionalTarget ) {
  380.  
  381. switch( elementType ) {
  382.  
  383. case gl.FLOAT:
  384. return THREE.Descriptor.ToFlatFloat32Array( array, optionalTarget );
  385. case gl.UNSIGNED_BYTE:
  386. return THREE.Descriptor.ToFlatUint8Array( array, optionalTarget );
  387. case gl.UNSIGNED_SHORT:
  388. return THREE.Descriptor.ToFlatUint16Array( array, optionalTarget );
  389. case gl.UNSIGNED_INT:
  390. return THREE.Descriptor.ToFlatUint32Array( array, optionalTarget );
  391. case gl.SHORT:
  392. return THREE.Descriptor.ToFlatInt16Array( array, optionalTarget );
  393. case gl.INT:
  394. return THREE.Descriptor.ToFlatInt32Array( array, optionalTarget );
  395.  
  396. default:
  397. throw new Error( "elementType not supported: " + elementType );
  398. };
  399.  
  400. }
  401.  
  402.  
  403.  
  404.  
  405.  
  406. THREE.UniformName = function( name, descriptor, defaultValue ) {
  407.  
  408. this.name = name;
  409. this.descriptor = descriptor;
  410. this.defaultValue = defaultValue;
  411.  
  412. }
  413.  
  414. THREE.UniformValue = function( descriptor, optionalValue ) {
  415.  
  416. this.descriptor = descriptor;
  417. this.value = optionalValue;
  418. }
  419.  
  420. THREE.UniformValue.updateMap = function( nameToUniformValues, uniformName, value ) {
  421. var uniformValue = nameToUniformValues[uniformName.name] || new THREE.UniformValue( uniformName.descriptor );
  422. uniformValue.value = value;
  423. nameToUniformValues[uniformName.name] = uniformValue;
  424. };
  425.  
  426.  
  427. THREE.AttributeName = function ( name, descriptor, defaultValue ) {
  428.  
  429. this.name = name;
  430. this.descriptor = descriptor;
  431. this.defaultValue = defaultValue;
  432.  
  433. };
  434.  
  435. THREE.AttributeValue = function( descriptor, optionalArray ) {
  436.  
  437. this.descriptor = descriptor;
  438. this.glBuffer = null;
  439. this.glBufferLength = -1;
  440.  
  441. if( optionalArray ) {
  442.  
  443. this.set( optionalArray );
  444.  
  445. }
  446.  
  447. }
  448.  
  449.  
  450. THREE.AttributeValue.prototype = {
  451.  
  452. constructor: THREE.AttributeValue,
  453.  
  454. set: function( array ) {
  455.  
  456. if( ! this.glBuffer ) {
  457.  
  458. this.glBuffer = gl.createBuffer();
  459.  
  460. }
  461.  
  462. var arrayType = ( this.descriptor.isIndex ) ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
  463. var flatArray = THREE.Descriptor.ToFlatArray( this.descriptor.elementType, array );
  464.  
  465. gl.bindBuffer( arrayType, this.glBuffer );
  466. gl.bufferData( arrayType, flatArray, gl.STATIC_DRAW );
  467.  
  468. this.glBufferLength = flatArray.length;
  469. },
  470.  
  471. dispose: function() {
  472.  
  473. if( this.glBuffer ) {
  474.  
  475. gl.deleteBuffer( this.glBuffer );
  476. this.glBufferVersion = -1;
  477. this.glBuffer = null;
  478.  
  479. }
  480.  
  481. }
  482.  
  483. };
  484.  
  485. THREE.AttributeValue.updateMap = function( nameToAttributeValues, attributeName, array ) {
  486. var attributeValue = nameToAttributeValues[attributeName.name] || new THREE.AttributeValue( attributeName.descriptor );
  487. attributeValue.set( array );
  488. nameToAttributeValues[attributeName.name] = attributeValue;
  489. };
  490.  
  491.  
  492.  
  493.  
  494.  
  495. THREE.BatchItem = function ( primitiveType, program, attributes, localUniforms, materialUniforms ) {
  496.  
  497. this.program = program;
  498. this.primitiveType = primitiveType;
  499. this.attributes = attributes;
  500. this.localUniforms = localUniforms;
  501. this.materialUniforms = materialUniforms;
  502.  
  503. };
  504.  
  505. THREE.BatchItem.prototype = {
  506.  
  507. constructor: THREE.BatchItem,
  508.  
  509. };
  510.  
  511.  
  512.  
  513. THREE.ProgramUniform = function( program, uniformName, failUponMissingLocation ) {
  514.  
  515. this.program = program;
  516. this.uniformName = uniformName;
  517. this.location = gl.getUniformLocation( this.program.glProgram, this.uniformName.name );
  518. this.textureUnits = null;
  519.  
  520. if( ! this.location && failUponMissingLocation ) throw new Error( "can not bind against uniform: " + this.uniformName.name );
  521.  
  522. };
  523.  
  524. THREE.ProgramUniform.prototype = {
  525.  
  526. constructor: THREE.ProgramUniform,
  527.  
  528. set: function( value ) {
  529.  
  530. var value = value.value || this.uniformName.defaultValue;
  531.  
  532. var descriptor = this.uniformName.descriptor;
  533.  
  534. if( descriptor.isTexture() ) {
  535.  
  536. this.textureUnits = this.textureUnits || [];
  537.  
  538. // if this is the first time we load up the texture, we need to set its units.
  539. while( this.textureUnits.length < value.length ) {
  540.  
  541. this.textureUnits.push( program.nextAvailableTextureUnit ++ );
  542.  
  543. }
  544.  
  545. if( ! descriptor.isArray ) {
  546.  
  547. glUniform1i( this.location, this.textureUnits[i] ); // Texture unit 0 is for base images. ???
  548. gl.activeTexture( gl.TEXTURE0 + this.textureUnits[i] );
  549. gl.bindTexture( descriptor.elementType, value );
  550.  
  551. }
  552. else {
  553.  
  554. _gl.uniform1iv( this.location, this.textureUnits );
  555.  
  556. for( var i = 0; i < this.value.length; i ++ ) {
  557.  
  558. gl.activeTexture( gl.TEXTURE0 + this.textureUnits[i] );
  559. gl.bindTexture( descriptor.elementType, value[i] );
  560.  
  561. }
  562. }
  563.  
  564. }
  565. else {
  566.  
  567. this._cachedFlatArray = THREE.Descriptor.ToFlatArray( descriptor.elementType, value, this._cachedFlatArray );
  568.  
  569. // special handling for matrices, whether arrays or not.
  570. if( descriptor.elementType === gl.FLOAT && descriptor.itemSize === 9 ) {
  571.  
  572. gl.uniformMatrix3fv( this.location, false, this._cachedFlatArray );
  573. return;
  574. }
  575.  
  576. if( descriptor.elementType === gl.FLOAT && descriptor.itemSize === 16 ) {
  577.  
  578. gl.uniformMatrix4fv( this.location, false, this._cachedFlatArray );
  579. return;
  580. }
  581.  
  582. var uniformFunctionName = 'uniform' + descriptor.itemSize;
  583.  
  584. switch( descriptor.elementType ) {
  585. case gl.FLOAT:
  586. uniformFunctionName += 'f';
  587. break;
  588.  
  589. case gl.INT:
  590. uniformFunctionName += 'i';
  591. break;
  592.  
  593. default:
  594. throw new Error( "elementType is not supported:" + descriptor.elementType );
  595. };
  596.  
  597. if( descriptor.isArray ) {
  598.  
  599. gl[uniformFunctionName + 'v']( this.location, this._cachedFlatArray );
  600.  
  601. }
  602. else {
  603.  
  604. switch( descriptor.itemSize ) {
  605.  
  606. case 1:
  607. gl[ uniformFunctionName ]( this.location, this._cachedFlatArray[0] );
  608. break;
  609.  
  610. case 2:
  611. gl[ uniformFunctionName ]( this.location, this._cachedFlatArray[0], this._cachedFlatArray[1] );
  612. break;
  613.  
  614. case 3:
  615. gl[ uniformFunctionName ]( this.location, this._cachedFlatArray[0], this._cachedFlatArray[1], this._cachedFlatArray[2] );
  616. break;
  617.  
  618. case 4:
  619. gl[ uniformFunctionName ]( this.location, this._cachedFlatArray[0], this._cachedFlatArray[1], this._cachedFlatArray[2], this._cachedFlatArray[3] );
  620. break;
  621.  
  622. default:
  623. throw new Error( "itemSize is not supported: " + descriptor.itemSize );
  624.  
  625. }
  626.  
  627. }
  628.  
  629. this.previousValue = value;
  630. }
  631.  
  632. }
  633.  
  634. };
  635.  
  636. THREE.ProgramAttribute = function( program, attributeName, failUponMissingLocation ) {
  637.  
  638. this.program = program;
  639. this.attributeName = attributeName;
  640. this.location = gl.getAttribLocation( this.program.glProgram, this.attributeName.name );
  641.  
  642. if( ( this.location < 0 ) && failUponMissingLocation ) throw new Error( "can not bind against attribute: " + this.attributeName.name );
  643.  
  644. };
  645.  
  646. THREE.ProgramAttribute.prototype = {
  647.  
  648. constructor: THREE.ProgramAttribute,
  649.  
  650. bind: function( attributeValue ) {
  651.  
  652. // can not bind buffers that do not exist in the program
  653. if( this.location < 0 ) return;
  654.  
  655. var descriptor = this.attributeName.descriptor;
  656. var name = descriptor.name;
  657.  
  658. if( descriptor.name !== attributeValue.descriptor.name ) throw new Error( "ProgramAttribute: names do not match: " + descriptor.name + ", " + attributeValue.descriptor.name );
  659.  
  660. // index buffers are used exclusively for drawElement calls, thus should not be bound in this fashion.
  661. if( name === 'index' ) return;
  662.  
  663. if( ! attributeValue ) throw new Error( "Program: can not bind attribute to attribute, no matching attribute: " + name );
  664. if( ! attributeValue.glBuffer ) throw new Error( "Program: Can not draw attribute as no WebGL attribute exists for: " + name );
  665.  
  666. gl.enableVertexAttribArray( this.location );
  667. gl.bindBuffer( gl.ARRAY_BUFFER, attributeValue.glBuffer );
  668. gl.vertexAttribPointer( this.location, descriptor.itemSize, descriptor.elementType, false, 0, 0);
  669.  
  670. }
  671.  
  672. };
  673.  
  674.  
  675. THREE.Program = function( glProgram, uniformNames, attributeNames, failUponMissingLocation ) {
  676.  
  677. failUponMissingLocation = failUponMissingLocation || false;
  678.  
  679. this.glProgram = glProgram;
  680. this.nextAvailableTextureUnit = 0;
  681. this.nameToProgramUniforms = {};
  682.  
  683. if( uniformNames ) {
  684.  
  685. for( var i = 0; i < uniformNames.length; i ++ ) {
  686.  
  687. var uniformName = uniformNames[i];
  688. this.nameToProgramUniforms[ uniformName.name ] = new THREE.ProgramUniform( this, uniformName, failUponMissingLocation );
  689.  
  690. }
  691. }
  692.  
  693. this.nameToProgramAttributes = {};
  694.  
  695. if( attributeNames ) {
  696.  
  697. for( var i = 0; i < attributeNames.length; i ++ ) {
  698.  
  699. var attributeName = attributeNames[i];
  700. this.nameToProgramAttributes[ attributeName.name ] = new THREE.ProgramAttribute( this, attributeName, failUponMissingLocation );
  701. }
  702. }
  703.  
  704. };
  705.  
  706. THREE.Program.prototype = {
  707.  
  708. constructor: THREE.Program,
  709.  
  710. bindAttributes: function( nameToAttributeValues ) {
  711.  
  712. // bind all attributes that have well defined locations, error if no matching attribute can be found.
  713. for( var name in this.nameToProgramAttributes ) {
  714.  
  715. if ( this.nameToProgramAttributes.hasOwnProperty( name ) ) {
  716.  
  717. var programAttribute = this.nameToProgramAttributes[ name ];
  718. var attributeValue = nameToAttributeValues[ name ];
  719.  
  720. if( attributeValue ) {
  721. console.log( "binding attribute: " + name + " attributeValue.length: " + attributeValue.glBufferLength );
  722. programAttribute.bind( attributeValue );
  723.  
  724. }
  725. }
  726. }
  727. },
  728.  
  729. setUniforms: function( nameToUniformValues ) {
  730.  
  731. for( var name in this.nameToProgramUniforms ) {
  732.  
  733. if ( this.nameToProgramUniforms.hasOwnProperty( name ) ) {
  734.  
  735. var programUniform = this.nameToProgramUniforms[ name ];
  736. var uniformValue = nameToUniformValues[ name ];
  737.  
  738. if( uniformValue ) {
  739.  
  740. programUniform.set( uniformValue );
  741.  
  742. }
  743. }
  744. }
  745. },
  746.  
  747. drawPrimitives: function( primitiveType, nameToAttributeValues ) {
  748.  
  749. var positionAttributeValue = nameToAttributeValues[ 'position' ];
  750. var indexAttributeValue = nameToAttributeValues[ 'index' ];
  751.  
  752. if( ! indexAttributeValue ) {
  753. console.log( primitiveType, positionAttributeValue );
  754.  
  755. var itemCount = positionAttributeValue.glBufferLength;
  756. switch( primitiveType ) {
  757. case gl.TRIANGLES:
  758. itemCount = itemCount / 3;
  759. break;
  760. case gl.TRIANGLE_STRIP:
  761. case gl.TRIANGLE_FAN:
  762. itemCount = itemCount - 2;
  763. break;
  764. case gl.POINTS:
  765. break;
  766. case gl.LINES:
  767. itemCount = itemCount / 2;
  768. break;
  769. case gl.LINE_LOOP:
  770. break;
  771. case gl.LINE_STRIP:
  772. itemCount = itemCount - 1;
  773. break;
  774. default:
  775. throw new Error( "should not get here, unknown primitiveType: " + primitiveType );
  776. }
  777. gl.drawArrays( primitiveType, 0, itemCount );
  778.  
  779. }
  780. else {
  781.  
  782. var indexDescriptor = indexAttributeValue.descriptor;
  783.  
  784. if( ! indexAttributeValue.glBuffer ) throw new Error( "Can not draw indexed attribute as no WebGL attribute exists for: index" );
  785.  
  786. gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, indexAttributeValue.glBuffer );
  787. gl.drawElements( primitiveType, indexAttributeValue.glBufferLength, indexDescriptor.elementType, 0 );
  788.  
  789. }
  790.  
  791. },
  792.  
  793. dispose: function() {
  794.  
  795. if( this.program ) {
  796.  
  797. gl.deleteProgram( this.program );
  798. this.program = null;
  799.  
  800. }
  801.  
  802. }
  803.  
  804. };
  805.  
  806. THREE.DepthRenderer = function() {
  807.  
  808. this.program = new THREE.Program( depthShaderProgram, attributeNamesToLocations, uniformNamesToLocations );
  809.  
  810. };
  811.  
  812. THREE.DepthRenderer.prototype = {
  813.  
  814. constructor: THREE.DepthRenderer,
  815.  
  816. render: function( batchItems, uniformSet, renderTarget ) {
  817.  
  818. gl.useProgram( this.program.glProgram );
  819. this.program.bindUniforms( uniformSet );
  820.  
  821. for( var i = 0; i < batchItems.length; i ++ ) {
  822.  
  823. var batchItem = batchItems[i];
  824.  
  825. // update uniforms
  826.  
  827. batchItem.bufferSet.update();
  828. this.program.bindAttributes( batchItem.buffers );
  829. this.program.setUniforms( batchItem.localUniforms );
  830.  
  831. batchItem.bufferSet.draw();
  832.  
  833. }
  834. }
  835.  
  836. };
  837.  
  838.  
  839. THREE.SimpleRenderer = function() {
  840.  
  841. };
  842.  
  843. THREE.SimpleRenderer.prototype = {
  844.  
  845. constructor: THREE.SimpleRenderer,
  846.  
  847. render: function( batchItems, globalUniformValues, renderTarget ) {
  848.  
  849. for( var i = 0; i < batchItems.length; i ++ ) {
  850.  
  851. var batchItem = batchItems[i];
  852.  
  853. var program = batchItem.program;
  854.  
  855. gl.useProgram( program.glProgram );
  856.  
  857. program.setUniforms( globalUniformValues );
  858.  
  859. program.bindAttributes( batchItem.attributeValues );
  860.  
  861. if( batchItem.localUniformValues ) {
  862. program.setUniforms( batchItem.localUniformValues );
  863. }
  864.  
  865. if( batchItem.materialUniformValues ) {
  866. program.setUniforms( batchItem.materialUniformValues );
  867. }
  868.  
  869. program.drawPrimitives( batchItem.primitiveType, batchItem.attributeValues );
  870.  
  871. }
  872.  
  873. }
  874.  
  875. };
  876.  
  877. THREE.DescriptorLibrary = {};
  878. THREE.UniformLibrary = {};
  879. THREE.AttributeLibrary = {};
  880.  
  881. THREE.LibraryUtils = function() {
  882. };
  883.  
  884. THREE.LibraryUtils.addToNamedMap = function( map, array ) {
  885. map = map || {};
  886. for( var i = 0; i < array.length; i ++ ) {
  887. map[ array[i].name ] = array[i];
  888. }
  889. return map;
  890. };
  891.  
  892. THREE.LibraryUtils.resolveDescriptors = function( map, nameArray, throwOnNotFound ) {
  893. var descriptorArray = [];
  894.  
  895. for( var i = 0; i < nameArray.length; i ++ ) {
  896. var descriptor = map[ nameArray[i] ];
  897. if( ! descriptor ) {
  898. if( throwOnNotFound ) {
  899. throw new Error( "can not find a descriptor named: " + nameArray[i] );
  900. }
  901. }
  902. else {
  903. descriptorArray.push( descriptor );
  904. }
  905. }
  906.  
  907. return descriptorArray;
  908. };
  909.  
  910. THREE.LibraryUtils.initSharedLibraries = function( gl ) {
  911.  
  912. THREE.DescriptorLibrary.Float1 = new THREE.Descriptor( gl.FLOAT, 1, false );
  913. THREE.DescriptorLibrary.Float2 = new THREE.Descriptor( gl.FLOAT, 2, false );
  914. THREE.DescriptorLibrary.Float3 = new THREE.Descriptor( gl.FLOAT, 3, false, false );
  915. THREE.DescriptorLibrary.Float4 = new THREE.Descriptor( gl.FLOAT, 4, false, false );
  916. THREE.DescriptorLibrary.Float9 = new THREE.Descriptor( gl.FLOAT, 9, false, false );
  917. THREE.DescriptorLibrary.Float16 = new THREE.Descriptor( gl.FLOAT, 16, false, false );
  918.  
  919. THREE.DescriptorLibrary.Float1Array = new THREE.Descriptor( gl.FLOAT, 1, true, false );
  920. THREE.DescriptorLibrary.Float2Array = new THREE.Descriptor( gl.FLOAT, 2, true, false );
  921. THREE.DescriptorLibrary.Float3Array = new THREE.Descriptor( gl.FLOAT, 3, true, false );
  922. THREE.DescriptorLibrary.Float4Array = new THREE.Descriptor( gl.FLOAT, 4, true, false );
  923. THREE.DescriptorLibrary.Float4Array = new THREE.Descriptor( gl.FLOAT, 16, true, false );
  924.  
  925. THREE.DescriptorLibrary.Int1 = new THREE.Descriptor( gl.INT, 1, false, false );
  926. THREE.DescriptorLibrary.Int2 = new THREE.Descriptor( gl.INT, 2, false, false );
  927. THREE.DescriptorLibrary.Int3 = new THREE.Descriptor( gl.INT, 3, false, false );
  928.  
  929. THREE.DescriptorLibrary.Int1Array = new THREE.Descriptor( gl.INT, 1, true, false );
  930.  
  931. THREE.DescriptorLibrary.Int1Index = new THREE.Descriptor( gl.INT, 1, true, true );
  932. THREE.DescriptorLibrary.Ushort1Index = new THREE.Descriptor( gl.UNSIGNED_SHORT, 1, true, true );
  933.  
  934. THREE.LibraryUtils.addToNamedMap( THREE.UniformLibrary, [
  935. new THREE.UniformName( 'projectionMatrix', THREE.DescriptorLibrary.Float16, new THREE.Matrix4() ),
  936. new THREE.UniformName( 'viewMatrix', THREE.DescriptorLibrary.Float16, new THREE.Matrix4() ),
  937. new THREE.UniformName( 'modelMatrix', THREE.DescriptorLibrary.Float16, new THREE.Matrix4() ),
  938. new THREE.UniformName( 'modelViewMatrix', THREE.DescriptorLibrary.Float16, new THREE.Matrix4() ),
  939. new THREE.UniformName( 'normalMatrix', THREE.DescriptorLibrary.Float9, new THREE.Matrix3() ),
  940. new THREE.UniformName( 'cameraPosition', THREE.DescriptorLibrary.Float3, new THREE.Vector3() ),
  941. new THREE.UniformName( 'fogColor', THREE.DescriptorLibrary.Float3, new THREE.Vector3( 1, 1, 1 ) ),
  942. new THREE.UniformName( 'fogDensity', THREE.DescriptorLibrary.Float1, 0.1 ),
  943. new THREE.UniformName( 'fogNear', THREE.DescriptorLibrary.Float1, 0.1 ),
  944. new THREE.UniformName( 'fogFar', THREE.DescriptorLibrary.Float1, 10000 ),
  945. new THREE.UniformName( 'reflectivity', THREE.DescriptorLibrary.Float1, 0.3 ),
  946. new THREE.UniformName( 'flipEnvMap', THREE.DescriptorLibrary.Float1, 1.0 ),
  947. new THREE.UniformName( 'combine', THREE.DescriptorLibrary.Int1, 1 ),
  948. new THREE.UniformName( 'useRefract', THREE.DescriptorLibrary.Int1, 1 ),
  949. new THREE.UniformName( 'refractionRatio', THREE.DescriptorLibrary.Float1, 0.3 ),
  950. new THREE.UniformName( 'opacity', THREE.DescriptorLibrary.Float1, 1.0 ),
  951. new THREE.UniformName( 'diffuse', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
  952. new THREE.UniformName( 'ambient', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
  953. new THREE.UniformName( 'emissive', THREE.DescriptorLibrary.Float3, new THREE.Vector3( 0, 0, 0 ) ),
  954. new THREE.UniformName( 'specular', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
  955. new THREE.UniformName( 'shininess', THREE.DescriptorLibrary.Float1, 50 ),
  956. new THREE.UniformName( 'size', THREE.DescriptorLibrary.Float1, 1.0 ),
  957. new THREE.UniformName( 'scalar', THREE.DescriptorLibrary.Float1, 1.0 ),
  958. new THREE.UniformName( 'psColor', THREE.DescriptorLibrary.Float3, new THREE.Vector3( 1, 1, 1 ) ),
  959. new THREE.UniformName( 'dashSize', THREE.DescriptorLibrary.Float1, 1.0 ),
  960. new THREE.UniformName( 'scale', THREE.DescriptorLibrary.Float1, 1.0 ),
  961. new THREE.UniformName( 'totalSize', THREE.DescriptorLibrary.Float1, 10.0 ),
  962. new THREE.UniformName( 'mNear', THREE.DescriptorLibrary.Float1, 1.0 ),
  963. new THREE.UniformName( 'mFar', THREE.DescriptorLibrary.Float1, 2000.0 ),
  964. new THREE.UniformName( 'scale', THREE.DescriptorLibrary.Float1, 1.0 ),
  965. new THREE.UniformName( 'enableAO', THREE.DescriptorLibrary.Int1, 0 ),
  966. new THREE.UniformName( 'enableDiffuse', THREE.DescriptorLibrary.Int1, 0 ),
  967. new THREE.UniformName( 'enableSpecular', THREE.DescriptorLibrary.Int1, 0 ),
  968. new THREE.UniformName( 'enableReflection', THREE.DescriptorLibrary.Int1, 0 ),
  969. new THREE.UniformName( 'enableDisplacement', THREE.DescriptorLibrary.Int1, 0 ),
  970. new THREE.UniformName( 'uNormalScale', THREE.DescriptorLibrary.Float2, new THREE.Vector2( 1, 1 ) ),
  971. new THREE.UniformName( 'uDisplacementBias', THREE.DescriptorLibrary.Float1, 0.0 ),
  972. new THREE.UniformName( 'uDisplacementScale', THREE.DescriptorLibrary.Float1, 1.0 ),
  973. new THREE.UniformName( 'uDiffuseColor', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
  974. new THREE.UniformName( 'uSpecularColor', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
  975. new THREE.UniformName( 'uAmbientColor', THREE.DescriptorLibrary.Float3, new THREE.Color( 0xffffff ) ),
  976. new THREE.UniformName( 'uShininess', THREE.DescriptorLibrary.Float1, 30 ),
  977. new THREE.UniformName( 'uOpacity', THREE.DescriptorLibrary.Float1, 30 ),
  978. new THREE.UniformName( 'directionalLightDirection', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 1, 0, 0 ) ),
  979. new THREE.UniformName( 'directionalLightColor', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 0xffffff ) ),
  980. new THREE.UniformName( 'spotLightPosition', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 0, 0, 0 ) ),
  981. new THREE.UniformName( 'spotLightDirection', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 1, 0, 0 ) ),
  982. new THREE.UniformName( 'spotLightColor', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 0xffffff ) ),
  983. new THREE.UniformName( 'spotLightDistance', THREE.DescriptorLibrary.Float1Array, 10000 ),
  984. new THREE.UniformName( 'spotLightExponent', THREE.DescriptorLibrary.Float1Array, 10000 ),
  985. new THREE.UniformName( 'spotLightAngleCos', THREE.DescriptorLibrary.Float1Array, 10000 ),
  986. new THREE.UniformName( 'pointLightPosition', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 0, 0, 0 ) ),
  987. new THREE.UniformName( 'pointLightDistance', THREE.DescriptorLibrary.Float1Array, 10000 ),
  988. new THREE.UniformName( 'pointLightColor', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 0xffffff ) ),
  989. new THREE.UniformName( 'hemisphereLightDirection', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 1, 0, 0 ) ),
  990. new THREE.UniformName( 'hemisphereLightSkyColor', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 0xffffff ) ),
  991. new THREE.UniformName( 'hemisphereLightGroundColor', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 0xffffff ) ),
  992. new THREE.UniformName( 'map', THREE.DescriptorLibrary.Texture2D ),
  993. new THREE.UniformName( 'specularMap', THREE.DescriptorLibrary.Texture2D ),
  994. new THREE.UniformName( 'bumpMap', THREE.DescriptorLibrary.Texture2D ),
  995. new THREE.UniformName( 'normalMap', THREE.DescriptorLibrary.Texture2D ),
  996. new THREE.UniformName( 'lightMap', THREE.DescriptorLibrary.Texture2D ),
  997. new THREE.UniformName( 'shadowMap', THREE.DescriptorLibrary.Texture2DArray ),
  998. new THREE.UniformName( 'boneTexture', THREE.DescriptorLibrary.Texture2D ),
  999. new THREE.UniformName( 'envMap', THREE.DescriptorLibrary.TextureCubeMap )
  1000. ] );
  1001.  
  1002. THREE.LibraryUtils.addToNamedMap( THREE.AttributeLibrary, [
  1003. new THREE.AttributeName( 'index', THREE.DescriptorLibrary.Ushort1Index, 0 ),
  1004. new THREE.AttributeName( 'position', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
  1005. new THREE.AttributeName( 'normal', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3( 1, 0, 0 ) ),
  1006. new THREE.AttributeName( 'uv', THREE.DescriptorLibrary.Float2Array, new THREE.Vector2() ),
  1007. new THREE.AttributeName( 'uv2', THREE.DescriptorLibrary.Float2Array, new THREE.Vector2() ),
  1008. new THREE.AttributeName( 'color', THREE.DescriptorLibrary.Float3Array, new THREE.Color( 1, 1, 1 ) ),
  1009. new THREE.AttributeName( 'morphTarget0', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
  1010. new THREE.AttributeName( 'morphTarget1', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
  1011. new THREE.AttributeName( 'morphTarget2', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
  1012. new THREE.AttributeName( 'morphTarget3', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
  1013. new THREE.AttributeName( 'morphNormal0', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
  1014. new THREE.AttributeName( 'morphNormal1', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
  1015. new THREE.AttributeName( 'morphNormal2', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
  1016. new THREE.AttributeName( 'morphNormal3', THREE.DescriptorLibrary.Float3Array, new THREE.Vector3() ),
  1017. new THREE.AttributeName( 'skinIndex', THREE.DescriptorLibrary.Float4Array, new THREE.Vector3() ),
  1018. new THREE.AttributeName( 'skinWeight', THREE.DescriptorLibrary.Float4Array, new THREE.Vector3() )
  1019. ] );
  1020.  
  1021. };
  1022.  
  1023.  
  1024. THREE.ShaderLibrary = function() {
  1025. this.shaderNameToContents = {};
  1026. }
  1027.  
  1028. THREE.ShaderLibrary.addShader = function( shaderName, contents ) {
  1029. this.shaderNameToContents[shaderName] = contents;
  1030. }
  1031.  
  1032. THREE.ShaderLibrary.combineShader = function( orderedShaderNames ) {
  1033.  
  1034. var result = "";
  1035.  
  1036. for( var i = 0; i < orderedShaderNames.length; i ++ ) {
  1037. var contents = this.shaderNameToContents[ orderedShaderNames[i] ];
  1038. if( ! contents ) {
  1039. throw new Error( "no shader named: " + orderedShaderNames[i] );
  1040. }
  1041. result += contents + '\n';
  1042. }
  1043.  
  1044. return result;
  1045.  
  1046. }
  1047.  
  1048.  
  1049. THREE.SceneTraversalInOrder = function() {
  1050.  
  1051. var nodeStack = [];
  1052.  
  1053. return function( scene, visitorFunction ) {
  1054.  
  1055. nodeStack.push( scene );
  1056.  
  1057. while( nodeStack.length > 0 ) {
  1058.  
  1059. var node = nodeStack.pop();
  1060.  
  1061. for( var i = 0; i < node.children.length; i ++ ) {
  1062.  
  1063. nodeStack.push( node.children[i] );
  1064.  
  1065. }
  1066.  
  1067. visitorFunction( node );
  1068.  
  1069. }
  1070.  
  1071. }
  1072. }();
  1073.  
  1074. THREE.SceneTraversalPreOrder = function() {
  1075.  
  1076. var nodeStack = [];
  1077.  
  1078. return function( scene, visitorFunction ) {
  1079.  
  1080. nodeStack.push( scene );
  1081.  
  1082. while( nodeStack.length > 0 ) {
  1083.  
  1084. var node = nodeStack.pop();
  1085.  
  1086. visitorFunction( node );
  1087.  
  1088. for( var i = 0; i < node.children.length; i ++ ) {
  1089.  
  1090. nodeStack.push( node.children[i] );
  1091.  
  1092. }
  1093.  
  1094. }
  1095.  
  1096. }
  1097. }();
  1098.  
  1099. var getRenderData = function( sceneItem ) {
  1100.  
  1101. sceneItem.__renderData = sceneItem.__renderData || {};
  1102.  
  1103. return sceneItem.__renderData;
  1104. };
  1105.  
  1106. THREE.SceneUpdater = function( traversal ) {
  1107.  
  1108. this.traversal = traversal || THREE.SceneTraversalInOrder;
  1109.  
  1110. };
  1111.  
  1112. THREE.SceneUpdater.prototype = {
  1113.  
  1114. constructor: THREE.SceneUpdater,
  1115.  
  1116. updateCamera: function( camera ) {
  1117.  
  1118. camera.updateProjectionMatrix();
  1119. camera.updateMatrix();
  1120. camera.updateMatrixWorld();
  1121. var projectionMatrix = camera.projectionMatrix;
  1122. var worldMatrixInverse = new THREE.Matrix4().getInverse( camera.matrixWorld, true );
  1123. projectionMatrix.multiplyMatrices( projectionMatrix, worldMatrixInverse );
  1124.  
  1125. var renderData = getRenderData( camera );
  1126. var uniformValues = ( renderData.uniformValues = renderData.uniformValues || {} );
  1127.  
  1128. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'projectionMatrix' ], projectionMatrix );
  1129.  
  1130. },
  1131.  
  1132. updateLights: function( scene ) {
  1133.  
  1134. var dirDirections = [];
  1135. var dirColors = [];
  1136.  
  1137. var spotPositions = [];
  1138. var spotDirections = [];
  1139. var spotColors = [];
  1140. var spotDistances = [];
  1141. var spotExponents = [];
  1142. var spotAngleCos = [];
  1143.  
  1144. var pointPositions = [];
  1145. var pointDistances = [];
  1146. var pointColors = [];
  1147.  
  1148. var hemiDirections = [];
  1149. var hemiSkyColor = [];
  1150. var hemiGroundColor = [];
  1151.  
  1152. this.traversal( scene, function( node ) {
  1153.  
  1154. if( node instanceof THREE.Light ) {
  1155.  
  1156. if ( ! node.visible ) return;
  1157.  
  1158. if( node instanceof THREE.DirectionalLight ) {
  1159.  
  1160. var direction = node.getWorldDirection();
  1161. if( direction.x == 0 && direction.y == 0 && direction.z == 0 ) {
  1162. return;
  1163. }
  1164. dirDirections.push( direction );
  1165. dirColors.push( node.color.clone().multiplyScalar( node.intensity ) );
  1166.  
  1167. }
  1168. else if( node instanceof THREE.SpotLight ) {
  1169.  
  1170. var direction = node.getWorldDirection();
  1171. if( direction.x == 0 && direction.y == 0 && direction.z == 0 ) {
  1172. return;
  1173. }
  1174. spotPositions.push( new THREE.Vector3().setFromMatrixPosition( node.matrixWorld ) );
  1175. spotDirections.push( direction );
  1176. spotColors.push( node.color.clone().multiplyScalar( node.intensity ) );
  1177. spotDistances.push( node.distance );
  1178. spotExponents.push( node.exponent );
  1179. spotCosAngle.push( Math.cos( THREE.Math.degToRad( node.angle ) ) );
  1180.  
  1181. }
  1182. else if( node instanceof THREE.PointLight ) {
  1183.  
  1184. pointPositions.push( new THREE.Vector3().setFromMatrixPosition( node.matrixWorld ) );
  1185. pointDistances.push( node.distance );
  1186. pointColors.push( node.color.clone().multiplyScalar( node.intensity ) );
  1187.  
  1188. } else if( node instanceof THREE.HemisphereLight ) {
  1189.  
  1190. hemiDirections.push( node.getWorldDirection() );
  1191. hemiSkyColors.push( node.color.clone().multiplyScalar( node.intensity ) );
  1192. hemiGroundColors.push( node.groundColor.clone().multiplyScalar( node.intensity ) );
  1193.  
  1194. }
  1195.  
  1196. }
  1197.  
  1198. });
  1199.  
  1200. var renderData = getRenderData( scene );
  1201. var uniformValues = renderData.uniformValues = renderData.uniformValues || {};
  1202.  
  1203. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'directionalLightDirection' ], dirColors );
  1204. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'directionalLightColor' ], dirColors );
  1205.  
  1206. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightPosition' ], spotPositions );
  1207. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightDirection' ], spotDirections );
  1208. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightColor' ], spotColors );
  1209. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightDistance' ], spotDistances );
  1210. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightExponent' ], spotExponents );
  1211. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'spotLightAngleCos' ], spotAngleCos );
  1212.  
  1213. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'pointLightPosition' ], pointPositions );
  1214. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'pointLightDistance' ], pointDistances );
  1215. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'pointLightColor' ], pointColors );
  1216.  
  1217. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'hemisphereLightDirection' ], hemiDirections );
  1218. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'hemisphereLightSkyColor' ], hemiSkyColor );
  1219. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'hemisphereLightGroundColor' ], hemiGroundColor );
  1220.  
  1221. // TODO handle previously used lights that are no lnoger visible.
  1222. },
  1223.  
  1224. // TODO: Handle programs separately from conversion of meshes into attributes and uniforms if possible.
  1225. updateMeshes: function( scene ) {
  1226.  
  1227. // TODO: optmize this by getting rid of intermediate arrays
  1228. var flattenGeometry = function( attributeValues, geometry ) {
  1229.  
  1230. var positions = [];
  1231. var vertexNormals = [];
  1232. var vertexColors = [];
  1233. var vertexUvs = [];
  1234.  
  1235. for( var i = 0, il = geometry.faces.length; i < il; i ++ ) {
  1236.  
  1237. var f = geometry.faces[i];
  1238. positions.push( geometry.vertices[f.a], geometry.vertices[f.b], geometry.vertices[f.c] );
  1239.  
  1240. if( f.vertexNormals && f.vertexNormals.length === 3 ) {
  1241. vertexNormals.push( f.vertexNormals[0], f.vertexNormals[1], f.vertexNormals[2] );
  1242. }
  1243. else if( f.normal ) {
  1244. vertexNormals.push( f.normal, f.normal, f.normal );
  1245. }
  1246.  
  1247. if( f.vertexColors && f.vertexColors.length === 3 ) {
  1248. vertexColors.push( f.vertexColors[0], f.vertexColors[1], f.vertexColors[2] )
  1249. }
  1250. else if( f.normal ) {
  1251. vertexColors.push( f.color, f.color, f.color );
  1252. }
  1253.  
  1254. if( geometry.faceVertexUvs[0] ) {
  1255. var faceUvs = geometry.faceVertexUvs[0][i];
  1256. vertexUvs.push( faceUvs[0], faceUvs[1], faceUvs[2] );
  1257. }
  1258.  
  1259. }
  1260.  
  1261. THREE.AttributeValue.updateMap( attributeValues, THREE.AttributeLibrary[ 'position' ], positions );
  1262. if( vertexNormals.length = positions.length ) {
  1263. THREE.AttributeValue.updateMap( attributeValues, THREE.AttributeLibrary[ 'normal' ], vertexNormals );
  1264. }
  1265. if( vertexColors.length = positions.length ) {
  1266. THREE.AttributeValue.updateMap( attributeValues, THREE.AttributeLibrary[ 'color' ], vertexColors );
  1267. }
  1268. if( vertexUvs.length = positions.length ) {
  1269. THREE.AttributeValue.updateMap( attributeValues, THREE.AttributeLibrary[ 'uv' ], vertexUvs );
  1270. }
  1271. }
  1272.  
  1273. this.traversal( scene, function( node ) {
  1274.  
  1275. if( node instanceof THREE.Mesh ) {
  1276.  
  1277. var renderData = getRenderData( node );
  1278.  
  1279. // update uniforms
  1280.  
  1281. node.updateMatrix();
  1282. node.updateMatrixWorld();
  1283.  
  1284. var uniformValues = renderData.uniformValues = renderData.uniformValues || {};
  1285. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'modelViewMatrix' ], node.matrixWorld );
  1286.  
  1287. var normalMatrix = new THREE.Matrix3().getNormalMatrix( node.matrixWorld );
  1288. THREE.UniformValue.updateMap( uniformValues, THREE.UniformLibrary[ 'normalMatrix' ], normalMatrix );
  1289.  
  1290. // update attributes
  1291.  
  1292. var attributeValues = renderData.attributeValues = renderData.attributeValues || {};
  1293.  
  1294. if( node.geometry instanceof THREE.Geometry ) {
  1295.  
  1296. flattenGeometry( attributeValues, node.geometry );
  1297.  
  1298. }
  1299. else if( node.geometry instanceof THREE.BufferGeometry ) {
  1300.  
  1301. for( var attributeName in THREE.AttributeLibrary ) {
  1302.  
  1303. if ( THREE.AttributeLibrary.hasOwnProperty( attributeName ) ) {
  1304.  
  1305. if( node.geometry.attributes[ attributeName ] ) {
  1306.  
  1307. THREE.AttributeValue.updateMap( attributeValues, THREE.AttributeLibrary[ attributeName ], node.geometry.attributes[ attributeName ].array );
  1308.  
  1309. }
  1310. }
  1311. }
  1312. }
  1313. else {
  1314.  
  1315. throw new Error( "should not get here, unrecognized Geometry type" );
  1316.  
  1317. }
  1318.  
  1319. }
  1320.  
  1321. });
  1322.  
  1323. },
  1324.  
  1325. update: function( scene, camera ) {
  1326.  
  1327. this.updateCamera( camera );
  1328. this.updateLights( scene );
  1329. this.updateMeshes( scene );
  1330.  
  1331. }
  1332.  
  1333. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement