CanvasRenderer.js 33 KB

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