SVGRenderer.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.SVGRenderer = function () {
  5. console.log( 'THREE.SVGRenderer', THREE.REVISION );
  6. var _this = this,
  7. _renderData, _elements, _lights,
  8. _projector = new THREE.Projector(),
  9. _svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'),
  10. _svgWidth, _svgHeight, _svgWidthHalf, _svgHeightHalf,
  11. _v1, _v2, _v3, _v4,
  12. _clipBox = new THREE.Box2(),
  13. _elemBox = new THREE.Box2(),
  14. _color = new THREE.Color(),
  15. _diffuseColor = new THREE.Color(),
  16. _ambientLight = new THREE.Color(),
  17. _directionalLights = new THREE.Color(),
  18. _pointLights = new THREE.Color(),
  19. _w, // z-buffer to w-buffer
  20. _vector3 = new THREE.Vector3(), // Needed for PointLight
  21. _centroid = new THREE.Vector3(),
  22. _svgPathPool = [], _svgLinePool = [], _svgRectPool = [],
  23. _svgNode, _pathCount = 0, _lineCount = 0, _rectCount = 0,
  24. _quality = 1;
  25. this.domElement = _svg;
  26. this.autoClear = true;
  27. this.sortObjects = true;
  28. this.sortElements = true;
  29. this.info = {
  30. render: {
  31. vertices: 0,
  32. faces: 0
  33. }
  34. }
  35. this.setQuality = function( quality ) {
  36. switch(quality) {
  37. case "high": _quality = 1; break;
  38. case "low": _quality = 0; break;
  39. }
  40. };
  41. // WebGLRenderer compatibility
  42. this.supportsVertexTextures = function () {};
  43. this.setFaceCulling = function () {};
  44. this.setClearColor = function ( color, alpha ) {
  45. // TODO
  46. };
  47. this.setSize = function( width, height ) {
  48. _svgWidth = width; _svgHeight = height;
  49. _svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
  50. _svg.setAttribute( 'viewBox', ( - _svgWidthHalf ) + ' ' + ( - _svgHeightHalf ) + ' ' + _svgWidth + ' ' + _svgHeight );
  51. _svg.setAttribute( 'width', _svgWidth );
  52. _svg.setAttribute( 'height', _svgHeight );
  53. _clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
  54. _clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
  55. };
  56. this.clear = function () {
  57. _pathCount = 0;
  58. _lineCount = 0;
  59. _rectCount = 0;
  60. while ( _svg.childNodes.length > 0 ) {
  61. _svg.removeChild( _svg.childNodes[ 0 ] );
  62. }
  63. };
  64. this.render = function ( scene, camera ) {
  65. if ( camera instanceof THREE.Camera === false ) {
  66. console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
  67. return;
  68. }
  69. if ( this.autoClear === true ) this.clear();
  70. _this.info.render.vertices = 0;
  71. _this.info.render.faces = 0;
  72. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  73. _elements = _renderData.elements;
  74. _lights = _renderData.lights;
  75. calculateLights( _lights );
  76. for ( var e = 0, el = _elements.length; e < el; e ++ ) {
  77. var element = _elements[ e ];
  78. var material = element.material;
  79. if ( material === undefined || material.visible === false ) continue;
  80. _elemBox.makeEmpty();
  81. if ( element instanceof THREE.RenderableSprite ) {
  82. _v1 = element;
  83. _v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
  84. renderSprite( _v1, element, material );
  85. } else if ( element instanceof THREE.RenderableLine ) {
  86. _v1 = element.v1; _v2 = element.v2;
  87. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  88. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  89. _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen ] );
  90. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  91. renderLine( _v1, _v2, element, material );
  92. }
  93. } else if ( element instanceof THREE.RenderableFace ) {
  94. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  95. if ( _v1.positionScreen.z < -1 || _v1.positionScreen.z > 1 ) continue;
  96. if ( _v2.positionScreen.z < -1 || _v2.positionScreen.z > 1 ) continue;
  97. if ( _v3.positionScreen.z < -1 || _v3.positionScreen.z > 1 ) continue;
  98. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  99. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  100. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;
  101. _elemBox.setFromPoints( [
  102. _v1.positionScreen,
  103. _v2.positionScreen,
  104. _v3.positionScreen
  105. ] );
  106. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  107. renderFace3( _v1, _v2, _v3, element, material );
  108. }
  109. }
  110. }
  111. };
  112. function calculateLights( lights ) {
  113. _ambientLight.setRGB( 0, 0, 0 );
  114. _directionalLights.setRGB( 0, 0, 0 );
  115. _pointLights.setRGB( 0, 0, 0 );
  116. for ( var l = 0, ll = lights.length; l < ll; l++ ) {
  117. var light = lights[ l ];
  118. var lightColor = light.color;
  119. if ( light instanceof THREE.AmbientLight ) {
  120. _ambientLight.r += lightColor.r;
  121. _ambientLight.g += lightColor.g;
  122. _ambientLight.b += lightColor.b;
  123. } else if ( light instanceof THREE.DirectionalLight ) {
  124. _directionalLights.r += lightColor.r;
  125. _directionalLights.g += lightColor.g;
  126. _directionalLights.b += lightColor.b;
  127. } else if ( light instanceof THREE.PointLight ) {
  128. _pointLights.r += lightColor.r;
  129. _pointLights.g += lightColor.g;
  130. _pointLights.b += lightColor.b;
  131. }
  132. }
  133. }
  134. function calculateLight( lights, position, normal, color ) {
  135. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  136. var light = lights[ l ];
  137. var lightColor = light.color;
  138. if ( light instanceof THREE.DirectionalLight ) {
  139. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize();
  140. var amount = normal.dot( lightPosition );
  141. if ( amount <= 0 ) continue;
  142. amount *= light.intensity;
  143. color.r += lightColor.r * amount;
  144. color.g += lightColor.g * amount;
  145. color.b += lightColor.b * amount;
  146. } else if ( light instanceof THREE.PointLight ) {
  147. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
  148. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  149. if ( amount <= 0 ) continue;
  150. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  151. if ( amount == 0 ) continue;
  152. amount *= light.intensity;
  153. color.r += lightColor.r * amount;
  154. color.g += lightColor.g * amount;
  155. color.b += lightColor.b * amount;
  156. }
  157. }
  158. }
  159. function renderSprite( v1, element, material ) {
  160. var scaleX = element.scale.x * _svgWidthHalf;
  161. var scaleY = element.scale.y * _svgHeightHalf;
  162. _svgNode = getRectNode( _rectCount ++ );
  163. _svgNode.setAttribute( 'x', v1.x - ( scaleX * 0.5 ) );
  164. _svgNode.setAttribute( 'y', v1.y - ( scaleY * 0.5 ) );
  165. _svgNode.setAttribute( 'width', scaleX );
  166. _svgNode.setAttribute( 'height', scaleY );
  167. if ( material instanceof THREE.SpriteMaterial ) {
  168. _svgNode.setAttribute( 'style', 'fill: ' + material.color.getStyle() );
  169. }
  170. _svg.appendChild( _svgNode );
  171. }
  172. function renderLine( v1, v2, element, material ) {
  173. _svgNode = getLineNode( _lineCount ++ );
  174. _svgNode.setAttribute( 'x1', v1.positionScreen.x );
  175. _svgNode.setAttribute( 'y1', v1.positionScreen.y );
  176. _svgNode.setAttribute( 'x2', v2.positionScreen.x );
  177. _svgNode.setAttribute( 'y2', v2.positionScreen.y );
  178. if ( material instanceof THREE.LineBasicMaterial ) {
  179. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + material.color.getStyle() + '; stroke-width: ' + material.linewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.linecap + '; stroke-linejoin: ' + material.linejoin );
  180. _svg.appendChild( _svgNode );
  181. }
  182. }
  183. function renderFace3( v1, v2, v3, element, material ) {
  184. _this.info.render.vertices += 3;
  185. _this.info.render.faces ++;
  186. _svgNode = getPathNode( _pathCount ++ );
  187. _svgNode.setAttribute( 'd', 'M ' + v1.positionScreen.x + ' ' + v1.positionScreen.y + ' L ' + v2.positionScreen.x + ' ' + v2.positionScreen.y + ' L ' + v3.positionScreen.x + ',' + v3.positionScreen.y + 'z' );
  188. if ( material instanceof THREE.MeshBasicMaterial ) {
  189. _color.copy( material.color );
  190. if ( material.vertexColors === THREE.FaceColors ) {
  191. _color.multiply( element.color );
  192. }
  193. } else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
  194. _diffuseColor.copy( material.color );
  195. if ( material.vertexColors === THREE.FaceColors ) {
  196. _diffuseColor.multiply( element.color );
  197. }
  198. _color.copy( _ambientLight );
  199. _centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );
  200. calculateLight( _lights, _centroid, element.normalModel, _color );
  201. _color.multiply( _diffuseColor ).add( material.emissive );
  202. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  203. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  204. _color.setRGB( _w, _w, _w );
  205. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  206. var normal = element.normalModelView;
  207. _color.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  208. }
  209. if ( material.wireframe ) {
  210. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  211. } else {
  212. _svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
  213. }
  214. _svg.appendChild( _svgNode );
  215. }
  216. function getLineNode( id ) {
  217. if ( _svgLinePool[ id ] == null ) {
  218. _svgLinePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'line' );
  219. if ( _quality == 0 ) {
  220. _svgLinePool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  221. }
  222. return _svgLinePool[ id ];
  223. }
  224. return _svgLinePool[ id ];
  225. }
  226. function getPathNode( id ) {
  227. if ( _svgPathPool[ id ] == null ) {
  228. _svgPathPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  229. if ( _quality == 0 ) {
  230. _svgPathPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  231. }
  232. return _svgPathPool[ id ];
  233. }
  234. return _svgPathPool[ id ];
  235. }
  236. function getRectNode( id ) {
  237. if ( _svgRectPool[ id ] == null ) {
  238. _svgRectPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'rect' );
  239. if ( _quality == 0 ) {
  240. _svgRectPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  241. }
  242. return _svgRectPool[ id ];
  243. }
  244. return _svgRectPool[ id ];
  245. }
  246. };