2
0

SVGRenderer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. _emissiveColor = new THREE.Color(),
  17. _ambientLight = new THREE.Color(),
  18. _directionalLights = new THREE.Color(),
  19. _pointLights = new THREE.Color(),
  20. _w, // z-buffer to w-buffer
  21. _vector3 = new THREE.Vector3(), // Needed for PointLight
  22. _svgPathPool = [], _svgCirclePool = [], _svgLinePool = [],
  23. _svgNode, _pathCount, _circleCount, _lineCount,
  24. _quality = 1;
  25. this.domElement = _svg;
  26. this.autoClear = true;
  27. this.sortObjects = true;
  28. this.sortElements = true;
  29. this.info = {
  30. render: {
  31. vertices: 0,
  32. faces: 0
  33. }
  34. }
  35. this.setQuality = function( quality ) {
  36. switch(quality) {
  37. case "high": _quality = 1; break;
  38. case "low": _quality = 0; break;
  39. }
  40. };
  41. // WebGLRenderer compatibility
  42. this.supportsVertexTextures = function () {};
  43. this.setFaceCulling = function () {};
  44. this.setSize = function( width, height ) {
  45. _svgWidth = width; _svgHeight = height;
  46. _svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
  47. _svg.setAttribute( 'viewBox', ( - _svgWidthHalf ) + ' ' + ( - _svgHeightHalf ) + ' ' + _svgWidth + ' ' + _svgHeight );
  48. _svg.setAttribute( 'width', _svgWidth );
  49. _svg.setAttribute( 'height', _svgHeight );
  50. _clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
  51. _clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
  52. };
  53. this.clear = function () {
  54. while ( _svg.childNodes.length > 0 ) {
  55. _svg.removeChild( _svg.childNodes[ 0 ] );
  56. }
  57. };
  58. this.render = function ( scene, camera ) {
  59. if ( camera instanceof THREE.Camera === false ) {
  60. console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
  61. return;
  62. }
  63. var e, el, element, material;
  64. this.autoClear && this.clear();
  65. _this.info.render.vertices = 0;
  66. _this.info.render.faces = 0;
  67. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  68. _elements = _renderData.elements;
  69. _lights = _renderData.lights;
  70. _pathCount = 0; _circleCount = 0; _lineCount = 0;
  71. calculateLights( _lights );
  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. _elemBox.makeEmpty();
  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. _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen ] );
  86. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  87. renderLine( _v1, _v2, element, material, scene );
  88. }
  89. } else if ( element instanceof THREE.RenderableFace3 ) {
  90. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  91. if ( _v1.positionScreen.z < -1 || _v1.positionScreen.z > 1 ) continue;
  92. if ( _v2.positionScreen.z < -1 || _v2.positionScreen.z > 1 ) continue;
  93. if ( _v3.positionScreen.z < -1 || _v3.positionScreen.z > 1 ) continue;
  94. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  95. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  96. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;
  97. _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen, _v3.positionScreen ] );
  98. renderFace3( _v1, _v2, _v3, element, material, scene );
  99. } else if ( element instanceof THREE.RenderableFace4 ) {
  100. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3; _v4 = element.v4;
  101. if ( _v1.positionScreen.z < -1 || _v1.positionScreen.z > 1 ) continue;
  102. if ( _v2.positionScreen.z < -1 || _v2.positionScreen.z > 1 ) continue;
  103. if ( _v3.positionScreen.z < -1 || _v3.positionScreen.z > 1 ) continue;
  104. if ( _v4.positionScreen.z < -1 || _v4.positionScreen.z > 1 ) continue;
  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. _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen, _v3.positionScreen, _v4.positionScreen ] );
  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. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  139. var light = lights[ l ];
  140. var lightColor = light.color;
  141. if ( light instanceof THREE.DirectionalLight ) {
  142. var lightPosition = _vector3.getPositionFromMatrix( light.matrixWorld ).normalize();
  143. var amount = normal.dot( lightPosition );
  144. if ( amount <= 0 ) continue;
  145. amount *= light.intensity;
  146. color.r += lightColor.r * amount;
  147. color.g += lightColor.g * amount;
  148. color.b += lightColor.b * amount;
  149. } else if ( light instanceof THREE.PointLight ) {
  150. var lightPosition = _vector3.getPositionFromMatrix( light.matrixWorld );
  151. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  152. if ( amount <= 0 ) continue;
  153. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  154. if ( amount == 0 ) continue;
  155. amount *= light.intensity;
  156. color.r += lightColor.r * amount;
  157. color.g += lightColor.g * amount;
  158. color.b += lightColor.b * amount;
  159. }
  160. }
  161. }
  162. function renderParticle( v1, element, material, scene ) {
  163. /*
  164. _svgNode = getCircleNode( _circleCount++ );
  165. _svgNode.setAttribute( 'cx', v1.x );
  166. _svgNode.setAttribute( 'cy', v1.y );
  167. _svgNode.setAttribute( 'r', element.scale.x * _svgWidthHalf );
  168. if ( material instanceof THREE.ParticleCircleMaterial ) {
  169. _color.r = _ambientLight.r + _directionalLights.r + _pointLights.r;
  170. _color.g = _ambientLight.g + _directionalLights.g + _pointLights.g;
  171. _color.b = _ambientLight.b + _directionalLights.b + _pointLights.b;
  172. _color.r = material.color.r * _color.r;
  173. _color.g = material.color.g * _color.g;
  174. _color.b = material.color.b * _color.b;
  175. _color.updateStyleString();
  176. _svgNode.setAttribute( 'style', 'fill: ' + _color.__styleString );
  177. }
  178. _svg.appendChild( _svgNode );
  179. */
  180. }
  181. function renderLine ( v1, v2, element, material, scene ) {
  182. _svgNode = getLineNode( _lineCount ++ );
  183. _svgNode.setAttribute( 'x1', v1.positionScreen.x );
  184. _svgNode.setAttribute( 'y1', v1.positionScreen.y );
  185. _svgNode.setAttribute( 'x2', v2.positionScreen.x );
  186. _svgNode.setAttribute( 'y2', v2.positionScreen.y );
  187. if ( material instanceof THREE.LineBasicMaterial ) {
  188. _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 );
  189. _svg.appendChild( _svgNode );
  190. }
  191. }
  192. function renderFace3( v1, v2, v3, element, material, scene ) {
  193. _this.info.render.vertices += 3;
  194. _this.info.render.faces ++;
  195. _svgNode = getPathNode( _pathCount ++ );
  196. _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' );
  197. if ( material instanceof THREE.MeshBasicMaterial ) {
  198. _color.copy( material.color );
  199. if ( material.vertexColors === THREE.FaceColors ) {
  200. _color.multiply( element.color );
  201. }
  202. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  203. _diffuseColor.copy( material.color );
  204. _emissiveColor.copy( material.emissive );
  205. if ( material.vertexColors === THREE.FaceColors ) {
  206. _diffuseColor.multiply( element.color );
  207. }
  208. _color.copy( _ambientLight );
  209. calculateLight( _lights, element.centroidModel, element.normalModel, _color );
  210. _color.multiply( _diffuseColor ).add( _emissiveColor );
  211. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  212. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  213. _color.setRGB( _w, _w, _w );
  214. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  215. var normal = element.normalModelView;
  216. _color.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  217. }
  218. if ( material.wireframe ) {
  219. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  220. } else {
  221. _svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
  222. }
  223. _svg.appendChild( _svgNode );
  224. }
  225. function renderFace4( v1, v2, v3, v4, element, material, scene ) {
  226. _this.info.render.vertices += 4;
  227. _this.info.render.faces ++;
  228. _svgNode = getPathNode( _pathCount ++ );
  229. _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' );
  230. if ( material instanceof THREE.MeshBasicMaterial ) {
  231. _color.copy( material.color );
  232. if ( material.vertexColors === THREE.FaceColors ) {
  233. _color.multiply( element.color );
  234. }
  235. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  236. _diffuseColor.copy( material.color );
  237. _emissiveColor.copy( material.emissive );
  238. if ( material.vertexColors === THREE.FaceColors ) {
  239. _diffuseColor.multiply( element.color );
  240. }
  241. _color.copy( _ambientLight );
  242. calculateLight( _lights, element.centroidModel, element.normalModel, _color );
  243. _color.multiply( _diffuseColor ).add( _emissiveColor );
  244. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  245. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  246. _color.setRGB( _w, _w, _w );
  247. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  248. var normal = element.normalModelView;
  249. _color.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  250. }
  251. if ( material.wireframe ) {
  252. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  253. } else {
  254. _svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
  255. }
  256. _svg.appendChild( _svgNode );
  257. }
  258. function getLineNode( id ) {
  259. if ( _svgLinePool[ id ] == null ) {
  260. _svgLinePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'line' );
  261. if ( _quality == 0 ) {
  262. _svgLinePool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  263. }
  264. return _svgLinePool[ id ];
  265. }
  266. return _svgLinePool[ id ];
  267. }
  268. function getPathNode( id ) {
  269. if ( _svgPathPool[ id ] == null ) {
  270. _svgPathPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  271. if ( _quality == 0 ) {
  272. _svgPathPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  273. }
  274. return _svgPathPool[ id ];
  275. }
  276. return _svgPathPool[ id ];
  277. }
  278. function getCircleNode( id ) {
  279. if ( _svgCirclePool[id] == null ) {
  280. _svgCirclePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' );
  281. if ( _quality == 0 ) {
  282. _svgCirclePool[id].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  283. }
  284. return _svgCirclePool[ id ];
  285. }
  286. return _svgCirclePool[ id ];
  287. }
  288. function pad( str ) {
  289. while ( str.length < 6 ) str = '0' + str;
  290. return str;
  291. }
  292. };