CanvasRender.js 25 KB

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