DebugAppearance.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import Appearance from "./Appearance.js";
  5. /**
  6. * Visualizes a vertex attribute by displaying it as a color for debugging.
  7. * <p>
  8. * Components for well-known unit-length vectors, i.e., <code>normal</code>,
  9. * <code>tangent</code>, and <code>bitangent</code>, are scaled and biased
  10. * from [-1.0, 1.0] to (-1.0, 1.0).
  11. * </p>
  12. *
  13. * @alias DebugAppearance
  14. * @constructor
  15. *
  16. * @param {object} options Object with the following properties:
  17. * @param {string} options.attributeName The name of the attribute to visualize.
  18. * @param {boolean} [options.perInstanceAttribute=false] Boolean that determines whether this attribute is a per-instance geometry attribute.
  19. * @param {string} [options.glslDatatype='vec3'] The GLSL datatype of the attribute. Supported datatypes are <code>float</code>, <code>vec2</code>, <code>vec3</code>, and <code>vec4</code>.
  20. * @param {string} [options.vertexShaderSource] Optional GLSL vertex shader source to override the default vertex shader.
  21. * @param {string} [options.fragmentShaderSource] Optional GLSL fragment shader source to override the default fragment shader.
  22. * @param {object} [options.renderState] Optional render state to override the default render state.
  23. *
  24. * @exception {DeveloperError} options.glslDatatype must be float, vec2, vec3, or vec4.
  25. *
  26. * @example
  27. * const primitive = new Cesium.Primitive({
  28. * geometryInstances : // ...
  29. * appearance : new Cesium.DebugAppearance({
  30. * attributeName : 'normal'
  31. * })
  32. * });
  33. */
  34. function DebugAppearance(options) {
  35. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  36. const attributeName = options.attributeName;
  37. let perInstanceAttribute = options.perInstanceAttribute;
  38. //>>includeStart('debug', pragmas.debug);
  39. if (!defined(attributeName)) {
  40. throw new DeveloperError("options.attributeName is required.");
  41. }
  42. //>>includeEnd('debug');
  43. if (!defined(perInstanceAttribute)) {
  44. perInstanceAttribute = false;
  45. }
  46. let glslDatatype = defaultValue(options.glslDatatype, "vec3");
  47. const varyingName = `v_${attributeName}`;
  48. let getColor;
  49. // Well-known normalized vector attributes in VertexFormat
  50. if (
  51. attributeName === "normal" ||
  52. attributeName === "tangent" ||
  53. attributeName === "bitangent"
  54. ) {
  55. getColor = `vec4 getColor() { return vec4((${varyingName} + vec3(1.0)) * 0.5, 1.0); }\n`;
  56. } else {
  57. // All other attributes, both well-known and custom
  58. if (attributeName === "st") {
  59. glslDatatype = "vec2";
  60. }
  61. switch (glslDatatype) {
  62. case "float":
  63. getColor = `vec4 getColor() { return vec4(vec3(${varyingName}), 1.0); }\n`;
  64. break;
  65. case "vec2":
  66. getColor = `vec4 getColor() { return vec4(${varyingName}, 0.0, 1.0); }\n`;
  67. break;
  68. case "vec3":
  69. getColor = `vec4 getColor() { return vec4(${varyingName}, 1.0); }\n`;
  70. break;
  71. case "vec4":
  72. getColor = `vec4 getColor() { return ${varyingName}; }\n`;
  73. break;
  74. //>>includeStart('debug', pragmas.debug);
  75. default:
  76. throw new DeveloperError(
  77. "options.glslDatatype must be float, vec2, vec3, or vec4."
  78. );
  79. //>>includeEnd('debug');
  80. }
  81. }
  82. const vs =
  83. `${
  84. "in vec3 position3DHigh;\n" +
  85. "in vec3 position3DLow;\n" +
  86. "in float batchId;\n"
  87. }${
  88. perInstanceAttribute ? "" : `in ${glslDatatype} ${attributeName};\n`
  89. }out ${glslDatatype} ${varyingName};\n` +
  90. `void main()\n` +
  91. `{\n` +
  92. `vec4 p = czm_translateRelativeToEye(position3DHigh, position3DLow);\n${
  93. perInstanceAttribute
  94. ? `${varyingName} = czm_batchTable_${attributeName}(batchId);\n`
  95. : `${varyingName} = ${attributeName};\n`
  96. }gl_Position = czm_modelViewProjectionRelativeToEye * p;\n` +
  97. `}`;
  98. const fs =
  99. `in ${glslDatatype} ${varyingName};\n${getColor}\n` +
  100. `void main()\n` +
  101. `{\n` +
  102. `out_FragColor = getColor();\n` +
  103. `}`;
  104. /**
  105. * This property is part of the {@link Appearance} interface, but is not
  106. * used by {@link DebugAppearance} since a fully custom fragment shader is used.
  107. *
  108. * @type Material
  109. *
  110. * @default undefined
  111. */
  112. this.material = undefined;
  113. /**
  114. * When <code>true</code>, the geometry is expected to appear translucent.
  115. *
  116. * @type {boolean}
  117. *
  118. * @default false
  119. */
  120. this.translucent = defaultValue(options.translucent, false);
  121. this._vertexShaderSource = defaultValue(options.vertexShaderSource, vs);
  122. this._fragmentShaderSource = defaultValue(options.fragmentShaderSource, fs);
  123. this._renderState = Appearance.getDefaultRenderState(
  124. false,
  125. false,
  126. options.renderState
  127. );
  128. this._closed = defaultValue(options.closed, false);
  129. // Non-derived members
  130. this._attributeName = attributeName;
  131. this._glslDatatype = glslDatatype;
  132. }
  133. Object.defineProperties(DebugAppearance.prototype, {
  134. /**
  135. * The GLSL source code for the vertex shader.
  136. *
  137. * @memberof DebugAppearance.prototype
  138. *
  139. * @type {string}
  140. * @readonly
  141. */
  142. vertexShaderSource: {
  143. get: function () {
  144. return this._vertexShaderSource;
  145. },
  146. },
  147. /**
  148. * The GLSL source code for the fragment shader. The full fragment shader
  149. * source is built procedurally taking into account the {@link DebugAppearance#material}.
  150. * Use {@link DebugAppearance#getFragmentShaderSource} to get the full source.
  151. *
  152. * @memberof DebugAppearance.prototype
  153. *
  154. * @type {string}
  155. * @readonly
  156. */
  157. fragmentShaderSource: {
  158. get: function () {
  159. return this._fragmentShaderSource;
  160. },
  161. },
  162. /**
  163. * The WebGL fixed-function state to use when rendering the geometry.
  164. *
  165. * @memberof DebugAppearance.prototype
  166. *
  167. * @type {object}
  168. * @readonly
  169. */
  170. renderState: {
  171. get: function () {
  172. return this._renderState;
  173. },
  174. },
  175. /**
  176. * When <code>true</code>, the geometry is expected to be closed.
  177. *
  178. * @memberof DebugAppearance.prototype
  179. *
  180. * @type {boolean}
  181. * @readonly
  182. *
  183. * @default false
  184. */
  185. closed: {
  186. get: function () {
  187. return this._closed;
  188. },
  189. },
  190. /**
  191. * The name of the attribute being visualized.
  192. *
  193. * @memberof DebugAppearance.prototype
  194. *
  195. * @type {string}
  196. * @readonly
  197. */
  198. attributeName: {
  199. get: function () {
  200. return this._attributeName;
  201. },
  202. },
  203. /**
  204. * The GLSL datatype of the attribute being visualized.
  205. *
  206. * @memberof DebugAppearance.prototype
  207. *
  208. * @type {string}
  209. * @readonly
  210. */
  211. glslDatatype: {
  212. get: function () {
  213. return this._glslDatatype;
  214. },
  215. },
  216. });
  217. /**
  218. * Returns the full GLSL fragment shader source, which for {@link DebugAppearance} is just
  219. * {@link DebugAppearance#fragmentShaderSource}.
  220. *
  221. * @function
  222. *
  223. * @returns {string} The full GLSL fragment shader source.
  224. */
  225. DebugAppearance.prototype.getFragmentShaderSource =
  226. Appearance.prototype.getFragmentShaderSource;
  227. /**
  228. * Determines if the geometry is translucent based on {@link DebugAppearance#translucent}.
  229. *
  230. * @function
  231. *
  232. * @returns {boolean} <code>true</code> if the appearance is translucent.
  233. */
  234. DebugAppearance.prototype.isTranslucent = Appearance.prototype.isTranslucent;
  235. /**
  236. * Creates a render state. This is not the final render state instance; instead,
  237. * it can contain a subset of render state properties identical to the render state
  238. * created in the context.
  239. *
  240. * @function
  241. *
  242. * @returns {object} The render state.
  243. */
  244. DebugAppearance.prototype.getRenderState = Appearance.prototype.getRenderState;
  245. export default DebugAppearance;