How-to-use-WebGL2.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <base href="../../../" />
  6. <script src="list.js"></script>
  7. <script src="page.js"></script>
  8. <link type="text/css" rel="stylesheet" href="page.css" />
  9. </head>
  10. <body>
  11. <h1>[name]</h1>
  12. <p>
  13. Starting with three.js R95, the engine supports rendering with a WebGL 2 context. By default three.js always uses a
  14. WebGL 1 context when creating an instance of *WebGLRenderer*. If you want use a WebGL 2 context, please have a look
  15. at the following workflow.
  16. </p>
  17. <h2>Workflow</h2>
  18. <p>
  19. Since WebGL 2 is not supported by all devices that support WebGL 1, it's important to check the respective availability.
  20. To do so, please include [link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/WebGL.js WebGL.js] into your project.
  21. </p>
  22. <code>
  23. import { WEBGL } from 'three/examples/jsm/WebGL.js';
  24. </code>
  25. <p>
  26. Next, use a code similar to the following in order to perform the availability check.
  27. </p>
  28. <code>
  29. if ( WEBGL.isWebGL2Available() === false ) {
  30. document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
  31. }
  32. </code>
  33. <p>
  34. Now it's time to create the renderer by applying the HTML5 canvas element and the respective WebGL 2 context
  35. to the constructor of *WebGLRenderer*. As a result, three.js will internally use the given context for rendering and
  36. automatically convert the built-in material's shader code to GLSL ES 3.00.
  37. </p>
  38. <code>
  39. var canvas = document.createElement( 'canvas' );
  40. var context = canvas.getContext( 'webgl2' );
  41. var renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  42. </code>
  43. <p>
  44. Sometimes it is necessary to write custom shader code. Use the following code template as a basis for your own
  45. implementation. First, the GLSL ES 3.00 code.
  46. </p>
  47. <code>
  48. &lt;script id="vs" type="x-shader/x-vertex"&gt;
  49. #version 300 es
  50. void main() {
  51. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  52. }
  53. &lt;/script&gt;
  54. &lt;script id="fs" type="x-shader/x-fragment"&gt;
  55. #version 300 es
  56. precision highp float;
  57. precision highp int;
  58. out vec4 out_FragColor;
  59. void main() {
  60. out_FragColor = vec4( 1.0 );
  61. }
  62. &lt;/script&gt;
  63. </code>
  64. <p>
  65. Second, the corresponding material creation in JavaScript.
  66. </p>
  67. <code>
  68. var material = new THREE.ShaderMaterial( {
  69. vertexShader: document.getElementById( 'vs' ).textContent.trim(),
  70. fragmentShader: document.getElementById( 'fs' ).textContent.trim()
  71. } );
  72. </code>
  73. <h2>Next Steps</h2>
  74. <p>
  75. Have a look at one of the official examples in order to see WebGL 2 features in action.<br /><br />
  76. [example:webgl2_materials_texture3d WebGL2 / materials / texture3d]<br />
  77. [example:webgl2_materials_texture2darray WebGL2 / materials / texture2darray]<br />
  78. [example:webgl2_multisampled_renderbuffers WebGL2 / multisampled renderbuffers]
  79. </p>
  80. <h2>Supported features</h2>
  81. <p>
  82. Right now, the engine does only support a subset of all existing WebGL 2 features. The following list provides an
  83. overview about what's already available in the latest version of three.js.
  84. <ul>
  85. <li>3D Textures</li>
  86. <li>2D Texture Arrays</li>
  87. <li>Multisampled Renderbuffers</li>
  88. <li>Non-power of two (POT) textures work just the same as POT textures now. No resizing is required for best quality.</li>
  89. </ul>
  90. </p>
  91. </body>
  92. </html>