SVGRenderer.js 11 KB

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