CanvasRenderer.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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, _canvasHeight, _canvasWidthHalf, _canvasHeightHalf,
  15. _context = _canvas.getContext( '2d' ),
  16. _clearColor = new THREE.Color( 0x000000 ),
  17. _clearAlpha = 0,
  18. _contextGlobalAlpha = 1,
  19. _contextGlobalCompositeOperation = 0,
  20. _contextStrokeStyle = null,
  21. _contextFillStyle = null,
  22. _contextLineWidth = null,
  23. _contextLineCap = null,
  24. _contextLineJoin = null,
  25. _contextDashSize = null,
  26. _contextGapSize = 0,
  27. _v1, _v2, _v3, _v4,
  28. _v5 = new THREE.RenderableVertex(),
  29. _v6 = new THREE.RenderableVertex(),
  30. _v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
  31. _v4x, _v4y, _v5x, _v5y, _v6x, _v6y,
  32. _color = new THREE.Color(),
  33. _color1 = new THREE.Color(),
  34. _color2 = new THREE.Color(),
  35. _color3 = new THREE.Color(),
  36. _color4 = new THREE.Color(),
  37. _diffuseColor = new THREE.Color(),
  38. _emissiveColor = new THREE.Color(),
  39. _lightColor = new THREE.Color(),
  40. _patterns = {}, _imagedatas = {},
  41. _near, _far,
  42. _image, _uvs,
  43. _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y,
  44. _clipBox = new THREE.Box2(),
  45. _clearBox = new THREE.Box2(),
  46. _elemBox = new THREE.Box2(),
  47. _ambientLight = new THREE.Color(),
  48. _directionalLights = new THREE.Color(),
  49. _pointLights = new THREE.Color(),
  50. _vector3 = new THREE.Vector3(), // Needed for PointLight
  51. _pixelMap, _pixelMapContext, _pixelMapImage, _pixelMapData,
  52. _gradientMap, _gradientMapContext, _gradientMapQuality = 16;
  53. _pixelMap = document.createElement( 'canvas' );
  54. _pixelMap.width = _pixelMap.height = 2;
  55. _pixelMapContext = _pixelMap.getContext( '2d' );
  56. _pixelMapContext.fillStyle = 'rgba(0,0,0,1)';
  57. _pixelMapContext.fillRect( 0, 0, 2, 2 );
  58. _pixelMapImage = _pixelMapContext.getImageData( 0, 0, 2, 2 );
  59. _pixelMapData = _pixelMapImage.data;
  60. _gradientMap = document.createElement( 'canvas' );
  61. _gradientMap.width = _gradientMap.height = _gradientMapQuality;
  62. _gradientMapContext = _gradientMap.getContext( '2d' );
  63. _gradientMapContext.translate( - _gradientMapQuality / 2, - _gradientMapQuality / 2 );
  64. _gradientMapContext.scale( _gradientMapQuality, _gradientMapQuality );
  65. _gradientMapQuality --; // Fix UVs
  66. // dash+gap fallbacks for Firefox and everything else
  67. if ( _context.setLineDash === undefined ) {
  68. if ( _context.mozDash !== undefined ) {
  69. _context.setLineDash = function ( values ) {
  70. _context.mozDash = values[ 0 ] !== null ? values : null;
  71. }
  72. } else {
  73. _context.setLineDash = function () {}
  74. }
  75. }
  76. this.domElement = _canvas;
  77. this.devicePixelRatio = parameters.devicePixelRatio !== undefined
  78. ? parameters.devicePixelRatio
  79. : window.devicePixelRatio !== undefined
  80. ? window.devicePixelRatio
  81. : 1;
  82. this.autoClear = true;
  83. this.sortObjects = true;
  84. this.sortElements = true;
  85. this.info = {
  86. render: {
  87. vertices: 0,
  88. faces: 0
  89. }
  90. }
  91. // WebGLRenderer compatibility
  92. this.supportsVertexTextures = function () {};
  93. this.setFaceCulling = function () {};
  94. this.setSize = function ( width, height, updateStyle ) {
  95. _canvasWidth = width * this.devicePixelRatio;
  96. _canvasHeight = height * this.devicePixelRatio;
  97. _canvasWidthHalf = Math.floor( _canvasWidth / 2 );
  98. _canvasHeightHalf = Math.floor( _canvasHeight / 2 );
  99. _canvas.width = _canvasWidth;
  100. _canvas.height = _canvasHeight;
  101. if ( this.devicePixelRatio !== 1 && updateStyle !== false ) {
  102. _canvas.style.width = width + 'px';
  103. _canvas.style.height = height + 'px';
  104. }
  105. _clipBox.set(
  106. new THREE.Vector2( - _canvasWidthHalf, - _canvasHeightHalf ),
  107. new THREE.Vector2( _canvasWidthHalf, _canvasHeightHalf )
  108. );
  109. _clearBox.set(
  110. new THREE.Vector2( - _canvasWidthHalf, - _canvasHeightHalf ),
  111. new THREE.Vector2( _canvasWidthHalf, _canvasHeightHalf )
  112. );
  113. _contextGlobalAlpha = 1;
  114. _contextGlobalCompositeOperation = 0;
  115. _contextStrokeStyle = null;
  116. _contextFillStyle = null;
  117. _contextLineWidth = null;
  118. _contextLineCap = null;
  119. _contextLineJoin = null;
  120. };
  121. this.setClearColor = function ( color, alpha ) {
  122. _clearColor.set( color );
  123. _clearAlpha = alpha !== undefined ? alpha : 1;
  124. _clearBox.set(
  125. new THREE.Vector2( - _canvasWidthHalf, - _canvasHeightHalf ),
  126. new THREE.Vector2( _canvasWidthHalf, _canvasHeightHalf )
  127. );
  128. };
  129. this.setClearColorHex = function ( hex, alpha ) {
  130. console.warn( 'DEPRECATED: .setClearColorHex() is being removed. Use .setClearColor() instead.' );
  131. this.setClearColor( hex, alpha );
  132. };
  133. this.getMaxAnisotropy = function () {
  134. return 0;
  135. };
  136. this.clear = function () {
  137. _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf );
  138. if ( _clearBox.empty() === false ) {
  139. _clearBox.intersect( _clipBox );
  140. _clearBox.expandByScalar( 2 );
  141. if ( _clearAlpha < 1 ) {
  142. _context.clearRect(
  143. _clearBox.min.x | 0,
  144. _clearBox.min.y | 0,
  145. ( _clearBox.max.x - _clearBox.min.x ) | 0,
  146. ( _clearBox.max.y - _clearBox.min.y ) | 0
  147. );
  148. }
  149. if ( _clearAlpha > 0 ) {
  150. setBlending( THREE.NormalBlending );
  151. setOpacity( 1 );
  152. setFillStyle( 'rgba(' + Math.floor( _clearColor.r * 255 ) + ',' + Math.floor( _clearColor.g * 255 ) + ',' + Math.floor( _clearColor.b * 255 ) + ',' + _clearAlpha + ')' );
  153. _context.fillRect(
  154. _clearBox.min.x | 0,
  155. _clearBox.min.y | 0,
  156. ( _clearBox.max.x - _clearBox.min.x ) | 0,
  157. ( _clearBox.max.y - _clearBox.min.y ) | 0
  158. );
  159. }
  160. _clearBox.makeEmpty();
  161. }
  162. };
  163. this.render = function ( scene, camera ) {
  164. if ( camera instanceof THREE.Camera === false ) {
  165. console.error( 'THREE.CanvasRenderer.render: camera is not an instance of THREE.Camera.' );
  166. return;
  167. }
  168. if ( this.autoClear === true ) this.clear();
  169. _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf );
  170. _this.info.render.vertices = 0;
  171. _this.info.render.faces = 0;
  172. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  173. _elements = _renderData.elements;
  174. _lights = _renderData.lights;
  175. /* DEBUG
  176. setFillStyle( 'rgba( 0, 255, 255, 0.5 )' );
  177. _context.fillRect( _clipBox.min.x, _clipBox.min.y, _clipBox.max.x - _clipBox.min.x, _clipBox.max.y - _clipBox.min.y );
  178. */
  179. calculateLights();
  180. for ( var e = 0, el = _elements.length; e < el; e++ ) {
  181. var element = _elements[ e ];
  182. var material = element.material;
  183. if ( material === undefined || material.visible === false ) continue;
  184. _elemBox.makeEmpty();
  185. if ( element instanceof THREE.RenderableParticle ) {
  186. _v1 = element;
  187. _v1.x *= _canvasWidthHalf; _v1.y *= _canvasHeightHalf;
  188. renderParticle( _v1, element, material );
  189. } else if ( element instanceof THREE.RenderableLine ) {
  190. _v1 = element.v1; _v2 = element.v2;
  191. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  192. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  193. _elemBox.setFromPoints( [
  194. _v1.positionScreen,
  195. _v2.positionScreen
  196. ] );
  197. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  198. renderLine( _v1, _v2, element, material );
  199. }
  200. } else if ( element instanceof THREE.RenderableFace3 ) {
  201. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  202. if ( _v1.positionScreen.z < -1 || _v1.positionScreen.z > 1 ) continue;
  203. if ( _v2.positionScreen.z < -1 || _v2.positionScreen.z > 1 ) continue;
  204. if ( _v3.positionScreen.z < -1 || _v3.positionScreen.z > 1 ) continue;
  205. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  206. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  207. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  208. if ( material.overdraw === true ) {
  209. expand( _v1.positionScreen, _v2.positionScreen );
  210. expand( _v2.positionScreen, _v3.positionScreen );
  211. expand( _v3.positionScreen, _v1.positionScreen );
  212. }
  213. _elemBox.setFromPoints( [
  214. _v1.positionScreen,
  215. _v2.positionScreen,
  216. _v3.positionScreen
  217. ] );
  218. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  219. renderFace3( _v1, _v2, _v3, 0, 1, 2, element, material );
  220. }
  221. } else if ( element instanceof THREE.RenderableFace4 ) {
  222. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3; _v4 = element.v4;
  223. if ( _v1.positionScreen.z < -1 || _v1.positionScreen.z > 1 ) continue;
  224. if ( _v2.positionScreen.z < -1 || _v2.positionScreen.z > 1 ) continue;
  225. if ( _v3.positionScreen.z < -1 || _v3.positionScreen.z > 1 ) continue;
  226. if ( _v4.positionScreen.z < -1 || _v4.positionScreen.z > 1 ) continue;
  227. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  228. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  229. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  230. _v4.positionScreen.x *= _canvasWidthHalf; _v4.positionScreen.y *= _canvasHeightHalf;
  231. _v5.positionScreen.copy( _v2.positionScreen );
  232. _v6.positionScreen.copy( _v4.positionScreen );
  233. if ( material.overdraw === true ) {
  234. expand( _v1.positionScreen, _v2.positionScreen );
  235. expand( _v2.positionScreen, _v4.positionScreen );
  236. expand( _v4.positionScreen, _v1.positionScreen );
  237. expand( _v3.positionScreen, _v5.positionScreen );
  238. expand( _v3.positionScreen, _v6.positionScreen );
  239. }
  240. _elemBox.setFromPoints( [
  241. _v1.positionScreen,
  242. _v2.positionScreen,
  243. _v3.positionScreen,
  244. _v4.positionScreen
  245. ] );
  246. if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
  247. renderFace4( _v1, _v2, _v3, _v4, _v5, _v6, element, material );
  248. }
  249. }
  250. /* DEBUG
  251. setLineWidth( 1 );
  252. setStrokeStyle( 'rgba( 0, 255, 0, 0.5 )' );
  253. _context.strokeRect( _elemBox.min.x, _elemBox.min.y, _elemBox.max.x - _elemBox.min.x, _elemBox.max.y - _elemBox.min.y );
  254. */
  255. _clearBox.union( _elemBox );
  256. }
  257. /* DEBUG
  258. setLineWidth( 1 );
  259. setStrokeStyle( 'rgba( 255, 0, 0, 0.5 )' );
  260. _context.strokeRect( _clearBox.min.x, _clearBox.min.y, _clearBox.max.x - _clearBox.min.x, _clearBox.max.y - _clearBox.min.y );
  261. */
  262. _context.setTransform( 1, 0, 0, 1, 0, 0 );
  263. };
  264. //
  265. function calculateLights() {
  266. _ambientLight.setRGB( 0, 0, 0 );
  267. _directionalLights.setRGB( 0, 0, 0 );
  268. _pointLights.setRGB( 0, 0, 0 );
  269. for ( var l = 0, ll = _lights.length; l < ll; l ++ ) {
  270. var light = _lights[ l ];
  271. var lightColor = light.color;
  272. if ( light instanceof THREE.AmbientLight ) {
  273. _ambientLight.add( lightColor );
  274. } else if ( light instanceof THREE.DirectionalLight ) {
  275. // for particles
  276. _directionalLights.add( lightColor );
  277. } else if ( light instanceof THREE.PointLight ) {
  278. // for particles
  279. _pointLights.add( lightColor );
  280. }
  281. }
  282. }
  283. function calculateLight( position, normal, color ) {
  284. for ( var l = 0, ll = _lights.length; l < ll; l ++ ) {
  285. var light = _lights[ l ];
  286. _lightColor.copy( light.color );
  287. if ( light instanceof THREE.DirectionalLight ) {
  288. var lightPosition = _vector3.getPositionFromMatrix( light.matrixWorld ).normalize();
  289. var amount = normal.dot( lightPosition );
  290. if ( amount <= 0 ) continue;
  291. amount *= light.intensity;
  292. color.add( _lightColor.multiplyScalar( amount ) );
  293. } else if ( light instanceof THREE.PointLight ) {
  294. var lightPosition = _vector3.getPositionFromMatrix( light.matrixWorld );
  295. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  296. if ( amount <= 0 ) continue;
  297. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  298. if ( amount == 0 ) continue;
  299. amount *= light.intensity;
  300. color.add( _lightColor.multiplyScalar( amount ) );
  301. }
  302. }
  303. }
  304. function renderParticle( v1, element, material ) {
  305. setOpacity( material.opacity );
  306. setBlending( material.blending );
  307. var width, height, scaleX, scaleY,
  308. bitmap, bitmapWidth, bitmapHeight;
  309. if ( material instanceof THREE.ParticleBasicMaterial ) {
  310. if ( material.map === null ) {
  311. scaleX = element.object.scale.x;
  312. scaleY = element.object.scale.y;
  313. // TODO: Be able to disable this
  314. scaleX *= element.scale.x * _canvasWidthHalf;
  315. scaleY *= element.scale.y * _canvasHeightHalf;
  316. _elemBox.min.set( v1.x - scaleX, v1.y - scaleY );
  317. _elemBox.max.set( v1.x + scaleX, v1.y + scaleY );
  318. if ( _clipBox.isIntersectionBox( _elemBox ) === false ) {
  319. _elemBox.makeEmpty();
  320. return;
  321. }
  322. setFillStyle( material.color.getStyle() );
  323. _context.save();
  324. _context.translate( v1.x, v1.y );
  325. _context.rotate( - element.rotation );
  326. _context.scale( scaleX, scaleY );
  327. _context.fillRect( -1, -1, 2, 2 );
  328. _context.restore();
  329. } else {
  330. bitmap = material.map.image;
  331. bitmapWidth = bitmap.width >> 1;
  332. bitmapHeight = bitmap.height >> 1;
  333. scaleX = element.scale.x * _canvasWidthHalf;
  334. scaleY = element.scale.y * _canvasHeightHalf;
  335. width = scaleX * bitmapWidth;
  336. height = scaleY * bitmapHeight;
  337. // TODO: Rotations break this...
  338. _elemBox.min.set( v1.x - width, v1.y - height );
  339. _elemBox.max.set( v1.x + width, v1.y + height );
  340. if ( _clipBox.isIntersectionBox( _elemBox ) === false ) {
  341. _elemBox.makeEmpty();
  342. return;
  343. }
  344. _context.save();
  345. _context.translate( v1.x, v1.y );
  346. _context.rotate( - element.rotation );
  347. _context.scale( scaleX, - scaleY );
  348. _context.translate( - bitmapWidth, - bitmapHeight );
  349. _context.drawImage( bitmap, 0, 0 );
  350. _context.restore();
  351. }
  352. /* DEBUG
  353. setStrokeStyle( 'rgb(255,255,0)' );
  354. _context.beginPath();
  355. _context.moveTo( v1.x - 10, v1.y );
  356. _context.lineTo( v1.x + 10, v1.y );
  357. _context.moveTo( v1.x, v1.y - 10 );
  358. _context.lineTo( v1.x, v1.y + 10 );
  359. _context.stroke();
  360. */
  361. } else if ( material instanceof THREE.ParticleCanvasMaterial ) {
  362. width = element.scale.x * _canvasWidthHalf;
  363. height = element.scale.y * _canvasHeightHalf;
  364. _elemBox.min.set( v1.x - width, v1.y - height );
  365. _elemBox.max.set( v1.x + width, v1.y + height );
  366. if ( _clipBox.isIntersectionBox( _elemBox ) === false ) {
  367. _elemBox.makeEmpty();
  368. return;
  369. }
  370. setStrokeStyle( material.color.getStyle() );
  371. setFillStyle( material.color.getStyle() );
  372. _context.save();
  373. _context.translate( v1.x, v1.y );
  374. _context.rotate( - element.rotation );
  375. _context.scale( width, height );
  376. material.program( _context );
  377. _context.restore();
  378. }
  379. }
  380. function renderLine( v1, v2, element, material ) {
  381. setOpacity( material.opacity );
  382. setBlending( material.blending );
  383. _context.beginPath();
  384. _context.moveTo( v1.positionScreen.x, v1.positionScreen.y );
  385. _context.lineTo( v2.positionScreen.x, v2.positionScreen.y );
  386. if ( material instanceof THREE.LineBasicMaterial ) {
  387. setLineWidth( material.linewidth );
  388. setLineCap( material.linecap );
  389. setLineJoin( material.linejoin );
  390. if ( material.vertexColors !== THREE.VertexColors ) {
  391. setStrokeStyle( material.color.getStyle() );
  392. } else {
  393. var colorStyle1 = element.vertexColors[0].getStyle();
  394. var colorStyle2 = element.vertexColors[1].getStyle();
  395. if ( colorStyle1 === colorStyle2 ) {
  396. setStrokeStyle( colorStyle1 );
  397. } else {
  398. try {
  399. var grad = _context.createLinearGradient(
  400. v1.positionScreen.x,
  401. v1.positionScreen.y,
  402. v2.positionScreen.x,
  403. v2.positionScreen.y
  404. );
  405. grad.addColorStop( 0, colorStyle1 );
  406. grad.addColorStop( 1, colorStyle2 );
  407. } catch ( exception ) {
  408. grad = colorStyle1;
  409. }
  410. setStrokeStyle( grad );
  411. }
  412. }
  413. _context.stroke();
  414. _elemBox.expandByScalar( material.linewidth * 2 );
  415. } else if ( material instanceof THREE.LineDashedMaterial ) {
  416. setLineWidth( material.linewidth );
  417. setLineCap( material.linecap );
  418. setLineJoin( material.linejoin );
  419. setStrokeStyle( material.color.getStyle() );
  420. setDashAndGap( material.dashSize, material.gapSize );
  421. _context.stroke();
  422. _elemBox.expandByScalar( material.linewidth * 2 );
  423. setDashAndGap( null, null );
  424. }
  425. }
  426. function renderFace3( v1, v2, v3, uv1, uv2, uv3, element, material ) {
  427. _this.info.render.vertices += 3;
  428. _this.info.render.faces ++;
  429. setOpacity( material.opacity );
  430. setBlending( material.blending );
  431. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  432. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  433. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  434. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y );
  435. if ( ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) && material.map === null ) {
  436. _diffuseColor.copy( material.color );
  437. _emissiveColor.copy( material.emissive );
  438. if ( material.vertexColors === THREE.FaceColors ) {
  439. _diffuseColor.multiply( element.color );
  440. }
  441. if ( material.wireframe === false && material.shading == THREE.SmoothShading && element.vertexNormalsLength == 3 ) {
  442. _color1.copy( _ambientLight );
  443. _color2.copy( _ambientLight );
  444. _color3.copy( _ambientLight );
  445. calculateLight( element.v1.positionWorld, element.vertexNormalsModel[ 0 ], _color1 );
  446. calculateLight( element.v2.positionWorld, element.vertexNormalsModel[ 1 ], _color2 );
  447. calculateLight( element.v3.positionWorld, element.vertexNormalsModel[ 2 ], _color3 );
  448. _color1.multiply( _diffuseColor ).add( _emissiveColor );
  449. _color2.multiply( _diffuseColor ).add( _emissiveColor );
  450. _color3.multiply( _diffuseColor ).add( _emissiveColor );
  451. _color4.addColors( _color2, _color3 ).multiplyScalar( 0.5 );
  452. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  453. clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image );
  454. } else {
  455. _color.copy( _ambientLight );
  456. calculateLight( element.centroidModel, element.normalModel, _color );
  457. _color.multiply( _diffuseColor ).add( _emissiveColor );
  458. material.wireframe === true
  459. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  460. : fillPath( _color );
  461. }
  462. } else if ( material instanceof THREE.MeshBasicMaterial || material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
  463. if ( material.map !== null ) {
  464. if ( material.map.mapping instanceof THREE.UVMapping ) {
  465. _uvs = element.uvs[ 0 ];
  466. 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 );
  467. }
  468. } else if ( material.envMap !== null ) {
  469. if ( material.envMap.mapping instanceof THREE.SphericalReflectionMapping ) {
  470. _vector3.copy( element.vertexNormalsModelView[ uv1 ] );
  471. _uv1x = 0.5 * _vector3.x + 0.5;
  472. _uv1y = 0.5 * _vector3.y + 0.5;
  473. _vector3.copy( element.vertexNormalsModelView[ uv2 ] );
  474. _uv2x = 0.5 * _vector3.x + 0.5;
  475. _uv2y = 0.5 * _vector3.y + 0.5;
  476. _vector3.copy( element.vertexNormalsModelView[ uv3 ] );
  477. _uv3x = 0.5 * _vector3.x + 0.5;
  478. _uv3y = 0.5 * _vector3.y + 0.5;
  479. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y, material.envMap );
  480. }/* else if ( material.envMap.mapping == THREE.SphericalRefractionMapping ) {
  481. }*/
  482. } else {
  483. _color.copy( material.color );
  484. if ( material.vertexColors === THREE.FaceColors ) {
  485. _color.multiply( element.color );
  486. }
  487. material.wireframe === true
  488. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  489. : fillPath( _color );
  490. }
  491. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  492. _near = camera.near;
  493. _far = camera.far;
  494. _color1.r = _color1.g = _color1.b = 1 - smoothstep( v1.positionScreen.z * v1.positionScreen.w, _near, _far );
  495. _color2.r = _color2.g = _color2.b = 1 - smoothstep( v2.positionScreen.z * v2.positionScreen.w, _near, _far );
  496. _color3.r = _color3.g = _color3.b = 1 - smoothstep( v3.positionScreen.z * v3.positionScreen.w, _near, _far );
  497. _color4.addColors( _color2, _color3 ).multiplyScalar( 0.5 );
  498. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  499. clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image );
  500. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  501. var normal;
  502. if ( material.shading == THREE.FlatShading ) {
  503. normal = element.normalModelView;
  504. _color.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  505. material.wireframe === true
  506. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  507. : fillPath( _color );
  508. } else if ( material.shading == THREE.SmoothShading ) {
  509. normal = element.vertexNormalsModelView[ uv1 ];
  510. _color1.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  511. normal = element.vertexNormalsModelView[ uv2 ];
  512. _color2.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  513. normal = element.vertexNormalsModelView[ uv3 ];
  514. _color3.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  515. _color4.addColors( _color2, _color3 ).multiplyScalar( 0.5 );
  516. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  517. clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image );
  518. }
  519. }
  520. }
  521. function renderFace4( v1, v2, v3, v4, v5, v6, element, material ) {
  522. _this.info.render.vertices += 4;
  523. _this.info.render.faces ++;
  524. setOpacity( material.opacity );
  525. setBlending( material.blending );
  526. if ( ( material.map !== undefined && material.map !== null ) || ( material.envMap !== undefined && material.envMap !== null ) ) {
  527. // Let renderFace3() handle this
  528. renderFace3( v1, v2, v4, 0, 1, 3, element, material );
  529. renderFace3( v5, v3, v6, 1, 2, 3, element, material );
  530. return;
  531. }
  532. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  533. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  534. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  535. _v4x = v4.positionScreen.x; _v4y = v4.positionScreen.y;
  536. _v5x = v5.positionScreen.x; _v5y = v5.positionScreen.y;
  537. _v6x = v6.positionScreen.x; _v6y = v6.positionScreen.y;
  538. if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
  539. _diffuseColor.copy( material.color );
  540. _emissiveColor.copy( material.emissive );
  541. if ( material.vertexColors === THREE.FaceColors ) {
  542. _diffuseColor.multiply( element.color );
  543. }
  544. if ( material.wireframe === false && material.shading == THREE.SmoothShading && element.vertexNormalsLength == 4 ) {
  545. _color1.copy( _ambientLight );
  546. _color2.copy( _ambientLight );
  547. _color3.copy( _ambientLight );
  548. _color4.copy( _ambientLight );
  549. calculateLight( element.v1.positionWorld, element.vertexNormalsModel[ 0 ], _color1 );
  550. calculateLight( element.v2.positionWorld, element.vertexNormalsModel[ 1 ], _color2 );
  551. calculateLight( element.v4.positionWorld, element.vertexNormalsModel[ 3 ], _color3 );
  552. calculateLight( element.v3.positionWorld, element.vertexNormalsModel[ 2 ], _color4 );
  553. _color1.multiply( _diffuseColor ).add( _emissiveColor );
  554. _color2.multiply( _diffuseColor ).add( _emissiveColor );
  555. _color3.multiply( _diffuseColor ).add( _emissiveColor );
  556. _color4.multiply( _diffuseColor ).add( _emissiveColor );
  557. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  558. // TODO: UVs are incorrect, v4->v3?
  559. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y );
  560. clipImage( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y, 0, 0, 1, 0, 0, 1, _image );
  561. drawTriangle( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y );
  562. clipImage( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y, 1, 0, 1, 1, 0, 1, _image );
  563. } else {
  564. _color.copy( _ambientLight );
  565. calculateLight( element.centroidModel, element.normalModel, _color );
  566. _color.multiply( _diffuseColor ).add( _emissiveColor );
  567. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  568. material.wireframe === true
  569. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  570. : fillPath( _color );
  571. }
  572. } else if ( material instanceof THREE.MeshBasicMaterial ) {
  573. _color.copy( material.color );
  574. if ( material.vertexColors === THREE.FaceColors ) {
  575. _color.multiply( element.color );
  576. }
  577. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  578. material.wireframe === true
  579. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  580. : fillPath( _color );
  581. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  582. var normal;
  583. if ( material.shading == THREE.FlatShading ) {
  584. normal = element.normalModelView;
  585. _color.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  586. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  587. material.wireframe === true
  588. ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
  589. : fillPath( _color );
  590. } else if ( material.shading == THREE.SmoothShading ) {
  591. normal = element.vertexNormalsModelView[ 0 ];
  592. _color1.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  593. normal = element.vertexNormalsModelView[ 1 ];
  594. _color2.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  595. normal = element.vertexNormalsModelView[ 3 ];
  596. _color3.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  597. normal = element.vertexNormalsModelView[ 2 ];
  598. _color4.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  599. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  600. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y );
  601. clipImage( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y, 0, 0, 1, 0, 0, 1, _image );
  602. drawTriangle( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y );
  603. clipImage( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y, 1, 0, 1, 1, 0, 1, _image );
  604. }
  605. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  606. _near = camera.near;
  607. _far = camera.far;
  608. _color1.r = _color1.g = _color1.b = 1 - smoothstep( v1.positionScreen.z * v1.positionScreen.w, _near, _far );
  609. _color2.r = _color2.g = _color2.b = 1 - smoothstep( v2.positionScreen.z * v2.positionScreen.w, _near, _far );
  610. _color3.r = _color3.g = _color3.b = 1 - smoothstep( v4.positionScreen.z * v4.positionScreen.w, _near, _far );
  611. _color4.r = _color4.g = _color4.b = 1 - smoothstep( v3.positionScreen.z * v3.positionScreen.w, _near, _far );
  612. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  613. // TODO: UVs are incorrect, v4->v3?
  614. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y );
  615. clipImage( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y, 0, 0, 1, 0, 0, 1, _image );
  616. drawTriangle( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y );
  617. clipImage( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y, 1, 0, 1, 1, 0, 1, _image );
  618. }
  619. }
  620. //
  621. function drawTriangle( x0, y0, x1, y1, x2, y2 ) {
  622. _context.beginPath();
  623. _context.moveTo( x0, y0 );
  624. _context.lineTo( x1, y1 );
  625. _context.lineTo( x2, y2 );
  626. _context.closePath();
  627. }
  628. function drawQuad( x0, y0, x1, y1, x2, y2, x3, y3 ) {
  629. _context.beginPath();
  630. _context.moveTo( x0, y0 );
  631. _context.lineTo( x1, y1 );
  632. _context.lineTo( x2, y2 );
  633. _context.lineTo( x3, y3 );
  634. _context.closePath();
  635. }
  636. function strokePath( color, linewidth, linecap, linejoin ) {
  637. setLineWidth( linewidth );
  638. setLineCap( linecap );
  639. setLineJoin( linejoin );
  640. setStrokeStyle( color.getStyle() );
  641. _context.stroke();
  642. _elemBox.expandByScalar( linewidth * 2 );
  643. }
  644. function fillPath( color ) {
  645. setFillStyle( color.getStyle() );
  646. _context.fill();
  647. }
  648. function patternPath( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, texture ) {
  649. if ( texture instanceof THREE.DataTexture || texture.image === undefined || texture.image.width == 0 ) return;
  650. if ( texture.needsUpdate === true ) {
  651. var repeatX = texture.wrapS == THREE.RepeatWrapping;
  652. var repeatY = texture.wrapT == THREE.RepeatWrapping;
  653. _patterns[ texture.id ] = _context.createPattern(
  654. texture.image, repeatX === true && repeatY === true
  655. ? 'repeat'
  656. : repeatX === true && repeatY === false
  657. ? 'repeat-x'
  658. : repeatX === false && repeatY === true
  659. ? 'repeat-y'
  660. : 'no-repeat'
  661. );
  662. texture.needsUpdate = false;
  663. }
  664. _patterns[ texture.id ] === undefined
  665. ? setFillStyle( 'rgba(0,0,0,1)' )
  666. : setFillStyle( _patterns[ texture.id ] );
  667. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  668. var a, b, c, d, e, f, det, idet,
  669. offsetX = texture.offset.x / texture.repeat.x,
  670. offsetY = texture.offset.y / texture.repeat.y,
  671. width = texture.image.width * texture.repeat.x,
  672. height = texture.image.height * texture.repeat.y;
  673. u0 = ( u0 + offsetX ) * width;
  674. v0 = ( 1.0 - v0 + offsetY ) * height;
  675. u1 = ( u1 + offsetX ) * width;
  676. v1 = ( 1.0 - v1 + offsetY ) * height;
  677. u2 = ( u2 + offsetX ) * width;
  678. v2 = ( 1.0 - v2 + offsetY ) * height;
  679. x1 -= x0; y1 -= y0;
  680. x2 -= x0; y2 -= y0;
  681. u1 -= u0; v1 -= v0;
  682. u2 -= u0; v2 -= v0;
  683. det = u1 * v2 - u2 * v1;
  684. if ( det === 0 ) {
  685. if ( _imagedatas[ texture.id ] === undefined ) {
  686. var canvas = document.createElement( 'canvas' )
  687. canvas.width = texture.image.width;
  688. canvas.height = texture.image.height;
  689. var context = canvas.getContext( '2d' );
  690. context.drawImage( texture.image, 0, 0 );
  691. _imagedatas[ texture.id ] = context.getImageData( 0, 0, texture.image.width, texture.image.height ).data;
  692. }
  693. var data = _imagedatas[ texture.id ];
  694. var index = ( Math.floor( u0 ) + Math.floor( v0 ) * texture.image.width ) * 4;
  695. _color.setRGB( data[ index ] / 255, data[ index + 1 ] / 255, data[ index + 2 ] / 255 );
  696. fillPath( _color );
  697. return;
  698. }
  699. idet = 1 / det;
  700. a = ( v2 * x1 - v1 * x2 ) * idet;
  701. b = ( v2 * y1 - v1 * y2 ) * idet;
  702. c = ( u1 * x2 - u2 * x1 ) * idet;
  703. d = ( u1 * y2 - u2 * y1 ) * idet;
  704. e = x0 - a * u0 - c * v0;
  705. f = y0 - b * u0 - d * v0;
  706. _context.save();
  707. _context.transform( a, b, c, d, e, f );
  708. _context.fill();
  709. _context.restore();
  710. }
  711. function clipImage( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, image ) {
  712. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  713. var a, b, c, d, e, f, det, idet,
  714. width = image.width - 1,
  715. height = image.height - 1;
  716. u0 *= width; v0 *= height;
  717. u1 *= width; v1 *= height;
  718. u2 *= width; v2 *= height;
  719. x1 -= x0; y1 -= y0;
  720. x2 -= x0; y2 -= y0;
  721. u1 -= u0; v1 -= v0;
  722. u2 -= u0; v2 -= v0;
  723. det = u1 * v2 - u2 * v1;
  724. idet = 1 / det;
  725. a = ( v2 * x1 - v1 * x2 ) * idet;
  726. b = ( v2 * y1 - v1 * y2 ) * idet;
  727. c = ( u1 * x2 - u2 * x1 ) * idet;
  728. d = ( u1 * y2 - u2 * y1 ) * idet;
  729. e = x0 - a * u0 - c * v0;
  730. f = y0 - b * u0 - d * v0;
  731. _context.save();
  732. _context.transform( a, b, c, d, e, f );
  733. _context.clip();
  734. _context.drawImage( image, 0, 0 );
  735. _context.restore();
  736. }
  737. function getGradientTexture( color1, color2, color3, color4 ) {
  738. // http://mrdoob.com/blog/post/710
  739. _pixelMapData[ 0 ] = ( color1.r * 255 ) | 0;
  740. _pixelMapData[ 1 ] = ( color1.g * 255 ) | 0;
  741. _pixelMapData[ 2 ] = ( color1.b * 255 ) | 0;
  742. _pixelMapData[ 4 ] = ( color2.r * 255 ) | 0;
  743. _pixelMapData[ 5 ] = ( color2.g * 255 ) | 0;
  744. _pixelMapData[ 6 ] = ( color2.b * 255 ) | 0;
  745. _pixelMapData[ 8 ] = ( color3.r * 255 ) | 0;
  746. _pixelMapData[ 9 ] = ( color3.g * 255 ) | 0;
  747. _pixelMapData[ 10 ] = ( color3.b * 255 ) | 0;
  748. _pixelMapData[ 12 ] = ( color4.r * 255 ) | 0;
  749. _pixelMapData[ 13 ] = ( color4.g * 255 ) | 0;
  750. _pixelMapData[ 14 ] = ( color4.b * 255 ) | 0;
  751. _pixelMapContext.putImageData( _pixelMapImage, 0, 0 );
  752. _gradientMapContext.drawImage( _pixelMap, 0, 0 );
  753. return _gradientMap;
  754. }
  755. // Hide anti-alias gaps
  756. function expand( v1, v2 ) {
  757. var x = v2.x - v1.x, y = v2.y - v1.y,
  758. det = x * x + y * y, idet;
  759. if ( det === 0 ) return;
  760. idet = 1 / Math.sqrt( det );
  761. x *= idet; y *= idet;
  762. v2.x += x; v2.y += y;
  763. v1.x -= x; v1.y -= y;
  764. }
  765. // Context cached methods.
  766. function setOpacity( value ) {
  767. if ( _contextGlobalAlpha !== value ) {
  768. _context.globalAlpha = value;
  769. _contextGlobalAlpha = value;
  770. }
  771. }
  772. function setBlending( value ) {
  773. if ( _contextGlobalCompositeOperation !== value ) {
  774. if ( value === THREE.NormalBlending ) {
  775. _context.globalCompositeOperation = 'source-over';
  776. } else if ( value === THREE.AdditiveBlending ) {
  777. _context.globalCompositeOperation = 'lighter';
  778. } else if ( value === THREE.SubtractiveBlending ) {
  779. _context.globalCompositeOperation = 'darker';
  780. }
  781. _contextGlobalCompositeOperation = value;
  782. }
  783. }
  784. function setLineWidth( value ) {
  785. if ( _contextLineWidth !== value ) {
  786. _context.lineWidth = value;
  787. _contextLineWidth = value;
  788. }
  789. }
  790. function setLineCap( value ) {
  791. // "butt", "round", "square"
  792. if ( _contextLineCap !== value ) {
  793. _context.lineCap = value;
  794. _contextLineCap = value;
  795. }
  796. }
  797. function setLineJoin( value ) {
  798. // "round", "bevel", "miter"
  799. if ( _contextLineJoin !== value ) {
  800. _context.lineJoin = value;
  801. _contextLineJoin = value;
  802. }
  803. }
  804. function setStrokeStyle( value ) {
  805. if ( _contextStrokeStyle !== value ) {
  806. _context.strokeStyle = value;
  807. _contextStrokeStyle = value;
  808. }
  809. }
  810. function setFillStyle( value ) {
  811. if ( _contextFillStyle !== value ) {
  812. _context.fillStyle = value;
  813. _contextFillStyle = value;
  814. }
  815. }
  816. function setDashAndGap( dashSizeValue, gapSizeValue ) {
  817. if ( _contextDashSize !== dashSizeValue || _contextGapSize !== gapSizeValue ) {
  818. _context.setLineDash( [ dashSizeValue, gapSizeValue ] );
  819. _contextDashSize = dashSizeValue;
  820. _contextGapSize = gapSizeValue;
  821. }
  822. }
  823. };