CanvasRenderer.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. THREE.CanvasRenderer = function () {
  5. var _renderList = null,
  6. _projector = new THREE.Projector(),
  7. _canvas = document.createElement( 'canvas' ),
  8. _canvasWidth, _canvasHeight, _canvasWidthHalf, _canvasHeightHalf,
  9. _context = _canvas.getContext( '2d' ),
  10. _contextGlobalAlpha = 1,
  11. _contextGlobalCompositeOperation = 0,
  12. _contextStrokeStyle = null,
  13. _contextFillStyle = null,
  14. _contextLineWidth = 1,
  15. _min = Math.min, _max = Math.max,
  16. _v1, _v2, _v3,
  17. _v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
  18. _color = new THREE.Color(),
  19. _color1 = new THREE.Color(),
  20. _color2 = new THREE.Color(),
  21. _color3 = new THREE.Color(),
  22. _color4 = new THREE.Color(),
  23. _2near, _farPlusNear, _farMinusNear,
  24. _bitmap,
  25. _clipRect = new THREE.Rectangle(),
  26. _clearRect = new THREE.Rectangle(),
  27. _bboxRect = new THREE.Rectangle(),
  28. _enableLighting = false,
  29. _light = new THREE.Color(),
  30. _ambientLight = new THREE.Color(),
  31. _directionalLights = new THREE.Color(),
  32. _pointLights = new THREE.Color(),
  33. _pi2 = Math.PI * 2,
  34. _vector3 = new THREE.Vector3(), // Needed for PointLight
  35. _pixelMap, _pixelMapContext, _pixelMapImage, _pixelMapData,
  36. _gradientMap, _gradientMapContext, _gradientMapQuality = 16;
  37. _pixelMap = document.createElement( 'canvas' );
  38. _pixelMap.width = _pixelMap.height = 2;
  39. _pixelMapContext = _pixelMap.getContext( '2d' );
  40. _pixelMapContext.fillStyle = 'rgba(0,0,0,1)';
  41. _pixelMapContext.fillRect( 0, 0, 2, 2 );
  42. _pixelMapImage = _pixelMapContext.getImageData( 0, 0, 2, 2 );
  43. _pixelMapData = _pixelMapImage.data;
  44. _gradientMap = document.createElement( 'canvas' );
  45. _gradientMap.width = _gradientMap.height = _gradientMapQuality;
  46. _gradientMapContext = _gradientMap.getContext( '2d' );
  47. _gradientMapContext.translate( - _gradientMapQuality / 2, - _gradientMapQuality / 2 );
  48. _gradientMapContext.scale( _gradientMapQuality, _gradientMapQuality );
  49. _gradientMapQuality --; // Fix UVs
  50. this.domElement = _canvas;
  51. this.autoClear = true;
  52. this.setSize = function ( width, height ) {
  53. _canvasWidth = width;
  54. _canvasHeight = height;
  55. _canvasWidthHalf = _canvasWidth / 2;
  56. _canvasHeightHalf = _canvasHeight / 2;
  57. _canvas.width = _canvasWidth;
  58. _canvas.height = _canvasHeight;
  59. _clipRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  60. };
  61. this.clear = function () {
  62. if ( !_clearRect.isEmpty() ) {
  63. _clearRect.inflate( 1 );
  64. _clearRect.minSelf( _clipRect );
  65. _context.clearRect( _clearRect.getX(), _clearRect.getY(), _clearRect.getWidth(), _clearRect.getHeight() );
  66. _clearRect.empty();
  67. }
  68. };
  69. this.render = function ( scene, camera ) {
  70. var e, el, element, m, ml, fm, fml, material;
  71. _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf );
  72. this.autoClear && this.clear();
  73. _renderList = _projector.projectScene( scene, camera );
  74. /* DEBUG
  75. _context.fillStyle = 'rgba(0, 255, 255, 0.5)';
  76. _context.fillRect( _clipRect.getX(), _clipRect.getY(), _clipRect.getWidth(), _clipRect.getHeight() );
  77. */
  78. _enableLighting = scene.lights.length > 0;
  79. if ( _enableLighting ) {
  80. calculateLights( scene );
  81. }
  82. for ( e = 0, el = _renderList.length; e < el; e++ ) {
  83. element = _renderList[ e ];
  84. _bboxRect.empty();
  85. if ( element instanceof THREE.RenderableParticle ) {
  86. _v1 = element;
  87. _v1.x *= _canvasWidthHalf; _v1.y *= _canvasHeightHalf;
  88. for ( m = 0, ml = element.material.length; m < ml; m++ ) {
  89. renderParticle( _v1, element, element.material[ m ], scene );
  90. }
  91. } else if ( element instanceof THREE.RenderableLine ) {
  92. _v1 = element.v1; _v2 = element.v2;
  93. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  94. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  95. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  96. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  97. if ( _clipRect.instersects( _bboxRect ) ) {
  98. m = 0; ml = element.material.length;
  99. while ( m < ml ) {
  100. renderLine( _v1, _v2, element, element.material[ m ++ ], scene );
  101. }
  102. }
  103. } else if ( element instanceof THREE.RenderableFace3 ) {
  104. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  105. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  106. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  107. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  108. if ( element.overdraw ) {
  109. expand( _v1.positionScreen, _v2.positionScreen );
  110. expand( _v2.positionScreen, _v3.positionScreen );
  111. expand( _v3.positionScreen, _v1.positionScreen );
  112. }
  113. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  114. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  115. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  116. if ( _clipRect.instersects( _bboxRect ) ) {
  117. m = 0; ml = element.meshMaterial.length;
  118. while ( m < ml ) {
  119. material = element.meshMaterial[ m ++ ];
  120. if ( material instanceof THREE.MeshFaceMaterial ) {
  121. fm = 0; fml = element.faceMaterial.length;
  122. while ( fm < fml ) {
  123. material = element.faceMaterial[ fm ++ ];
  124. material && renderFace3( _v1, _v2, _v3, element, material, scene );
  125. }
  126. continue;
  127. }
  128. renderFace3( _v1, _v2, _v3, element, material, scene );
  129. }
  130. }
  131. }
  132. /*
  133. _context.lineWidth = 1;
  134. _context.strokeStyle = 'rgba( 0, 255, 0, 0.5 )';
  135. _context.strokeRect( _bboxRect.getX(), _bboxRect.getY(), _bboxRect.getWidth(), _bboxRect.getHeight() );
  136. */
  137. _clearRect.addRectangle( _bboxRect );
  138. }
  139. /* DEBUG
  140. _context.lineWidth = 1;
  141. _context.strokeStyle = 'rgba( 255, 0, 0, 0.5 )';
  142. _context.strokeRect( _clearRect.getX(), _clearRect.getY(), _clearRect.getWidth(), _clearRect.getHeight() );
  143. */
  144. _context.setTransform( 1, 0, 0, 1, 0, 0 );
  145. };
  146. function calculateLights( scene ) {
  147. var l, ll, light, lightColor,
  148. lights = scene.lights;
  149. _ambientLight.setRGB( 0, 0, 0 );
  150. _directionalLights.setRGB( 0, 0, 0 );
  151. _pointLights.setRGB( 0, 0, 0 );
  152. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  153. light = lights[ l ];
  154. lightColor = light.color;
  155. if ( light instanceof THREE.AmbientLight ) {
  156. _ambientLight.r += lightColor.r;
  157. _ambientLight.g += lightColor.g;
  158. _ambientLight.b += lightColor.b;
  159. } else if ( light instanceof THREE.DirectionalLight ) {
  160. _directionalLights.r += lightColor.r;
  161. _directionalLights.g += lightColor.g;
  162. _directionalLights.b += lightColor.b;
  163. } else if ( light instanceof THREE.PointLight ) {
  164. _pointLights.r += lightColor.r;
  165. _pointLights.g += lightColor.g;
  166. _pointLights.b += lightColor.b;
  167. }
  168. }
  169. }
  170. function calculateLight( scene, position, normal, color ) {
  171. var l, ll, light, lightColor, lightIntensity,
  172. amount, lights = scene.lights;
  173. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  174. light = lights[ l ];
  175. lightColor = light.color;
  176. lightIntensity = light.intensity;
  177. if ( light instanceof THREE.DirectionalLight ) {
  178. amount = normal.dot( light.position ) * lightIntensity;
  179. if ( amount > 0 ) {
  180. color.r += lightColor.r * amount;
  181. color.g += lightColor.g * amount;
  182. color.b += lightColor.b * amount;
  183. }
  184. } else if ( light instanceof THREE.PointLight ) {
  185. _vector3.sub( light.position, position );
  186. _vector3.normalize();
  187. amount = normal.dot( _vector3 ) * lightIntensity;
  188. if ( amount > 0 ) {
  189. color.r += lightColor.r * amount;
  190. color.g += lightColor.g * amount;
  191. color.b += lightColor.b * amount;
  192. }
  193. }
  194. }
  195. }
  196. function renderParticle ( v1, element, material, scene ) {
  197. if ( material.opacity == 0 ) return;
  198. setOpacity( material.opacity );
  199. setBlending( material.blending );
  200. var width, height, scaleX, scaleY,
  201. bitmap, bitmapWidth, bitmapHeight;
  202. if ( material instanceof THREE.ParticleBasicMaterial ) {
  203. if ( material.map ) {
  204. bitmap = material.map;
  205. bitmapWidth = bitmap.width >> 1;
  206. bitmapHeight = bitmap.height >> 1;
  207. scaleX = element.scale.x * _canvasWidthHalf;
  208. scaleY = element.scale.y * _canvasHeightHalf;
  209. width = scaleX * bitmapWidth;
  210. height = scaleY * bitmapHeight;
  211. // TODO: Rotations break this...
  212. _bboxRect.set( v1.x - width, v1.y - height, v1.x + width, v1.y + height );
  213. if ( !_clipRect.instersects( _bboxRect ) ) {
  214. return;
  215. }
  216. _context.save();
  217. _context.translate( v1.x, v1.y );
  218. _context.rotate( - element.rotation );
  219. _context.scale( scaleX, - scaleY );
  220. _context.translate( - bitmapWidth, - bitmapHeight );
  221. _context.drawImage( bitmap, 0, 0 );
  222. _context.restore();
  223. }
  224. /* DEBUG
  225. _context.beginPath();
  226. _context.moveTo( v1.x - 10, v1.y );
  227. _context.lineTo( v1.x + 10, v1.y );
  228. _context.moveTo( v1.x, v1.y - 10 );
  229. _context.lineTo( v1.x, v1.y + 10 );
  230. _context.closePath();
  231. _context.strokeStyle = 'rgb(255,255,0)';
  232. _context.stroke();
  233. */
  234. } else if ( material instanceof THREE.ParticleCircleMaterial ) {
  235. if ( _enableLighting ) {
  236. _light.r = _ambientLight.r + _directionalLights.r + _pointLights.r;
  237. _light.g = _ambientLight.g + _directionalLights.g + _pointLights.g;
  238. _light.b = _ambientLight.b + _directionalLights.b + _pointLights.b;
  239. _color.r = material.color.r * _light.r;
  240. _color.g = material.color.g * _light.g;
  241. _color.b = material.color.b * _light.b;
  242. _color.updateStyleString();
  243. } else {
  244. _color.__styleString = material.color.__styleString;
  245. }
  246. width = element.scale.x * _canvasWidthHalf;
  247. height = element.scale.y * _canvasHeightHalf;
  248. _bboxRect.set( v1.x - width, v1.y - height, v1.x + width, v1.y + height );
  249. if ( !_clipRect.instersects( _bboxRect ) ) {
  250. return;
  251. }
  252. setFillStyle( _color.__styleString );
  253. _context.save();
  254. _context.translate( v1.x, v1.y );
  255. _context.rotate( - element.rotation );
  256. _context.scale( width, height );
  257. _context.beginPath();
  258. _context.arc( 0, 0, 1, 0, _pi2, true );
  259. _context.closePath();
  260. _context.fill();
  261. _context.restore();
  262. }
  263. }
  264. function renderLine( v1, v2, element, material, scene ) {
  265. if ( material.opacity == 0 ) return;
  266. setOpacity( material.opacity );
  267. setBlending( material.blending );
  268. _context.beginPath();
  269. _context.moveTo( v1.positionScreen.x, v1.positionScreen.y );
  270. _context.lineTo( v2.positionScreen.x, v2.positionScreen.y );
  271. _context.closePath();
  272. if ( material instanceof THREE.LineBasicMaterial ) {
  273. _color.__styleString = material.color.__styleString;
  274. setLineWidth( material.linewidth );
  275. setStrokeStyle( _color.__styleString );
  276. _context.stroke();
  277. _bboxRect.inflate( material.linewidth * 2 );
  278. }
  279. }
  280. function renderFace3( v1, v2, v3, element, material, scene ) {
  281. if ( material.opacity == 0 ) return;
  282. setOpacity( material.opacity );
  283. setBlending( material.blending );
  284. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  285. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  286. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  287. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y );
  288. if ( material instanceof THREE.MeshBasicMaterial ) {
  289. if ( material.map ) {
  290. texturePath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, material.map.image, element.uvs[ 0 ].u, element.uvs[ 0 ].v, element.uvs[ 1 ].u, element.uvs[ 1 ].v, element.uvs[ 2 ].u, element.uvs[ 2 ].v );
  291. /*
  292. setBlending( THREE.SubtractiveBlending );
  293. fillPath( material.color.__styleString );
  294. */
  295. } else {
  296. material.wireframe ? strokePath( material.color.__styleString, material.wireframe_linewidth ) : fillPath( material.color.__styleString );
  297. }
  298. /*
  299. if ( material.env_map ) {
  300. if ( material.env_map.mapping == THREE.ReflectionMapping ) {
  301. } else if ( material.env_map.mapping == THREE.RefractionMapping ) {
  302. }
  303. }
  304. */
  305. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  306. if ( material.map && !material.wireframe ) {
  307. texturePath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, material.map.image, element.uvs[ 0 ].u, element.uvs[ 0 ].v, element.uvs[ 1 ].u, element.uvs[ 1 ].v, element.uvs[ 2 ].u, element.uvs[ 2 ].v );
  308. setBlending( THREE.SubtractiveBlending );
  309. }
  310. if ( _enableLighting ) {
  311. if ( !material.wireframe && material.shading == THREE.SmoothShading && element.vertexNormalsWorld.length == 3 ) {
  312. _color1.r = _color2.r = _color3.r = _ambientLight.r;
  313. _color1.g = _color2.g = _color3.g = _ambientLight.g;
  314. _color1.b = _color2.b = _color3.b = _ambientLight.b;
  315. calculateLight( scene, element.v1.positionWorld, element.vertexNormalsWorld[ 0 ], _color1 );
  316. calculateLight( scene, element.v2.positionWorld, element.vertexNormalsWorld[ 1 ], _color2 );
  317. calculateLight( scene, element.v3.positionWorld, element.vertexNormalsWorld[ 2 ], _color3 );
  318. _color4.r = ( _color2.r + _color3.r ) * 0.5;
  319. _color4.g = ( _color2.g + _color3.g ) * 0.5;
  320. _color4.b = ( _color2.b + _color3.b ) * 0.5;
  321. _bitmap = getGradientTexture( _color1, _color2, _color3, _color4 );
  322. texturePath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _bitmap, 0, 0, 1, 0, 0, 1 );
  323. } else {
  324. _light.r = _ambientLight.r;
  325. _light.g = _ambientLight.g;
  326. _light.b = _ambientLight.b;
  327. calculateLight( scene, element.centroidWorld, element.normalWorld, _light );
  328. _color.r = material.color.r * _light.r;
  329. _color.g = material.color.g * _light.g;
  330. _color.b = material.color.b * _light.b;
  331. _color.updateStyleString();
  332. material.wireframe ? strokePath( _color.__styleString, material.wireframe_linewidth ) : fillPath( _color.__styleString );
  333. }
  334. } else {
  335. material.wireframe ? strokePath( material.color.__styleString, material.wireframe_linewidth ) : fillPath( material.color.__styleString );
  336. }
  337. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  338. /*
  339. _w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear ) );
  340. _color.setRGB( _w, _w, _w );
  341. */
  342. _2near = material.__2near;
  343. _farPlusNear = material.__farPlusNear;
  344. _farMinusNear = material.__farMinusNear;
  345. _color1.r = _color1.g = _color1.b = 1 - ( _2near / ( _farPlusNear - v1.positionScreen.z * _farMinusNear ) );
  346. _color2.r = _color2.g = _color2.b = 1 - ( _2near / ( _farPlusNear - v2.positionScreen.z * _farMinusNear ) );
  347. _color3.r = _color3.g = _color3.b = 1 - ( _2near / ( _farPlusNear - v3.positionScreen.z * _farMinusNear ) );
  348. _color4.r = ( _color2.r + _color3.r ) * 0.5;
  349. _color4.g = ( _color2.g + _color3.g ) * 0.5;
  350. _color4.b = ( _color2.b + _color3.b ) * 0.5;
  351. _bitmap = getGradientTexture( _color1, _color2, _color3, _color4 );
  352. texturePath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _bitmap, 0, 0, 1, 0, 0, 1 );
  353. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  354. _color.r = normalToComponent( element.normalWorld.x );
  355. _color.g = normalToComponent( element.normalWorld.y );
  356. _color.b = normalToComponent( element.normalWorld.z );
  357. _color.updateStyleString();
  358. material.wireframe ? strokePath( _color.__styleString, material.wireframe_linewidth ) : fillPath( _color.__styleString );
  359. }
  360. }
  361. function drawTriangle( x0, y0, x1, y1, x2, y2 ) {
  362. _context.beginPath();
  363. _context.moveTo( x0, y0 );
  364. _context.lineTo( x1, y1 );
  365. _context.lineTo( x2, y2 );
  366. _context.lineTo( x0, y0 );
  367. _context.closePath();
  368. }
  369. /*
  370. function drawQuad( x0, y0, x1, y1, x2, y2, x3, y3 ) {
  371. _context.beginPath();
  372. _context.moveTo( x0, y0 );
  373. _context.lineTo( x1, y1 );
  374. _context.lineTo( x2, y2 );
  375. _context.lineTo( x3, y3 );
  376. _context.lineTo( x0, y0 );
  377. _context.closePath();
  378. }
  379. */
  380. function strokePath( color, linewidth ) {
  381. setStrokeStyle( color );
  382. setLineWidth( linewidth );
  383. _context.stroke();
  384. _bboxRect.inflate( linewidth * 2 );
  385. }
  386. function fillPath( color ) {
  387. setFillStyle( color );
  388. _context.fill();
  389. }
  390. function texturePath( x0, y0, x1, y1, x2, y2, bitmap, u0, v0, u1, v1, u2, v2 ) {
  391. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  392. var a, b, c, d, e, f, det,
  393. width = bitmap.width - 1,
  394. height = bitmap.height - 1;
  395. u0 *= width; v0 *= height;
  396. u1 *= width; v1 *= height;
  397. u2 *= width; v2 *= height;
  398. x1 -= x0; y1 -= y0;
  399. x2 -= x0; y2 -= y0;
  400. u1 -= u0; v1 -= v0;
  401. u2 -= u0; v2 -= v0;
  402. det = 1 / ( u1 * v2 - u2 * v1 ),
  403. a = ( v2 * x1 - v1 * x2 ) * det,
  404. b = ( v2 * y1 - v1 * y2 ) * det,
  405. c = ( u1 * x2 - u2 * x1 ) * det,
  406. d = ( u1 * y2 - u2 * y1 ) * det,
  407. e = x0 - a * u0 - c * v0,
  408. f = y0 - b * u0 - d * v0;
  409. _context.save();
  410. _context.transform( a, b, c, d, e, f );
  411. _context.clip();
  412. _context.drawImage( bitmap, 0, 0 );
  413. _context.restore();
  414. }
  415. //
  416. function setOpacity( value ) {
  417. if ( _contextGlobalAlpha != value ) {
  418. _context.globalAlpha = _contextGlobalAlpha = value;
  419. }
  420. }
  421. function setBlending( value ) {
  422. if ( _contextGlobalCompositeOperation != value ) {
  423. switch ( value ) {
  424. case THREE.NormalBlending:
  425. _context.globalCompositeOperation = 'source-over';
  426. break;
  427. case THREE.AdditiveBlending:
  428. _context.globalCompositeOperation = 'lighter';
  429. break;
  430. case THREE.SubtractiveBlending:
  431. _context.globalCompositeOperation = 'darker';
  432. break;
  433. }
  434. _contextGlobalCompositeOperation = value;
  435. }
  436. }
  437. function setLineWidth( value ) {
  438. if ( _contextLineWidth != value ) {
  439. _context.lineWidth = _contextLineWidth = value;
  440. }
  441. }
  442. function setStrokeStyle( value ) {
  443. if ( _contextStrokeStyle != value ) {
  444. _context.strokeStyle = _contextStrokeStyle = value;
  445. }
  446. }
  447. function setFillStyle( value ) {
  448. if ( _contextFillStyle != value ) {
  449. _context.fillStyle = _contextFillStyle = value;
  450. }
  451. }
  452. function getGradientTexture( color1, color2, color3, color4 ) {
  453. // http://mrdoob.com/blog/post/710
  454. _pixelMapData[ 0 ] = _max( 0, _min( 255, ~~ ( color1.r * 255 ) ) );
  455. _pixelMapData[ 1 ] = _max( 0, _min( 255, ~~ ( color1.g * 255 ) ) );
  456. _pixelMapData[ 2 ] = _max( 0, _min( 255, ~~ ( color1.b * 255 ) ) );
  457. _pixelMapData[ 4 ] = _max( 0, _min( 255, ~~ ( color2.r * 255 ) ) );
  458. _pixelMapData[ 5 ] = _max( 0, _min( 255, ~~ ( color2.g * 255 ) ) );
  459. _pixelMapData[ 6 ] = _max( 0, _min( 255, ~~ ( color2.b * 255 ) ) );
  460. _pixelMapData[ 8 ] = _max( 0, _min( 255, ~~ ( color3.r * 255 ) ) );
  461. _pixelMapData[ 9 ] = _max( 0, _min( 255, ~~ ( color3.g * 255 ) ) );
  462. _pixelMapData[ 10 ] = _max( 0, _min( 255, ~~ ( color3.b * 255 ) ) );
  463. _pixelMapData[ 12 ] = _max( 0, _min( 255, ~~ ( color4.r * 255 ) ) );
  464. _pixelMapData[ 13 ] = _max( 0, _min( 255, ~~ ( color4.g * 255 ) ) );
  465. _pixelMapData[ 14 ] = _max( 0, _min( 255, ~~ ( color4.b * 255 ) ) );
  466. _pixelMapContext.putImageData( _pixelMapImage, 0, 0 );
  467. _gradientMapContext.drawImage( _pixelMap, 0, 0 );
  468. return _gradientMap;
  469. }
  470. function normalToComponent( normal ) {
  471. // https://gist.github.com/665829
  472. return normal < 0 ? _min( ( 1 + normal ) * 0.5, 0.5 ) : 0.5 + _min( normal * 0.5, 0.5 );
  473. }
  474. // Hide anti-alias gaps
  475. function expand( v1, v2 ) {
  476. var x = v2.x - v1.x, y = v2.y - v1.y,
  477. unit = 1 / Math.sqrt( x * x + y * y );
  478. x *= unit; y *= unit;
  479. v2.x += x; v2.y += y;
  480. v1.x -= x; v1.y -= y;
  481. }
  482. };