locked
help with this javascript cube please RRS feed

  • Question

  • User186897 posted

    Hello All,

    I have been struggling a lot with this

    I have below code for a cube out of cubes...I managed to get this online but I need to arrange these small cubes in this in an order, not random and also same color not different colours...can some one plz help me how do I change this to all same size and cubes in a level and all same color please

    <script>
    
    			var container, stats;
    			var camera, scene, raycaster, renderer;
    
    			var mouse = new THREE.Vector2(), INTERSECTED;
    			var radius = 500, theta = 0;
    			var frustumSize = 1000;
    
    			init();
    			animate();
    
    			function init() {
    
    				container = document.createElement( 'div' );
    				document.body.appendChild( container );
    
    				var info = document.createElement( 'div' );
    				info.style.position = 'absolute';
    				info.style.top = '10px';
    				info.style.width = '100%';
    				info.style.textAlign = 'center';
    				info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - interactive cubes';
    				container.appendChild( info );
    
    				var aspect = window.innerWidth / window.innerHeight;
    				camera = new THREE.OrthographicCamera( frustumSize * aspect / - 2, frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 1, 1000 );
    
    				scene = new THREE.Scene();
    
    				var light = new THREE.DirectionalLight( 0xffffff, 1 );
    				light.position.set( 1, 1, 1 ).normalize();
    				scene.add( light );
    
    				var geometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
    
    				for ( var i = 0; i < 10000; i ++ ) {
    
    					var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
    
    					
    object.position.x = Math.random() * 800 - 400;
    					object.position.y = Math.random() * 800 - 400;
    					object.position.z = Math.random() * 800 - 400;
    
    					object.rotation.x = Math.random() * 2 * Math.PI;
    					object.rotation.y =Math.random() * 2 * Math.PI;
    					object.rotation.z = Math.random() * 2 * Math.PI;
    					
    					scene.add( object );
    
    				}
    
    				raycaster = new THREE.Raycaster();
    
    				renderer = new THREE.WebGLRenderer();
    				renderer.setClearColor( 0xf0f0f0 );
    				renderer.setPixelRatio( window.devicePixelRatio );
    				renderer.setSize( window.innerWidth, window.innerHeight );
    				renderer.sortObjects = false;
    				container.appendChild(renderer.domElement);
    
    				stats = new Stats();
    				container.appendChild( stats.dom );
    
    				document.addEventListener( 'mousemove', onDocumentMouseMove, false );
    
    				//
    
    				window.addEventListener( 'resize', onWindowResize, false );
    
    			}
    
    			function onWindowResize() {
    
    				var aspect = window.innerWidth / window.innerHeight;
    
    				camera.left   = - frustumSize * aspect / 2;
    				camera.right  =   frustumSize * aspect / 2;
    				camera.top    =   frustumSize / 2;
    				camera.bottom = - frustumSize / 2;
    
    				camera.updateProjectionMatrix();
    
    				renderer.setSize( window.innerWidth, window.innerHeight );
    
    			}
    
    			function onDocumentMouseMove( event ) {
    
    				event.preventDefault();
    
    				mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    				mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
    
    			}
    
    			//
    
    			function animate() {
    
    				requestAnimationFrame( animate );
    
    				render();
    				stats.update();
    
    			}
    
    			function render() {
    
    				theta += 0.1;
    
    				camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
    				camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
    				camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
    				camera.lookAt( scene.position );
    
    				camera.updateMatrixWorld();
    
    				// find intersections
    
    				raycaster.setFromCamera( mouse, camera );
    
    				var intersects = raycaster.intersectObjects( scene.children );
    
    				if ( intersects.length > 0 ) {
    
    					if ( INTERSECTED != intersects[ 0 ].object ) {
    
    						if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
    
    						INTERSECTED = intersects[ 0 ].object;
    						INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
    						INTERSECTED.material.emissive.setHex( 0xff0000 );
    
    					}
    
    				} else {
    
    					if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
    
    					INTERSECTED = null;
    
    				}
    
    				renderer.render( scene, camera );
    
    			}
    
    		</script>

    Monday, August 15, 2016 7:39 AM

All replies

  • User-1142886626 posted

    Hi Lexi85,

    var geometry = new THREE.BoxBufferGeometry(20, 20, 20);
    for (var i = 0; i < 10000; i++) {
     
        var object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff }));
     
     
        object.position.x = Math.random() * 800 - 400;
        object.position.y = Math.random() * 800 - 400;
        object.position.z = Math.random() * 800 - 400;
     
        object.rotation.x = Math.random() * 2 * Math.PI;
        object.rotation.y = Math.random() * 2 * Math.PI;
        object.rotation.z = Math.random() * 2 * Math.PI;
     
        scene.add(object);
     
    }
    

    It seems you used the three.js canvas.

    From your code, the BoxBufferGeometryBesides draw a box. In your for()method, the color were set Math.random() * 0xffffff  which a random color. You could refer the API to know how to set color. And the position or rotation you also set the  Math.random() which a random position .You could refer to the API learn more about the “three.js”. http://threejs.org/docs/index.html#Reference/Objects/Bone .

    Best Regards,

    Ailleen

    Tuesday, August 16, 2016 12:48 PM
  • User186897 posted

    Yes its just that I am unable to figure out how to code for few cubes to be random color and few cubes to be transparent...

    Tuesday, August 16, 2016 12:53 PM