Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
258 lines (197 sloc) 7.68 KB
<!DOCTYPE html>
<html>
<head>
<title>
CubicVR.js: AMMO physics adapter test
</title>
<script src="../../lib/ammo.js" type="text/javascript"></script>
<script src="../../CubicVR.js" type="text/javascript"></script>
<script type='text/javascript'>
var spawnObjs = 20;
function generateObjects() {
var result = [];
var envTex = new CubicVR.Texture("../images/fract_reflections.jpg");
// Create a material for the mesh
var material1 = new CubicVR.Material({
specular:[1,1,1],
shininess: 0.9,
env_amount: 1.0,
textures: {
color: new CubicVR.Texture("../images/2576-diffuse.jpg"),
normal: new CubicVR.Texture("../images/2576-normal.jpg"),
bump: new CubicVR.Texture("../images/2576-bump.jpg"),
envsphere: envTex
}
});
var material2 = new CubicVR.Material({
specular:[1,1,1],
shininess: 0.9,
env_amount: 1.0,
textures: {
color: new CubicVR.Texture("../images/2282-diffuse.jpg"),
normal: new CubicVR.Texture("../images/2282-normal.jpg"),
bump: new CubicVR.Texture("../images/2282-bump.jpg"),
envsphere: envTex
}
});
var uv_cubic = {
projectionMode: CubicVR.enums.uv.projection.CUBIC,
scale: [1, 1, 1]
};
// Add a box to mesh, size 1.0, apply material and UV parameters
var boxMesh = CubicVR.primitives.box({
size: 2.0,
material: material1,
uvmapper: uv_cubic
}).prepare();
var sphereMesh = CubicVR.primitives.sphere({
radius: 1,
lat: 24,
lon: 24,
material: material2,
uvmapper: uv_cubic
});
var coneMesh = CubicVR.primitives.cone({
base: 2.0,
height: 2.0,
material: material1,
uvmapper: uv_cubic
}).prepare();
var cylinderMesh = CubicVR.primitives.cylinder({
radius: 1.0,
height: 2,
lon: 24,
material: material2,
uvmapper: uv_cubic
});
var capsuleMesh = new CubicVR.Mesh();
capsuleMesh.booleanAdd(cylinderMesh);
capsuleMesh.booleanAdd(sphereMesh,(new CubicVR.Transform()).translate([0,1,0]));
capsuleMesh.booleanAdd(sphereMesh,(new CubicVR.Transform()).translate([0,-1,0]));
var uvm = new CubicVR.UVMapper(uv_cubic);
uvm.apply(capsuleMesh);
capsuleMesh.prepare();
cylinderMesh.prepare();
sphereMesh.prepare();
var sphereCollision = new CubicVR.CollisionMap({
type: CubicVR.enums.collision.shape.SPHERE,
radius: 1
});
var coneCollision = new CubicVR.CollisionMap({
type: CubicVR.enums.collision.shape.CONE,
radius: 1,
height: 2
});
var cylinderCollision = new CubicVR.CollisionMap({
type: CubicVR.enums.collision.shape.CYLINDER,
size: [2,2,2]
});
var capsuleCollision = new CubicVR.CollisionMap({
type: CubicVR.enums.collision.shape.CAPSULE,
radius: 1,
height: 2
});
var boxCollision = new CubicVR.CollisionMap({
type: CubicVR.enums.collision.shape.BOX,
size: [2,2,2]
});
result.push({mesh:boxMesh,collision:boxCollision});
result.push({mesh:sphereMesh,collision:sphereCollision});
result.push({mesh:coneMesh,collision:coneCollision});
result.push({mesh:cylinderMesh,collision:cylinderCollision});
result.push({mesh:capsuleMesh,collision:capsuleCollision});
return result;
}
function spawnObjects(scene,physics,objlist) {
var nobjs = objlist.length;
var b = parseInt(window.location.search.substr(1),10);
if (b && !(b!=b)) spawnObjs = b;
for (var i = 0; i < spawnObjs; i++) {
var src = objlist[i%nobjs];
var sceneObj = new CubicVR.SceneObject({
mesh:src.mesh,
position:[(Math.random()-0.5)*2.0*10,10+(Math.random())*10,(Math.random()-0.5)*2.0*10],
rotation:[Math.random()*360,Math.random()*360,Math.random()*360]
});
var rigidObj = new CubicVR.RigidBody(sceneObj, {
type: CubicVR.enums.physics.body.DYNAMIC,
mass: 1,
collision: src.collision
});
scene.bindSceneObject(sceneObj);
physics.bindRigidBody(rigidObj);
}
}
function webGLStart() {
// by default generate a full screen canvas with automatic resize
var gl = CubicVR.init();
var canvas = CubicVR.getCanvas();
if (!gl) {
alert("Sorry, no WebGL support.");
return;
};
// New scene with our canvas dimensions and default camera with FOV 80
var scene = new CubicVR.Scene(canvas.width, canvas.height, 80);
// set initial camera position and target
scene.camera.position = [5, 5, -5];
scene.camera.target = [0, -3, 0];
// Add a simple directional light
scene.bindLight(new CubicVR.Light({
type: CubicVR.enums.light.type.DIRECTIONAL,
specular: [1, 1, 1],
direction: [0.5, -1, 0.5]
}));
var floorMaterial = new CubicVR.Material({
specular:[0,0,0],
shininess: 0.9,
env_amount: 1.0,
textures: {
color: new CubicVR.Texture("../images/6583-diffuse.jpg"),
}
});
var floorMesh = CubicVR.primitives.box({
size: 1.0,
material: floorMaterial,
uvmapper: {
projectionMode: CubicVR.enums.uv.projection.CUBIC,
scale: [0.05, 0.05, 0.05]
}
}).prepare();
var floorObject = new CubicVR.SceneObject({
mesh: floorMesh,
scale: [100, 0.2, 100],
position: [0, -5, 0],
});
// init physics manager
var physics = new CubicVR.ScenePhysics();
var rigidFloor = new CubicVR.RigidBody(floorObject, {
type: CubicVR.enums.physics.body.STATIC,
collision: {
type: CubicVR.enums.collision.shape.BOX,
size: floorObject.scale
}
});
physics.bindRigidBody(rigidFloor);
// Add SceneObject containing the mesh to the scene
scene.bindSceneObject(floorObject);
// initialize a mouse view controller
mvc = new CubicVR.MouseViewController(canvas, scene.camera);
// Add our scene to the window resize list
CubicVR.addResizeable(scene);
var objlist = generateObjects();
spawnObjects(scene,physics,objlist);
// Start our main drawing loop, it provides a timer and the gl context as parameters
CubicVR.MainLoop(function(timer, gl) {
var seconds = timer.getSeconds();
physics.stepSimulation(timer.getLastUpdateSeconds());
if (seconds > 15) {
physics.reset();
timer.setSeconds(0);
}
scene.render();
});
}
</script>
</head>
<body onLoad="webGLStart();"></body>
</html>