CanvasRenderer.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. _context = _canvas.getContext( '2d' ),
  9. _width, _height, _widthHalf, _heightHalf,
  10. _clipRect = new THREE.Rectangle(),
  11. _clearRect = new THREE.Rectangle(),
  12. _bboxRect = new THREE.Rectangle(),
  13. _enableLighting = false,
  14. _color = new THREE.Color( 0xffffffff ),
  15. _light = new THREE.Color( 0xffffffff ),
  16. _ambientLight = new THREE.Color( 0xffffffff ),
  17. _vector2 = new THREE.Vector2(), // Needed for expand
  18. _vector3 = new THREE.Vector3(), // Needed for PointLight
  19. v5 = new THREE.Vector2(), v6 = new THREE.Vector2(), // Needed for latter splitting tris to quads
  20. uv1 = new THREE.UV(), uv2 = new THREE.UV(), uv3 = new THREE.UV(), uv4 = new THREE.UV();
  21. this.domElement = _canvas;
  22. this.autoClear = true;
  23. this.setSize = function ( width, height ) {
  24. _width = width; _height = height;
  25. _widthHalf = _width / 2; _heightHalf = _height / 2;
  26. _canvas.width = _width;
  27. _canvas.height = _height;
  28. _clipRect.set( - _widthHalf, - _heightHalf, _widthHalf, _heightHalf );
  29. };
  30. this.clear = function () {
  31. if ( !_clearRect.isEmpty() ) {
  32. _clearRect.inflate( 1 );
  33. _clearRect.minSelf( _clipRect );
  34. _context.setTransform( 1, 0, 0, - 1, _widthHalf, _heightHalf );
  35. _context.clearRect( _clearRect.getX(), _clearRect.getY(), _clearRect.getWidth(), _clearRect.getHeight() );
  36. /*
  37. // Opera workaround
  38. _context.setTransform( 1, 0, 0, 1, _widthHalf, _heightHalf );
  39. _context.clearRect( _clearRect.getX(), - ( _clearRect.getHeight() + _clearRect.getY() ), _clearRect.getWidth(), _clearRect.getHeight() );
  40. */
  41. _clearRect.empty();
  42. }
  43. };
  44. this.render = function ( scene, camera ) {
  45. var e, el, element, m, ml, material, pi2 = Math.PI * 2,
  46. v1x, v1y, v2x, v2y, v3x, v3y, v4x, v4y, v5x, v5y, v6x, v6y,
  47. width, height, scaleX, scaleY, offsetX, offsetY,
  48. bitmap, bitmapWidth, bitmapHeight;
  49. if ( this.autoClear ) {
  50. this.clear();
  51. }
  52. _renderList = _projector.projectScene( scene, camera );
  53. _context.setTransform( 1, 0, 0, - 1, _widthHalf, _heightHalf );
  54. /* DEBUG
  55. _context.fillStyle = 'rgba(0, 255, 255, 0.5)';
  56. _context.fillRect( _clipRect.getX(), _clipRect.getY(), _clipRect.getWidth(), _clipRect.getHeight() );
  57. */
  58. _enableLighting = scene.lights.length > 0;
  59. if ( _enableLighting ) {
  60. calculateAmbientLight( scene, _ambientLight );
  61. }
  62. for ( e = 0, el = _renderList.length; e < el; e++ ) {
  63. element = _renderList[ e ];
  64. _bboxRect.empty();
  65. if ( element instanceof THREE.RenderableParticle ) {
  66. v1x = element.x * _widthHalf; v1y = element.y * _heightHalf;
  67. for ( m = 0, ml = element.material.length; m < ml; m++ ) {
  68. material = element.material[ m ];
  69. if ( material instanceof THREE.ParticleCircleMaterial ) {
  70. if ( _enableLighting ) {
  71. _light.copyRGB( _ambientLight );
  72. _color.copyRGBA( material.color );
  73. _color.multiplySelfRGB( _light );
  74. _color.updateStyleString();
  75. } else {
  76. _color = material.color;
  77. }
  78. width = element.scale.x * _widthHalf;
  79. height = element.scale.y * _heightHalf;
  80. _bboxRect.set( v1x - width, v1y - height, v1x + width, v1y + height );
  81. if ( !_clipRect.instersects( _bboxRect ) ) {
  82. continue;
  83. }
  84. _context.save();
  85. _context.translate( v1x, v1y );
  86. _context.rotate( - element.rotation );
  87. _context.scale( width, height );
  88. _context.beginPath();
  89. _context.arc( 0, 0, 1, 0, pi2, true );
  90. _context.closePath();
  91. _context.fillStyle = _color.__styleString;
  92. _context.fill();
  93. _context.restore();
  94. } else if ( material instanceof THREE.ParticleBitmapMaterial ) {
  95. bitmap = material.bitmap;
  96. bitmapWidth = bitmap.width / 2;
  97. bitmapHeight = bitmap.height / 2;
  98. scaleX = element.scale.x * _widthHalf;
  99. scaleY = element.scale.y * _heightHalf;
  100. width = scaleX * bitmapWidth;
  101. height = scaleY * bitmapHeight;
  102. offsetX = material.offset.x * scaleX;
  103. offsetY = material.offset.y * scaleY;
  104. // TODO: Rotations break this...
  105. _bboxRect.set( v1x + offsetX - width, v1y + offsetY - height, v1x + offsetX + width, v1y + offsetY + height );
  106. if ( !_clipRect.instersects( _bboxRect ) ) {
  107. continue;
  108. }
  109. _context.save();
  110. _context.translate( v1x, v1y );
  111. _context.rotate( - element.rotation );
  112. _context.scale( scaleX, - scaleY );
  113. _context.translate( - bitmapWidth + material.offset.x, - bitmapHeight - material.offset.y );
  114. _context.drawImage( bitmap, 0, 0 );
  115. _context.restore();
  116. /* DEBUG
  117. _context.beginPath();
  118. _context.moveTo( v1x - 10, v1y );
  119. _context.lineTo( v1x + 10, v1y );
  120. _context.moveTo( v1x, v1y - 10 );
  121. _context.lineTo( v1x, v1y + 10 );
  122. _context.closePath();
  123. _context.strokeStyle = 'rgb(255,255,0)';
  124. _context.stroke();
  125. */
  126. }
  127. }
  128. } else if ( element instanceof THREE.RenderableLine ) {
  129. v1x = element.v1.x * _widthHalf; v1y = element.v1.y * _heightHalf;
  130. v2x = element.v2.x * _widthHalf; v2y = element.v2.y * _heightHalf;
  131. _bboxRect.addPoint( v1x, v1y );
  132. _bboxRect.addPoint( v2x, v2y );
  133. if ( !_clipRect.instersects( _bboxRect ) ) {
  134. continue;
  135. }
  136. _context.beginPath();
  137. _context.moveTo( v1x, v1y );
  138. _context.lineTo( v2x, v2y );
  139. _context.closePath();
  140. for ( m = 0, ml = element.material.length; m < ml; m++ ) {
  141. material = element.material[ m ];
  142. if ( material instanceof THREE.LineColorMaterial ) {
  143. if ( _enableLighting ) {
  144. _light.copyRGB( _ambientLight );
  145. _color.copyRGBA( material.color );
  146. _color.multiplySelfRGB( _light );
  147. _color.updateStyleString();
  148. } else {
  149. _color = material.color;
  150. }
  151. _context.lineWidth = material.lineWidth;
  152. _context.lineJoin = "round";
  153. _context.lineCap = "round";
  154. _context.strokeStyle = _color.__styleString;
  155. _context.stroke();
  156. _bboxRect.inflate( _context.lineWidth );
  157. }
  158. }
  159. } else if ( element instanceof THREE.RenderableFace3 ) {
  160. element.v1.x *= _widthHalf; element.v1.y *= _heightHalf;
  161. element.v2.x *= _widthHalf; element.v2.y *= _heightHalf;
  162. element.v3.x *= _widthHalf; element.v3.y *= _heightHalf;
  163. if ( element.overdraw ) {
  164. expand( element.v1, element.v2 );
  165. expand( element.v2, element.v3 );
  166. expand( element.v3, element.v1 );
  167. }
  168. v1x = element.v1.x; v1y = element.v1.y;
  169. v2x = element.v2.x; v2y = element.v2.y;
  170. v3x = element.v3.x; v3y = element.v3.y;
  171. _bboxRect.addPoint( v1x, v1y );
  172. _bboxRect.addPoint( v2x, v2y );
  173. _bboxRect.addPoint( v3x, v3y );
  174. if ( !_clipRect.instersects( _bboxRect ) ) {
  175. continue;
  176. }
  177. for ( m = 0, ml = element.material.length; m < ml; m++ ) {
  178. material = element.material[ m ];
  179. if ( material instanceof THREE.MeshColorFillMaterial ) {
  180. if ( _enableLighting ) {
  181. _light.copyRGB( _ambientLight );
  182. addLights( scene, element, _light );
  183. _color.copyRGBA( material.color );
  184. _color.multiplySelfRGB( _light );
  185. _color.updateStyleString();
  186. } else {
  187. _color = material.color;
  188. }
  189. _context.beginPath();
  190. _context.moveTo( v1x, v1y );
  191. _context.lineTo( v2x, v2y );
  192. _context.lineTo( v3x, v3y );
  193. _context.lineTo( v1x, v1y );
  194. _context.closePath();
  195. _context.fillStyle = _color.__styleString;
  196. _context.fill();
  197. } else if ( material instanceof THREE.MeshColorStrokeMaterial ) {
  198. if ( _enableLighting ) {
  199. _light.copyRGB( _ambientLight );
  200. addLights( scene, element, _light );
  201. _color.copyRGBA( material.color );
  202. _color.multiplySelfRGB( _light );
  203. _color.updateStyleString();
  204. } else {
  205. _color = material.color;
  206. }
  207. _context.beginPath();
  208. _context.moveTo( v1x, v1y );
  209. _context.lineTo( v2x, v2y );
  210. _context.lineTo( v3x, v3y );
  211. _context.lineTo( v1x, v1y );
  212. _context.closePath();
  213. _context.lineWidth = material.lineWidth;
  214. _context.lineJoin = "round";
  215. _context.lineCap = "round";
  216. _context.strokeStyle = _color.__styleString;
  217. _context.stroke();
  218. _bboxRect.inflate( _context.lineWidth );
  219. } else if ( material instanceof THREE.MeshFaceColorFillMaterial ) {
  220. if ( _enableLighting ) {
  221. _light.copyRGB( _ambientLight );
  222. addLights( scene, element, _light );
  223. _color.copyRGBA( element.color );
  224. _color.multiplySelfRGB( _light );
  225. _color.updateStyleString();
  226. } else {
  227. _color = element.color;
  228. }
  229. _context.beginPath();
  230. _context.moveTo( v1x, v1y );
  231. _context.lineTo( v2x, v2y );
  232. _context.lineTo( v3x, v3y );
  233. _context.lineTo( v1x, v1y );
  234. _context.closePath();
  235. _context.fillStyle = _color.__styleString;
  236. _context.fill();
  237. } else if ( material instanceof THREE.MeshFaceColorStrokeMaterial ) {
  238. if ( _enableLighting ) {
  239. _light.copyRGB( _ambientLight );
  240. addLights( scene, element, _light );
  241. _color.copyRGBA( element.color );
  242. _color.multiplySelfRGB( _light );
  243. _color.updateStyleString();
  244. } else {
  245. _color = element.color;
  246. }
  247. _context.beginPath();
  248. _context.moveTo( v1x, v1y );
  249. _context.lineTo( v2x, v2y );
  250. _context.lineTo( v3x, v3y );
  251. _context.lineTo( v1x, v1y );
  252. _context.closePath();
  253. _context.lineWidth = material.lineWidth;
  254. _context.lineJoin = "round";
  255. _context.lineCap = "round";
  256. _context.strokeStyle = _color.__styleString;
  257. _context.stroke();
  258. _bboxRect.inflate( _context.lineWidth );
  259. } else if ( material instanceof THREE.MeshBitmapUVMappingMaterial &&
  260. ( m == element.materialIndex || element.materialIndex == material.decalIndex ) ) {
  261. bitmap = material.bitmap;
  262. bitmapWidth = bitmap.width - 1;
  263. bitmapHeight = bitmap.height - 1;
  264. /* DEBUG
  265. if ( !element.uvs[ 0 ] || !element.uvs[ 1 ] || !element.uvs[ 2 ]) {
  266. _context.beginPath();
  267. _context.moveTo( v1x, v1y );
  268. _context.lineTo( v2x, v2y );
  269. _context.lineTo( v3x, v3y );
  270. _context.lineTo( v1x, v1y );
  271. _context.closePath();
  272. _context.fillStyle = 'rgb(0, 255, 0)';
  273. _context.fill();
  274. continue;
  275. }
  276. */
  277. uv1.copy( element.uvs[ 0 ] );
  278. uv2.copy( element.uvs[ 1 ] );
  279. uv3.copy( element.uvs[ 2 ] );
  280. uv1.u *= bitmapWidth; uv1.v *= bitmapHeight;
  281. uv2.u *= bitmapWidth; uv2.v *= bitmapHeight;
  282. uv3.u *= bitmapWidth; uv3.v *= bitmapHeight;
  283. drawTexturedTriangle( bitmap, v1x, v1y, v2x, v2y, v3x, v3y, uv1.u, uv1.v, uv2.u, uv2.v, uv3.u, uv3.v );
  284. }
  285. }
  286. } else if ( element instanceof THREE.RenderableFace4 ) {
  287. element.v1.x *= _widthHalf; element.v1.y *= _heightHalf;
  288. element.v2.x *= _widthHalf; element.v2.y *= _heightHalf;
  289. element.v3.x *= _widthHalf; element.v3.y *= _heightHalf;
  290. element.v4.x *= _widthHalf; element.v4.y *= _heightHalf;
  291. v5.copy( element.v2 ); v6.copy( element.v4 );
  292. if ( element.overdraw ) {
  293. expand( element.v1, element.v2 );
  294. expand( element.v2, element.v4 );
  295. expand( element.v4, element.v1 );
  296. }
  297. v1x = element.v1.x; v1y = element.v1.y;
  298. v2x = element.v2.x; v2y = element.v2.y;
  299. v4x = element.v4.x; v4y = element.v4.y;
  300. if ( element.overdraw ) {
  301. expand( element.v3, v5 );
  302. expand( element.v3, v6 );
  303. }
  304. v3x = element.v3.x; v3y = element.v3.y;
  305. v5x = v5.x; v5y = v5.y;
  306. v6x = v6.x; v6y = v6.y;
  307. _bboxRect.addPoint( v1x, v1y );
  308. _bboxRect.addPoint( v2x, v2y );
  309. _bboxRect.addPoint( v3x, v3y );
  310. _bboxRect.addPoint( v4x, v4y );
  311. if ( !_clipRect.instersects( _bboxRect ) ) {
  312. continue;
  313. }
  314. for ( m = 0, ml = element.material.length; m < ml; m++ ) {
  315. material = element.material[ m ];
  316. if ( material instanceof THREE.MeshColorFillMaterial ) {
  317. if ( _enableLighting ) {
  318. _light.copyRGB( _ambientLight );
  319. addLights( scene, element, _light );
  320. _color.copyRGBA( material.color );
  321. _color.multiplySelfRGB( _light );
  322. _color.updateStyleString();
  323. } else {
  324. _color = material.color;
  325. }
  326. _context.beginPath();
  327. _context.moveTo( v1x, v1y );
  328. _context.lineTo( v2x, v2y );
  329. _context.lineTo( v3x, v3y );
  330. _context.lineTo( v4x, v4y );
  331. _context.lineTo( v1x, v1y );
  332. _context.closePath();
  333. _context.fillStyle = _color.__styleString;
  334. _context.fill();
  335. } else if ( material instanceof THREE.MeshColorStrokeMaterial ) {
  336. if ( _enableLighting ) {
  337. _light.copyRGB( _ambientLight );
  338. addLights( scene, element, _light );
  339. _color.copyRGBA( material.color );
  340. _color.multiplySelfRGB( _light );
  341. _color.updateStyleString();
  342. } else {
  343. _color = material.color;
  344. }
  345. _context.beginPath();
  346. _context.moveTo( v1x, v1y );
  347. _context.lineTo( v2x, v2y );
  348. _context.lineTo( v3x, v3y );
  349. _context.lineTo( v4x, v4y );
  350. _context.lineTo( v1x, v1y );
  351. _context.closePath();
  352. _context.lineWidth = material.lineWidth;
  353. _context.lineJoin = "round";
  354. _context.lineCap = "round";
  355. _context.strokeStyle = _color.__styleString;
  356. _context.stroke();
  357. _bboxRect.inflate( _context.lineWidth );
  358. } else if ( material instanceof THREE.MeshFaceColorFillMaterial ) {
  359. if ( _enableLighting ) {
  360. _light.copyRGB( _ambientLight );
  361. addLights( scene, element, _light );
  362. _color.copyRGBA( element.color );
  363. _color.multiplySelfRGB( _light );
  364. _color.updateStyleString();
  365. } else {
  366. _color = element.color;
  367. }
  368. _context.beginPath();
  369. _context.moveTo( v1x, v1y );
  370. _context.lineTo( v2x, v2y );
  371. _context.lineTo( v3x, v3y );
  372. _context.lineTo( v4x, v4y );
  373. _context.lineTo( v1x, v1y );
  374. _context.closePath();
  375. _context.fillStyle = _color.__styleString;
  376. _context.fill();
  377. } else if ( material instanceof THREE.MeshFaceColorStrokeMaterial ) {
  378. if ( _enableLighting ) {
  379. _light.copyRGB( _ambientLight );
  380. addLights( scene, element, _light );
  381. _color.copyRGBA( element.color );
  382. _color.multiplySelfRGB( _light );
  383. _color.updateStyleString();
  384. } else {
  385. _color = element.color;
  386. }
  387. _context.beginPath();
  388. _context.moveTo( v1x, v1y );
  389. _context.lineTo( v2x, v2y );
  390. _context.lineTo( v3x, v3y );
  391. _context.lineTo( v4x, v4y );
  392. _context.lineTo( v1x, v1y );
  393. _context.closePath();
  394. _context.lineWidth = material.lineWidth;
  395. _context.lineJoin = "round";
  396. _context.lineCap = "round";
  397. _context.strokeStyle = _color.__styleString;
  398. _context.stroke();
  399. _bboxRect.inflate( _context.lineWidth );
  400. } else if ( material instanceof THREE.MeshBitmapUVMappingMaterial &&
  401. ( m == element.materialIndex || element.materialIndex == material.decalIndex ) ) {
  402. bitmap = material.bitmap;
  403. bitmapWidth = bitmap.width - 1;
  404. bitmapHeight = bitmap.height - 1;
  405. /* DEBUG
  406. if ( !element.uvs[ 0 ] || !element.uvs[ 1 ] || !element.uvs[ 2 ] || !element.uvs[ 3 ]) {
  407. _context.beginPath();
  408. _context.moveTo( v1x, v1y );
  409. _context.lineTo( v2x, v2y );
  410. _context.lineTo( v3x, v3y );
  411. _context.lineTo( v4x, v4y );
  412. _context.lineTo( v1x, v1y );
  413. _context.closePath();
  414. _context.fillStyle = 'rgb(255, 0, 255)';
  415. _context.fill();
  416. continue;
  417. }
  418. */
  419. uv1.copy( element.uvs[ 0 ] );
  420. uv2.copy( element.uvs[ 1 ] );
  421. uv3.copy( element.uvs[ 2 ] );
  422. uv4.copy( element.uvs[ 3 ] );
  423. uv1.u *= bitmapWidth; uv1.v *= bitmapHeight;
  424. uv2.u *= bitmapWidth; uv2.v *= bitmapHeight;
  425. uv3.u *= bitmapWidth; uv3.v *= bitmapHeight;
  426. uv4.u *= bitmapWidth; uv4.v *= bitmapHeight;
  427. drawTexturedTriangle( bitmap, v1x, v1y, v2x, v2y, v4x, v4y, uv1.u, uv1.v, uv2.u, uv2.v, uv4.u, uv4.v );
  428. drawTexturedTriangle( bitmap, v5x, v5y, v3x, v3y, v6x, v6y, uv2.u, uv2.v, uv3.u, uv3.v, uv4.u, uv4.v );
  429. }
  430. }
  431. }
  432. /*
  433. _context.lineWidth = 1;
  434. _context.strokeStyle = 'rgba( 0, 255, 0, 0.5 )';
  435. _context.strokeRect( _bboxRect.getX(), _bboxRect.getY(), _bboxRect.getWidth(), _bboxRect.getHeight() );
  436. */
  437. _clearRect.addRectangle( _bboxRect );
  438. }
  439. /* DEBUG
  440. _context.lineWidth = 1;
  441. _context.strokeStyle = 'rgba( 255, 0, 0, 0.5 )';
  442. _context.strokeRect( _clearRect.getX(), _clearRect.getY(), _clearRect.getWidth(), _clearRect.getHeight() );
  443. */
  444. _context.setTransform( 1, 0, 0, 1, 0, 0 );
  445. };
  446. function calculateAmbientLight( scene, color ) {
  447. var l, ll, light;
  448. color.setRGBA( 1, 1, 1, 1 );
  449. for ( l = 0, ll = scene.lights.length; l < ll; l++ ) {
  450. light = scene.lights[ l ];
  451. if ( light instanceof THREE.AmbientLight ) {
  452. color.r *= light.color.r;
  453. color.g *= light.color.g;
  454. color.b *= light.color.b;
  455. }
  456. }
  457. }
  458. function addLights( scene, element, color ) {
  459. var l, ll, light, amount;
  460. for ( l = 0, ll = scene.lights.length; l < ll; l++ ) {
  461. light = scene.lights[ l ];
  462. if ( light instanceof THREE.DirectionalLight ) {
  463. amount = element.normalWorld.dot( light.position ) * light.intensity;
  464. if ( amount > 0 ) {
  465. color.r += light.color.r * amount;
  466. color.g += light.color.g * amount;
  467. color.b += light.color.b * amount;
  468. }
  469. } else if ( light instanceof THREE.PointLight ) {
  470. _vector3.sub( light.position, element.centroidWorld );
  471. _vector3.normalize();
  472. amount = element.normalWorld.dot( _vector3 ) * light.intensity;
  473. if ( amount > 0 ) {
  474. color.r += light.color.r * amount;
  475. color.g += light.color.g * amount;
  476. color.b += light.color.b * amount;
  477. }
  478. }
  479. }
  480. }
  481. function drawTexturedTriangle( bitmap, v1x, v1y, v2x, v2y, v3x, v3y, uv1u, uv1v, uv2u, uv2v, uv3u, uv3v ) {
  482. // Textured triangle drawing by Thatcher Ulrich.
  483. // http://tulrich.com/geekstuff/canvas/jsgl.js
  484. var denom, m11, m12, m21, m22, dx, dy;
  485. _context.beginPath();
  486. _context.moveTo( v1x, v1y );
  487. _context.lineTo( v2x, v2y );
  488. _context.lineTo( v3x, v3y );
  489. _context.lineTo( v1x, v1y );
  490. _context.closePath();
  491. _context.save();
  492. _context.clip();
  493. // debug triangle outline
  494. //_context.strokeStyle = "black";
  495. //_context.stroke();
  496. denom = uv1u * ( uv3v - uv2v ) - uv2u * uv3v + uv3u * uv2v + ( uv2u - uv3u ) * uv1v;
  497. m11 = - ( uv1v * (v3x - v2x ) - uv2v * v3x + uv3v * v2x + ( uv2v - uv3v ) * v1x ) / denom;
  498. m12 = ( uv2v * v3y + uv1v * ( v2y - v3y ) - uv3v * v2y + ( uv3v - uv2v) * v1y ) / denom;
  499. m21 = ( uv1u * ( v3x - v2x ) - uv2u * v3x + uv3u * v2x + ( uv2u - uv3u ) * v1x ) / denom;
  500. m22 = - ( uv2u * v3y + uv1u * ( v2y - v3y ) - uv3u * v2y + ( uv3u - uv2u ) * v1y ) / denom;
  501. dx = ( uv1u * ( uv3v * v2x - uv2v * v3x ) + uv1v * ( uv2u * v3x - uv3u * v2x ) + ( uv3u * uv2v - uv2u * uv3v ) * v1x ) / denom;
  502. dy = ( uv1u * ( uv3v * v2y - uv2v * v3y ) + uv1v * ( uv2u * v3y - uv3u * v2y ) + ( uv3u * uv2v - uv2u * uv3v ) * v1y ) / denom;
  503. _context.transform( m11, m12, m21, m22, dx, dy );
  504. _context.drawImage( bitmap, 0, 0 );
  505. _context.restore();
  506. }
  507. // Hide anti-alias gaps
  508. function expand( a, b ) {
  509. _vector2.sub( b, a );
  510. _vector2.unit();
  511. _vector2.multiplyScalar( 0.75 );
  512. b.addSelf( _vector2 );
  513. a.subSelf( _vector2 );
  514. }
  515. };