Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. // Welcome! require() some modules from npm (like you were using browserify)
  2. // and then hit Run Code to run your code on the right side.
  3. // Modules get downloaded from browserify-cdn and bundled in your browser.
  4.  
  5.  
  6. const React = require('react');
  7. const ReactDOM = require('react-dom');
  8. const reglInit = require('regl')
  9. const mat4 = require('gl-mat4')
  10. const bunny = require('bunny')
  11.  
  12. const Stats = require('stats.js');
  13. const updateStats = new Stats();
  14. document.body.appendChild(updateStats.dom);
  15. updateStats.showPanel(0);
  16.  
  17. const SPECTOR = require('spectorjs');
  18. var s = new SPECTOR.Spector();
  19. s.displayUI();
  20.  
  21. class Root extends React.Component {
  22. componentDidMount() {
  23. const regl = reglInit({
  24. canvas: this.refs.drawCanvas
  25. });
  26.  
  27. const drawBunny = regl({
  28. vert: `
  29. precision mediump float;
  30. attribute vec3 position;
  31. uniform mat4 model, view, projection;
  32. void main() {
  33. gl_Position = projection * view * model * vec4(position, 1);
  34. }`,
  35.  
  36. frag: `
  37. precision mediump float;
  38. void main() {
  39. gl_FragColor = vec4(1, 1, 1, 1);
  40. }`,
  41.  
  42. // this converts the vertices of the mesh into the position attribute
  43. attributes: {
  44. position: bunny.positions
  45. },
  46.  
  47. // and this converts the faces fo the mesh into elements
  48. elements: bunny.cells,
  49.  
  50. uniforms: {
  51. model: mat4.identity([]),
  52. view: ({tick}) => {
  53. const t = 0.01 * tick
  54. return mat4.lookAt([],
  55. [30 * Math.cos(t), 2.5, 30 * Math.sin(t)],
  56. [0, 2.5, 0],
  57. [0, 1, 0])
  58. },
  59. projection: ({viewportWidth, viewportHeight}) =>
  60. mat4.perspective([],
  61. Math.PI / 4,
  62. viewportWidth / viewportHeight,
  63. 0.01,
  64. 1000)
  65. }
  66. });
  67. regl.frame(() => {
  68. updateStats.begin();
  69. regl.clear({
  70. depth: 1,
  71. color: [0, 0, 0, 1]
  72. })
  73.  
  74. drawBunny()
  75. updateStats.end();
  76. })
  77. }
  78.  
  79. render() {
  80. return React.createElement('canvas', {
  81. //ref: "drawCanvas",
  82. id: "draw-canvas"
  83. }, null);
  84. }
  85. }
  86.  
  87. ReactDOM.render(
  88. React.createElement(Root, null, null),
  89. document.getElementById('ui-root')
  90. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement