SVGRenderer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 = [], _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.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. this.clear = function () {
  78. removeChildNodes();
  79. _svg.style.backgroundColor = 'rgba(' + Math.floor( _clearColor.r * 255 ) + ',' + Math.floor( _clearColor.g * 255 ) + ',' + Math.floor( _clearColor.b * 255 ) + ',' + _clearAlpha + ')';
  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 = 'rgb(' + Math.floor( background.r * 255 ) + ',' + Math.floor( background.g * 255 ) + ',' + Math.floor( background.b * 255 ) + ')';
  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.getInverse( camera.matrixWorld ) );
  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. for ( var e = 0, el = _elements.length; e < el; e ++ ) {
  103. var element = _elements[ e ];
  104. var material = element.material;
  105. if ( material === undefined || material.opacity === 0 ) continue;
  106. _elemBox.makeEmpty();
  107. if ( element instanceof THREE.RenderableSprite ) {
  108. _v1 = element;
  109. _v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
  110. renderSprite( _v1, element, material );
  111. } else if ( element instanceof THREE.RenderableLine ) {
  112. _v1 = element.v1; _v2 = element.v2;
  113. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  114. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  115. _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen ] );
  116. if ( _clipBox.intersectsBox( _elemBox ) === true ) {
  117. renderLine( _v1, _v2, element, material );
  118. }
  119. } else if ( element instanceof THREE.RenderableFace ) {
  120. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  121. if ( _v1.positionScreen.z < - 1 || _v1.positionScreen.z > 1 ) continue;
  122. if ( _v2.positionScreen.z < - 1 || _v2.positionScreen.z > 1 ) continue;
  123. if ( _v3.positionScreen.z < - 1 || _v3.positionScreen.z > 1 ) continue;
  124. _v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
  125. _v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
  126. _v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;
  127. _elemBox.setFromPoints( [
  128. _v1.positionScreen,
  129. _v2.positionScreen,
  130. _v3.positionScreen
  131. ] );
  132. if ( _clipBox.intersectsBox( _elemBox ) === true ) {
  133. renderFace3( _v1, _v2, _v3, element, material );
  134. }
  135. }
  136. }
  137. scene.traverseVisible( function ( object ) {
  138. if ( object instanceof THREE.SVGObject ) {
  139. _vector3.setFromMatrixPosition( object.matrixWorld );
  140. _vector3.applyMatrix4( _viewProjectionMatrix );
  141. var x = _vector3.x * _svgWidthHalf;
  142. var y = - _vector3.y * _svgHeightHalf;
  143. var node = object.node;
  144. node.setAttribute( 'transform', 'translate(' + x + ',' + y + ')' );
  145. _svg.appendChild( node );
  146. }
  147. } );
  148. };
  149. function calculateLights( lights ) {
  150. _ambientLight.setRGB( 0, 0, 0 );
  151. _directionalLights.setRGB( 0, 0, 0 );
  152. _pointLights.setRGB( 0, 0, 0 );
  153. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  154. var light = lights[ l ];
  155. var lightColor = light.color;
  156. if ( light instanceof THREE.AmbientLight ) {
  157. _ambientLight.r += lightColor.r;
  158. _ambientLight.g += lightColor.g;
  159. _ambientLight.b += lightColor.b;
  160. } else if ( light instanceof THREE.DirectionalLight ) {
  161. _directionalLights.r += lightColor.r;
  162. _directionalLights.g += lightColor.g;
  163. _directionalLights.b += lightColor.b;
  164. } else if ( light instanceof THREE.PointLight ) {
  165. _pointLights.r += lightColor.r;
  166. _pointLights.g += lightColor.g;
  167. _pointLights.b += lightColor.b;
  168. }
  169. }
  170. }
  171. function calculateLight( lights, position, normal, color ) {
  172. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  173. var light = lights[ l ];
  174. var lightColor = light.color;
  175. if ( light instanceof THREE.DirectionalLight ) {
  176. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize();
  177. var amount = normal.dot( lightPosition );
  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. } else if ( light instanceof THREE.PointLight ) {
  184. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
  185. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  186. if ( amount <= 0 ) continue;
  187. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  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. }
  194. }
  195. }
  196. function renderSprite( v1, element, material ) {
  197. var scaleX = element.scale.x * _svgWidthHalf;
  198. var scaleY = element.scale.y * _svgHeightHalf;
  199. _svgNode = getRectNode( _rectCount ++ );
  200. _svgNode.setAttribute( 'x', v1.x - ( scaleX * 0.5 ) );
  201. _svgNode.setAttribute( 'y', v1.y - ( scaleY * 0.5 ) );
  202. _svgNode.setAttribute( 'width', scaleX );
  203. _svgNode.setAttribute( 'height', scaleY );
  204. if ( material instanceof THREE.SpriteMaterial ) {
  205. _svgNode.setAttribute( 'style', 'fill: ' + material.color.getStyle() );
  206. }
  207. _svg.appendChild( _svgNode );
  208. }
  209. function renderLine( v1, v2, element, material ) {
  210. _svgNode = getLineNode( _lineCount ++ );
  211. _svgNode.setAttribute( 'x1', v1.positionScreen.x );
  212. _svgNode.setAttribute( 'y1', v1.positionScreen.y );
  213. _svgNode.setAttribute( 'x2', v2.positionScreen.x );
  214. _svgNode.setAttribute( 'y2', v2.positionScreen.y );
  215. if ( material instanceof THREE.LineBasicMaterial ) {
  216. _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 );
  217. _svg.appendChild( _svgNode );
  218. }
  219. }
  220. function renderFace3( v1, v2, v3, element, material ) {
  221. _this.info.render.vertices += 3;
  222. _this.info.render.faces ++;
  223. _svgNode = getPathNode( _pathCount ++ );
  224. _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' );
  225. if ( material instanceof THREE.MeshBasicMaterial ) {
  226. _color.copy( material.color );
  227. if ( material.vertexColors === THREE.FaceColors ) {
  228. _color.multiply( element.color );
  229. }
  230. } else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
  231. _diffuseColor.copy( material.color );
  232. if ( material.vertexColors === THREE.FaceColors ) {
  233. _diffuseColor.multiply( element.color );
  234. }
  235. _color.copy( _ambientLight );
  236. _centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );
  237. calculateLight( _lights, _centroid, element.normalModel, _color );
  238. _color.multiply( _diffuseColor ).add( material.emissive );
  239. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  240. _normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
  241. _color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  242. }
  243. if ( material.wireframe ) {
  244. _svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
  245. } else {
  246. _svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
  247. }
  248. _svg.appendChild( _svgNode );
  249. }
  250. function getLineNode( id ) {
  251. if ( _svgLinePool[ id ] == null ) {
  252. _svgLinePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'line' );
  253. if ( _quality == 0 ) {
  254. _svgLinePool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  255. }
  256. return _svgLinePool[ id ];
  257. }
  258. return _svgLinePool[ id ];
  259. }
  260. function getPathNode( id ) {
  261. if ( _svgPathPool[ id ] == null ) {
  262. _svgPathPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  263. if ( _quality == 0 ) {
  264. _svgPathPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  265. }
  266. return _svgPathPool[ id ];
  267. }
  268. return _svgPathPool[ id ];
  269. }
  270. function getRectNode( id ) {
  271. if ( _svgRectPool[ id ] == null ) {
  272. _svgRectPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'rect' );
  273. if ( _quality == 0 ) {
  274. _svgRectPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  275. }
  276. return _svgRectPool[ id ];
  277. }
  278. return _svgRectPool[ id ];
  279. }
  280. };