CanvasRenderer.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. THREE.CanvasRenderer = function () {
  5. var _renderList = null,
  6. _projector = new THREE.Projector(),
  7. _canvas = document.createElement( 'canvas' ),
  8. _canvasWidth, _canvasHeight, _canvasWidthHalf, _canvasHeightHalf,
  9. _context = _canvas.getContext( '2d' ),
  10. _contextGlobalAlpha = 1,
  11. _contextGlobalCompositeOperation = 0,
  12. _contextStrokeStyle = null,
  13. _contextFillStyle = null,
  14. _contextLineWidth = 1,
  15. _v1, _v2, _v3, _v4,
  16. _v5 = new THREE.Vertex(), _v6 = new THREE.Vertex(), // Needed for latter splitting quads to tris
  17. _v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
  18. _v4x, _v4y, _v5x, _v5y, _v6x, _v6y,
  19. _color1, _color2, _color3, _color4,
  20. _2near, _farPlusNear, _farMinusNear,
  21. _bitmap, _bitmapWidth, _bitmapHeight,
  22. _clipRect = new THREE.Rectangle(),
  23. _clearRect = new THREE.Rectangle(),
  24. _bboxRect = new THREE.Rectangle(),
  25. _enableLighting = false,
  26. _color = new THREE.Color( 0xffffff ),
  27. _light = new THREE.Color( 0xffffff ),
  28. _ambientLight = new THREE.Color( 0x000000 ),
  29. _directionalLights = new THREE.Color( 0x000000 ),
  30. _pointLights = new THREE.Color( 0x000000 ),
  31. _pi2 = Math.PI * 2,
  32. _vector3 = new THREE.Vector3(), // Needed for PointLight
  33. _uv1 = new THREE.UV(), _uv2 = new THREE.UV(), _uv3 = new THREE.UV(), _uv4 = new THREE.UV(),
  34. _pixelMap, _pixelMapContext, _pixelMapImage, _pixelMapData,
  35. _gradientMap, _gradientMapContext, _gradientMapQuality = 16;
  36. _pixelMap = document.createElement( 'canvas' );
  37. _pixelMap.width = _pixelMap.height = 2;
  38. _pixelMapContext = _pixelMap.getContext( '2d' );
  39. _pixelMapContext.fillStyle = 'rgba(0,0,0,1)';
  40. _pixelMapContext.fillRect( 0, 0, 2, 2 );
  41. _pixelMapImage = _pixelMapContext.getImageData( 0, 0, 2, 2 );
  42. _pixelMapData = _pixelMapImage.data;
  43. _gradientMap = document.createElement( 'canvas' );
  44. _gradientMap.width = _gradientMap.height = _gradientMapQuality;
  45. _gradientMapContext = _gradientMap.getContext( '2d' );
  46. _gradientMapContext.translate( - _gradientMapQuality / 2, - _gradientMapQuality / 2 );
  47. _gradientMapContext.scale( _gradientMapQuality, _gradientMapQuality );
  48. _gradientMapQuality --; // Fix UVs
  49. this.domElement = _canvas;
  50. this.autoClear = true;
  51. this.setSize = function ( width, height ) {
  52. _canvasWidth = width;
  53. _canvasHeight = height;
  54. _canvasWidthHalf = _canvasWidth / 2;
  55. _canvasHeightHalf = _canvasHeight / 2;
  56. _canvas.width = _canvasWidth;
  57. _canvas.height = _canvasHeight;
  58. _context.lineJoin = 'round';
  59. _context.lineCap = 'round';
  60. _clipRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  61. };
  62. this.clear = function () {
  63. if ( !_clearRect.isEmpty() ) {
  64. _clearRect.inflate( 1 );
  65. _clearRect.minSelf( _clipRect );
  66. _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf );
  67. _context.clearRect( _clearRect.getX(), _clearRect.getY(), _clearRect.getWidth(), _clearRect.getHeight() );
  68. _clearRect.empty();
  69. }
  70. };
  71. this.render = function ( scene, camera ) {
  72. var e, el, element, m, ml, fm, fml, material;
  73. if ( this.autoClear ) {
  74. this.clear();
  75. }
  76. _renderList = _projector.projectScene( scene, camera );
  77. _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf );
  78. /* DEBUG
  79. _context.fillStyle = 'rgba(0, 255, 255, 0.5)';
  80. _context.fillRect( _clipRect.getX(), _clipRect.getY(), _clipRect.getWidth(), _clipRect.getHeight() );
  81. */
  82. if ( _enableLighting = scene.lights.length > 0 ) {
  83. calculateLights( scene );
  84. }
  85. for ( e = 0, el = _renderList.length; e < el; e++ ) {
  86. element = _renderList[ e ];
  87. _bboxRect.empty();
  88. if ( element instanceof THREE.RenderableParticle ) {
  89. _v1 = element;
  90. _v1.x *= _canvasWidthHalf; _v1.y *= _canvasHeightHalf;
  91. for ( m = 0, ml = element.material.length; m < ml; m++ ) {
  92. material = element.material[ m ];
  93. material && renderParticle( _v1, element, material, scene );
  94. }
  95. } else if ( element instanceof THREE.RenderableLine ) {
  96. _v1 = element.v1; _v2 = element.v2;
  97. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  98. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  99. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  100. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  101. if ( !_clipRect.instersects( _bboxRect ) ) {
  102. continue;
  103. }
  104. m = 0; ml = element.material.length;
  105. while ( m < ml ) {
  106. material = element.material[ m ++ ];
  107. material && renderLine( _v1, _v2, element, material, scene );
  108. }
  109. } else if ( element instanceof THREE.RenderableFace3 ) {
  110. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  111. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  112. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  113. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  114. if ( element.overdraw ) {
  115. expand( _v1.positionScreen, _v2.positionScreen );
  116. expand( _v2.positionScreen, _v3.positionScreen );
  117. expand( _v3.positionScreen, _v1.positionScreen );
  118. }
  119. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  120. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  121. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  122. if ( !_clipRect.instersects( _bboxRect ) ) {
  123. continue;
  124. }
  125. m = 0; ml = element.meshMaterial.length;
  126. while ( m < ml ) {
  127. material = element.meshMaterial[ m ++ ];
  128. if ( material instanceof THREE.MeshFaceMaterial ) {
  129. fm = 0; fml = element.faceMaterial.length;
  130. while ( fm < fml ) {
  131. material = element.faceMaterial[ fm ++ ];
  132. material && renderFace3( _v1, _v2, _v3, element, material, scene );
  133. }
  134. continue;
  135. }
  136. material && renderFace3( _v1, _v2, _v3, element, material, scene );
  137. }
  138. } else if ( element instanceof THREE.RenderableFace4 ) {
  139. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3; _v4 = element.v4;
  140. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  141. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  142. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  143. _v4.positionScreen.x *= _canvasWidthHalf; _v4.positionScreen.y *= _canvasHeightHalf;
  144. _v5.positionScreen.copy( _v2.positionScreen );
  145. _v6.positionScreen.copy( _v4.positionScreen );
  146. if ( element.overdraw ) {
  147. expand( _v1.positionScreen, _v2.positionScreen );
  148. expand( _v2.positionScreen, _v4.positionScreen );
  149. expand( _v4.positionScreen, _v1.positionScreen );
  150. }
  151. if ( element.overdraw ) {
  152. expand( _v3.positionScreen, _v5.positionScreen );
  153. expand( _v3.positionScreen, _v6.positionScreen );
  154. }
  155. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  156. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  157. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  158. _bboxRect.addPoint( _v4.positionScreen.x, _v4.positionScreen.y );
  159. if ( !_clipRect.instersects( _bboxRect ) ) {
  160. continue;
  161. }
  162. m = 0; ml = element.meshMaterial.length;
  163. while ( m < ml ) {
  164. material = element.meshMaterial[ m ++ ];
  165. if ( material instanceof THREE.MeshFaceMaterial ) {
  166. fm = 0; fml = element.faceMaterial.length;
  167. while ( fm < fml ) {
  168. material = element.faceMaterial[ fm ++ ];
  169. material && renderFace4( _v1, _v2, _v3, _v4, _v5, _v6, element, material, scene );
  170. }
  171. continue;
  172. }
  173. material && renderFace4( _v1, _v2, _v3, _v4, _v5, _v6, element, material, scene );
  174. }
  175. }
  176. /*
  177. _context.lineWidth = 1;
  178. _context.strokeStyle = 'rgba( 0, 255, 0, 0.5 )';
  179. _context.strokeRect( _bboxRect.getX(), _bboxRect.getY(), _bboxRect.getWidth(), _bboxRect.getHeight() );
  180. */
  181. _clearRect.addRectangle( _bboxRect );
  182. }
  183. /* DEBUG
  184. _context.lineWidth = 1;
  185. _context.strokeStyle = 'rgba( 255, 0, 0, 0.5 )';
  186. _context.strokeRect( _clearRect.getX(), _clearRect.getY(), _clearRect.getWidth(), _clearRect.getHeight() );
  187. */
  188. _context.setTransform( 1, 0, 0, 1, 0, 0 );
  189. };
  190. function calculateLights( scene ) {
  191. var l, ll, light, lightColor,
  192. lights = scene.lights;
  193. _ambientLight.setRGB( 0, 0, 0 );
  194. _directionalLights.setRGB( 0, 0, 0 );
  195. _pointLights.setRGB( 0, 0, 0 );
  196. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  197. light = lights[ l ];
  198. lightColor = light.color;
  199. if ( light instanceof THREE.AmbientLight ) {
  200. _ambientLight.r += lightColor.r;
  201. _ambientLight.g += lightColor.g;
  202. _ambientLight.b += lightColor.b;
  203. } else if ( light instanceof THREE.DirectionalLight ) {
  204. _directionalLights.r += lightColor.r;
  205. _directionalLights.g += lightColor.g;
  206. _directionalLights.b += lightColor.b;
  207. } else if ( light instanceof THREE.PointLight ) {
  208. _pointLights.r += lightColor.r;
  209. _pointLights.g += lightColor.g;
  210. _pointLights.b += lightColor.b;
  211. }
  212. }
  213. }
  214. function calculateFaceLight( scene, element, color ) {
  215. var l, ll, light, lightColor, amount
  216. lights = scene.lights;
  217. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  218. light = lights[ l ];
  219. lightColor = light.color;
  220. if ( light instanceof THREE.DirectionalLight ) {
  221. amount = element.normalWorld.dot( light.position ) * light.intensity;
  222. if ( amount > 0 ) {
  223. color.r += lightColor.r * amount;
  224. color.g += lightColor.g * amount;
  225. color.b += lightColor.b * amount;
  226. }
  227. } else if ( light instanceof THREE.PointLight ) {
  228. _vector3.sub( light.position, element.centroidWorld );
  229. _vector3.normalize();
  230. amount = element.normalWorld.dot( _vector3 ) * light.intensity;
  231. if ( amount > 0 ) {
  232. color.r += lightColor.r * amount;
  233. color.g += lightColor.g * amount;
  234. color.b += lightColor.b * amount;
  235. }
  236. }
  237. }
  238. }
  239. function renderParticle ( v1, element, material, scene ) {
  240. if ( material.opacity == 0 ) return;
  241. setOpacity( material.opacity );
  242. setBlending( material.blending );
  243. var width, height, scaleX, scaleY, offsetX, offsetY,
  244. bitmap, bitmapWidth, bitmapHeight;
  245. if ( material instanceof THREE.ParticleBasicMaterial ) {
  246. bitmap = material.bitmap;
  247. bitmapWidth = bitmap.width / 2;
  248. bitmapHeight = bitmap.height / 2;
  249. scaleX = element.scale.x * _canvasWidthHalf;
  250. scaleY = element.scale.y * _canvasHeightHalf;
  251. width = scaleX * bitmapWidth;
  252. height = scaleY * bitmapHeight;
  253. offsetX = material.offset.x * scaleX;
  254. offsetY = material.offset.y * scaleY;
  255. // TODO: Rotations break this...
  256. _bboxRect.set( v1.x + offsetX - width, v1.y + offsetY - height, v1.x + offsetX + width, v1.y + offsetY + height );
  257. if ( !_clipRect.instersects( _bboxRect ) ) {
  258. return;
  259. }
  260. _context.save();
  261. _context.translate( v1.x, v1.y );
  262. _context.rotate( - element.rotation );
  263. _context.scale( scaleX, - scaleY );
  264. _context.translate( - bitmapWidth + material.offset.x, - bitmapHeight - material.offset.y );
  265. _context.drawImage( bitmap, 0, 0 );
  266. _context.restore();
  267. /* DEBUG
  268. _context.beginPath();
  269. _context.moveTo( v1.x - 10, v1.y );
  270. _context.lineTo( v1.x + 10, v1.y );
  271. _context.moveTo( v1.x, v1.y - 10 );
  272. _context.lineTo( v1.x, v1.y + 10 );
  273. _context.closePath();
  274. _context.strokeStyle = 'rgb(255,255,0)';
  275. _context.stroke();
  276. */
  277. } else if ( material instanceof THREE.ParticleCircleMaterial ) {
  278. if ( _enableLighting ) {
  279. _light.r = _ambientLight.r + _directionalLights.r + _pointLights.r;
  280. _light.g = _ambientLight.g + _directionalLights.g + _pointLights.g;
  281. _light.b = _ambientLight.b + _directionalLights.b + _pointLights.b;
  282. _color.r = material.color.r * _light.r;
  283. _color.g = material.color.g * _light.g;
  284. _color.b = material.color.b * _light.b;
  285. _color.updateStyleString();
  286. } else {
  287. _color.__styleString = material.color.__styleString;
  288. }
  289. width = element.scale.x * _canvasWidthHalf;
  290. height = element.scale.y * _canvasHeightHalf;
  291. _bboxRect.set( v1.x - width, v1.y - height, v1.x + width, v1.y + height );
  292. if ( !_clipRect.instersects( _bboxRect ) ) {
  293. return;
  294. }
  295. setFillStyle( _color.__styleString );
  296. _context.save();
  297. _context.translate( v1.x, v1.y );
  298. _context.rotate( - element.rotation );
  299. _context.scale( width, height );
  300. _context.beginPath();
  301. _context.arc( 0, 0, 1, 0, _pi2, true );
  302. _context.closePath();
  303. _context.fill();
  304. _context.restore();
  305. }
  306. }
  307. function renderLine( v1, v2, element, material, scene ) {
  308. if ( material.opacity == 0 ) return;
  309. setOpacity( material.opacity );
  310. setBlending( material.blending );
  311. _context.beginPath();
  312. _context.moveTo( v1.positionScreen.x, v1.positionScreen.y );
  313. _context.lineTo( v2.positionScreen.x, v2.positionScreen.y );
  314. _context.closePath();
  315. if ( material instanceof THREE.LineBasicMaterial ) {
  316. _color.__styleString = material.color.__styleString;
  317. setLineWidth( material.linewidth );
  318. setStrokeStyle( _color.__styleString );
  319. _context.stroke();
  320. _bboxRect.inflate( material.linewidth * 2 );
  321. }
  322. }
  323. function renderFace3( v1, v2, v3, element, material, scene ) {
  324. if ( material.opacity == 0 ) return;
  325. setOpacity( material.opacity );
  326. setBlending( material.blending );
  327. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  328. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  329. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  330. if ( material.map ) {
  331. _bitmap = material.map.image;
  332. _bitmapWidth = _bitmap.width - 1;
  333. _bitmapHeight = _bitmap.height - 1;
  334. _uv1.u = element.uvs[ 0 ].u * _bitmapWidth; _uv1.v = element.uvs[ 0 ].v * _bitmapHeight;
  335. _uv2.u = element.uvs[ 1 ].u * _bitmapWidth; _uv2.v = element.uvs[ 1 ].v * _bitmapHeight;
  336. _uv3.u = element.uvs[ 2 ].u * _bitmapWidth; _uv3.v = element.uvs[ 2 ].v * _bitmapHeight;
  337. drawTexturedTriangle( _bitmap, _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1.u, _uv1.v, _uv2.u, _uv2.v, _uv3.u, _uv3.v );
  338. return;
  339. }
  340. if ( material instanceof THREE.MeshBasicMaterial ) {
  341. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, material.color, material.wireframe, material.wireframe_linewidth );
  342. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  343. if ( _enableLighting ) {
  344. _light.r = _ambientLight.r;
  345. _light.g = _ambientLight.g;
  346. _light.b = _ambientLight.b;
  347. calculateFaceLight( scene, element, _light );
  348. _color.r = material.color.r * _light.r;
  349. _color.g = material.color.g * _light.g;
  350. _color.b = material.color.b * _light.b;
  351. _color.updateStyleString();
  352. } else {
  353. _color.__styleString = material.color.__styleString;
  354. }
  355. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _color, material.wireframe, material.wireframe_linewidth );
  356. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  357. /*
  358. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear ) );
  359. _color.setRGB( _w, _w, _w );
  360. */
  361. _2near = material.__2near;
  362. _farPlusNear = material.__farPlusNear;
  363. _farMinusNear = material.__farMinusNear;
  364. _color1 = ~~ ( ( 1 - ( _2near / ( _farPlusNear - v1.positionScreen.z * _farMinusNear ) ) ) * 255 );
  365. _color2 = ~~ ( ( 1 - ( _2near / ( _farPlusNear - v2.positionScreen.z * _farMinusNear ) ) ) * 255 );
  366. _color3 = ~~ ( ( 1 - ( _2near / ( _farPlusNear - v3.positionScreen.z * _farMinusNear ) ) ) * 255 );
  367. // _color4 = ~~ ( ( _color2 + _color3 ) * 0.5 );
  368. _bitmap = getGradientTexture( [ _color1, _color1, _color1 ], [ _color2, _color2, _color2 ], [ _color3, _color3, _color3 ], [ _color3, _color3, _color3 ] );
  369. _uv1.u = 0; _uv1.v = 0;
  370. _uv2.u = _gradientMapQuality; _uv2.v = 0;
  371. _uv3.u = 0; _uv3.v = _gradientMapQuality;
  372. drawTexturedTriangle( _bitmap, _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1.u, _uv1.v, _uv2.u, _uv2.v, _uv3.u, _uv3.v );
  373. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  374. _color.r = normalToComponent( element.normalWorld.x );
  375. _color.g = normalToComponent( element.normalWorld.y );
  376. _color.b = normalToComponent( element.normalWorld.z );
  377. _color.updateStyleString();
  378. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _color, material.wireframe, material.wireframe_linewidth );
  379. }
  380. }
  381. function renderFace4( v1, v2, v3, v4, v5, v6, element, material, scene ) {
  382. if ( material.opacity == 0 ) return;
  383. setOpacity( material.opacity );
  384. setBlending( material.blending );
  385. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  386. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  387. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  388. _v4x = v4.positionScreen.x; _v4y = v4.positionScreen.y;
  389. _v5x = v5.positionScreen.x; _v5y = v5.positionScreen.y;
  390. _v6x = v6.positionScreen.x; _v6y = v6.positionScreen.y;
  391. if ( material.map ) {
  392. _bitmap = material.map.image;
  393. _bitmapWidth = _bitmap.width - 1;
  394. _bitmapHeight = _bitmap.height - 1;
  395. _uv1.copy( element.uvs[ 0 ] );
  396. _uv2.copy( element.uvs[ 1 ] );
  397. _uv3.copy( element.uvs[ 2 ] );
  398. _uv4.copy( element.uvs[ 3 ] );
  399. _uv1.u *= _bitmapWidth; _uv1.v *= _bitmapHeight;
  400. _uv2.u *= _bitmapWidth; _uv2.v *= _bitmapHeight;
  401. _uv3.u *= _bitmapWidth; _uv3.v *= _bitmapHeight;
  402. _uv4.u *= _bitmapWidth; _uv4.v *= _bitmapHeight;
  403. drawTexturedTriangle( _bitmap, _v1x, _v1y, _v2x, _v2y, _v4x, _v4y, _uv1.u, _uv1.v, _uv2.u, _uv2.v, _uv4.u, _uv4.v );
  404. drawTexturedTriangle( _bitmap, _v5x, _v5y, _v3x, _v3y, _v6x, _v6y, _uv2.u, _uv2.v, _uv3.u, _uv3.v, _uv4.u, _uv4.v );
  405. return;
  406. }
  407. if ( material instanceof THREE.MeshBasicMaterial ) {
  408. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y, material.color, material.wireframe, material.wireframe_linewidth );
  409. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  410. if ( _enableLighting ) {
  411. _light.r = _ambientLight.r;
  412. _light.g = _ambientLight.g;
  413. _light.b = _ambientLight.b;
  414. calculateFaceLight( scene, element, _light );
  415. _color.r = material.color.r * _light.r;
  416. _color.g = material.color.g * _light.g;
  417. _color.b = material.color.b * _light.b;
  418. _color.updateStyleString();
  419. } else {
  420. _color.__styleString = material.color.__styleString;
  421. }
  422. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y, _color, material.wireframe, material.wireframe_linewidth );
  423. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  424. /*
  425. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear ) );
  426. _color.setRGB( _w, _w, _w );
  427. */
  428. _2near = material.__2near;
  429. _farPlusNear = material.__farPlusNear;
  430. _farMinusNear = material.__farMinusNear;
  431. _color1 = ~~ ( ( 1 - ( _2near / ( _farPlusNear - v1.positionScreen.z * _farMinusNear ) ) ) * 255 );
  432. _color2 = ~~ ( ( 1 - ( _2near / ( _farPlusNear - v2.positionScreen.z * _farMinusNear ) ) ) * 255 );
  433. _color3 = ~~ ( ( 1 - ( _2near / ( _farPlusNear - v3.positionScreen.z * _farMinusNear ) ) ) * 255 );
  434. _color4 = ~~ ( ( 1 - ( _2near / ( _farPlusNear - v4.positionScreen.z * _farMinusNear ) ) ) * 255 );
  435. _bitmap = getGradientTexture( [ _color1, _color1, _color1 ], [ _color2, _color2, _color2 ], [ _color4, _color4, _color4 ], [ _color3, _color3, _color3 ] );
  436. _uv1.u = 0; _uv1.v = 0;
  437. _uv2.u = _gradientMapQuality; _uv2.v = 0;
  438. _uv3.u = _gradientMapQuality; _uv3.v = _gradientMapQuality;
  439. _uv4.u = 0; _uv4.v = _gradientMapQuality;
  440. drawTexturedTriangle( _bitmap, _v1x, _v1y, _v2x, _v2y, _v4x, _v4y, _uv1.u, _uv1.v, _uv2.u, _uv2.v, _uv4.u, _uv4.v );
  441. drawTexturedTriangle( _bitmap, _v5x, _v5y, _v3x, _v3y, _v6x, _v6y, _uv2.u, _uv2.v, _uv3.u, _uv3.v, _uv4.u, _uv4.v );
  442. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  443. _color.r = normalToComponent( element.normalWorld.x );
  444. _color.g = normalToComponent( element.normalWorld.y );
  445. _color.b = normalToComponent( element.normalWorld.z );
  446. _color.updateStyleString();
  447. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y, _color, material.wireframe, material.wireframe_linewidth );
  448. }
  449. }
  450. function drawTriangle( x0, y0, x1, y1, x2, y2, color, wireframe, wireframe_linewidth ) {
  451. _context.beginPath();
  452. _context.moveTo( x0, y0 );
  453. _context.lineTo( x1, y1 );
  454. _context.lineTo( x2, y2 );
  455. _context.lineTo( x0, y0 );
  456. _context.closePath();
  457. if ( wireframe ) {
  458. setLineWidth( wireframe_linewidth );
  459. setStrokeStyle( color.__styleString );
  460. _context.stroke();
  461. _bboxRect.inflate( wireframe_linewidth * 2 );
  462. } else {
  463. setFillStyle( color.__styleString );
  464. _context.fill();
  465. }
  466. }
  467. function drawQuad( x0, y0, x1, y1, x2, y2, x3, y3, color, wireframe, wireframe_linewidth ) {
  468. _context.beginPath();
  469. _context.moveTo( x0, y0 );
  470. _context.lineTo( x1, y1 );
  471. _context.lineTo( x2, y2 );
  472. _context.lineTo( x3, y3 );
  473. _context.lineTo( x0, y0 );
  474. _context.closePath();
  475. if ( wireframe ) {
  476. setLineWidth( wireframe_linewidth );
  477. setStrokeStyle( color.__styleString );
  478. _context.stroke();
  479. _bboxRect.inflate( wireframe_linewidth * 2 );
  480. } else {
  481. setFillStyle( color.__styleString );
  482. _context.fill();
  483. }
  484. }
  485. function drawTexturedTriangle( bitmap, x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2 ) {
  486. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  487. _context.beginPath();
  488. _context.moveTo( x0, y0 );
  489. _context.lineTo( x1, y1 );
  490. _context.lineTo( x2, y2 );
  491. _context.closePath();
  492. x1 -= x0; y1 -= y0;
  493. x2 -= x0; y2 -= y0;
  494. u1 -= u0; v1 -= v0;
  495. u2 -= u0; v2 -= v0;
  496. var det = 1 / ( u1 * v2 - u2 * v1 ),
  497. a = ( v2 * x1 - v1 * x2 ) * det,
  498. b = ( v2 * y1 - v1 * y2 ) * det,
  499. c = ( u1 * x2 - u2 * x1 ) * det,
  500. d = ( u1 * y2 - u2 * y1 ) * det,
  501. e = x0 - a * u0 - c * v0,
  502. f = y0 - b * u0 - d * v0;
  503. _context.save();
  504. _context.transform( a, b, c, d, e, f );
  505. _context.clip();
  506. _context.drawImage( bitmap, 0, 0 );
  507. _context.restore();
  508. }
  509. //
  510. function setOpacity( value ) {
  511. if ( _contextGlobalAlpha != value ) {
  512. _context.globalAlpha = _contextGlobalAlpha = value;
  513. }
  514. }
  515. function setBlending( value ) {
  516. if ( _contextGlobalCompositeOperation != value ) {
  517. switch ( value ) {
  518. case THREE.NormalBlending:
  519. _context.globalCompositeOperation = 'source-over';
  520. break;
  521. case THREE.AdditiveBlending:
  522. _context.globalCompositeOperation = 'lighter';
  523. break;
  524. case THREE.SubtractiveBlending:
  525. _context.globalCompositeOperation = 'darker';
  526. break;
  527. }
  528. _contextGlobalCompositeOperation = value;
  529. }
  530. }
  531. function setLineWidth( value ) {
  532. if ( _contextLineWidth != value ) {
  533. _context.lineWidth = _contextLineWidth = value;
  534. }
  535. }
  536. function setStrokeStyle( value ) {
  537. if ( _contextStrokeStyle != value ) {
  538. _context.strokeStyle = _contextStrokeStyle = value;
  539. }
  540. }
  541. function setFillStyle( value ) {
  542. if ( _contextFillStyle != value ) {
  543. _context.fillStyle = _contextFillStyle = value;
  544. }
  545. }
  546. function getGradientTexture( c1, c2, c3, c4 ) {
  547. // http://mrdoob.com/blog/post/710
  548. _pixelMapData[ 0 ] = c1[ 0 ];
  549. _pixelMapData[ 1 ] = c1[ 1 ];
  550. _pixelMapData[ 2 ] = c1[ 2 ];
  551. _pixelMapData[ 4 ] = c2[ 0 ];
  552. _pixelMapData[ 5 ] = c2[ 1 ];
  553. _pixelMapData[ 6 ] = c2[ 2 ];
  554. _pixelMapData[ 8 ] = c3[ 0 ];
  555. _pixelMapData[ 9 ] = c3[ 1 ];
  556. _pixelMapData[ 10 ] = c3[ 2 ];
  557. _pixelMapData[ 12 ] = c4[ 0 ];
  558. _pixelMapData[ 13 ] = c4[ 1 ];
  559. _pixelMapData[ 14 ] = c4[ 2 ];
  560. _pixelMapContext.putImageData( _pixelMapImage, 0, 0 );
  561. _gradientMapContext.drawImage( _pixelMap, 0, 0 );
  562. return _gradientMap;
  563. }
  564. function normalToComponent( normal ) {
  565. // https://gist.github.com/665829
  566. return normal < 0 ? Math.min( ( 1 + normal ) * 0.5, 0.5 ) : 0.5 + Math.min( normal * 0.5, 0.5 );
  567. }
  568. // Hide anti-alias gaps
  569. function expand( v1, v2 ) {
  570. var x = v2.x - v1.x, y = v2.y - v1.y,
  571. unit = 1 / Math.sqrt( x * x + y * y );
  572. x *= unit; y *= unit;
  573. v2.x += x; v2.y += y;
  574. v1.x -= x; v1.y -= y;
  575. }
  576. };