SVGRenderer.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. _clipRect = new THREE.Rectangle(),
  13. _bboxRect = new THREE.Rectangle(),
  14. _enableLighting = false,
  15. _color = new THREE.Color(),
  16. _diffuseColor = new THREE.Color(),
  17. _emissiveColor = new THREE.Color(),
  18. _ambientLight = new THREE.Color(),
  19. _directionalLights = new THREE.Color(),
  20. _pointLights = new THREE.Color(),
  21. _w, // z-buffer to w-buffer
  22. _vector3 = new THREE.Vector3(), // Needed for PointLight
  23. _svgPathPool = [], _svgCirclePool = [], _svgLinePool = [],
  24. _svgNode, _pathCount, _circleCount, _lineCount,
  25. _quality = 1;
  26. this.domElement = _svg;
  27. this.autoClear = true;
  28. this.sortObjects = true;
  29. this.sortElements = true;
  30. this.info = {
  31. render: {
  32. vertices: 0,
  33. faces: 0
  34. }
  35. }
  36. this.setQuality = function( quality ) {
  37. switch(quality) {
  38. case "high": _quality = 1; break;
  39. case "low": _quality = 0; break;
  40. }
  41. };
  42. this.setSize = function( width, height ) {
  43. _svgWidth = width; _svgHeight = height;
  44. _svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
  45. _svg.setAttribute( 'viewBox', ( - _svgWidthHalf ) + ' ' + ( - _svgHeightHalf ) + ' ' + _svgWidth + ' ' + _svgHeight );
  46. _svg.setAttribute( 'width', _svgWidth );
  47. _svg.setAttribute( 'height', _svgHeight );
  48. _clipRect.set( - _svgWidthHalf, - _svgHeightHalf, _svgWidthHalf, _svgHeightHalf );
  49. };
  50. this.clear = function () {
  51. while ( _svg.childNodes.length > 0 ) {
  52. _svg.removeChild( _svg.childNodes[ 0 ] );
  53. }
  54. };
  55. this.render = function ( scene, camera ) {
  56. if ( camera instanceof THREE.Camera === false ) {
  57. console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
  58. return;
  59. }
  60. var e, el, element, material;
  61. this.autoClear && this.clear();
  62. _this.info.render.vertices = 0;
  63. _this.info.render.faces = 0;
  64. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  65. _elements = _renderData.elements;
  66. _lights = _renderData.lights;
  67. _pathCount = 0; _circleCount = 0; _lineCount = 0;
  68. _enableLighting = _lights.length > 0;
  69. if ( _enableLighting ) {
  70. calculateLights( _lights );
  71. }
  72. for ( e = 0, el = _elements.length; e < el; e ++ ) {
  73. element = _elements[ e ];
  74. material = element.material;
  75. if ( material === undefined || material.visible === false ) continue;
  76. _bboxRect.empty();
  77. if ( element instanceof THREE.RenderableParticle ) {
  78. _v1 = element;
  79. _v1.x *= _svgWidthHalf; _v1.y *= -_svgHeightHalf;
  80. renderParticle( _v1, element, material, scene );
  81. } else if ( element instanceof THREE.RenderableLine ) {
  82. _v1 = element.v1; _v2 = element.v2;
  83. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  84. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  85. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  86. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  87. if ( !_clipRect.intersects( _bboxRect ) ) {
  88. continue;
  89. }
  90. renderLine( _v1, _v2, element, material, scene );
  91. } else if ( element instanceof THREE.RenderableFace3 ) {
  92. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  93. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  94. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  95. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;
  96. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  97. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  98. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  99. if ( !_clipRect.intersects( _bboxRect ) ) {
  100. continue;
  101. }
  102. renderFace3( _v1, _v2, _v3, element, material, scene );
  103. } else if ( element instanceof THREE.RenderableFace4 ) {
  104. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3; _v4 = element.v4;
  105. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= -_svgHeightHalf;
  106. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= -_svgHeightHalf;
  107. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= -_svgHeightHalf;
  108. _v4.positionScreen.x *= _svgWidthHalf; _v4.positionScreen.y *= -_svgHeightHalf;
  109. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  110. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  111. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  112. _bboxRect.addPoint( _v4.positionScreen.x, _v4.positionScreen.y );
  113. if ( !_clipRect.intersects( _bboxRect) ) {
  114. continue;
  115. }
  116. renderFace4( _v1, _v2, _v3, _v4, element, material, scene );
  117. }
  118. }
  119. };
  120. function calculateLights( lights ) {
  121. var l, ll, light, lightColor;
  122. _ambientLight.setRGB( 0, 0, 0 );
  123. _directionalLights.setRGB( 0, 0, 0 );
  124. _pointLights.setRGB( 0, 0, 0 );
  125. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  126. light = lights[ l ];
  127. lightColor = light.color;
  128. if ( light instanceof THREE.AmbientLight ) {
  129. _ambientLight.r += lightColor.r;
  130. _ambientLight.g += lightColor.g;
  131. _ambientLight.b += lightColor.b;
  132. } else if ( light instanceof THREE.DirectionalLight ) {
  133. _directionalLights.r += lightColor.r;
  134. _directionalLights.g += lightColor.g;
  135. _directionalLights.b += lightColor.b;
  136. } else if ( light instanceof THREE.PointLight ) {
  137. _pointLights.r += lightColor.r;
  138. _pointLights.g += lightColor.g;
  139. _pointLights.b += lightColor.b;
  140. }
  141. }
  142. }
  143. function calculateLight( lights, position, normal, color ) {
  144. var l, ll, light, lightColor, lightPosition, amount;
  145. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  146. light = lights[ l ];
  147. lightColor = light.color;
  148. if ( light instanceof THREE.DirectionalLight ) {
  149. lightPosition = light.matrixWorld.getPosition().normalize();
  150. amount = normal.dot( lightPosition );
  151. if ( amount <= 0 ) continue;
  152. amount *= light.intensity;
  153. color.r += lightColor.r * amount;
  154. color.g += lightColor.g * amount;
  155. color.b += lightColor.b * amount;
  156. } else if ( light instanceof THREE.PointLight ) {
  157. lightPosition = light.matrixWorld.getPosition();
  158. amount = normal.dot( _vector3.sub( lightPosition, position ).normalize() );
  159. if ( amount <= 0 ) continue;
  160. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  161. if ( amount == 0 ) continue;
  162. amount *= light.intensity;
  163. color.r += lightColor.r * amount;
  164. color.g += lightColor.g * amount;
  165. color.b += lightColor.b * amount;
  166. }
  167. }
  168. }
  169. function renderParticle( v1, element, material, scene ) {
  170. /*
  171. _svgNode = getCircleNode( _circleCount++ );
  172. _svgNode.setAttribute( 'cx', v1.x );
  173. _svgNode.setAttribute( 'cy', v1.y );
  174. _svgNode.setAttribute( 'r', element.scale.x * _svgWidthHalf );
  175. if ( material instanceof THREE.ParticleCircleMaterial ) {
  176. if ( _enableLighting ) {
  177. _color.r = _ambientLight.r + _directionalLights.r + _pointLights.r;
  178. _color.g = _ambientLight.g + _directionalLights.g + _pointLights.g;
  179. _color.b = _ambientLight.b + _directionalLights.b + _pointLights.b;
  180. _color.r = material.color.r * _color.r;
  181. _color.g = material.color.g * _color.g;
  182. _color.b = material.color.b * _color.b;
  183. _color.updateStyleString();
  184. } else {
  185. _color = material.color;
  186. }
  187. _svgNode.setAttribute( 'style', 'fill: ' + _color.__styleString );
  188. }
  189. _svg.appendChild( _svgNode );
  190. */
  191. }
  192. function renderLine ( v1, v2, element, material, scene ) {
  193. _svgNode = getLineNode( _lineCount ++ );
  194. _svgNode.setAttribute( 'x1', v1.positionScreen.x );
  195. _svgNode.setAttribute( 'y1', v1.positionScreen.y );
  196. _svgNode.setAttribute( 'x2', v2.positionScreen.x );
  197. _svgNode.setAttribute( 'y2', v2.positionScreen.y );
  198. if ( material instanceof THREE.LineBasicMaterial ) {
  199. _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 );
  200. _svg.appendChild( _svgNode );
  201. }
  202. }
  203. function renderFace3( v1, v2, v3, element, material, scene ) {
  204. _this.info.render.vertices += 3;
  205. _this.info.render.faces ++;
  206. _svgNode = getPathNode( _pathCount ++ );
  207. _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' );
  208. if ( material instanceof THREE.MeshBasicMaterial ) {
  209. _color.copy( material.color );
  210. if ( material.vertexColors === THREE.FaceColors ) {
  211. _color.r *= element.color.r;
  212. _color.g *= element.color.g;
  213. _color.b *= element.color.b;
  214. }
  215. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  216. _diffuseColor.copy( material.color );
  217. _emissiveColor.copy( material.emissive );
  218. if ( material.vertexColors === THREE.FaceColors ) {
  219. _diffuseColor.r *= element.color.r;
  220. _diffuseColor.g *= element.color.g;
  221. _diffuseColor.b *= element.color.b;
  222. }
  223. if ( _enableLighting ) {
  224. _color.r = _ambientLight.r;
  225. _color.g = _ambientLight.g;
  226. _color.b = _ambientLight.b;
  227. calculateLight( _lights, element.centroidWorld, element.normalWorld, _color );
  228. _color.r = _color.r * _diffuseColor.r + _emissiveColor.r;
  229. _color.g = _color.g * _diffuseColor.g + _emissiveColor.g;
  230. _color.b = _color.b * _diffuseColor.b + _emissiveColor.b;
  231. } else {
  232. _color.copy( _diffuseColor );
  233. }
  234. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  235. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  236. _color.setRGB( _w, _w, _w );
  237. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  238. _color.setRGB( normalToComponent( element.normalWorld.x ), normalToComponent( element.normalWorld.y ), normalToComponent( element.normalWorld.z ) );
  239. }
  240. if ( material.wireframe ) {
  241. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getContextStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  242. } else {
  243. _svgNode.setAttribute( 'style', 'fill: ' + _color.getContextStyle() + '; fill-opacity: ' + material.opacity );
  244. }
  245. _svg.appendChild( _svgNode );
  246. }
  247. function renderFace4( v1, v2, v3, v4, element, material, scene ) {
  248. _this.info.render.vertices += 4;
  249. _this.info.render.faces ++;
  250. _svgNode = getPathNode( _pathCount ++ );
  251. _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' );
  252. if ( material instanceof THREE.MeshBasicMaterial ) {
  253. _color.copy( material.color );
  254. if ( material.vertexColors === THREE.FaceColors ) {
  255. _color.r *= element.color.r;
  256. _color.g *= element.color.g;
  257. _color.b *= element.color.b;
  258. }
  259. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  260. _diffuseColor.copy( material.color );
  261. _emissiveColor.copy( material.emissive );
  262. if ( material.vertexColors === THREE.FaceColors ) {
  263. _diffuseColor.r *= element.color.r;
  264. _diffuseColor.g *= element.color.g;
  265. _diffuseColor.b *= element.color.b;
  266. }
  267. if ( _enableLighting ) {
  268. _color.r = _ambientLight.r;
  269. _color.g = _ambientLight.g;
  270. _color.b = _ambientLight.b;
  271. calculateLight( _lights, element.centroidWorld, element.normalWorld, _color );
  272. _color.r = _color.r * _diffuseColor.r + _emissiveColor.r;
  273. _color.g = _color.g * _diffuseColor.g + _emissiveColor.g;
  274. _color.b = _color.b * _diffuseColor.b + _emissiveColor.b;
  275. } else {
  276. _color.copy( _diffuseColor );
  277. }
  278. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  279. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  280. _color.setRGB( _w, _w, _w );
  281. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  282. _color.setRGB( normalToComponent( element.normalWorld.x ), normalToComponent( element.normalWorld.y ), normalToComponent( element.normalWorld.z ) );
  283. }
  284. if ( material.wireframe ) {
  285. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getContextStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  286. } else {
  287. _svgNode.setAttribute( 'style', 'fill: ' + _color.getContextStyle() + '; fill-opacity: ' + material.opacity );
  288. }
  289. _svg.appendChild( _svgNode );
  290. }
  291. function getLineNode( id ) {
  292. if ( _svgLinePool[ id ] == null ) {
  293. _svgLinePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'line' );
  294. if ( _quality == 0 ) {
  295. _svgLinePool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  296. }
  297. return _svgLinePool[ id ];
  298. }
  299. return _svgLinePool[ id ];
  300. }
  301. function getPathNode( id ) {
  302. if ( _svgPathPool[ id ] == null ) {
  303. _svgPathPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  304. if ( _quality == 0 ) {
  305. _svgPathPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  306. }
  307. return _svgPathPool[ id ];
  308. }
  309. return _svgPathPool[ id ];
  310. }
  311. function getCircleNode( id ) {
  312. if ( _svgCirclePool[id] == null ) {
  313. _svgCirclePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' );
  314. if ( _quality == 0 ) {
  315. _svgCirclePool[id].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  316. }
  317. return _svgCirclePool[ id ];
  318. }
  319. return _svgCirclePool[ id ];
  320. }
  321. function normalToComponent( normal ) {
  322. var component = ( normal + 1 ) * 0.5;
  323. return component < 0 ? 0 : ( component > 1 ? 1 : component );
  324. }
  325. function pad( str ) {
  326. while ( str.length < 6 ) str = '0' + str;
  327. return str;
  328. }
  329. };