CanvasRenderer.js 33 KB

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