CanvasRenderer.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. THREE.CanvasRenderer = function ( parameters ) {
  5. parameters = parameters || {};
  6. var _this = this,
  7. _renderData, _elements, _lights,
  8. _projector = new THREE.Projector(),
  9. _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
  10. _canvasWidth, _canvasHeight, _canvasWidthHalf, _canvasHeightHalf,
  11. _context = _canvas.getContext( '2d' ),
  12. _clearColor = new THREE.Color( 0x000000 ),
  13. _clearOpacity = 0,
  14. _contextGlobalAlpha = 1,
  15. _contextGlobalCompositeOperation = 0,
  16. _contextStrokeStyle = null,
  17. _contextFillStyle = null,
  18. _contextLineWidth = null,
  19. _contextLineCap = null,
  20. _contextLineJoin = null,
  21. _v1, _v2, _v3, _v4,
  22. _v5 = new THREE.RenderableVertex(),
  23. _v6 = new THREE.RenderableVertex(),
  24. _v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
  25. _v4x, _v4y, _v5x, _v5y, _v6x, _v6y,
  26. _color = new THREE.Color(),
  27. _color1 = new THREE.Color(),
  28. _color2 = new THREE.Color(),
  29. _color3 = new THREE.Color(),
  30. _color4 = new THREE.Color(),
  31. _patterns = [], _imagedatas = [],
  32. _near, _far,
  33. _image, _uvs,
  34. _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y,
  35. _clipRect = new THREE.Rectangle(),
  36. _clearRect = new THREE.Rectangle(),
  37. _bboxRect = new THREE.Rectangle(),
  38. _enableLighting = false,
  39. _ambientLight = new THREE.Color(),
  40. _directionalLights = new THREE.Color(),
  41. _pointLights = new THREE.Color(),
  42. _pi2 = Math.PI * 2,
  43. _vector3 = new THREE.Vector3(), // Needed for PointLight
  44. _pixelMap, _pixelMapContext, _pixelMapImage, _pixelMapData,
  45. _gradientMap, _gradientMapContext, _gradientMapQuality = 16;
  46. _pixelMap = document.createElement( 'canvas' );
  47. _pixelMap.width = _pixelMap.height = 2;
  48. _pixelMapContext = _pixelMap.getContext( '2d' );
  49. _pixelMapContext.fillStyle = 'rgba(0,0,0,1)';
  50. _pixelMapContext.fillRect( 0, 0, 2, 2 );
  51. _pixelMapImage = _pixelMapContext.getImageData( 0, 0, 2, 2 );
  52. _pixelMapData = _pixelMapImage.data;
  53. _gradientMap = document.createElement( 'canvas' );
  54. _gradientMap.width = _gradientMap.height = _gradientMapQuality;
  55. _gradientMapContext = _gradientMap.getContext( '2d' );
  56. _gradientMapContext.translate( - _gradientMapQuality / 2, - _gradientMapQuality / 2 );
  57. _gradientMapContext.scale( _gradientMapQuality, _gradientMapQuality );
  58. _gradientMapQuality --; // Fix UVs
  59. this.domElement = _canvas;
  60. this.autoClear = true;
  61. this.sortObjects = true;
  62. this.sortElements = true;
  63. this.info = {
  64. render: {
  65. vertices: 0,
  66. faces: 0
  67. }
  68. }
  69. this.setSize = function ( width, height ) {
  70. _canvasWidth = width;
  71. _canvasHeight = height;
  72. _canvasWidthHalf = Math.floor( _canvasWidth / 2 );
  73. _canvasHeightHalf = Math.floor( _canvasHeight / 2 );
  74. _canvas.width = _canvasWidth;
  75. _canvas.height = _canvasHeight;
  76. _clipRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  77. _clearRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  78. _contextGlobalAlpha = 1;
  79. _contextGlobalCompositeOperation = 0;
  80. _contextStrokeStyle = null;
  81. _contextFillStyle = null;
  82. _contextLineWidth = null;
  83. _contextLineCap = null;
  84. _contextLineJoin = null;
  85. };
  86. this.setClearColor = function( color, opacity ) {
  87. _clearColor.copy( color );
  88. _clearOpacity = opacity;
  89. _clearRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  90. };
  91. this.setClearColorHex = function( hex, opacity ) {
  92. _clearColor.setHex( hex );
  93. _clearOpacity = opacity;
  94. _clearRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  95. };
  96. this.clear = function () {
  97. _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf );
  98. if ( !_clearRect.isEmpty() ) {
  99. _clearRect.minSelf( _clipRect );
  100. _clearRect.inflate( 2 );
  101. if ( _clearOpacity < 1 ) {
  102. _context.clearRect( Math.floor( _clearRect.getX() ), Math.floor( _clearRect.getY() ), Math.floor( _clearRect.getWidth() ), Math.floor( _clearRect.getHeight() ) );
  103. }
  104. if ( _clearOpacity > 0 ) {
  105. setBlending( THREE.NormalBlending );
  106. setOpacity( 1 );
  107. setFillStyle( 'rgba(' + Math.floor( _clearColor.r * 255 ) + ',' + Math.floor( _clearColor.g * 255 ) + ',' + Math.floor( _clearColor.b * 255 ) + ',' + _clearOpacity + ')' );
  108. _context.fillRect( Math.floor( _clearRect.getX() ), Math.floor( _clearRect.getY() ), Math.floor( _clearRect.getWidth() ), Math.floor( _clearRect.getHeight() ) );
  109. }
  110. _clearRect.empty();
  111. }
  112. };
  113. this.render = function ( scene, camera ) {
  114. var e, el, element, material;
  115. this.autoClear ? this.clear() : _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf );
  116. _this.info.render.vertices = 0;
  117. _this.info.render.faces = 0;
  118. _renderData = _projector.projectScene( scene, camera, this.sortElements );
  119. _elements = _renderData.elements;
  120. _lights = _renderData.lights;
  121. /* DEBUG
  122. _context.fillStyle = 'rgba( 0, 255, 255, 0.5 )';
  123. _context.fillRect( _clipRect.getX(), _clipRect.getY(), _clipRect.getWidth(), _clipRect.getHeight() );
  124. */
  125. _enableLighting = _lights.length > 0;
  126. if ( _enableLighting ) {
  127. calculateLights( _lights );
  128. }
  129. for ( e = 0, el = _elements.length; e < el; e++ ) {
  130. element = _elements[ e ];
  131. material = element.material;
  132. material = material instanceof THREE.MeshFaceMaterial ? element.faceMaterial : material;
  133. if ( material == null || material.opacity == 0 ) continue;
  134. _bboxRect.empty();
  135. if ( element instanceof THREE.RenderableParticle ) {
  136. _v1 = element;
  137. _v1.x *= _canvasWidthHalf; _v1.y *= _canvasHeightHalf;
  138. renderParticle( _v1, element, material, scene );
  139. } else if ( element instanceof THREE.RenderableLine ) {
  140. _v1 = element.v1; _v2 = element.v2;
  141. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  142. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  143. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  144. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  145. if ( _clipRect.intersects( _bboxRect ) ) {
  146. renderLine( _v1, _v2, element, material, scene );
  147. }
  148. } else if ( element instanceof THREE.RenderableFace3 ) {
  149. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  150. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  151. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  152. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  153. if ( material.overdraw ) {
  154. expand( _v1.positionScreen, _v2.positionScreen );
  155. expand( _v2.positionScreen, _v3.positionScreen );
  156. expand( _v3.positionScreen, _v1.positionScreen );
  157. }
  158. _bboxRect.add3Points( _v1.positionScreen.x, _v1.positionScreen.y,
  159. _v2.positionScreen.x, _v2.positionScreen.y,
  160. _v3.positionScreen.x, _v3.positionScreen.y );
  161. if ( _clipRect.intersects( _bboxRect ) ) {
  162. renderFace3( _v1, _v2, _v3, 0, 1, 2, element, material, scene );
  163. }
  164. } else if ( element instanceof THREE.RenderableFace4 ) {
  165. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3; _v4 = element.v4;
  166. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  167. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  168. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  169. _v4.positionScreen.x *= _canvasWidthHalf; _v4.positionScreen.y *= _canvasHeightHalf;
  170. _v5.positionScreen.copy( _v2.positionScreen );
  171. _v6.positionScreen.copy( _v4.positionScreen );
  172. if ( material.overdraw ) {
  173. expand( _v1.positionScreen, _v2.positionScreen );
  174. expand( _v2.positionScreen, _v4.positionScreen );
  175. expand( _v4.positionScreen, _v1.positionScreen );
  176. expand( _v3.positionScreen, _v5.positionScreen );
  177. expand( _v3.positionScreen, _v6.positionScreen );
  178. }
  179. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  180. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  181. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  182. _bboxRect.addPoint( _v4.positionScreen.x, _v4.positionScreen.y );
  183. if ( _clipRect.intersects( _bboxRect ) ) {
  184. renderFace4( _v1, _v2, _v3, _v4, _v5, _v6, element, material, scene );
  185. }
  186. }
  187. /*
  188. _context.lineWidth = 1;
  189. _context.strokeStyle = 'rgba( 0, 255, 0, 0.5 )';
  190. _context.strokeRect( _bboxRect.getX(), _bboxRect.getY(), _bboxRect.getWidth(), _bboxRect.getHeight() );
  191. */
  192. _clearRect.addRectangle( _bboxRect );
  193. }
  194. /* DEBUG
  195. _context.lineWidth = 1;
  196. _context.strokeStyle = 'rgba( 255, 0, 0, 0.5 )';
  197. _context.strokeRect( _clearRect.getX(), _clearRect.getY(), _clearRect.getWidth(), _clearRect.getHeight() );
  198. */
  199. _context.setTransform( 1, 0, 0, 1, 0, 0 );
  200. //
  201. function calculateLights( lights ) {
  202. var l, ll, light, lightColor;
  203. _ambientLight.setRGB( 0, 0, 0 );
  204. _directionalLights.setRGB( 0, 0, 0 );
  205. _pointLights.setRGB( 0, 0, 0 );
  206. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  207. light = lights[ l ];
  208. lightColor = light.color;
  209. if ( light instanceof THREE.AmbientLight ) {
  210. _ambientLight.r += lightColor.r;
  211. _ambientLight.g += lightColor.g;
  212. _ambientLight.b += lightColor.b;
  213. } else if ( light instanceof THREE.DirectionalLight ) {
  214. // for particles
  215. _directionalLights.r += lightColor.r;
  216. _directionalLights.g += lightColor.g;
  217. _directionalLights.b += lightColor.b;
  218. } else if ( light instanceof THREE.PointLight ) {
  219. // for particles
  220. _pointLights.r += lightColor.r;
  221. _pointLights.g += lightColor.g;
  222. _pointLights.b += lightColor.b;
  223. }
  224. }
  225. }
  226. function calculateLight( lights, position, normal, color ) {
  227. var l, ll, light, lightColor, lightPosition, amount;
  228. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  229. light = lights[ l ];
  230. lightColor = light.color;
  231. if ( light instanceof THREE.DirectionalLight ) {
  232. lightPosition = light.matrixWorld.getPosition();
  233. amount = normal.dot( lightPosition );
  234. if ( amount <= 0 ) continue;
  235. amount *= light.intensity;
  236. color.r += lightColor.r * amount;
  237. color.g += lightColor.g * amount;
  238. color.b += lightColor.b * amount;
  239. } else if ( light instanceof THREE.PointLight ) {
  240. lightPosition = light.matrixWorld.getPosition();
  241. amount = normal.dot( _vector3.sub( lightPosition, position ).normalize() );
  242. if ( amount <= 0 ) continue;
  243. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  244. if ( amount == 0 ) continue;
  245. amount *= light.intensity;
  246. color.r += lightColor.r * amount;
  247. color.g += lightColor.g * amount;
  248. color.b += lightColor.b * amount;
  249. }
  250. }
  251. }
  252. function renderParticle ( v1, element, material, scene ) {
  253. setOpacity( material.opacity );
  254. setBlending( material.blending );
  255. var width, height, scaleX, scaleY,
  256. bitmap, bitmapWidth, bitmapHeight;
  257. if ( material instanceof THREE.ParticleBasicMaterial ) {
  258. if ( material.map ) {
  259. bitmap = material.map.image;
  260. bitmapWidth = bitmap.width >> 1;
  261. bitmapHeight = bitmap.height >> 1;
  262. scaleX = element.scale.x * _canvasWidthHalf;
  263. scaleY = element.scale.y * _canvasHeightHalf;
  264. width = scaleX * bitmapWidth;
  265. height = scaleY * bitmapHeight;
  266. // TODO: Rotations break this...
  267. _bboxRect.set( v1.x - width, v1.y - height, v1.x + width, v1.y + height );
  268. if ( !_clipRect.intersects( _bboxRect ) ) {
  269. return;
  270. }
  271. _context.save();
  272. _context.translate( v1.x, v1.y );
  273. _context.rotate( - element.rotation );
  274. _context.scale( scaleX, - scaleY );
  275. _context.translate( - bitmapWidth, - bitmapHeight );
  276. _context.drawImage( bitmap, 0, 0 );
  277. _context.restore();
  278. }
  279. /* DEBUG
  280. _context.beginPath();
  281. _context.moveTo( v1.x - 10, v1.y );
  282. _context.lineTo( v1.x + 10, v1.y );
  283. _context.moveTo( v1.x, v1.y - 10 );
  284. _context.lineTo( v1.x, v1.y + 10 );
  285. _context.closePath();
  286. _context.strokeStyle = 'rgb(255,255,0)';
  287. _context.stroke();
  288. */
  289. } else if ( material instanceof THREE.ParticleCanvasMaterial ) {
  290. width = element.scale.x * _canvasWidthHalf;
  291. height = element.scale.y * _canvasHeightHalf;
  292. _bboxRect.set( v1.x - width, v1.y - height, v1.x + width, v1.y + height );
  293. if ( !_clipRect.intersects( _bboxRect ) ) {
  294. return;
  295. }
  296. setStrokeStyle( material.color.getContextStyle() );
  297. setFillStyle( material.color.getContextStyle() );
  298. _context.save();
  299. _context.translate( v1.x, v1.y );
  300. _context.rotate( - element.rotation );
  301. _context.scale( width, height );
  302. material.program( _context );
  303. _context.restore();
  304. }
  305. }
  306. function renderLine( v1, v2, element, material, scene ) {
  307. setOpacity( material.opacity );
  308. setBlending( material.blending );
  309. _context.beginPath();
  310. _context.moveTo( v1.positionScreen.x, v1.positionScreen.y );
  311. _context.lineTo( v2.positionScreen.x, v2.positionScreen.y );
  312. _context.closePath();
  313. if ( material instanceof THREE.LineBasicMaterial ) {
  314. setLineWidth( material.linewidth );
  315. setLineCap( material.linecap );
  316. setLineJoin( material.linejoin );
  317. setStrokeStyle( material.color.getContextStyle() );
  318. _context.stroke();
  319. _bboxRect.inflate( material.linewidth * 2 );
  320. }
  321. }
  322. function renderFace3( v1, v2, v3, uv1, uv2, uv3, element, material, scene ) {
  323. _this.info.render.vertices += 3;
  324. _this.info.render.faces ++;
  325. setOpacity( material.opacity );
  326. setBlending( material.blending );
  327. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  328. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  329. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  330. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y );
  331. if ( material instanceof THREE.MeshBasicMaterial ) {
  332. if ( material.map/* && !material.wireframe*/ ) {
  333. if ( material.map.mapping instanceof THREE.UVMapping ) {
  334. _uvs = element.uvs[ 0 ];
  335. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uvs[ uv1 ].u, _uvs[ uv1 ].v, _uvs[ uv2 ].u, _uvs[ uv2 ].v, _uvs[ uv3 ].u, _uvs[ uv3 ].v, material.map );
  336. }
  337. } else if ( material.envMap ) {
  338. if ( material.envMap.mapping instanceof THREE.SphericalReflectionMapping ) {
  339. var cameraMatrix = camera.matrixWorldInverse;
  340. _vector3.copy( element.vertexNormalsWorld[ uv1 ] );
  341. _uv1x = ( _vector3.x * cameraMatrix.n11 + _vector3.y * cameraMatrix.n12 + _vector3.z * cameraMatrix.n13 ) * 0.5 + 0.5;
  342. _uv1y = - ( _vector3.x * cameraMatrix.n21 + _vector3.y * cameraMatrix.n22 + _vector3.z * cameraMatrix.n23 ) * 0.5 + 0.5;
  343. _vector3.copy( element.vertexNormalsWorld[ uv2 ] );
  344. _uv2x = ( _vector3.x * cameraMatrix.n11 + _vector3.y * cameraMatrix.n12 + _vector3.z * cameraMatrix.n13 ) * 0.5 + 0.5;
  345. _uv2y = - ( _vector3.x * cameraMatrix.n21 + _vector3.y * cameraMatrix.n22 + _vector3.z * cameraMatrix.n23 ) * 0.5 + 0.5;
  346. _vector3.copy( element.vertexNormalsWorld[ uv3 ] );
  347. _uv3x = ( _vector3.x * cameraMatrix.n11 + _vector3.y * cameraMatrix.n12 + _vector3.z * cameraMatrix.n13 ) * 0.5 + 0.5;
  348. _uv3y = - ( _vector3.x * cameraMatrix.n21 + _vector3.y * cameraMatrix.n22 + _vector3.z * cameraMatrix.n23 ) * 0.5 + 0.5;
  349. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y, material.envMap );
  350. }/* else if ( material.envMap.mapping == THREE.SphericalRefractionMapping ) {
  351. }*/
  352. } else {
  353. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  354. }
  355. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  356. if ( material.map && !material.wireframe ) {
  357. if ( material.map.mapping instanceof THREE.UVMapping ) {
  358. _uvs = element.uvs[ 0 ];
  359. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uvs[ uv1 ].u, _uvs[ uv1 ].v, _uvs[ uv2 ].u, _uvs[ uv2 ].v, _uvs[ uv3 ].u, _uvs[ uv3 ].v, material.map );
  360. }
  361. setBlending( THREE.SubtractiveBlending );
  362. }
  363. if ( _enableLighting ) {
  364. if ( !material.wireframe && material.shading == THREE.SmoothShading && element.vertexNormalsWorld.length == 3 ) {
  365. _color1.r = _color2.r = _color3.r = _ambientLight.r;
  366. _color1.g = _color2.g = _color3.g = _ambientLight.g;
  367. _color1.b = _color2.b = _color3.b = _ambientLight.b;
  368. calculateLight( _lights, element.v1.positionWorld, element.vertexNormalsWorld[ 0 ], _color1 );
  369. calculateLight( _lights, element.v2.positionWorld, element.vertexNormalsWorld[ 1 ], _color2 );
  370. calculateLight( _lights, element.v3.positionWorld, element.vertexNormalsWorld[ 2 ], _color3 );
  371. _color1.r = Math.max( 0, Math.min( material.color.r * _color1.r, 1 ) );
  372. _color1.g = Math.max( 0, Math.min( material.color.g * _color1.g, 1 ) );
  373. _color1.b = Math.max( 0, Math.min( material.color.b * _color1.b, 1 ) );
  374. _color2.r = Math.max( 0, Math.min( material.color.r * _color2.r, 1 ) );
  375. _color2.g = Math.max( 0, Math.min( material.color.g * _color2.g, 1 ) );
  376. _color2.b = Math.max( 0, Math.min( material.color.b * _color2.b, 1 ) );
  377. _color3.r = Math.max( 0, Math.min( material.color.r * _color3.r, 1 ) );
  378. _color3.g = Math.max( 0, Math.min( material.color.g * _color3.g, 1 ) );
  379. _color3.b = Math.max( 0, Math.min( material.color.b * _color3.b, 1 ) );
  380. _color4.r = ( _color2.r + _color3.r ) * 0.5;
  381. _color4.g = ( _color2.g + _color3.g ) * 0.5;
  382. _color4.b = ( _color2.b + _color3.b ) * 0.5;
  383. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  384. clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image );
  385. } else {
  386. _color.r = _ambientLight.r;
  387. _color.g = _ambientLight.g;
  388. _color.b = _ambientLight.b;
  389. calculateLight( _lights, element.centroidWorld, element.normalWorld, _color );
  390. _color.r = Math.max( 0, Math.min( material.color.r * _color.r, 1 ) );
  391. _color.g = Math.max( 0, Math.min( material.color.g * _color.g, 1 ) );
  392. _color.b = Math.max( 0, Math.min( material.color.b * _color.b, 1 ) );
  393. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  394. }
  395. } else {
  396. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  397. }
  398. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  399. _near = camera.near;
  400. _far = camera.far;
  401. _color1.r = _color1.g = _color1.b = 1 - smoothstep( v1.positionScreen.z, _near, _far );
  402. _color2.r = _color2.g = _color2.b = 1 - smoothstep( v2.positionScreen.z, _near, _far );
  403. _color3.r = _color3.g = _color3.b = 1 - smoothstep( v3.positionScreen.z, _near, _far );
  404. _color4.r = ( _color2.r + _color3.r ) * 0.5;
  405. _color4.g = ( _color2.g + _color3.g ) * 0.5;
  406. _color4.b = ( _color2.b + _color3.b ) * 0.5;
  407. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  408. clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image );
  409. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  410. _color.r = normalToComponent( element.normalWorld.x );
  411. _color.g = normalToComponent( element.normalWorld.y );
  412. _color.b = normalToComponent( element.normalWorld.z );
  413. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  414. }
  415. }
  416. function renderFace4( v1, v2, v3, v4, v5, v6, element, material, scene ) {
  417. _this.info.render.vertices += 4;
  418. _this.info.render.faces ++;
  419. setOpacity( material.opacity );
  420. setBlending( material.blending );
  421. if ( material.map || material.envMap ) {
  422. // Let renderFace3() handle this
  423. renderFace3( v1, v2, v4, 0, 1, 3, element, material, scene );
  424. renderFace3( v5, v3, v6, 1, 2, 3, element, material, scene );
  425. return;
  426. }
  427. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  428. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  429. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  430. _v4x = v4.positionScreen.x; _v4y = v4.positionScreen.y;
  431. _v5x = v5.positionScreen.x; _v5y = v5.positionScreen.y;
  432. _v6x = v6.positionScreen.x; _v6y = v6.positionScreen.y;
  433. if ( material instanceof THREE.MeshBasicMaterial ) {
  434. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  435. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  436. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  437. if ( _enableLighting ) {
  438. if ( !material.wireframe && material.shading == THREE.SmoothShading && element.vertexNormalsWorld.length == 4 ) {
  439. _color1.r = _color2.r = _color3.r = _color4.r = _ambientLight.r;
  440. _color1.g = _color2.g = _color3.g = _color4.g = _ambientLight.g;
  441. _color1.b = _color2.b = _color3.b = _color4.b = _ambientLight.b;
  442. calculateLight( _lights, element.v1.positionWorld, element.vertexNormalsWorld[ 0 ], _color1 );
  443. calculateLight( _lights, element.v2.positionWorld, element.vertexNormalsWorld[ 1 ], _color2 );
  444. calculateLight( _lights, element.v4.positionWorld, element.vertexNormalsWorld[ 3 ], _color3 );
  445. calculateLight( _lights, element.v3.positionWorld, element.vertexNormalsWorld[ 2 ], _color4 );
  446. _color1.r = Math.max( 0, Math.min( material.color.r * _color1.r, 1 ) );
  447. _color1.g = Math.max( 0, Math.min( material.color.g * _color1.g, 1 ) );
  448. _color1.b = Math.max( 0, Math.min( material.color.b * _color1.b, 1 ) );
  449. _color2.r = Math.max( 0, Math.min( material.color.r * _color2.r, 1 ) );
  450. _color2.g = Math.max( 0, Math.min( material.color.g * _color2.g, 1 ) );
  451. _color2.b = Math.max( 0, Math.min( material.color.b * _color2.b, 1 ) );
  452. _color3.r = Math.max( 0, Math.min( material.color.r * _color3.r, 1 ) );
  453. _color3.g = Math.max( 0, Math.min( material.color.g * _color3.g, 1 ) );
  454. _color3.b = Math.max( 0, Math.min( material.color.b * _color3.b, 1 ) );
  455. _color4.r = Math.max( 0, Math.min( material.color.r * _color4.r, 1 ) );
  456. _color4.g = Math.max( 0, Math.min( material.color.g * _color4.g, 1 ) );
  457. _color4.b = Math.max( 0, Math.min( material.color.b * _color4.b, 1 ) );
  458. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  459. // TODO: UVs are incorrect, v4->v3?
  460. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y );
  461. clipImage( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y, 0, 0, 1, 0, 0, 1, _image );
  462. drawTriangle( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y );
  463. clipImage( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y, 1, 0, 1, 1, 0, 1, _image );
  464. } else {
  465. _color.r = _ambientLight.r;
  466. _color.g = _ambientLight.g;
  467. _color.b = _ambientLight.b;
  468. calculateLight( _lights, element.centroidWorld, element.normalWorld, _color );
  469. _color.r = Math.max( 0, Math.min( material.color.r * _color.r, 1 ) );
  470. _color.g = Math.max( 0, Math.min( material.color.g * _color.g, 1 ) );
  471. _color.b = Math.max( 0, Math.min( material.color.b * _color.b, 1 ) );
  472. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  473. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  474. }
  475. } else {
  476. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  477. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  478. }
  479. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  480. _color.r = normalToComponent( element.normalWorld.x );
  481. _color.g = normalToComponent( element.normalWorld.y );
  482. _color.b = normalToComponent( element.normalWorld.z );
  483. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  484. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  485. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  486. _near = camera.near;
  487. _far = camera.far;
  488. _color1.r = _color1.g = _color1.b = 1 - smoothstep( v1.positionScreen.z, _near, _far );
  489. _color2.r = _color2.g = _color2.b = 1 - smoothstep( v2.positionScreen.z, _near, _far );
  490. _color3.r = _color3.g = _color3.b = 1 - smoothstep( v4.positionScreen.z, _near, _far );
  491. _color4.r = _color4.g = _color4.b = 1 - smoothstep( v3.positionScreen.z, _near, _far );
  492. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  493. // TODO: UVs are incorrect, v4->v3?
  494. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y );
  495. clipImage( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y, 0, 0, 1, 0, 0, 1, _image );
  496. drawTriangle( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y );
  497. clipImage( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y, 1, 0, 1, 1, 0, 1, _image );
  498. }
  499. }
  500. //
  501. function drawTriangle( x0, y0, x1, y1, x2, y2 ) {
  502. _context.beginPath();
  503. _context.moveTo( x0, y0 );
  504. _context.lineTo( x1, y1 );
  505. _context.lineTo( x2, y2 );
  506. _context.lineTo( x0, y0 );
  507. _context.closePath();
  508. }
  509. function drawQuad( x0, y0, x1, y1, x2, y2, x3, y3 ) {
  510. _context.beginPath();
  511. _context.moveTo( x0, y0 );
  512. _context.lineTo( x1, y1 );
  513. _context.lineTo( x2, y2 );
  514. _context.lineTo( x3, y3 );
  515. _context.lineTo( x0, y0 );
  516. _context.closePath();
  517. }
  518. function strokePath( color, linewidth, linecap, linejoin ) {
  519. setLineWidth( linewidth );
  520. setLineCap( linecap );
  521. setLineJoin( linejoin );
  522. setStrokeStyle( color.getContextStyle() );
  523. _context.stroke();
  524. _bboxRect.inflate( linewidth * 2 );
  525. }
  526. function fillPath( color ) {
  527. setFillStyle( color.getContextStyle() );
  528. _context.fill();
  529. }
  530. function patternPath( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, texture ) {
  531. if ( texture.image.width == 0 ) return;
  532. if ( texture.needsUpdate == true || _patterns[ texture.id ] == undefined ) {
  533. var repeatX = texture.wrapS == THREE.RepeatWrapping;
  534. var repeatY = texture.wrapT == THREE.RepeatWrapping;
  535. _patterns[ texture.id ] = _context.createPattern( texture.image, repeatX && repeatY ? 'repeat' : repeatX && !repeatY ? 'repeat-x' : !repeatX && repeatY ? 'repeat-y' : 'no-repeat' );
  536. texture.needsUpdate = false;
  537. }
  538. setFillStyle( _patterns[ texture.id ] );
  539. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  540. var a, b, c, d, e, f, det, idet,
  541. offsetX = texture.offset.x / texture.repeat.x,
  542. offsetY = texture.offset.y / texture.repeat.y,
  543. width = texture.image.width * texture.repeat.x,
  544. height = texture.image.height * texture.repeat.y;
  545. u0 = ( u0 + offsetX ) * width;
  546. v0 = ( v0 + offsetY ) * height;
  547. u1 = ( u1 + offsetX ) * width;
  548. v1 = ( v1 + offsetY ) * height;
  549. u2 = ( u2 + offsetX ) * width;
  550. v2 = ( v2 + offsetY ) * height;
  551. x1 -= x0; y1 -= y0;
  552. x2 -= x0; y2 -= y0;
  553. u1 -= u0; v1 -= v0;
  554. u2 -= u0; v2 -= v0;
  555. det = u1 * v2 - u2 * v1;
  556. if ( det == 0 ) {
  557. if ( _imagedatas[ texture.id ] === undefined ) {
  558. var canvas = document.createElement( 'canvas' )
  559. canvas.width = texture.image.width;
  560. canvas.height = texture.image.height;
  561. var context = canvas.getContext( '2d' );
  562. context.drawImage( texture.image, 0, 0 );
  563. _imagedatas[ texture.id ] = context.getImageData( 0, 0, texture.image.width, texture.image.height ).data;
  564. // variables cannot be deleted in ES5 strict mode
  565. //delete canvas;
  566. }
  567. var data = _imagedatas[ texture.id ];
  568. var index = ( Math.floor( u0 ) + Math.floor( v0 ) * texture.image.width ) * 4;
  569. _color.setRGB( data[ index ] / 255, data[ index + 1 ] / 255, data[ index + 2 ] / 255 );
  570. fillPath( _color );
  571. return;
  572. }
  573. idet = 1 / det;
  574. a = ( v2 * x1 - v1 * x2 ) * idet;
  575. b = ( v2 * y1 - v1 * y2 ) * idet;
  576. c = ( u1 * x2 - u2 * x1 ) * idet;
  577. d = ( u1 * y2 - u2 * y1 ) * idet;
  578. e = x0 - a * u0 - c * v0;
  579. f = y0 - b * u0 - d * v0;
  580. _context.save();
  581. _context.transform( a, b, c, d, e, f );
  582. _context.fill();
  583. _context.restore();
  584. }
  585. function clipImage( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, image ) {
  586. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  587. var a, b, c, d, e, f, det, idet,
  588. width = image.width - 1,
  589. height = image.height - 1;
  590. u0 *= width; v0 *= height;
  591. u1 *= width; v1 *= height;
  592. u2 *= width; v2 *= height;
  593. x1 -= x0; y1 -= y0;
  594. x2 -= x0; y2 -= y0;
  595. u1 -= u0; v1 -= v0;
  596. u2 -= u0; v2 -= v0;
  597. det = u1 * v2 - u2 * v1;
  598. idet = 1 / det;
  599. a = ( v2 * x1 - v1 * x2 ) * idet;
  600. b = ( v2 * y1 - v1 * y2 ) * idet;
  601. c = ( u1 * x2 - u2 * x1 ) * idet;
  602. d = ( u1 * y2 - u2 * y1 ) * idet;
  603. e = x0 - a * u0 - c * v0;
  604. f = y0 - b * u0 - d * v0;
  605. _context.save();
  606. _context.transform( a, b, c, d, e, f );
  607. _context.clip();
  608. _context.drawImage( image, 0, 0 );
  609. _context.restore();
  610. }
  611. function getGradientTexture( color1, color2, color3, color4 ) {
  612. // http://mrdoob.com/blog/post/710
  613. var c1r = ~~ ( color1.r * 255 ), c1g = ~~ ( color1.g * 255 ), c1b = ~~ ( color1.b * 255 ),
  614. c2r = ~~ ( color2.r * 255 ), c2g = ~~ ( color2.g * 255 ), c2b = ~~ ( color2.b * 255 ),
  615. c3r = ~~ ( color3.r * 255 ), c3g = ~~ ( color3.g * 255 ), c3b = ~~ ( color3.b * 255 ),
  616. c4r = ~~ ( color4.r * 255 ), c4g = ~~ ( color4.g * 255 ), c4b = ~~ ( color4.b * 255 );
  617. _pixelMapData[ 0 ] = c1r < 0 ? 0 : c1r > 255 ? 255 : c1r;
  618. _pixelMapData[ 1 ] = c1g < 0 ? 0 : c1g > 255 ? 255 : c1g;
  619. _pixelMapData[ 2 ] = c1b < 0 ? 0 : c1b > 255 ? 255 : c1b;
  620. _pixelMapData[ 4 ] = c2r < 0 ? 0 : c2r > 255 ? 255 : c2r;
  621. _pixelMapData[ 5 ] = c2g < 0 ? 0 : c2g > 255 ? 255 : c2g;
  622. _pixelMapData[ 6 ] = c2b < 0 ? 0 : c2b > 255 ? 255 : c2b;
  623. _pixelMapData[ 8 ] = c3r < 0 ? 0 : c3r > 255 ? 255 : c3r;
  624. _pixelMapData[ 9 ] = c3g < 0 ? 0 : c3g > 255 ? 255 : c3g;
  625. _pixelMapData[ 10 ] = c3b < 0 ? 0 : c3b > 255 ? 255 : c3b;
  626. _pixelMapData[ 12 ] = c4r < 0 ? 0 : c4r > 255 ? 255 : c4r;
  627. _pixelMapData[ 13 ] = c4g < 0 ? 0 : c4g > 255 ? 255 : c4g;
  628. _pixelMapData[ 14 ] = c4b < 0 ? 0 : c4b > 255 ? 255 : c4b;
  629. _pixelMapContext.putImageData( _pixelMapImage, 0, 0 );
  630. _gradientMapContext.drawImage( _pixelMap, 0, 0 );
  631. return _gradientMap;
  632. }
  633. function smoothstep( value, min, max ) {
  634. var x = ( value - min ) / ( max - min );
  635. return x * x * ( 3 - 2 * x );
  636. }
  637. function normalToComponent( normal ) {
  638. var component = ( normal + 1 ) * 0.5;
  639. return component < 0 ? 0 : ( component > 1 ? 1 : component );
  640. }
  641. // Hide anti-alias gaps
  642. function expand( v1, v2 ) {
  643. var x = v2.x - v1.x, y = v2.y - v1.y,
  644. det = x * x + y * y, idet;
  645. if ( det == 0 ) return;
  646. idet = 1 / Math.sqrt( det );
  647. x *= idet; y *= idet;
  648. v2.x += x; v2.y += y;
  649. v1.x -= x; v1.y -= y;
  650. }
  651. };
  652. // Context cached methods.
  653. function setOpacity( value ) {
  654. if ( _contextGlobalAlpha != value ) {
  655. _context.globalAlpha = _contextGlobalAlpha = value;
  656. }
  657. }
  658. function setBlending( value ) {
  659. if ( _contextGlobalCompositeOperation != value ) {
  660. switch ( value ) {
  661. case THREE.NormalBlending:
  662. _context.globalCompositeOperation = 'source-over';
  663. break;
  664. case THREE.AdditiveBlending:
  665. _context.globalCompositeOperation = 'lighter';
  666. break;
  667. case THREE.SubtractiveBlending:
  668. _context.globalCompositeOperation = 'darker';
  669. break;
  670. }
  671. _contextGlobalCompositeOperation = value;
  672. }
  673. }
  674. function setLineWidth( value ) {
  675. if ( _contextLineWidth != value ) {
  676. _context.lineWidth = _contextLineWidth = value;
  677. }
  678. }
  679. function setLineCap( value ) {
  680. // "butt", "round", "square"
  681. if ( _contextLineCap != value ) {
  682. _context.lineCap = _contextLineCap = value;
  683. }
  684. }
  685. function setLineJoin( value ) {
  686. // "round", "bevel", "miter"
  687. if ( _contextLineJoin != value ) {
  688. _context.lineJoin = _contextLineJoin = value;
  689. }
  690. }
  691. function setStrokeStyle( style ) {
  692. if ( _contextStrokeStyle != style ) {
  693. _context.strokeStyle = _contextStrokeStyle = style;
  694. }
  695. }
  696. function setFillStyle( style ) {
  697. if ( _contextFillStyle != style ) {
  698. _context.fillStyle = _contextFillStyle = style;
  699. }
  700. }
  701. };