SVGRenderer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. material = material instanceof THREE.MeshFaceMaterial ? element.faceMaterial : material;
  70. if ( material === undefined || material.visible === false ) continue;
  71. _bboxRect.empty();
  72. if ( element instanceof THREE.RenderableParticle ) {
  73. _v1 = element;
  74. _v1.x *= _svgWidthHalf; _v1.y *= -_svgHeightHalf;
  75. renderParticle( _v1, element, material, scene );
  76. } else if ( element instanceof THREE.RenderableLine ) {
  77. _v1 = element.v1; _v2 = element.v2;
  78. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  79. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  80. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  81. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  82. if ( !_clipRect.intersects( _bboxRect ) ) {
  83. continue;
  84. }
  85. renderLine( _v1, _v2, element, material, scene );
  86. } else if ( element instanceof THREE.RenderableFace3 ) {
  87. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  88. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  89. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  90. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;
  91. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  92. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  93. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  94. if ( !_clipRect.intersects( _bboxRect ) ) {
  95. continue;
  96. }
  97. renderFace3( _v1, _v2, _v3, element, material, scene );
  98. } else if ( element instanceof THREE.RenderableFace4 ) {
  99. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3; _v4 = element.v4;
  100. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= -_svgHeightHalf;
  101. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= -_svgHeightHalf;
  102. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= -_svgHeightHalf;
  103. _v4.positionScreen.x *= _svgWidthHalf; _v4.positionScreen.y *= -_svgHeightHalf;
  104. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  105. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  106. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  107. _bboxRect.addPoint( _v4.positionScreen.x, _v4.positionScreen.y );
  108. if ( !_clipRect.intersects( _bboxRect) ) {
  109. continue;
  110. }
  111. renderFace4( _v1, _v2, _v3, _v4, element, material, scene );
  112. }
  113. }
  114. };
  115. function calculateLights( lights ) {
  116. var l, ll, light, lightColor;
  117. _ambientLight.setRGB( 0, 0, 0 );
  118. _directionalLights.setRGB( 0, 0, 0 );
  119. _pointLights.setRGB( 0, 0, 0 );
  120. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  121. light = lights[ l ];
  122. 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. var l, ll, light, lightColor, lightPosition, amount;
  140. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  141. light = lights[ l ];
  142. lightColor = light.color;
  143. if ( light instanceof THREE.DirectionalLight ) {
  144. lightPosition = light.matrixWorld.getPosition();
  145. amount = normal.dot( lightPosition );
  146. if ( amount <= 0 ) continue;
  147. amount *= light.intensity;
  148. color.r += lightColor.r * amount;
  149. color.g += lightColor.g * amount;
  150. color.b += lightColor.b * amount;
  151. } else if ( light instanceof THREE.PointLight ) {
  152. lightPosition = light.matrixWorld.getPosition();
  153. amount = normal.dot( _vector3.sub( lightPosition, position ).normalize() );
  154. if ( amount <= 0 ) continue;
  155. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  156. if ( amount == 0 ) continue;
  157. amount *= light.intensity;
  158. color.r += lightColor.r * amount;
  159. color.g += lightColor.g * amount;
  160. color.b += lightColor.b * amount;
  161. }
  162. }
  163. }
  164. function renderParticle( v1, element, material, scene ) {
  165. /*
  166. _svgNode = getCircleNode( _circleCount++ );
  167. _svgNode.setAttribute( 'cx', v1.x );
  168. _svgNode.setAttribute( 'cy', v1.y );
  169. _svgNode.setAttribute( 'r', element.scale.x * _svgWidthHalf );
  170. if ( material instanceof THREE.ParticleCircleMaterial ) {
  171. if ( _enableLighting ) {
  172. _color.r = _ambientLight.r + _directionalLights.r + _pointLights.r;
  173. _color.g = _ambientLight.g + _directionalLights.g + _pointLights.g;
  174. _color.b = _ambientLight.b + _directionalLights.b + _pointLights.b;
  175. _color.r = material.color.r * _color.r;
  176. _color.g = material.color.g * _color.g;
  177. _color.b = material.color.b * _color.b;
  178. _color.updateStyleString();
  179. } else {
  180. _color = material.color;
  181. }
  182. _svgNode.setAttribute( 'style', 'fill: ' + _color.__styleString );
  183. }
  184. _svg.appendChild( _svgNode );
  185. */
  186. }
  187. function renderLine ( v1, v2, element, material, scene ) {
  188. _svgNode = getLineNode( _lineCount ++ );
  189. _svgNode.setAttribute( 'x1', v1.positionScreen.x );
  190. _svgNode.setAttribute( 'y1', v1.positionScreen.y );
  191. _svgNode.setAttribute( 'x2', v2.positionScreen.x );
  192. _svgNode.setAttribute( 'y2', v2.positionScreen.y );
  193. if ( material instanceof THREE.LineBasicMaterial ) {
  194. _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 );
  195. _svg.appendChild( _svgNode );
  196. }
  197. }
  198. function renderFace3( v1, v2, v3, element, material, scene ) {
  199. _this.info.render.vertices += 3;
  200. _this.info.render.faces ++;
  201. _svgNode = getPathNode( _pathCount ++ );
  202. _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' );
  203. if ( material instanceof THREE.MeshBasicMaterial ) {
  204. _color.copy( material.color );
  205. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  206. if ( _enableLighting ) {
  207. _color.r = _ambientLight.r;
  208. _color.g = _ambientLight.g;
  209. _color.b = _ambientLight.b;
  210. calculateLight( _lights, element.centroidWorld, element.normalWorld, _color );
  211. _color.r = Math.max( 0, Math.min( material.color.r * _color.r, 1 ) );
  212. _color.g = Math.max( 0, Math.min( material.color.g * _color.g, 1 ) );
  213. _color.b = Math.max( 0, Math.min( material.color.b * _color.b, 1 ) );
  214. } else {
  215. _color.copy( material.color );
  216. }
  217. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  218. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  219. _color.setRGB( _w, _w, _w );
  220. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  221. _color.setRGB( normalToComponent( element.normalWorld.x ), normalToComponent( element.normalWorld.y ), normalToComponent( element.normalWorld.z ) );
  222. }
  223. if ( material.wireframe ) {
  224. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getContextStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  225. } else {
  226. _svgNode.setAttribute( 'style', 'fill: ' + _color.getContextStyle() + '; fill-opacity: ' + material.opacity );
  227. }
  228. _svg.appendChild( _svgNode );
  229. }
  230. function renderFace4( v1, v2, v3, v4, element, material, scene ) {
  231. _this.info.render.vertices += 4;
  232. _this.info.render.faces ++;
  233. _svgNode = getPathNode( _pathCount ++ );
  234. _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' );
  235. if ( material instanceof THREE.MeshBasicMaterial ) {
  236. _color.copy( material.color );
  237. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  238. if ( _enableLighting ) {
  239. _color.r = _ambientLight.r;
  240. _color.g = _ambientLight.g;
  241. _color.b = _ambientLight.b;
  242. calculateLight( _lights, element.centroidWorld, element.normalWorld, _color );
  243. _color.r = Math.max( 0, Math.min( material.color.r * _color.r, 1 ) );
  244. _color.g = Math.max( 0, Math.min( material.color.g * _color.g, 1 ) );
  245. _color.b = Math.max( 0, Math.min( material.color.b * _color.b, 1 ) );
  246. } else {
  247. _color.copy( material.color );
  248. }
  249. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  250. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  251. _color.setRGB( _w, _w, _w );
  252. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  253. _color.setRGB( normalToComponent( element.normalWorld.x ), normalToComponent( element.normalWorld.y ), normalToComponent( element.normalWorld.z ) );
  254. }
  255. if ( material.wireframe ) {
  256. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getContextStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  257. } else {
  258. _svgNode.setAttribute( 'style', 'fill: ' + _color.getContextStyle() + '; fill-opacity: ' + material.opacity );
  259. }
  260. _svg.appendChild( _svgNode );
  261. }
  262. function getLineNode( id ) {
  263. if ( _svgLinePool[ id ] == null ) {
  264. _svgLinePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'line' );
  265. if ( _quality == 0 ) {
  266. _svgLinePool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  267. }
  268. return _svgLinePool[ id ];
  269. }
  270. return _svgLinePool[ id ];
  271. }
  272. function getPathNode( id ) {
  273. if ( _svgPathPool[ id ] == null ) {
  274. _svgPathPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  275. if ( _quality == 0 ) {
  276. _svgPathPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  277. }
  278. return _svgPathPool[ id ];
  279. }
  280. return _svgPathPool[ id ];
  281. }
  282. function getCircleNode( id ) {
  283. if ( _svgCirclePool[id] == null ) {
  284. _svgCirclePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' );
  285. if ( _quality == 0 ) {
  286. _svgCirclePool[id].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  287. }
  288. return _svgCirclePool[ id ];
  289. }
  290. return _svgCirclePool[ id ];
  291. }
  292. function normalToComponent( normal ) {
  293. var component = ( normal + 1 ) * 0.5;
  294. return component < 0 ? 0 : ( component > 1 ? 1 : component );
  295. }
  296. function pad( str ) {
  297. while ( str.length < 6 ) str = '0' + str;
  298. return str;
  299. }
  300. };