CanvasRenderer.js 24 KB

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