CanvasRenderer.js 33 KB

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