debugging-glsl.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Debugging - GLSL</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – Debugging - GLSL">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>Debugging - GLSL</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>This site so far does not teach GLSL just like it does not teach JavaScript.
  29. Those are really large topics. If you want to learn GLSL consider checking out
  30. <a href="https://webglfundamentals.org">these articles</a> as a starting place.</p>
  31. <p>If you already know GLSL then here are a few tips for debugging.</p>
  32. <p>When I'm making a new GLSL shader and nothing appears generally
  33. the first thing I do is change the fragment shader to return a solid
  34. color. For example at the very bottom of the shader I might put</p>
  35. <pre class="prettyprint showlinemods notranslate lang-glsl" translate="no">void main() {
  36. ...
  37. gl_FragColor = vec4(1, 0, 0, 1); // red
  38. }
  39. </pre>
  40. <p>If I see the object I was trying to draw then I know the issue is
  41. related to my fragment shader. It could be anything like bad textures,
  42. uninitialized uniforms, uniforms with the wrong values but at least
  43. I have a direction to look.</p>
  44. <p>To test some of those I might start trying to draw some of the inputs.
  45. For example if I'm using normals in the fragment shader then I might
  46. add</p>
  47. <pre class="prettyprint showlinemods notranslate lang-glsl" translate="no">gl_FragColor = vec4(vNormal * 0.5 + 0.5, 1);
  48. </pre>
  49. <p>Normals go from -1 to +1 so by multiplying by 0.5 and adding 0.5 we get
  50. values that go from 0.0 to 1.0 which makes them useful for colors.</p>
  51. <p>Try this with some things you know work and you'll start getting an idea
  52. of what normals <em>normally</em> look like. If your normals don't look normal
  53. then you have some clue where to look. If you're manipulating normals
  54. in the fragments shader you can use the same technique to draw the
  55. result of that manipulation.</p>
  56. <div class="threejs_center"><img src="../resources/images/standard-primitive-normals.jpg" style="width: 650px;"></div>
  57. <p>Similarly if we're using textures there will be texture coordinates and we
  58. can draw them with something like</p>
  59. <pre class="prettyprint showlinemods notranslate lang-glsl" translate="no">gl_FragColor = vec4(fract(vUv), 0, 1);
  60. </pre>
  61. <p>The <code class="notranslate" translate="no">fract</code> is there in case we're using texture coordinates that go outside
  62. the 0 to 1 range. This is common if <code class="notranslate" translate="no">texture.repeat</code> is set to something greater
  63. than 1.</p>
  64. <div class="threejs_center"><img src="../resources/images/standard-primitive-uvs.jpg" style="width: 650px;"></div>
  65. <p>You can do similar things for all values in your fragment shader. Figure out
  66. what their range is likely to be, add some code to set <code class="notranslate" translate="no">gl_FragColor</code> with
  67. that range scaled to 0.0 to 1.0</p>
  68. <p>To check textures try a <a href="/docs/#api/en/textures/CanvasTexture"><code class="notranslate" translate="no">CanvasTexture</code></a> or a <a href="/docs/#api/en/textures/DataTexture"><code class="notranslate" translate="no">DataTexture</code></a> that you
  69. know works.</p>
  70. <p>Conversely, if after setting <code class="notranslate" translate="no">gl_FragColor</code> to red I still see nothing
  71. then I have a hint my issue might be in the direction of the things
  72. related to the vertex shader. Some matrices might be wrong or my
  73. attributes might have bad data or be setup incorrectly.</p>
  74. <p>I'd first look at the matrices. I might put a breakpoint right after
  75. my call to <code class="notranslate" translate="no">renderer.render(scene, camera)</code> and then start expanding
  76. things in the inspector. Is the camera's world matrix and projection
  77. matrix at least not full of <code class="notranslate" translate="no">NaN</code>s? Expanding the scene and looking
  78. at its <code class="notranslate" translate="no">children</code> I'd check that the world matrices look reasonable (no <code class="notranslate" translate="no">NaN</code>s)
  79. and last 4 values of each matrix look reasonable for my scene. If I
  80. expect my scene to be 50x50x50 units and some matrix shows 552352623.123
  81. clearly something is wrong there.</p>
  82. <div class="threejs_center"><img src="../resources/images/inspect-matrices.gif"></div>
  83. <p>Just like we did for the fragment shader we can also draw values from the
  84. vertex shader by passing them to the fragment shader. Declare a varying
  85. in both and pass the value you're not sure is correct. In fact if my
  86. shader use using normals I'll change the fragment shader to display them
  87. like is mentioned above and then just set <code class="notranslate" translate="no">vNormal</code> to the value I want
  88. to display but scaled so the values go from 0.0 to 1.0. I then look at the
  89. results and see if they fit my expectations.</p>
  90. <p>Another good thing to do is use a simpler shader. Can you draw your data
  91. with <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a>? If you can then try it and make sure it shows
  92. up as expected.</p>
  93. <p>If not what's the simplest vertex shader that will let you visualize your
  94. geometry? Usually it's as simple as</p>
  95. <pre class="prettyprint showlinemods notranslate lang-glsl" translate="no">gl_Position = projection * modelView * vec4(position.xyz, 1);
  96. </pre>
  97. <p>If that works start adding in your changes a little at a time.</p>
  98. <p>Yet another thing you can do is use the
  99. <a href="https://chrome.google.com/webstore/detail/shader-editor/ggeaidddejpbakgafapihjbgdlbbbpob?hl=en">Shader Editor extension for Chrome</a>
  100. or similar for other browsers. It's a great way to look at how other shaders
  101. are working. It's also good as you can make some of the changes suggested above
  102. live while the code is running.</p>
  103. </div>
  104. </div>
  105. </div>
  106. <script src="../resources/prettify.js"></script>
  107. <script src="../resources/lesson.js"></script>
  108. </body></html>