debugging-glsl.html 6.8 KB

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