CanvasRenderer.js 25 KB

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