Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions metaverse_modules/land/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
// import * as THREE from 'three';
import * as THREE from 'three';
import metaversefile from 'metaversefile';
const {useApp, useCleanup} = metaversefile;
const {useApp, useLocalPlayer, useCamera, useProcGenManager, useFrame, useCleanup} = metaversefile;

const localVector = new THREE.Vector3();
const localVector2 = new THREE.Vector3();
const localVector3 = new THREE.Vector3();
const localQuaternion = new THREE.Quaternion();
const localMatrix = new THREE.Matrix4();
const localMatrix2 = new THREE.Matrix4();

export default e => {
const app = useApp();
const localPlayer = useLocalPlayer();
const camera = useCamera();
const procGenManager = useProcGenManager();

app.name = 'land';

Expand Down Expand Up @@ -39,7 +49,7 @@ export default e => {
];
const passComponents = [];
const seed = app.getComponent('seed');
const clipRange = app.getComponent('clipRange');
let clipRange = app.getComponent('clipRange');
const physicsInstance = app.getComponent('physicsInstance');
const wait = app.getComponent('wait');
const debug = app.getComponent('debug');
Expand Down Expand Up @@ -96,6 +106,15 @@ export default e => {
});
}

if (clipRange) {
clipRange = new THREE.Box3(
new THREE.Vector3().fromArray(clipRange[0]),
new THREE.Vector3().fromArray(clipRange[1]),
);
}

const procGenInstance = procGenManager.getInstance(seed, clipRange);

app.addEventListener('componentsupdate', e => {
const {keys} = e;
const components = {};
Expand Down Expand Up @@ -154,6 +173,31 @@ export default e => {
return null;
}

if (!renderPosition) {
useFrame(() => {
const appMatrixWorldInverse = localMatrix2.copy(app.matrixWorld).invert();
localMatrix
.copy(localPlayer.matrixWorld)
.premultiply(appMatrixWorldInverse)
.decompose(localVector, localQuaternion, localVector2);
const playerPosition = localVector;

localMatrix
.copy(camera.matrixWorld)
.premultiply(appMatrixWorldInverse)
.decompose(localVector2, localQuaternion, localVector3);
const cameraPosition = localVector2;
const cameraQuaternion = localQuaternion;

procGenInstance.dcWorkerManager.setCamera(
playerPosition,
cameraPosition,
cameraQuaternion,
camera.projectionMatrix
);
});
}

useCleanup(() => {
for (const subApp of subApps) {
subApp.destroy();
Expand Down
25 changes: 25 additions & 0 deletions webaverse-render-pass.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ class WebaverseRenderPass extends Pass {
this.internalRenderPass = null;
this.onBeforeRender = null;
this.onAfterRender = null;

this.foamInvisibleList = [];
this.foamDepthMaterial = null;
this.foamRenderTarget = null;
this.scene = null;
this.camera = null;
}
renderFoam(renderer){
if(this.foamDepthMaterial && this.foamRenderTarget){
renderer.setRenderTarget(this.foamRenderTarget);
renderer.clear();
for(const invisibleObject of this.foamInvisibleList){
invisibleObject.visible = false;
}
this.scene.overrideMaterial = this.foamDepthMaterial;

renderer.render(this.scene, this.camera);
renderer.setRenderTarget(null);

this.scene.overrideMaterial = null;
for(const invisibleObject of this.foamInvisibleList){
invisibleObject.visible = true;
}
}
}
setSize(width, height) {
if (this.internalDepthPass) {
Expand All @@ -30,6 +54,7 @@ class WebaverseRenderPass extends Pass {

}
render(renderer, renderTarget, readBuffer, deltaTime, maskActive) {
this.renderFoam(renderer);
this.onBeforeRender && this.onBeforeRender();

// render
Expand Down