智慧水务管理系统 - 精河县供水工程综合管理平台

GlobeTranslucencyState.js 32KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. import combine from "../Core/combine.js";
  2. import defined from "../Core/defined.js";
  3. import NearFarScalar from "../Core/NearFarScalar.js";
  4. import Rectangle from "../Core/Rectangle.js";
  5. import DrawCommand from "../Renderer/DrawCommand.js";
  6. import Pass from "../Renderer/Pass.js";
  7. import RenderState from "../Renderer/RenderState.js";
  8. import ShaderSource from "../Renderer/ShaderSource.js";
  9. import BlendingState from "./BlendingState.js";
  10. import CullFace from "./CullFace.js";
  11. import SceneMode from "./SceneMode.js";
  12. const DerivedCommandType = {
  13. OPAQUE_FRONT_FACE: 0,
  14. OPAQUE_BACK_FACE: 1,
  15. DEPTH_ONLY_FRONT_FACE: 2,
  16. DEPTH_ONLY_BACK_FACE: 3,
  17. DEPTH_ONLY_FRONT_AND_BACK_FACE: 4,
  18. TRANSLUCENT_FRONT_FACE: 5,
  19. TRANSLUCENT_BACK_FACE: 6,
  20. TRANSLUCENT_FRONT_FACE_MANUAL_DEPTH_TEST: 7,
  21. TRANSLUCENT_BACK_FACE_MANUAL_DEPTH_TEST: 8,
  22. PICK_FRONT_FACE: 9,
  23. PICK_BACK_FACE: 10,
  24. DERIVED_COMMANDS_MAXIMUM_LENGTH: 11,
  25. };
  26. const derivedCommandsMaximumLength =
  27. DerivedCommandType.DERIVED_COMMANDS_MAXIMUM_LENGTH;
  28. const DerivedCommandNames = [
  29. "opaqueFrontFaceCommand",
  30. "opaqueBackFaceCommand",
  31. "depthOnlyFrontFaceCommand",
  32. "depthOnlyBackFaceCommand",
  33. "depthOnlyFrontAndBackFaceCommand",
  34. "translucentFrontFaceCommand",
  35. "translucentBackFaceCommand",
  36. "translucentFrontFaceManualDepthTestCommand",
  37. "translucentBackFaceManualDepthTestCommand",
  38. "pickFrontFaceCommand",
  39. "pickBackFaceCommand",
  40. ];
  41. /**
  42. * @private
  43. */
  44. function GlobeTranslucencyState() {
  45. this._frontFaceAlphaByDistance = new NearFarScalar(0.0, 1.0, 0.0, 1.0);
  46. this._backFaceAlphaByDistance = new NearFarScalar(0.0, 1.0, 0.0, 1.0);
  47. this._frontFaceTranslucent = false;
  48. this._backFaceTranslucent = false;
  49. this._requiresManualDepthTest = false;
  50. this._sunVisibleThroughGlobe = false;
  51. this._environmentVisible = false;
  52. this._useDepthPlane = false;
  53. this._numberOfTextureUniforms = 0;
  54. this._globeTranslucencyFramebuffer = undefined;
  55. this._rectangle = Rectangle.clone(Rectangle.MAX_VALUE);
  56. this._derivedCommandKey = 0;
  57. this._derivedCommandsDirty = false;
  58. this._derivedCommandPacks = undefined;
  59. this._derivedCommandTypes = new Array(derivedCommandsMaximumLength);
  60. this._derivedBlendCommandTypes = new Array(derivedCommandsMaximumLength);
  61. this._derivedPickCommandTypes = new Array(derivedCommandsMaximumLength);
  62. this._derivedCommandTypesToUpdate = new Array(derivedCommandsMaximumLength);
  63. this._derivedCommandsLength = 0;
  64. this._derivedBlendCommandsLength = 0;
  65. this._derivedPickCommandsLength = 0;
  66. this._derivedCommandsToUpdateLength = 0;
  67. }
  68. Object.defineProperties(GlobeTranslucencyState.prototype, {
  69. frontFaceAlphaByDistance: {
  70. get: function () {
  71. return this._frontFaceAlphaByDistance;
  72. },
  73. },
  74. backFaceAlphaByDistance: {
  75. get: function () {
  76. return this._backFaceAlphaByDistance;
  77. },
  78. },
  79. translucent: {
  80. get: function () {
  81. return this._frontFaceTranslucent;
  82. },
  83. },
  84. sunVisibleThroughGlobe: {
  85. get: function () {
  86. return this._sunVisibleThroughGlobe;
  87. },
  88. },
  89. environmentVisible: {
  90. get: function () {
  91. return this._environmentVisible;
  92. },
  93. },
  94. useDepthPlane: {
  95. get: function () {
  96. return this._useDepthPlane;
  97. },
  98. },
  99. numberOfTextureUniforms: {
  100. get: function () {
  101. return this._numberOfTextureUniforms;
  102. },
  103. },
  104. rectangle: {
  105. get: function () {
  106. return this._rectangle;
  107. },
  108. },
  109. });
  110. GlobeTranslucencyState.prototype.update = function (scene) {
  111. const globe = scene.globe;
  112. if (!defined(globe) || !globe.show) {
  113. this._frontFaceTranslucent = false;
  114. this._backFaceTranslucent = false;
  115. this._sunVisibleThroughGlobe = true;
  116. this._environmentVisible = true;
  117. this._useDepthPlane = false;
  118. return;
  119. }
  120. this._frontFaceAlphaByDistance = updateAlphaByDistance(
  121. globe.translucency.enabled,
  122. globe.translucency.frontFaceAlpha,
  123. globe.translucency.frontFaceAlphaByDistance,
  124. this._frontFaceAlphaByDistance,
  125. );
  126. this._backFaceAlphaByDistance = updateAlphaByDistance(
  127. globe.translucency.enabled,
  128. globe.translucency.backFaceAlpha,
  129. globe.translucency.backFaceAlphaByDistance,
  130. this._backFaceAlphaByDistance,
  131. );
  132. this._frontFaceTranslucent = isFaceTranslucent(
  133. globe.translucency.enabled,
  134. this._frontFaceAlphaByDistance,
  135. globe,
  136. );
  137. this._backFaceTranslucent = isFaceTranslucent(
  138. globe.translucency.enabled,
  139. this._backFaceAlphaByDistance,
  140. globe,
  141. );
  142. this._requiresManualDepthTest = requiresManualDepthTest(this, scene, globe);
  143. this._sunVisibleThroughGlobe = isSunVisibleThroughGlobe(this, scene);
  144. this._environmentVisible = isEnvironmentVisible(this, scene);
  145. this._useDepthPlane = useDepthPlane(this, scene);
  146. this._numberOfTextureUniforms = getNumberOfTextureUniforms(this);
  147. this._rectangle = Rectangle.clone(
  148. globe.translucency.rectangle,
  149. this._rectangle,
  150. );
  151. gatherDerivedCommandRequirements(this, scene);
  152. };
  153. function updateAlphaByDistance(enabled, alpha, alphaByDistance, result) {
  154. if (!enabled) {
  155. result.nearValue = 1.0;
  156. result.farValue = 1.0;
  157. return result;
  158. }
  159. if (!defined(alphaByDistance)) {
  160. result.nearValue = alpha;
  161. result.farValue = alpha;
  162. return result;
  163. }
  164. NearFarScalar.clone(alphaByDistance, result);
  165. result.nearValue *= alpha;
  166. result.farValue *= alpha;
  167. return result;
  168. }
  169. function isFaceTranslucent(translucencyEnabled, alphaByDistance, globe) {
  170. return (
  171. translucencyEnabled &&
  172. (globe.baseColor.alpha < 1.0 ||
  173. alphaByDistance.nearValue < 1.0 ||
  174. alphaByDistance.farValue < 1.0)
  175. );
  176. }
  177. function isSunVisibleThroughGlobe(state, scene) {
  178. // The sun is visible through the globe if the front and back faces are translucent when above ground
  179. // or if front faces are translucent when below ground
  180. const frontTranslucent = state._frontFaceTranslucent;
  181. const backTranslucent = state._backFaceTranslucent;
  182. return frontTranslucent && (scene.cameraUnderground || backTranslucent);
  183. }
  184. function isEnvironmentVisible(state, scene) {
  185. // The environment is visible if the camera is above ground or underground with translucency
  186. return !scene.cameraUnderground || state._frontFaceTranslucent;
  187. }
  188. function useDepthPlane(state, scene) {
  189. // Use the depth plane when the camera is above ground and the globe is opaque
  190. return !scene.cameraUnderground && !state._frontFaceTranslucent;
  191. }
  192. function requiresManualDepthTest(state, scene, globe) {
  193. return (
  194. state._frontFaceTranslucent &&
  195. !state._backFaceTranslucent &&
  196. !globe.depthTestAgainstTerrain &&
  197. scene.mode !== SceneMode.SCENE2D &&
  198. scene.context.depthTexture
  199. );
  200. }
  201. function getNumberOfTextureUniforms(state) {
  202. let numberOfTextureUniforms = 0;
  203. if (state._frontFaceTranslucent) {
  204. ++numberOfTextureUniforms; // classification texture
  205. }
  206. if (state._requiresManualDepthTest) {
  207. ++numberOfTextureUniforms; // czm_globeDepthTexture for manual depth testing
  208. }
  209. return numberOfTextureUniforms;
  210. }
  211. function gatherDerivedCommandRequirements(state, scene) {
  212. state._derivedCommandsLength = getDerivedCommandTypes(
  213. state,
  214. scene,
  215. false,
  216. false,
  217. state._derivedCommandTypes,
  218. );
  219. state._derivedBlendCommandsLength = getDerivedCommandTypes(
  220. state,
  221. scene,
  222. true,
  223. false,
  224. state._derivedBlendCommandTypes,
  225. );
  226. state._derivedPickCommandsLength = getDerivedCommandTypes(
  227. state,
  228. scene,
  229. false,
  230. true,
  231. state._derivedPickCommandTypes,
  232. );
  233. let i;
  234. let derivedCommandKey = 0;
  235. for (i = 0; i < state._derivedCommandsLength; ++i) {
  236. derivedCommandKey |= 1 << state._derivedCommandTypes[i];
  237. }
  238. for (i = 0; i < state._derivedBlendCommandsLength; ++i) {
  239. derivedCommandKey |= 1 << state._derivedBlendCommandTypes[i];
  240. }
  241. for (i = 0; i < state._derivedPickCommandsLength; ++i) {
  242. derivedCommandKey |= 1 << state._derivedPickCommandTypes[i];
  243. }
  244. let derivedCommandsToUpdateLength = 0;
  245. for (i = 0; i < derivedCommandsMaximumLength; ++i) {
  246. if ((derivedCommandKey & (1 << i)) > 0) {
  247. state._derivedCommandTypesToUpdate[derivedCommandsToUpdateLength++] = i;
  248. }
  249. }
  250. state._derivedCommandsToUpdateLength = derivedCommandsToUpdateLength;
  251. const derivedCommandsDirty = derivedCommandKey !== state._derivedCommandKey;
  252. state._derivedCommandKey = derivedCommandKey;
  253. state._derivedCommandsDirty = derivedCommandsDirty;
  254. if (!defined(state._derivedCommandPacks) && state._frontFaceTranslucent) {
  255. state._derivedCommandPacks = createDerivedCommandPacks();
  256. }
  257. }
  258. function getDerivedCommandTypes(
  259. state,
  260. scene,
  261. isBlendCommand,
  262. isPickCommand,
  263. types,
  264. ) {
  265. let length = 0;
  266. const frontTranslucent = state._frontFaceTranslucent;
  267. const backTranslucent = state._backFaceTranslucent;
  268. if (!frontTranslucent) {
  269. // Don't use derived commands if the globe is opaque
  270. return length;
  271. }
  272. const cameraUnderground = scene.cameraUnderground;
  273. const requiresManualDepthTest = state._requiresManualDepthTest;
  274. const translucentFrontFaceCommandType = isPickCommand
  275. ? DerivedCommandType.PICK_FRONT_FACE
  276. : requiresManualDepthTest
  277. ? DerivedCommandType.TRANSLUCENT_FRONT_FACE_MANUAL_DEPTH_TEST
  278. : DerivedCommandType.TRANSLUCENT_FRONT_FACE;
  279. const translucentBackFaceCommandType = isPickCommand
  280. ? DerivedCommandType.PICK_BACK_FACE
  281. : requiresManualDepthTest
  282. ? DerivedCommandType.TRANSLUCENT_BACK_FACE_MANUAL_DEPTH_TEST
  283. : DerivedCommandType.TRANSLUCENT_BACK_FACE;
  284. if (scene.mode === SceneMode.SCENE2D) {
  285. types[length++] = DerivedCommandType.DEPTH_ONLY_FRONT_FACE;
  286. types[length++] = translucentFrontFaceCommandType;
  287. return length;
  288. }
  289. if (backTranslucent) {
  290. // Push depth-only command for classification. Blend commands do not need to write depth.
  291. // Push translucent commands for front and back faces.
  292. if (!isBlendCommand) {
  293. types[length++] = DerivedCommandType.DEPTH_ONLY_FRONT_AND_BACK_FACE;
  294. }
  295. if (cameraUnderground) {
  296. types[length++] = translucentFrontFaceCommandType;
  297. types[length++] = translucentBackFaceCommandType;
  298. } else {
  299. types[length++] = translucentBackFaceCommandType;
  300. types[length++] = translucentFrontFaceCommandType;
  301. }
  302. } else {
  303. // Push opaque command for the face that appears in back.
  304. // Push depth-only command and translucent command for the face that appears in front.
  305. // eslint-disable-next-line no-lonely-if
  306. if (cameraUnderground) {
  307. if (!isBlendCommand) {
  308. types[length++] = DerivedCommandType.DEPTH_ONLY_BACK_FACE;
  309. }
  310. types[length++] = DerivedCommandType.OPAQUE_FRONT_FACE;
  311. types[length++] = translucentBackFaceCommandType;
  312. } else {
  313. if (!isBlendCommand) {
  314. types[length++] = DerivedCommandType.DEPTH_ONLY_FRONT_FACE;
  315. }
  316. types[length++] = DerivedCommandType.OPAQUE_BACK_FACE;
  317. types[length++] = translucentFrontFaceCommandType;
  318. }
  319. }
  320. return length;
  321. }
  322. function removeDefine(defines, defineToRemove) {
  323. const index = defines.indexOf(defineToRemove);
  324. if (index > -1) {
  325. defines.splice(index, 1);
  326. }
  327. }
  328. function hasDefine(defines, define) {
  329. return defines.indexOf(define) > -1;
  330. }
  331. function getOpaqueFrontFaceShaderProgram(vs, fs) {
  332. removeDefine(vs.defines, "TRANSLUCENT");
  333. removeDefine(fs.defines, "TRANSLUCENT");
  334. }
  335. function getOpaqueBackFaceShaderProgram(vs, fs) {
  336. removeDefine(vs.defines, "GROUND_ATMOSPHERE");
  337. removeDefine(fs.defines, "GROUND_ATMOSPHERE");
  338. removeDefine(vs.defines, "FOG");
  339. removeDefine(fs.defines, "FOG");
  340. removeDefine(vs.defines, "TRANSLUCENT");
  341. removeDefine(fs.defines, "TRANSLUCENT");
  342. }
  343. function getDepthOnlyShaderProgram(vs, fs) {
  344. if (
  345. hasDefine(fs.defines, "TILE_LIMIT_RECTANGLE") ||
  346. hasDefine(fs.defines, "ENABLE_CLIPPING_PLANES")
  347. ) {
  348. // Need to execute the full shader if discard is called
  349. return;
  350. }
  351. const depthOnlyShader =
  352. "void main() \n" + "{ \n" + " out_FragColor = vec4(1.0); \n" + "} \n";
  353. fs.sources = [depthOnlyShader];
  354. }
  355. function getTranslucentShaderProgram(vs, fs) {
  356. const sources = fs.sources;
  357. const length = sources.length;
  358. for (let i = 0; i < length; ++i) {
  359. sources[i] = ShaderSource.replaceMain(
  360. sources[i],
  361. "czm_globe_translucency_main",
  362. );
  363. }
  364. const globeTranslucencyMain =
  365. "\n\n" +
  366. "uniform sampler2D u_classificationTexture; \n" +
  367. "void main() \n" +
  368. "{ \n" +
  369. " vec2 st = gl_FragCoord.xy / czm_viewport.zw; \n" +
  370. "#ifdef MANUAL_DEPTH_TEST \n" +
  371. " float logDepthOrDepth = czm_unpackDepth(texture(czm_globeDepthTexture, st)); \n" +
  372. " if (logDepthOrDepth != 0.0) \n" +
  373. " { \n" +
  374. " vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, logDepthOrDepth); \n" +
  375. " float depthEC = eyeCoordinate.z / eyeCoordinate.w; \n" +
  376. " if (v_positionEC.z < depthEC) \n" +
  377. " { \n" +
  378. " discard; \n" +
  379. " } \n" +
  380. " } \n" +
  381. "#endif \n" +
  382. " czm_globe_translucency_main(); \n" +
  383. " vec4 classificationColor = texture(u_classificationTexture, st); \n" +
  384. " if (classificationColor.a > 0.0) \n" +
  385. " { \n" +
  386. " // Reverse premultiplication process to get the correct composited result of the classification primitives \n" +
  387. " classificationColor.rgb /= classificationColor.a; \n" +
  388. " } \n" +
  389. " out_FragColor = classificationColor * vec4(classificationColor.aaa, 1.0) + out_FragColor * (1.0 - classificationColor.a); \n" +
  390. "} \n";
  391. sources.push(globeTranslucencyMain);
  392. }
  393. function getTranslucentBackFaceShaderProgram(vs, fs) {
  394. getTranslucentShaderProgram(vs, fs);
  395. removeDefine(vs.defines, "GROUND_ATMOSPHERE");
  396. removeDefine(fs.defines, "GROUND_ATMOSPHERE");
  397. removeDefine(vs.defines, "FOG");
  398. removeDefine(fs.defines, "FOG");
  399. }
  400. function getTranslucentFrontFaceManualDepthTestShaderProgram(vs, fs) {
  401. getTranslucentShaderProgram(vs, fs);
  402. vs.defines.push("GENERATE_POSITION");
  403. fs.defines.push("MANUAL_DEPTH_TEST");
  404. }
  405. function getTranslucentBackFaceManualDepthTestShaderProgram(vs, fs) {
  406. getTranslucentBackFaceShaderProgram(vs, fs);
  407. vs.defines.push("GENERATE_POSITION");
  408. fs.defines.push("MANUAL_DEPTH_TEST");
  409. }
  410. function getPickShaderProgram(vs, fs) {
  411. const pickShader =
  412. "uniform sampler2D u_classificationTexture; \n" +
  413. "void main() \n" +
  414. "{ \n" +
  415. " vec2 st = gl_FragCoord.xy / czm_viewport.zw; \n" +
  416. " vec4 pickColor = texture(u_classificationTexture, st); \n" +
  417. " if (pickColor == vec4(0.0)) \n" +
  418. " { \n" +
  419. " discard; \n" +
  420. " } \n" +
  421. " out_FragColor = pickColor; \n" +
  422. "} \n";
  423. fs.sources = [pickShader];
  424. }
  425. function getDerivedShaderProgram(
  426. context,
  427. shaderProgram,
  428. derivedShaderProgram,
  429. shaderProgramDirty,
  430. getShaderProgramFunction,
  431. cacheName,
  432. ) {
  433. if (!defined(getShaderProgramFunction)) {
  434. return shaderProgram;
  435. }
  436. if (!shaderProgramDirty && defined(derivedShaderProgram)) {
  437. return derivedShaderProgram;
  438. }
  439. let shader = context.shaderCache.getDerivedShaderProgram(
  440. shaderProgram,
  441. cacheName,
  442. );
  443. if (!defined(shader)) {
  444. const attributeLocations = shaderProgram._attributeLocations;
  445. const vs = shaderProgram.vertexShaderSource.clone();
  446. const fs = shaderProgram.fragmentShaderSource.clone();
  447. vs.defines = defined(vs.defines) ? vs.defines.slice(0) : [];
  448. fs.defines = defined(fs.defines) ? fs.defines.slice(0) : [];
  449. getShaderProgramFunction(vs, fs);
  450. shader = context.shaderCache.createDerivedShaderProgram(
  451. shaderProgram,
  452. cacheName,
  453. {
  454. vertexShaderSource: vs,
  455. fragmentShaderSource: fs,
  456. attributeLocations: attributeLocations,
  457. },
  458. );
  459. }
  460. return shader;
  461. }
  462. function getOpaqueFrontFaceRenderState(renderState) {
  463. renderState.cull.face = CullFace.BACK;
  464. renderState.cull.enabled = true;
  465. }
  466. function getOpaqueBackFaceRenderState(renderState) {
  467. renderState.cull.face = CullFace.FRONT;
  468. renderState.cull.enabled = true;
  469. }
  470. function getDepthOnlyFrontFaceRenderState(renderState) {
  471. renderState.cull.face = CullFace.BACK;
  472. renderState.cull.enabled = true;
  473. renderState.colorMask = {
  474. red: false,
  475. green: false,
  476. blue: false,
  477. alpha: false,
  478. };
  479. }
  480. function getDepthOnlyBackFaceRenderState(renderState) {
  481. renderState.cull.face = CullFace.FRONT;
  482. renderState.cull.enabled = true;
  483. renderState.colorMask = {
  484. red: false,
  485. green: false,
  486. blue: false,
  487. alpha: false,
  488. };
  489. }
  490. function getDepthOnlyFrontAndBackFaceRenderState(renderState) {
  491. renderState.cull.enabled = false;
  492. renderState.colorMask = {
  493. red: false,
  494. green: false,
  495. blue: false,
  496. alpha: false,
  497. };
  498. }
  499. function getTranslucentFrontFaceRenderState(renderState) {
  500. renderState.cull.face = CullFace.BACK;
  501. renderState.cull.enabled = true;
  502. renderState.depthMask = false;
  503. renderState.blending = BlendingState.ALPHA_BLEND;
  504. }
  505. function getTranslucentBackFaceRenderState(renderState) {
  506. renderState.cull.face = CullFace.FRONT;
  507. renderState.cull.enabled = true;
  508. renderState.depthMask = false;
  509. renderState.blending = BlendingState.ALPHA_BLEND;
  510. }
  511. function getPickFrontFaceRenderState(renderState) {
  512. renderState.cull.face = CullFace.BACK;
  513. renderState.cull.enabled = true;
  514. renderState.blending.enabled = false;
  515. }
  516. function getPickBackFaceRenderState(renderState) {
  517. renderState.cull.face = CullFace.FRONT;
  518. renderState.cull.enabled = true;
  519. renderState.blending.enabled = false;
  520. }
  521. function getDerivedRenderState(
  522. renderState,
  523. derivedRenderState,
  524. renderStateDirty,
  525. getRenderStateFunction,
  526. cache,
  527. ) {
  528. if (!defined(getRenderStateFunction)) {
  529. return renderState;
  530. }
  531. if (!renderStateDirty && defined(derivedRenderState)) {
  532. return derivedRenderState;
  533. }
  534. let cachedRenderState = cache[renderState.id];
  535. if (!defined(cachedRenderState)) {
  536. const rs = RenderState.getState(renderState);
  537. getRenderStateFunction(rs);
  538. cachedRenderState = RenderState.fromCache(rs);
  539. cache[renderState.id] = cachedRenderState;
  540. }
  541. return cachedRenderState;
  542. }
  543. function getTranslucencyUniformMap(state) {
  544. return {
  545. u_classificationTexture: function () {
  546. return state._globeTranslucencyFramebuffer.classificationTexture;
  547. },
  548. };
  549. }
  550. function getDerivedUniformMap(
  551. state,
  552. uniformMap,
  553. derivedUniformMap,
  554. uniformMapDirty,
  555. getDerivedUniformMapFunction,
  556. ) {
  557. if (!defined(getDerivedUniformMapFunction)) {
  558. return uniformMap;
  559. }
  560. if (!uniformMapDirty && defined(derivedUniformMap)) {
  561. return derivedUniformMap;
  562. }
  563. return combine(uniformMap, getDerivedUniformMapFunction(state), false);
  564. }
  565. function DerivedCommandPack(options) {
  566. this.pass = options.pass;
  567. this.pickOnly = options.pickOnly;
  568. this.getShaderProgramFunction = options.getShaderProgramFunction;
  569. this.getRenderStateFunction = options.getRenderStateFunction;
  570. this.getUniformMapFunction = options.getUniformMapFunction;
  571. this.renderStateCache = {};
  572. }
  573. function createDerivedCommandPacks() {
  574. return [
  575. // opaqueFrontFaceCommand
  576. new DerivedCommandPack({
  577. pass: Pass.GLOBE,
  578. pickOnly: false,
  579. getShaderProgramFunction: getOpaqueFrontFaceShaderProgram,
  580. getRenderStateFunction: getOpaqueFrontFaceRenderState,
  581. getUniformMapFunction: undefined,
  582. }),
  583. // opaqueBackFaceCommand
  584. new DerivedCommandPack({
  585. pass: Pass.GLOBE,
  586. pickOnly: false,
  587. getShaderProgramFunction: getOpaqueBackFaceShaderProgram,
  588. getRenderStateFunction: getOpaqueBackFaceRenderState,
  589. getUniformMapFunction: undefined,
  590. }),
  591. // depthOnlyFrontFaceCommand
  592. new DerivedCommandPack({
  593. pass: Pass.GLOBE,
  594. pickOnly: false,
  595. getShaderProgramFunction: getDepthOnlyShaderProgram,
  596. getRenderStateFunction: getDepthOnlyFrontFaceRenderState,
  597. getUniformMapFunction: undefined,
  598. }),
  599. // depthOnlyBackFaceCommand
  600. new DerivedCommandPack({
  601. pass: Pass.GLOBE,
  602. pickOnly: false,
  603. getShaderProgramFunction: getDepthOnlyShaderProgram,
  604. getRenderStateFunction: getDepthOnlyBackFaceRenderState,
  605. getUniformMapFunction: undefined,
  606. }),
  607. // depthOnlyFrontAndBackFaceCommand
  608. new DerivedCommandPack({
  609. pass: Pass.GLOBE,
  610. pickOnly: false,
  611. getShaderProgramFunction: getDepthOnlyShaderProgram,
  612. getRenderStateFunction: getDepthOnlyFrontAndBackFaceRenderState,
  613. getUniformMapFunction: undefined,
  614. }),
  615. // translucentFrontFaceCommand
  616. new DerivedCommandPack({
  617. pass: Pass.TRANSLUCENT,
  618. pickOnly: false,
  619. getShaderProgramFunction: getTranslucentShaderProgram,
  620. getRenderStateFunction: getTranslucentFrontFaceRenderState,
  621. getUniformMapFunction: getTranslucencyUniformMap,
  622. }),
  623. // translucentBackFaceCommand
  624. new DerivedCommandPack({
  625. pass: Pass.TRANSLUCENT,
  626. pickOnly: false,
  627. getShaderProgramFunction: getTranslucentBackFaceShaderProgram,
  628. getRenderStateFunction: getTranslucentBackFaceRenderState,
  629. getUniformMapFunction: getTranslucencyUniformMap,
  630. }),
  631. // translucentFrontFaceManualDepthTestCommand
  632. new DerivedCommandPack({
  633. pass: Pass.TRANSLUCENT,
  634. pickOnly: false,
  635. getShaderProgramFunction:
  636. getTranslucentFrontFaceManualDepthTestShaderProgram,
  637. getRenderStateFunction: getTranslucentFrontFaceRenderState,
  638. getUniformMapFunction: getTranslucencyUniformMap,
  639. }),
  640. // translucentBackFaceManualDepthTestCommand
  641. new DerivedCommandPack({
  642. pass: Pass.TRANSLUCENT,
  643. pickOnly: false,
  644. getShaderProgramFunction:
  645. getTranslucentBackFaceManualDepthTestShaderProgram,
  646. getRenderStateFunction: getTranslucentBackFaceRenderState,
  647. getUniformMapFunction: getTranslucencyUniformMap,
  648. }),
  649. // pickFrontFaceCommand
  650. new DerivedCommandPack({
  651. pass: Pass.TRANSLUCENT,
  652. pickOnly: true,
  653. getShaderProgramFunction: getPickShaderProgram,
  654. getRenderStateFunction: getPickFrontFaceRenderState,
  655. getUniformMapFunction: getTranslucencyUniformMap,
  656. }),
  657. // pickBackFaceCommand
  658. new DerivedCommandPack({
  659. pass: Pass.TRANSLUCENT,
  660. pickOnly: true,
  661. getShaderProgramFunction: getPickShaderProgram,
  662. getRenderStateFunction: getPickBackFaceRenderState,
  663. getUniformMapFunction: getTranslucencyUniformMap,
  664. }),
  665. ];
  666. }
  667. const derivedCommandNames = new Array(derivedCommandsMaximumLength);
  668. const derivedCommandPacks = new Array(derivedCommandsMaximumLength);
  669. GlobeTranslucencyState.prototype.updateDerivedCommands = function (
  670. command,
  671. frameState,
  672. ) {
  673. const derivedCommandTypes = this._derivedCommandTypesToUpdate;
  674. const derivedCommandsLength = this._derivedCommandsToUpdateLength;
  675. if (derivedCommandsLength === 0) {
  676. return;
  677. }
  678. for (let i = 0; i < derivedCommandsLength; ++i) {
  679. derivedCommandPacks[i] = this._derivedCommandPacks[derivedCommandTypes[i]];
  680. derivedCommandNames[i] = DerivedCommandNames[derivedCommandTypes[i]];
  681. }
  682. updateDerivedCommands(
  683. this,
  684. command,
  685. derivedCommandsLength,
  686. derivedCommandTypes,
  687. derivedCommandNames,
  688. derivedCommandPacks,
  689. frameState,
  690. );
  691. };
  692. function updateDerivedCommands(
  693. state,
  694. command,
  695. derivedCommandsLength,
  696. derivedCommandTypes,
  697. derivedCommandNames,
  698. derivedCommandPacks,
  699. frameState,
  700. ) {
  701. let derivedCommandsObject = command.derivedCommands.globeTranslucency;
  702. const derivedCommandsDirty = state._derivedCommandsDirty;
  703. if (
  704. command.dirty ||
  705. !defined(derivedCommandsObject) ||
  706. derivedCommandsDirty
  707. ) {
  708. command.dirty = false;
  709. if (!defined(derivedCommandsObject)) {
  710. derivedCommandsObject = {};
  711. command.derivedCommands.globeTranslucency = derivedCommandsObject;
  712. }
  713. const frameNumber = frameState.frameNumber;
  714. const uniformMapDirtyFrame =
  715. derivedCommandsObject.uniformMapDirtyFrame ?? 0;
  716. const shaderProgramDirtyFrame =
  717. derivedCommandsObject.shaderProgramDirtyFrame ?? 0;
  718. const renderStateDirtyFrame =
  719. derivedCommandsObject.renderStateDirtyFrame ?? 0;
  720. const uniformMapDirty =
  721. derivedCommandsObject.uniformMap !== command.uniformMap;
  722. const shaderProgramDirty =
  723. derivedCommandsObject.shaderProgramId !== command.shaderProgram.id;
  724. const renderStateDirty =
  725. derivedCommandsObject.renderStateId !== command.renderState.id;
  726. if (uniformMapDirty) {
  727. derivedCommandsObject.uniformMapDirtyFrame = frameNumber;
  728. }
  729. if (shaderProgramDirty) {
  730. derivedCommandsObject.shaderProgramDirtyFrame = frameNumber;
  731. }
  732. if (renderStateDirty) {
  733. derivedCommandsObject.renderStateDirtyFrame = frameNumber;
  734. }
  735. derivedCommandsObject.uniformMap = command.uniformMap;
  736. derivedCommandsObject.shaderProgramId = command.shaderProgram.id;
  737. derivedCommandsObject.renderStateId = command.renderState.id;
  738. for (let i = 0; i < derivedCommandsLength; ++i) {
  739. const derivedCommandPack = derivedCommandPacks[i];
  740. const derivedCommandType = derivedCommandTypes[i];
  741. const derivedCommandName = derivedCommandNames[i];
  742. let derivedCommand = derivedCommandsObject[derivedCommandName];
  743. let derivedUniformMap;
  744. let derivedShaderProgram;
  745. let derivedRenderState;
  746. if (defined(derivedCommand)) {
  747. derivedUniformMap = derivedCommand.uniformMap;
  748. derivedShaderProgram = derivedCommand.shaderProgram;
  749. derivedRenderState = derivedCommand.renderState;
  750. } else {
  751. derivedUniformMap = undefined;
  752. derivedShaderProgram = undefined;
  753. derivedRenderState = undefined;
  754. }
  755. derivedCommand = DrawCommand.shallowClone(command, derivedCommand);
  756. derivedCommandsObject[derivedCommandName] = derivedCommand;
  757. const derivedUniformMapDirtyFrame =
  758. derivedCommand.derivedCommands.uniformMapDirtyFrame ?? 0;
  759. const derivedShaderProgramDirtyFrame =
  760. derivedCommand.derivedCommands.shaderProgramDirtyFrame ?? 0;
  761. const derivedRenderStateDirtyFrame =
  762. derivedCommand.derivedCommands.renderStateDirtyFrame ?? 0;
  763. const derivedUniformMapDirty =
  764. uniformMapDirty || derivedUniformMapDirtyFrame < uniformMapDirtyFrame;
  765. const derivedShaderProgramDirty =
  766. shaderProgramDirty ||
  767. derivedShaderProgramDirtyFrame < shaderProgramDirtyFrame;
  768. const derivedRenderStateDirty =
  769. renderStateDirty ||
  770. derivedRenderStateDirtyFrame < renderStateDirtyFrame;
  771. if (derivedUniformMapDirty) {
  772. derivedCommand.derivedCommands.uniformMapDirtyFrame = frameNumber;
  773. }
  774. if (derivedShaderProgramDirty) {
  775. derivedCommand.derivedCommands.shaderProgramDirtyFrame = frameNumber;
  776. }
  777. if (derivedRenderStateDirty) {
  778. derivedCommand.derivedCommands.renderStateDirtyFrame = frameNumber;
  779. }
  780. derivedCommand.derivedCommands.type = derivedCommandType;
  781. derivedCommand.pass = derivedCommandPack.pass;
  782. derivedCommand.pickOnly = derivedCommandPack.pickOnly;
  783. derivedCommand.uniformMap = getDerivedUniformMap(
  784. state,
  785. command.uniformMap,
  786. derivedUniformMap,
  787. derivedUniformMapDirty,
  788. derivedCommandPack.getUniformMapFunction,
  789. );
  790. derivedCommand.shaderProgram = getDerivedShaderProgram(
  791. frameState.context,
  792. command.shaderProgram,
  793. derivedShaderProgram,
  794. derivedShaderProgramDirty,
  795. derivedCommandPack.getShaderProgramFunction,
  796. derivedCommandName,
  797. );
  798. derivedCommand.renderState = getDerivedRenderState(
  799. command.renderState,
  800. derivedRenderState,
  801. derivedRenderStateDirty,
  802. derivedCommandPack.getRenderStateFunction,
  803. derivedCommandPack.renderStateCache,
  804. );
  805. }
  806. }
  807. }
  808. GlobeTranslucencyState.prototype.pushDerivedCommands = function (
  809. command,
  810. isBlendCommand,
  811. frameState,
  812. ) {
  813. const picking = frameState.passes.pick || frameState.passes.pickVoxel;
  814. if (picking && isBlendCommand) {
  815. // No need to push blend commands in the pick pass
  816. return;
  817. }
  818. let derivedCommandTypes = this._derivedCommandTypes;
  819. let derivedCommandsLength = this._derivedCommandsLength;
  820. if (picking) {
  821. derivedCommandTypes = this._derivedPickCommandTypes;
  822. derivedCommandsLength = this._derivedPickCommandsLength;
  823. } else if (isBlendCommand) {
  824. derivedCommandTypes = this._derivedBlendCommandTypes;
  825. derivedCommandsLength = this._derivedBlendCommandsLength;
  826. }
  827. if (derivedCommandsLength === 0) {
  828. // No derived commands to push so just push the globe command
  829. frameState.commandList.push(command);
  830. return;
  831. }
  832. // Push derived commands
  833. const derivedCommands = command.derivedCommands.globeTranslucency;
  834. for (let i = 0; i < derivedCommandsLength; ++i) {
  835. const derivedCommandName = DerivedCommandNames[derivedCommandTypes[i]];
  836. frameState.commandList.push(derivedCommands[derivedCommandName]);
  837. }
  838. };
  839. function executeCommandsMatchingType(
  840. commands,
  841. commandsLength,
  842. executeCommandFunction,
  843. scene,
  844. passState,
  845. types,
  846. ) {
  847. for (let i = 0; i < commandsLength; ++i) {
  848. const command = commands[i];
  849. const type = command.derivedCommands.type;
  850. if (!defined(types) || types.indexOf(type) > -1) {
  851. executeCommandFunction(command, scene, passState);
  852. }
  853. }
  854. }
  855. function executeCommands(
  856. commands,
  857. commandsLength,
  858. executeCommandFunction,
  859. scene,
  860. passState,
  861. ) {
  862. for (let i = 0; i < commandsLength; ++i) {
  863. executeCommandFunction(commands[i], scene, passState);
  864. }
  865. }
  866. const opaqueTypes = [
  867. DerivedCommandType.OPAQUE_FRONT_FACE,
  868. DerivedCommandType.OPAQUE_BACK_FACE,
  869. ];
  870. const depthOnlyTypes = [
  871. DerivedCommandType.DEPTH_ONLY_FRONT_FACE,
  872. DerivedCommandType.DEPTH_ONLY_BACK_FACE,
  873. DerivedCommandType.DEPTH_ONLY_FRONT_AND_BACK_FACE,
  874. ];
  875. GlobeTranslucencyState.prototype.executeGlobeCommands = function (
  876. frustumCommands,
  877. executeCommandFunction,
  878. globeTranslucencyFramebuffer,
  879. scene,
  880. passState,
  881. ) {
  882. const context = scene.context;
  883. const globeCommands = frustumCommands.commands[Pass.GLOBE];
  884. const globeCommandsLength = frustumCommands.indices[Pass.GLOBE];
  885. if (globeCommandsLength === 0) {
  886. return;
  887. }
  888. this._globeTranslucencyFramebuffer = globeTranslucencyFramebuffer;
  889. globeTranslucencyFramebuffer.clearClassification(context, passState);
  890. // Render opaque commands like normal
  891. executeCommandsMatchingType(
  892. globeCommands,
  893. globeCommandsLength,
  894. executeCommandFunction,
  895. scene,
  896. passState,
  897. opaqueTypes,
  898. );
  899. };
  900. GlobeTranslucencyState.prototype.executeGlobeClassificationCommands = function (
  901. frustumCommands,
  902. executeCommandFunction,
  903. globeTranslucencyFramebuffer,
  904. scene,
  905. passState,
  906. ) {
  907. const { context } = scene;
  908. const { uniformState } = context;
  909. const globeCommands = frustumCommands.commands[Pass.GLOBE];
  910. const globeCommandsLength = frustumCommands.indices[Pass.GLOBE];
  911. const classificationCommands =
  912. frustumCommands.commands[Pass.TERRAIN_CLASSIFICATION];
  913. const classificationCommandsLength =
  914. frustumCommands.indices[Pass.TERRAIN_CLASSIFICATION];
  915. if (globeCommandsLength === 0 || classificationCommandsLength === 0) {
  916. return;
  917. }
  918. const frontTranslucent = this._frontFaceTranslucent;
  919. const backTranslucent = this._backFaceTranslucent;
  920. if (!frontTranslucent || !backTranslucent) {
  921. // Render classification on opaque faces like normal
  922. executeCommands(
  923. classificationCommands,
  924. classificationCommandsLength,
  925. executeCommandFunction,
  926. scene,
  927. passState,
  928. );
  929. }
  930. if (!frontTranslucent && !backTranslucent) {
  931. // No translucent commands to render. Skip translucent classification.
  932. return;
  933. }
  934. this._globeTranslucencyFramebuffer = globeTranslucencyFramebuffer;
  935. const originalGlobeDepthTexture = uniformState.globeDepthTexture;
  936. const originalFramebuffer = passState.framebuffer;
  937. // Render to internal framebuffer and get the first depth peel
  938. passState.framebuffer =
  939. globeTranslucencyFramebuffer.classificationFramebuffer;
  940. executeCommandsMatchingType(
  941. globeCommands,
  942. globeCommandsLength,
  943. executeCommandFunction,
  944. scene,
  945. passState,
  946. depthOnlyTypes,
  947. );
  948. if (context.depthTexture) {
  949. // Pack depth into separate texture for ground polylines and textured ground primitives
  950. const packedDepthTexture = globeTranslucencyFramebuffer.packDepth(
  951. context,
  952. passState,
  953. );
  954. uniformState.globeDepthTexture = packedDepthTexture;
  955. }
  956. // Render classification on translucent faces
  957. executeCommands(
  958. classificationCommands,
  959. classificationCommandsLength,
  960. executeCommandFunction,
  961. scene,
  962. passState,
  963. );
  964. // Unset temporary state
  965. uniformState.globeDepthTexture = originalGlobeDepthTexture;
  966. passState.framebuffer = originalFramebuffer;
  967. };
  968. export default GlobeTranslucencyState;