CanvasRenderer.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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() ) {
  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 ? 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 ) {
  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 ) ) {
  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 ) {
  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 ) ) {
  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 ) {
  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 ) ) {
  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 ) {
  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 ) ) {
  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 ) ) {
  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 ) {
  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 ) {
  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 ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  355. }
  356. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  357. if ( _enableLighting ) {
  358. if ( !material.wireframe && 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 ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  388. }
  389. } else {
  390. material.wireframe ? 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 ? 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 || material.envMap ) {
  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 ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  430. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  431. if ( _enableLighting ) {
  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 ? 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 ? 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 ? 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.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 && repeatY ? 'repeat' : repeatX && !repeatY ? 'repeat-x' : !repeatX && repeatY ? '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 = ( v0 + offsetY ) * height;
  539. u1 = ( u1 + offsetX ) * width;
  540. v1 = ( v1 + offsetY ) * height;
  541. u2 = ( u2 + offsetX ) * width;
  542. v2 = ( 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 = _contextGlobalAlpha = value;
  648. }
  649. }
  650. function setBlending( value ) {
  651. if ( _contextGlobalCompositeOperation != value ) {
  652. switch ( value ) {
  653. case THREE.NormalBlending:
  654. _context.globalCompositeOperation = 'source-over';
  655. break;
  656. case THREE.AdditiveBlending:
  657. _context.globalCompositeOperation = 'lighter';
  658. break;
  659. }
  660. _contextGlobalCompositeOperation = value;
  661. }
  662. }
  663. function setLineWidth( value ) {
  664. if ( _contextLineWidth != value ) {
  665. _context.lineWidth = _contextLineWidth = value;
  666. }
  667. }
  668. function setLineCap( value ) {
  669. // "butt", "round", "square"
  670. if ( _contextLineCap != value ) {
  671. _context.lineCap = _contextLineCap = value;
  672. }
  673. }
  674. function setLineJoin( value ) {
  675. // "round", "bevel", "miter"
  676. if ( _contextLineJoin != value ) {
  677. _context.lineJoin = _contextLineJoin = value;
  678. }
  679. }
  680. function setStrokeStyle( style ) {
  681. if ( _contextStrokeStyle != style ) {
  682. _context.strokeStyle = _contextStrokeStyle = style;
  683. }
  684. }
  685. function setFillStyle( style ) {
  686. if ( _contextFillStyle != style ) {
  687. _context.fillStyle = _contextFillStyle = style;
  688. }
  689. }
  690. };
粤ICP备19079148号