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