CanvasRenderer.js 25 KB

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