CanvasRenderer.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.SpriteCanvasMaterial = function ( parameters ) {
  5. THREE.Material.call( this );
  6. this.type = 'SpriteCanvasMaterial';
  7. this.color = new THREE.Color( 0xffffff );
  8. this.program = function ( context, color ) {};
  9. this.setValues( parameters );
  10. };
  11. THREE.SpriteCanvasMaterial.prototype = Object.create( THREE.Material.prototype );
  12. THREE.SpriteCanvasMaterial.prototype.constructor = THREE.SpriteCanvasMaterial;
  13. THREE.SpriteCanvasMaterial.prototype.clone = function () {
  14. var material = new THREE.SpriteCanvasMaterial();
  15. material.copy( this );
  16. material.color.copy( this.color );
  17. material.program = this.program;
  18. return material;
  19. };
  20. //
  21. THREE.CanvasRenderer = function ( parameters ) {
  22. console.log( 'THREE.CanvasRenderer', THREE.REVISION );
  23. parameters = parameters || {};
  24. var _this = this,
  25. _renderData, _elements, _lights,
  26. _projector = new THREE.Projector(),
  27. _canvas = parameters.canvas !== undefined
  28. ? parameters.canvas
  29. : document.createElement( 'canvas' ),
  30. _canvasWidth = _canvas.width,
  31. _canvasHeight = _canvas.height,
  32. _canvasWidthHalf = Math.floor( _canvasWidth / 2 ),
  33. _canvasHeightHalf = Math.floor( _canvasHeight / 2 ),
  34. _viewportX = 0,
  35. _viewportY = 0,
  36. _viewportWidth = _canvasWidth,
  37. _viewportHeight = _canvasHeight,
  38. _pixelRatio = 1,
  39. _context = _canvas.getContext( '2d', {
  40. alpha: parameters.alpha === true
  41. } ),
  42. _clearColor = new THREE.Color( 0x000000 ),
  43. _clearAlpha = parameters.alpha === true ? 0 : 1,
  44. _contextGlobalAlpha = 1,
  45. _contextGlobalCompositeOperation = 0,
  46. _contextStrokeStyle = null,
  47. _contextFillStyle = null,
  48. _contextLineWidth = null,
  49. _contextLineCap = null,
  50. _contextLineJoin = null,
  51. _contextLineDash = [],
  52. _camera,
  53. _v1, _v2, _v3, _v4,
  54. _v5 = new THREE.RenderableVertex(),
  55. _v6 = new THREE.RenderableVertex(),
  56. _v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
  57. _v4x, _v4y, _v5x, _v5y, _v6x, _v6y,
  58. _color = new THREE.Color(),
  59. _color1 = new THREE.Color(),
  60. _color2 = new THREE.Color(),
  61. _color3 = new THREE.Color(),
  62. _color4 = new THREE.Color(),
  63. _diffuseColor = new THREE.Color(),
  64. _emissiveColor = new THREE.Color(),
  65. _lightColor = new THREE.Color(),
  66. _patterns = {},
  67. _image, _uvs,
  68. _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y,
  69. _clipBox = new THREE.Box2(),
  70. _clearBox = new THREE.Box2(),
  71. _elemBox = new THREE.Box2(),
  72. _ambientLight = new THREE.Color(),
  73. _directionalLights = new THREE.Color(),
  74. _pointLights = new THREE.Color(),
  75. _vector3 = new THREE.Vector3(), // Needed for PointLight
  76. _centroid = new THREE.Vector3(),
  77. _normal = new THREE.Vector3(),
  78. _normalViewMatrix = new THREE.Matrix3();
  79. /* TODO
  80. _canvas.mozImageSmoothingEnabled = false;
  81. _canvas.webkitImageSmoothingEnabled = false;
  82. _canvas.msImageSmoothingEnabled = false;
  83. _canvas.imageSmoothingEnabled = false;
  84. */
  85. // dash+gap fallbacks for Firefox and everything else
  86. if ( _context.setLineDash === undefined ) {
  87. _context.setLineDash = function () {};
  88. }
  89. this.domElement = _canvas;
  90. this.autoClear = true;
  91. this.sortObjects = true;
  92. this.sortElements = true;
  93. this.info = {
  94. render: {
  95. vertices: 0,
  96. faces: 0
  97. }
  98. };
  99. // WebGLRenderer compatibility
  100. this.supportsVertexTextures = function () {};
  101. this.setFaceCulling = function () {};
  102. // API
  103. this.getContext = function () {
  104. return _context;
  105. };
  106. this.getContextAttributes = function () {
  107. return _context.getContextAttributes();
  108. };
  109. this.getPixelRatio = function () {
  110. return _pixelRatio;
  111. };
  112. this.setPixelRatio = function ( value ) {
  113. if ( value !== undefined ) _pixelRatio = value;
  114. };
  115. this.setSize = function ( width, height, updateStyle ) {
  116. _canvasWidth = width * _pixelRatio;
  117. _canvasHeight = height * _pixelRatio;
  118. _canvas.width = _canvasWidth;
  119. _canvas.height = _canvasHeight;
  120. _canvasWidthHalf = Math.floor( _canvasWidth / 2 );
  121. _canvasHeightHalf = Math.floor( _canvasHeight / 2 );
  122. if ( updateStyle !== false ) {
  123. _canvas.style.width = width + 'px';
  124. _canvas.style.height = height + 'px';
  125. }
  126. _clipBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf );
  127. _clipBox.max.set( _canvasWidthHalf, _canvasHeightHalf );
  128. _clearBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf );
  129. _clearBox.max.set( _canvasWidthHalf, _canvasHeightHalf );
  130. _contextGlobalAlpha = 1;
  131. _contextGlobalCompositeOperation = 0;
  132. _contextStrokeStyle = null;
  133. _contextFillStyle = null;
  134. _contextLineWidth = null;
  135. _contextLineCap = null;
  136. _contextLineJoin = null;
  137. this.setViewport( 0, 0, width, height );
  138. };
  139. this.setViewport = function ( x, y, width, height ) {
  140. _viewportX = x * _pixelRatio;
  141. _viewportY = y * _pixelRatio;
  142. _viewportWidth = width * _pixelRatio;
  143. _viewportHeight = height * _pixelRatio;
  144. };
  145. this.setScissor = function () {};
  146. this.setScissorTest = function () {};
  147. this.setClearColor = function ( color, alpha ) {
  148. _clearColor.set( color );
  149. _clearAlpha = alpha !== undefined ? alpha : 1;
  150. _clearBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf );
  151. _clearBox.max.set( _canvasWidthHalf, _canvasHeightHalf );
  152. };
  153. this.setClearColorHex = function ( hex, alpha ) {
  154. console.warn( 'THREE.CanvasRenderer: .setClearColorHex() is being removed. Use .setClearColor() instead.' );
  155. this.setClearColor( hex, alpha );
  156. };
  157. this.getClearColor = function () {
  158. return _clearColor;
  159. };
  160. this.getClearAlpha = function () {
  161. return _clearAlpha;
  162. };
  163. this.getMaxAnisotropy = function () {
  164. return 0;
  165. };
  166. this.clear = function () {
  167. if ( _clearBox.isEmpty() === false ) {
  168. _clearBox.intersect( _clipBox );
  169. _clearBox.expandByScalar( 2 );
  170. _clearBox.min.x = _clearBox.min.x + _canvasWidthHalf;
  171. _clearBox.min.y = - _clearBox.min.y + _canvasHeightHalf; // higher y value !
  172. _clearBox.max.x = _clearBox.max.x + _canvasWidthHalf;
  173. _clearBox.max.y = - _clearBox.max.y + _canvasHeightHalf; // lower y value !
  174. if ( _clearAlpha < 1 ) {
  175. _context.clearRect(
  176. _clearBox.min.x | 0,
  177. _clearBox.max.y | 0,
  178. ( _clearBox.max.x - _clearBox.min.x ) | 0,
  179. ( _clearBox.min.y - _clearBox.max.y ) | 0
  180. );
  181. }
  182. if ( _clearAlpha > 0 ) {
  183. setBlending( THREE.NormalBlending );
  184. setOpacity( 1 );
  185. setFillStyle( 'rgba(' + Math.floor( _clearColor.r * 255 ) + ',' + Math.floor( _clearColor.g * 255 ) + ',' + Math.floor( _clearColor.b * 255 ) + ',' + _clearAlpha + ')' );
  186. _context.fillRect(
  187. _clearBox.min.x | 0,
  188. _clearBox.max.y | 0,
  189. ( _clearBox.max.x - _clearBox.min.x ) | 0,
  190. ( _clearBox.min.y - _clearBox.max.y ) | 0
  191. );
  192. }
  193. _clearBox.makeEmpty();
  194. }
  195. };
  196. // compatibility
  197. this.clearColor = function () {};
  198. this.clearDepth = function () {};
  199. this.clearStencil = function () {};
  200. this.render = function ( scene, camera ) {
  201. if ( camera instanceof THREE.Camera === false ) {
  202. console.error( 'THREE.CanvasRenderer.render: camera is not an instance of THREE.Camera.' );
  203. return;
  204. }
  205. var background = scene.background;
  206. if ( background && background.isColor ) {
  207. setFillStyle( 'rgb(' + Math.floor( background.r * 255 ) + ',' + Math.floor( background.g * 255 ) + ',' + Math.floor( background.b * 255 ) + ')' );
  208. _context.fillRect( 0, 0, _canvasWidth, _canvasHeight );
  209. } else if ( this.autoClear === true ) {
  210. this.clear();
  211. }
  212. _this.info.render.vertices = 0;
  213. _this.info.render.faces = 0;
  214. _context.setTransform( _viewportWidth / _canvasWidth, 0, 0, - _viewportHeight / _canvasHeight, _viewportX, _canvasHeight - _viewportY );
  215. _context.translate( _canvasWidthHalf, _canvasHeightHalf );
  216. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  217. _elements = _renderData.elements;
  218. _lights = _renderData.lights;
  219. _camera = camera;
  220. _normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );
  221. /* DEBUG
  222. setFillStyle( 'rgba( 0, 255, 255, 0.5 )' );
  223. _context.fillRect( _clipBox.min.x, _clipBox.min.y, _clipBox.max.x - _clipBox.min.x, _clipBox.max.y - _clipBox.min.y );
  224. */
  225. calculateLights();
  226. for ( var e = 0, el = _elements.length; e < el; e ++ ) {
  227. var element = _elements[ e ];
  228. var material = element.material;
  229. if ( material === undefined || material.opacity === 0 ) continue;
  230. _elemBox.makeEmpty();
  231. if ( element instanceof THREE.RenderableSprite ) {
  232. _v1 = element;
  233. _v1.x *= _canvasWidthHalf; _v1.y *= _canvasHeightHalf;
  234. renderSprite( _v1, element, material );
  235. } else if ( element instanceof THREE.RenderableLine ) {
  236. _v1 = element.v1; _v2 = element.v2;
  237. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  238. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  239. _elemBox.setFromPoints( [
  240. _v1.positionScreen,
  241. _v2.positionScreen
  242. ] );
  243. if ( _clipBox.intersectsBox( _elemBox ) === true ) {
  244. renderLine( _v1, _v2, element, material );
  245. }
  246. } else if ( element instanceof THREE.RenderableFace ) {
  247. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  248. if ( _v1.positionScreen.z < - 1 || _v1.positionScreen.z > 1 ) continue;
  249. if ( _v2.positionScreen.z < - 1 || _v2.positionScreen.z > 1 ) continue;
  250. if ( _v3.positionScreen.z < - 1 || _v3.positionScreen.z > 1 ) continue;
  251. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  252. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  253. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  254. if ( material.overdraw > 0 ) {
  255. expand( _v1.positionScreen, _v2.positionScreen, material.overdraw );
  256. expand( _v2.positionScreen, _v3.positionScreen, material.overdraw );
  257. expand( _v3.positionScreen, _v1.positionScreen, material.overdraw );
  258. }
  259. _elemBox.setFromPoints( [
  260. _v1.positionScreen,
  261. _v2.positionScreen,
  262. _v3.positionScreen
  263. ] );
  264. if ( _clipBox.intersectsBox( _elemBox ) === true ) {
  265. renderFace3( _v1, _v2, _v3, 0, 1, 2, element, material );
  266. }
  267. }
  268. /* DEBUG
  269. setLineWidth( 1 );
  270. setStrokeStyle( 'rgba( 0, 255, 0, 0.5 )' );
  271. _context.strokeRect( _elemBox.min.x, _elemBox.min.y, _elemBox.max.x - _elemBox.min.x, _elemBox.max.y - _elemBox.min.y );
  272. */
  273. _clearBox.union( _elemBox );
  274. }
  275. /* DEBUG
  276. setLineWidth( 1 );
  277. setStrokeStyle( 'rgba( 255, 0, 0, 0.5 )' );
  278. _context.strokeRect( _clearBox.min.x, _clearBox.min.y, _clearBox.max.x - _clearBox.min.x, _clearBox.max.y - _clearBox.min.y );
  279. */
  280. _context.setTransform( 1, 0, 0, 1, 0, 0 );
  281. };
  282. //
  283. function calculateLights() {
  284. _ambientLight.setRGB( 0, 0, 0 );
  285. _directionalLights.setRGB( 0, 0, 0 );
  286. _pointLights.setRGB( 0, 0, 0 );
  287. for ( var l = 0, ll = _lights.length; l < ll; l ++ ) {
  288. var light = _lights[ l ];
  289. var lightColor = light.color;
  290. if ( light instanceof THREE.AmbientLight ) {
  291. _ambientLight.add( lightColor );
  292. } else if ( light instanceof THREE.DirectionalLight ) {
  293. // for sprites
  294. _directionalLights.add( lightColor );
  295. } else if ( light instanceof THREE.PointLight ) {
  296. // for sprites
  297. _pointLights.add( lightColor );
  298. }
  299. }
  300. }
  301. function calculateLight( position, normal, color ) {
  302. for ( var l = 0, ll = _lights.length; l < ll; l ++ ) {
  303. var light = _lights[ l ];
  304. _lightColor.copy( light.color );
  305. if ( light instanceof THREE.DirectionalLight ) {
  306. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize();
  307. var amount = normal.dot( lightPosition );
  308. if ( amount <= 0 ) continue;
  309. amount *= light.intensity;
  310. color.add( _lightColor.multiplyScalar( amount ) );
  311. } else if ( light instanceof THREE.PointLight ) {
  312. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
  313. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  314. if ( amount <= 0 ) continue;
  315. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  316. if ( amount == 0 ) continue;
  317. amount *= light.intensity;
  318. color.add( _lightColor.multiplyScalar( amount ) );
  319. }
  320. }
  321. }
  322. function renderSprite( v1, element, material ) {
  323. setOpacity( material.opacity );
  324. setBlending( material.blending );
  325. var scaleX = element.scale.x * _canvasWidthHalf;
  326. var scaleY = element.scale.y * _canvasHeightHalf;
  327. var dist = 0.5 * Math.sqrt( scaleX * scaleX + scaleY * scaleY ); // allow for rotated sprite
  328. _elemBox.min.set( v1.x - dist, v1.y - dist );
  329. _elemBox.max.set( v1.x + dist, v1.y + dist );
  330. if ( material instanceof THREE.SpriteMaterial ) {
  331. var texture = material.map;
  332. if ( texture !== null ) {
  333. var pattern = _patterns[ texture.id ];
  334. if ( pattern === undefined || pattern.version !== texture.version ) {
  335. pattern = textureToPattern( texture );
  336. _patterns[ texture.id ] = pattern;
  337. }
  338. if ( pattern.canvas !== undefined ) {
  339. setFillStyle( pattern.canvas );
  340. var bitmap = texture.image;
  341. var ox = bitmap.width * texture.offset.x;
  342. var oy = bitmap.height * texture.offset.y;
  343. var sx = bitmap.width * texture.repeat.x;
  344. var sy = bitmap.height * texture.repeat.y;
  345. var cx = scaleX / sx;
  346. var cy = scaleY / sy;
  347. _context.save();
  348. _context.translate( v1.x, v1.y );
  349. if ( material.rotation !== 0 ) _context.rotate( material.rotation );
  350. _context.translate( - scaleX / 2, - scaleY / 2 );
  351. _context.scale( cx, cy );
  352. _context.translate( - ox, - oy );
  353. _context.fillRect( ox, oy, sx, sy );
  354. _context.restore();
  355. }
  356. } else {
  357. // no texture
  358. setFillStyle( material.color.getStyle() );
  359. _context.save();
  360. _context.translate( v1.x, v1.y );
  361. if ( material.rotation !== 0 ) _context.rotate( material.rotation );
  362. _context.scale( scaleX, - scaleY );
  363. _context.fillRect( - 0.5, - 0.5, 1, 1 );
  364. _context.restore();
  365. }
  366. } else if ( material instanceof THREE.SpriteCanvasMaterial ) {
  367. setStrokeStyle( material.color.getStyle() );
  368. setFillStyle( material.color.getStyle() );
  369. _context.save();
  370. _context.translate( v1.x, v1.y );
  371. if ( material.rotation !== 0 ) _context.rotate( material.rotation );
  372. _context.scale( scaleX, scaleY );
  373. material.program( _context );
  374. _context.restore();
  375. }
  376. /* DEBUG
  377. setStrokeStyle( 'rgb(255,255,0)' );
  378. _context.beginPath();
  379. _context.moveTo( v1.x - 10, v1.y );
  380. _context.lineTo( v1.x + 10, v1.y );
  381. _context.moveTo( v1.x, v1.y - 10 );
  382. _context.lineTo( v1.x, v1.y + 10 );
  383. _context.stroke();
  384. */
  385. }
  386. function renderLine( v1, v2, element, material ) {
  387. setOpacity( material.opacity );
  388. setBlending( material.blending );
  389. _context.beginPath();
  390. _context.moveTo( v1.positionScreen.x, v1.positionScreen.y );
  391. _context.lineTo( v2.positionScreen.x, v2.positionScreen.y );
  392. if ( material instanceof THREE.LineBasicMaterial ) {
  393. setLineWidth( material.linewidth );
  394. setLineCap( material.linecap );
  395. setLineJoin( material.linejoin );
  396. if ( material.vertexColors !== THREE.VertexColors ) {
  397. setStrokeStyle( material.color.getStyle() );
  398. } else {
  399. var colorStyle1 = element.vertexColors[ 0 ].getStyle();
  400. var colorStyle2 = element.vertexColors[ 1 ].getStyle();
  401. if ( colorStyle1 === colorStyle2 ) {
  402. setStrokeStyle( colorStyle1 );
  403. } else {
  404. try {
  405. var grad = _context.createLinearGradient(
  406. v1.positionScreen.x,
  407. v1.positionScreen.y,
  408. v2.positionScreen.x,
  409. v2.positionScreen.y
  410. );
  411. grad.addColorStop( 0, colorStyle1 );
  412. grad.addColorStop( 1, colorStyle2 );
  413. } catch ( exception ) {
  414. grad = colorStyle1;
  415. }
  416. setStrokeStyle( grad );
  417. }
  418. }
  419. _context.stroke();
  420. _elemBox.expandByScalar( material.linewidth * 2 );
  421. } else if ( material instanceof THREE.LineDashedMaterial ) {
  422. setLineWidth( material.linewidth );
  423. setLineCap( material.linecap );
  424. setLineJoin( material.linejoin );
  425. setStrokeStyle( material.color.getStyle() );
  426. setLineDash( [ material.dashSize, material.gapSize ] );
  427. _context.stroke();
  428. _elemBox.expandByScalar( material.linewidth * 2 );
  429. setLineDash( [] );
  430. }
  431. }
  432. function renderFace3( v1, v2, v3, uv1, uv2, uv3, element, material ) {
  433. _this.info.render.vertices += 3;
  434. _this.info.render.faces ++;
  435. setOpacity( material.opacity );
  436. setBlending( material.blending );
  437. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  438. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  439. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  440. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y );
  441. if ( ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) && material.map === null ) {
  442. _diffuseColor.copy( material.color );
  443. _emissiveColor.copy( material.emissive );
  444. if ( material.vertexColors === THREE.FaceColors ) {
  445. _diffuseColor.multiply( element.color );
  446. }
  447. _color.copy( _ambientLight );
  448. _centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );
  449. calculateLight( _centroid, element.normalModel, _color );
  450. _color.multiply( _diffuseColor ).add( _emissiveColor );
  451. material.wireframe === true
  452. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  453. : fillPath( _color );
  454. } else if ( material instanceof THREE.MeshBasicMaterial ||
  455. material instanceof THREE.MeshLambertMaterial ||
  456. material instanceof THREE.MeshPhongMaterial ) {
  457. if ( material.map !== null ) {
  458. var mapping = material.map.mapping;
  459. if ( mapping === THREE.UVMapping ) {
  460. _uvs = element.uvs;
  461. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uvs[ uv1 ].x, _uvs[ uv1 ].y, _uvs[ uv2 ].x, _uvs[ uv2 ].y, _uvs[ uv3 ].x, _uvs[ uv3 ].y, material.map );
  462. }
  463. } else if ( material.envMap !== null ) {
  464. if ( material.envMap.mapping === THREE.SphericalReflectionMapping ) {
  465. _normal.copy( element.vertexNormalsModel[ uv1 ] ).applyMatrix3( _normalViewMatrix );
  466. _uv1x = 0.5 * _normal.x + 0.5;
  467. _uv1y = 0.5 * _normal.y + 0.5;
  468. _normal.copy( element.vertexNormalsModel[ uv2 ] ).applyMatrix3( _normalViewMatrix );
  469. _uv2x = 0.5 * _normal.x + 0.5;
  470. _uv2y = 0.5 * _normal.y + 0.5;
  471. _normal.copy( element.vertexNormalsModel[ uv3 ] ).applyMatrix3( _normalViewMatrix );
  472. _uv3x = 0.5 * _normal.x + 0.5;
  473. _uv3y = 0.5 * _normal.y + 0.5;
  474. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y, material.envMap );
  475. }
  476. } else {
  477. _color.copy( material.color );
  478. if ( material.vertexColors === THREE.FaceColors ) {
  479. _color.multiply( element.color );
  480. }
  481. material.wireframe === true
  482. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  483. : fillPath( _color );
  484. }
  485. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  486. _normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
  487. _color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  488. material.wireframe === true
  489. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  490. : fillPath( _color );
  491. } else {
  492. _color.setRGB( 1, 1, 1 );
  493. material.wireframe === true
  494. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  495. : fillPath( _color );
  496. }
  497. }
  498. //
  499. function drawTriangle( x0, y0, x1, y1, x2, y2 ) {
  500. _context.beginPath();
  501. _context.moveTo( x0, y0 );
  502. _context.lineTo( x1, y1 );
  503. _context.lineTo( x2, y2 );
  504. _context.closePath();
  505. }
  506. function strokePath( color, linewidth, linecap, linejoin ) {
  507. setLineWidth( linewidth );
  508. setLineCap( linecap );
  509. setLineJoin( linejoin );
  510. setStrokeStyle( color.getStyle() );
  511. _context.stroke();
  512. _elemBox.expandByScalar( linewidth * 2 );
  513. }
  514. function fillPath( color ) {
  515. setFillStyle( color.getStyle() );
  516. _context.fill();
  517. }
  518. function textureToPattern( texture ) {
  519. if ( texture.version === 0 ||
  520. texture instanceof THREE.CompressedTexture ||
  521. texture instanceof THREE.DataTexture ) {
  522. return {
  523. canvas: undefined,
  524. version: texture.version
  525. };
  526. }
  527. var image = texture.image;
  528. if ( image.complete === false ) {
  529. return {
  530. canvas: undefined,
  531. version: 0
  532. };
  533. }
  534. var repeatX = texture.wrapS === THREE.RepeatWrapping || texture.wrapS === THREE.MirroredRepeatWrapping;
  535. var repeatY = texture.wrapT === THREE.RepeatWrapping || texture.wrapT === THREE.MirroredRepeatWrapping;
  536. var mirrorX = texture.wrapS === THREE.MirroredRepeatWrapping;
  537. var mirrorY = texture.wrapT === THREE.MirroredRepeatWrapping;
  538. //
  539. var canvas = document.createElement( 'canvas' );
  540. canvas.width = image.width * ( mirrorX ? 2 : 1 );
  541. canvas.height = image.height * ( mirrorY ? 2 : 1 );
  542. var context = canvas.getContext( '2d' );
  543. context.setTransform( 1, 0, 0, - 1, 0, image.height );
  544. context.drawImage( image, 0, 0 );
  545. if ( mirrorX === true ) {
  546. context.setTransform( - 1, 0, 0, - 1, image.width, image.height );
  547. context.drawImage( image, - image.width, 0 );
  548. }
  549. if ( mirrorY === true ) {
  550. context.setTransform( 1, 0, 0, 1, 0, 0 );
  551. context.drawImage( image, 0, image.height );
  552. }
  553. if ( mirrorX === true && mirrorY === true ) {
  554. context.setTransform( - 1, 0, 0, 1, image.width, 0 );
  555. context.drawImage( image, - image.width, image.height );
  556. }
  557. var repeat = 'no-repeat';
  558. if ( repeatX === true && repeatY === true ) {
  559. repeat = 'repeat';
  560. } else if ( repeatX === true ) {
  561. repeat = 'repeat-x';
  562. } else if ( repeatY === true ) {
  563. repeat = 'repeat-y';
  564. }
  565. var pattern = _context.createPattern( canvas, repeat );
  566. if ( texture.onUpdate ) texture.onUpdate( texture );
  567. return {
  568. canvas: pattern,
  569. version: texture.version
  570. };
  571. }
  572. function patternPath( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, texture ) {
  573. var pattern = _patterns[ texture.id ];
  574. if ( pattern === undefined || pattern.version !== texture.version ) {
  575. pattern = textureToPattern( texture );
  576. _patterns[ texture.id ] = pattern;
  577. }
  578. if ( pattern.canvas !== undefined ) {
  579. setFillStyle( pattern.canvas );
  580. } else {
  581. setFillStyle( 'rgba( 0, 0, 0, 1)' );
  582. _context.fill();
  583. return;
  584. }
  585. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  586. var a, b, c, d, e, f, det, idet,
  587. offsetX = texture.offset.x / texture.repeat.x,
  588. offsetY = texture.offset.y / texture.repeat.y,
  589. width = texture.image.width * texture.repeat.x,
  590. height = texture.image.height * texture.repeat.y;
  591. u0 = ( u0 + offsetX ) * width;
  592. v0 = ( v0 + offsetY ) * height;
  593. u1 = ( u1 + offsetX ) * width;
  594. v1 = ( v1 + offsetY ) * height;
  595. u2 = ( u2 + offsetX ) * width;
  596. v2 = ( v2 + offsetY ) * height;
  597. x1 -= x0; y1 -= y0;
  598. x2 -= x0; y2 -= y0;
  599. u1 -= u0; v1 -= v0;
  600. u2 -= u0; v2 -= v0;
  601. det = u1 * v2 - u2 * v1;
  602. if ( det === 0 ) return;
  603. idet = 1 / det;
  604. a = ( v2 * x1 - v1 * x2 ) * idet;
  605. b = ( v2 * y1 - v1 * y2 ) * idet;
  606. c = ( u1 * x2 - u2 * x1 ) * idet;
  607. d = ( u1 * y2 - u2 * y1 ) * idet;
  608. e = x0 - a * u0 - c * v0;
  609. f = y0 - b * u0 - d * v0;
  610. _context.save();
  611. _context.transform( a, b, c, d, e, f );
  612. _context.fill();
  613. _context.restore();
  614. }
  615. function clipImage( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, image ) {
  616. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  617. var a, b, c, d, e, f, det, idet,
  618. width = image.width - 1,
  619. height = image.height - 1;
  620. u0 *= width; v0 *= height;
  621. u1 *= width; v1 *= height;
  622. u2 *= width; v2 *= height;
  623. x1 -= x0; y1 -= y0;
  624. x2 -= x0; y2 -= y0;
  625. u1 -= u0; v1 -= v0;
  626. u2 -= u0; v2 -= v0;
  627. det = u1 * v2 - u2 * v1;
  628. idet = 1 / det;
  629. a = ( v2 * x1 - v1 * x2 ) * idet;
  630. b = ( v2 * y1 - v1 * y2 ) * idet;
  631. c = ( u1 * x2 - u2 * x1 ) * idet;
  632. d = ( u1 * y2 - u2 * y1 ) * idet;
  633. e = x0 - a * u0 - c * v0;
  634. f = y0 - b * u0 - d * v0;
  635. _context.save();
  636. _context.transform( a, b, c, d, e, f );
  637. _context.clip();
  638. _context.drawImage( image, 0, 0 );
  639. _context.restore();
  640. }
  641. // Hide anti-alias gaps
  642. function expand( v1, v2, pixels ) {
  643. var x = v2.x - v1.x, y = v2.y - v1.y,
  644. det = x * x + y * y, idet;
  645. if ( det === 0 ) return;
  646. idet = pixels / Math.sqrt( det );
  647. x *= idet; y *= idet;
  648. v2.x += x; v2.y += y;
  649. v1.x -= x; v1.y -= y;
  650. }
  651. // Context cached methods.
  652. function setOpacity( value ) {
  653. if ( _contextGlobalAlpha !== value ) {
  654. _context.globalAlpha = value;
  655. _contextGlobalAlpha = value;
  656. }
  657. }
  658. function setBlending( value ) {
  659. if ( _contextGlobalCompositeOperation !== value ) {
  660. if ( value === THREE.NormalBlending ) {
  661. _context.globalCompositeOperation = 'source-over';
  662. } else if ( value === THREE.AdditiveBlending ) {
  663. _context.globalCompositeOperation = 'lighter';
  664. } else if ( value === THREE.SubtractiveBlending ) {
  665. _context.globalCompositeOperation = 'darker';
  666. } else if ( value === THREE.MultiplyBlending ) {
  667. _context.globalCompositeOperation = 'multiply';
  668. }
  669. _contextGlobalCompositeOperation = value;
  670. }
  671. }
  672. function setLineWidth( value ) {
  673. if ( _contextLineWidth !== value ) {
  674. _context.lineWidth = value;
  675. _contextLineWidth = value;
  676. }
  677. }
  678. function setLineCap( value ) {
  679. // "butt", "round", "square"
  680. if ( _contextLineCap !== value ) {
  681. _context.lineCap = value;
  682. _contextLineCap = value;
  683. }
  684. }
  685. function setLineJoin( value ) {
  686. // "round", "bevel", "miter"
  687. if ( _contextLineJoin !== value ) {
  688. _context.lineJoin = value;
  689. _contextLineJoin = value;
  690. }
  691. }
  692. function setStrokeStyle( value ) {
  693. if ( _contextStrokeStyle !== value ) {
  694. _context.strokeStyle = value;
  695. _contextStrokeStyle = value;
  696. }
  697. }
  698. function setFillStyle( value ) {
  699. if ( _contextFillStyle !== value ) {
  700. _context.fillStyle = value;
  701. _contextFillStyle = value;
  702. }
  703. }
  704. function setLineDash( value ) {
  705. if ( _contextLineDash.length !== value.length ) {
  706. _context.setLineDash( value );
  707. _contextLineDash = value;
  708. }
  709. }
  710. };