JSFiddle - React, Tailwind, and code Playground
by chuby0307
HTML
<div id="info">hw7 parts<br/>Basics</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
<script src="https://dl.dropboxusercontent.com/u/3587259/Code/Threejs/OrbitControls.js">
</script>
CSS
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
color: #ffff00
}
body {
overflow: hidden;
}
JavaScript
var camera, scene, renderer, geometry, material, mesh, controls;
var pos, vel;
var clock = new THREE.Clock();
var puck;
var pColor;
init();
animate();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 500;
scene.add(camera);
geometry = new THREE.BoxGeometry(220, 30, 10);
material = new THREE.MeshBasicMaterial({
transparent: true,
color: 0xff2387,
opacity: 0.4
});
mesh = new THREE.Mesh(geometry, material);
mesh.position.set(0, 15, 105);
scene.add(mesh);
mesh2 = mesh.clone();
mesh2.position.set(0, 15, -105);
scene.add(mesh2);
mesh3 = mesh.clone();
mesh3.rotation.y = Math.PI / 2;
mesh3.position.set(105, 15, 0);
scene.add(mesh3);
mesh4 = mesh.clone();
mesh4.rotation.y = Math.PI / 2;
mesh4.position.set(-105, 15, 0);
scene.add(mesh4);
ground = new THREE.Mesh(new THREE.PlaneGeometry(200, 200, 130, 130),
new THREE.MeshLambertMaterial({
color: 0x888888
}));
ground.rotation.x = -Math.PI / 2;
scene.add(ground);
var Puck = function(){
this.pos = new THREE.Vector3();
this.vel = new THREE.Vector3();
this.mesh = new THREE.Mesh(new THREE.CylinderGeometry(5, 5, 2, 20),
new THREE.MeshBasicMaterial());
};
Puck.prototype.update=function(dt){
this.pos.add(this.vel.clone().multiplyScalar(dt));
this.mesh.position.copy(this.pos);
};
Puck.prototype.collision=function(){
if (this.pos.x > 100) {
this.pos.x = 100;
this.vel.set(-this.vel.x, 0, this.vel.z);
pColor.setHSL(Math.random(), Math.random(), Math.random() / 2 + 0.5);
} else if (this.pos.x < -100) {
this.pos.x = -100;
this.vel.set(-this.vel.x, 0, this.vel.z);
pColor.setHSL(Math.random(), Math.random(), Math.random() / 2 + 0.5);
...