Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Render bodies as subworlds.

  • Loading branch information...
commit 5a26a35769734a50140a39638ff7e008544b943b 1 parent 9540863
@kpreid authored
View
1  blockset.js
@@ -163,6 +163,7 @@
interest: function () { return true; },
dirtyBlock: rebuild,
relitBlock: noop,
+ bodiesChanged: noop,
dirtyAll: rebuild,
dirtyCircuit: dirtyProperties,
deletedCircuit: dirtyProperties,
View
6 body.js
@@ -17,7 +17,7 @@
var POSITION_EPSILON = 1e-6; // close-but-not-intersecting objects are set to this separation
var VELOCITY_EPSILON = 1e-6; // velocities below this are treated as zero
- function Body(world, aabb) {
+ function Body(world, aabb, skin) {
this.aabb = aabb;
this.pos = new Float64Array(3);
this.vel = new Float64Array(3);
@@ -25,6 +25,7 @@
this.flying = false;
this.noclip = false;
this.isPlayerBody = false;
+ this.skin = skin || null;
// non-persisted properties
this.world = world;
@@ -46,6 +47,8 @@
json.noclip = this.noclip;
if (this.isPlayerBody !== false)
json.isPlayerBody = this.isPlayerBody;
+ if (this.skin !== null)
+ json.skin = subSerialize(this.skin);
return json;
};
@@ -58,6 +61,7 @@
if ("flying" in json) body.flying = !!json.flying;
if ("noclip" in json) body.noclip = !!json.noclip;
if ("isPlayerBody" in json) body.isPlayerBody = !!json.isPlayerBody;
+ if ("skin" in json) body.skin = unserialize(json.skin);
return body;
};
View
12 renderer.js
@@ -431,9 +431,9 @@ This is what you can assume/should do:
}
this.setViewTo2D = setViewTo2D;
function saveView() {
- var savedMVMatrix = mvMatrix; mvMatrix = mat4.create();
- var savedPMatrix = pMatrix; pMatrix = mat4.create();
- var savedView = viewPosition;
+ var savedMVMatrix = mvMatrix; mvMatrix = mat4.create(mvMatrix);
+ var savedPMatrix = pMatrix; pMatrix = mat4.create(pMatrix);
+ var savedView = viewPosition; viewPosition = vec3.create(viewPosition);
return function () {
mvMatrix = savedMVMatrix;
pMatrix = savedPMatrix;
@@ -445,6 +445,12 @@ This is what you can assume/should do:
}
this.saveView = saveView;
+ function modifyModelview(f) {
+ f(mvMatrix);
+ sendViewUniforms();
+ }
+ this.modifyModelview = modifyModelview;
+
function setExposure(val) {
exposure = val;
sendExposure();
View
18 world-gen.js
@@ -1124,9 +1124,25 @@
playerBody.pos[2] = topWorld.wz/2;
topWorld.addBody(playerBody);
+ var skinColorBlockset = new Blockset([new BlockType([1, 1, 1, 1])]);
+
+ var skinBlockset = new Blockset([]);
+ var sbt = new BlockType(null, new World([16,16,16], skinColorBlockset));
+ sbt.world.edit(function (x,y,z) {
+ var cx = x-7.5;
+ //var cy = y-7.5;
+ var cz = z-7.5;
+ return y == Math.floor(Math.abs(cx)) && (cx*cx+cz*cz) < 8*8 ? 1 : 0;
+ });
+ skinBlockset.add(sbt);
+
+ var skin = new World([1, 1, 1], skinBlockset);
+ skin.s(0, 0, 0, 1);
+ skin.step(1); // update lighting
+
var testBodyAABB = new AAB(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
for (var tbi = 0; tbi < 10; tbi++) {
- var body = new Body(topWorld, testBodyAABB);
+ var body = new Body(topWorld, testBodyAABB, skin);
vec3.set([wx/2, wy, wz/2], body.pos);
vec3.set([Math.random(), Math.random(), Math.random()], body.vel);
topWorld.addBody(body);
View
57 world-render.js
@@ -28,6 +28,11 @@
var z = v1[2] - v2[2];
return x*x + y*y + z*z;
}
+
+ var angleStep = Math.PI/2;
+ function discreteRotation(angle) {
+ return Math.round(angle/angleStep) * angleStep;
+ }
var rotationsByCode = CubeRotation.byCode;
@@ -229,6 +234,9 @@
world.listen.cancel(listenerWorld);
blockset.listen.cancel(listenerBlockset);
config.renderDistance.listen.cancel(listenerRenderDistance);
+ subWRs.forEach(function (record) {
+ record.wr.deleteResources();
+ });
world = blockset = chunks = nonemptyChunks = dirtyChunks = addChunks = textureDebugR = null;
}
function isAlive() {
@@ -332,6 +340,7 @@
interest: isAlive,
dirtyBlock: dirtyBlock,
relitBlock: relitBlock,
+ bodiesChanged: scheduleDraw,
dirtyAll: dirtyAll,
dirtyCircuit: dirtyCircuit,
deletedCircuit: deletedCircuit,
@@ -433,6 +442,10 @@
// Schedule rendering more chunks
scheduleDraw();
}
+
+ subWRs.forEach(function (record) {
+ record.wr.updateSomeChunks();
+ });
}
this.updateSomeChunks = updateSomeChunks;
@@ -493,6 +506,34 @@
if (config.debugTextureAllocation.get()) {
textureDebugR.draw();
}
+
+ // Draw subworlds.
+ subWRs.forEach(function (record) {
+ var body = record.body;
+ var bodyWorld = body.skin;
+ var wr = record.wr;
+
+ var restoreView = renderer.saveView();
+ renderer.modifyModelview(function (m) {
+ mat4.translate(m, body.pos);
+
+ // Translate to body AABB center
+ var aabb = body.aabb;
+ mat4.translate(m, [
+ (aabb[1] + aabb[0]) * 0.5,
+ (aabb[3] + aabb[2]) * 0.5,
+ (aabb[5] + aabb[4]) * 0.5
+ ]);
+
+ // Apply rotation
+ mat4.rotateY(m, discreteRotation(body.yaw));
+
+ // Translate to center world about AABB center
+ mat4.translate(m, [bodyWorld.wx * -0.5, bodyWorld.wy * -0.5, bodyWorld.wz * -0.5]);
+ });
+ wr.draw();
+ restoreView();
+ });
}
this.draw = draw;
@@ -782,13 +823,25 @@
return dirtyChunks.size() + addChunks.size();
}
this.chunkRendersToDo = chunkRendersToDo;
-
+
+ // --- bodies in the world ---
+
+ var subWRs = [];
+ world.forEachBody(function (body) {
+ if (!body.skin) return;
+ subWRs.push({
+ body: body,
+ wr: new WorldRenderer(body.skin, function () { return [0,0,0]; }, renderer, optAudio, scheduleDraw, false)
+ });
+ });
+
// --- init ---
-
+
world.listen(listenerWorld);
blockset.listen(listenerBlockset);
config.renderDistance.listen(listenerRenderDistance);
config.debugTextureAllocation.listen(listenerRedraw);
+
Object.freeze(this);
}
View
1  world.js
@@ -547,6 +547,7 @@
}
if (someBodyChanged) {
self.persistence.dirty();
+ notifier.notify("bodiesChanged");
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.