DebugAppearance.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. "attribute vec3 position3DHigh;\n" +
  85. "attribute vec3 position3DLow;\n" +
  86. "attribute float batchId;\n"
  87. }${
  88. perInstanceAttribute
  89. ? ""
  90. : `attribute ${glslDatatype} ${attributeName};\n`
  91. }varying ${glslDatatype} ${varyingName};\n` +
  92. `void main()\n` +
  93. `{\n` +
  94. `vec4 p = czm_translateRelativeToEye(position3DHigh, position3DLow);\n${
  95. perInstanceAttribute
  96. ? `${varyingName} = czm_batchTable_${attributeName}(batchId);\n`
  97. : `${varyingName} = ${attributeName};\n`
  98. }gl_Position = czm_modelViewProjectionRelativeToEye * p;\n` +
  99. `}`;
  100. const fs =
  101. `varying ${glslDatatype} ${varyingName};\n${getColor}\n` +
  102. `void main()\n` +
  103. `{\n` +
  104. `gl_FragColor = getColor();\n` +
  105. `}`;
  106. /**
  107. * This property is part of the {@link Appearance} interface, but is not
  108. * used by {@link DebugAppearance} since a fully custom fragment shader is used.
  109. *
  110. * @type Material
  111. *
  112. * @default undefined
  113. */
  114. this.material = undefined;
  115. /**
  116. * When <code>true</code>, the geometry is expected to appear translucent.
  117. *
  118. * @type {Boolean}
  119. *
  120. * @default false
  121. */
  122. this.translucent = defaultValue(options.translucent, false);
  123. this._vertexShaderSource = defaultValue(options.vertexShaderSource, vs);
  124. this._fragmentShaderSource = defaultValue(options.fragmentShaderSource, fs);
  125. this._renderState = Appearance.getDefaultRenderState(
  126. false,
  127. false,
  128. options.renderState
  129. );
  130. this._closed = defaultValue(options.closed, false);
  131. // Non-derived members
  132. this._attributeName = attributeName;
  133. this._glslDatatype = glslDatatype;
  134. }
  135. Object.defineProperties(DebugAppearance.prototype, {
  136. /**
  137. * The GLSL source code for the vertex shader.
  138. *
  139. * @memberof DebugAppearance.prototype
  140. *
  141. * @type {String}
  142. * @readonly
  143. */
  144. vertexShaderSource: {
  145. get: function () {
  146. return this._vertexShaderSource;
  147. },
  148. },
  149. /**
  150. * The GLSL source code for the fragment shader. The full fragment shader
  151. * source is built procedurally taking into account the {@link DebugAppearance#material}.
  152. * Use {@link DebugAppearance#getFragmentShaderSource} to get the full source.
  153. *
  154. * @memberof DebugAppearance.prototype
  155. *
  156. * @type {String}
  157. * @readonly
  158. */
  159. fragmentShaderSource: {
  160. get: function () {
  161. return this._fragmentShaderSource;
  162. },
  163. },
  164. /**
  165. * The WebGL fixed-function state to use when rendering the geometry.
  166. *
  167. * @memberof DebugAppearance.prototype
  168. *
  169. * @type {Object}
  170. * @readonly
  171. */
  172. renderState: {
  173. get: function () {
  174. return this._renderState;
  175. },
  176. },
  177. /**
  178. * When <code>true</code>, the geometry is expected to be closed.
  179. *
  180. * @memberof DebugAppearance.prototype
  181. *
  182. * @type {Boolean}
  183. * @readonly
  184. *
  185. * @default false
  186. */
  187. closed: {
  188. get: function () {
  189. return this._closed;
  190. },
  191. },
  192. /**
  193. * The name of the attribute being visualized.
  194. *
  195. * @memberof DebugAppearance.prototype
  196. *
  197. * @type {String}
  198. * @readonly
  199. */
  200. attributeName: {
  201. get: function () {
  202. return this._attributeName;
  203. },
  204. },
  205. /**
  206. * The GLSL datatype of the attribute being visualized.
  207. *
  208. * @memberof DebugAppearance.prototype
  209. *
  210. * @type {String}
  211. * @readonly
  212. */
  213. glslDatatype: {
  214. get: function () {
  215. return this._glslDatatype;
  216. },
  217. },
  218. });
  219. /**
  220. * Returns the full GLSL fragment shader source, which for {@link DebugAppearance} is just
  221. * {@link DebugAppearance#fragmentShaderSource}.
  222. *
  223. * @function
  224. *
  225. * @returns {String} The full GLSL fragment shader source.
  226. */
  227. DebugAppearance.prototype.getFragmentShaderSource =
  228. Appearance.prototype.getFragmentShaderSource;
  229. /**
  230. * Determines if the geometry is translucent based on {@link DebugAppearance#translucent}.
  231. *
  232. * @function
  233. *
  234. * @returns {Boolean} <code>true</code> if the appearance is translucent.
  235. */
  236. DebugAppearance.prototype.isTranslucent = Appearance.prototype.isTranslucent;
  237. /**
  238. * Creates a render state. This is not the final render state instance; instead,
  239. * it can contain a subset of render state properties identical to the render state
  240. * created in the context.
  241. *
  242. * @function
  243. *
  244. * @returns {Object} The render state.
  245. */
  246. DebugAppearance.prototype.getRenderState = Appearance.prototype.getRenderState;
  247. export default DebugAppearance;