Renderer.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. THREE.Renderer = function() {
  5. var face3Pool = [],
  6. face4Pool = [],
  7. linePool = [],
  8. particlePool = [],
  9. matrix = new THREE.Matrix4();
  10. this.renderList;
  11. function sort(a, b) {
  12. return a.screenZ - b.screenZ;
  13. }
  14. this.project = function (scene, camera) {
  15. var i, j, vertex, vertex2, face, object, v1, v2, v3, v4,
  16. face3count = 0, face4count = 0, lineCount = 0, particleCount = 0,
  17. camerafocus = camera.focus, focuszoom = camera.focus * camera.zoom,
  18. verticesLength = 0, facesLength = 0;
  19. this.renderList = [];
  20. if(camera.autoUpdateMatrix) {
  21. camera.updateMatrix();
  22. }
  23. for (i = 0; i < scene.objects.length; i++) {
  24. object = scene.objects[i];
  25. if(object.autoUpdateMatrix) {
  26. object.updateMatrix();
  27. }
  28. if (object instanceof THREE.Mesh) {
  29. matrix.multiply(camera.matrix, object.matrix);
  30. // vertices
  31. verticesLength = object.geometry.vertices.length;
  32. for (j = 0; j < verticesLength; j++) {
  33. vertex = object.geometry.vertices[j];
  34. vertex.screen.copy(vertex.position);
  35. matrix.transform(vertex.screen);
  36. vertex.screen.z = focuszoom / (camerafocus + vertex.screen.z);
  37. vertex.__visible = vertex.screen.z > 0;
  38. vertex.screen.x *= vertex.screen.z;
  39. vertex.screen.y *= vertex.screen.z;
  40. }
  41. // faces
  42. facesLength = object.geometry.faces.length;
  43. for (j = 0; j < facesLength; j++) {
  44. face = object.geometry.faces[j];
  45. // TODO: Use normals for culling
  46. if (face instanceof THREE.Face3) {
  47. v1 = object.geometry.vertices[face.a];
  48. v2 = object.geometry.vertices[face.b];
  49. v3 = object.geometry.vertices[face.c];
  50. if (v1.__visible && v2.__visible && v3.__visible && (object.doubleSided ||
  51. (v3.screen.x - v1.screen.x) * (v2.screen.y - v1.screen.y) -
  52. (v3.screen.y - v1.screen.y) * (v2.screen.x - v1.screen.x) > 0) ) {
  53. face.screen.z = (v1.screen.z + v2.screen.z + v3.screen.z) * 0.3;
  54. if (!face3Pool[face3count]) {
  55. face3Pool[face3count] = new THREE.RenderableFace3();
  56. }
  57. face3Pool[face3count].v1.x = v1.screen.x;
  58. face3Pool[face3count].v1.y = v1.screen.y;
  59. face3Pool[face3count].v2.x = v2.screen.x;
  60. face3Pool[face3count].v2.y = v2.screen.y;
  61. face3Pool[face3count].v3.x = v3.screen.x;
  62. face3Pool[face3count].v3.y = v3.screen.y;
  63. face3Pool[face3count].screenZ = face.screen.z;
  64. face3Pool[face3count].material = object.material;
  65. face3Pool[face3count].uvs = object.geometry.uvs[j];
  66. face3Pool[face3count].color = face.color;
  67. this.renderList.push(face3Pool[face3count]);
  68. face3count++;
  69. }
  70. } else if (face instanceof THREE.Face4) {
  71. v1 = object.geometry.vertices[face.a];
  72. v2 = object.geometry.vertices[face.b];
  73. v3 = object.geometry.vertices[face.c];
  74. v4 = object.geometry.vertices[face.d];
  75. if (v1.__visible && v2.__visible && v3.__visible && v4.__visible && (object.doubleSided ||
  76. ((v4.screen.x - v1.screen.x) * (v2.screen.y - v1.screen.y) -
  77. (v4.screen.y - v1.screen.y) * (v2.screen.x - v1.screen.x) > 0 ||
  78. (v2.screen.x - v3.screen.x) * (v4.screen.y - v3.screen.y) -
  79. (v2.screen.y - v3.screen.y) * (v4.screen.x - v3.screen.x) > 0)) ) {
  80. face.screen.z = (v1.screen.z + v2.screen.z + v3.screen.z + v4.screen.z) * 0.25;
  81. if (!face4Pool[face4count]) {
  82. face4Pool[face4count] = new THREE.RenderableFace4();
  83. }
  84. face4Pool[face4count].v1.x = v1.screen.x;
  85. face4Pool[face4count].v1.y = v1.screen.y;
  86. face4Pool[face4count].v2.x = v2.screen.x;
  87. face4Pool[face4count].v2.y = v2.screen.y;
  88. face4Pool[face4count].v3.x = v3.screen.x;
  89. face4Pool[face4count].v3.y = v3.screen.y;
  90. face4Pool[face4count].v4.x = v4.screen.x;
  91. face4Pool[face4count].v4.y = v4.screen.y;
  92. face4Pool[face4count].screenZ = face.screen.z;
  93. face4Pool[face4count].material = object.material;
  94. face4Pool[face4count].uvs = object.geometry.uvs[j];
  95. face4Pool[face4count].color = face.color;
  96. this.renderList.push(face4Pool[face4count]);
  97. face4count++;
  98. }
  99. }
  100. }
  101. } else if (object instanceof THREE.Line) {
  102. matrix.multiply(camera.matrix, object.matrix);
  103. verticesLength = object.geometry.vertices.length;
  104. for (j = 0; j < verticesLength; j++) {
  105. vertex = object.geometry.vertices[j];
  106. vertex.screen.copy(vertex.position);
  107. matrix.transform(vertex.screen);
  108. vertex.screen.z = focuszoom / (camerafocus + vertex.screen.z);
  109. vertex.visible = vertex.screen.z > 0;
  110. vertex.screen.x *= vertex.screen.z;
  111. vertex.screen.y *= vertex.screen.z;
  112. if (j > 0) {
  113. vertex2 = object.geometry.vertices[j-1];
  114. if (!vertex.visible || !vertex2.visible) {
  115. continue;
  116. }
  117. if (!linePool[lineCount]) {
  118. linePool[lineCount] = new THREE.RenderableLine();
  119. }
  120. linePool[lineCount].v1.x = vertex.screen.x;
  121. linePool[lineCount].v1.y = vertex.screen.y;
  122. linePool[lineCount].v2.x = vertex2.screen.x;
  123. linePool[lineCount].v2.y = vertex2.screen.y;
  124. linePool[lineCount].screenZ = (vertex.screen.z + vertex2.screen.z) * 0.5;
  125. linePool[lineCount].material = object.material;
  126. this.renderList.push( linePool[lineCount] );
  127. lineCount++;
  128. }
  129. }
  130. } else if (object instanceof THREE.Particle) {
  131. object.screen.copy(object.position);
  132. camera.matrix.transform(object.screen);
  133. object.screen.z = focuszoom / (camerafocus + object.screen.z);
  134. if (object.screen.z < 0) {
  135. continue;
  136. }
  137. object.screen.x *= object.screen.z;
  138. object.screen.y *= object.screen.z;
  139. if (!particlePool[particleCount]) {
  140. particlePool[particleCount] = new THREE.RenderableParticle();
  141. }
  142. particlePool[particleCount].x = object.screen.x;
  143. particlePool[particleCount].y = object.screen.y;
  144. particlePool[particleCount].screenZ = object.screen.z;
  145. particlePool[particleCount].size = object.size;
  146. particlePool[particleCount].material = object.material;
  147. particlePool[particleCount].color = object.color;
  148. this.renderList.push( particlePool[particleCount] );
  149. particleCount++;
  150. }
  151. }
  152. this.renderList.sort(sort);
  153. }
  154. }