webgl_ubo.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. let camera, scene, renderer, clock;
  129. init();
  130. function init() {
  131. const container = document.getElementById( 'container' );
  132. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  133. camera.position.set( 0, 0, 25 );
  134. scene = new THREE.Scene();
  135. camera.lookAt( scene.position );
  136. clock = new THREE.Clock();
  137. // geometry
  138. const geometry1 = new THREE.TetrahedronGeometry();
  139. const geometry2 = new THREE.BoxGeometry();
  140. // texture
  141. const texture = new THREE.TextureLoader().load( 'textures/crate.gif' );
  142. texture.colorSpace = THREE.SRGBColorSpace;
  143. // uniforms groups
  144. // Camera and lighting related data are perfect examples of using UBOs since you have to store these
  145. // data just once. They can be shared across all shader programs.
  146. const cameraUniformsGroup = new THREE.UniformsGroup();
  147. cameraUniformsGroup.setName( 'ViewData' );
  148. cameraUniformsGroup.add( new THREE.Uniform( camera.projectionMatrix ) ); // projection matrix
  149. cameraUniformsGroup.add( new THREE.Uniform( camera.matrixWorldInverse ) ); // view matrix
  150. const lightingUniformsGroup = new THREE.UniformsGroup();
  151. lightingUniformsGroup.setName( 'LightingData' );
  152. lightingUniformsGroup.add( new THREE.Uniform( new THREE.Vector3( 0, 0, 10 ) ) ); // light position
  153. lightingUniformsGroup.add( new THREE.Uniform( new THREE.Color( 0x7c7c7c ) ) ); // ambient color
  154. lightingUniformsGroup.add( new THREE.Uniform( new THREE.Color( 0xd5d5d5 ) ) ); // diffuse color
  155. lightingUniformsGroup.add( new THREE.Uniform( new THREE.Color( 0xe7e7e7 ) ) ); // specular color
  156. lightingUniformsGroup.add( new THREE.Uniform( 64 ) ); // shininess
  157. // materials
  158. const material1 = new THREE.RawShaderMaterial( {
  159. uniforms: {
  160. modelMatrix: { value: null },
  161. normalMatrix: { value: null },
  162. color: { value: null }
  163. },
  164. vertexShader: document.getElementById( 'vertexShader1' ).textContent,
  165. fragmentShader: document.getElementById( 'fragmentShader1' ).textContent,
  166. glslVersion: THREE.GLSL3
  167. } );
  168. const material2 = new THREE.RawShaderMaterial( {
  169. uniforms: {
  170. modelMatrix: { value: null },
  171. diffuseMap: { value: null },
  172. },
  173. vertexShader: document.getElementById( 'vertexShader2' ).textContent,
  174. fragmentShader: document.getElementById( 'fragmentShader2' ).textContent,
  175. glslVersion: THREE.GLSL3
  176. } );
  177. // meshes
  178. for ( let i = 0; i < 200; i ++ ) {
  179. let mesh;
  180. if ( i % 2 === 0 ) {
  181. mesh = new THREE.Mesh( geometry1, material1.clone() );
  182. mesh.material.uniformsGroups = [ cameraUniformsGroup, lightingUniformsGroup ];
  183. mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld;
  184. mesh.material.uniforms.normalMatrix.value = mesh.normalMatrix;
  185. mesh.material.uniforms.color.value = new THREE.Color( 0xffffff * Math.random() );
  186. } else {
  187. mesh = new THREE.Mesh( geometry2, material2.clone() );
  188. mesh.material.uniformsGroups = [ cameraUniformsGroup, lightingUniformsGroup ];
  189. mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld;
  190. mesh.material.uniforms.diffuseMap.value = texture;
  191. }
  192. scene.add( mesh );
  193. const s = 1 + Math.random() * 0.5;
  194. mesh.scale.x = s;
  195. mesh.scale.y = s;
  196. mesh.scale.z = s;
  197. mesh.rotation.x = Math.random() * Math.PI;
  198. mesh.rotation.y = Math.random() * Math.PI;
  199. mesh.rotation.z = Math.random() * Math.PI;
  200. mesh.position.x = Math.random() * 40 - 20;
  201. mesh.position.y = Math.random() * 40 - 20;
  202. mesh.position.z = Math.random() * 20 - 10;
  203. }
  204. //
  205. renderer = new THREE.WebGLRenderer( { antialias: true } );
  206. renderer.setPixelRatio( window.devicePixelRatio );
  207. renderer.setSize( window.innerWidth, window.innerHeight );
  208. renderer.setAnimationLoop( animate );
  209. container.appendChild( renderer.domElement );
  210. window.addEventListener( 'resize', onWindowResize, false );
  211. }
  212. function onWindowResize() {
  213. camera.aspect = window.innerWidth / window.innerHeight;
  214. camera.updateProjectionMatrix();
  215. renderer.setSize( window.innerWidth, window.innerHeight );
  216. }
  217. //
  218. function animate() {
  219. const delta = clock.getDelta();
  220. scene.traverse( function ( child ) {
  221. if ( child.isMesh ) {
  222. child.rotation.x += delta * 0.5;
  223. child.rotation.y += delta * 0.3;
  224. }
  225. } );
  226. renderer.render( scene, camera );
  227. }
  228. </script>
  229. </body>
  230. </html>