Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. const regl = require('regl')();
  2. const d3Random = require('d3-random');
  3. const d3Array = require('d3-array');
  4.  
  5. const rng = d3Random.randomNormal(0, 0.1);
  6. const { range } = d3Array;
  7.  
  8. const numPoints = 100000;
  9.  
  10. // the size of the points we draw on screen
  11. const pointWidth = 2;
  12.  
  13. // dimensions of the viewport we are drawing in
  14. const width = window.innerWidth;
  15. const height = window.innerHeight;
  16.  
  17. // generaate some random points
  18. const points = range(numPoints).map(i => ({
  19. x1: (rng() * width) + (width / 2),
  20. y1: (rng() * height) + (height / 2),
  21. x: rng(),
  22. y: rng(),
  23. size: Math.random() * 5 + pointWidth,
  24. color: [0, Math.random(), 0],
  25. delta: [rng(), rng()],
  26. }));
  27.  
  28. // draw points
  29. const drawStuff = regl({
  30. frag: `
  31. precision mediump float;
  32. varying vec3 fragColor;
  33.  
  34.  
  35. void main() {
  36. float r = 0.0;
  37. float delta = 0.0;
  38. float alpha = 1.0;
  39. vec2 cxy = 2.0 * gl_PointCoord - 1.0;
  40. r = dot(cxy, cxy);
  41. if (r > 1.0) {
  42. discard;
  43. }
  44.  
  45. gl_FragColor = vec4(fragColor, 0.5) * alpha;
  46. }
  47. `,
  48. vert: `
  49. precision mediump float;
  50. attribute vec2 position;
  51. attribute vec2 delta;
  52. attribute vec3 color;
  53. attribute float size;
  54.  
  55.  
  56. varying vec3 fragColor;
  57.  
  58. uniform float pointWidth;
  59. uniform float stageWidth;
  60. uniform float stageHeight;
  61. uniform float time;
  62.  
  63. void main() {
  64. gl_PointSize = size;
  65.  
  66. fragColor = color;
  67. gl_Position = vec4(position.x + sin(time * delta.x), position.y + cos(time * delta.y), 0.0, 1.0);
  68. }
  69. `,
  70. attributes: {
  71. position: points.map(d => [d.x, d.y]),
  72. delta: points.map(d => d.delta),
  73. color: points.map(d => [1.0,1.0,0.0]),
  74. size: points.map(d => d.size),
  75. },
  76. uniforms: {
  77. pointWidth: regl.prop('pointWidth'),
  78. stageWidth: regl.prop('width'),
  79. stageHeight: regl.prop('height'),
  80. time: regl.context('time'),
  81. },
  82. blend: {
  83. enable: true,
  84. func: {
  85. srcRGB: 'src alpha',
  86. srcAlpha: 'src alpha',
  87. dstRGB: 'one minus src alpha',
  88. dstAlpha: 'one minus src alpha',
  89. },
  90. },
  91. depth: { enable: false },
  92. count: points.length,
  93. primitive: 'points',
  94. })
  95.  
  96. // loop
  97. regl.frame(({time}) => {
  98. regl.clear({
  99. color: [0,0,0,1],
  100. depth: 1,
  101. });
  102. drawStuff({
  103. pointWidth: pointWidth,
  104. width,
  105. height,
  106. });
  107. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement