2
0

SVGRenderer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. console.log( 'THREE.SVGRenderer', THREE.REVISION );
  12. var _this = this,
  13. _renderData, _elements, _lights,
  14. _projector = new THREE.Projector(),
  15. _svg = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' ),
  16. _svgWidth, _svgHeight, _svgWidthHalf, _svgHeightHalf,
  17. _v1, _v2, _v3, _v4,
  18. _clipBox = new THREE.Box2(),
  19. _elemBox = new THREE.Box2(),
  20. _color = new THREE.Color(),
  21. _diffuseColor = new THREE.Color(),
  22. _ambientLight = new THREE.Color(),
  23. _directionalLights = new THREE.Color(),
  24. _pointLights = new THREE.Color(),
  25. _clearColor = new THREE.Color(),
  26. _clearAlpha = 1,
  27. _vector3 = new THREE.Vector3(), // Needed for PointLight
  28. _centroid = new THREE.Vector3(),
  29. _normal = new THREE.Vector3(),
  30. _normalViewMatrix = new THREE.Matrix3(),
  31. _viewMatrix = new THREE.Matrix4(),
  32. _viewProjectionMatrix = new THREE.Matrix4(),
  33. _svgPathPool = [],
  34. _svgNode, _pathCount = 0, _currPath, _currStyle,
  35. _quality = 1;
  36. this.domElement = _svg;
  37. this.autoClear = true;
  38. this.sortObjects = true;
  39. this.sortElements = true;
  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. // WebGLRenderer compatibility
  53. this.supportsVertexTextures = function () {};
  54. this.setFaceCulling = function () {};
  55. this.setClearColor = function ( color, alpha ) {
  56. _clearColor.set( color );
  57. _clearAlpha = alpha !== undefined ? alpha : 1;
  58. };
  59. this.setPixelRatio = function () {};
  60. this.setSize = function( width, height ) {
  61. _svgWidth = width; _svgHeight = height;
  62. _svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
  63. _svg.setAttribute( 'viewBox', ( - _svgWidthHalf ) + ' ' + ( - _svgHeightHalf ) + ' ' + _svgWidth + ' ' + _svgHeight );
  64. _svg.setAttribute( 'width', _svgWidth );
  65. _svg.setAttribute( 'height', _svgHeight );
  66. _clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
  67. _clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
  68. };
  69. function removeChildNodes() {
  70. _pathCount = 0;
  71. _lineCount = 0;
  72. _rectCount = 0;
  73. while ( _svg.childNodes.length > 0 ) {
  74. _svg.removeChild( _svg.childNodes[ 0 ] );
  75. }
  76. }
  77. function getSvgColor ( color, opacity ) {
  78. var arg = Math.floor( color.r * 255 ) + ',' + Math.floor( color.g * 255 ) + ',' + Math.floor( color.b * 255 );
  79. if ( opacity === undefined || opacity === 1 ) return 'rgb(' + arg + ')';
  80. return 'rgba(' + arg + ',' + opacity + ')';
  81. }
  82. this.clear = function () {
  83. removeChildNodes();
  84. _svg.style.backgroundColor = getSvgColor( _clearColor, _clearAlpha );
  85. };
  86. this.render = function ( scene, camera ) {
  87. if ( camera instanceof THREE.Camera === false ) {
  88. console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
  89. return;
  90. }
  91. var background = scene.background;
  92. if ( background && background.isColor ) {
  93. removeChildNodes();
  94. _svg.style.backgroundColor = getSvgColor( background );
  95. } else if ( this.autoClear === true ) {
  96. this.clear();
  97. }
  98. _this.info.render.vertices = 0;
  99. _this.info.render.faces = 0;
  100. _viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
  101. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
  102. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  103. _elements = _renderData.elements;
  104. _lights = _renderData.lights;
  105. _normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );
  106. calculateLights( _lights );
  107. _currPath = _currStyle = ""; // reset accumulated path
  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. _elemBox.setFromPoints( [
  134. _v1.positionScreen,
  135. _v2.positionScreen,
  136. _v3.positionScreen
  137. ] );
  138. if ( _clipBox.intersectsBox( _elemBox ) === true ) {
  139. renderFace3( _v1, _v2, _v3, element, material );
  140. }
  141. }
  142. }
  143. addPath ( "!dummy!", ""); // just to flush last svg:path
  144. scene.traverseVisible( function ( object ) {
  145. if ( object instanceof THREE.SVGObject ) {
  146. _vector3.setFromMatrixPosition( object.matrixWorld );
  147. _vector3.applyMatrix4( _viewProjectionMatrix );
  148. var x = _vector3.x * _svgWidthHalf;
  149. var y = - _vector3.y * _svgHeightHalf;
  150. var node = object.node;
  151. node.setAttribute( 'transform', 'translate(' + x + ',' + y + ')' );
  152. _svg.appendChild( node );
  153. }
  154. } );
  155. };
  156. function calculateLights( lights ) {
  157. _ambientLight.setRGB( 0, 0, 0 );
  158. _directionalLights.setRGB( 0, 0, 0 );
  159. _pointLights.setRGB( 0, 0, 0 );
  160. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  161. var light = lights[ l ];
  162. var lightColor = light.color;
  163. if ( light instanceof THREE.AmbientLight ) {
  164. _ambientLight.r += lightColor.r;
  165. _ambientLight.g += lightColor.g;
  166. _ambientLight.b += lightColor.b;
  167. } else if ( light instanceof THREE.DirectionalLight ) {
  168. _directionalLights.r += lightColor.r;
  169. _directionalLights.g += lightColor.g;
  170. _directionalLights.b += lightColor.b;
  171. } else if ( light instanceof THREE.PointLight ) {
  172. _pointLights.r += lightColor.r;
  173. _pointLights.g += lightColor.g;
  174. _pointLights.b += lightColor.b;
  175. }
  176. }
  177. }
  178. function calculateLight( lights, position, normal, color ) {
  179. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  180. var light = lights[ l ];
  181. var lightColor = light.color;
  182. if ( light instanceof THREE.DirectionalLight ) {
  183. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize();
  184. var amount = normal.dot( lightPosition );
  185. if ( amount <= 0 ) continue;
  186. amount *= light.intensity;
  187. color.r += lightColor.r * amount;
  188. color.g += lightColor.g * amount;
  189. color.b += lightColor.b * amount;
  190. } else if ( light instanceof THREE.PointLight ) {
  191. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
  192. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  193. if ( amount <= 0 ) continue;
  194. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  195. if ( amount == 0 ) continue;
  196. amount *= light.intensity;
  197. color.r += lightColor.r * amount;
  198. color.g += lightColor.g * amount;
  199. color.b += lightColor.b * amount;
  200. }
  201. }
  202. }
  203. function renderSprite( v1, element, material ) {
  204. var scaleX = element.scale.x * _svgWidthHalf;
  205. var scaleY = element.scale.y * _svgHeightHalf;
  206. if ( material.isPointsMaterial ) {
  207. scaleX *= material.size;
  208. scaleY *= material.size;
  209. }
  210. var path = 'M' + ( v1.x - scaleX * 0.5 ) + ',' + ( v1.y - scaleY * 0.5 ) + 'h' + scaleX + 'v' + scaleY + 'h' + (-scaleX) + 'z';
  211. var style = "";
  212. if ( material.isSpriteMaterial || material.isPointsMaterial ) {
  213. style = 'fill:' + getSvgColor( material.color, material.opacity );
  214. }
  215. addPath( style, path );
  216. }
  217. function renderLine( v1, v2, element, material ) {
  218. var path = 'M' + v1.positionScreen.x + ',' + v1.positionScreen.y + 'L' + v2.positionScreen.x + ',' + v2.positionScreen.y;
  219. if ( material instanceof THREE.LineBasicMaterial ) {
  220. var style = 'fill:none;stroke:' + getSvgColor( material.color, material.opacity ) + ';stroke-width:' + material.linewidth + ';stroke-linecap:' + material.linecap + ';stroke-linejoin:' + material.linejoin;
  221. addPath( style, path );
  222. }
  223. }
  224. function renderFace3( v1, v2, v3, element, material ) {
  225. _this.info.render.vertices += 3;
  226. _this.info.render.faces ++;
  227. var path = 'M' + v1.positionScreen.x + ',' + v1.positionScreen.y + 'L' + v2.positionScreen.x + ',' + v2.positionScreen.y + 'L' + v3.positionScreen.x + ',' + v3.positionScreen.y + 'z';
  228. var style = '';
  229. if ( material instanceof THREE.MeshBasicMaterial ) {
  230. _color.copy( material.color );
  231. if ( material.vertexColors === THREE.FaceColors ) {
  232. _color.multiply( element.color );
  233. }
  234. } else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
  235. _diffuseColor.copy( material.color );
  236. if ( material.vertexColors === THREE.FaceColors ) {
  237. _diffuseColor.multiply( element.color );
  238. }
  239. _color.copy( _ambientLight );
  240. _centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );
  241. calculateLight( _lights, _centroid, element.normalModel, _color );
  242. _color.multiply( _diffuseColor ).add( material.emissive );
  243. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  244. _normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
  245. _color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  246. }
  247. if ( material.wireframe ) {
  248. style = 'fill:none;stroke:' + getSvgColor( _color, material.opacity ) + ';stroke-width:' + material.wireframeLinewidth + ';stroke-linecap:' + material.wireframeLinecap + ';stroke-linejoin:' + material.wireframeLinejoin;
  249. } else {
  250. style = 'fill:' + getSvgColor( _color, material.opacity );
  251. }
  252. addPath( style, path );
  253. }
  254. function addPath ( style, path ) {
  255. if ( _currStyle == style ) {
  256. _currPath += path
  257. } else {
  258. if ( _currPath ) {
  259. _svgNode = getPathNode( _pathCount ++ );
  260. _svgNode.setAttribute( 'd', _currPath );
  261. _svgNode.setAttribute( 'style', _currStyle );
  262. _svg.appendChild( _svgNode );
  263. }
  264. _currStyle = style;
  265. _currPath = path;
  266. }
  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. };