SVGRenderer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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.SVGRenderer = function () {
  10. console.log( 'THREE.SVGRenderer', THREE.REVISION );
  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, _v4,
  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. _clearAlpha = 1,
  26. _w, // z-buffer to w-buffer
  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 = [], _svgLinePool = [], _svgRectPool = [],
  34. _svgNode, _pathCount = 0, _lineCount = 0, _rectCount = 0,
  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.setSize = function( width, height ) {
  60. _svgWidth = width; _svgHeight = height;
  61. _svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
  62. _svg.setAttribute( 'viewBox', ( - _svgWidthHalf ) + ' ' + ( - _svgHeightHalf ) + ' ' + _svgWidth + ' ' + _svgHeight );
  63. _svg.setAttribute( 'width', _svgWidth );
  64. _svg.setAttribute( 'height', _svgHeight );
  65. _clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
  66. _clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
  67. };
  68. this.clear = function () {
  69. _pathCount = 0;
  70. _lineCount = 0;
  71. _rectCount = 0;
  72. while ( _svg.childNodes.length > 0 ) {
  73. _svg.removeChild( _svg.childNodes[ 0 ] );
  74. }
  75. _svg.style.backgroundColor = 'rgba(' + ( ( _clearColor.r * 255 ) | 0 ) + ',' + ( ( _clearColor.g * 255 ) | 0 ) + ',' + ( ( _clearColor.b * 255 ) | 0 ) + ',' + _clearAlpha + ')';
  76. };
  77. this.render = function ( scene, camera ) {
  78. if ( camera instanceof THREE.Camera === false ) {
  79. console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
  80. return;
  81. }
  82. if ( this.autoClear === true ) this.clear();
  83. _this.info.render.vertices = 0;
  84. _this.info.render.faces = 0;
  85. _viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
  86. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
  87. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  88. _elements = _renderData.elements;
  89. _lights = _renderData.lights;
  90. _normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );
  91. calculateLights( _lights );
  92. for ( var e = 0, el = _elements.length; e < el; e ++ ) {
  93. var element = _elements[ e ];
  94. var material = element.material;
  95. if ( material === undefined || material.opacity === 0 ) continue;
  96. _elemBox.makeEmpty();
  97. if ( element instanceof THREE.RenderableSprite ) {
  98. _v1 = element;
  99. _v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
  100. renderSprite( _v1, element, material );
  101. } else if ( element instanceof THREE.RenderableLine ) {
  102. _v1 = element.v1; _v2 = element.v2;
  103. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  104. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  105. _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen ] );
  106. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  107. renderLine( _v1, _v2, element, material );
  108. }
  109. } else if ( element instanceof THREE.RenderableFace ) {
  110. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  111. if ( _v1.positionScreen.z < -1 || _v1.positionScreen.z > 1 ) continue;
  112. if ( _v2.positionScreen.z < -1 || _v2.positionScreen.z > 1 ) continue;
  113. if ( _v3.positionScreen.z < -1 || _v3.positionScreen.z > 1 ) continue;
  114. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  115. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  116. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;
  117. _elemBox.setFromPoints( [
  118. _v1.positionScreen,
  119. _v2.positionScreen,
  120. _v3.positionScreen
  121. ] );
  122. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  123. renderFace3( _v1, _v2, _v3, element, material );
  124. }
  125. }
  126. }
  127. scene.traverseVisible( function ( object ) {
  128. if ( object instanceof THREE.SVGObject ) {
  129. _vector3.setFromMatrixPosition( object.matrixWorld );
  130. _vector3.applyProjection( _viewProjectionMatrix );
  131. var x = _vector3.x * _svgWidthHalf;
  132. var y = - _vector3.y * _svgHeightHalf;
  133. var node = object.node;
  134. node.setAttribute( 'transform', 'translate(' + x + ',' + y + ')' );
  135. _svg.appendChild( node );
  136. }
  137. } );
  138. };
  139. function calculateLights( lights ) {
  140. _ambientLight.setRGB( 0, 0, 0 );
  141. _directionalLights.setRGB( 0, 0, 0 );
  142. _pointLights.setRGB( 0, 0, 0 );
  143. for ( var l = 0, ll = lights.length; l < ll; l++ ) {
  144. var light = lights[ l ];
  145. var lightColor = light.color;
  146. if ( light instanceof THREE.AmbientLight ) {
  147. _ambientLight.r += lightColor.r;
  148. _ambientLight.g += lightColor.g;
  149. _ambientLight.b += lightColor.b;
  150. } else if ( light instanceof THREE.DirectionalLight ) {
  151. _directionalLights.r += lightColor.r;
  152. _directionalLights.g += lightColor.g;
  153. _directionalLights.b += lightColor.b;
  154. } else if ( light instanceof THREE.PointLight ) {
  155. _pointLights.r += lightColor.r;
  156. _pointLights.g += lightColor.g;
  157. _pointLights.b += lightColor.b;
  158. }
  159. }
  160. }
  161. function calculateLight( lights, position, normal, color ) {
  162. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  163. var light = lights[ l ];
  164. var lightColor = light.color;
  165. if ( light instanceof THREE.DirectionalLight ) {
  166. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize();
  167. var amount = normal.dot( lightPosition );
  168. if ( amount <= 0 ) continue;
  169. amount *= light.intensity;
  170. color.r += lightColor.r * amount;
  171. color.g += lightColor.g * amount;
  172. color.b += lightColor.b * amount;
  173. } else if ( light instanceof THREE.PointLight ) {
  174. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
  175. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  176. if ( amount <= 0 ) continue;
  177. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  178. if ( amount == 0 ) continue;
  179. amount *= light.intensity;
  180. color.r += lightColor.r * amount;
  181. color.g += lightColor.g * amount;
  182. color.b += lightColor.b * amount;
  183. }
  184. }
  185. }
  186. function renderSprite( v1, element, material ) {
  187. var scaleX = element.scale.x * _svgWidthHalf;
  188. var scaleY = element.scale.y * _svgHeightHalf;
  189. _svgNode = getRectNode( _rectCount ++ );
  190. _svgNode.setAttribute( 'x', v1.x - ( scaleX * 0.5 ) );
  191. _svgNode.setAttribute( 'y', v1.y - ( scaleY * 0.5 ) );
  192. _svgNode.setAttribute( 'width', scaleX );
  193. _svgNode.setAttribute( 'height', scaleY );
  194. if ( material instanceof THREE.SpriteMaterial ) {
  195. _svgNode.setAttribute( 'style', 'fill: ' + material.color.getStyle() );
  196. }
  197. _svg.appendChild( _svgNode );
  198. }
  199. function renderLine( v1, v2, element, material ) {
  200. _svgNode = getLineNode( _lineCount ++ );
  201. _svgNode.setAttribute( 'x1', v1.positionScreen.x );
  202. _svgNode.setAttribute( 'y1', v1.positionScreen.y );
  203. _svgNode.setAttribute( 'x2', v2.positionScreen.x );
  204. _svgNode.setAttribute( 'y2', v2.positionScreen.y );
  205. if ( material instanceof THREE.LineBasicMaterial ) {
  206. _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 );
  207. _svg.appendChild( _svgNode );
  208. }
  209. }
  210. function renderFace3( v1, v2, v3, element, material ) {
  211. _this.info.render.vertices += 3;
  212. _this.info.render.faces ++;
  213. _svgNode = getPathNode( _pathCount ++ );
  214. _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' );
  215. if ( material instanceof THREE.MeshBasicMaterial ) {
  216. _color.copy( material.color );
  217. if ( material.vertexColors === THREE.FaceColors ) {
  218. _color.multiply( element.color );
  219. }
  220. } else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
  221. _diffuseColor.copy( material.color );
  222. if ( material.vertexColors === THREE.FaceColors ) {
  223. _diffuseColor.multiply( element.color );
  224. }
  225. _color.copy( _ambientLight );
  226. _centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );
  227. calculateLight( _lights, _centroid, element.normalModel, _color );
  228. _color.multiply( _diffuseColor ).add( material.emissive );
  229. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  230. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
  231. _color.setRGB( _w, _w, _w );
  232. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  233. _normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
  234. _color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  235. }
  236. if ( material.wireframe ) {
  237. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  238. } else {
  239. _svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
  240. }
  241. _svg.appendChild( _svgNode );
  242. }
  243. function getLineNode( id ) {
  244. if ( _svgLinePool[ id ] == null ) {
  245. _svgLinePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'line' );
  246. if ( _quality == 0 ) {
  247. _svgLinePool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  248. }
  249. return _svgLinePool[ id ];
  250. }
  251. return _svgLinePool[ id ];
  252. }
  253. function getPathNode( id ) {
  254. if ( _svgPathPool[ id ] == null ) {
  255. _svgPathPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  256. if ( _quality == 0 ) {
  257. _svgPathPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  258. }
  259. return _svgPathPool[ id ];
  260. }
  261. return _svgPathPool[ id ];
  262. }
  263. function getRectNode( id ) {
  264. if ( _svgRectPool[ id ] == null ) {
  265. _svgRectPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'rect' );
  266. if ( _quality == 0 ) {
  267. _svgRectPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  268. }
  269. return _svgRectPool[ id ];
  270. }
  271. return _svgRectPool[ id ];
  272. }
  273. };