SVGRenderer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.SVGObject = function ( node ) {
  5. THREE.Object3D.call( this );
  6. this.node = node;
  7. };
  8. THREE.SVGObject.prototype = Object.create( THREE.Object3D.prototype );
  9. THREE.SVGObject.prototype.constructor = THREE.SVGObject;
  10. THREE.SVGRenderer = function () {
  11. var _this = this,
  12. _renderData, _elements, _lights,
  13. _projector = new THREE.Projector(),
  14. _svg = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' ),
  15. _svgWidth, _svgHeight, _svgWidthHalf, _svgHeightHalf,
  16. _v1, _v2, _v3,
  17. _clipBox = new THREE.Box2(),
  18. _elemBox = new THREE.Box2(),
  19. _color = new THREE.Color(),
  20. _diffuseColor = new THREE.Color(),
  21. _ambientLight = new THREE.Color(),
  22. _directionalLights = new THREE.Color(),
  23. _pointLights = new THREE.Color(),
  24. _clearColor = new THREE.Color(),
  25. _vector3 = new THREE.Vector3(), // Needed for PointLight
  26. _centroid = new THREE.Vector3(),
  27. _normal = new THREE.Vector3(),
  28. _normalViewMatrix = new THREE.Matrix3(),
  29. _viewMatrix = new THREE.Matrix4(),
  30. _viewProjectionMatrix = new THREE.Matrix4(),
  31. _svgPathPool = [],
  32. _svgNode, _pathCount = 0,
  33. _currentPath, _currentStyle,
  34. _quality = 1, _precision = null;
  35. this.domElement = _svg;
  36. this.autoClear = true;
  37. this.sortObjects = true;
  38. this.sortElements = true;
  39. this.overdraw = 0.5;
  40. this.info = {
  41. render: {
  42. vertices: 0,
  43. faces: 0
  44. }
  45. };
  46. this.setQuality = function ( quality ) {
  47. switch ( quality ) {
  48. case "high": _quality = 1; break;
  49. case "low": _quality = 0; break;
  50. }
  51. };
  52. this.setClearColor = function ( color ) {
  53. _clearColor.set( color );
  54. };
  55. this.setPixelRatio = function () {};
  56. this.setSize = function ( width, height ) {
  57. _svgWidth = width; _svgHeight = height;
  58. _svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
  59. _svg.setAttribute( 'viewBox', ( - _svgWidthHalf ) + ' ' + ( - _svgHeightHalf ) + ' ' + _svgWidth + ' ' + _svgHeight );
  60. _svg.setAttribute( 'width', _svgWidth );
  61. _svg.setAttribute( 'height', _svgHeight );
  62. _clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
  63. _clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
  64. };
  65. this.setPrecision = function ( precision ) {
  66. _precision = precision;
  67. };
  68. function removeChildNodes() {
  69. _pathCount = 0;
  70. while ( _svg.childNodes.length > 0 ) {
  71. _svg.removeChild( _svg.childNodes[ 0 ] );
  72. }
  73. }
  74. function convert( c ) {
  75. return _precision !== null ? c.toFixed( _precision ) : c;
  76. }
  77. this.clear = function () {
  78. removeChildNodes();
  79. _svg.style.backgroundColor = _clearColor.getStyle();
  80. };
  81. this.render = function ( scene, camera ) {
  82. if ( camera instanceof THREE.Camera === false ) {
  83. console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
  84. return;
  85. }
  86. var background = scene.background;
  87. if ( background && background.isColor ) {
  88. removeChildNodes();
  89. _svg.style.backgroundColor = background.getStyle();
  90. } else if ( this.autoClear === true ) {
  91. this.clear();
  92. }
  93. _this.info.render.vertices = 0;
  94. _this.info.render.faces = 0;
  95. _viewMatrix.copy( camera.matrixWorldInverse );
  96. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
  97. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  98. _elements = _renderData.elements;
  99. _lights = _renderData.lights;
  100. _normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );
  101. calculateLights( _lights );
  102. // reset accumulated path
  103. _currentPath = '';
  104. _currentStyle = '';
  105. for ( var e = 0, el = _elements.length; e < el; e ++ ) {
  106. var element = _elements[ e ];
  107. var material = element.material;
  108. if ( material === undefined || material.opacity === 0 ) continue;
  109. _elemBox.makeEmpty();
  110. if ( element instanceof THREE.RenderableSprite ) {
  111. _v1 = element;
  112. _v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
  113. renderSprite( _v1, element, material );
  114. } else if ( element instanceof THREE.RenderableLine ) {
  115. _v1 = element.v1; _v2 = element.v2;
  116. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  117. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  118. _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen ] );
  119. if ( _clipBox.intersectsBox( _elemBox ) === true ) {
  120. renderLine( _v1, _v2, element, material );
  121. }
  122. } else if ( element instanceof THREE.RenderableFace ) {
  123. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  124. if ( _v1.positionScreen.z < - 1 || _v1.positionScreen.z > 1 ) continue;
  125. if ( _v2.positionScreen.z < - 1 || _v2.positionScreen.z > 1 ) continue;
  126. if ( _v3.positionScreen.z < - 1 || _v3.positionScreen.z > 1 ) continue;
  127. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  128. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  129. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;
  130. if ( this.overdraw > 0 ) {
  131. expand( _v1.positionScreen, _v2.positionScreen, this.overdraw );
  132. expand( _v2.positionScreen, _v3.positionScreen, this.overdraw );
  133. expand( _v3.positionScreen, _v1.positionScreen, this.overdraw );
  134. }
  135. _elemBox.setFromPoints( [
  136. _v1.positionScreen,
  137. _v2.positionScreen,
  138. _v3.positionScreen
  139. ] );
  140. if ( _clipBox.intersectsBox( _elemBox ) === true ) {
  141. renderFace3( _v1, _v2, _v3, element, material );
  142. }
  143. }
  144. }
  145. flushPath(); // just to flush last svg:path
  146. scene.traverseVisible( function ( object ) {
  147. if ( object instanceof THREE.SVGObject ) {
  148. _vector3.setFromMatrixPosition( object.matrixWorld );
  149. _vector3.applyMatrix4( _viewProjectionMatrix );
  150. if ( _vector3.z < - 1 || _vector3.z > 1 ) return;
  151. var x = _vector3.x * _svgWidthHalf;
  152. var y = - _vector3.y * _svgHeightHalf;
  153. var node = object.node;
  154. node.setAttribute( 'transform', 'translate(' + x + ',' + y + ')' );
  155. _svg.appendChild( node );
  156. }
  157. } );
  158. };
  159. function calculateLights( lights ) {
  160. _ambientLight.setRGB( 0, 0, 0 );
  161. _directionalLights.setRGB( 0, 0, 0 );
  162. _pointLights.setRGB( 0, 0, 0 );
  163. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  164. var light = lights[ l ];
  165. var lightColor = light.color;
  166. if ( light.isAmbientLight ) {
  167. _ambientLight.r += lightColor.r;
  168. _ambientLight.g += lightColor.g;
  169. _ambientLight.b += lightColor.b;
  170. } else if ( light.isDirectionalLight ) {
  171. _directionalLights.r += lightColor.r;
  172. _directionalLights.g += lightColor.g;
  173. _directionalLights.b += lightColor.b;
  174. } else if ( light.isPointLight ) {
  175. _pointLights.r += lightColor.r;
  176. _pointLights.g += lightColor.g;
  177. _pointLights.b += lightColor.b;
  178. }
  179. }
  180. }
  181. function calculateLight( lights, position, normal, color ) {
  182. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  183. var light = lights[ l ];
  184. var lightColor = light.color;
  185. if ( light.isDirectionalLight ) {
  186. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize();
  187. var amount = normal.dot( lightPosition );
  188. if ( amount <= 0 ) continue;
  189. amount *= light.intensity;
  190. color.r += lightColor.r * amount;
  191. color.g += lightColor.g * amount;
  192. color.b += lightColor.b * amount;
  193. } else if ( light.isPointLight ) {
  194. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
  195. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  196. if ( amount <= 0 ) continue;
  197. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  198. if ( amount == 0 ) continue;
  199. amount *= light.intensity;
  200. color.r += lightColor.r * amount;
  201. color.g += lightColor.g * amount;
  202. color.b += lightColor.b * amount;
  203. }
  204. }
  205. }
  206. function renderSprite( v1, element, material ) {
  207. var scaleX = element.scale.x * _svgWidthHalf;
  208. var scaleY = element.scale.y * _svgHeightHalf;
  209. if ( material.isPointsMaterial ) {
  210. scaleX *= material.size;
  211. scaleY *= material.size;
  212. }
  213. var path = 'M' + convert( v1.x - scaleX * 0.5 ) + ',' + convert( v1.y - scaleY * 0.5 ) + 'h' + convert( scaleX ) + 'v' + convert( scaleY ) + 'h' + convert( - scaleX ) + 'z';
  214. var style = "";
  215. if ( material.isSpriteMaterial || material.isPointsMaterial ) {
  216. style = 'fill:' + material.color.getStyle() + ';fill-opacity:' + material.opacity;
  217. }
  218. addPath( style, path );
  219. }
  220. function renderLine( v1, v2, element, material ) {
  221. var path = 'M' + convert( v1.positionScreen.x ) + ',' + convert( v1.positionScreen.y ) + 'L' + convert( v2.positionScreen.x ) + ',' + convert( v2.positionScreen.y );
  222. if ( material.isLineBasicMaterial ) {
  223. var style = 'fill:none;stroke:' + material.color.getStyle() + ';stroke-opacity:' + material.opacity + ';stroke-width:' + material.linewidth + ';stroke-linecap:' + material.linecap;
  224. if ( material.isLineDashedMaterial ) {
  225. style = style + ';stroke-dasharray:' + material.dashSize + "," + material.gapSize;
  226. }
  227. addPath( style, path );
  228. }
  229. }
  230. function renderFace3( v1, v2, v3, element, material ) {
  231. _this.info.render.vertices += 3;
  232. _this.info.render.faces ++;
  233. var path = 'M' + convert( v1.positionScreen.x ) + ',' + convert( v1.positionScreen.y ) + 'L' + convert( v2.positionScreen.x ) + ',' + convert( v2.positionScreen.y ) + 'L' + convert( v3.positionScreen.x ) + ',' + convert( v3.positionScreen.y ) + 'z';
  234. var style = '';
  235. if ( material.isMeshBasicMaterial ) {
  236. _color.copy( material.color );
  237. if ( material.vertexColors ) {
  238. _color.multiply( element.color );
  239. }
  240. } else if ( material.isMeshLambertMaterial || material.isMeshPhongMaterial || material.isMeshStandardMaterial ) {
  241. _diffuseColor.copy( material.color );
  242. if ( material.vertexColors ) {
  243. _diffuseColor.multiply( element.color );
  244. }
  245. _color.copy( _ambientLight );
  246. _centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );
  247. calculateLight( _lights, _centroid, element.normalModel, _color );
  248. _color.multiply( _diffuseColor ).add( material.emissive );
  249. } else if ( material.isMeshNormalMaterial ) {
  250. _normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix ).normalize();
  251. _color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  252. }
  253. if ( material.wireframe ) {
  254. style = 'fill:none;stroke:' + _color.getStyle() + ';stroke-opacity:' + material.opacity + ';stroke-width:' + material.wireframeLinewidth + ';stroke-linecap:' + material.wireframeLinecap + ';stroke-linejoin:' + material.wireframeLinejoin;
  255. } else {
  256. style = 'fill:' + _color.getStyle() + ';fill-opacity:' + material.opacity;
  257. }
  258. addPath( style, path );
  259. }
  260. // Hide anti-alias gaps
  261. function expand( v1, v2, pixels ) {
  262. var x = v2.x - v1.x, y = v2.y - v1.y,
  263. det = x * x + y * y, idet;
  264. if ( det === 0 ) return;
  265. idet = pixels / Math.sqrt( det );
  266. x *= idet; y *= idet;
  267. v2.x += x; v2.y += y;
  268. v1.x -= x; v1.y -= y;
  269. }
  270. function addPath( style, path ) {
  271. if ( _currentStyle === style ) {
  272. _currentPath += path;
  273. } else {
  274. flushPath();
  275. _currentStyle = style;
  276. _currentPath = path;
  277. }
  278. }
  279. function flushPath() {
  280. if ( _currentPath ) {
  281. _svgNode = getPathNode( _pathCount ++ );
  282. _svgNode.setAttribute( 'd', _currentPath );
  283. _svgNode.setAttribute( 'style', _currentStyle );
  284. _svg.appendChild( _svgNode );
  285. }
  286. _currentPath = '';
  287. _currentStyle = '';
  288. }
  289. function getPathNode( id ) {
  290. if ( _svgPathPool[ id ] == null ) {
  291. _svgPathPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  292. if ( _quality == 0 ) {
  293. _svgPathPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  294. }
  295. return _svgPathPool[ id ];
  296. }
  297. return _svgPathPool[ id ];
  298. }
  299. };