Skip to content

Instantly share code, notes, and snippets.

@itsmunim
Created November 10, 2016 18:27
Show Gist options
  • Save itsmunim/b7055c6a41a127814e86ad30f53f3384 to your computer and use it in GitHub Desktop.
Save itsmunim/b7055c6a41a127814e86ad30f53f3384 to your computer and use it in GitHub Desktop.
var sceneObj = (function() {
"use strict";
Physijs.scripts.worker = "scripts/physijs_worker.js";
Physijs.scripts.ammo = "ammo.js";
var scene, camera, renderer
var physijsBox, physijsGround, spawnBox;
var render, _boxes = [],
spawnBox, loader,
renderer, scene, ground_material, ground, light, camera, mbspec;
function createBox(posX, posY) {
var box, material;
var handleCollision = function(collided_with, linearVelocity, angularVelocity) {
alert('hi');
}
var box_geometry = new THREE.BoxGeometry(4, 4, 4);
material = Physijs.createMaterial(
new THREE.MeshBasicMaterial({
color: 0xff00ff
}),
.6, // medium friction
.3 // low restitution
);
box = new Physijs.BoxMesh(
box_geometry,
material
);
box.position.set(
Math.random() * 15 - 7.5,
25,
Math.random() * 15 - 7.5
);
box.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
box.addEventListener('collision', handleCollision);
box.position.x = posX;
box.position.y = posY;
var velocity = new THREE.Vector3(0, 0, 0);
box.setLinearVelocity(velocity);
scene.add(box);
return box;
};
function initScene() {
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMapSoft = true;
document.getElementById('webgl-container').appendChild(renderer.domElement);
scene = new Physijs.Scene;
scene.setGravity = new THREE.Vector3(0, -50, 0);
camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
1,
1000
);
camera.position.set(60, 50, 60);
camera.lookAt(scene.position);
scene.add(camera);
// Light
light = new THREE.DirectionalLight(0xFFFFFF);
light.position.set(20, 40, -15);
light.target.position.copy(scene.position);
light.castShadow = true;
light.shadowCameraLeft = -60;
light.shadowCameraTop = -60;
light.shadowCameraRight = 60;
light.shadowCameraBottom = 60;
light.shadowCameraNear = 20;
light.shadowCameraFar = 200;
light.shadowBias = -.0001
light.shadowMapWidth = light.shadowMapHeight = 2048;
light.shadowDarkness = .7;
scene.add(light);
// Loader
loader = new THREE.TextureLoader();
// Ground
ground_material = Physijs.createMaterial(
new THREE.MeshBasicMaterial({
color: 0x008888
}),
.8, // high friction
.3 // low restitution
);
ground = new Physijs.BoxMesh(
new THREE.BoxGeometry(100, 1, 100),
ground_material,
0 // mass
);
ground.receiveShadow = true;
scene.add(ground);
createBox(0, 10);
createBox(0, 10);
render();
};
render = function() {
scene.simulate();
renderer.render(scene, camera);
requestAnimationFrame(render);
};
window.onLoad = initScene();
return scene;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment