Renderer.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author supereggbert / http://www.paulbrunt.co.uk/
  4. */
  5. THREE.Renderer = function() {
  6. var face3Pool = [],
  7. face4Pool = [],
  8. linePool = [],
  9. particlePool = [],
  10. vector4 = new THREE.Vector4(),
  11. projViewMatrix = new THREE.Matrix4();
  12. projViewObjMatrix = new THREE.Matrix4();
  13. function painterSort( a, b ) {
  14. return b.z - a.z;
  15. }
  16. this.renderList = null;
  17. this.project = function (scene, camera) {
  18. var o, ol, v, vl, f, fl, vertex, vertex2, face, object, v1, v2, v3, v4,
  19. face3count = 0, face4count = 0, lineCount = 0, particleCount = 0;
  20. this.renderList = [];
  21. if( camera.autoUpdateMatrix ) {
  22. camera.updateMatrix();
  23. }
  24. projViewMatrix.multiply( camera.projectionMatrix, camera.matrix );
  25. for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {
  26. object = scene.objects[ o ];
  27. if( object.autoUpdateMatrix ) {
  28. object.updateMatrix();
  29. }
  30. if ( object instanceof THREE.Mesh ) {
  31. projViewObjMatrix.multiply( projViewMatrix, object.matrix );
  32. // vertices
  33. for ( v = 0, vl = object.geometry.vertices.length; v < vl; v++ ) {
  34. vertex = object.geometry.vertices[ v ];
  35. vertex.screen.copy( vertex.position );
  36. projViewObjMatrix.transform( vertex.screen );
  37. vertex.__visible = vertex.screen.z > 0 && vertex.screen.z < 1;
  38. }
  39. // faces
  40. for ( f = 0, fl = object.geometry.faces.length; f < fl; f++ ) {
  41. face = object.geometry.faces[ f ];
  42. if ( face instanceof THREE.Face3 ) {
  43. v1 = object.geometry.vertices[ face.a ];
  44. v2 = object.geometry.vertices[ face.b ];
  45. v3 = object.geometry.vertices[ face.c ];
  46. if ( v1.__visible && v2.__visible && v3.__visible &&
  47. ( object.doubleSided || ( object.flipSided !=
  48. ( v3.screen.x - v1.screen.x ) * ( v2.screen.y - v1.screen.y ) -
  49. ( v3.screen.y - v1.screen.y ) * ( v2.screen.x - v1.screen.x ) < 0 ) ) ) {
  50. if ( !face3Pool[ face3count ] ) {
  51. face3Pool[ face3count ] = new THREE.RenderableFace3();
  52. }
  53. face3Pool[ face3count ].v1.copy( v1.screen );
  54. face3Pool[ face3count ].v2.copy( v2.screen );
  55. face3Pool[ face3count ].v3.copy( v3.screen );
  56. face3Pool[ face3count ].z = Math.max( v1.screen.z, Math.max( v2.screen.z, v3.screen.z ) );
  57. face3Pool[ face3count ].material = object.material;
  58. face3Pool[ face3count ].overdraw = object.overdraw;
  59. face3Pool[ face3count ].uvs = object.geometry.uvs[ f ];
  60. face3Pool[ face3count ].color = face.color;
  61. this.renderList.push(face3Pool[face3count]);
  62. face3count++;
  63. }
  64. } else if ( face instanceof THREE.Face4 ) {
  65. v1 = object.geometry.vertices[ face.a ];
  66. v2 = object.geometry.vertices[ face.b ];
  67. v3 = object.geometry.vertices[ face.c ];
  68. v4 = object.geometry.vertices[ face.d ];
  69. if ( v1.__visible && v2.__visible && v3.__visible && v4.__visible &&
  70. ( object.doubleSided || ( object.flipSided !=
  71. ( ( v4.screen.x - v1.screen.x ) * ( v2.screen.y - v1.screen.y ) -
  72. ( v4.screen.y - v1.screen.y ) * ( v2.screen.x - v1.screen.x ) < 0 ||
  73. ( v2.screen.x - v3.screen.x ) * ( v4.screen.y - v3.screen.y ) -
  74. ( v2.screen.y - v3.screen.y ) * ( v4.screen.x - v3.screen.x ) < 0 ) ) ) ) {
  75. if ( !face4Pool[ face4count ] ) {
  76. face4Pool[ face4count ] = new THREE.RenderableFace4();
  77. }
  78. face4Pool[ face4count ].v1.copy( v1.screen );
  79. face4Pool[ face4count ].v2.copy( v2.screen );
  80. face4Pool[ face4count ].v3.copy( v3.screen );
  81. face4Pool[ face4count ].v4.copy( v4.screen );
  82. face4Pool[ face4count ].z = Math.max( v1.screen.z, Math.max( v2.screen.z, Math.max( v3.screen.z, v4.screen.z ) ) );
  83. face4Pool[ face4count ].material = object.material;
  84. face4Pool[ face4count ].overdraw = object.overdraw;
  85. face4Pool[ face4count ].uvs = object.geometry.uvs[ f ];
  86. face4Pool[ face4count ].color = face.color;
  87. this.renderList.push( face4Pool[ face4count ] );
  88. face4count++;
  89. }
  90. }
  91. }
  92. } else if ( object instanceof THREE.Line ) {
  93. projViewObjMatrix.multiply( projViewMatrix, object.matrix );
  94. for ( v = 0, vl = object.geometry.vertices.length; v < vl; v++ ) {
  95. vertex = object.geometry.vertices[ v ];
  96. vertex.screen.copy( vertex.position );
  97. projViewObjMatrix.transform( vertex.screen );
  98. vertex.__visible = vertex.screen.z > 0 && vertex.screen.z < 1;
  99. if ( v > 0 ) {
  100. vertex2 = object.geometry.vertices[ v - 1 ];
  101. if ( vertex.__visible && vertex2.__visible ) {
  102. if ( !linePool[ lineCount ] ) {
  103. linePool[ lineCount ] = new THREE.RenderableLine();
  104. }
  105. linePool[ lineCount ].v1.copy( vertex.screen );
  106. linePool[ lineCount ].v2.copy( vertex2.screen );
  107. linePool[ lineCount ].z = Math.max( vertex.screen.z, vertex2.screen.z );
  108. linePool[ lineCount ].material = object.material;
  109. this.renderList.push( linePool[lineCount] );
  110. lineCount++;
  111. }
  112. }
  113. }
  114. } else if ( object instanceof THREE.Particle ) {
  115. vector4.set( object.position.x, object.position.y, object.position.z, 1 );
  116. camera.matrix.transform( vector4 );
  117. camera.projectionMatrix.transform( vector4 );
  118. object.screen.set( vector4.x / vector4.w, vector4.y / vector4.w, vector4.z / vector4.w );
  119. if ( object.screen.z > 0 && object.screen.z < 1 ) {
  120. if ( !particlePool[ particleCount ] ) {
  121. particlePool[ particleCount ] = new THREE.RenderableParticle();
  122. }
  123. particlePool[ particleCount ].x = object.screen.x;
  124. particlePool[ particleCount ].y = object.screen.y;
  125. particlePool[ particleCount ].z = object.screen.z;
  126. particlePool[ particleCount ].rotation = object.rotation.z;
  127. particlePool[ particleCount ].scale.x = object.scale.x * Math.abs( vector4.x / vector4.w - ( vector4.x + camera.projectionMatrix.n11 ) / ( vector4.w + camera.projectionMatrix.n14 ) );
  128. particlePool[ particleCount ].scale.y = object.scale.y * Math.abs( vector4.y / vector4.w - ( vector4.y + camera.projectionMatrix.n22 ) / ( vector4.w + camera.projectionMatrix.n24 ) );
  129. particlePool[ particleCount ].material = object.material;
  130. particlePool[ particleCount ].color = object.color;
  131. this.renderList.push( particlePool[ particleCount ] );
  132. particleCount++;
  133. }
  134. }
  135. }
  136. this.renderList.sort( painterSort );
  137. };
  138. };