Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

320 lines (247 sloc) 10.334 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 = 40;
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,4+(Math.random())*5,(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 = [10, 10, -10];
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();
physics.setGravity([0,0,0]);
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>
Jump to Line
Something went wrong with that request. Please try again.