webgl_buffergeometry_rawshader.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - raw shader</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 - raw shader">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_buffergeometry_rawshader.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_buffergeometry_rawshader.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - raw shader demo</div>
  16. <script id="vertexShader" type="x-shader/x-vertex">
  17. precision mediump float;
  18. precision mediump int;
  19. uniform mat4 modelViewMatrix; // optional
  20. uniform mat4 projectionMatrix; // optional
  21. attribute vec3 position;
  22. attribute vec4 color;
  23. varying vec3 vPosition;
  24. varying vec4 vColor;
  25. void main() {
  26. vPosition = position;
  27. vColor = color;
  28. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  29. }
  30. </script>
  31. <script id="fragmentShader" type="x-shader/x-fragment">
  32. precision mediump float;
  33. precision mediump int;
  34. uniform float time;
  35. varying vec3 vPosition;
  36. varying vec4 vColor;
  37. void main() {
  38. vec4 color = vec4( vColor );
  39. color.r += sin( vPosition.x * 10.0 + time ) * 0.5;
  40. gl_FragColor = color;
  41. }
  42. </script>
  43. <script type="importmap">
  44. {
  45. "imports": {
  46. "three": "../build/three.module.js",
  47. "three/addons/": "./jsm/"
  48. }
  49. }
  50. </script>
  51. <script type="module">
  52. import * as THREE from 'three';
  53. import Stats from 'three/addons/libs/stats.module.js';
  54. let container, stats;
  55. let camera, scene, renderer;
  56. init();
  57. function init() {
  58. container = document.getElementById( 'container' );
  59. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10 );
  60. camera.position.z = 2;
  61. scene = new THREE.Scene();
  62. scene.background = new THREE.Color( 0x101010 );
  63. // geometry
  64. // nr of triangles with 3 vertices per triangle
  65. const vertexCount = 200 * 3;
  66. const geometry = new THREE.BufferGeometry();
  67. const positions = [];
  68. const colors = [];
  69. for ( let i = 0; i < vertexCount; i ++ ) {
  70. // adding x,y,z
  71. positions.push( Math.random() - 0.5 );
  72. positions.push( Math.random() - 0.5 );
  73. positions.push( Math.random() - 0.5 );
  74. // adding r,g,b,a
  75. colors.push( Math.random() * 255 );
  76. colors.push( Math.random() * 255 );
  77. colors.push( Math.random() * 255 );
  78. colors.push( Math.random() * 255 );
  79. }
  80. const positionAttribute = new THREE.Float32BufferAttribute( positions, 3 );
  81. const colorAttribute = new THREE.Uint8BufferAttribute( colors, 4 );
  82. colorAttribute.normalized = true; // this will map the buffer values to 0.0f - +1.0f in the shader
  83. geometry.setAttribute( 'position', positionAttribute );
  84. geometry.setAttribute( 'color', colorAttribute );
  85. // material
  86. const material = new THREE.RawShaderMaterial( {
  87. uniforms: {
  88. time: { value: 1.0 }
  89. },
  90. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  91. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  92. side: THREE.DoubleSide,
  93. transparent: true
  94. } );
  95. const mesh = new THREE.Mesh( geometry, material );
  96. scene.add( mesh );
  97. renderer = new THREE.WebGLRenderer();
  98. renderer.setPixelRatio( window.devicePixelRatio );
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. renderer.setAnimationLoop( animate );
  101. container.appendChild( renderer.domElement );
  102. stats = new Stats();
  103. container.appendChild( stats.dom );
  104. window.addEventListener( 'resize', onWindowResize );
  105. }
  106. function onWindowResize() {
  107. camera.aspect = window.innerWidth / window.innerHeight;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. }
  111. //
  112. function animate() {
  113. const time = performance.now();
  114. const object = scene.children[ 0 ];
  115. object.rotation.y = time * 0.0005;
  116. object.material.uniforms.time.value = time * 0.005;
  117. renderer.render( scene, camera );
  118. stats.update();
  119. }
  120. </script>
  121. </body>
  122. </html>
粤ICP备19079148号