Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
280 lines (217 sloc) 8.88 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 = 3;
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 metaCube = new CubicVR.Mesh();
var szx = [3.0, 0.5, 0.5], szy =[0.5, 3.5, 0.5], szz = [0.5, 0.5, 3.5];
var boxParts = [
[szx,[0,-1.5,-1.5]],
[szx,[0,1.5,1.5]],
[szx,[0,-1.5,1.5]],
[szx,[0,1.5,-1.5]],
[szy,[-1.5,0,-1.5]],
[szy,[1.5,0,1.5]],
[szy,[-1.5,0,1.5]],
[szy,[1.5,0,-1.5]],
[szz,[-1.5,-1.5,0]],
[szz,[1.5,1.5,0]],
[szz,[-1.5,1.5,0]],
[szz,[1.5,-1.5,0]]];
var boxCollision = new CubicVR.CollisionMap();
for (var i = 0; i < boxParts.length; i++) {
CubicVR.primitives.box({
mesh: metaCube,
size: boxParts[i][0],
transform: (new CubicVR.Transform()).translate(boxParts[i][1]),
material: material1,
uvmapper: uv_cubic
});
boxCollision.addShape({
type: CubicVR.enums.collision.shape.BOX,
size: boxParts[i][0],
position: boxParts[i][1]
});
}
metaCube.prepare();
result.push({mesh:metaCube,collision:boxCollision});
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:[i*1.7,i*1.7+5,i*1.7],
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]
// }));
// Create a shadowed area light, map resolution 2048
// designed for shadowing larger areas than spotlights can provide
// it represents a directional light with shadows.
scene.bindLight(new CubicVR.Light({
type:CubicVR.enums.light.type.AREA,
intensity:0.9,
mapRes:2048, // 4096 ? 8192 ? ;)
areaCeiling:40,
areaFloor:-40,
areaAxis: [-2,-2], // specified in degrees east/west north/south
distance: 60
}));
CubicVR.setSoftShadows(true);
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],
});
floorObject.shadowCast = false;
// 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);
var pickConstraint = null;
var pickDist = 0;
mvc.setEvents({
mouseMove: function (ctx, mpos, mdelta, keyState) {
if (!ctx.mdown) return;
if (pickConstraint) {
pickConstraint.setPosition(scene.camera.unProject(mpos[0],mpos[1],pickDist));
} else {
ctx.orbitView(mdelta);
}
// ctx.panView(mdelta);
},
mouseWheel: function (ctx, mpos, wdelta, keyState) {
ctx.zoomView(wdelta);
},
mouseDown: function (ctx, mpos, keyState) {
var rayTo = scene.camera.unProject(mpos[0],mpos[1]);
var result = physics.getRayHit(scene.camera.position,rayTo);
if (result && !pickConstraint) {
pickConstraint = new CubicVR.Constraint({
type: CubicVR.enums.physics.constraint.P2P,
rigidBody: result.rigidBody,
positionA: result.localPosition
});
physics.addConstraint(pickConstraint);
pickDist = CubicVR.vec3.length(CubicVR.vec3.subtract(scene.camera.position,result.position));
pickConstraint.setPosition(scene.camera.unProject(mpos[0],mpos[1],pickDist));
}
},
mouseUp: function(ctx, mpos, keyState) {
if (pickConstraint) {
physics.removeConstraint(pickConstraint);
pickConstraint = null;
}
},
keyDown: null,
keyUp: null
});
window.addEventListener("keypress",function(evt) { physics.reset(); },false);
// 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());
scene.render();
});
}
</script>
</head>
<body onLoad="webGLStart();"></body>
</html>
Something went wrong with that request. Please try again.