webgl_buffergeometry_rawshader.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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="module">
  40. import * as THREE from '../build/three.module.js';
  41. import Stats from './jsm/libs/stats.module.js';
  42. var container, stats;
  43. var camera, scene, renderer;
  44. init();
  45. animate();
  46. function init() {
  47. container = document.getElementById( 'container' );
  48. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10 );
  49. camera.position.z = 2;
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0x101010 );
  52. // geometry
  53. // nr of triangles with 3 vertices per triangle
  54. var vertexCount = 200 * 3;
  55. var geometry = new THREE.BufferGeometry();
  56. var positions = [];
  57. var colors = [];
  58. for ( var i = 0; i < vertexCount; i ++ ) {
  59. // adding x,y,z
  60. positions.push( Math.random() - 0.5 );
  61. positions.push( Math.random() - 0.5 );
  62. positions.push( Math.random() - 0.5 );
  63. // adding r,g,b,a
  64. colors.push( Math.random() * 255 );
  65. colors.push( Math.random() * 255 );
  66. colors.push( Math.random() * 255 );
  67. colors.push( Math.random() * 255 );
  68. }
  69. var positionAttribute = new THREE.Float32BufferAttribute( positions, 3 );
  70. var colorAttribute = new THREE.Uint8BufferAttribute( colors, 4 );
  71. colorAttribute.normalized = true; // this will map the buffer values to 0.0f - +1.0f in the shader
  72. geometry.setAttribute( 'position', positionAttribute );
  73. geometry.setAttribute( 'color', colorAttribute );
  74. // material
  75. var material = new THREE.RawShaderMaterial( {
  76. uniforms: {
  77. time: { value: 1.0 }
  78. },
  79. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  80. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  81. side: THREE.DoubleSide,
  82. transparent: true
  83. } );
  84. var mesh = new THREE.Mesh( geometry, material );
  85. scene.add( mesh );
  86. renderer = new THREE.WebGLRenderer();
  87. renderer.setPixelRatio( window.devicePixelRatio );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. container.appendChild( renderer.domElement );
  90. stats = new Stats();
  91. container.appendChild( stats.dom );
  92. window.addEventListener( 'resize', onWindowResize, false );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. //
  100. function animate() {
  101. requestAnimationFrame( animate );
  102. render();
  103. stats.update();
  104. }
  105. function render() {
  106. var time = performance.now();
  107. var object = scene.children[ 0 ];
  108. object.rotation.y = time * 0.0005;
  109. object.material.uniforms.time.value = time * 0.005;
  110. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>