
var Import_Obj;

function ray(ms, pos, tar) {
    var r = new BABYLON.Ray(pos, { x: tar.x, y: tar.y, z: tar.z }, 1000.);
    var meshFound = scene.pickWithRay(r);

    if (meshFound != null && meshFound.pickedPoint != null) {
        return { o: meshFound.pickedMesh, p: meshFound.pickedPoint };
    }
    return null;
}

var ground, camera, defMat;
var createScene = function () {

    // This creates a basic Babylon Scene object (non-mesh)
    var scene = new BABYLON.Scene(engine);
    // This creates and positions a free camera (non-mesh)
    camera = new BABYLON.ArcRotateCamera("camera1", 0, 2, 3,
        new BABYLON.Vector3(0, 0, 10), scene);
    camera.setTarget(BABYLON.Vector3.Zero());
    camera.wheelPrecision = 0.3  
    camera.beta = 0.5;


    
    scene.clearColor = new BABYLON.Color4(1, 1, 1, 1.);

    // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
    var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
    light.intensity = 0.7;
   

    BABYLONX.GeometryBuilder.InitializeEngine();
    BABYLONX.ShaderBuilder.InitializeEngine();
    var smap0 = '1.0';
    var smap1 = '1.0';

    function Light(me){

        me = Init(me);
        me = Atm(me, 'nrm', 'vec3(1.)', 'vec3(420.)', '1.', '0.0525', smap0, smap1);
        me = Atm(me, 'nrm', 'vec3(1.,1.0,0.8)', 'vec3(0.,-420.,-0.)', '1.', '0.0525', smap0, smap1);
        me = Atm(me, 'nrm', 'vec3(0.7)', 'vec3(-400.,10.,-400.)', '1.', '0.04495', smap0, smap1);

        return me;
    }


    function Init(me) {
        me.Setting.FragmentWorld = true;

        var s = 'vec3 wpos = (world*vec4(pos,1.)).xyz;float atm , phg,specComp=0. ;vec3 spc = vec3(0.);vec3 deff = vec3(0.);';
        me = me.InLine(s);

        return me;
    }

    function Atm(me, nrm, co, pt, at, sc, rg, mt) {

        me = me.InLine(`
                
             atm = ((max(0.,log(1000000./length( (`+ pt + `-wpos)/( ` + at + ` ) ))/125.- ` + sc + `   ))
                *40.);
                 phg = dot(mat3(world)*nrm,normalize(`+ pt + `-wpos)) ;
                if(length(result.xyz)== 0.) result.xyz =  (atm*phg)*`+ rg + `*` + co + `;
                else result.xyz =  ( (atm*phg)*`+ rg + `*` + co + ` + result.xyz);

             
               specComp = max(0., dot(mat3(world)*nrm,  normalize(normalize(camera - wpos) +  
               normalize(`+ pt + ` - wpos))  ));
               
                 spc += min(1.,max(0.,pow(specComp, max(1.,  64./`+ mt + `)) * (` + mt + `*1.) ))*` + co + `;
                result.xyz = vec3(spc+result.xyz );   
              
            `);

        return me;
    }

     defMat = new BABYLONX.ShaderBuilder()
         .Solid({r:0.5,g:0.5,b:0.5})
        .Reference(1)
        .Solid()
        .Func(Light)
        .InLine('result.xyz = result_1.xyz*0.3+result.xyz*result.xyz*0.7+spc*1.0; ').BuildMaterial(scene); 
     
    camera.wheelPrecision = 1.;

    camera.alpha = .5;
    camera.beta = .75;
    camera.radius = 500; 
    camera.panningInertia = 0.5;
    var time = 0;
    scene.registerBeforeRender(function () { 

        camera.panningSensibility = 2500. / (camera.radius);
        camera.wheelPrecision = 500. / (camera.radius);

        new BABYLONX.ShaderMaterialHelper().SetUniforms(
            scene.meshes,
            camera.position,
            camera.target,
            { x: 0, y: 0 },
            { x: 100, y: 100 },
            time++);

    }); 

    setTimeout(function () {
        camera.attachControl(canvas, true);
        startingPoint = null;
    }, 300); 

    return scene;
};