webgl2_ubo.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js WebGL 2 - Uniform Buffer Objects</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Uniform Buffer Objects
  12. </div>
  13. <div id="container"></div>
  14. <script id="vertexShader1" type="x-shader/x-vertex">
  15. uniform ViewData {
  16. mat4 projectionMatrix;
  17. mat4 viewMatrix;
  18. };
  19. uniform mat4 modelMatrix;
  20. uniform mat3 normalMatrix;
  21. in vec3 position;
  22. in vec3 normal;
  23. out vec3 vPositionEye;
  24. out vec3 vNormalEye;
  25. void main() {
  26. vec4 vertexPositionEye = viewMatrix * modelMatrix * vec4( position, 1.0 );
  27. vPositionEye = vertexPositionEye.xyz;
  28. vNormalEye = normalMatrix * normal;
  29. gl_Position = projectionMatrix * vertexPositionEye;
  30. }
  31. </script>
  32. <script id="fragmentShader1" type="x-shader/x-fragment">
  33. precision highp float;
  34. vec4 LinearTosRGB( in vec4 value ) {
  35. return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a );
  36. }
  37. uniform LightingData {
  38. vec3 position;
  39. vec3 ambientColor;
  40. vec3 diffuseColor;
  41. vec3 specularColor;
  42. float shininess;
  43. } Light;
  44. uniform vec3 color;
  45. in vec3 vPositionEye;
  46. in vec3 vNormalEye;
  47. out vec4 fragColor;
  48. void main() {
  49. // a very basic lighting equation (Phong reflection model) for testing
  50. vec3 l = normalize( Light.position - vPositionEye );
  51. vec3 n = normalize( vNormalEye );
  52. vec3 e = - normalize( vPositionEye );
  53. vec3 r = normalize( reflect( - l, n ) );
  54. float diffuseLightWeighting = max( dot( n, l ), 0.0 );
  55. float specularLightWeighting = max( dot( r, e ), 0.0 );
  56. specularLightWeighting = pow( specularLightWeighting, Light.shininess );
  57. vec3 lightWeighting = Light.ambientColor +
  58. Light.diffuseColor * diffuseLightWeighting +
  59. Light.specularColor * specularLightWeighting;
  60. fragColor = vec4( color.rgb * lightWeighting.rgb, 1.0 );
  61. fragColor = LinearTosRGB( fragColor );
  62. }
  63. </script>
  64. <script id="vertexShader2" type="x-shader/x-vertex">
  65. layout(std140) uniform ViewData {
  66. mat4 projectionMatrix;
  67. mat4 viewMatrix;
  68. };
  69. uniform mat4 modelMatrix;
  70. uniform mat3 normalMatrix;
  71. in vec3 position;
  72. in vec3 normal;
  73. in vec2 uv;
  74. out vec3 vPositionEye;
  75. out vec3 vNormalEye;
  76. out vec2 vUv;
  77. void main() {
  78. vec4 vertexPositionEye = viewMatrix * modelMatrix * vec4( position, 1.0 );
  79. vPositionEye = vertexPositionEye.xyz;
  80. vNormalEye = normalMatrix * normal;
  81. vUv = uv;
  82. gl_Position = projectionMatrix * vertexPositionEye;
  83. }
  84. </script>
  85. <script id="fragmentShader2" type="x-shader/x-fragment">
  86. precision highp float;
  87. vec4 LinearTosRGB( in vec4 value ) {
  88. return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a );
  89. }
  90. uniform sampler2D diffuseMap;
  91. in vec2 vUv;
  92. in vec3 vPositionEye;
  93. in vec3 vNormalEye;
  94. out vec4 fragColor;
  95. uniform LightingData {
  96. vec3 position;
  97. vec3 ambientColor;
  98. vec3 diffuseColor;
  99. vec3 specularColor;
  100. float shininess;
  101. } Light;
  102. void main() {
  103. // a very basic lighting equation (Phong reflection model) for testing
  104. vec3 l = normalize( Light.position - vPositionEye );
  105. vec3 n = normalize( vNormalEye );
  106. vec3 e = - normalize( vPositionEye );
  107. vec3 r = normalize( reflect( - l, n ) );
  108. float diffuseLightWeighting = max( dot( n, l ), 0.0 );
  109. float specularLightWeighting = max( dot( r, e ), 0.0 );
  110. specularLightWeighting = pow( specularLightWeighting, Light.shininess );
  111. vec3 lightWeighting = Light.ambientColor +
  112. Light.diffuseColor * diffuseLightWeighting +
  113. Light.specularColor * specularLightWeighting;
  114. fragColor = vec4( texture( diffuseMap, vUv ).rgb * lightWeighting.rgb, 1.0 );
  115. fragColor = LinearTosRGB( fragColor );
  116. }
  117. </script>
  118. <script type="importmap">
  119. {
  120. "imports": {
  121. "three": "../build/three.module.js",
  122. "three/addons/": "./jsm/"
  123. }
  124. }
  125. </script>
  126. <script type="module">
  127. import * as THREE from 'three';
  128. import WebGL from 'three/addons/capabilities/WebGL.js';
  129. let camera, scene, renderer, clock;
  130. init();
  131. animate();
  132. function init() {
  133. if ( WebGL.isWebGL2Available() === false ) {
  134. document.body.appendChild( WebGL.getWebGL2ErrorMessage() );
  135. return;
  136. }
  137. const container = document.getElementById( 'container' );
  138. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  139. camera.position.set( 0, 0, 25 );
  140. scene = new THREE.Scene();
  141. camera.lookAt( scene.position );
  142. clock = new THREE.Clock();
  143. // geometry
  144. const geometry1 = new THREE.TetrahedronGeometry();
  145. const geometry2 = new THREE.BoxGeometry();
  146. // texture
  147. const texture = new THREE.TextureLoader().load( 'textures/crate.gif' );
  148. texture.colorSpace = THREE.SRGBColorSpace;
  149. // uniforms groups
  150. // Camera and lighting related data are perfect examples of using UBOs since you have to store these
  151. // data just once. They can be shared across all shader programs.
  152. const cameraUniformsGroup = new THREE.UniformsGroup();
  153. cameraUniformsGroup.setName( 'ViewData' );
  154. cameraUniformsGroup.add( new THREE.Uniform( camera.projectionMatrix ) ); // projection matrix
  155. cameraUniformsGroup.add( new THREE.Uniform( camera.matrixWorldInverse ) ); // view matrix
  156. const lightingUniformsGroup = new THREE.UniformsGroup();
  157. lightingUniformsGroup.setName( 'LightingData' );
  158. lightingUniformsGroup.add( new THREE.Uniform( new THREE.Vector3( 0, 0, 10 ) ) ); // light position
  159. lightingUniformsGroup.add( new THREE.Uniform( new THREE.Color( 0x7c7c7c ) ) ); // ambient color
  160. lightingUniformsGroup.add( new THREE.Uniform( new THREE.Color( 0xd5d5d5 ) ) ); // diffuse color
  161. lightingUniformsGroup.add( new THREE.Uniform( new THREE.Color( 0xe7e7e7 ) ) ); // specular color
  162. lightingUniformsGroup.add( new THREE.Uniform( 64 ) ); // shininess
  163. // materials
  164. const material1 = new THREE.RawShaderMaterial( {
  165. uniforms: {
  166. modelMatrix: { value: null },
  167. normalMatrix: { value: null },
  168. color: { value: null }
  169. },
  170. vertexShader: document.getElementById( 'vertexShader1' ).textContent,
  171. fragmentShader: document.getElementById( 'fragmentShader1' ).textContent,
  172. glslVersion: THREE.GLSL3
  173. } );
  174. const material2 = new THREE.RawShaderMaterial( {
  175. uniforms: {
  176. modelMatrix: { value: null },
  177. diffuseMap: { value: null },
  178. },
  179. vertexShader: document.getElementById( 'vertexShader2' ).textContent,
  180. fragmentShader: document.getElementById( 'fragmentShader2' ).textContent,
  181. glslVersion: THREE.GLSL3
  182. } );
  183. // meshes
  184. for ( let i = 0; i < 200; i ++ ) {
  185. let mesh;
  186. if ( i % 2 === 0 ) {
  187. mesh = new THREE.Mesh( geometry1, material1.clone() );
  188. mesh.material.uniformsGroups = [ cameraUniformsGroup, lightingUniformsGroup ];
  189. mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld;
  190. mesh.material.uniforms.normalMatrix.value = mesh.normalMatrix;
  191. mesh.material.uniforms.color.value = new THREE.Color( 0xffffff * Math.random() );
  192. } else {
  193. mesh = new THREE.Mesh( geometry2, material2.clone() );
  194. mesh.material.uniformsGroups = [ cameraUniformsGroup, lightingUniformsGroup ];
  195. mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld;
  196. mesh.material.uniforms.diffuseMap.value = texture;
  197. }
  198. scene.add( mesh );
  199. const s = 1 + Math.random() * 0.5;
  200. mesh.scale.x = s;
  201. mesh.scale.y = s;
  202. mesh.scale.z = s;
  203. mesh.rotation.x = Math.random() * Math.PI;
  204. mesh.rotation.y = Math.random() * Math.PI;
  205. mesh.rotation.z = Math.random() * Math.PI;
  206. mesh.position.x = Math.random() * 40 - 20;
  207. mesh.position.y = Math.random() * 40 - 20;
  208. mesh.position.z = Math.random() * 20 - 10;
  209. }
  210. //
  211. renderer = new THREE.WebGLRenderer( { antialias: true } );
  212. renderer.setPixelRatio( window.devicePixelRatio );
  213. renderer.setSize( window.innerWidth, window.innerHeight );
  214. container.appendChild( renderer.domElement );
  215. window.addEventListener( 'resize', onWindowResize, false );
  216. }
  217. function onWindowResize() {
  218. camera.aspect = window.innerWidth / window.innerHeight;
  219. camera.updateProjectionMatrix();
  220. renderer.setSize( window.innerWidth, window.innerHeight );
  221. }
  222. //
  223. function animate() {
  224. requestAnimationFrame( animate );
  225. const delta = clock.getDelta();
  226. scene.traverse( function ( child ) {
  227. if ( child.isMesh ) {
  228. child.rotation.x += delta * 0.5;
  229. child.rotation.y += delta * 0.3;
  230. }
  231. } );
  232. renderer.render( scene, camera );
  233. }
  234. </script>
  235. </body>
  236. </html>