/**
 *@author liuhaixiao
 *@createTime 2018-11-09 11:40
 *@description 烟雾效果
 */
var cam = void 0,scene = void 0,renderer = void 0,
  clock = void 0,smokeMaterial = void 0,
  h = void 0,w = void 0,
  smokeParticles = [];

var THREE = require('../three.min');

var skip = 0;

export default class Smoke {

  constructor(select) {
    this.oC = select;
    this.widthCanvas = this.oC.width;
    this.heightCanvas = this.oC.height;
    this.oGc = select.getContext("webgl");
  }


  init() {
    // h = window.innerHeight;
    // w = window.innerWidth;
    h = this.heightCanvas;
    w = this.widthCanvas;
    this.lights(); //馃挕
    this.camera(); // 馃帴
    this.action(); // 馃幀
    // window.addEventListener('resize', this.resize);
  }

  animate() {
    var delta = clock.getDelta();

    requestAnimationFrame(this.animate.bind(this));
    if(skip !== 0) {
      skip = ++skip % 2;
      return;
    } else {
      skip = ++skip % 2;
    }
    // console.log("i", skip);
    [].forEach.call(smokeParticles, function (sp) {
      sp.rotation.z += delta * 0.2;
    });

    renderer.render(scene, cam);
  }

  resize() {
    // renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setSize(this.widthCanvas, this.heightCanvas);
  }

  lights() {
    var light = new THREE.DirectionalLight(0xffffff, 0.5);

    clock = new THREE.Clock();
    renderer = new THREE.WebGLRenderer({antialias: false,canvas:this.oC, context: this.oGc, preserveDrawingBuffer: true});

    renderer.setClearColor(0x00547f, 1);
    renderer.setSize(w, h);

    scene = new THREE.Scene();

    light.position.set(-1, 0, 1);
    scene.add(light);
  }

  camera() {
    cam = new THREE.PerspectiveCamera(
      75,
      w / h,
      1,
      10000);


    cam.position.z = 1000;

    scene.add(cam);
  }


  action() {
    var loader = new THREE.TextureLoader();
    var that = this
    loader.crossOrigin = '';

    loader.load(
      'https://dm.static.elab-plus.com/static/img/blue-smoke.png',
      function onLoad(texture) {
        var smokeGeo = new THREE.PlaneBufferGeometry(300, 300);

        smokeMaterial = new THREE.MeshLambertMaterial({
          map: texture,
          transparent: true
        });


        for (var p = 0, l = 100; p < l; p++) {
          var particle = new THREE.Mesh(smokeGeo, smokeMaterial);

          particle.position.set(
            Math.random() * 500 - 250,
            Math.random() * 500 - 250,
            Math.random() * 1000 - 100);


          particle.rotation.z = Math.random() * 360;
          scene.add(particle);
          smokeParticles.push(particle);
        }

        that.animate();
      });


  }

  process() {
    this.init();
  }
}


