CanvasRenderer.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. THREE.CanvasRenderer = function ( parameters ) {
  5. var _this = this,
  6. _renderList = null,
  7. _projector = new THREE.Projector(),
  8. parameters = parameters || {},
  9. _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
  10. _canvasWidth, _canvasHeight, _canvasWidthHalf, _canvasHeightHalf,
  11. _context = _canvas.getContext( '2d' ),
  12. _clearColor = new THREE.Color( 0x000000 ),
  13. _clearOpacity = 0,
  14. _contextGlobalAlpha = 1,
  15. _contextGlobalCompositeOperation = 0,
  16. _contextStrokeStyle = null,
  17. _contextFillStyle = null,
  18. _contextLineWidth = null,
  19. _contextLineCap = null,
  20. _contextLineJoin = null,
  21. _v1, _v2, _v3, _v4,
  22. _v5 = new THREE.RenderableVertex(),
  23. _v6 = new THREE.RenderableVertex(),
  24. _v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
  25. _v4x, _v4y, _v5x, _v5y, _v6x, _v6y,
  26. _color = new THREE.Color( 0x000000 ),
  27. _color1 = new THREE.Color( 0x000000 ),
  28. _color2 = new THREE.Color( 0x000000 ),
  29. _color3 = new THREE.Color( 0x000000 ),
  30. _color4 = new THREE.Color( 0x000000 ),
  31. _patterns = [],
  32. _near, _far,
  33. _image, _uvs,
  34. _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y,
  35. _clipRect = new THREE.Rectangle(),
  36. _clearRect = new THREE.Rectangle(),
  37. _bboxRect = new THREE.Rectangle(),
  38. _enableLighting = false,
  39. _light = new THREE.Color(),
  40. _ambientLight = new THREE.Color(),
  41. _directionalLights = new THREE.Color(),
  42. _pointLights = new THREE.Color(),
  43. _pi2 = Math.PI * 2,
  44. _vector3 = new THREE.Vector3(), // Needed for PointLight
  45. _pixelMap, _pixelMapContext, _pixelMapImage, _pixelMapData,
  46. _gradientMap, _gradientMapContext, _gradientMapQuality = 16;
  47. _pixelMap = document.createElement( 'canvas' );
  48. _pixelMap.width = _pixelMap.height = 2;
  49. _pixelMapContext = _pixelMap.getContext( '2d' );
  50. _pixelMapContext.fillStyle = 'rgba(0,0,0,1)';
  51. _pixelMapContext.fillRect( 0, 0, 2, 2 );
  52. _pixelMapImage = _pixelMapContext.getImageData( 0, 0, 2, 2 );
  53. _pixelMapData = _pixelMapImage.data;
  54. _gradientMap = document.createElement( 'canvas' );
  55. _gradientMap.width = _gradientMap.height = _gradientMapQuality;
  56. _gradientMapContext = _gradientMap.getContext( '2d' );
  57. _gradientMapContext.translate( - _gradientMapQuality / 2, - _gradientMapQuality / 2 );
  58. _gradientMapContext.scale( _gradientMapQuality, _gradientMapQuality );
  59. _gradientMapQuality --; // Fix UVs
  60. this.domElement = _canvas;
  61. this.autoClear = true;
  62. this.sortObjects = true;
  63. this.sortElements = true;
  64. this.data = {
  65. vertices: 0,
  66. faces: 0
  67. }
  68. this.setSize = function ( width, height ) {
  69. _canvasWidth = width;
  70. _canvasHeight = height;
  71. _canvasWidthHalf = Math.floor( _canvasWidth / 2 );
  72. _canvasHeightHalf = Math.floor( _canvasHeight / 2 );
  73. _canvas.width = _canvasWidth;
  74. _canvas.height = _canvasHeight;
  75. _clipRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  76. _clearRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  77. _contextGlobalAlpha = 1;
  78. _contextGlobalCompositeOperation = 0;
  79. _contextStrokeStyle = null;
  80. _contextFillStyle = null;
  81. _contextLineWidth = null;
  82. _contextLineCap = null;
  83. _contextLineJoin = null;
  84. };
  85. this.setClearColor = function( color, opacity ) {
  86. _clearColor.copy( color );
  87. _clearOpacity = opacity;
  88. _clearRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  89. };
  90. this.setClearColorHex = function( hex, opacity ) {
  91. _clearColor.setHex( hex );
  92. _clearOpacity = opacity;
  93. _clearRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  94. };
  95. this.clear = function () {
  96. _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf );
  97. if ( !_clearRect.isEmpty() ) {
  98. _clearRect.minSelf( _clipRect );
  99. _clearRect.inflate( 2 );
  100. if ( _clearOpacity < 1 ) {
  101. _context.clearRect( Math.floor( _clearRect.getX() ), Math.floor( _clearRect.getY() ), Math.floor( _clearRect.getWidth() ), Math.floor( _clearRect.getHeight() ) );
  102. }
  103. if ( _clearOpacity > 0 ) {
  104. setBlending( THREE.NormalBlending );
  105. setOpacity( 1 );
  106. setFillStyle( 'rgba(' + Math.floor( _clearColor.r * 255 ) + ',' + Math.floor( _clearColor.g * 255 ) + ',' + Math.floor( _clearColor.b * 255 ) + ',' + _clearOpacity + ')' );
  107. _context.fillRect( Math.floor( _clearRect.getX() ), Math.floor( _clearRect.getY() ), Math.floor( _clearRect.getWidth() ), Math.floor( _clearRect.getHeight() ) );
  108. }
  109. _clearRect.empty();
  110. }
  111. };
  112. this.render = function ( scene, camera ) {
  113. var e, el, element, m, ml, fm, fml, material;
  114. this.autoClear ? this.clear() : _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf );
  115. _this.data.vertices = 0;
  116. _this.data.faces = 0;
  117. _renderList = _projector.projectScene( scene, camera, this.sortElements );
  118. /* DEBUG
  119. _context.fillStyle = 'rgba( 0, 255, 255, 0.5 )';
  120. _context.fillRect( _clipRect.getX(), _clipRect.getY(), _clipRect.getWidth(), _clipRect.getHeight() );
  121. */
  122. _enableLighting = scene.lights.length > 0;
  123. if ( _enableLighting ) {
  124. calculateLights( scene );
  125. }
  126. for ( e = 0, el = _renderList.length; e < el; e++ ) {
  127. element = _renderList[ e ];
  128. _bboxRect.empty();
  129. if ( element instanceof THREE.RenderableParticle ) {
  130. _v1 = element;
  131. _v1.x *= _canvasWidthHalf; _v1.y *= _canvasHeightHalf;
  132. m = 0; ml = element.materials.length;
  133. while( m < ml ) {
  134. material = element.materials[ m ++ ];
  135. material.opacity != 0 && renderParticle( _v1, element, material, scene );
  136. }
  137. } else if ( element instanceof THREE.RenderableLine ) {
  138. _v1 = element.v1; _v2 = element.v2;
  139. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  140. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  141. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  142. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  143. if ( _clipRect.intersects( _bboxRect ) ) {
  144. m = 0; ml = element.materials.length;
  145. while ( m < ml ) {
  146. material = element.materials[ m ++ ];
  147. material.opacity != 0 && renderLine( _v1, _v2, element, material, scene );
  148. }
  149. }
  150. } else if ( element instanceof THREE.RenderableFace3 ) {
  151. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  152. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  153. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  154. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  155. if ( element.overdraw ) {
  156. expand( _v1.positionScreen, _v2.positionScreen );
  157. expand( _v2.positionScreen, _v3.positionScreen );
  158. expand( _v3.positionScreen, _v1.positionScreen );
  159. }
  160. _bboxRect.add3Points( _v1.positionScreen.x, _v1.positionScreen.y,
  161. _v2.positionScreen.x, _v2.positionScreen.y,
  162. _v3.positionScreen.x, _v3.positionScreen.y );
  163. if ( _clipRect.intersects( _bboxRect ) ) {
  164. m = 0; ml = element.meshMaterials.length;
  165. while ( m < ml ) {
  166. material = element.meshMaterials[ m ++ ];
  167. if ( material instanceof THREE.MeshFaceMaterial ) {
  168. fm = 0; fml = element.faceMaterials.length;
  169. while ( fm < fml ) {
  170. material = element.faceMaterials[ fm ++ ];
  171. material && material.opacity != 0 && renderFace3( _v1, _v2, _v3, 0, 1, 2, element, material, scene );
  172. }
  173. continue;
  174. }
  175. material.opacity != 0 && renderFace3( _v1, _v2, _v3, 0, 1, 2, element, material, scene );
  176. }
  177. }
  178. } else if ( element instanceof THREE.RenderableFace4 ) {
  179. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3; _v4 = element.v4;
  180. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  181. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  182. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  183. _v4.positionScreen.x *= _canvasWidthHalf; _v4.positionScreen.y *= _canvasHeightHalf;
  184. _v5.positionScreen.copy( _v2.positionScreen );
  185. _v6.positionScreen.copy( _v4.positionScreen );
  186. if ( element.overdraw ) {
  187. expand( _v1.positionScreen, _v2.positionScreen );
  188. expand( _v2.positionScreen, _v4.positionScreen );
  189. expand( _v4.positionScreen, _v1.positionScreen );
  190. expand( _v3.positionScreen, _v5.positionScreen );
  191. expand( _v3.positionScreen, _v6.positionScreen );
  192. }
  193. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  194. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  195. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  196. _bboxRect.addPoint( _v4.positionScreen.x, _v4.positionScreen.y );
  197. if ( _clipRect.intersects( _bboxRect ) ) {
  198. m = 0; ml = element.meshMaterials.length;
  199. while ( m < ml ) {
  200. material = element.meshMaterials[ m ++ ];
  201. if ( material instanceof THREE.MeshFaceMaterial ) {
  202. fm = 0; fml = element.faceMaterials.length;
  203. while ( fm < fml ) {
  204. material = element.faceMaterials[ fm ++ ];
  205. material && material.opacity != 0 && renderFace4( _v1, _v2, _v3, _v4, _v5, _v6, element, material, scene );
  206. }
  207. continue;
  208. }
  209. material.opacity != 0 && renderFace4( _v1, _v2, _v3, _v4, _v5, _v6, element, material, scene );
  210. }
  211. }
  212. }
  213. /*
  214. _context.lineWidth = 1;
  215. _context.strokeStyle = 'rgba( 0, 255, 0, 0.5 )';
  216. _context.strokeRect( _bboxRect.getX(), _bboxRect.getY(), _bboxRect.getWidth(), _bboxRect.getHeight() );
  217. */
  218. _clearRect.addRectangle( _bboxRect );
  219. }
  220. /* DEBUG
  221. _context.lineWidth = 1;
  222. _context.strokeStyle = 'rgba( 255, 0, 0, 0.5 )';
  223. _context.strokeRect( _clearRect.getX(), _clearRect.getY(), _clearRect.getWidth(), _clearRect.getHeight() );
  224. */
  225. _context.setTransform( 1, 0, 0, 1, 0, 0 );
  226. //
  227. function calculateLights( scene ) {
  228. var l, ll, light, lightColor,
  229. lights = scene.lights;
  230. _ambientLight.setRGB( 0, 0, 0 );
  231. _directionalLights.setRGB( 0, 0, 0 );
  232. _pointLights.setRGB( 0, 0, 0 );
  233. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  234. light = lights[ l ];
  235. lightColor = light.color;
  236. if ( light instanceof THREE.AmbientLight ) {
  237. _ambientLight.r += lightColor.r;
  238. _ambientLight.g += lightColor.g;
  239. _ambientLight.b += lightColor.b;
  240. } else if ( light instanceof THREE.DirectionalLight ) {
  241. // for particles
  242. _directionalLights.r += lightColor.r;
  243. _directionalLights.g += lightColor.g;
  244. _directionalLights.b += lightColor.b;
  245. } else if ( light instanceof THREE.PointLight ) {
  246. // for particles
  247. _pointLights.r += lightColor.r;
  248. _pointLights.g += lightColor.g;
  249. _pointLights.b += lightColor.b;
  250. }
  251. }
  252. }
  253. function calculateLight( scene, position, normal, color ) {
  254. var l, ll, light, lightColor,
  255. amount, lights = scene.lights;
  256. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  257. light = lights[ l ];
  258. lightColor = light.color;
  259. if ( light instanceof THREE.DirectionalLight ) {
  260. amount = normal.dot( light.position );
  261. if ( amount <= 0 ) continue;
  262. amount *= light.intensity;
  263. color.r += lightColor.r * amount;
  264. color.g += lightColor.g * amount;
  265. color.b += lightColor.b * amount;
  266. } else if ( light instanceof THREE.PointLight ) {
  267. amount = normal.dot( _vector3.sub( light.position, position ).normalize() );
  268. if ( amount <= 0 ) continue;
  269. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( light.position ) / light.distance, 1 );
  270. if ( amount == 0 ) continue;
  271. amount *= light.intensity;
  272. color.r += lightColor.r * amount;
  273. color.g += lightColor.g * amount;
  274. color.b += lightColor.b * amount;
  275. }
  276. }
  277. }
  278. function renderParticle ( v1, element, material, scene ) {
  279. setOpacity( material.opacity );
  280. setBlending( material.blending );
  281. var width, height, scaleX, scaleY,
  282. bitmap, bitmapWidth, bitmapHeight;
  283. if ( material instanceof THREE.ParticleBasicMaterial ) {
  284. if ( material.map ) {
  285. bitmap = material.map.image;
  286. bitmapWidth = bitmap.width >> 1;
  287. bitmapHeight = bitmap.height >> 1;
  288. scaleX = element.scale.x * _canvasWidthHalf;
  289. scaleY = element.scale.y * _canvasHeightHalf;
  290. width = scaleX * bitmapWidth;
  291. height = scaleY * bitmapHeight;
  292. // TODO: Rotations break this...
  293. _bboxRect.set( v1.x - width, v1.y - height, v1.x + width, v1.y + height );
  294. if ( !_clipRect.intersects( _bboxRect ) ) {
  295. return;
  296. }
  297. _context.save();
  298. _context.translate( v1.x, v1.y );
  299. _context.rotate( - element.rotation );
  300. _context.scale( scaleX, - scaleY );
  301. _context.translate( - bitmapWidth, - bitmapHeight );
  302. _context.drawImage( bitmap, 0, 0 );
  303. _context.restore();
  304. }
  305. /* DEBUG
  306. _context.beginPath();
  307. _context.moveTo( v1.x - 10, v1.y );
  308. _context.lineTo( v1.x + 10, v1.y );
  309. _context.moveTo( v1.x, v1.y - 10 );
  310. _context.lineTo( v1.x, v1.y + 10 );
  311. _context.closePath();
  312. _context.strokeStyle = 'rgb(255,255,0)';
  313. _context.stroke();
  314. */
  315. } else if ( material instanceof THREE.ParticleCanvasMaterial ) {
  316. width = element.scale.x * _canvasWidthHalf;
  317. height = element.scale.y * _canvasHeightHalf;
  318. _bboxRect.set( v1.x - width, v1.y - height, v1.x + width, v1.y + height );
  319. if ( !_clipRect.intersects( _bboxRect ) ) {
  320. return;
  321. }
  322. setStrokeStyle( material.color.getContextStyle() );
  323. setFillStyle( material.color.getContextStyle() );
  324. _context.save();
  325. _context.translate( v1.x, v1.y );
  326. _context.rotate( - element.rotation );
  327. _context.scale( width, height );
  328. material.program( _context );
  329. _context.restore();
  330. }
  331. }
  332. function renderLine( v1, v2, element, material, scene ) {
  333. setOpacity( material.opacity );
  334. setBlending( material.blending );
  335. _context.beginPath();
  336. _context.moveTo( v1.positionScreen.x, v1.positionScreen.y );
  337. _context.lineTo( v2.positionScreen.x, v2.positionScreen.y );
  338. _context.closePath();
  339. if ( material instanceof THREE.LineBasicMaterial ) {
  340. setLineWidth( material.linewidth );
  341. setLineCap( material.linecap );
  342. setLineJoin( material.linejoin );
  343. setStrokeStyle( material.color.getContextStyle() );
  344. _context.stroke();
  345. _bboxRect.inflate( material.linewidth * 2 );
  346. }
  347. }
  348. function renderFace3( v1, v2, v3, uv1, uv2, uv3, element, material, scene ) {
  349. _this.data.vertices += 3;
  350. _this.data.faces ++;
  351. setOpacity( material.opacity );
  352. setBlending( material.blending );
  353. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  354. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  355. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  356. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y );
  357. if ( material instanceof THREE.MeshBasicMaterial ) {
  358. if ( material.map/* && !material.wireframe*/ ) {
  359. if ( material.map.mapping instanceof THREE.UVMapping ) {
  360. _uvs = element.uvs[ 0 ];
  361. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uvs[ uv1 ].u, _uvs[ uv1 ].v, _uvs[ uv2 ].u, _uvs[ uv2 ].v, _uvs[ uv3 ].u, _uvs[ uv3 ].v, material.map );
  362. }
  363. } else if ( material.envMap ) {
  364. if ( material.envMap.mapping instanceof THREE.SphericalReflectionMapping ) {
  365. var cameraMatrix = camera.matrixWorldInverse;
  366. _vector3.copy( element.vertexNormalsWorld[ 0 ] );
  367. _uv1x = ( _vector3.x * cameraMatrix.n11 + _vector3.y * cameraMatrix.n12 + _vector3.z * cameraMatrix.n13 ) * 0.5 + 0.5;
  368. _uv1y = - ( _vector3.x * cameraMatrix.n21 + _vector3.y * cameraMatrix.n22 + _vector3.z * cameraMatrix.n23 ) * 0.5 + 0.5;
  369. _vector3.copy( element.vertexNormalsWorld[ 1 ] );
  370. _uv2x = ( _vector3.x * cameraMatrix.n11 + _vector3.y * cameraMatrix.n12 + _vector3.z * cameraMatrix.n13 ) * 0.5 + 0.5;
  371. _uv2y = - ( _vector3.x * cameraMatrix.n21 + _vector3.y * cameraMatrix.n22 + _vector3.z * cameraMatrix.n23 ) * 0.5 + 0.5;
  372. _vector3.copy( element.vertexNormalsWorld[ 2 ] );
  373. _uv3x = ( _vector3.x * cameraMatrix.n11 + _vector3.y * cameraMatrix.n12 + _vector3.z * cameraMatrix.n13 ) * 0.5 + 0.5;
  374. _uv3y = - ( _vector3.x * cameraMatrix.n21 + _vector3.y * cameraMatrix.n22 + _vector3.z * cameraMatrix.n23 ) * 0.5 + 0.5;
  375. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y, material.envMap );
  376. }/* else if ( material.envMap.mapping == THREE.SphericalRefractionMapping ) {
  377. }*/
  378. } else {
  379. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  380. }
  381. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  382. if ( material.map && !material.wireframe ) {
  383. if ( material.map.mapping instanceof THREE.UVMapping ) {
  384. _uvs = element.uvs[ 0 ];
  385. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uvs[ uv1 ].u, _uvs[ uv1 ].v, _uvs[ uv2 ].u, _uvs[ uv2 ].v, _uvs[ uv3 ].u, _uvs[ uv3 ].v, material.map );
  386. }
  387. setBlending( THREE.SubtractiveBlending );
  388. }
  389. if ( _enableLighting ) {
  390. if ( !material.wireframe && material.shading == THREE.SmoothShading && element.vertexNormalsWorld.length == 3 ) {
  391. _color1.r = _color2.r = _color3.r = _ambientLight.r;
  392. _color1.g = _color2.g = _color3.g = _ambientLight.g;
  393. _color1.b = _color2.b = _color3.b = _ambientLight.b;
  394. calculateLight( scene, element.v1.positionWorld, element.vertexNormalsWorld[ 0 ], _color1 );
  395. calculateLight( scene, element.v2.positionWorld, element.vertexNormalsWorld[ 1 ], _color2 );
  396. calculateLight( scene, element.v3.positionWorld, element.vertexNormalsWorld[ 2 ], _color3 );
  397. _color1.r = Math.max( 0, Math.min( material.color.r * _color1.r, 1 ) );
  398. _color1.g = Math.max( 0, Math.min( material.color.g * _color1.g, 1 ) );
  399. _color1.b = Math.max( 0, Math.min( material.color.b * _color1.b, 1 ) );
  400. _color2.r = Math.max( 0, Math.min( material.color.r * _color2.r, 1 ) );
  401. _color2.g = Math.max( 0, Math.min( material.color.g * _color2.g, 1 ) );
  402. _color2.b = Math.max( 0, Math.min( material.color.b * _color2.b, 1 ) );
  403. _color3.r = Math.max( 0, Math.min( material.color.r * _color3.r, 1 ) );
  404. _color3.g = Math.max( 0, Math.min( material.color.g * _color3.g, 1 ) );
  405. _color3.b = Math.max( 0, Math.min( material.color.b * _color3.b, 1 ) );
  406. _color4.r = ( _color2.r + _color3.r ) * 0.5;
  407. _color4.g = ( _color2.g + _color3.g ) * 0.5;
  408. _color4.b = ( _color2.b + _color3.b ) * 0.5;
  409. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  410. clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image );
  411. } else {
  412. _light.r = _ambientLight.r;
  413. _light.g = _ambientLight.g;
  414. _light.b = _ambientLight.b;
  415. calculateLight( scene, element.centroidWorld, element.normalWorld, _light );
  416. _color.r = Math.max( 0, Math.min( material.color.r * _light.r, 1 ) );
  417. _color.g = Math.max( 0, Math.min( material.color.g * _light.g, 1 ) );
  418. _color.b = Math.max( 0, Math.min( material.color.b * _light.b, 1 ) );
  419. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  420. }
  421. } else {
  422. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  423. }
  424. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  425. _near = camera.near;
  426. _far = camera.far;
  427. _color1.r = _color1.g = _color1.b = 1 - smoothstep( v1.positionScreen.z, _near, _far );
  428. _color2.r = _color2.g = _color2.b = 1 - smoothstep( v2.positionScreen.z, _near, _far );
  429. _color3.r = _color3.g = _color3.b = 1 - smoothstep( v3.positionScreen.z, _near, _far );
  430. _color4.r = ( _color2.r + _color3.r ) * 0.5;
  431. _color4.g = ( _color2.g + _color3.g ) * 0.5;
  432. _color4.b = ( _color2.b + _color3.b ) * 0.5;
  433. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  434. clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image );
  435. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  436. _color.r = normalToComponent( element.normalWorld.x );
  437. _color.g = normalToComponent( element.normalWorld.y );
  438. _color.b = normalToComponent( element.normalWorld.z );
  439. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  440. }
  441. }
  442. function renderFace4( v1, v2, v3, v4, v5, v6, element, material, scene ) {
  443. _this.data.vertices += 4;
  444. _this.data.faces ++;
  445. setOpacity( material.opacity );
  446. setBlending( material.blending );
  447. if ( material.map || material.envMap ) {
  448. // Let renderFace3() handle this
  449. renderFace3( v1, v2, v4, 0, 1, 3, element, material, scene );
  450. renderFace3( v5, v3, v6, 1, 2, 3, element, material, scene );
  451. return;
  452. }
  453. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  454. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  455. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  456. _v4x = v4.positionScreen.x; _v4y = v4.positionScreen.y;
  457. _v5x = v5.positionScreen.x; _v5y = v5.positionScreen.y;
  458. _v6x = v6.positionScreen.x; _v6y = v6.positionScreen.y;
  459. if ( material instanceof THREE.MeshBasicMaterial ) {
  460. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  461. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  462. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  463. if ( _enableLighting ) {
  464. if ( !material.wireframe && material.shading == THREE.SmoothShading && element.vertexNormalsWorld.length == 4 ) {
  465. _color1.r = _color2.r = _color3.r = _color4.r = _ambientLight.r;
  466. _color1.g = _color2.g = _color3.g = _color4.g = _ambientLight.g;
  467. _color1.b = _color2.b = _color3.b = _color4.b = _ambientLight.b;
  468. calculateLight( scene, element.v1.positionWorld, element.vertexNormalsWorld[ 0 ], _color1 );
  469. calculateLight( scene, element.v2.positionWorld, element.vertexNormalsWorld[ 1 ], _color2 );
  470. calculateLight( scene, element.v4.positionWorld, element.vertexNormalsWorld[ 3 ], _color3 );
  471. calculateLight( scene, element.v3.positionWorld, element.vertexNormalsWorld[ 2 ], _color4 );
  472. _color1.r = Math.max( 0, Math.min( material.color.r * _color1.r, 1 ) );
  473. _color1.g = Math.max( 0, Math.min( material.color.g * _color1.g, 1 ) );
  474. _color1.b = Math.max( 0, Math.min( material.color.b * _color1.b, 1 ) );
  475. _color2.r = Math.max( 0, Math.min( material.color.r * _color2.r, 1 ) );
  476. _color2.g = Math.max( 0, Math.min( material.color.g * _color2.g, 1 ) );
  477. _color2.b = Math.max( 0, Math.min( material.color.b * _color2.b, 1 ) );
  478. _color3.r = Math.max( 0, Math.min( material.color.r * _color3.r, 1 ) );
  479. _color3.g = Math.max( 0, Math.min( material.color.g * _color3.g, 1 ) );
  480. _color3.b = Math.max( 0, Math.min( material.color.b * _color3.b, 1 ) );
  481. _color4.r = Math.max( 0, Math.min( material.color.r * _color4.r, 1 ) );
  482. _color4.g = Math.max( 0, Math.min( material.color.g * _color4.g, 1 ) );
  483. _color4.b = Math.max( 0, Math.min( material.color.b * _color4.b, 1 ) );
  484. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  485. // TODO: UVs are incorrect, v4->v3?
  486. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y );
  487. clipImage( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y, 0, 0, 1, 0, 0, 1, _image );
  488. drawTriangle( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y );
  489. clipImage( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y, 1, 0, 1, 1, 0, 1, _image );
  490. } else {
  491. _light.r = _ambientLight.r;
  492. _light.g = _ambientLight.g;
  493. _light.b = _ambientLight.b;
  494. calculateLight( scene, element.centroidWorld, element.normalWorld, _light );
  495. _color.r = Math.max( 0, Math.min( material.color.r * _light.r, 1 ) );
  496. _color.g = Math.max( 0, Math.min( material.color.g * _light.g, 1 ) );
  497. _color.b = Math.max( 0, Math.min( material.color.b * _light.b, 1 ) );
  498. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  499. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  500. }
  501. } else {
  502. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  503. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  504. }
  505. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  506. _color.r = normalToComponent( element.normalWorld.x );
  507. _color.g = normalToComponent( element.normalWorld.y );
  508. _color.b = normalToComponent( element.normalWorld.z );
  509. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  510. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  511. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  512. _near = camera.near;
  513. _far = camera.far;
  514. _color1.r = _color1.g = _color1.b = 1 - smoothstep( v1.positionScreen.z, _near, _far );
  515. _color2.r = _color2.g = _color2.b = 1 - smoothstep( v2.positionScreen.z, _near, _far );
  516. _color3.r = _color3.g = _color3.b = 1 - smoothstep( v4.positionScreen.z, _near, _far );
  517. _color4.r = _color4.g = _color4.b = 1 - smoothstep( v3.positionScreen.z, _near, _far );
  518. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  519. // TODO: UVs are incorrect, v4->v3?
  520. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y );
  521. clipImage( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y, 0, 0, 1, 0, 0, 1, _image );
  522. drawTriangle( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y );
  523. clipImage( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y, 1, 0, 1, 1, 0, 1, _image );
  524. }
  525. }
  526. //
  527. function drawTriangle( x0, y0, x1, y1, x2, y2 ) {
  528. _context.beginPath();
  529. _context.moveTo( x0, y0 );
  530. _context.lineTo( x1, y1 );
  531. _context.lineTo( x2, y2 );
  532. _context.lineTo( x0, y0 );
  533. _context.closePath();
  534. }
  535. function drawQuad( x0, y0, x1, y1, x2, y2, x3, y3 ) {
  536. _context.beginPath();
  537. _context.moveTo( x0, y0 );
  538. _context.lineTo( x1, y1 );
  539. _context.lineTo( x2, y2 );
  540. _context.lineTo( x3, y3 );
  541. _context.lineTo( x0, y0 );
  542. _context.closePath();
  543. }
  544. function strokePath( color, linewidth, linecap, linejoin ) {
  545. setLineWidth( linewidth );
  546. setLineCap( linecap );
  547. setLineJoin( linejoin );
  548. setStrokeStyle( color.getContextStyle() );
  549. _context.stroke();
  550. _bboxRect.inflate( linewidth * 2 );
  551. }
  552. function fillPath( color ) {
  553. setFillStyle( color.getContextStyle() );
  554. _context.fill();
  555. }
  556. function patternPath( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, texture ) {
  557. if ( texture.image.width == 0 ) return;
  558. if ( texture.needsUpdate == true || _patterns[ texture.id ] == undefined ) {
  559. var repeatX = texture.wrapS == THREE.RepeatWrapping;
  560. var repeatY = texture.wrapT == THREE.RepeatWrapping;
  561. _patterns[ texture.id ] = _context.createPattern( texture.image, repeatX && repeatY ? 'repeat' : repeatX && !repeatY ? 'repeat-x' : !repeatX && repeatY ? 'repeat-y' : 'no-repeat' );
  562. texture.needsUpdate = false;
  563. }
  564. setFillStyle( _patterns[ texture.id ] );
  565. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  566. var a, b, c, d, e, f, det, idet,
  567. offsetX = texture.offset.x / texture.repeat.x,
  568. offsetY = texture.offset.y / texture.repeat.y,
  569. width = ( texture.image.width - 1 ) * texture.repeat.x,
  570. height = ( texture.image.height - 1 ) * texture.repeat.y;
  571. u0 = ( u0 + offsetX ) * width;
  572. v0 = ( v0 + offsetY ) * height;
  573. u1 = ( u1 + offsetX ) * width;
  574. v1 = ( v1 + offsetY ) * height;
  575. u2 = ( u2 + offsetX ) * width;
  576. v2 = ( v2 + offsetY ) * height;
  577. x1 -= x0; y1 -= y0;
  578. x2 -= x0; y2 -= y0;
  579. u1 -= u0; v1 -= v0;
  580. u2 -= u0; v2 -= v0;
  581. det = u1 * v2 - u2 * v1;
  582. idet = 1 / det;
  583. a = ( v2 * x1 - v1 * x2 ) * idet;
  584. b = ( v2 * y1 - v1 * y2 ) * idet;
  585. c = ( u1 * x2 - u2 * x1 ) * idet;
  586. d = ( u1 * y2 - u2 * y1 ) * idet;
  587. e = x0 - a * u0 - c * v0;
  588. f = y0 - b * u0 - d * v0;
  589. _context.save();
  590. _context.transform( a, b, c, d, e, f );
  591. _context.fill();
  592. _context.restore();
  593. }
  594. function clipImage( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, image ) {
  595. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  596. var a, b, c, d, e, f, det, idet,
  597. width = image.width - 1,
  598. height = image.height - 1;
  599. u0 *= width; v0 *= height;
  600. u1 *= width; v1 *= height;
  601. u2 *= width; v2 *= height;
  602. x1 -= x0; y1 -= y0;
  603. x2 -= x0; y2 -= y0;
  604. u1 -= u0; v1 -= v0;
  605. u2 -= u0; v2 -= v0;
  606. det = u1 * v2 - u2 * v1;
  607. idet = 1 / det;
  608. a = ( v2 * x1 - v1 * x2 ) * idet;
  609. b = ( v2 * y1 - v1 * y2 ) * idet;
  610. c = ( u1 * x2 - u2 * x1 ) * idet;
  611. d = ( u1 * y2 - u2 * y1 ) * idet;
  612. e = x0 - a * u0 - c * v0;
  613. f = y0 - b * u0 - d * v0;
  614. _context.save();
  615. _context.transform( a, b, c, d, e, f );
  616. _context.clip();
  617. _context.drawImage( image, 0, 0 );
  618. _context.restore();
  619. }
  620. function getGradientTexture( color1, color2, color3, color4 ) {
  621. // http://mrdoob.com/blog/post/710
  622. var c1r = ~~ ( color1.r * 255 ), c1g = ~~ ( color1.g * 255 ), c1b = ~~ ( color1.b * 255 ),
  623. c2r = ~~ ( color2.r * 255 ), c2g = ~~ ( color2.g * 255 ), c2b = ~~ ( color2.b * 255 ),
  624. c3r = ~~ ( color3.r * 255 ), c3g = ~~ ( color3.g * 255 ), c3b = ~~ ( color3.b * 255 ),
  625. c4r = ~~ ( color4.r * 255 ), c4g = ~~ ( color4.g * 255 ), c4b = ~~ ( color4.b * 255 );
  626. _pixelMapData[ 0 ] = c1r < 0 ? 0 : c1r > 255 ? 255 : c1r;
  627. _pixelMapData[ 1 ] = c1g < 0 ? 0 : c1g > 255 ? 255 : c1g;
  628. _pixelMapData[ 2 ] = c1b < 0 ? 0 : c1b > 255 ? 255 : c1b;
  629. _pixelMapData[ 4 ] = c2r < 0 ? 0 : c2r > 255 ? 255 : c2r;
  630. _pixelMapData[ 5 ] = c2g < 0 ? 0 : c2g > 255 ? 255 : c2g;
  631. _pixelMapData[ 6 ] = c2b < 0 ? 0 : c2b > 255 ? 255 : c2b;
  632. _pixelMapData[ 8 ] = c3r < 0 ? 0 : c3r > 255 ? 255 : c3r;
  633. _pixelMapData[ 9 ] = c3g < 0 ? 0 : c3g > 255 ? 255 : c3g;
  634. _pixelMapData[ 10 ] = c3b < 0 ? 0 : c3b > 255 ? 255 : c3b;
  635. _pixelMapData[ 12 ] = c4r < 0 ? 0 : c4r > 255 ? 255 : c4r;
  636. _pixelMapData[ 13 ] = c4g < 0 ? 0 : c4g > 255 ? 255 : c4g;
  637. _pixelMapData[ 14 ] = c4b < 0 ? 0 : c4b > 255 ? 255 : c4b;
  638. _pixelMapContext.putImageData( _pixelMapImage, 0, 0 );
  639. _gradientMapContext.drawImage( _pixelMap, 0, 0 );
  640. return _gradientMap;
  641. }
  642. function smoothstep( value, min, max ) {
  643. /*
  644. if ( value <= min ) return 0;
  645. if ( value >= max ) return 1;
  646. */
  647. var x = ( value - min ) / ( max - min );
  648. return x * x * ( 3 - 2 * x );
  649. }
  650. function normalToComponent( normal ) {
  651. var component = ( normal + 1 ) * 0.5;
  652. return component < 0 ? 0 : ( component > 1 ? 1 : component );
  653. }
  654. // Hide anti-alias gaps
  655. function expand( v1, v2 ) {
  656. var x = v2.x - v1.x, y = v2.y - v1.y,
  657. det = x * x + y * y, idet;
  658. if ( det == 0 ) return;
  659. idet = 1 / Math.sqrt( det );
  660. x *= idet; y *= idet;
  661. v2.x += x; v2.y += y;
  662. v1.x -= x; v1.y -= y;
  663. }
  664. };
  665. // Context cached methods.
  666. function setOpacity( value ) {
  667. if ( _contextGlobalAlpha != value ) {
  668. _context.globalAlpha = _contextGlobalAlpha = value;
  669. }
  670. }
  671. function setBlending( value ) {
  672. if ( _contextGlobalCompositeOperation != value ) {
  673. switch ( value ) {
  674. case THREE.NormalBlending:
  675. _context.globalCompositeOperation = 'source-over';
  676. break;
  677. case THREE.AdditiveBlending:
  678. _context.globalCompositeOperation = 'lighter';
  679. break;
  680. case THREE.SubtractiveBlending:
  681. _context.globalCompositeOperation = 'darker';
  682. break;
  683. }
  684. _contextGlobalCompositeOperation = value;
  685. }
  686. }
  687. function setLineWidth( value ) {
  688. if ( _contextLineWidth != value ) {
  689. _context.lineWidth = _contextLineWidth = value;
  690. }
  691. }
  692. function setLineCap( value ) {
  693. // "butt", "round", "square"
  694. if ( _contextLineCap != value ) {
  695. _context.lineCap = _contextLineCap = value;
  696. }
  697. }
  698. function setLineJoin( value ) {
  699. // "round", "bevel", "miter"
  700. if ( _contextLineJoin != value ) {
  701. _context.lineJoin = _contextLineJoin = value;
  702. }
  703. }
  704. function setStrokeStyle( style ) {
  705. if ( _contextStrokeStyle != style ) {
  706. _context.strokeStyle = _contextStrokeStyle = style;
  707. }
  708. }
  709. function setFillStyle( style ) {
  710. if ( _contextFillStyle != style ) {
  711. _context.fillStyle = _contextFillStyle = style;
  712. }
  713. }
  714. };