CanvasRenderer.js 24 KB

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