Skip to content

Commit

Permalink
perf: fixed initMaterial rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
madblade committed Jun 20, 2020
1 parent bb57c99 commit 7ec5b19
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 142 deletions.
14 changes: 7 additions & 7 deletions client/app/engine/controls/mouse/listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,17 @@ let ListenerModule = {
let clientSelfModel = this.app.model.client.selfComponent;
let activeItemID = clientSelfModel.getCurrentItemID();

if ((ItemsModelModule.isItemNaught(activeItemID) ||
ItemsModelModule.isItemBlock(activeItemID)) &&
!isButtonUp)
if (!isButtonUp && ItemsModelModule.isItemX(activeItemID))
this.requestAddBlock(false);
else if (!isButtonUp &&
(ItemsModelModule.isItemNaught(activeItemID) ||
ItemsModelModule.isItemBlock(activeItemID))
)
{
this.requestDelBlock();
return;
}

if (!ItemsModelModule.isItemIDSupported(activeItemID))
else if (!ItemsModelModule.isItemIDSupported(activeItemID))
console.warn('[Mouse/Listener] Item ID unsupported.');
else if (ItemsModelModule.isItemUseable(activeItemID))
this.requestItemUse(isButtonUp, false);
Expand All @@ -247,8 +249,6 @@ let ListenerModule = {
this.requestAddBlock(true);
else if (ItemsModelModule.isItemUseable(activeItemID))
this.requestItemUse(isButtonUp, true);
else if (ItemsModelModule.isItemX2(activeItemID) && !isButtonUp)
this.requestAddBlock(false);
},

onLeftMouseUp()
Expand Down
5 changes: 5 additions & 0 deletions client/app/engine/graphics/graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ let Graphics = function(app)
this._nbMeshesLoadedOrError = 0;
this._debugChunkBoundingBoxes = false;

// Materials
this.instancedMaterials = new Map(); // Chunks
this.waterMaterials = new Map(); // Waters
this.waterRTTResolution = 512;

// Animations
this.mixers = null;

Expand Down
74 changes: 51 additions & 23 deletions client/app/engine/graphics/materials.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import {

let MaterialsModule = {

createMaterial(whatMaterial, meta)
createMaterial(whatMaterial, meta, worldId)
{
let material;

switch (whatMaterial) {
switch (whatMaterial)
{
case 'flat-phong':
material = new MeshPhongMaterial({
specular: 0xffffff,
Expand All @@ -28,27 +29,54 @@ let MaterialsModule = {
break;

case 'textured-phong':
// material = new MeshPhongMaterial({
//shading: FlatShading,
// color: 0x110011,
// emissive: new Color(0, 0, 0),
// specular: new Color(0, 0, 0),
// shininess: 0,
// side: FrontSide,
//vertexColors: VertexColors,
// map: this.texture
// });
let params = {
//shading: FlatShading,
// color: 0x110011,
side: FrontSide,
//vertexColors: VertexColors,
map: this.textureAtlas,
transparent: false
};
// if (this.rendererManager.shadowVolumes)
// params.transparent = true;
material = new MeshLambertMaterial(params);
if (worldId === undefined) worldId = '-1';
let im = this.instancedMaterials.get(worldId);
if (!im)
{
let params = {
side: FrontSide,
map: this.textureAtlas,
transparent: false
};
// if (this.rendererManager.shadowVolumes)
// params.transparent = true;

material = new MeshLambertMaterial(params);
let materials = [material]; // 0 -> material for main cam
// if (worldId === -1) materials.push(material.clone()); // 1 -> material for secondary cam
this.instancedMaterials.set(worldId, materials);
}
else
{
material = im[0];
if (!material)
{
console.error(`[Materials] Could not get instanced material for ${worldId}.`);
}
}
break;

case 'textured-phong-water':
if (worldId === undefined) worldId = '-1';
let wm = this.waterMaterials.get(worldId);
if (!wm)
{
let params = {
side: FrontSide,
map: this.textureAtlas,
transparent: true
};
material = new MeshLambertMaterial(params);
this.waterMaterials.set(worldId, material);
}
else
{
material = wm;
if (!material)
{
console.error(`[Materials] Could not get instanced material for ${worldId}.`);
}
}
break;

case 'basic-black':
Expand Down
6 changes: 2 additions & 4 deletions client/app/engine/graphics/render/cameras.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ extend(CameraManager.prototype, {

if (this.subCameras.has(cameraId))
{
//console.log('Camera ' + cameraId + ' cannot be added a second time.');
console.log('[CameraManager] Skipping camera addition.');
//let err = new Error();
//console.log(err.stack);
// console.log('[CameraManager] Skipping camera addition.');
// This happens quite often.
return;
}

Expand Down
2 changes: 1 addition & 1 deletion client/app/engine/graphics/render/renderer.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let RendererFactory = {
let u = 'resolution';
fxaa.uniforms[u].value.set(resolutionX, resolutionY);

let copy = new ShaderPass(CopyShader);
// let copy = new ShaderPass(CopyShader);

let composer = new EffectComposer(rendrr);
composer.renderTarget1.stencilBuffer = true;
Expand Down
110 changes: 96 additions & 14 deletions client/app/engine/graphics/render/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ let RendererManager = function(graphicsEngine)

// ISSUES
// Performance issue with Firefox + three (water reflection)
const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
if (isFirefox)
this.shortCircuitWaterReflection = true;
// const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
// if (isFirefox)
// this.shortCircuitWaterReflection = true;
// Bloom + light intensity issue on mobile
const isMobile = 'ontouchstart' in window || navigator.msMaxTouchPoints > 0;
if (isMobile)
Expand Down Expand Up @@ -90,6 +90,9 @@ let RendererManager = function(graphicsEngine)
this.darkMaterial = new MeshBasicMaterial(
{ color: 'black', side: DoubleSide, morphTargets: true }
);
this.darkWater = new MeshBasicMaterial(
{ color: 'black', side: DoubleSide, morphTargets: true }
);

if (this.shadowVolumes)
{
Expand Down Expand Up @@ -147,7 +150,28 @@ extend(RendererManager.prototype, {
try {
this.updateSkies(mainCamera);
if (this.waterReflection)
this.updateWaters(cameraManager, renderer, mainScene, mainCamera);
{
// get main
let currentWid = this.graphics.app.model.server.selfModel.worldId.toString();
if (currentWid === '-1')
this.updateWaters(cameraManager, renderer, mainScene, mainCamera);
else
{
for (let j = 0, m = renderRegister.length; j < m; ++j)
{
const pass = renderRegister[j];
let sc = pass.scene;
if (!sc) continue;
sceneId = pass.sceneId.toString();
if (sceneId === '-1')
{
let cm = pass.camera.cameraObject;
this.updateWaters(cameraManager, renderer, sc, cm);
break;
}
}
}
}
if (this.shadowVolumes)
this.updateShadows(cameraManager, renderer, mainScene, mainCamera);
} catch (e) {
Expand All @@ -162,7 +186,8 @@ extend(RendererManager.prototype, {

let currentPass; let screen1; let screen2; let camera;
let bufferScene; let bufferCamera; let bufferTexture;
let otherEnd; let otherSceneId;
let otherEnd;
// let otherSceneId;

// This fixes the 1-frame lag for inner-most scenes.
for (let j = 0, m = renderRegister.length; j < m; ++j)
Expand All @@ -183,13 +208,44 @@ extend(RendererManager.prototype, {
let stc = cameraManager.stencilCamera;
this.stencilScene.updateMatrixWorld();
this.graphics.cameraManager.moveCameraFromMouse(0, 0, 0, 0);

let worlds = this.graphics.app.model.server.chunkModel.worlds;
let pathId;
let sceneId;
let instancedMaterials = this.graphics.instancedMaterials;
let waterMaterials = this.graphics.waterMaterials;
for (let i = 0, n = renderRegister.length; i < n; ++i)
{
if (renderCount++ > renderMax) break;
currentPass = renderRegister[i];
screen1 = currentPass.screen1;
screen2 = currentPass.screen2;
camera = currentPass.camera;
sceneId = currentPass.sceneId.toString();
pathId = currentPass.id;
let defaultMaterials = instancedMaterials.get(sceneId);
let defaultMaterial;
let defaultWaterMaterial;
if (!defaultMaterials) {
// console.error('[Renderer] Default material not found!');
} else {
defaultMaterial = defaultMaterials[0];
defaultWaterMaterial = waterMaterials.get(sceneId);
}
let passMaterial = instancedMaterials.get(pathId);
let passWaterMaterial = waterMaterials.get(pathId);
if (!passMaterial && defaultMaterial)
{
passMaterial = defaultMaterial.clone();
instancedMaterials.set(pathId, passMaterial);
passWaterMaterial = defaultWaterMaterial.clone();
waterMaterials.set(pathId, passWaterMaterial);
}
let chks = worlds.get(sceneId);
if (!chks)
{
// console.log('No chunks there.');
}

bufferScene = currentPass.scene;
if (!camera) continue;
Expand All @@ -200,7 +256,8 @@ extend(RendererManager.prototype, {
{
if (this.corrupted < 5)
{
console.log(`[Renderer] Could not get buffer scene ${currentPass.sceneId}.`);
// console.log(`[Renderer] Could not get buffer scene ${currentPass.sceneId}.`);
// Happens while loading other worlds.
this.corrupted++;
}

Expand All @@ -213,10 +270,10 @@ extend(RendererManager.prototype, {

if (screen2)
{
otherSceneId = currentPass.sceneId;
// otherSceneId = currentPass.sceneId;
otherEnd = screen2.getMesh();
// otherEnd.visible = false;
sceneManager.removeObject(otherEnd, otherSceneId, true);
otherEnd.visible = false;
// sceneManager.removeObject(otherEnd, otherSceneId, true);
}
//console.log('[Renderer] Rendering.');
//screen1.getMesh().updateMatrixWorld();
Expand All @@ -243,7 +300,6 @@ extend(RendererManager.prototype, {
// stc.rotation.copy(bufferCamera.rotation);
// bufferCamera.updateMatrixWorld(true);
stc.matrixWorld.copy(bufferCamera.matrixWorld);
// stc.updateProjectionMatrix();
// stc.matrixWorld.copy(bufferCamera.matrixWorld);
// stc.projectionMatrix.copy(bufferCamera.projectionMatrix);

Expand All @@ -259,6 +315,20 @@ extend(RendererManager.prototype, {
this.composers.set(id, bufferComposer);
}

if (chks && defaultMaterial && passMaterial)
chks.forEach(c => { let m = c.meshes; for (let cc = 0; cc < m.length; ++cc) {
let mi = m[cc];
if (!mi) continue;
if (c.water[cc])
{
// mi.material = passWaterMaterial;
}
else if (mi.material)
{
mi.material = passMaterial;
}
}});
s1.visible = false;
if (this.selectiveBloom)
{
bufferScene.traverse(obj => this.darkenNonBloomed(obj, materials));
Expand All @@ -268,12 +338,24 @@ extend(RendererManager.prototype, {
} else {
bufferComposer[2].render();
}
// s1.visible = false;
// s1.visible = true;
s1.visible = true;
if (chks && defaultMaterial && passMaterial)
chks.forEach(c => { let m = c.meshes; for (let cc = 0; cc < m.length; ++cc) {
let mi = m[cc];
if (!mi) continue;
if (c.water[cc])
{
// mi.material = defaultWaterMaterial;
}
else if (mi.material)
{
mi.material = defaultMaterial;
}
}});

if (screen2) {
sceneManager.addObject(otherEnd, otherSceneId);
// otherEnd.visible = true;
// sceneManager.addObject(otherEnd, otherSceneId);
otherEnd.visible = true;
}
}

Expand Down
Loading

0 comments on commit 7ec5b19

Please sign in to comment.