CanvasRenderer.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.SpriteCanvasMaterial = function ( parameters ) {
  5. THREE.Material.call( this );
  6. this.type = 'SpriteCanvasMaterial';
  7. this.rotation = 0;
  8. this.color = new THREE.Color( 0xffffff );
  9. this.program = function () {};
  10. this.setValues( parameters );
  11. };
  12. THREE.SpriteCanvasMaterial.prototype = Object.create( THREE.Material.prototype );
  13. THREE.SpriteCanvasMaterial.prototype.constructor = THREE.SpriteCanvasMaterial;
  14. THREE.SpriteCanvasMaterial.prototype.isSpriteCanvasMaterial = true;
  15. THREE.SpriteCanvasMaterial.prototype.clone = function () {
  16. var material = new THREE.SpriteCanvasMaterial();
  17. material.copy( this );
  18. material.color.copy( this.color );
  19. material.program = this.program;
  20. return material;
  21. };
  22. //
  23. THREE.CanvasRenderer = function ( parameters ) {
  24. console.log( 'THREE.CanvasRenderer', THREE.REVISION );
  25. parameters = parameters || {};
  26. var _this = this,
  27. _renderData, _elements, _lights,
  28. _projector = new THREE.Projector(),
  29. _canvas = parameters.canvas !== undefined
  30. ? parameters.canvas
  31. : document.createElement( 'canvas' ),
  32. _canvasWidth = _canvas.width,
  33. _canvasHeight = _canvas.height,
  34. _canvasWidthHalf = Math.floor( _canvasWidth / 2 ),
  35. _canvasHeightHalf = Math.floor( _canvasHeight / 2 ),
  36. _viewportX = 0,
  37. _viewportY = 0,
  38. _viewportWidth = _canvasWidth,
  39. _viewportHeight = _canvasHeight,
  40. _pixelRatio = 1,
  41. _context = _canvas.getContext( '2d', {
  42. alpha: parameters.alpha === true
  43. } ),
  44. _clearColor = new THREE.Color( 0x000000 ),
  45. _clearAlpha = parameters.alpha === true ? 0 : 1,
  46. _contextGlobalAlpha = 1,
  47. _contextGlobalCompositeOperation = 0,
  48. _contextStrokeStyle = null,
  49. _contextFillStyle = null,
  50. _contextLineWidth = null,
  51. _contextLineCap = null,
  52. _contextLineJoin = null,
  53. _contextLineDash = [],
  54. _v1, _v2, _v3,
  55. _v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
  56. _color = new THREE.Color(),
  57. _diffuseColor = new THREE.Color(),
  58. _emissiveColor = new THREE.Color(),
  59. _lightColor = new THREE.Color(),
  60. _patterns = {},
  61. _uvs,
  62. _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y,
  63. _clipBox = new THREE.Box2(),
  64. _clearBox = new THREE.Box2(),
  65. _elemBox = new THREE.Box2(),
  66. _ambientLight = new THREE.Color(),
  67. _directionalLights = new THREE.Color(),
  68. _pointLights = new THREE.Color(),
  69. _vector3 = new THREE.Vector3(), // Needed for PointLight
  70. _centroid = new THREE.Vector3(),
  71. _normal = new THREE.Vector3(),
  72. _normalViewMatrix = new THREE.Matrix3();
  73. /* TODO
  74. _canvas.mozImageSmoothingEnabled = false;
  75. _canvas.webkitImageSmoothingEnabled = false;
  76. _canvas.msImageSmoothingEnabled = false;
  77. _canvas.imageSmoothingEnabled = false;
  78. */
  79. // dash+gap fallbacks for Firefox and everything else
  80. if ( _context.setLineDash === undefined ) {
  81. _context.setLineDash = function () {};
  82. }
  83. this.domElement = _canvas;
  84. this.autoClear = true;
  85. this.sortObjects = true;
  86. this.sortElements = true;
  87. this.info = {
  88. render: {
  89. vertices: 0,
  90. faces: 0
  91. }
  92. };
  93. // API
  94. this.getContext = function () {
  95. return _context;
  96. };
  97. this.getContextAttributes = function () {
  98. return _context.getContextAttributes();
  99. };
  100. this.getPixelRatio = function () {
  101. return _pixelRatio;
  102. };
  103. this.setPixelRatio = function ( value ) {
  104. if ( value !== undefined ) _pixelRatio = value;
  105. };
  106. this.setSize = function ( width, height, updateStyle ) {
  107. _canvasWidth = width * _pixelRatio;
  108. _canvasHeight = height * _pixelRatio;
  109. _canvas.width = _canvasWidth;
  110. _canvas.height = _canvasHeight;
  111. _canvasWidthHalf = Math.floor( _canvasWidth / 2 );
  112. _canvasHeightHalf = Math.floor( _canvasHeight / 2 );
  113. if ( updateStyle !== false ) {
  114. _canvas.style.width = width + 'px';
  115. _canvas.style.height = height + 'px';
  116. }
  117. _clipBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf );
  118. _clipBox.max.set( _canvasWidthHalf, _canvasHeightHalf );
  119. _clearBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf );
  120. _clearBox.max.set( _canvasWidthHalf, _canvasHeightHalf );
  121. _contextGlobalAlpha = 1;
  122. _contextGlobalCompositeOperation = 0;
  123. _contextStrokeStyle = null;
  124. _contextFillStyle = null;
  125. _contextLineWidth = null;
  126. _contextLineCap = null;
  127. _contextLineJoin = null;
  128. this.setViewport( 0, 0, width, height );
  129. };
  130. this.setViewport = function ( x, y, width, height ) {
  131. _viewportX = x * _pixelRatio;
  132. _viewportY = y * _pixelRatio;
  133. _viewportWidth = width * _pixelRatio;
  134. _viewportHeight = height * _pixelRatio;
  135. };
  136. this.setScissor = function () {};
  137. this.setScissorTest = function () {};
  138. this.setClearColor = function ( color, alpha ) {
  139. _clearColor.set( color );
  140. _clearAlpha = alpha !== undefined ? alpha : 1;
  141. _clearBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf );
  142. _clearBox.max.set( _canvasWidthHalf, _canvasHeightHalf );
  143. };
  144. this.setClearColorHex = function ( hex, alpha ) {
  145. console.warn( 'THREE.CanvasRenderer: .setClearColorHex() is being removed. Use .setClearColor() instead.' );
  146. this.setClearColor( hex, alpha );
  147. };
  148. this.getClearColor = function () {
  149. return _clearColor;
  150. };
  151. this.getClearAlpha = function () {
  152. return _clearAlpha;
  153. };
  154. this.getMaxAnisotropy = function () {
  155. return 0;
  156. };
  157. this.clear = function () {
  158. if ( _clearBox.isEmpty() === false ) {
  159. _clearBox.intersect( _clipBox );
  160. _clearBox.expandByScalar( 2 );
  161. _clearBox.min.x = _clearBox.min.x + _canvasWidthHalf;
  162. _clearBox.min.y = - _clearBox.min.y + _canvasHeightHalf; // higher y value !
  163. _clearBox.max.x = _clearBox.max.x + _canvasWidthHalf;
  164. _clearBox.max.y = - _clearBox.max.y + _canvasHeightHalf; // lower y value !
  165. if ( _clearAlpha < 1 ) {
  166. _context.clearRect(
  167. _clearBox.min.x | 0,
  168. _clearBox.max.y | 0,
  169. ( _clearBox.max.x - _clearBox.min.x ) | 0,
  170. ( _clearBox.min.y - _clearBox.max.y ) | 0
  171. );
  172. }
  173. if ( _clearAlpha > 0 ) {
  174. setOpacity( 1 );
  175. setBlending( THREE.NormalBlending );
  176. setFillStyle( 'rgba(' + Math.floor( _clearColor.r * 255 ) + ',' + Math.floor( _clearColor.g * 255 ) + ',' + Math.floor( _clearColor.b * 255 ) + ',' + _clearAlpha + ')' );
  177. _context.fillRect(
  178. _clearBox.min.x | 0,
  179. _clearBox.max.y | 0,
  180. ( _clearBox.max.x - _clearBox.min.x ) | 0,
  181. ( _clearBox.min.y - _clearBox.max.y ) | 0
  182. );
  183. }
  184. _clearBox.makeEmpty();
  185. }
  186. };
  187. // compatibility
  188. this.clearColor = function () {};
  189. this.clearDepth = function () {};
  190. this.clearStencil = function () {};
  191. this.render = function ( scene, camera ) {
  192. if ( camera.isCamera === undefined ) {
  193. console.error( 'THREE.CanvasRenderer.render: camera is not an instance of THREE.Camera.' );
  194. return;
  195. }
  196. var background = scene.background;
  197. if ( background && background.isColor ) {
  198. setOpacity( 1 );
  199. setBlending( THREE.NormalBlending );
  200. setFillStyle( background.getStyle() );
  201. _context.fillRect( 0, 0, _canvasWidth, _canvasHeight );
  202. } else if ( this.autoClear === true ) {
  203. this.clear();
  204. }
  205. _this.info.render.vertices = 0;
  206. _this.info.render.faces = 0;
  207. _context.setTransform( _viewportWidth / _canvasWidth, 0, 0, - _viewportHeight / _canvasHeight, _viewportX, _canvasHeight - _viewportY );
  208. _context.translate( _canvasWidthHalf, _canvasHeightHalf );
  209. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  210. _elements = _renderData.elements;
  211. _lights = _renderData.lights;
  212. _normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );
  213. /* DEBUG
  214. setFillStyle( 'rgba( 0, 255, 255, 0.5 )' );
  215. _context.fillRect( _clipBox.min.x, _clipBox.min.y, _clipBox.max.x - _clipBox.min.x, _clipBox.max.y - _clipBox.min.y );
  216. */
  217. calculateLights();
  218. for ( var e = 0, el = _elements.length; e < el; e ++ ) {
  219. var element = _elements[ e ];
  220. var material = element.material;
  221. if ( material === undefined || material.opacity === 0 ) continue;
  222. _elemBox.makeEmpty();
  223. if ( element instanceof THREE.RenderableSprite ) {
  224. _v1 = element;
  225. _v1.x *= _canvasWidthHalf; _v1.y *= _canvasHeightHalf;
  226. renderSprite( _v1, element, material );
  227. } else if ( element instanceof THREE.RenderableLine ) {
  228. _v1 = element.v1; _v2 = element.v2;
  229. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  230. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  231. _elemBox.setFromPoints( [
  232. _v1.positionScreen,
  233. _v2.positionScreen
  234. ] );
  235. if ( _clipBox.intersectsBox( _elemBox ) === true ) {
  236. renderLine( _v1, _v2, element, material );
  237. }
  238. } else if ( element instanceof THREE.RenderableFace ) {
  239. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  240. if ( _v1.positionScreen.z < - 1 || _v1.positionScreen.z > 1 ) continue;
  241. if ( _v2.positionScreen.z < - 1 || _v2.positionScreen.z > 1 ) continue;
  242. if ( _v3.positionScreen.z < - 1 || _v3.positionScreen.z > 1 ) continue;
  243. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  244. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  245. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  246. if ( material.overdraw > 0 ) {
  247. expand( _v1.positionScreen, _v2.positionScreen, material.overdraw );
  248. expand( _v2.positionScreen, _v3.positionScreen, material.overdraw );
  249. expand( _v3.positionScreen, _v1.positionScreen, material.overdraw );
  250. }
  251. _elemBox.setFromPoints( [
  252. _v1.positionScreen,
  253. _v2.positionScreen,
  254. _v3.positionScreen
  255. ] );
  256. if ( _clipBox.intersectsBox( _elemBox ) === true ) {
  257. renderFace3( _v1, _v2, _v3, 0, 1, 2, element, material );
  258. }
  259. }
  260. /* DEBUG
  261. setLineWidth( 1 );
  262. setStrokeStyle( 'rgba( 0, 255, 0, 0.5 )' );
  263. _context.strokeRect( _elemBox.min.x, _elemBox.min.y, _elemBox.max.x - _elemBox.min.x, _elemBox.max.y - _elemBox.min.y );
  264. */
  265. _clearBox.union( _elemBox );
  266. }
  267. /* DEBUG
  268. setLineWidth( 1 );
  269. setStrokeStyle( 'rgba( 255, 0, 0, 0.5 )' );
  270. _context.strokeRect( _clearBox.min.x, _clearBox.min.y, _clearBox.max.x - _clearBox.min.x, _clearBox.max.y - _clearBox.min.y );
  271. */
  272. _context.setTransform( 1, 0, 0, 1, 0, 0 );
  273. };
  274. //
  275. function calculateLights() {
  276. _ambientLight.setRGB( 0, 0, 0 );
  277. _directionalLights.setRGB( 0, 0, 0 );
  278. _pointLights.setRGB( 0, 0, 0 );
  279. for ( var l = 0, ll = _lights.length; l < ll; l ++ ) {
  280. var light = _lights[ l ];
  281. var lightColor = light.color;
  282. if ( light.isAmbientLight ) {
  283. _ambientLight.add( lightColor );
  284. } else if ( light.isDirectionalLight ) {
  285. // for sprites
  286. _directionalLights.add( lightColor );
  287. } else if ( light.isPointLight ) {
  288. // for sprites
  289. _pointLights.add( lightColor );
  290. }
  291. }
  292. }
  293. function calculateLight( position, normal, color ) {
  294. for ( var l = 0, ll = _lights.length; l < ll; l ++ ) {
  295. var light = _lights[ l ];
  296. _lightColor.copy( light.color );
  297. if ( light.isDirectionalLight ) {
  298. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize();
  299. var amount = normal.dot( lightPosition );
  300. if ( amount <= 0 ) continue;
  301. amount *= light.intensity;
  302. color.add( _lightColor.multiplyScalar( amount ) );
  303. } else if ( light.isPointLight ) {
  304. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
  305. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  306. if ( amount <= 0 ) continue;
  307. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  308. if ( amount == 0 ) continue;
  309. amount *= light.intensity;
  310. color.add( _lightColor.multiplyScalar( amount ) );
  311. }
  312. }
  313. }
  314. function renderSprite( v1, element, material ) {
  315. setOpacity( material.opacity );
  316. setBlending( material.blending );
  317. var scaleX = element.scale.x * _canvasWidthHalf;
  318. var scaleY = element.scale.y * _canvasHeightHalf;
  319. var dist = Math.sqrt( scaleX * scaleX + scaleY * scaleY ); // allow for rotated sprite
  320. _elemBox.min.set( v1.x - dist, v1.y - dist );
  321. _elemBox.max.set( v1.x + dist, v1.y + dist );
  322. if ( material.isSpriteMaterial ) {
  323. var texture = material.map;
  324. if ( texture !== null ) {
  325. var pattern = _patterns[ texture.id ];
  326. if ( pattern === undefined || pattern.version !== texture.version ) {
  327. pattern = textureToPattern( texture );
  328. _patterns[ texture.id ] = pattern;
  329. }
  330. if ( pattern.canvas !== undefined ) {
  331. setFillStyle( pattern.canvas );
  332. var bitmap = texture.image;
  333. var ox = bitmap.width * texture.offset.x;
  334. var oy = bitmap.height * texture.offset.y;
  335. var sx = bitmap.width * texture.repeat.x;
  336. var sy = bitmap.height * texture.repeat.y;
  337. var cx = scaleX / sx;
  338. var cy = scaleY / sy;
  339. _context.save();
  340. _context.translate( v1.x, v1.y );
  341. if ( material.rotation !== 0 ) _context.rotate( material.rotation );
  342. _context.translate( - scaleX / 2, - scaleY / 2 );
  343. _context.scale( cx, cy );
  344. _context.translate( - ox, - oy );
  345. _context.fillRect( ox, oy, sx, sy );
  346. _context.restore();
  347. }
  348. } else {
  349. // no texture
  350. setFillStyle( material.color.getStyle() );
  351. _context.save();
  352. _context.translate( v1.x, v1.y );
  353. if ( material.rotation !== 0 ) _context.rotate( material.rotation );
  354. _context.scale( scaleX, - scaleY );
  355. _context.fillRect( - 0.5, - 0.5, 1, 1 );
  356. _context.restore();
  357. }
  358. } else if ( material.isSpriteCanvasMaterial ) {
  359. setStrokeStyle( material.color.getStyle() );
  360. setFillStyle( material.color.getStyle() );
  361. _context.save();
  362. _context.translate( v1.x, v1.y );
  363. if ( material.rotation !== 0 ) _context.rotate( material.rotation );
  364. _context.scale( scaleX, scaleY );
  365. material.program( _context );
  366. _context.restore();
  367. } else if ( material.isPointsMaterial ) {
  368. setFillStyle( material.color.getStyle() );
  369. _context.save();
  370. _context.translate( v1.x, v1.y );
  371. if ( material.rotation !== 0 ) _context.rotate( material.rotation );
  372. _context.scale( scaleX * material.size, - scaleY * material.size );
  373. _context.fillRect( - 0.5, - 0.5, 1, 1 );
  374. _context.restore();
  375. }
  376. /* DEBUG
  377. setStrokeStyle( 'rgb(255,255,0)' );
  378. _context.beginPath();
  379. _context.moveTo( v1.x - 10, v1.y );
  380. _context.lineTo( v1.x + 10, v1.y );
  381. _context.moveTo( v1.x, v1.y - 10 );
  382. _context.lineTo( v1.x, v1.y + 10 );
  383. _context.stroke();
  384. */
  385. }
  386. function renderLine( v1, v2, element, material ) {
  387. setOpacity( material.opacity );
  388. setBlending( material.blending );
  389. _context.beginPath();
  390. _context.moveTo( v1.positionScreen.x, v1.positionScreen.y );
  391. _context.lineTo( v2.positionScreen.x, v2.positionScreen.y );
  392. if ( material.isLineBasicMaterial ) {
  393. setLineWidth( material.linewidth );
  394. setLineCap( material.linecap );
  395. setLineJoin( material.linejoin );
  396. if ( material.vertexColors !== THREE.VertexColors ) {
  397. setStrokeStyle( material.color.getStyle() );
  398. } else {
  399. var colorStyle1 = element.vertexColors[ 0 ].getStyle();
  400. var colorStyle2 = element.vertexColors[ 1 ].getStyle();
  401. if ( colorStyle1 === colorStyle2 ) {
  402. setStrokeStyle( colorStyle1 );
  403. } else {
  404. try {
  405. var grad = _context.createLinearGradient(
  406. v1.positionScreen.x,
  407. v1.positionScreen.y,
  408. v2.positionScreen.x,
  409. v2.positionScreen.y
  410. );
  411. grad.addColorStop( 0, colorStyle1 );
  412. grad.addColorStop( 1, colorStyle2 );
  413. } catch ( exception ) {
  414. grad = colorStyle1;
  415. }
  416. setStrokeStyle( grad );
  417. }
  418. }
  419. if ( material.isLineDashedMaterial ) {
  420. setLineDash( [ material.dashSize, material.gapSize ] );
  421. }
  422. _context.stroke();
  423. _elemBox.expandByScalar( material.linewidth * 2 );
  424. if ( material.isLineDashedMaterial ) {
  425. setLineDash( [] );
  426. }
  427. }
  428. }
  429. function renderFace3( v1, v2, v3, uv1, uv2, uv3, element, material ) {
  430. _this.info.render.vertices += 3;
  431. _this.info.render.faces ++;
  432. setOpacity( material.opacity );
  433. setBlending( material.blending );
  434. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  435. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  436. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  437. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y );
  438. if ( ( material.isMeshLambertMaterial || material.isMeshPhongMaterial || material.isMeshStandardMaterial ) && material.map === null ) {
  439. _diffuseColor.copy( material.color );
  440. _emissiveColor.copy( material.emissive );
  441. if ( material.vertexColors === THREE.FaceColors ) {
  442. _diffuseColor.multiply( element.color );
  443. }
  444. _color.copy( _ambientLight );
  445. _centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );
  446. calculateLight( _centroid, element.normalModel, _color );
  447. _color.multiply( _diffuseColor ).add( _emissiveColor );
  448. material.wireframe === true
  449. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  450. : fillPath( _color );
  451. } else if ( material.isMeshBasicMaterial || material.isMeshLambertMaterial || material.isMeshPhongMaterial || material.isMeshStandardMaterial ) {
  452. if ( material.map !== null ) {
  453. var mapping = material.map.mapping;
  454. if ( mapping === THREE.UVMapping ) {
  455. _uvs = element.uvs;
  456. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uvs[ uv1 ].x, _uvs[ uv1 ].y, _uvs[ uv2 ].x, _uvs[ uv2 ].y, _uvs[ uv3 ].x, _uvs[ uv3 ].y, material.map );
  457. }
  458. } else if ( material.envMap !== null ) {
  459. if ( material.envMap.mapping === THREE.SphericalReflectionMapping ) {
  460. _normal.copy( element.vertexNormalsModel[ uv1 ] ).applyMatrix3( _normalViewMatrix );
  461. _uv1x = 0.5 * _normal.x + 0.5;
  462. _uv1y = 0.5 * _normal.y + 0.5;
  463. _normal.copy( element.vertexNormalsModel[ uv2 ] ).applyMatrix3( _normalViewMatrix );
  464. _uv2x = 0.5 * _normal.x + 0.5;
  465. _uv2y = 0.5 * _normal.y + 0.5;
  466. _normal.copy( element.vertexNormalsModel[ uv3 ] ).applyMatrix3( _normalViewMatrix );
  467. _uv3x = 0.5 * _normal.x + 0.5;
  468. _uv3y = 0.5 * _normal.y + 0.5;
  469. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y, material.envMap );
  470. }
  471. } else {
  472. _color.copy( material.color );
  473. if ( material.vertexColors === THREE.FaceColors ) {
  474. _color.multiply( element.color );
  475. }
  476. material.wireframe === true
  477. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  478. : fillPath( _color );
  479. }
  480. } else if ( material.isMeshNormalMaterial ) {
  481. _normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
  482. _color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  483. material.wireframe === true
  484. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  485. : fillPath( _color );
  486. } else {
  487. _color.setRGB( 1, 1, 1 );
  488. material.wireframe === true
  489. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  490. : fillPath( _color );
  491. }
  492. }
  493. //
  494. function drawTriangle( x0, y0, x1, y1, x2, y2 ) {
  495. _context.beginPath();
  496. _context.moveTo( x0, y0 );
  497. _context.lineTo( x1, y1 );
  498. _context.lineTo( x2, y2 );
  499. _context.closePath();
  500. }
  501. function strokePath( color, linewidth, linecap, linejoin ) {
  502. setLineWidth( linewidth );
  503. setLineCap( linecap );
  504. setLineJoin( linejoin );
  505. setStrokeStyle( color.getStyle() );
  506. _context.stroke();
  507. _elemBox.expandByScalar( linewidth * 2 );
  508. }
  509. function fillPath( color ) {
  510. setFillStyle( color.getStyle() );
  511. _context.fill();
  512. }
  513. function textureToPattern( texture ) {
  514. if ( texture.version === 0 ||
  515. texture instanceof THREE.CompressedTexture ||
  516. texture instanceof THREE.DataTexture ) {
  517. return {
  518. canvas: undefined,
  519. version: texture.version
  520. };
  521. }
  522. var image = texture.image;
  523. if ( image.complete === false ) {
  524. return {
  525. canvas: undefined,
  526. version: 0
  527. };
  528. }
  529. var repeatX = texture.wrapS === THREE.RepeatWrapping || texture.wrapS === THREE.MirroredRepeatWrapping;
  530. var repeatY = texture.wrapT === THREE.RepeatWrapping || texture.wrapT === THREE.MirroredRepeatWrapping;
  531. var mirrorX = texture.wrapS === THREE.MirroredRepeatWrapping;
  532. var mirrorY = texture.wrapT === THREE.MirroredRepeatWrapping;
  533. //
  534. var canvas = document.createElement( 'canvas' );
  535. canvas.width = image.width * ( mirrorX ? 2 : 1 );
  536. canvas.height = image.height * ( mirrorY ? 2 : 1 );
  537. var context = canvas.getContext( '2d' );
  538. context.setTransform( 1, 0, 0, - 1, 0, image.height );
  539. context.drawImage( image, 0, 0 );
  540. if ( mirrorX === true ) {
  541. context.setTransform( - 1, 0, 0, - 1, image.width, image.height );
  542. context.drawImage( image, - image.width, 0 );
  543. }
  544. if ( mirrorY === true ) {
  545. context.setTransform( 1, 0, 0, 1, 0, 0 );
  546. context.drawImage( image, 0, image.height );
  547. }
  548. if ( mirrorX === true && mirrorY === true ) {
  549. context.setTransform( - 1, 0, 0, 1, image.width, 0 );
  550. context.drawImage( image, - image.width, image.height );
  551. }
  552. var repeat = 'no-repeat';
  553. if ( repeatX === true && repeatY === true ) {
  554. repeat = 'repeat';
  555. } else if ( repeatX === true ) {
  556. repeat = 'repeat-x';
  557. } else if ( repeatY === true ) {
  558. repeat = 'repeat-y';
  559. }
  560. var pattern = _context.createPattern( canvas, repeat );
  561. if ( texture.onUpdate ) texture.onUpdate( texture );
  562. return {
  563. canvas: pattern,
  564. version: texture.version
  565. };
  566. }
  567. function patternPath( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, texture ) {
  568. var pattern = _patterns[ texture.id ];
  569. if ( pattern === undefined || pattern.version !== texture.version ) {
  570. pattern = textureToPattern( texture );
  571. _patterns[ texture.id ] = pattern;
  572. }
  573. if ( pattern.canvas !== undefined ) {
  574. setFillStyle( pattern.canvas );
  575. } else {
  576. setFillStyle( 'rgba( 0, 0, 0, 1)' );
  577. _context.fill();
  578. return;
  579. }
  580. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  581. var a, b, c, d, e, f, det, idet,
  582. offsetX = texture.offset.x / texture.repeat.x,
  583. offsetY = texture.offset.y / texture.repeat.y,
  584. width = texture.image.width * texture.repeat.x,
  585. height = texture.image.height * texture.repeat.y;
  586. u0 = ( u0 + offsetX ) * width;
  587. v0 = ( v0 + offsetY ) * height;
  588. u1 = ( u1 + offsetX ) * width;
  589. v1 = ( v1 + offsetY ) * height;
  590. u2 = ( u2 + offsetX ) * width;
  591. v2 = ( v2 + offsetY ) * height;
  592. x1 -= x0; y1 -= y0;
  593. x2 -= x0; y2 -= y0;
  594. u1 -= u0; v1 -= v0;
  595. u2 -= u0; v2 -= v0;
  596. det = u1 * v2 - u2 * v1;
  597. if ( det === 0 ) return;
  598. idet = 1 / det;
  599. a = ( v2 * x1 - v1 * x2 ) * idet;
  600. b = ( v2 * y1 - v1 * y2 ) * idet;
  601. c = ( u1 * x2 - u2 * x1 ) * idet;
  602. d = ( u1 * y2 - u2 * y1 ) * idet;
  603. e = x0 - a * u0 - c * v0;
  604. f = y0 - b * u0 - d * v0;
  605. _context.save();
  606. _context.transform( a, b, c, d, e, f );
  607. _context.fill();
  608. _context.restore();
  609. }
  610. /*
  611. function clipImage( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, image ) {
  612. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  613. var a, b, c, d, e, f, det, idet,
  614. width = image.width - 1,
  615. height = image.height - 1;
  616. u0 *= width; v0 *= height;
  617. u1 *= width; v1 *= height;
  618. u2 *= width; v2 *= height;
  619. x1 -= x0; y1 -= y0;
  620. x2 -= x0; y2 -= y0;
  621. u1 -= u0; v1 -= v0;
  622. u2 -= u0; v2 -= v0;
  623. det = u1 * v2 - u2 * v1;
  624. idet = 1 / det;
  625. a = ( v2 * x1 - v1 * x2 ) * idet;
  626. b = ( v2 * y1 - v1 * y2 ) * idet;
  627. c = ( u1 * x2 - u2 * x1 ) * idet;
  628. d = ( u1 * y2 - u2 * y1 ) * idet;
  629. e = x0 - a * u0 - c * v0;
  630. f = y0 - b * u0 - d * v0;
  631. _context.save();
  632. _context.transform( a, b, c, d, e, f );
  633. _context.clip();
  634. _context.drawImage( image, 0, 0 );
  635. _context.restore();
  636. }
  637. */
  638. // Hide anti-alias gaps
  639. function expand( v1, v2, pixels ) {
  640. var x = v2.x - v1.x, y = v2.y - v1.y,
  641. det = x * x + y * y, idet;
  642. if ( det === 0 ) return;
  643. idet = pixels / Math.sqrt( det );
  644. x *= idet; y *= idet;
  645. v2.x += x; v2.y += y;
  646. v1.x -= x; v1.y -= y;
  647. }
  648. // Context cached methods.
  649. function setOpacity( value ) {
  650. if ( _contextGlobalAlpha !== value ) {
  651. _context.globalAlpha = value;
  652. _contextGlobalAlpha = value;
  653. }
  654. }
  655. function setBlending( value ) {
  656. if ( _contextGlobalCompositeOperation !== value ) {
  657. if ( value === THREE.NormalBlending ) {
  658. _context.globalCompositeOperation = 'source-over';
  659. } else if ( value === THREE.AdditiveBlending ) {
  660. _context.globalCompositeOperation = 'lighter';
  661. } else if ( value === THREE.SubtractiveBlending ) {
  662. _context.globalCompositeOperation = 'darker';
  663. } else if ( value === THREE.MultiplyBlending ) {
  664. _context.globalCompositeOperation = 'multiply';
  665. }
  666. _contextGlobalCompositeOperation = value;
  667. }
  668. }
  669. function setLineWidth( value ) {
  670. if ( _contextLineWidth !== value ) {
  671. _context.lineWidth = value;
  672. _contextLineWidth = value;
  673. }
  674. }
  675. function setLineCap( value ) {
  676. // "butt", "round", "square"
  677. if ( _contextLineCap !== value ) {
  678. _context.lineCap = value;
  679. _contextLineCap = value;
  680. }
  681. }
  682. function setLineJoin( value ) {
  683. // "round", "bevel", "miter"
  684. if ( _contextLineJoin !== value ) {
  685. _context.lineJoin = value;
  686. _contextLineJoin = value;
  687. }
  688. }
  689. function setStrokeStyle( value ) {
  690. if ( _contextStrokeStyle !== value ) {
  691. _context.strokeStyle = value;
  692. _contextStrokeStyle = value;
  693. }
  694. }
  695. function setFillStyle( value ) {
  696. if ( _contextFillStyle !== value ) {
  697. _context.fillStyle = value;
  698. _contextFillStyle = value;
  699. }
  700. }
  701. function setLineDash( value ) {
  702. if ( _contextLineDash.length !== value.length ) {
  703. _context.setLineDash( value );
  704. _contextLineDash = value;
  705. }
  706. }
  707. };