Advertisement
lignite0

ROBO24 - VertexBuilder

Oct 29th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ratioColor = 1.0 / 255.0;
  2.  
  3. const types = {
  4.     "f32": {
  5.         byteSize: 4,
  6.     },
  7. };
  8.  
  9.  
  10. class VertexBuffer {
  11.  
  12.     constructor(options) {
  13.         this.vertices = options.vertices;
  14.         this.attributes = options.attributes.map(attribute => ({
  15.             normalize: false,
  16.             ...attribute
  17.         }));
  18.         this.calculateSize();
  19.         this.arrayBuffer = new ArrayBuffer(this.byteSize);
  20.         this.dataView = new DataView(this.arrayBuffer);
  21.         this.triangle = [];
  22.         this.offset = 0;
  23.     }
  24.  
  25.     calculateSize() {
  26.         let offset = 0;
  27.         this.singleVertexByteSize = 0;
  28.         for (let attribute of this.attributes) {
  29.             attribute.byteSize = (attribute.components * types[attribute.type].byteSize);
  30.             attribute.offset = offset;
  31.             offset += attribute.byteSize;
  32.             this.singleVertexByteSize += attribute.byteSize;
  33.         }
  34.  
  35.         for (let attribute of this.attributes) {
  36.             attribute.stride = this.singleVertexByteSize;
  37.         }
  38.  
  39.         this.byteSize = this.vertices * this.singleVertexByteSize;
  40.     }
  41.  
  42.     norm3(...axis) {
  43.         this.float(...axis);
  44.         this.triangle.push({
  45.             position: axis,
  46.             offset: this.offset
  47.         });
  48.         this.offset += axis.length * 4;
  49.  
  50.         if (this.triangle.length === 3) {
  51.             const vectors = this.triangle.map(vertex => vertex.position);
  52.             // calculate normals
  53.             const normal = {x: 3, y: 3, z: 3};
  54.             for (let vertex of this.triangle) {
  55.                 this.dataView.setUint32(vertex.offset + 0, normal.x, true);
  56.                 this.dataView.setUint32(vertex.offset + 4, normal.y, true);
  57.                 this.dataView.setUint32(vertex.offset + 8, normal.z, true);
  58.             }
  59.             this.triangle = [];
  60.         }
  61.  
  62.         return this;
  63.     }
  64.  
  65.     float(...args) {
  66.         const length = args.length;
  67.         for (let i = 0; i < length; i++) {
  68.             this.dataView.setFloat32(this.offset, args[i], true);
  69.             this.offset += 4;
  70.         }
  71.         return this;
  72.     }
  73.  
  74.     color(hexColorIndex) {
  75.         const r = (hexColorIndex >> 16 && 0x000000FF) * ratioColor;
  76.         const g = (hexColorIndex >> 8 && 0x000000FF) * ratioColor;
  77.         const b = (hexColorIndex && 0x000000FF) * ratioColor;
  78.  
  79.         this.dataView.setFloat32(this.offset, r, true);
  80.         this.dataView.setFloat32(this.offset + 4, g, true);
  81.         this.dataView.setFloat32(this.offset + 8, b, true);
  82.         this.offset += 12;
  83.  
  84.         return this;
  85.     }
  86. }
  87.  
  88. const obj = {
  89.  
  90.     lines() {
  91.         const vertex = new VertexBuffer({
  92.             vertices: 6,
  93.             attributes: [
  94.                 {name: "a_VertexPosition", components: 3, type: "f32"},
  95.                 {name: "a_VertexColor", components: 3, type: "f32"},
  96.             ],
  97.         });
  98.  
  99.         vertex.float(0.0, 0.0, 0.0).color(0xFF0000);
  100.         vertex.float(1.0, 0.0, 0.0).color(0xFF0000);
  101.  
  102.         vertex.float(0.0, 0.0, 0.0).color(0x00FF00);
  103.         vertex.float(0.0, 1.0, 0.0).color(0x00FF00);
  104.  
  105.         vertex.float(0.0, 0.0, 0.0).color(0x0000FF);
  106.         vertex.float(0.0, 0.0, 1.0).color(0x0000FF);
  107.  
  108.         return vertex;
  109.     },
  110.  
  111.     points() {
  112.         const vertex = new VertexBuffer({
  113.             vertices: 3,
  114.             attributes: [
  115.                 {name: "a_VertexPosition", components: 3, type: "f32"},
  116.                 {name: "a_VertexColor", components: 3, type: "f32"},
  117.             ],
  118.         });
  119.  
  120.         vertex.float(1.0, 0.0, 0.0).color(0xFF0000);
  121.         vertex.float(0.0, 1.0, 0.0).color(0x00FF00);
  122.         vertex.float(0.0, 0.0, 1.0).color(0x0000FF);
  123.  
  124.         return vertex;
  125.     },
  126. };
  127.  
  128. const lines = obj.lines();
  129. const points = obj.points();
  130. console.log(lines);
  131. console.log(points);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement