SVGRenderer.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. _svgPathPool = [], _svgCirclePool = [], _svgLinePool = [],
  22. _svgNode, _pathCount, _circleCount, _lineCount,
  23. _quality = 1;
  24. this.domElement = _svg;
  25. this.autoClear = true;
  26. this.sortObjects = true;
  27. this.sortElements = true;
  28. this.info = {
  29. render: {
  30. vertices: 0,
  31. faces: 0
  32. }
  33. }
  34. this.setQuality = function( quality ) {
  35. switch(quality) {
  36. case "high": _quality = 1; break;
  37. case "low": _quality = 0; break;
  38. }
  39. };
  40. // WebGLRenderer compatibility
  41. this.supportsVertexTextures = function () {};
  42. this.setFaceCulling = function () {};
  43. this.setClearColor = function ( color, alpha ) {
  44. // TODO
  45. };
  46. this.setSize = function( width, height ) {
  47. _svgWidth = width; _svgHeight = height;
  48. _svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
  49. _svg.setAttribute( 'viewBox', ( - _svgWidthHalf ) + ' ' + ( - _svgHeightHalf ) + ' ' + _svgWidth + ' ' + _svgHeight );
  50. _svg.setAttribute( 'width', _svgWidth );
  51. _svg.setAttribute( 'height', _svgHeight );
  52. _clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
  53. _clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
  54. };
  55. this.clear = function () {
  56. _pathCount = 0;
  57. _circleCount = 0;
  58. _lineCount = 0;
  59. while ( _svg.childNodes.length > 0 ) {
  60. _svg.removeChild( _svg.childNodes[ 0 ] );
  61. }
  62. };
  63. this.render = function ( scene, camera ) {
  64. if ( camera instanceof THREE.Camera === false ) {
  65. console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
  66. return;
  67. }
  68. if ( this.autoClear === true ) this.clear();
  69. _this.info.render.vertices = 0;
  70. _this.info.render.faces = 0;
  71. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  72. _elements = _renderData.elements;
  73. _lights = _renderData.lights;
  74. calculateLights( _lights );
  75. for ( var e = 0, el = _elements.length; e < el; e ++ ) {
  76. var element = _elements[ e ];
  77. var material = element.material;
  78. if ( material === undefined || material.visible === false ) continue;
  79. _elemBox.makeEmpty();
  80. if ( element instanceof THREE.RenderableParticle ) {
  81. _v1 = element;
  82. _v1.x *= _svgWidthHalf; _v1.y *= -_svgHeightHalf;
  83. renderParticle( _v1, element, material );
  84. } else if ( element instanceof THREE.RenderableLine ) {
  85. _v1 = element.v1; _v2 = element.v2;
  86. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  87. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  88. _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen ] );
  89. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  90. renderLine( _v1, _v2, element, material );
  91. }
  92. } else if ( element instanceof THREE.RenderableFace3 ) {
  93. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  94. if ( _v1.positionScreen.z < -1 || _v1.positionScreen.z > 1 ) continue;
  95. if ( _v2.positionScreen.z < -1 || _v2.positionScreen.z > 1 ) continue;
  96. if ( _v3.positionScreen.z < -1 || _v3.positionScreen.z > 1 ) continue;
  97. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  98. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  99. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;
  100. _elemBox.setFromPoints( [
  101. _v1.positionScreen,
  102. _v2.positionScreen,
  103. _v3.positionScreen
  104. ] );
  105. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  106. renderFace3( _v1, _v2, _v3, element, material );
  107. }
  108. }
  109. }
  110. };
  111. function calculateLights( lights ) {
  112. _ambientLight.setRGB( 0, 0, 0 );
  113. _directionalLights.setRGB( 0, 0, 0 );
  114. _pointLights.setRGB( 0, 0, 0 );
  115. for ( var l = 0, ll = lights.length; l < ll; l++ ) {
  116. var light = lights[ l ];
  117. var lightColor = light.color;
  118. if ( light instanceof THREE.AmbientLight ) {
  119. _ambientLight.r += lightColor.r;
  120. _ambientLight.g += lightColor.g;
  121. _ambientLight.b += lightColor.b;
  122. } else if ( light instanceof THREE.DirectionalLight ) {
  123. _directionalLights.r += lightColor.r;
  124. _directionalLights.g += lightColor.g;
  125. _directionalLights.b += lightColor.b;
  126. } else if ( light instanceof THREE.PointLight ) {
  127. _pointLights.r += lightColor.r;
  128. _pointLights.g += lightColor.g;
  129. _pointLights.b += lightColor.b;
  130. }
  131. }
  132. }
  133. function calculateLight( lights, position, normal, color ) {
  134. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  135. var light = lights[ l ];
  136. var lightColor = light.color;
  137. if ( light instanceof THREE.DirectionalLight ) {
  138. var lightPosition = _vector3.getPositionFromMatrix( light.matrixWorld ).normalize();
  139. var amount = normal.dot( lightPosition );
  140. if ( amount <= 0 ) continue;
  141. amount *= light.intensity;
  142. color.r += lightColor.r * amount;
  143. color.g += lightColor.g * amount;
  144. color.b += lightColor.b * amount;
  145. } else if ( light instanceof THREE.PointLight ) {
  146. var lightPosition = _vector3.getPositionFromMatrix( light.matrixWorld );
  147. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  148. if ( amount <= 0 ) continue;
  149. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  150. if ( amount == 0 ) continue;
  151. amount *= light.intensity;
  152. color.r += lightColor.r * amount;
  153. color.g += lightColor.g * amount;
  154. color.b += lightColor.b * amount;
  155. }
  156. }
  157. }
  158. function renderParticle( v1, element, material ) {
  159. /*
  160. _svgNode = getCircleNode( _circleCount++ );
  161. _svgNode.setAttribute( 'cx', v1.x );
  162. _svgNode.setAttribute( 'cy', v1.y );
  163. _svgNode.setAttribute( 'r', element.scale.x * _svgWidthHalf );
  164. if ( material instanceof THREE.ParticleCircleMaterial ) {
  165. _color.r = _ambientLight.r + _directionalLights.r + _pointLights.r;
  166. _color.g = _ambientLight.g + _directionalLights.g + _pointLights.g;
  167. _color.b = _ambientLight.b + _directionalLights.b + _pointLights.b;
  168. _color.r = material.color.r * _color.r;
  169. _color.g = material.color.g * _color.g;
  170. _color.b = material.color.b * _color.b;
  171. _color.updateStyleString();
  172. _svgNode.setAttribute( 'style', 'fill: ' + _color.__styleString );
  173. }
  174. _svg.appendChild( _svgNode );
  175. */
  176. }
  177. function renderLine ( v1, v2, element, material ) {
  178. _svgNode = getLineNode( _lineCount ++ );
  179. _svgNode.setAttribute( 'x1', v1.positionScreen.x );
  180. _svgNode.setAttribute( 'y1', v1.positionScreen.y );
  181. _svgNode.setAttribute( 'x2', v2.positionScreen.x );
  182. _svgNode.setAttribute( 'y2', v2.positionScreen.y );
  183. if ( material instanceof THREE.LineBasicMaterial ) {
  184. _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 );
  185. _svg.appendChild( _svgNode );
  186. }
  187. }
  188. function renderFace3( v1, v2, v3, element, material ) {
  189. _this.info.render.vertices += 3;
  190. _this.info.render.faces ++;
  191. _svgNode = getPathNode( _pathCount ++ );
  192. _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' );
  193. if ( material instanceof THREE.MeshBasicMaterial ) {
  194. _color.copy( material.color );
  195. if ( material.vertexColors === THREE.FaceColors ) {
  196. _color.multiply( element.color );
  197. }
  198. } else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
  199. _diffuseColor.copy( material.color );
  200. if ( material.vertexColors === THREE.FaceColors ) {
  201. _diffuseColor.multiply( element.color );
  202. }
  203. _color.copy( _ambientLight );
  204. calculateLight( _lights, element.centroidModel, 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 getCircleNode( id ) {
  241. if ( _svgCirclePool[id] == null ) {
  242. _svgCirclePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' );
  243. if ( _quality == 0 ) {
  244. _svgCirclePool[id].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  245. }
  246. return _svgCirclePool[ id ];
  247. }
  248. return _svgCirclePool[ id ];
  249. }
  250. };