CanvasRenderer.js 24 KB

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