webgl_ubo.html 9.1 KB

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