CanvasRenderer.js 24 KB

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