CanvasRenderer.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. calculateLight( scene, element, _light );
  73. _color.copyRGBA( material.color );
  74. _color.multiplySelfRGB( _light );
  75. _color.updateStyleString();
  76. } else {
  77. _color = material.color;
  78. }
  79. width = element.scale.x * _widthHalf;
  80. height = element.scale.y * _heightHalf;
  81. _bboxRect.set( v1x - width, v1y - height, v1x + width, v1y + height );
  82. if ( !_clipRect.instersects( _bboxRect ) ) {
  83. continue;
  84. }
  85. _context.save();
  86. _context.translate( v1x, v1y );
  87. _context.rotate( - element.rotation );
  88. _context.scale( width, height );
  89. _context.beginPath();
  90. _context.arc( 0, 0, 1, 0, pi2, true );
  91. _context.closePath();
  92. _context.fillStyle = _color.__styleString;
  93. _context.fill();
  94. _context.restore();
  95. } else if ( material instanceof THREE.ParticleBitmapMaterial ) {
  96. bitmap = material.bitmap;
  97. bitmapWidth = bitmap.width / 2;
  98. bitmapHeight = bitmap.height / 2;
  99. scaleX = element.scale.x * _widthHalf;
  100. scaleY = element.scale.y * _heightHalf;
  101. width = scaleX * bitmapWidth;
  102. height = scaleY * bitmapHeight;
  103. offsetX = material.offset.x * scaleX;
  104. offsetY = material.offset.y * scaleY;
  105. // TODO: Rotations break this...
  106. _bboxRect.set( v1x + offsetX - width, v1y + offsetY - height, v1x + offsetX + width, v1y + offsetY + height );
  107. if ( !_clipRect.instersects( _bboxRect ) ) {
  108. continue;
  109. }
  110. _context.save();
  111. _context.translate( v1x, v1y );
  112. _context.rotate( - element.rotation );
  113. _context.scale( scaleX, - scaleY );
  114. _context.translate( - bitmapWidth + material.offset.x, - bitmapHeight - material.offset.y );
  115. _context.drawImage( bitmap, 0, 0 );
  116. _context.restore();
  117. /* DEBUG
  118. _context.beginPath();
  119. _context.moveTo( v1x - 10, v1y );
  120. _context.lineTo( v1x + 10, v1y );
  121. _context.moveTo( v1x, v1y - 10 );
  122. _context.lineTo( v1x, v1y + 10 );
  123. _context.closePath();
  124. _context.strokeStyle = 'rgb(255,255,0)';
  125. _context.stroke();
  126. */
  127. }
  128. }
  129. } else if ( element instanceof THREE.RenderableLine ) {
  130. v1x = element.v1.x * _widthHalf; v1y = element.v1.y * _heightHalf;
  131. v2x = element.v2.x * _widthHalf; v2y = element.v2.y * _heightHalf;
  132. _bboxRect.addPoint( v1x, v1y );
  133. _bboxRect.addPoint( v2x, v2y );
  134. if ( !_clipRect.instersects( _bboxRect ) ) {
  135. continue;
  136. }
  137. _context.beginPath();
  138. _context.moveTo( v1x, v1y );
  139. _context.lineTo( v2x, v2y );
  140. _context.closePath();
  141. for ( m = 0, ml = element.material.length; m < ml; m++ ) {
  142. material = element.material[ m ];
  143. if ( material instanceof THREE.LineColorMaterial ) {
  144. if ( _enableLighting ) {
  145. _light.copyRGB( _ambientLight );
  146. calculateLight( scene, element, _light );
  147. _color.copyRGBA( material.color );
  148. _color.multiplySelfRGB( _light );
  149. _color.updateStyleString();
  150. } else {
  151. _color = material.color;
  152. }
  153. _context.lineWidth = material.lineWidth;
  154. _context.lineJoin = "round";
  155. _context.lineCap = "round";
  156. _context.strokeStyle = _color.__styleString;
  157. _context.stroke();
  158. _bboxRect.inflate( _context.lineWidth );
  159. }
  160. }
  161. } else if ( element instanceof THREE.RenderableFace3 ) {
  162. element.v1.x *= _widthHalf; element.v1.y *= _heightHalf;
  163. element.v2.x *= _widthHalf; element.v2.y *= _heightHalf;
  164. element.v3.x *= _widthHalf; element.v3.y *= _heightHalf;
  165. if ( element.overdraw ) {
  166. expand( element.v1, element.v2 );
  167. expand( element.v2, element.v3 );
  168. expand( element.v3, element.v1 );
  169. }
  170. v1x = element.v1.x; v1y = element.v1.y;
  171. v2x = element.v2.x; v2y = element.v2.y;
  172. v3x = element.v3.x; v3y = element.v3.y;
  173. _bboxRect.addPoint( v1x, v1y );
  174. _bboxRect.addPoint( v2x, v2y );
  175. _bboxRect.addPoint( v3x, v3y );
  176. if ( !_clipRect.instersects( _bboxRect ) ) {
  177. continue;
  178. }
  179. for ( m = 0, ml = element.material.length; m < ml; m++ ) {
  180. material = element.material[ m ];
  181. if ( material instanceof THREE.MeshColorFillMaterial ) {
  182. if ( _enableLighting ) {
  183. _light.copyRGB( _ambientLight );
  184. calculateFaceLight( scene, element, _light );
  185. _color.copyRGBA( material.color );
  186. _color.multiplySelfRGB( _light );
  187. _color.updateStyleString();
  188. } else {
  189. _color = material.color;
  190. }
  191. _context.beginPath();
  192. _context.moveTo( v1x, v1y );
  193. _context.lineTo( v2x, v2y );
  194. _context.lineTo( v3x, v3y );
  195. _context.lineTo( v1x, v1y );
  196. _context.closePath();
  197. _context.fillStyle = _color.__styleString;
  198. _context.fill();
  199. } else if ( material instanceof THREE.MeshColorStrokeMaterial ) {
  200. if ( _enableLighting ) {
  201. _light.copyRGB( _ambientLight );
  202. calculateFaceLight( scene, element, _light );
  203. _color.copyRGBA( material.color );
  204. _color.multiplySelfRGB( _light );
  205. _color.updateStyleString();
  206. } else {
  207. _color = material.color;
  208. }
  209. _context.beginPath();
  210. _context.moveTo( v1x, v1y );
  211. _context.lineTo( v2x, v2y );
  212. _context.lineTo( v3x, v3y );
  213. _context.lineTo( v1x, v1y );
  214. _context.closePath();
  215. _context.lineWidth = material.lineWidth;
  216. _context.lineJoin = "round";
  217. _context.lineCap = "round";
  218. _context.strokeStyle = _color.__styleString;
  219. _context.stroke();
  220. _bboxRect.inflate( _context.lineWidth );
  221. } else if ( material instanceof THREE.MeshFaceColorFillMaterial ) {
  222. if ( _enableLighting ) {
  223. _light.copyRGB( _ambientLight );
  224. calculateFaceLight( scene, element, _light );
  225. _color.copyRGBA( element.color );
  226. _color.multiplySelfRGB( _light );
  227. _color.updateStyleString();
  228. } else {
  229. _color = element.color;
  230. }
  231. _context.beginPath();
  232. _context.moveTo( v1x, v1y );
  233. _context.lineTo( v2x, v2y );
  234. _context.lineTo( v3x, v3y );
  235. _context.lineTo( v1x, v1y );
  236. _context.closePath();
  237. _context.fillStyle = _color.__styleString;
  238. _context.fill();
  239. } else if ( material instanceof THREE.MeshFaceColorStrokeMaterial ) {
  240. if ( _enableLighting ) {
  241. _light.copyRGB( _ambientLight );
  242. calculateFaceLight( scene, element, _light );
  243. _color.copyRGBA( element.color );
  244. _color.multiplySelfRGB( _light );
  245. _color.updateStyleString();
  246. } else {
  247. _color = element.color;
  248. }
  249. _context.beginPath();
  250. _context.moveTo( v1x, v1y );
  251. _context.lineTo( v2x, v2y );
  252. _context.lineTo( v3x, v3y );
  253. _context.lineTo( v1x, v1y );
  254. _context.closePath();
  255. _context.lineWidth = material.lineWidth;
  256. _context.lineJoin = "round";
  257. _context.lineCap = "round";
  258. _context.strokeStyle = _color.__styleString;
  259. _context.stroke();
  260. _bboxRect.inflate( _context.lineWidth );
  261. } else if ( material instanceof THREE.MeshBitmapUVMappingMaterial ) {
  262. bitmap = material.bitmap;
  263. bitmapWidth = bitmap.width - 1;
  264. bitmapHeight = bitmap.height - 1;
  265. /* DEBUG
  266. if ( !element.uvs[ 0 ] || !element.uvs[ 1 ] || !element.uvs[ 2 ]) {
  267. _context.beginPath();
  268. _context.moveTo( v1x, v1y );
  269. _context.lineTo( v2x, v2y );
  270. _context.lineTo( v3x, v3y );
  271. _context.lineTo( v1x, v1y );
  272. _context.closePath();
  273. _context.fillStyle = 'rgb(0, 255, 0)';
  274. _context.fill();
  275. continue;
  276. }
  277. */
  278. uv1.copy( element.uvs[ 0 ] );
  279. uv2.copy( element.uvs[ 1 ] );
  280. uv3.copy( element.uvs[ 2 ] );
  281. uv1.u *= bitmapWidth; uv1.v *= bitmapHeight;
  282. uv2.u *= bitmapWidth; uv2.v *= bitmapHeight;
  283. uv3.u *= bitmapWidth; uv3.v *= bitmapHeight;
  284. drawTexturedTriangle( bitmap, v1x, v1y, v2x, v2y, v3x, v3y, uv1.u, uv1.v, uv2.u, uv2.v, uv3.u, uv3.v );
  285. }
  286. }
  287. } else if ( element instanceof THREE.RenderableFace4 ) {
  288. element.v1.x *= _widthHalf; element.v1.y *= _heightHalf;
  289. element.v2.x *= _widthHalf; element.v2.y *= _heightHalf;
  290. element.v3.x *= _widthHalf; element.v3.y *= _heightHalf;
  291. element.v4.x *= _widthHalf; element.v4.y *= _heightHalf;
  292. v5.copy( element.v2 ); v6.copy( element.v4 );
  293. if ( element.overdraw ) {
  294. expand( element.v1, element.v2 );
  295. expand( element.v2, element.v4 );
  296. expand( element.v4, element.v1 );
  297. }
  298. v1x = element.v1.x; v1y = element.v1.y;
  299. v2x = element.v2.x; v2y = element.v2.y;
  300. v4x = element.v4.x; v4y = element.v4.y;
  301. if ( element.overdraw ) {
  302. expand( element.v3, v5 );
  303. expand( element.v3, v6 );
  304. }
  305. v3x = element.v3.x; v3y = element.v3.y;
  306. v5x = v5.x; v5y = v5.y;
  307. v6x = v6.x; v6y = v6.y;
  308. _bboxRect.addPoint( v1x, v1y );
  309. _bboxRect.addPoint( v2x, v2y );
  310. _bboxRect.addPoint( v3x, v3y );
  311. _bboxRect.addPoint( v4x, v4y );
  312. if ( !_clipRect.instersects( _bboxRect ) ) {
  313. continue;
  314. }
  315. for ( m = 0, ml = element.material.length; m < ml; m++ ) {
  316. material = element.material[ m ];
  317. if ( material instanceof THREE.MeshColorFillMaterial ) {
  318. if ( _enableLighting ) {
  319. _light.copyRGB( _ambientLight );
  320. calculateFaceLight( scene, element, _light );
  321. _color.copyRGBA( material.color );
  322. _color.multiplySelfRGB( _light );
  323. _color.updateStyleString();
  324. } else {
  325. _color = material.color;
  326. }
  327. _context.beginPath();
  328. _context.moveTo( v1x, v1y );
  329. _context.lineTo( v2x, v2y );
  330. _context.lineTo( v3x, v3y );
  331. _context.lineTo( v4x, v4y );
  332. _context.lineTo( v1x, v1y );
  333. _context.closePath();
  334. _context.fillStyle = _color.__styleString;
  335. _context.fill();
  336. } else if ( material instanceof THREE.MeshColorStrokeMaterial ) {
  337. if ( _enableLighting ) {
  338. _light.copyRGB( _ambientLight );
  339. calculateFaceLight( scene, element, _light );
  340. _color.copyRGBA( material.color );
  341. _color.multiplySelfRGB( _light );
  342. _color.updateStyleString();
  343. } else {
  344. _color = material.color;
  345. }
  346. _context.beginPath();
  347. _context.moveTo( v1x, v1y );
  348. _context.lineTo( v2x, v2y );
  349. _context.lineTo( v3x, v3y );
  350. _context.lineTo( v4x, v4y );
  351. _context.lineTo( v1x, v1y );
  352. _context.closePath();
  353. _context.lineWidth = material.lineWidth;
  354. _context.lineJoin = "round";
  355. _context.lineCap = "round";
  356. _context.strokeStyle = _color.__styleString;
  357. _context.stroke();
  358. _bboxRect.inflate( _context.lineWidth );
  359. } else if ( material instanceof THREE.MeshFaceColorFillMaterial ) {
  360. if ( _enableLighting ) {
  361. _light.copyRGB( _ambientLight );
  362. calculateFaceLight( scene, element, _light );
  363. _color.copyRGBA( element.color );
  364. _color.multiplySelfRGB( _light );
  365. _color.updateStyleString();
  366. } else {
  367. _color = element.color;
  368. }
  369. _context.beginPath();
  370. _context.moveTo( v1x, v1y );
  371. _context.lineTo( v2x, v2y );
  372. _context.lineTo( v3x, v3y );
  373. _context.lineTo( v4x, v4y );
  374. _context.lineTo( v1x, v1y );
  375. _context.closePath();
  376. _context.fillStyle = _color.__styleString;
  377. _context.fill();
  378. } else if ( material instanceof THREE.MeshFaceColorStrokeMaterial ) {
  379. if ( _enableLighting ) {
  380. _light.copyRGB( _ambientLight );
  381. calculateFaceLight( scene, element, _light );
  382. _color.copyRGBA( element.color );
  383. _color.multiplySelfRGB( _light );
  384. _color.updateStyleString();
  385. } else {
  386. _color = element.color;
  387. }
  388. _context.beginPath();
  389. _context.moveTo( v1x, v1y );
  390. _context.lineTo( v2x, v2y );
  391. _context.lineTo( v3x, v3y );
  392. _context.lineTo( v4x, v4y );
  393. _context.lineTo( v1x, v1y );
  394. _context.closePath();
  395. _context.lineWidth = material.lineWidth;
  396. _context.lineJoin = "round";
  397. _context.lineCap = "round";
  398. _context.strokeStyle = _color.__styleString;
  399. _context.stroke();
  400. _bboxRect.inflate( _context.lineWidth );
  401. } else if ( material instanceof THREE.MeshBitmapUVMappingMaterial ) {
  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 calculateLight( scene, element, color ) {
  459. var l, ll, light;
  460. for ( l = 0, ll = scene.lights.length; l < ll; l++ ) {
  461. light = scene.lights[ l ];
  462. if ( light instanceof THREE.DirectionalLight ) {
  463. color.r += light.color.r;
  464. color.g += light.color.g;
  465. color.b += light.color.b;
  466. } else if ( light instanceof THREE.PointLight ) {
  467. color.r += light.color.r;
  468. color.g += light.color.g;
  469. color.b += light.color.b;
  470. }
  471. }
  472. }
  473. function calculateFaceLight( scene, element, color ) {
  474. var l, ll, light, amount;
  475. for ( l = 0, ll = scene.lights.length; l < ll; l++ ) {
  476. light = scene.lights[ l ];
  477. if ( light instanceof THREE.DirectionalLight ) {
  478. amount = element.normalWorld.dot( light.position ) * light.intensity;
  479. if ( amount > 0 ) {
  480. color.r += light.color.r * amount;
  481. color.g += light.color.g * amount;
  482. color.b += light.color.b * amount;
  483. }
  484. } else if ( light instanceof THREE.PointLight ) {
  485. _vector3.sub( light.position, element.centroidWorld );
  486. _vector3.normalize();
  487. amount = element.normalWorld.dot( _vector3 ) * light.intensity;
  488. if ( amount > 0 ) {
  489. color.r += light.color.r * amount;
  490. color.g += light.color.g * amount;
  491. color.b += light.color.b * amount;
  492. }
  493. }
  494. }
  495. }
  496. function drawTexturedTriangle( bitmap, v1x, v1y, v2x, v2y, v3x, v3y, uv1u, uv1v, uv2u, uv2v, uv3u, uv3v ) {
  497. // Textured triangle drawing by Thatcher Ulrich.
  498. // http://tulrich.com/geekstuff/canvas/jsgl.js
  499. var denom, m11, m12, m21, m22, dx, dy;
  500. _context.beginPath();
  501. _context.moveTo( v1x, v1y );
  502. _context.lineTo( v2x, v2y );
  503. _context.lineTo( v3x, v3y );
  504. _context.lineTo( v1x, v1y );
  505. _context.closePath();
  506. _context.save();
  507. _context.clip();
  508. denom = uv1u * ( uv3v - uv2v ) - uv2u * uv3v + uv3u * uv2v + ( uv2u - uv3u ) * uv1v;
  509. m11 = - ( uv1v * (v3x - v2x ) - uv2v * v3x + uv3v * v2x + ( uv2v - uv3v ) * v1x ) / denom;
  510. m12 = ( uv2v * v3y + uv1v * ( v2y - v3y ) - uv3v * v2y + ( uv3v - uv2v) * v1y ) / denom;
  511. m21 = ( uv1u * ( v3x - v2x ) - uv2u * v3x + uv3u * v2x + ( uv2u - uv3u ) * v1x ) / denom;
  512. m22 = - ( uv2u * v3y + uv1u * ( v2y - v3y ) - uv3u * v2y + ( uv3u - uv2u ) * v1y ) / denom;
  513. dx = ( uv1u * ( uv3v * v2x - uv2v * v3x ) + uv1v * ( uv2u * v3x - uv3u * v2x ) + ( uv3u * uv2v - uv2u * uv3v ) * v1x ) / denom;
  514. dy = ( uv1u * ( uv3v * v2y - uv2v * v3y ) + uv1v * ( uv2u * v3y - uv3u * v2y ) + ( uv3u * uv2v - uv2u * uv3v ) * v1y ) / denom;
  515. _context.transform( m11, m12, m21, m22, dx, dy );
  516. _context.drawImage( bitmap, 0, 0 );
  517. _context.restore();
  518. }
  519. // Hide anti-alias gaps
  520. function expand( a, b ) {
  521. _vector2.sub( b, a );
  522. _vector2.unit();
  523. _vector2.multiplyScalar( 0.75 );
  524. b.addSelf( _vector2 );
  525. a.subSelf( _vector2 );
  526. }
  527. };