CanvasRenderer.js 22 KB

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