SVGRenderer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /**
  2. * @author mr.doob / 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. _clipRect = new THREE.Rectangle(),
  13. _bboxRect = new THREE.Rectangle(),
  14. _enableLighting = false,
  15. _color = 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. this.setSize = function( width, height ) {
  41. _svgWidth = width; _svgHeight = height;
  42. _svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
  43. _svg.setAttribute( 'viewBox', ( - _svgWidthHalf ) + ' ' + ( - _svgHeightHalf ) + ' ' + _svgWidth + ' ' + _svgHeight );
  44. _svg.setAttribute( 'width', _svgWidth );
  45. _svg.setAttribute( 'height', _svgHeight );
  46. _clipRect.set( - _svgWidthHalf, - _svgHeightHalf, _svgWidthHalf, _svgHeightHalf );
  47. };
  48. this.clear = function () {
  49. while ( _svg.childNodes.length > 0 ) {
  50. _svg.removeChild( _svg.childNodes[ 0 ] );
  51. }
  52. };
  53. this.render = function ( scene, camera ) {
  54. var e, el, element, material;
  55. this.autoClear && this.clear();
  56. _this.info.render.vertices = 0;
  57. _this.info.render.faces = 0;
  58. _renderData = _projector.projectScene( scene, camera, this.sortElements );
  59. _elements = _renderData.elements;
  60. _lights = _renderData.lights;
  61. _pathCount = 0; _circleCount = 0; _lineCount = 0;
  62. _enableLighting = _lights.length > 0;
  63. if ( _enableLighting ) {
  64. calculateLights( _lights );
  65. }
  66. for ( e = 0, el = _elements.length; e < el; e ++ ) {
  67. element = _elements[ e ];
  68. material = element.material;
  69. if ( material === undefined || material.visible === false ) continue;
  70. _bboxRect.empty();
  71. if ( element instanceof THREE.RenderableParticle ) {
  72. _v1 = element;
  73. _v1.x *= _svgWidthHalf; _v1.y *= -_svgHeightHalf;
  74. renderParticle( _v1, element, material, scene );
  75. } else if ( element instanceof THREE.RenderableLine ) {
  76. _v1 = element.v1; _v2 = element.v2;
  77. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  78. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  79. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  80. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  81. if ( !_clipRect.intersects( _bboxRect ) ) {
  82. continue;
  83. }
  84. renderLine( _v1, _v2, element, material, scene );
  85. } else if ( element instanceof THREE.RenderableFace3 ) {
  86. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  87. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  88. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  89. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;
  90. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  91. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  92. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  93. if ( !_clipRect.intersects( _bboxRect ) ) {
  94. continue;
  95. }
  96. renderFace3( _v1, _v2, _v3, element, material, scene );
  97. } else if ( element instanceof THREE.RenderableFace4 ) {
  98. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3; _v4 = element.v4;
  99. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= -_svgHeightHalf;
  100. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= -_svgHeightHalf;
  101. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= -_svgHeightHalf;
  102. _v4.positionScreen.x *= _svgWidthHalf; _v4.positionScreen.y *= -_svgHeightHalf;
  103. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  104. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  105. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  106. _bboxRect.addPoint( _v4.positionScreen.x, _v4.positionScreen.y );
  107. if ( !_clipRect.intersects( _bboxRect) ) {
  108. continue;
  109. }
  110. renderFace4( _v1, _v2, _v3, _v4, element, material, scene );
  111. }
  112. }
  113. };
  114. function calculateLights( lights ) {
  115. var l, ll, light, lightColor;
  116. _ambientLight.setRGB( 0, 0, 0 );
  117. _directionalLights.setRGB( 0, 0, 0 );
  118. _pointLights.setRGB( 0, 0, 0 );
  119. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  120. light = lights[ l ];
  121. lightColor = light.color;
  122. if ( light instanceof THREE.AmbientLight ) {
  123. _ambientLight.r += lightColor.r;
  124. _ambientLight.g += lightColor.g;
  125. _ambientLight.b += lightColor.b;
  126. } else if ( light instanceof THREE.DirectionalLight ) {
  127. _directionalLights.r += lightColor.r;
  128. _directionalLights.g += lightColor.g;
  129. _directionalLights.b += lightColor.b;
  130. } else if ( light instanceof THREE.PointLight ) {
  131. _pointLights.r += lightColor.r;
  132. _pointLights.g += lightColor.g;
  133. _pointLights.b += lightColor.b;
  134. }
  135. }
  136. }
  137. function calculateLight( lights, position, normal, color ) {
  138. var l, ll, light, lightColor, lightPosition, amount;
  139. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  140. light = lights[ l ];
  141. lightColor = light.color;
  142. if ( light instanceof THREE.DirectionalLight ) {
  143. lightPosition = light.matrixWorld.getPosition().normalize();
  144. 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. lightPosition = light.matrixWorld.getPosition();
  152. amount = normal.dot( _vector3.sub( 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 renderParticle( v1, element, material, scene ) {
  164. /*
  165. _svgNode = getCircleNode( _circleCount++ );
  166. _svgNode.setAttribute( 'cx', v1.x );
  167. _svgNode.setAttribute( 'cy', v1.y );
  168. _svgNode.setAttribute( 'r', element.scale.x * _svgWidthHalf );
  169. if ( material instanceof THREE.ParticleCircleMaterial ) {
  170. if ( _enableLighting ) {
  171. _color.r = _ambientLight.r + _directionalLights.r + _pointLights.r;
  172. _color.g = _ambientLight.g + _directionalLights.g + _pointLights.g;
  173. _color.b = _ambientLight.b + _directionalLights.b + _pointLights.b;
  174. _color.r = material.color.r * _color.r;
  175. _color.g = material.color.g * _color.g;
  176. _color.b = material.color.b * _color.b;
  177. _color.updateStyleString();
  178. } else {
  179. _color = material.color;
  180. }
  181. _svgNode.setAttribute( 'style', 'fill: ' + _color.__styleString );
  182. }
  183. _svg.appendChild( _svgNode );
  184. */
  185. }
  186. function renderLine ( v1, v2, element, material, scene ) {
  187. _svgNode = getLineNode( _lineCount ++ );
  188. _svgNode.setAttribute( 'x1', v1.positionScreen.x );
  189. _svgNode.setAttribute( 'y1', v1.positionScreen.y );
  190. _svgNode.setAttribute( 'x2', v2.positionScreen.x );
  191. _svgNode.setAttribute( 'y2', v2.positionScreen.y );
  192. if ( material instanceof THREE.LineBasicMaterial ) {
  193. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + material.color.getContextStyle() + '; stroke-width: ' + material.linewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.linecap + '; stroke-linejoin: ' + material.linejoin );
  194. _svg.appendChild( _svgNode );
  195. }
  196. }
  197. function renderFace3( v1, v2, v3, element, material, scene ) {
  198. _this.info.render.vertices += 3;
  199. _this.info.render.faces ++;
  200. _svgNode = getPathNode( _pathCount ++ );
  201. _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' );
  202. if ( material instanceof THREE.MeshBasicMaterial ) {
  203. _color.copy( material.color );
  204. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  205. if ( _enableLighting ) {
  206. _color.r = _ambientLight.r;
  207. _color.g = _ambientLight.g;
  208. _color.b = _ambientLight.b;
  209. calculateLight( _lights, element.centroidWorld, element.normalWorld, _color );
  210. _color.r = Math.max( 0, Math.min( material.color.r * _color.r, 1 ) );
  211. _color.g = Math.max( 0, Math.min( material.color.g * _color.g, 1 ) );
  212. _color.b = Math.max( 0, Math.min( material.color.b * _color.b, 1 ) );
  213. } else {
  214. _color.copy( material.color );
  215. }
  216. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  217. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  218. _color.setRGB( _w, _w, _w );
  219. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  220. _color.setRGB( normalToComponent( element.normalWorld.x ), normalToComponent( element.normalWorld.y ), normalToComponent( element.normalWorld.z ) );
  221. }
  222. if ( material.wireframe ) {
  223. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getContextStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  224. } else {
  225. _svgNode.setAttribute( 'style', 'fill: ' + _color.getContextStyle() + '; fill-opacity: ' + material.opacity );
  226. }
  227. _svg.appendChild( _svgNode );
  228. }
  229. function renderFace4( v1, v2, v3, v4, element, material, scene ) {
  230. _this.info.render.vertices += 4;
  231. _this.info.render.faces ++;
  232. _svgNode = getPathNode( _pathCount ++ );
  233. _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' );
  234. if ( material instanceof THREE.MeshBasicMaterial ) {
  235. _color.copy( material.color );
  236. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  237. if ( _enableLighting ) {
  238. _color.r = _ambientLight.r;
  239. _color.g = _ambientLight.g;
  240. _color.b = _ambientLight.b;
  241. calculateLight( _lights, element.centroidWorld, element.normalWorld, _color );
  242. _color.r = Math.max( 0, Math.min( material.color.r * _color.r, 1 ) );
  243. _color.g = Math.max( 0, Math.min( material.color.g * _color.g, 1 ) );
  244. _color.b = Math.max( 0, Math.min( material.color.b * _color.b, 1 ) );
  245. } else {
  246. _color.copy( material.color );
  247. }
  248. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  249. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  250. _color.setRGB( _w, _w, _w );
  251. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  252. _color.setRGB( normalToComponent( element.normalWorld.x ), normalToComponent( element.normalWorld.y ), normalToComponent( element.normalWorld.z ) );
  253. }
  254. if ( material.wireframe ) {
  255. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getContextStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  256. } else {
  257. _svgNode.setAttribute( 'style', 'fill: ' + _color.getContextStyle() + '; fill-opacity: ' + material.opacity );
  258. }
  259. _svg.appendChild( _svgNode );
  260. }
  261. function getLineNode( id ) {
  262. if ( _svgLinePool[ id ] == null ) {
  263. _svgLinePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'line' );
  264. if ( _quality == 0 ) {
  265. _svgLinePool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  266. }
  267. return _svgLinePool[ id ];
  268. }
  269. return _svgLinePool[ id ];
  270. }
  271. function getPathNode( id ) {
  272. if ( _svgPathPool[ id ] == null ) {
  273. _svgPathPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  274. if ( _quality == 0 ) {
  275. _svgPathPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  276. }
  277. return _svgPathPool[ id ];
  278. }
  279. return _svgPathPool[ id ];
  280. }
  281. function getCircleNode( id ) {
  282. if ( _svgCirclePool[id] == null ) {
  283. _svgCirclePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' );
  284. if ( _quality == 0 ) {
  285. _svgCirclePool[id].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  286. }
  287. return _svgCirclePool[ id ];
  288. }
  289. return _svgCirclePool[ id ];
  290. }
  291. function normalToComponent( normal ) {
  292. var component = ( normal + 1 ) * 0.5;
  293. return component < 0 ? 0 : ( component > 1 ? 1 : component );
  294. }
  295. function pad( str ) {
  296. while ( str.length < 6 ) str = '0' + str;
  297. return str;
  298. }
  299. };