How-to-use-WebGL2.html 3.1 KB

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