CanvasRenderer.js 26 KB

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