<commit_before>import GLBoost from '../globals'
import GLContext from '../GLContext'
import Geometry from '../Geometry'
import ClassicMaterial from '../ClassicMaterial'
import Mesh from '../Mesh'
import PhongShader from '../shaders/PhongShader'
import Texture from '../textures/Texture'
import Vector3 from '../math/Vector3'
import Vector2 from '../math/Vector2'
import Vector4 from '../math/Vector4'
import ArrayUtil from '../misc/ArrayUtil'

let singleton = Symbol();
let singletonEnforcer = Symbol();

export default class GLTFLoader {

  constructor(enforcer) {
    if (enforcer !== singletonEnforcer) {
      throw new Error("This is a Singleton class. get the instance using 'getInstance' static method.");
    }
  }

  static getInstance() {
    if (!this[singleton]) {
      this[singleton] = new GLTFLoader(singletonEnforcer);
    }
    return this[singleton];
  }

  loadGLTF(url, canvas, defaultShader = null) {
    return new Promise((resolve, reject)=> {
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.onreadystatechange = ()=> {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
          var gotText = xmlHttp.responseText;
          var partsOfPath = url.split('/');
          var basePath = '';
          for(var i=0; i<partsOfPath.length-1; i++) {
            basePath += partsOfPath[i] + '/';
          }
          console.log(basePath);
          this._constructMesh(gotText, basePath, canvas, defaultShader, resolve);
        }
      };

      xmlHttp.open("GET", url, true);
      xmlHttp.send(null);
    });
  }

  _constructMesh(gotText, basePath, canvas, defaultShader, resolve) {
    var json = JSON.parse(gotText);

    for (let bufferName in json.buffers) {
      //console.log("name: " + bufferName + " data:" + );
      let bufferInfo = json.buffers[bufferName];

      if ( bufferInfo.uri.match(/^data:application\/octet-stream;base64,/) ){
        this._loadBinaryFile(bufferInfo.uri, basePath, json, canvas, defaultShader, resolve);
      } else {
        this._loadBinaryFile(basePath + bufferInfo.uri, basePath, json, canvas, defaultShader, resolve);
      }
    }
  }

  _loadBinaryFile(binaryFilePath, basePath, json, canvas, defaultShader, resolve) {
    var oReq = new XMLHttpRequest();
    oReq.open("GET", binaryFilePath, true);
    oReq.responseType = "arraybuffer";

    var material = new ClassicMaterial(canvas);

    oReq.onload = (oEvent)=> {
      var arrayBuffer = oReq.response; // Note: not oReq.responseText
      if (arrayBuffer) {

        let meshJson = null;
        for (let mesh in json.meshes) {
          meshJson = json.meshes[mesh];
        }
        let primitiveJson = meshJson.primitives[0];
        let gl = GLContext.getInstance(canvas).gl;

        let indicesAccessorStr = primitiveJson.indices;
        var indices = this._accessBinary(indicesAccessorStr, json, arrayBuffer, gl);

        let positionsAccessorStr = primitiveJson.attributes.POSITION;
        let positions = this._accessBinary(positionsAccessorStr, json, arrayBuffer, gl);

        let normalsAccessorStr = primitiveJson.attributes.NORMAL;
        let normals = this._accessBinary(normalsAccessorStr, json, arrayBuffer, gl);

        // Texture
        let texcoords0AccessorStr = primitiveJson.attributes.TEXCOORD_0;
        var texcoords = null;
        var additional = {};

        let materialStr = primitiveJson.material;
        let materialJson = json.materials[materialStr];
        let diffuseValue = materialJson.values.diffuse;
        if (texcoords0AccessorStr) {
          texcoords = this._accessBinary(texcoords0AccessorStr, json, arrayBuffer, gl);
          additional['texcoord'] = texcoords;

          if (typeof diffuseValue === 'string') {
            let textureStr = diffuseValue;
            let textureJson = json.textures[textureStr];
            let imageStr = textureJson.source;
            let imageJson = json.images[imageStr];
            let imageFileStr = imageJson.uri;

            var texture = new Texture(basePath + imageFileStr, canvas);
            texture.name = textureStr;
            material.diffuseTexture = texture;
          }

        }
        if (typeof diffuseValue !== 'string') {
          material.diffuseColor = new Vector4(diffuseValue[0], diffuseValue[1], diffuseValue[2], diffuseValue[3]);
        }
        let ambientValue = materialJson.values.ambient;
        if (typeof ambientValue !== 'string') {
          material.ambientColor = new Vector4(ambientValue[0], ambientValue[1], ambientValue[2], ambientValue[3]);
        }
        let specularValue = materialJson.values.specular;
        if (typeof specularValue !== 'string') {
          material.specularColor = new Vector4(specularValue[0], specularValue[1], specularValue[2], specularValue[3]);
        }

        let opacityValue = 1.0 - materialJson.values.transparency;

        var vertexData = {
          position: positions,
          normal: normals
        };

        var geometry = new Geometry(canvas);
        geometry.setVerticesData(ArrayUtil.merge(vertexData, additional), [indices]);
      }
      var mesh = new Mesh(geometry);
      material.setVertexN(geometry, indices.length);
      if (defaultShader) {
        material.shader = defaultShader;
      } else {
        material.shader = new PhongShader(canvas);
      }
      geometry.materials = [material];
<commit_msg>Add comments.<commit_after>import GLBoost from '../globals'
import GLContext from '../GLContext'
import Geometry from '../Geometry'
import ClassicMaterial from '../ClassicMaterial'
import Mesh from '../Mesh'
import PhongShader from '../shaders/PhongShader'
import Texture from '../textures/Texture'
import Vector3 from '../math/Vector3'
import Vector2 from '../math/Vector2'
import Vector4 from '../math/Vector4'
import ArrayUtil from '../misc/ArrayUtil'

let singleton = Symbol();
let singletonEnforcer = Symbol();

export default class GLTFLoader {

  constructor(enforcer) {
    if (enforcer !== singletonEnforcer) {
      throw new Error("This is a Singleton class. get the instance using 'getInstance' static method.");
    }
  }

  static getInstance() {
    if (!this[singleton]) {
      this[singleton] = new GLTFLoader(singletonEnforcer);
    }
    return this[singleton];
  }

  loadGLTF(url, canvas, defaultShader = null) {
    return new Promise((resolve, reject)=> {
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.onreadystatechange = ()=> {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
          var gotText = xmlHttp.responseText;
          var partsOfPath = url.split('/');
          var basePath = '';
          for(var i=0; i<partsOfPath.length-1; i++) {
            basePath += partsOfPath[i] + '/';
          }
          console.log(basePath);
          this._constructMesh(gotText, basePath, canvas, defaultShader, resolve);
        }
      };

      xmlHttp.open("GET", url, true);
      xmlHttp.send(null);
    });
  }

  _constructMesh(gotText, basePath, canvas, defaultShader, resolve) {
    var json = JSON.parse(gotText);

    for (let bufferName in json.buffers) {
      //console.log("name: " + bufferName + " data:" + );
      let bufferInfo = json.buffers[bufferName];

      if ( bufferInfo.uri.match(/^data:application\/octet-stream;base64,/) ){
        this._loadBinaryFile(bufferInfo.uri, basePath, json, canvas, defaultShader, resolve);
      } else {
        this._loadBinaryFile(basePath + bufferInfo.uri, basePath, json, canvas, defaultShader, resolve);
      }
    }
  }

  _loadBinaryFile(binaryFilePath, basePath, json, canvas, defaultShader, resolve) {
    var oReq = new XMLHttpRequest();
    oReq.open("GET", binaryFilePath, true);
    oReq.responseType = "arraybuffer";

    var material = new ClassicMaterial(canvas);

    oReq.onload = (oEvent)=> {
      var arrayBuffer = oReq.response; // Note: not oReq.responseText
      if (arrayBuffer) {

        let meshJson = null;
        for (let mesh in json.meshes) {
          meshJson = json.meshes[mesh];
        }
        let primitiveJson = meshJson.primitives[0];
        let gl = GLContext.getInstance(canvas).gl;

        // Geometry
        let indicesAccessorStr = primitiveJson.indices;
        var indices = this._accessBinary(indicesAccessorStr, json, arrayBuffer, gl);

        let positionsAccessorStr = primitiveJson.attributes.POSITION;
        let positions = this._accessBinary(positionsAccessorStr, json, arrayBuffer, gl);

        let normalsAccessorStr = primitiveJson.attributes.NORMAL;
        let normals = this._accessBinary(normalsAccessorStr, json, arrayBuffer, gl);

        // Texture
        let texcoords0AccessorStr = primitiveJson.attributes.TEXCOORD_0;
        var texcoords = null;
        var additional = {};

        let materialStr = primitiveJson.material;
        let materialJson = json.materials[materialStr];
        let diffuseValue = materialJson.values.diffuse;
        // Diffuse Texture
        if (texcoords0AccessorStr) {
          texcoords = this._accessBinary(texcoords0AccessorStr, json, arrayBuffer, gl);
          additional['texcoord'] = texcoords;

          if (typeof diffuseValue === 'string') {
            let textureStr = diffuseValue;
            let textureJson = json.textures[textureStr];
            let imageStr = textureJson.source;
            let imageJson = json.images[imageStr];
            let imageFileStr = imageJson.uri;

            var texture = new Texture(basePath + imageFileStr, canvas);
            texture.name = textureStr;
            material.diffuseTexture = texture;
          }
        }
        // Diffuse
        if (typeof diffuseValue !== 'string') {
          material.diffuseColor = new Vector4(diffuseValue[0], diffuseValue[1], diffuseValue[2], diffuseValue[3]);
        }
        // Ambient
        let ambientValue = materialJson.values.ambient;
        if (typeof ambientValue !== 'string') {
          material.ambientColor = new Vector4(ambientValue[0], ambientValue[1], ambientValue[2], ambientValue[3]);
        }
        // Specular
        let specularValue = materialJson.values.specular;
        if (typeof specularValue !== 'string') {
          material.specularColor = new Vector4(specularValue[0], specularValue[1], specularValue[2], specularValue[3]);
        }

        let opacityValue = 1.0 - materialJson.values.transparency;

        var vertexData = {
          position: positions,
          normal: normals
        };

        var geometry = new Geometry(canvas);
        geometry.setVerticesData(ArrayUtil.merge(vertexData, additional), [indices]);

        //
      }
      var mesh = new Mesh(geometry);
      material.setVertexN(geometry, indices.length);
      if (defaultShader) {
        material.shader = defaultShader;
      } else {
        material.shader = new PhongShader(canvas);
      }
      geometry.materials = [material];

{'fraction_non_alphanumeric': 0.11517215982323697, 'fraction_numerical': 0.006628613515006445, 'mean_word_length': 2.713846153846154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7432183', 'n_tokens_mistral': 3092, 'n_tokens_neox': 2996, 'n_words': 717}