CanvasRenderer.js 24 KB

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