2
0

SVGRenderer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. } else if ( element instanceof THREE.RenderableFace4 ) {
  109. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3; _v4 = element.v4;
  110. if ( _v1.positionScreen.z < -1 || _v1.positionScreen.z > 1 ) continue;
  111. if ( _v2.positionScreen.z < -1 || _v2.positionScreen.z > 1 ) continue;
  112. if ( _v3.positionScreen.z < -1 || _v3.positionScreen.z > 1 ) continue;
  113. if ( _v4.positionScreen.z < -1 || _v4.positionScreen.z > 1 ) continue;
  114. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= -_svgHeightHalf;
  115. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= -_svgHeightHalf;
  116. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= -_svgHeightHalf;
  117. _v4.positionScreen.x *= _svgWidthHalf; _v4.positionScreen.y *= -_svgHeightHalf;
  118. _elemBox.setFromPoints( [
  119. _v1.positionScreen,
  120. _v2.positionScreen,
  121. _v3.positionScreen,
  122. _v4.positionScreen
  123. ] );
  124. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  125. renderFace4( _v1, _v2, _v3, _v4, element, material );
  126. }
  127. }
  128. }
  129. };
  130. function calculateLights( lights ) {
  131. _ambientLight.setRGB( 0, 0, 0 );
  132. _directionalLights.setRGB( 0, 0, 0 );
  133. _pointLights.setRGB( 0, 0, 0 );
  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.AmbientLight ) {
  138. _ambientLight.r += lightColor.r;
  139. _ambientLight.g += lightColor.g;
  140. _ambientLight.b += lightColor.b;
  141. } else if ( light instanceof THREE.DirectionalLight ) {
  142. _directionalLights.r += lightColor.r;
  143. _directionalLights.g += lightColor.g;
  144. _directionalLights.b += lightColor.b;
  145. } else if ( light instanceof THREE.PointLight ) {
  146. _pointLights.r += lightColor.r;
  147. _pointLights.g += lightColor.g;
  148. _pointLights.b += lightColor.b;
  149. }
  150. }
  151. }
  152. function calculateLight( lights, position, normal, color ) {
  153. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  154. var light = lights[ l ];
  155. var lightColor = light.color;
  156. if ( light instanceof THREE.DirectionalLight ) {
  157. var lightPosition = _vector3.getPositionFromMatrix( light.matrixWorld ).normalize();
  158. var amount = normal.dot( lightPosition );
  159. if ( amount <= 0 ) continue;
  160. amount *= light.intensity;
  161. color.r += lightColor.r * amount;
  162. color.g += lightColor.g * amount;
  163. color.b += lightColor.b * amount;
  164. } else if ( light instanceof THREE.PointLight ) {
  165. var lightPosition = _vector3.getPositionFromMatrix( light.matrixWorld );
  166. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  167. if ( amount <= 0 ) continue;
  168. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  169. if ( amount == 0 ) continue;
  170. amount *= light.intensity;
  171. color.r += lightColor.r * amount;
  172. color.g += lightColor.g * amount;
  173. color.b += lightColor.b * amount;
  174. }
  175. }
  176. }
  177. function renderParticle( v1, element, material ) {
  178. /*
  179. _svgNode = getCircleNode( _circleCount++ );
  180. _svgNode.setAttribute( 'cx', v1.x );
  181. _svgNode.setAttribute( 'cy', v1.y );
  182. _svgNode.setAttribute( 'r', element.scale.x * _svgWidthHalf );
  183. if ( material instanceof THREE.ParticleCircleMaterial ) {
  184. _color.r = _ambientLight.r + _directionalLights.r + _pointLights.r;
  185. _color.g = _ambientLight.g + _directionalLights.g + _pointLights.g;
  186. _color.b = _ambientLight.b + _directionalLights.b + _pointLights.b;
  187. _color.r = material.color.r * _color.r;
  188. _color.g = material.color.g * _color.g;
  189. _color.b = material.color.b * _color.b;
  190. _color.updateStyleString();
  191. _svgNode.setAttribute( 'style', 'fill: ' + _color.__styleString );
  192. }
  193. _svg.appendChild( _svgNode );
  194. */
  195. }
  196. function renderLine ( v1, v2, element, material ) {
  197. _svgNode = getLineNode( _lineCount ++ );
  198. _svgNode.setAttribute( 'x1', v1.positionScreen.x );
  199. _svgNode.setAttribute( 'y1', v1.positionScreen.y );
  200. _svgNode.setAttribute( 'x2', v2.positionScreen.x );
  201. _svgNode.setAttribute( 'y2', v2.positionScreen.y );
  202. if ( material instanceof THREE.LineBasicMaterial ) {
  203. _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 );
  204. _svg.appendChild( _svgNode );
  205. }
  206. }
  207. function renderFace3( v1, v2, v3, element, material ) {
  208. _this.info.render.vertices += 3;
  209. _this.info.render.faces ++;
  210. _svgNode = getPathNode( _pathCount ++ );
  211. _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' );
  212. if ( material instanceof THREE.MeshBasicMaterial ) {
  213. _color.copy( material.color );
  214. if ( material.vertexColors === THREE.FaceColors ) {
  215. _color.multiply( element.color );
  216. }
  217. } else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
  218. _diffuseColor.copy( material.color );
  219. if ( material.vertexColors === THREE.FaceColors ) {
  220. _diffuseColor.multiply( element.color );
  221. }
  222. _color.copy( _ambientLight );
  223. calculateLight( _lights, element.centroidModel, element.normalModel, _color );
  224. _color.multiply( _diffuseColor ).add( material.emissive );
  225. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  226. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  227. _color.setRGB( _w, _w, _w );
  228. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  229. var normal = element.normalModelView;
  230. _color.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  231. }
  232. if ( material.wireframe ) {
  233. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  234. } else {
  235. _svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
  236. }
  237. _svg.appendChild( _svgNode );
  238. }
  239. function renderFace4( v1, v2, v3, v4, element, material ) {
  240. _this.info.render.vertices += 4;
  241. _this.info.render.faces ++;
  242. _svgNode = getPathNode( _pathCount ++ );
  243. _svgNode.setAttribute( 'd', 'M ' + v1.positionScreen.x + ' ' + v1.positionScreen.y + ' L ' + v2.positionScreen.x + ' ' + v2.positionScreen.y + ' L ' + v3.positionScreen.x + ',' + v3.positionScreen.y + ' L ' + v4.positionScreen.x + ',' + v4.positionScreen.y + 'z' );
  244. if ( material instanceof THREE.MeshBasicMaterial ) {
  245. _color.copy( material.color );
  246. if ( material.vertexColors === THREE.FaceColors ) {
  247. _color.multiply( element.color );
  248. }
  249. } else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
  250. _diffuseColor.copy( material.color );
  251. if ( material.vertexColors === THREE.FaceColors ) {
  252. _diffuseColor.multiply( element.color );
  253. }
  254. _color.copy( _ambientLight );
  255. calculateLight( _lights, element.centroidModel, element.normalModel, _color );
  256. _color.multiply( _diffuseColor ).add( material.emissive );
  257. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  258. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  259. _color.setRGB( _w, _w, _w );
  260. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  261. var normal = element.normalModelView;
  262. _color.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  263. }
  264. if ( material.wireframe ) {
  265. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  266. } else {
  267. _svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
  268. }
  269. _svg.appendChild( _svgNode );
  270. }
  271. function getLineNode( id ) {
  272. if ( _svgLinePool[ id ] == null ) {
  273. _svgLinePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'line' );
  274. if ( _quality == 0 ) {
  275. _svgLinePool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  276. }
  277. return _svgLinePool[ id ];
  278. }
  279. return _svgLinePool[ id ];
  280. }
  281. function getPathNode( id ) {
  282. if ( _svgPathPool[ id ] == null ) {
  283. _svgPathPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  284. if ( _quality == 0 ) {
  285. _svgPathPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  286. }
  287. return _svgPathPool[ id ];
  288. }
  289. return _svgPathPool[ id ];
  290. }
  291. function getCircleNode( id ) {
  292. if ( _svgCirclePool[id] == null ) {
  293. _svgCirclePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' );
  294. if ( _quality == 0 ) {
  295. _svgCirclePool[id].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  296. }
  297. return _svgCirclePool[ id ];
  298. }
  299. return _svgCirclePool[ id ];
  300. }
  301. function pad( str ) {
  302. while ( str.length < 6 ) str = '0' + str;
  303. return str;
  304. }
  305. };