
const lightFlicker = ()=>(Math.sin(getSceneTimeFromStart()*11.0)+1)/2;

Demo.prototype.text = function (start, duration, array) {

  const flickeriness = start < 115 && start > 4 ? ()=>(Math.sin(getSceneTimeFromStart()*11.0)+1)/2*0.25 : 0;

  let startY = (0 + array.length/2)*0.2;
  let y = startY;
  for(let i = 0; i < array.length; i++) {
    let obj = array[i];
    let size = obj.size || 5;
    y -= (0.2*size/5);
    let text = obj.text;
    const fadeTime = 0.25;
    this.loader.addAnimation({
      start:start,duration:duration,
        text:{
            string:text,
            name:'font/Joe College Sans NF W00 Rg.ttf',
            parameters: {size:size,depth:0.01,bevelEnabled:false,bevelThickness:0.01,bevelSize:0.01,bevelSegments:1}
        },
        material:{
          //transparent:true,
        },
        perspective:'2d',
        position:{
            x:0+0.005,
            y:y+0.005,
            z:0,
        },
        color:[{r:flickeriness,g:0,b:0}]
      });
    this.loader.addAnimation({
      start:start,duration:duration,
        text:{
            string:text,
            name:'font/Joe College Sans NF W00 Rg.ttf',
            parameters: {size:size,depth:0.01,bevelEnabled:false,bevelThickness:0.01,bevelSize:0.01,bevelSegments:1}
        },
        material:{
          transparent:true,
          side:'Front',
          //opacity:0.2,
        },
        perspective:'2d',
        position:{
            x:0,
            y:y,
            z:0,
        },
        //color:[{r:1,g:0,b:1,a:0},{a:1,duration:fadeTime},{duration:duration-fadeTime*2},{duration:fadeTime,a:0}]
      });
  }
};

