Skip to content

Instantly share code, notes, and snippets.

@TheAnonymous
Created April 5, 2015 22:45
Show Gist options
  • Save TheAnonymous/1f49de327673a741394a to your computer and use it in GitHub Desktop.
Save TheAnonymous/1f49de327673a741394a to your computer and use it in GitHub Desktop.
<html>
<head>
<title>My first Three.js app</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.min.js"></script>
<script>
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material2 = new THREE.MeshPhongMaterial({ color: 0x00ff0f , opacity: 0.5});
material2.transparent = true;
var cube2 = new THREE.Mesh(geometry, material2);
cube2.translateX(-10);
//scene.add( cube );
//scene.add(cube2);
var objects = new Array(cube2);
// create a point light
var pointLight =
new THREE.PointLight(0xFFFFFF);
// set its position
pointLight.position.x = 10;
pointLight.position.y = 50;
pointLight.position.z = 130;
// add to the scene
scene.add(pointLight);
camera.position.z = 5;
// so that it allow updates
cube2.geometry.dynamic = true;
// changes to the vertices
cube2.geometry.verticesNeedUpdate = true;
for(var i=0;i < 100;i++){
var tmp_cube = cube2.clone();
tmp_cube.translateX(0.9*i);
tmp_cube.material.color.setRGB(0.004*getRandomArbitrary(1,100), 0.004*getRandomArbitrary(1,100),0.004*getRandomArbitrary(1,100)) ;
scene.add(tmp_cube);
objects[objects.length] = tmp_cube;
}
// changes to the normals
cube2.geometry.normalsNeedUpdate = true;
var limit = true;
var render = function () {
requestAnimationFrame( render );
var now = new Date();
var sec = now.getSeconds();
var rand = getRandomArbitrary(0.001,0.01)
cube2.rotation.x += 0.002;
cube2.rotation.y += 0.002;
for(var i=0;i< objects.length;i++){
objects[i].rotation.x += rand - (i/100000.0);
objects[i].rotation.y += rand - (i/100000.0);
objects[i].rotation.z += rand - (i/100000.0);
}
if(limit === false) {
for(var i=0; i < objects.length;i++){
objects[i].scale.y += sec / 10000.0;
objects[i].scale.x += sec / 10000.0;
objects[i].scale.z += sec / 10000.0;
}
cube2.scale.y += sec / 10000.0;
cube2.scale.x += sec / 10000.0;
cube2.scale.z += sec / 10000.0;
}else if (limit === true){
cube2.scale.y -= sec / 10000.0;
cube2.scale.x -= sec / 10000.0;
cube2.scale.z -= sec / 10000.0;
for(var i=0; i < objects.length;i++){
objects[i].scale.y -= sec / 10000.0;
objects[i].scale.x -= sec / 10000.0;
objects[i].scale.z -= sec / 10000.0;
}
}
if(cube2.scale.y < 1.0){
limit = false;
}
if(cube2.scale.y >2.9){
limit = true;
}
renderer.render(scene, camera);
};
render();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment