webgl_buffergeometry_rawshader.html 4.1 KB

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