SVGRenderer.js 14 KB

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