{"size": 4167, "ext": "js", "max_stars_repo_path": "test/js/testPoolPosition_manual2.es.js", "max_stars_repo_name": "bobanmak/three-overlaps", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/js/testPoolPosition_manual2.es.js", "max_issues_repo_name": "bobanmak/three-overlaps", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/js/testPoolPosition_manual2.es.js", "max_forks_repo_name": "bobanmak/three-overlaps", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.7222222222, "max_line_length": 112, "alphanum_fraction": 0.5442764579}
import * as THREE from '../../node_modules/three/build/three.module.js';
import Viewport from "../../node_modules/three-viewport/dist/viewport.es.js";
import WoodBox from "./WoodBox.js";
import Grassground from "./Grassground.es.js";
import { TransformControls  } from'../../node_modules/three/examples/jsm/controls/TransformControls.js';
import { PoolPosition  } from'../../src/three-poolposition.es.js';

(function () {

    // Viewport starts ---
    let VP = new Viewport();

    VP.init();
    VP.start();


    // add a ambient light
    VP.scene.add( new THREE.AmbientLight( 0x020202 ) );

    // add a light in front
    let light	= new THREE.DirectionalLight('white', 2);
    light.position.set(100, 100, 300);
    VP.scene.add( light );
   
    VP.camera.position.copy( new THREE.Vector3( 120, 2000, 2400) );
    VP.camera.rotation.copy( new THREE.Euler( -0.67, -2.8, -2.3 ) ) ;
    // Viewport ends ---

    const config = {
        interval: false,
        intervalTime: 1000,
        visualise: true
    };

    let Helper  = new PoolPosition();
    let constraints = [];
    let p, mesh, lastInserted;
    
    // available space
    let availableSpace = new Grassground({
        width		: 2500,
        height		: 2500,
        repeatX		: 10,
        repeatY		: 10,
        "image" : "big"
    });

    VP.scene.add( availableSpace );

    let box1 = createBox({ 
        size        : [300, 300, 300], 
        position    : [450, 0, 0],
        name: "box1" 
    });

    constraints.push( box1 );
    lastInserted = box1;

    let fillSpace = setInterval( ()=>{

        let newBox = createBox({ 
            size        : [300, 300, 300], 
            name: "newBox"  
        });

        let res = Helper.findFreeSpace( availableSpace, constraints, newBox, { neighbour: lastInserted } );
        // liste : side: [ "left", "right" ] } ); als Liste mit Anweisungen
        
        console.log( res, newBox );
        
        if ( !res.position ) {
            VP.scene.remove( mesh );
            clearInterval( fillSpace );
            return;
        } 
       
        newBox.rotation.y =  res.rotationY ;
        newBox.position.copy( res.position );
        constraints.push( newBox );
        lastInserted = newBox;

        if ( config.visualise ) visualiseVertices( Helper.getVertices( newBox ) );
        
        clearInterval( fillSpace );
        
    }, 1000 );

    

  

    

    
  
    
    function visualiseVertices( list ){ 
        // add cubes on List of Points

        list = [ list[0] ];
        let deletable = VP.scene.getObjectByName( "deletable" );

        if ( typeof deletable === "undefined" ){
            deletable = new THREE.Group();
            deletable.name = "deletable";
            VP.scene.add( deletable );
        } 

        // delete old markers
        for (let i = deletable.children.length - 1; i >= 0; i--) {
            deletable.remove(deletable.children[i]);
        }

        let geometry = new THREE.BoxGeometry( 15, 15, 15 );
        let material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );

        for ( let i= 0; i < list.length ; i++ ){
            let cube = new THREE.Mesh( geometry, material );
            cube.position.copy( list[i] );
            
            deletable.add( cube );
        }
        
    };

    function createBox( param ){
        // createBox with parameter and add to scene
        mesh = new WoodBox( ...param.size );
        
        if ( param.rotationY ) mesh.rotation.y = param.rotationY;  
        if ( param.position ) mesh.position.set( ...param.position );

        p = mesh.geometry.parameters;
        mesh.geometry.applyMatrix4( new THREE.Matrix4().makeTranslation(  p.depth/2, p.width/2,  p.height/2 ) );
        mesh.name = "box";

        VP.scene.add( mesh );
        return mesh;
    };

    function makeInteractive( mesh ){


        let ctr = new TransformControls( VP.camera, VP.renderer.domElement );
       
        ctr.attach( mesh );
        
        ctr.addEventListener( 'dragging-changed', function( e ){
            VP.control.enabled = ! e.value 
        });
    

        VP.scene.add( ctr );

    };
   
})();




