webgl_buffergeometry_rawshader.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. <!-- Import maps polyfill -->
  40. <!-- Remove this when import maps will be widely supported -->
  41. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  42. <script type="importmap">
  43. {
  44. "imports": {
  45. "three": "../build/three.module.js",
  46. "three/addons/": "./jsm/"
  47. }
  48. }
  49. </script>
  50. <script type="module">
  51. import * as THREE from 'three';
  52. import Stats from 'three/addons/libs/stats.module.js';
  53. let container, stats;
  54. let camera, scene, renderer;
  55. init();
  56. animate();
  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. container.appendChild( renderer.domElement );
  101. stats = new Stats();
  102. container.appendChild( stats.dom );
  103. window.addEventListener( 'resize', onWindowResize );
  104. }
  105. function onWindowResize() {
  106. camera.aspect = window.innerWidth / window.innerHeight;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. //
  111. function animate() {
  112. requestAnimationFrame( animate );
  113. render();
  114. stats.update();
  115. }
  116. function render() {
  117. const time = performance.now();
  118. const object = scene.children[ 0 ];
  119. object.rotation.y = time * 0.0005;
  120. object.material.uniforms.time.value = time * 0.005;
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>