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