DebugAppearance.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * var 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. var attributeName = options.attributeName;
  37. var 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. var glslDatatype = defaultValue(options.glslDatatype, "vec3");
  47. var varyingName = "v_" + attributeName;
  48. var getColor;
  49. // Well-known normalized vector attributes in VertexFormat
  50. if (
  51. attributeName === "normal" ||
  52. attributeName === "tangent" ||
  53. attributeName === "bitangent"
  54. ) {
  55. getColor =
  56. "vec4 getColor() { return vec4((" +
  57. varyingName +
  58. " + vec3(1.0)) * 0.5, 1.0); }\n";
  59. } else {
  60. // All other attributes, both well-known and custom
  61. if (attributeName === "st") {
  62. glslDatatype = "vec2";
  63. }
  64. switch (glslDatatype) {
  65. case "float":
  66. getColor =
  67. "vec4 getColor() { return vec4(vec3(" + varyingName + "), 1.0); }\n";
  68. break;
  69. case "vec2":
  70. getColor =
  71. "vec4 getColor() { return vec4(" + varyingName + ", 0.0, 1.0); }\n";
  72. break;
  73. case "vec3":
  74. getColor =
  75. "vec4 getColor() { return vec4(" + varyingName + ", 1.0); }\n";
  76. break;
  77. case "vec4":
  78. getColor = "vec4 getColor() { return " + varyingName + "; }\n";
  79. break;
  80. //>>includeStart('debug', pragmas.debug);
  81. default:
  82. throw new DeveloperError(
  83. "options.glslDatatype must be float, vec2, vec3, or vec4."
  84. );
  85. //>>includeEnd('debug');
  86. }
  87. }
  88. var vs =
  89. "attribute vec3 position3DHigh;\n" +
  90. "attribute vec3 position3DLow;\n" +
  91. "attribute float batchId;\n" +
  92. (perInstanceAttribute
  93. ? ""
  94. : "attribute " + glslDatatype + " " + attributeName + ";\n") +
  95. "varying " +
  96. glslDatatype +
  97. " " +
  98. varyingName +
  99. ";\n" +
  100. "void main()\n" +
  101. "{\n" +
  102. "vec4 p = czm_translateRelativeToEye(position3DHigh, position3DLow);\n" +
  103. (perInstanceAttribute
  104. ? varyingName + " = czm_batchTable_" + attributeName + "(batchId);\n"
  105. : varyingName + " = " + attributeName + ";\n") +
  106. "gl_Position = czm_modelViewProjectionRelativeToEye * p;\n" +
  107. "}";
  108. var fs =
  109. "varying " +
  110. glslDatatype +
  111. " " +
  112. varyingName +
  113. ";\n" +
  114. getColor +
  115. "\n" +
  116. "void main()\n" +
  117. "{\n" +
  118. "gl_FragColor = getColor();\n" +
  119. "}";
  120. /**
  121. * This property is part of the {@link Appearance} interface, but is not
  122. * used by {@link DebugAppearance} since a fully custom fragment shader is used.
  123. *
  124. * @type Material
  125. *
  126. * @default undefined
  127. */
  128. this.material = undefined;
  129. /**
  130. * When <code>true</code>, the geometry is expected to appear translucent.
  131. *
  132. * @type {Boolean}
  133. *
  134. * @default false
  135. */
  136. this.translucent = defaultValue(options.translucent, false);
  137. this._vertexShaderSource = defaultValue(options.vertexShaderSource, vs);
  138. this._fragmentShaderSource = defaultValue(options.fragmentShaderSource, fs);
  139. this._renderState = Appearance.getDefaultRenderState(
  140. false,
  141. false,
  142. options.renderState
  143. );
  144. this._closed = defaultValue(options.closed, false);
  145. // Non-derived members
  146. this._attributeName = attributeName;
  147. this._glslDatatype = glslDatatype;
  148. }
  149. Object.defineProperties(DebugAppearance.prototype, {
  150. /**
  151. * The GLSL source code for the vertex shader.
  152. *
  153. * @memberof DebugAppearance.prototype
  154. *
  155. * @type {String}
  156. * @readonly
  157. */
  158. vertexShaderSource: {
  159. get: function () {
  160. return this._vertexShaderSource;
  161. },
  162. },
  163. /**
  164. * The GLSL source code for the fragment shader. The full fragment shader
  165. * source is built procedurally taking into account the {@link DebugAppearance#material}.
  166. * Use {@link DebugAppearance#getFragmentShaderSource} to get the full source.
  167. *
  168. * @memberof DebugAppearance.prototype
  169. *
  170. * @type {String}
  171. * @readonly
  172. */
  173. fragmentShaderSource: {
  174. get: function () {
  175. return this._fragmentShaderSource;
  176. },
  177. },
  178. /**
  179. * The WebGL fixed-function state to use when rendering the geometry.
  180. *
  181. * @memberof DebugAppearance.prototype
  182. *
  183. * @type {Object}
  184. * @readonly
  185. */
  186. renderState: {
  187. get: function () {
  188. return this._renderState;
  189. },
  190. },
  191. /**
  192. * When <code>true</code>, the geometry is expected to be closed.
  193. *
  194. * @memberof DebugAppearance.prototype
  195. *
  196. * @type {Boolean}
  197. * @readonly
  198. *
  199. * @default false
  200. */
  201. closed: {
  202. get: function () {
  203. return this._closed;
  204. },
  205. },
  206. /**
  207. * The name of the attribute being visualized.
  208. *
  209. * @memberof DebugAppearance.prototype
  210. *
  211. * @type {String}
  212. * @readonly
  213. */
  214. attributeName: {
  215. get: function () {
  216. return this._attributeName;
  217. },
  218. },
  219. /**
  220. * The GLSL datatype of the attribute being visualized.
  221. *
  222. * @memberof DebugAppearance.prototype
  223. *
  224. * @type {String}
  225. * @readonly
  226. */
  227. glslDatatype: {
  228. get: function () {
  229. return this._glslDatatype;
  230. },
  231. },
  232. });
  233. /**
  234. * Returns the full GLSL fragment shader source, which for {@link DebugAppearance} is just
  235. * {@link DebugAppearance#fragmentShaderSource}.
  236. *
  237. * @function
  238. *
  239. * @returns {String} The full GLSL fragment shader source.
  240. */
  241. DebugAppearance.prototype.getFragmentShaderSource =
  242. Appearance.prototype.getFragmentShaderSource;
  243. /**
  244. * Determines if the geometry is translucent based on {@link DebugAppearance#translucent}.
  245. *
  246. * @function
  247. *
  248. * @returns {Boolean} <code>true</code> if the appearance is translucent.
  249. */
  250. DebugAppearance.prototype.isTranslucent = Appearance.prototype.isTranslucent;
  251. /**
  252. * Creates a render state. This is not the final render state instance; instead,
  253. * it can contain a subset of render state properties identical to the render state
  254. * created in the context.
  255. *
  256. * @function
  257. *
  258. * @returns {Object} The render state.
  259. */
  260. DebugAppearance.prototype.getRenderState = Appearance.prototype.getRenderState;
  261. export default DebugAppearance;