CanvasRenderer.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. _contextDashSize = null,
  31. _contextGapSize = 0,
  32. _camera,
  33. _v1, _v2, _v3, _v4,
  34. _v5 = new THREE.RenderableVertex(),
  35. _v6 = new THREE.RenderableVertex(),
  36. _v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
  37. _v4x, _v4y, _v5x, _v5y, _v6x, _v6y,
  38. _color = new THREE.Color(),
  39. _color1 = new THREE.Color(),
  40. _color2 = new THREE.Color(),
  41. _color3 = new THREE.Color(),
  42. _color4 = new THREE.Color(),
  43. _diffuseColor = new THREE.Color(),
  44. _emissiveColor = new THREE.Color(),
  45. _lightColor = new THREE.Color(),
  46. _patterns = {},
  47. _image, _uvs,
  48. _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y,
  49. _clipBox = new THREE.Box2(),
  50. _clearBox = new THREE.Box2(),
  51. _elemBox = new THREE.Box2(),
  52. _ambientLight = new THREE.Color(),
  53. _directionalLights = new THREE.Color(),
  54. _pointLights = new THREE.Color(),
  55. _vector3 = new THREE.Vector3(), // Needed for PointLight
  56. _centroid = new THREE.Vector3(),
  57. _normal = new THREE.Vector3(),
  58. _normalViewMatrix = new THREE.Matrix3();
  59. // dash+gap fallbacks for Firefox and everything else
  60. if ( _context.setLineDash === undefined ) {
  61. if ( _context.mozDash !== undefined ) {
  62. _context.setLineDash = function ( values ) {
  63. _context.mozDash = values[ 0 ] !== null ? values : null;
  64. }
  65. } else {
  66. _context.setLineDash = function () {}
  67. }
  68. }
  69. this.domElement = _canvas;
  70. this.devicePixelRatio = parameters.devicePixelRatio !== undefined
  71. ? parameters.devicePixelRatio
  72. : self.devicePixelRatio !== undefined
  73. ? self.devicePixelRatio
  74. : 1;
  75. this.autoClear = true;
  76. this.sortObjects = true;
  77. this.sortElements = true;
  78. this.info = {
  79. render: {
  80. vertices: 0,
  81. faces: 0
  82. }
  83. }
  84. // WebGLRenderer compatibility
  85. this.supportsVertexTextures = function () {};
  86. this.setFaceCulling = function () {};
  87. this.setSize = function ( width, height, updateStyle ) {
  88. _canvasWidth = width * this.devicePixelRatio;
  89. _canvasHeight = height * this.devicePixelRatio;
  90. _canvas.width = _canvasWidth;
  91. _canvas.height = _canvasHeight;
  92. _canvasWidthHalf = Math.floor( _canvasWidth / 2 );
  93. _canvasHeightHalf = Math.floor( _canvasHeight / 2 );
  94. if ( this.devicePixelRatio !== 1 && updateStyle !== false ) {
  95. _canvas.style.width = width + 'px';
  96. _canvas.style.height = height + 'px';
  97. }
  98. _clipBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf ),
  99. _clipBox.max.set( _canvasWidthHalf, _canvasHeightHalf );
  100. _clearBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf );
  101. _clearBox.max.set( _canvasWidthHalf, _canvasHeightHalf );
  102. _contextGlobalAlpha = 1;
  103. _contextGlobalCompositeOperation = 0;
  104. _contextStrokeStyle = null;
  105. _contextFillStyle = null;
  106. _contextLineWidth = null;
  107. _contextLineCap = null;
  108. _contextLineJoin = null;
  109. this.setViewport( 0, 0, width, height );
  110. };
  111. this.setViewport = function ( x, y, width, height ) {
  112. _context.setTransform( width / _canvasWidth, 0, 0, - height / _canvasHeight, x, _canvasHeight - y );
  113. _context.translate( _canvasWidthHalf, _canvasHeightHalf );
  114. };
  115. this.setScissor = function () {};
  116. this.enableScissorTest = function () {};
  117. this.setClearColor = function ( color, alpha ) {
  118. _clearColor.set( color );
  119. _clearAlpha = alpha !== undefined ? alpha : 1;
  120. _clearBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf );
  121. _clearBox.max.set( _canvasWidthHalf, _canvasHeightHalf );
  122. };
  123. this.setClearColorHex = function ( hex, alpha ) {
  124. console.warn( 'DEPRECATED: .setClearColorHex() is being removed. Use .setClearColor() instead.' );
  125. this.setClearColor( hex, alpha );
  126. };
  127. this.getMaxAnisotropy = function () {
  128. return 0;
  129. };
  130. this.clear = function () {
  131. if ( _clearBox.empty() === false ) {
  132. _clearBox.intersect( _clipBox );
  133. _clearBox.expandByScalar( 2 );
  134. if ( _clearAlpha < 1 ) {
  135. _context.clearRect(
  136. _clearBox.min.x | 0,
  137. _clearBox.min.y | 0,
  138. ( _clearBox.max.x - _clearBox.min.x ) | 0,
  139. ( _clearBox.max.y - _clearBox.min.y ) | 0
  140. );
  141. }
  142. if ( _clearAlpha > 0 ) {
  143. setBlending( THREE.NormalBlending );
  144. setOpacity( 1 );
  145. setFillStyle( 'rgba(' + Math.floor( _clearColor.r * 255 ) + ',' + Math.floor( _clearColor.g * 255 ) + ',' + Math.floor( _clearColor.b * 255 ) + ',' + _clearAlpha + ')' );
  146. _context.fillRect(
  147. _clearBox.min.x | 0,
  148. _clearBox.min.y | 0,
  149. ( _clearBox.max.x - _clearBox.min.x ) | 0,
  150. ( _clearBox.max.y - _clearBox.min.y ) | 0
  151. );
  152. }
  153. _clearBox.makeEmpty();
  154. }
  155. };
  156. // compatibility
  157. this.clearColor = function () {};
  158. this.clearDepth = function () {};
  159. this.clearStencil = function () {};
  160. this.render = function ( scene, camera ) {
  161. if ( camera instanceof THREE.Camera === false ) {
  162. console.error( 'THREE.CanvasRenderer.render: camera is not an instance of THREE.Camera.' );
  163. return;
  164. }
  165. if ( this.autoClear === true ) this.clear();
  166. _this.info.render.vertices = 0;
  167. _this.info.render.faces = 0;
  168. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  169. _elements = _renderData.elements;
  170. _lights = _renderData.lights;
  171. _camera = camera;
  172. _normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );
  173. /* DEBUG
  174. setFillStyle( 'rgba( 0, 255, 255, 0.5 )' );
  175. _context.fillRect( _clipBox.min.x, _clipBox.min.y, _clipBox.max.x - _clipBox.min.x, _clipBox.max.y - _clipBox.min.y );
  176. */
  177. calculateLights();
  178. for ( var e = 0, el = _elements.length; e < el; e ++ ) {
  179. var element = _elements[ e ];
  180. var material = element.material;
  181. if ( material === undefined || material.visible === false ) continue;
  182. _elemBox.makeEmpty();
  183. if ( element instanceof THREE.RenderableSprite ) {
  184. _v1 = element;
  185. _v1.x *= _canvasWidthHalf; _v1.y *= _canvasHeightHalf;
  186. renderSprite( _v1, element, material );
  187. } else if ( element instanceof THREE.RenderableLine ) {
  188. _v1 = element.v1; _v2 = element.v2;
  189. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  190. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  191. _elemBox.setFromPoints( [
  192. _v1.positionScreen,
  193. _v2.positionScreen
  194. ] );
  195. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  196. renderLine( _v1, _v2, element, material );
  197. }
  198. } else if ( element instanceof THREE.RenderableFace ) {
  199. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  200. if ( _v1.positionScreen.z < -1 || _v1.positionScreen.z > 1 ) continue;
  201. if ( _v2.positionScreen.z < -1 || _v2.positionScreen.z > 1 ) continue;
  202. if ( _v3.positionScreen.z < -1 || _v3.positionScreen.z > 1 ) continue;
  203. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  204. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  205. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  206. if ( material.overdraw > 0 ) {
  207. expand( _v1.positionScreen, _v2.positionScreen, material.overdraw );
  208. expand( _v2.positionScreen, _v3.positionScreen, material.overdraw );
  209. expand( _v3.positionScreen, _v1.positionScreen, material.overdraw );
  210. }
  211. _elemBox.setFromPoints( [
  212. _v1.positionScreen,
  213. _v2.positionScreen,
  214. _v3.positionScreen
  215. ] );
  216. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  217. renderFace3( _v1, _v2, _v3, 0, 1, 2, element, material );
  218. }
  219. }
  220. /* DEBUG
  221. setLineWidth( 1 );
  222. setStrokeStyle( 'rgba( 0, 255, 0, 0.5 )' );
  223. _context.strokeRect( _elemBox.min.x, _elemBox.min.y, _elemBox.max.x - _elemBox.min.x, _elemBox.max.y - _elemBox.min.y );
  224. */
  225. _clearBox.union( _elemBox );
  226. }
  227. /* DEBUG
  228. setLineWidth( 1 );
  229. setStrokeStyle( 'rgba( 255, 0, 0, 0.5 )' );
  230. _context.strokeRect( _clearBox.min.x, _clearBox.min.y, _clearBox.max.x - _clearBox.min.x, _clearBox.max.y - _clearBox.min.y );
  231. */
  232. // _context.setTransform( 1, 0, 0, 1, 0, 0 );
  233. };
  234. //
  235. function calculateLights() {
  236. _ambientLight.setRGB( 0, 0, 0 );
  237. _directionalLights.setRGB( 0, 0, 0 );
  238. _pointLights.setRGB( 0, 0, 0 );
  239. for ( var l = 0, ll = _lights.length; l < ll; l ++ ) {
  240. var light = _lights[ l ];
  241. var lightColor = light.color;
  242. if ( light instanceof THREE.AmbientLight ) {
  243. _ambientLight.add( lightColor );
  244. } else if ( light instanceof THREE.DirectionalLight ) {
  245. // for sprites
  246. _directionalLights.add( lightColor );
  247. } else if ( light instanceof THREE.PointLight ) {
  248. // for sprites
  249. _pointLights.add( lightColor );
  250. }
  251. }
  252. }
  253. function calculateLight( position, normal, color ) {
  254. for ( var l = 0, ll = _lights.length; l < ll; l ++ ) {
  255. var light = _lights[ l ];
  256. _lightColor.copy( light.color );
  257. if ( light instanceof THREE.DirectionalLight ) {
  258. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize();
  259. var amount = normal.dot( lightPosition );
  260. if ( amount <= 0 ) continue;
  261. amount *= light.intensity;
  262. color.add( _lightColor.multiplyScalar( amount ) );
  263. } else if ( light instanceof THREE.PointLight ) {
  264. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
  265. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  266. if ( amount <= 0 ) continue;
  267. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  268. if ( amount == 0 ) continue;
  269. amount *= light.intensity;
  270. color.add( _lightColor.multiplyScalar( amount ) );
  271. }
  272. }
  273. }
  274. function renderSprite( v1, element, material ) {
  275. setOpacity( material.opacity );
  276. setBlending( material.blending );
  277. var scaleX = element.scale.x * _canvasWidthHalf;
  278. var scaleY = element.scale.y * _canvasHeightHalf;
  279. var dist = 0.5 * Math.sqrt( scaleX * scaleX + scaleY * scaleY ); // allow for rotated sprite
  280. _elemBox.min.set( v1.x - dist, v1.y - dist );
  281. _elemBox.max.set( v1.x + dist, v1.y + dist );
  282. if ( material instanceof THREE.SpriteMaterial ||
  283. material instanceof THREE.ParticleSystemMaterial ) { // Backwards compatibility
  284. var texture = material.map;
  285. if ( texture !== null ) {
  286. if ( texture.hasEventListener( 'update', onTextureUpdate ) === false ) {
  287. if ( texture.image !== undefined && texture.image.width > 0 ) {
  288. textureToPattern( texture );
  289. }
  290. texture.addEventListener( 'update', onTextureUpdate );
  291. }
  292. var pattern = _patterns[ texture.id ];
  293. if ( pattern !== undefined ) {
  294. setFillStyle( pattern );
  295. } else {
  296. setFillStyle( 'rgba( 0, 0, 0, 1 )' );
  297. }
  298. //
  299. var bitmap = texture.image;
  300. var ox = bitmap.width * texture.offset.x;
  301. var oy = bitmap.height * texture.offset.y;
  302. var sx = bitmap.width * texture.repeat.x;
  303. var sy = bitmap.height * texture.repeat.y;
  304. var cx = scaleX / sx;
  305. var cy = scaleY / sy;
  306. _context.save();
  307. _context.translate( v1.x, v1.y );
  308. if ( material.rotation !== 0 ) _context.rotate( material.rotation );
  309. _context.translate( - scaleX / 2, - scaleY / 2 );
  310. _context.scale( cx, cy );
  311. _context.translate( - ox, - oy );
  312. _context.fillRect( ox, oy, sx, sy );
  313. _context.restore();
  314. } else { // no texture
  315. setFillStyle( material.color.getStyle() );
  316. _context.save();
  317. _context.translate( v1.x, v1.y );
  318. if ( material.rotation !== 0 ) _context.rotate( material.rotation );
  319. _context.scale( scaleX, - scaleY );
  320. _context.fillRect( - 0.5, - 0.5, 1, 1 );
  321. _context.restore();
  322. }
  323. } else if ( material instanceof THREE.SpriteCanvasMaterial ) {
  324. setStrokeStyle( material.color.getStyle() );
  325. setFillStyle( material.color.getStyle() );
  326. _context.save();
  327. _context.translate( v1.x, v1.y );
  328. if ( material.rotation !== 0 ) _context.rotate( material.rotation );
  329. _context.scale( scaleX, scaleY );
  330. material.program( _context );
  331. _context.restore();
  332. }
  333. /* DEBUG
  334. setStrokeStyle( 'rgb(255,255,0)' );
  335. _context.beginPath();
  336. _context.moveTo( v1.x - 10, v1.y );
  337. _context.lineTo( v1.x + 10, v1.y );
  338. _context.moveTo( v1.x, v1.y - 10 );
  339. _context.lineTo( v1.x, v1.y + 10 );
  340. _context.stroke();
  341. */
  342. }
  343. function renderLine( v1, v2, element, material ) {
  344. setOpacity( material.opacity );
  345. setBlending( material.blending );
  346. _context.beginPath();
  347. _context.moveTo( v1.positionScreen.x, v1.positionScreen.y );
  348. _context.lineTo( v2.positionScreen.x, v2.positionScreen.y );
  349. if ( material instanceof THREE.LineBasicMaterial ) {
  350. setLineWidth( material.linewidth );
  351. setLineCap( material.linecap );
  352. setLineJoin( material.linejoin );
  353. if ( material.vertexColors !== THREE.VertexColors ) {
  354. setStrokeStyle( material.color.getStyle() );
  355. } else {
  356. var colorStyle1 = element.vertexColors[0].getStyle();
  357. var colorStyle2 = element.vertexColors[1].getStyle();
  358. if ( colorStyle1 === colorStyle2 ) {
  359. setStrokeStyle( colorStyle1 );
  360. } else {
  361. try {
  362. var grad = _context.createLinearGradient(
  363. v1.positionScreen.x,
  364. v1.positionScreen.y,
  365. v2.positionScreen.x,
  366. v2.positionScreen.y
  367. );
  368. grad.addColorStop( 0, colorStyle1 );
  369. grad.addColorStop( 1, colorStyle2 );
  370. } catch ( exception ) {
  371. grad = colorStyle1;
  372. }
  373. setStrokeStyle( grad );
  374. }
  375. }
  376. _context.stroke();
  377. _elemBox.expandByScalar( material.linewidth * 2 );
  378. } else if ( material instanceof THREE.LineDashedMaterial ) {
  379. setLineWidth( material.linewidth );
  380. setLineCap( material.linecap );
  381. setLineJoin( material.linejoin );
  382. setStrokeStyle( material.color.getStyle() );
  383. setDashAndGap( material.dashSize, material.gapSize );
  384. _context.stroke();
  385. _elemBox.expandByScalar( material.linewidth * 2 );
  386. setDashAndGap( null, null );
  387. }
  388. }
  389. function renderFace3( v1, v2, v3, uv1, uv2, uv3, element, material ) {
  390. _this.info.render.vertices += 3;
  391. _this.info.render.faces ++;
  392. setOpacity( material.opacity );
  393. setBlending( material.blending );
  394. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  395. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  396. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  397. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y );
  398. if ( ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) && material.map === null ) {
  399. _diffuseColor.copy( material.color );
  400. _emissiveColor.copy( material.emissive );
  401. if ( material.vertexColors === THREE.FaceColors ) {
  402. _diffuseColor.multiply( element.color );
  403. }
  404. _color.copy( _ambientLight );
  405. _centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );
  406. calculateLight( _centroid, element.normalModel, _color );
  407. _color.multiply( _diffuseColor ).add( _emissiveColor );
  408. material.wireframe === true
  409. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  410. : fillPath( _color );
  411. } else if ( material instanceof THREE.MeshBasicMaterial || material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
  412. if ( material.map !== null ) {
  413. if ( material.map.mapping instanceof THREE.UVMapping ) {
  414. _uvs = element.uvs;
  415. 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 );
  416. }
  417. } else if ( material.envMap !== null ) {
  418. if ( material.envMap.mapping instanceof THREE.SphericalReflectionMapping ) {
  419. _normal.copy( element.vertexNormalsModel[ uv1 ] ).applyMatrix3( _normalViewMatrix );
  420. _uv1x = 0.5 * _normal.x + 0.5;
  421. _uv1y = 0.5 * _normal.y + 0.5;
  422. _normal.copy( element.vertexNormalsModel[ uv2 ] ).applyMatrix3( _normalViewMatrix );
  423. _uv2x = 0.5 * _normal.x + 0.5;
  424. _uv2y = 0.5 * _normal.y + 0.5;
  425. _normal.copy( element.vertexNormalsModel[ uv3 ] ).applyMatrix3( _normalViewMatrix );
  426. _uv3x = 0.5 * _normal.x + 0.5;
  427. _uv3y = 0.5 * _normal.y + 0.5;
  428. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y, material.envMap );
  429. } else if ( material.envMap.mapping instanceof THREE.SphericalRefractionMapping ) {
  430. _normal.copy( element.vertexNormalsModel[ uv1 ] ).applyMatrix3( _normalViewMatrix );
  431. _uv1x = - 0.5 * _normal.x + 0.5;
  432. _uv1y = - 0.5 * _normal.y + 0.5;
  433. _normal.copy( element.vertexNormalsModel[ uv2 ] ).applyMatrix3( _normalViewMatrix );
  434. _uv2x = - 0.5 * _normal.x + 0.5;
  435. _uv2y = - 0.5 * _normal.y + 0.5;
  436. _normal.copy( element.vertexNormalsModel[ uv3 ] ).applyMatrix3( _normalViewMatrix );
  437. _uv3x = - 0.5 * _normal.x + 0.5;
  438. _uv3y = - 0.5 * _normal.y + 0.5;
  439. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y, material.envMap );
  440. }
  441. } else {
  442. _color.copy( material.color );
  443. if ( material.vertexColors === THREE.FaceColors ) {
  444. _color.multiply( element.color );
  445. }
  446. material.wireframe === true
  447. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  448. : fillPath( _color );
  449. }
  450. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  451. _color.r = _color.g = _color.b = 1 - smoothstep( v1.positionScreen.z * v1.positionScreen.w, _camera.near, _camera.far );
  452. material.wireframe === true
  453. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  454. : fillPath( _color );
  455. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  456. _normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
  457. _color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  458. material.wireframe === true
  459. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  460. : fillPath( _color );
  461. } else {
  462. _color.setRGB( 1, 1, 1 );
  463. material.wireframe === true
  464. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  465. : fillPath( _color );
  466. }
  467. }
  468. //
  469. function drawTriangle( x0, y0, x1, y1, x2, y2 ) {
  470. _context.beginPath();
  471. _context.moveTo( x0, y0 );
  472. _context.lineTo( x1, y1 );
  473. _context.lineTo( x2, y2 );
  474. _context.closePath();
  475. }
  476. function strokePath( color, linewidth, linecap, linejoin ) {
  477. setLineWidth( linewidth );
  478. setLineCap( linecap );
  479. setLineJoin( linejoin );
  480. setStrokeStyle( color.getStyle() );
  481. _context.stroke();
  482. _elemBox.expandByScalar( linewidth * 2 );
  483. }
  484. function fillPath( color ) {
  485. setFillStyle( color.getStyle() );
  486. _context.fill();
  487. }
  488. function onTextureUpdate ( event ) {
  489. textureToPattern( event.target );
  490. }
  491. function textureToPattern( texture ) {
  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 setDashAndGap( dashSizeValue, gapSizeValue ) {
  645. if ( _contextDashSize !== dashSizeValue || _contextGapSize !== gapSizeValue ) {
  646. _context.setLineDash( [ dashSizeValue, gapSizeValue ] );
  647. _contextDashSize = dashSizeValue;
  648. _contextGapSize = gapSizeValue;
  649. }
  650. }
  651. };