CanvasRenderer.js 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.SpriteCanvasMaterial = function(parameters) {
  5. THREE.Material.call(this);
  6. this.type = 'SpriteCanvasMaterial';
  7. this.color = new THREE.Color(0xffffff);
  8. this.program = function() {};
  9. this.setValues(parameters);
  10. };
  11. THREE.SpriteCanvasMaterial.prototype = Object.create(THREE.Material.prototype);
  12. THREE.SpriteCanvasMaterial.prototype.constructor = THREE.SpriteCanvasMaterial;
  13. THREE.SpriteCanvasMaterial.prototype.isSpriteCanvasMaterial = true;
  14. THREE.SpriteCanvasMaterial.prototype.clone = function() {
  15. var material = new THREE.SpriteCanvasMaterial();
  16. material.copy(this);
  17. material.color.copy(this.color);
  18. material.program = this.program;
  19. return material;
  20. };
  21. //
  22. THREE.CanvasRenderer = function(parameters) {
  23. console.log('THREE.CanvasRenderer', THREE.REVISION);
  24. parameters = parameters || {};
  25. var _this = this,
  26. _renderData, _elements, _lights,
  27. _projector = new THREE.Projector(),
  28. _canvas = parameters.canvas !== undefined ?
  29. parameters.canvas :
  30. document.createElement('canvas'),
  31. _canvasWidth = _canvas.width,
  32. _canvasHeight = _canvas.height,
  33. _canvasWidthHalf = Math.floor(_canvasWidth / 2),
  34. _canvasHeightHalf = Math.floor(_canvasHeight / 2),
  35. _viewportX = 0,
  36. _viewportY = 0,
  37. _viewportWidth = _canvasWidth,
  38. _viewportHeight = _canvasHeight,
  39. _pixelRatio = 1,
  40. _context = _canvas.getContext('2d', {
  41. alpha: parameters.alpha === true
  42. }),
  43. _clearColor = new THREE.Color(0x000000),
  44. _clearAlpha = parameters.alpha === true ? 0 : 1,
  45. _contextGlobalAlpha = 1,
  46. _contextGlobalCompositeOperation = 0,
  47. _contextStrokeStyle = null,
  48. _contextFillStyle = null,
  49. _contextLineWidth = null,
  50. _contextLineCap = null,
  51. _contextLineJoin = null,
  52. _contextLineDash = [],
  53. _v1, _v2, _v3,
  54. _v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
  55. _color = new THREE.Color(),
  56. _diffuseColor = new THREE.Color(),
  57. _emissiveColor = new THREE.Color(),
  58. _lightColor = new THREE.Color(),
  59. _patterns = {},
  60. _uvs,
  61. _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y,
  62. _clipBox = new THREE.Box2(),
  63. _clearBox = new THREE.Box2(),
  64. _elemBox = new THREE.Box2(),
  65. _ambientLight = new THREE.Color(),
  66. _directionalLights = new THREE.Color(),
  67. _pointLights = new THREE.Color(),
  68. _vector3 = new THREE.Vector3(), // Needed for PointLight
  69. _centroid = new THREE.Vector3(),
  70. _normal = new THREE.Vector3(),
  71. _normalViewMatrix = new THREE.Matrix3();
  72. /* TODO
  73. _canvas.mozImageSmoothingEnabled = false;
  74. _canvas.webkitImageSmoothingEnabled = false;
  75. _canvas.msImageSmoothingEnabled = false;
  76. _canvas.imageSmoothingEnabled = false;
  77. */
  78. // dash+gap fallbacks for Firefox and everything else
  79. if (_context.setLineDash === undefined) {
  80. _context.setLineDash = function() {};
  81. }
  82. this.domElement = _canvas;
  83. this.autoClear = true;
  84. this.sortObjects = true;
  85. this.sortElements = true;
  86. this.info = {
  87. render: {
  88. vertices: 0,
  89. faces: 0
  90. }
  91. };
  92. // API
  93. this.getContext = function() {
  94. return _context;
  95. };
  96. this.getContextAttributes = function() {
  97. return _context.getContextAttributes();
  98. };
  99. this.getPixelRatio = function() {
  100. return _pixelRatio;
  101. };
  102. this.setPixelRatio = function(value) {
  103. if (value !== undefined) _pixelRatio = value;
  104. };
  105. this.setSize = function(width, height, updateStyle) {
  106. _canvasWidth = width * _pixelRatio;
  107. _canvasHeight = height * _pixelRatio;
  108. _canvas.width = _canvasWidth;
  109. _canvas.height = _canvasHeight;
  110. _canvasWidthHalf = Math.floor(_canvasWidth / 2);
  111. _canvasHeightHalf = Math.floor(_canvasHeight / 2);
  112. if (updateStyle !== false) {
  113. _canvas.style.width = width + 'px';
  114. _canvas.style.height = height + 'px';
  115. }
  116. _clipBox.min.set(-_canvasWidthHalf, -_canvasHeightHalf);
  117. _clipBox.max.set(_canvasWidthHalf, _canvasHeightHalf);
  118. _clearBox.min.set(-_canvasWidthHalf, -_canvasHeightHalf);
  119. _clearBox.max.set(_canvasWidthHalf, _canvasHeightHalf);
  120. _contextGlobalAlpha = 1;
  121. _contextGlobalCompositeOperation = 0;
  122. _contextStrokeStyle = null;
  123. _contextFillStyle = null;
  124. _contextLineWidth = null;
  125. _contextLineCap = null;
  126. _contextLineJoin = null;
  127. this.setViewport(0, 0, width, height);
  128. };
  129. this.setViewport = function(x, y, width, height) {
  130. _viewportX = x * _pixelRatio;
  131. _viewportY = y * _pixelRatio;
  132. _viewportWidth = width * _pixelRatio;
  133. _viewportHeight = height * _pixelRatio;
  134. };
  135. this.setScissor = function() {};
  136. this.setScissorTest = function() {};
  137. this.setClearColor = function(color, alpha) {
  138. _clearColor.set(color);
  139. _clearAlpha = alpha !== undefined ? alpha : 1;
  140. _clearBox.min.set(-_canvasWidthHalf, -_canvasHeightHalf);
  141. _clearBox.max.set(_canvasWidthHalf, _canvasHeightHalf);
  142. };
  143. this.setClearColorHex = function(hex, alpha) {
  144. console.warn('THREE.CanvasRenderer: .setClearColorHex() is being removed. Use .setClearColor() instead.');
  145. this.setClearColor(hex, alpha);
  146. };
  147. this.getClearColor = function() {
  148. return _clearColor;
  149. };
  150. this.getClearAlpha = function() {
  151. return _clearAlpha;
  152. };
  153. this.getMaxAnisotropy = function() {
  154. return 0;
  155. };
  156. this.clear = function() {
  157. if (_clearBox.isEmpty() === false) {
  158. _clearBox.intersect(_clipBox);
  159. _clearBox.expandByScalar(2);
  160. _clearBox.min.x = _clearBox.min.x + _canvasWidthHalf;
  161. _clearBox.min.y = -_clearBox.min.y + _canvasHeightHalf; // higher y value !
  162. _clearBox.max.x = _clearBox.max.x + _canvasWidthHalf;
  163. _clearBox.max.y = -_clearBox.max.y + _canvasHeightHalf; // lower y value !
  164. if (_clearAlpha < 1) {
  165. _context.clearRect(
  166. _clearBox.min.x | 0,
  167. _clearBox.max.y | 0,
  168. (_clearBox.max.x - _clearBox.min.x) | 0,
  169. (_clearBox.min.y - _clearBox.max.y) | 0
  170. );
  171. }
  172. if (_clearAlpha > 0) {
  173. setOpacity(1);
  174. setBlending(THREE.NormalBlending);
  175. setFillStyle('rgba(' + Math.floor(_clearColor.r * 255) + ',' + Math.floor(_clearColor.g * 255) + ',' + Math.floor(_clearColor.b * 255) + ',' + _clearAlpha + ')');
  176. _context.fillRect(
  177. _clearBox.min.x | 0,
  178. _clearBox.max.y | 0,
  179. (_clearBox.max.x - _clearBox.min.x) | 0,
  180. (_clearBox.min.y - _clearBox.max.y) | 0
  181. );
  182. }
  183. _clearBox.makeEmpty();
  184. }
  185. };
  186. // compatibility
  187. this.clearColor = function() {};
  188. this.clearDepth = function() {};
  189. this.clearStencil = function() {};
  190. this.render = function(scene, camera) {
  191. if (camera.isCamera === undefined) {
  192. console.error('THREE.CanvasRenderer.render: camera is not an instance of THREE.Camera.');
  193. return;
  194. }
  195. var background = scene.background;
  196. if (background && background.isColor) {
  197. setOpacity(1);
  198. setBlending(THREE.NormalBlending);
  199. setFillStyle(background.getStyle());
  200. _context.fillRect(0, 0, _canvasWidth, _canvasHeight);
  201. } else if (this.autoClear === true) {
  202. this.clear();
  203. }
  204. _this.info.render.vertices = 0;
  205. _this.info.render.faces = 0;
  206. _context.setTransform(_viewportWidth / _canvasWidth, 0, 0, -_viewportHeight / _canvasHeight, _viewportX, _canvasHeight - _viewportY);
  207. _context.translate(_canvasWidthHalf, _canvasHeightHalf);
  208. _renderData = _projector.projectScene(scene, camera, this.sortObjects, this.sortElements);
  209. _elements = _renderData.elements;
  210. _lights = _renderData.lights;
  211. _normalViewMatrix.getNormalMatrix(camera.matrixWorldInverse);
  212. /* DEBUG
  213. setFillStyle( 'rgba( 0, 255, 255, 0.5 )' );
  214. _context.fillRect( _clipBox.min.x, _clipBox.min.y, _clipBox.max.x - _clipBox.min.x, _clipBox.max.y - _clipBox.min.y );
  215. */
  216. calculateLights();
  217. for (var e = 0, el = _elements.length; e < el; e++) {
  218. var element = _elements[e];
  219. var material = element.material;
  220. if (material === undefined || material.opacity === 0) continue;
  221. _elemBox.makeEmpty();
  222. if (element instanceof THREE.RenderableSprite) {
  223. _v1 = element;
  224. _v1.x *= _canvasWidthHalf;
  225. _v1.y *= _canvasHeightHalf;
  226. renderSprite(_v1, element, material);
  227. } else if (element instanceof THREE.RenderableLine) {
  228. _v1 = element.v1;
  229. _v2 = element.v2;
  230. _v1.positionScreen.x *= _canvasWidthHalf;
  231. _v1.positionScreen.y *= _canvasHeightHalf;
  232. _v2.positionScreen.x *= _canvasWidthHalf;
  233. _v2.positionScreen.y *= _canvasHeightHalf;
  234. _elemBox.setFromPoints([
  235. _v1.positionScreen,
  236. _v2.positionScreen
  237. ]);
  238. if (_clipBox.intersectsBox(_elemBox) === true) {
  239. renderLine(_v1, _v2, element, material);
  240. }
  241. } else if (element instanceof THREE.RenderableFace) {
  242. _v1 = element.v1;
  243. _v2 = element.v2;
  244. _v3 = element.v3;
  245. if (_v1.positionScreen.z < -1 || _v1.positionScreen.z > 1) continue;
  246. if (_v2.positionScreen.z < -1 || _v2.positionScreen.z > 1) continue;
  247. if (_v3.positionScreen.z < -1 || _v3.positionScreen.z > 1) continue;
  248. _v1.positionScreen.x *= _canvasWidthHalf;
  249. _v1.positionScreen.y *= _canvasHeightHalf;
  250. _v2.positionScreen.x *= _canvasWidthHalf;
  251. _v2.positionScreen.y *= _canvasHeightHalf;
  252. _v3.positionScreen.x *= _canvasWidthHalf;
  253. _v3.positionScreen.y *= _canvasHeightHalf;
  254. if (material.overdraw > 0) {
  255. expand(_v1.positionScreen, _v2.positionScreen, material.overdraw);
  256. expand(_v2.positionScreen, _v3.positionScreen, material.overdraw);
  257. expand(_v3.positionScreen, _v1.positionScreen, material.overdraw);
  258. }
  259. _elemBox.setFromPoints([
  260. _v1.positionScreen,
  261. _v2.positionScreen,
  262. _v3.positionScreen
  263. ]);
  264. if (_clipBox.intersectsBox(_elemBox) === true) {
  265. renderFace3(_v1, _v2, _v3, 0, 1, 2, element, material);
  266. }
  267. }
  268. /* DEBUG
  269. setLineWidth( 1 );
  270. setStrokeStyle( 'rgba( 0, 255, 0, 0.5 )' );
  271. _context.strokeRect( _elemBox.min.x, _elemBox.min.y, _elemBox.max.x - _elemBox.min.x, _elemBox.max.y - _elemBox.min.y );
  272. */
  273. _clearBox.union(_elemBox);
  274. }
  275. /* DEBUG
  276. setLineWidth( 1 );
  277. setStrokeStyle( 'rgba( 255, 0, 0, 0.5 )' );
  278. _context.strokeRect( _clearBox.min.x, _clearBox.min.y, _clearBox.max.x - _clearBox.min.x, _clearBox.max.y - _clearBox.min.y );
  279. */
  280. _context.setTransform(1, 0, 0, 1, 0, 0);
  281. };
  282. //
  283. function calculateLights() {
  284. _ambientLight.setRGB(0, 0, 0);
  285. _directionalLights.setRGB(0, 0, 0);
  286. _pointLights.setRGB(0, 0, 0);
  287. for (var l = 0, ll = _lights.length; l < ll; l++) {
  288. var light = _lights[l];
  289. var lightColor = light.color;
  290. if (light.isAmbientLight) {
  291. _ambientLight.add(lightColor);
  292. } else if (light.isDirectionalLight) {
  293. // for sprites
  294. _directionalLights.add(lightColor);
  295. } else if (light.isPointLight) {
  296. // for sprites
  297. _pointLights.add(lightColor);
  298. }
  299. }
  300. }
  301. function calculateLight(position, normal, color) {
  302. for (var l = 0, ll = _lights.length; l < ll; l++) {
  303. var light = _lights[l];
  304. _lightColor.copy(light.color);
  305. if (light.isDirectionalLight) {
  306. var lightPosition = _vector3.setFromMatrixPosition(light.matrixWorld).normalize();
  307. var amount = normal.dot(lightPosition);
  308. if (amount <= 0) continue;
  309. amount *= light.intensity;
  310. color.add(_lightColor.multiplyScalar(amount));
  311. } else if (light.isPointLight) {
  312. var lightPosition = _vector3.setFromMatrixPosition(light.matrixWorld);
  313. var amount = normal.dot(_vector3.subVectors(lightPosition, position).normalize());
  314. if (amount <= 0) continue;
  315. amount *= light.distance == 0 ? 1 : 1 - Math.min(position.distanceTo(lightPosition) / light.distance, 1);
  316. if (amount == 0) continue;
  317. amount *= light.intensity;
  318. color.add(_lightColor.multiplyScalar(amount));
  319. }
  320. }
  321. }
  322. function renderSprite(v1, element, material) {
  323. setOpacity(material.opacity);
  324. setBlending(material.blending);
  325. var scaleX = element.scale.x * _canvasWidthHalf;
  326. var scaleY = element.scale.y * _canvasHeightHalf;
  327. var dist = Math.sqrt(scaleX * scaleX + scaleY * scaleY); // allow for rotated sprite
  328. _elemBox.min.set(v1.x - dist, v1.y - dist);
  329. _elemBox.max.set(v1.x + dist, v1.y + dist);
  330. if (material.isSpriteMaterial) {
  331. var texture = material.map;
  332. if (texture !== null) {
  333. var pattern = _patterns[texture.id];
  334. if (pattern === undefined || pattern.version !== texture.version) {
  335. pattern = textureToPattern(texture);
  336. _patterns[texture.id] = pattern;
  337. }
  338. if (pattern.canvas !== undefined) {
  339. setFillStyle(pattern.canvas);
  340. var bitmap = texture.image;
  341. var ox = bitmap.width * texture.offset.x;
  342. var oy = bitmap.height * texture.offset.y;
  343. var sx = bitmap.width * texture.repeat.x;
  344. var sy = bitmap.height * texture.repeat.y;
  345. var cx = scaleX / sx;
  346. var cy = scaleY / sy;
  347. _context.save();
  348. _context.translate(v1.x, v1.y);
  349. if (material.rotation !== 0) _context.rotate(material.rotation);
  350. _context.translate(-scaleX / 2, -scaleY / 2);
  351. _context.scale(cx, cy);
  352. _context.translate(-ox, -oy);
  353. _context.fillRect(ox, oy, sx, sy);
  354. _context.restore();
  355. }
  356. } else {
  357. // no texture
  358. setFillStyle(material.color.getStyle());
  359. _context.save();
  360. _context.translate(v1.x, v1.y);
  361. if (material.rotation !== 0) _context.rotate(material.rotation);
  362. _context.scale(scaleX, -scaleY);
  363. _context.fillRect(-0.5, -0.5, 1, 1);
  364. _context.restore();
  365. }
  366. } else if (material.isSpriteCanvasMaterial) {
  367. setStrokeStyle(material.color.getStyle());
  368. setFillStyle(material.color.getStyle());
  369. _context.save();
  370. _context.translate(v1.x, v1.y);
  371. if (material.rotation !== 0) _context.rotate(material.rotation);
  372. _context.scale(scaleX, scaleY);
  373. material.program(_context);
  374. _context.restore();
  375. } else if (material.isPointsMaterial) {
  376. setFillStyle(material.color.getStyle());
  377. _context.save();
  378. _context.translate(v1.x, v1.y);
  379. if (material.rotation !== 0) _context.rotate(material.rotation);
  380. _context.scale(scaleX * material.size, -scaleY * material.size);
  381. _context.fillRect(-0.5, -0.5, 1, 1);
  382. _context.restore();
  383. }
  384. /* DEBUG
  385. setStrokeStyle( 'rgb(255,255,0)' );
  386. _context.beginPath();
  387. _context.moveTo( v1.x - 10, v1.y );
  388. _context.lineTo( v1.x + 10, v1.y );
  389. _context.moveTo( v1.x, v1.y - 10 );
  390. _context.lineTo( v1.x, v1.y + 10 );
  391. _context.stroke();
  392. */
  393. }
  394. function renderLine(v1, v2, element, material) {
  395. setOpacity(material.opacity);
  396. setBlending(material.blending);
  397. _context.beginPath();
  398. _context.moveTo(v1.positionScreen.x, v1.positionScreen.y);
  399. _context.lineTo(v2.positionScreen.x, v2.positionScreen.y);
  400. if (material.isLineBasicMaterial) {
  401. setLineWidth(material.linewidth);
  402. setLineCap(material.linecap);
  403. setLineJoin(material.linejoin);
  404. if (material.vertexColors !== THREE.VertexColors) {
  405. setStrokeStyle(material.color.getStyle());
  406. } else {
  407. var colorStyle1 = element.vertexColors[0].getStyle();
  408. var colorStyle2 = element.vertexColors[1].getStyle();
  409. if (colorStyle1 === colorStyle2) {
  410. setStrokeStyle(colorStyle1);
  411. } else {
  412. try {
  413. var grad = _context.createLinearGradient(
  414. v1.positionScreen.x,
  415. v1.positionScreen.y,
  416. v2.positionScreen.x,
  417. v2.positionScreen.y
  418. );
  419. grad.addColorStop(0, colorStyle1);
  420. grad.addColorStop(1, colorStyle2);
  421. } catch (exception) {
  422. grad = colorStyle1;
  423. }
  424. setStrokeStyle(grad);
  425. }
  426. }
  427. if (material.isLineDashedMaterial) {
  428. setLineDash([material.dashSize, material.gapSize]);
  429. }
  430. _context.stroke();
  431. _elemBox.expandByScalar(material.linewidth * 2);
  432. if (material.isLineDashedMaterial) {
  433. setLineDash([]);
  434. }
  435. }
  436. }
  437. function renderFace3(v1, v2, v3, uv1, uv2, uv3, element, material) {
  438. _this.info.render.vertices += 3;
  439. _this.info.render.faces++;
  440. setOpacity(material.opacity);
  441. setBlending(material.blending);
  442. _v1x = v1.positionScreen.x;
  443. _v1y = v1.positionScreen.y;
  444. _v2x = v2.positionScreen.x;
  445. _v2y = v2.positionScreen.y;
  446. _v3x = v3.positionScreen.x;
  447. _v3y = v3.positionScreen.y;
  448. drawTriangle(_v1x, _v1y, _v2x, _v2y, _v3x, _v3y);
  449. if ((material.isMeshLambertMaterial || material.isMeshPhongMaterial || material.isMeshStandardMaterial) && material.map === null) {
  450. _diffuseColor.copy(material.color);
  451. _emissiveColor.copy(material.emissive);
  452. if (material.vertexColors === THREE.FaceColors) {
  453. _diffuseColor.multiply(element.color);
  454. }
  455. _color.copy(_ambientLight);
  456. _centroid.copy(v1.positionWorld).add(v2.positionWorld).add(v3.positionWorld).divideScalar(3);
  457. calculateLight(_centroid, element.normalModel, _color);
  458. _color.multiply(_diffuseColor).add(_emissiveColor);
  459. material.wireframe === true ?
  460. strokePath(_color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin) :
  461. fillPath(_color);
  462. } else if (material.isMeshBasicMaterial || material.isMeshLambertMaterial || material.isMeshPhongMaterial || material.isMeshStandardMaterial) {
  463. if (material.map !== null) {
  464. var mapping = material.map.mapping;
  465. if (mapping === THREE.UVMapping) {
  466. _uvs = element.uvs;
  467. patternPath(_v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uvs[uv1].x, _uvs[uv1].y, _uvs[uv2].x, _uvs[uv2].y, _uvs[uv3].x, _uvs[uv3].y, material.map);
  468. }
  469. } else if (material.envMap !== null) {
  470. if (material.envMap.mapping === THREE.SphericalReflectionMapping) {
  471. _normal.copy(element.vertexNormalsModel[uv1]).applyMatrix3(_normalViewMatrix);
  472. _uv1x = 0.5 * _normal.x + 0.5;
  473. _uv1y = 0.5 * _normal.y + 0.5;
  474. _normal.copy(element.vertexNormalsModel[uv2]).applyMatrix3(_normalViewMatrix);
  475. _uv2x = 0.5 * _normal.x + 0.5;
  476. _uv2y = 0.5 * _normal.y + 0.5;
  477. _normal.copy(element.vertexNormalsModel[uv3]).applyMatrix3(_normalViewMatrix);
  478. _uv3x = 0.5 * _normal.x + 0.5;
  479. _uv3y = 0.5 * _normal.y + 0.5;
  480. patternPath(_v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y, material.envMap);
  481. }
  482. } else {
  483. _color.copy(material.color);
  484. if (material.vertexColors === THREE.FaceColors) {
  485. _color.multiply(element.color);
  486. }
  487. material.wireframe === true ?
  488. strokePath(_color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin) :
  489. fillPath(_color);
  490. }
  491. } else if (material.isMeshNormalMaterial) {
  492. _normal.copy(element.normalModel).applyMatrix3(_normalViewMatrix);
  493. _color.setRGB(_normal.x, _normal.y, _normal.z).multiplyScalar(0.5).addScalar(0.5);
  494. material.wireframe === true ?
  495. strokePath(_color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin) :
  496. fillPath(_color);
  497. } else {
  498. _color.setRGB(1, 1, 1);
  499. material.wireframe === true ?
  500. strokePath(_color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin) :
  501. fillPath(_color);
  502. }
  503. }
  504. //
  505. function drawTriangle(x0, y0, x1, y1, x2, y2) {
  506. _context.beginPath();
  507. _context.moveTo(x0, y0);
  508. _context.lineTo(x1, y1);
  509. _context.lineTo(x2, y2);
  510. _context.closePath();
  511. }
  512. function strokePath(color, linewidth, linecap, linejoin) {
  513. setLineWidth(linewidth);
  514. setLineCap(linecap);
  515. setLineJoin(linejoin);
  516. setStrokeStyle(color.getStyle());
  517. _context.stroke();
  518. _elemBox.expandByScalar(linewidth * 2);
  519. }
  520. function fillPath(color) {
  521. setFillStyle(color.getStyle());
  522. _context.fill();
  523. }
  524. function textureToPattern(texture) {
  525. if (texture.version === 0 ||
  526. texture instanceof THREE.CompressedTexture ||
  527. texture instanceof THREE.DataTexture) {
  528. return {
  529. canvas: undefined,
  530. version: texture.version
  531. };
  532. }
  533. var image = texture.image;
  534. if (image.complete === false) {
  535. return {
  536. canvas: undefined,
  537. version: 0
  538. };
  539. }
  540. var repeatX = texture.wrapS === THREE.RepeatWrapping || texture.wrapS === THREE.MirroredRepeatWrapping;
  541. var repeatY = texture.wrapT === THREE.RepeatWrapping || texture.wrapT === THREE.MirroredRepeatWrapping;
  542. var mirrorX = texture.wrapS === THREE.MirroredRepeatWrapping;
  543. var mirrorY = texture.wrapT === THREE.MirroredRepeatWrapping;
  544. //
  545. var canvas = document.createElement('canvas');
  546. canvas.width = image.width * (mirrorX ? 2 : 1);
  547. canvas.height = image.height * (mirrorY ? 2 : 1);
  548. var context = canvas.getContext('2d');
  549. context.setTransform(1, 0, 0, -1, 0, image.height);
  550. context.drawImage(image, 0, 0);
  551. if (mirrorX === true) {
  552. context.setTransform(-1, 0, 0, -1, image.width, image.height);
  553. context.drawImage(image, -image.width, 0);
  554. }
  555. if (mirrorY === true) {
  556. context.setTransform(1, 0, 0, 1, 0, 0);
  557. context.drawImage(image, 0, image.height);
  558. }
  559. if (mirrorX === true && mirrorY === true) {
  560. context.setTransform(-1, 0, 0, 1, image.width, 0);
  561. context.drawImage(image, -image.width, image.height);
  562. }
  563. var repeat = 'no-repeat';
  564. if (repeatX === true && repeatY === true) {
  565. repeat = 'repeat';
  566. } else if (repeatX === true) {
  567. repeat = 'repeat-x';
  568. } else if (repeatY === true) {
  569. repeat = 'repeat-y';
  570. }
  571. var pattern = _context.createPattern(canvas, repeat);
  572. if (texture.onUpdate) texture.onUpdate(texture);
  573. return {
  574. canvas: pattern,
  575. version: texture.version
  576. };
  577. }
  578. function patternPath(x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, texture) {
  579. var pattern = _patterns[texture.id];
  580. if (pattern === undefined || pattern.version !== texture.version) {
  581. pattern = textureToPattern(texture);
  582. _patterns[texture.id] = pattern;
  583. }
  584. if (pattern.canvas !== undefined) {
  585. setFillStyle(pattern.canvas);
  586. } else {
  587. setFillStyle('rgba( 0, 0, 0, 1)');
  588. _context.fill();
  589. return;
  590. }
  591. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  592. var a, b, c, d, e, f, det, idet,
  593. offsetX = texture.offset.x / texture.repeat.x,
  594. offsetY = texture.offset.y / texture.repeat.y,
  595. width = texture.image.width * texture.repeat.x,
  596. height = texture.image.height * texture.repeat.y;
  597. u0 = (u0 + offsetX) * width;
  598. v0 = (v0 + offsetY) * height;
  599. u1 = (u1 + offsetX) * width;
  600. v1 = (v1 + offsetY) * height;
  601. u2 = (u2 + offsetX) * width;
  602. v2 = (v2 + offsetY) * height;
  603. x1 -= x0;
  604. y1 -= y0;
  605. x2 -= x0;
  606. y2 -= y0;
  607. u1 -= u0;
  608. v1 -= v0;
  609. u2 -= u0;
  610. v2 -= v0;
  611. det = u1 * v2 - u2 * v1;
  612. if (det === 0) return;
  613. idet = 1 / det;
  614. a = (v2 * x1 - v1 * x2) * idet;
  615. b = (v2 * y1 - v1 * y2) * idet;
  616. c = (u1 * x2 - u2 * x1) * idet;
  617. d = (u1 * y2 - u2 * y1) * idet;
  618. e = x0 - a * u0 - c * v0;
  619. f = y0 - b * u0 - d * v0;
  620. _context.save();
  621. _context.transform(a, b, c, d, e, f);
  622. _context.fill();
  623. _context.restore();
  624. }
  625. /*
  626. function clipImage( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, image ) {
  627. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  628. var a, b, c, d, e, f, det, idet,
  629. width = image.width - 1,
  630. height = image.height - 1;
  631. u0 *= width; v0 *= height;
  632. u1 *= width; v1 *= height;
  633. u2 *= width; v2 *= height;
  634. x1 -= x0; y1 -= y0;
  635. x2 -= x0; y2 -= y0;
  636. u1 -= u0; v1 -= v0;
  637. u2 -= u0; v2 -= v0;
  638. det = u1 * v2 - u2 * v1;
  639. idet = 1 / det;
  640. a = ( v2 * x1 - v1 * x2 ) * idet;
  641. b = ( v2 * y1 - v1 * y2 ) * idet;
  642. c = ( u1 * x2 - u2 * x1 ) * idet;
  643. d = ( u1 * y2 - u2 * y1 ) * idet;
  644. e = x0 - a * u0 - c * v0;
  645. f = y0 - b * u0 - d * v0;
  646. _context.save();
  647. _context.transform( a, b, c, d, e, f );
  648. _context.clip();
  649. _context.drawImage( image, 0, 0 );
  650. _context.restore();
  651. }
  652. */
  653. // Hide anti-alias gaps
  654. function expand(v1, v2, pixels) {
  655. var x = v2.x - v1.x,
  656. y = v2.y - v1.y,
  657. det = x * x + y * y,
  658. idet;
  659. if (det === 0) return;
  660. idet = pixels / Math.sqrt(det);
  661. x *= idet;
  662. y *= idet;
  663. v2.x += x;
  664. v2.y += y;
  665. v1.x -= x;
  666. v1.y -= y;
  667. }
  668. // Context cached methods.
  669. function setOpacity(value) {
  670. if (_contextGlobalAlpha !== value) {
  671. _context.globalAlpha = value;
  672. _contextGlobalAlpha = value;
  673. }
  674. }
  675. function setBlending(value) {
  676. if (_contextGlobalCompositeOperation !== value) {
  677. if (value === THREE.NormalBlending) {
  678. _context.globalCompositeOperation = 'source-over';
  679. } else if (value === THREE.AdditiveBlending) {
  680. _context.globalCompositeOperation = 'lighter';
  681. } else if (value === THREE.SubtractiveBlending) {
  682. _context.globalCompositeOperation = 'darker';
  683. } else if (value === THREE.MultiplyBlending) {
  684. _context.globalCompositeOperation = 'multiply';
  685. }
  686. _contextGlobalCompositeOperation = value;
  687. }
  688. }
  689. function setLineWidth(value) {
  690. if (_contextLineWidth !== value) {
  691. _context.lineWidth = value;
  692. _contextLineWidth = value;
  693. }
  694. }
  695. function setLineCap(value) {
  696. // "butt", "round", "square"
  697. if (_contextLineCap !== value) {
  698. _context.lineCap = value;
  699. _contextLineCap = value;
  700. }
  701. }
  702. function setLineJoin(value) {
  703. // "round", "bevel", "miter"
  704. if (_contextLineJoin !== value) {
  705. _context.lineJoin = value;
  706. _contextLineJoin = value;
  707. }
  708. }
  709. function setStrokeStyle(value) {
  710. if (_contextStrokeStyle !== value) {
  711. _context.strokeStyle = value;
  712. _contextStrokeStyle = value;
  713. }
  714. }
  715. function setFillStyle(value) {
  716. if (_contextFillStyle !== value) {
  717. _context.fillStyle = value;
  718. _contextFillStyle = value;
  719. }
  720. }
  721. function setLineDash(value) {
  722. if (_contextLineDash.length !== value.length) {
  723. _context.setLineDash(value);
  724. _contextLineDash = value;
  725. }
  726. }
  727. };