Demo.prototype.sceneBloom = function () {
  //this.setScene('abstract');
  this.setScene('bloom');

  const bpm = new Settings().demo.sync.beatsPerMinute;
  const beat = 60/bpm;
  //return;

//return;
  this.loader.addAnimation({
    "light": {
        "type": "Ambient",
        "properties": { "intensity": 0.2 },
    }
  });

  this.loader.addAnimation({
    "light": {
        "type": "Directional",
        "properties": { "intensity": 1.0 },
        "castShadow": true
    }
    ,position:[{x:-3,y:5,z:3}]
  });


  /*  this.loader.addAnimation({
    start: 0,
    image: '_embedded/defaultWhite.png',
    //visible:()=>(getSceneTimeFromStart()%beat)%1.0 < 0.25 ? false : true,
    color:{
        r:()=>1,g:0,b:1,a:1.0
    },
    perspective:'3d',
    billboard:true,
    position:[{x:0,y:0,z:-20}],
    scale:[{uniform3d:25}],
  });
*/


const blackness=0.6;
    this.loader.addAnimation({
      image: {name:"naked_farjan.mp4", video:{speed:1.0} },
      //visible:false,
      //image: {name:"_embedded/defaultWhite.png"},
      //perspective:'3d',
      position:[{x:0,y:0,z:-5}],
      scale:[{uniform2d:1.0}],
      //color:[{a:1.0,r:0.2,g:0.2,b:0.2}]
      color:[{a:0.0,r:blackness,g:blackness,b:blackness},{"duration":1,a:1}]
      //,scale:[{x:4.0,y:4.0}], 
    });

    this.loader.addAnimation({
      //image: {name:"naked_farjan.mp4", video:{speed:1.0} },
      //visible:false,
      object:null,
    shape: {
      type: 'PLANE',
      width: 16,
      height: 9,
    },
      //image: {name:"_embedded/defaultWhite.png"},
      //perspective:'3d',
      position:[{x:0,y:0,z:-4.5}],
      scale:[{uniform2d:2.1}],
      color:[{a:0.2,r:blackness,g:blackness,b:blackness}]
      //,scale:[{x:4.0,y:4.0}], 
    });

        const objStart = 3.2;
  const lightZ = 1.0;

  this.loader.addAnimation({
    start:objStart,
    end:115.1,
    visible:true,
    id:'policeship',
    object: null,
    position:[{x:0,y:-1.0,z:0.0},{duration:1,y:0},{duration:106.5},{duration:3,y:-1.5}],
  });


  this.loader.addAnimation({
    start:objStart,
    visible:true,
    id:'ship',
    parent:'policeship',
            cubeMap: {name:'cube1'},
    material:{
      envMap: 'cube1.cube.map',
    },

    object: 'cargo-ship.obj',
    angle:[{degreesY:90}],
    position:[{x:0,y:-0.7,z:7.0}],
  });

  this.loader.addAnimation({
    start:objStart,
    object: 'siren-case.obj',
    id:'siren',
    parent:'policeship',
    material:{
      envMap: 'cube1.cube.map',
  metalness: 0,
  roughness: 1,

    },
    angle:[{degreesX:()=>-90}],
    color:[{r:1,g:1,b:1}],
    position:[{x:0,y:-0.2,z:lightZ}],
    scale:[{uniform3d:5}],
  });
  this.loader.addAnimation({
    start:objStart,
    object: 'siren-light.obj',
    parent:'siren',
    id:'sirenlight',
    material:{
      envMap: 'cube1.cube.map',
    },
    angle:[{degreesY:()=>getSceneTimeFromStart()*800,degreesX:90}],
    color:[{r:()=>1.0,g:lightFlicker,b:lightFlicker}],
    position:[{x:0,y:0.0,z:0}],
    scale:[{uniform3d:3.1}]
  });


  const spotIntensity = 4;
  const spotAngle = Math.PI/14;
 this.loader.addAnimation({
  parent:'sirenlight',
  light: {
      type: 'Spot',
      properties: {
        intensity: spotIntensity,
        distance:0,
        decay:0,
        angle:spotAngle
      },
      castShadow: false,
  },
  color:[{r:lightFlicker,g:0,b:0}],
  //angle:[{degreesY:90}],
  position: [{ x: 0.0, y: -0.0, z: 1.0 }]
});
 this.loader.addAnimation({
  parent:'sirenlight',
  light: {
      type: 'Spot',
      properties: {
        intensity: spotIntensity,
        distance:0,
        decay:0,
        angle:spotAngle
      },
      castShadow: false,
  },
  color:[{r:lightFlicker,g:0,b:0}],
  //angle:[{degreesY:90}],
  position: [{ x: 0.0, y: -0.0, z: -1.0 }]
});

  //FIXME: DO NOT CAST SHADOW
  this.loader.addAnimation({
    //object: 'siren-case.obj',
    start:objStart,
    visible:true,
    object: null,
    //parent:'siren',
    parent:'policeship',
    shape: {
      type: 'SPHERE',
    },
    material:{
      type:'Physical',
      envMap: 'cube1.cube.map',
            transparent:true,
            castShadow:false,
      opacity:0.2,
  metalness: 0,
  roughness: 0,

      /*type:'Physical',
  transmission: 1,
  opacity: 1,
  ior: 1.52,
  thickness: 0.1,
  specularIntensity: 1,
  specularColor: 0xffffff,
  lightIntensity: 1,
  exposure: 1,
*/
    },
    //angle:[{degreesX:90}],
    //color:[{a:0.8}],
    position:[{x:0,y:-0.2,z:lightZ}],
    scale:[{x:0.6,z:0.6,y:0.8}],
    /*shader: {
      fragmentShaderSuffix:`gl_FragColor.a = 0.4;`
    },*/
  });

//return;
  const defaultSize = 3.5;
    this.text(0,3.25,[
    {text:"JUMALAUTA"},
    {text:"MOTION PICTURES COMMITTEE",size:defaultSize},
    {text:"PRESENTS", size:2},
  ]);

  this.text(5,3,[
    {text:"HALUTTU MAKSULLINEN",size:defaultSize},
  ]);
  this.text(11.2,3,[
    {text:"ASTU",size:defaultSize},
  ]);
  this.text(15,3,[
    {text:"YLVAES",size:defaultSize},
  ]);

    this.text(50.0,5,[
    {text:"guest starring",size:2},
    {text:"MAGNETISMIN ALASTON ASEKERHO",size:2.5},
    {text:"Proxima Centauri",size:2.5},
  ]);

  this.text(60.1,5,[
    {text:"music by",size:3},
    {text:"IRA NEWBORN",size:defaultSize},
    {text:"YLVAES",size:defaultSize},
  ]);


  this.text(81.0,3.5,[
    {text:"choreographers",size:2},
    {text:"naetti tyttoe",size:2.5},
    {text:"meritaehti",size:2.5},
    {text:"violetti pesukarhu",size:2.5},
  ]);


  this.text(86.7,4.5,[
    {text:"Costume design by",size:2},
  {text:"maitotuote    anteeksi",size:2},
  {text:"soluttautuja    laki ja oikeus",size:2},
  {text:"mies joka juo    tekohengitysta    mn-l",size:2},
  ]);

  this.text(100,4.5,[
    {text:"also starring",size:2},
  {text:"pelimanni",size:2},
  {text:"oispa artesaanikaljaa",size:2},
  {text:"johanna seena",size:2},
  ]);


  this.text(115.1,10,[
    {text:"JUMALAUTA 24.5 3/2 YEARS",size:defaultSize},
    {text:"4.-6.9.2026",size:defaultSize},
    {text:"Aulanko, FI",size:defaultSize},
  ]);

    this.loader.addAnimation({
      object:null,
    shape: {
      type: 'SPHERE',
    },
      position:[{x:3,y:3,z:4.5}],
      scale:[{uniform3d:5}],
      color:[{a:1.0,r:1,g:1,b:0}]
    });

    const maxWaves = 4;
    for(let i = 0; i < maxWaves; i++) {
      const randomAngle = Utils.random()*10-5;
      const randX = Utils.random()*5-2.5;
    this.loader.addAnimation({
      end:115.1,start:110,
      object:'tex_waves.png',
    shape: {
      type: 'PLANE',
      width: 32,
      height: 4,
    },
      position:[{x:()=>Math.sin(getSceneTimeFromStart())*1+randX,y:-8,z:-4.5+i*0.05},{duration:3,y:-4.0-i*0.1}],
      scale:[{uniform2d:1.0}],
      color:[{a:1.0,r:0.2,g:0.2,b:0.2+i/maxWaves*0.8}],
      angle:[{degreesZ:()=>Math.sin(getSceneTimeFromStart())*5+randomAngle}]
    });
    }


  };
