CanvasRenderer.js 32 KB

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