Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/108/three.js"></script>
<script src='https://stemkoski.github.io/Three.js/js/Detector.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/OrbitControls.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/THREEx.KeyboardState.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/THREEx.FullScreen.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/THREEx.WindowResize.js'></script>
<script src='https://stemkoski.github.io/Three.js/fonts/helvetiker_bold.typeface.js'></script>
              
            
!

CSS

              
                body{
	padding: 0;
	margin: 0;
	overflow: hidden;
	cursor: move;
}
              
            
!

JS

              
                var scene, camera, renderer, controls;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
	//polygon points array
var polygonPoints = [{"x":-317.8337097167969,"y":283.7328186035156,"z":0},{"x":324.4554748535156,"y":290.35455322265625,"z":0},{"x":364.1678161621094,"y":-173.14523315429688,"z":0},{"x":-36.41810989379883,"y":191.02955627441406,"z":0},{"x":-374.10302734375,"y":-83.75933837890625,"z":0},{"x":-370.8056335449219,"y":273.8002624511719,"z":0},{"x":-370.8056335449219,"y":273.8002624511719,"z":0},{"x":-370.8056335449219,"y":273.8002624511719,"z":0},{"x":-317.8337097167969,"y":283.7328186035156,"z":0}];

var binPaneSize = { x: 50, y: 30 };
var binPaneOffset = {x:5,y:5};
// custom global variables
var cube;

	scene = new THREE.Scene();

	var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;	
	// camera attributes
	var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
	// set up camera
	camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
	// add the camera to the scene
	scene.add(camera);
	
	camera.position.set(0,150,400);
	camera.lookAt(scene.position);	
	camera.position.z = 500;
        camera.up.set(0, 0, 1);
	// create and start the renderer; choose antialias setting.
	if ( Detector.webgl ){
		renderer = new THREE.WebGLRenderer( {antialias:true} );
	}else{
		renderer = new THREE.CanvasRenderer(); 
	}
	
	renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
	document.body.appendChild(renderer.domElement);
	
	// automatically resize renderer
	THREEx.WindowResize(renderer, camera);
	// toggle full-screen on given key press
	THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
	
	controls = new THREE.OrbitControls( camera, renderer.domElement );
	
	var light = new THREE.PointLight(0xffffff);
	light.position.set(0,0,0);
	scene.add(light);
	
 gridGround = new THREE.GridHelper(1000, 30, 0x3f3f3f, 0x3f3f3f);
gridGround.rotation.x = Math.PI / 2;
        scene.add(gridGround);
drawbinsInPolygon();
	
	// fog must be added to scene before first render
	scene.fog = new THREE.FogExp2( 0x9999ff, 0.00025 );

(function animate(){
  requestAnimationFrame(animate);
	render();		
	update();
})();

function update(){
	// update controls
	controls.update();

}
function render(){	
	renderer.render( scene, camera );
}

function drawbinsInPolygon(){
	
		//Polygon region geometry
        var extrudeSettings = { depth: 8, bevelEnabled: true, bevelSegments: 2, steps: 2, bevelSize: 1, bevelThickness: 1 };
        var polygonShape = new THREE.Shape(polygonPoints);
        var polygonGeometry = new THREE.ExtrudeBufferGeometry(polygonShape, extrudeSettings);
        var polygonMesh = new THREE.Mesh(polygonGeometry , new THREE.MeshPhongMaterial({ color: 0xf08000 }));
        scene.add(polygonMesh);

        //bins geometry
        var planeGeometry = new THREE.PlaneGeometry(binPaneSize.x, binPaneSize.y);
        var planematerial = new THREE.MeshBasicMaterial({ color: 0xffff00, side: THREE.DoubleSide });
        var binMesh = new THREE.Mesh(planeGeometry, planematerial);

        var polygonOutBounds = {
            minX: Math.min.apply(Math, polygonPoints.map(function (o) { return o.x; })),
            minY: Math.min.apply(Math, polygonPoints.map(function (o) { return o.y; })),
            maxX: Math.max.apply(Math, polygonPoints.map(function (o) { return o.x; })),
            maxY: Math.max.apply(Math, polygonPoints.map(function (o) { return o.y; }))
        };
       debugger; 
        for (var i = polygonOutBounds.minX; i < polygonOutBounds.maxX; i += binPaneSize.x + binPaneOffset.x) {
            for (var j = polygonOutBounds.minY; j < polygonOutBounds.maxY; j += binPaneSize.y + binPaneOffset.y) {
                console.log(i + " - " + j);
                var _binMesh = binMesh.clone();
                scene.add(_binMesh);
                _binMesh.position.set(i, j, 2);
                if (!DetectCollisionOfObjects(polygonMesh,_binMesh )) {
                    console.log('test');
                    scene.remove(_binMesh);
                }
            }
        }	
}

function DetectCollisionOfObjects (object1, object2) {
            object1.geometry.computeBoundingBox(); //not needed if its already calculated
            object2.geometry.computeBoundingBox();
            object1.updateMatrixWorld();
            object2.updateMatrixWorld();

            var obj1 = object1.geometry.boundingBox.clone();
            obj1.applyMatrix4(object1.matrixWorld);

            var obj2 = object2.geometry.boundingBox.clone();
            obj2.applyMatrix4(object2.matrixWorld);
            
            return obj1.intersectsBox(obj2);
        }
              
            
!
999px

Console