2
0

CanvasRenderer.js 25 KB

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