0tamega

segitiga_gradasi

Jan 25th, 2023
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <canvas id="canvas" width="500" height="500"></canvas>
  5.  
  6.  
  7. <script id="vertex" type="x-shader/x-vertex">
  8. #version 300 es
  9.  
  10.  
  11. in vec4 vertexPosition;
  12.  
  13.  
  14. void main() {
  15. gl_Position = vertexPosition;
  16. }
  17. </script>
  18.  
  19.  
  20. <script id="fragment" type="x-shader/x-fragment">
  21. #version 300 es
  22. precision highp float;
  23.  
  24.  
  25. uniform vec2 canvasSize;
  26. uniform float time;
  27. out vec4 fragColor;
  28.  
  29.  
  30. void main() {
  31. vec2 coord = gl_FragCoord.xy/canvasSize.xy;
  32. coord = 2.*coord - 1.;
  33. float scale = (sin(time) + 1.)/2.;
  34. coord /= scale;
  35. if (abs(coord.x) < 1. && abs(coord.y) < 1.) {
  36. coord = (coord + 1.)/4.0;
  37. fragColor = vec4(coord.x, 2, 5.2-coord.y, 0.3);
  38. } else {
  39. fragColor = vec4(1,1,1,1);
  40. }
  41. }
  42. </script>
  43.  
  44.  
  45. <script>
  46. const canvas = document.getElementById("canvas");
  47. const vertexCode = document.getElementById("vertex").textContent;
  48. const fragmentCode = document.getElementById("fragment").textContent;
  49.  
  50.  
  51. const gl = canvas.getContext("webgl2");
  52. if (!gl) throw "WebGL2 not supported";
  53.  
  54.  
  55. function createShader(shaderType, sourceCode) {
  56. const shader = gl.createShader(shaderType);
  57. gl.shaderSource(shader, sourceCode.trim());
  58. gl.compileShader(shader);
  59. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  60. throw gl.getShaderInfoLog(shader);
  61. }
  62. return shader;
  63. }
  64.  
  65.  
  66. const program = gl.createProgram();
  67. gl.attachShader(program, createShader(gl.VERTEX_SHADER, vertexCode));
  68. gl.attachShader(program, createShader(gl.FRAGMENT_SHADER, fragmentCode));
  69. gl.linkProgram(program);
  70. if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
  71. throw gl.getProgramInfoLog(program);
  72. }
  73. gl.useProgram(program);
  74.  
  75.  
  76. const vertices = [
  77. [0, 0],
  78. [-1, 0.5],
  79. [-1, 0],
  80. [-1, 0],
  81. ];
  82. const vertexData = new Float32Array(vertices.flat());
  83. gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
  84. gl.bufferData(gl.ARRAY_BUFFER, vertexData, gl.STATIC_DRAW);
  85.  
  86.  
  87. const vertexPosition = gl.getAttribLocation(program, "vertexPosition");
  88. gl.enableVertexAttribArray(vertexPosition);
  89. gl.vertexAttribPointer(vertexPosition, 2, gl.FLOAT, false, 0, 0);
  90.  
  91.  
  92. const canvasSizeUniform = gl.getUniformLocation(program, "canvasSize");
  93. gl.uniform2f(canvasSizeUniform, canvas.width, canvas.height);
  94.  
  95.  
  96. const timeUniform = gl.getUniformLocation(program, "time");
  97.  
  98.  
  99. function draw() {
  100. gl.uniform1f(timeUniform, performance.now() / 800);
  101. gl.drawArrays(gl.TRIANGLE_STRIP, 0, vertices.length);
  102. requestAnimationFrame(draw);
  103. }
  104. draw();
  105. </script>
  106. </body>
  107. </html>
Advertisement
Add Comment
Please, Sign In to add comment