CanvasRenderer.js 24 KB

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