SVGRenderer.js 12 KB

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