How-to-use-WebGL2.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. <p>
  39. Since you are manually creating the WebGL 2 rendering context, you also have to pass in all necessary context attributes.
  40. Note: It's not possible to modify these attributes after the context has been created, so passing them to the WebGLRenderer won't have any effect.
  41. </p>
  42. <code>
  43. var canvas = document.createElement( 'canvas' );
  44. var context = canvas.getContext( 'webgl2', { alpha: false } );
  45. var renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  46. </code>
  47. <p>
  48. Sometimes it is necessary to write custom shader code. Use the following code template as a basis for your own
  49. implementation. First, the GLSL ES 3.00 code.
  50. </p>
  51. <code>
  52. &lt;script id="vs" type="x-shader/x-vertex"&gt;
  53. #version 300 es
  54. void main() {
  55. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  56. }
  57. &lt;/script&gt;
  58. &lt;script id="fs" type="x-shader/x-fragment"&gt;
  59. #version 300 es
  60. precision highp float;
  61. precision highp int;
  62. out vec4 out_FragColor;
  63. void main() {
  64. out_FragColor = vec4( 1.0 );
  65. }
  66. &lt;/script&gt;
  67. </code>
  68. <p>
  69. Second, the corresponding material creation in JavaScript.
  70. </p>
  71. <code>
  72. var material = new THREE.ShaderMaterial( {
  73. vertexShader: document.getElementById( 'vs' ).textContent.trim(),
  74. fragmentShader: document.getElementById( 'fs' ).textContent.trim()
  75. } );
  76. </code>
  77. <h2>Next Steps</h2>
  78. <p>
  79. Have a look at one of the official examples in order to see WebGL 2 features in action.<br /><br />
  80. [example:webgl2_materials_texture3d WebGL2 / materials / texture3d]<br />
  81. [example:webgl2_materials_texture2darray WebGL2 / materials / texture2darray]<br />
  82. [example:webgl2_multisampled_renderbuffers WebGL2 / multisampled renderbuffers]
  83. </p>
  84. <h2>Supported features</h2>
  85. <p>
  86. Right now, the engine does only support a subset of all existing WebGL 2 features. The following list provides an
  87. overview about what's already available in the latest version of three.js.
  88. <ul>
  89. <li>3D Textures</li>
  90. <li>2D Texture Arrays</li>
  91. <li>Multisampled Renderbuffers</li>
  92. <li>Non-power of two (POT) textures work just the same as POT textures now. No resizing is required for best quality.</li>
  93. </ul>
  94. </p>
  95. </body>
  96. </html>