Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 363726d5d7
Fetching contributors…

Cannot retrieve contributors at this time

354 lines (291 sloc) 12.813 kB
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<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 = [];
// Create a material for the mesh
var material1 = {
specular: [1,1,1],
shininess: 0.9,
env_amount: 1.0,
textures: {
color: "../images/2576-diffuse.jpg",
normal: "../images/2576-normal.jpg",
bump: "../images/2576-bump.jpg",
envsphere: "../images/fract_reflections.jpg"
}
};
var material2 = {
specular: [1,1,1],
shininess: 0.9,
env_amount: 1.0,
textures: {
color: "../images/2282-diffuse.jpg",
normal: "../images/2282-normal.jpg",
bump: "../images/2282-bump.jpg",
envsphere: "../images/fract_reflections.jpg"
}
};
var uv_cubic = {
projectionMode: "cubic",
scale: [1, 1, 1]
};
// Add a box to mesh, size 1.0, apply material and UV parameters
var boxMesh = new CubicVR.Mesh({
primitive: {
type: "box",
size: 2.0,
material: material1,
uvmapper: uv_cubic
},
compile:true
});
var sphereMesh = new CubicVR.Mesh({
primitive: {
type: "sphere",
radius: 1,
lat: 24,
lon: 24,
material: material2,
uvmapper: uv_cubic
},
compile: true
});
var coneMesh = new CubicVR.Mesh({
primitive: {
type: "cone",
base: 2.0,
height: 2.0,
material: material1,
uvmapper: uv_cubic
},
compile: true
})
var cylinderMesh = new CubicVR.Mesh({
primitive: {
type: "cylinder",
radius: 1.0,
height: 2,
lon: 24,
material: material2,
uvmapper: uv_cubic
},
compile: true
});
var capsuleMesh = new CubicVR.Mesh({
primitives: [
{
type: "sphere",
radius: 1,
lat: 24,
lon: 24,
material: material2,
uvmapper: uv_cubic,
transform: {
position: [0,1,0]
}
},
{
type: "cylinder",
radius: 1.0,
height: 2,
lon: 24,
material: material2,
uvmapper: uv_cubic,
},
{
type: "sphere",
radius: 1,
lat: 24,
lon: 24,
material: material2,
uvmapper: uv_cubic,
transform: {
position: [0,-1,0]
}
}
],
compile: true
});
var sphereCollision = new CubicVR.CollisionMap({
type: "sphere",
radius: 1
});
var coneCollision = new CubicVR.CollisionMap({
type: "cone",
radius: 1,
height: 2
});
var cylinderCollision = new CubicVR.CollisionMap({
type: "cylinder",
size: [2,2,2]
});
var capsuleCollision = new CubicVR.CollisionMap({
type: "capsule",
radius: 1,
height: 2
});
var boxCollision = new CubicVR.CollisionMap({
type: "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: "dynamic",
mass: 1,
collision: src.collision
});
scene.bind(sceneObj);
physics.bind(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({
camera: {
width: canvas.width,
height: canvas.height,
fov: 80,
position: [5, 5, -5],
target: [0, -3, 0]
},
light: {
type: "area",
intensity: 0.9,
mapRes: 2048,
areaCeiling: 40,
areaFloor: -40,
areaAxis: [-2,-2], // specified in degrees east/west north/south
distance: 60
}
});
CubicVR.setSoftShadows(true);
var floorMaterial = new CubicVR.Material();
var floorMesh = new CubicVR.Mesh({
primitive: {
type: "box",
size: 1.0,
material: {
specular:[0,0,0],
shininess: 0.9,
env_amount: 1.0,
textures: {
color: "../images/6583-diffuse.jpg"
}
},
uv: {
projectionMode: "cubic",
scale: [0.05, 0.05, 0.05]
}
},
compile:true
});
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();
// create floor rigid body
var rigidFloor = new CubicVR.RigidBody(floorObject, {
type: "static",
collision: {
type: "box",
size: floorObject.scale
}
});
// bind floor to physics
physics.bind(rigidFloor);
// Add SceneObject containing the mesh to the scene
scene.bind(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) {
physics.stepSimulation(timer.getLastUpdateSeconds());
scene.render();
});
}
</script>
</head>
<body onLoad="webGLStart();">
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.