Created
August 12, 2014 17:06
-
-
Save mpmckenna8/e0e3a8f79c711b29c55f to your computer and use it in GitHub Desktop.
three.js child moves and rotates w/ parent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var demo = (function(){ | |
"use strict"; | |
var scene=new THREE.Scene(), | |
light= new THREE.AmbientLight(0xffffff), | |
renderer, | |
camera, | |
renderer = new THREE.WebGLRenderer(), | |
box, | |
ground, | |
controls=null; | |
function initScene(){ | |
renderer.setSize( window.innerWidth, window.innerHeight ); | |
document.getElementById("webgl-container").appendChild(renderer.domElement); | |
scene.add(light); | |
camera = new THREE.PerspectiveCamera( | |
35, | |
window.innerWidth / window.innerHeight, | |
1, | |
1000 | |
); | |
camera.position.set( 0, 0, 100 ); | |
scene.add(camera); | |
box = new THREE.Mesh( | |
new THREE.BoxGeometry( | |
20, | |
20, | |
20), | |
new THREE.MeshBasicMaterial({color: 0xFF0000})); | |
scene.add(box); | |
var childBox = new THREE.Mesh( | |
new THREE.BoxGeometry(29,29,20), | |
new THREE.MeshBasicMaterial({color:0x00FF00}) | |
); | |
childBox.position.x = 30; | |
box.add(childBox); | |
console.log(childBox.position) | |
console.log(box.children); | |
requestAnimationFrame(render); | |
}; | |
var keep = true; | |
function movebox() { | |
if(keep){ | |
if(box.position.x>-55){ | |
box.position.x -= .05; | |
//console.log(box.position.x) | |
} | |
else{ | |
keep = false; | |
} | |
} | |
else{ | |
if(box.position.x<55){ | |
box.position.x += .05; | |
} | |
else{ | |
keep = true; | |
} | |
} | |
}; | |
function render() { | |
renderer.render(scene, camera); | |
box.rotation.x +=.01; | |
box.rotation.y += .04; | |
movebox(); | |
requestAnimationFrame(render); | |
}; | |
window.onload = initScene; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Intro to WebGL with three.js</title> | |
</head> | |
<body> | |
<div id="webgl-container"></div> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/three.js/r68/three.js"></script> | |
<script src="chicube.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment