Skip to content
This repository has been archived by the owner on Dec 6, 2020. It is now read-only.

Commit

Permalink
Simple terrain shading
Browse files Browse the repository at this point in the history
  • Loading branch information
clvrk committed May 3, 2019
1 parent 232c2d6 commit c40bdbf
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 209 deletions.
362 changes: 180 additions & 182 deletions app.js

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions docs/documentation.md

This file was deleted.

9 changes: 0 additions & 9 deletions docs/todo.md

This file was deleted.

1 change: 1 addition & 0 deletions src/downloadTextures.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = function(extract_address) {
})

.then(() => new Promise((resolve, reject) => {
console.log('\nExtracting...');
lib_extract(zip_address, { dir: lib_path.dirname(zip_address) }, err => {
if (err !== null && err !== undefined) reject(err.toString() + "\nFailed to download textures, zip extraction failed");
else resolve();
Expand Down
8 changes: 4 additions & 4 deletions src/palettes/textureCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module.exports = function() {

var cache = [ ];

this.save = function( name, value, data ) {
this.save = function( name, value, data, y ) {
// if ( argv.verbose == true ) { console.log( '\nSaving new texture to cache.\t' + name + ' ' + value ) };
cache[ JSON.stringify( { name: name, value: value } ) ] = data;
cache[ JSON.stringify( { name: name, value: value, y: y } ) ] = data;
};

this.get = function( name, value ) {
return cache[ JSON.stringify( { name: name, value: value } ) ];
this.get = function( name, value, y ) {
return cache[ JSON.stringify( { name: name, value: value, y: y } ) ];
};

this.list = function() {
Expand Down
39 changes: 36 additions & 3 deletions src/render/loadTexture.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const patchTable = require( '../../app.js' ).patchTable,
monoTable = require( '../../app.js' ).monoTable,
path_resourcepack = require( '../../app.js' ).path_resourcepack;

const renderMode = require( '../../app.js' ).renderMode;

var file = null,
fileExt = '.png';

module.exports = async function loadTexture( name, value, x, y, z, cache ) {
module.exports = async function loadTexture( name, value, x, y, z, blockY, cache ) {
// Is the texture in the cache already? No: Load texture from filesystem, Yes: Skip to compositing :)!
if ( cache.get( name, value ) === undefined ) {
if ( cache.get( name, value, blockY ) === undefined ) {
var imageBuffer = null;
// Does the texture have multiple faces?
if ( blockTable[ name.slice( 10 ) ] !== undefined ) {
Expand Down Expand Up @@ -92,6 +94,37 @@ module.exports = async function loadTexture( name, value, x, y, z, cache ) {
} );
} );
};
cache.save( name, value, imageBuffer );

if( name != 'minecraft:water' ) {
switch( renderMode ) {
case 'topdown_shaded':
if (imageBuffer['scaling'] == undefined) {
imageBuffer = mapnik.Image.fromBytesSync(imageBuffer);
};
imageBuffer.premultiplySync();

var opac = 0,
blendImg = null;

switch( true ) {
case ( blockY < 64 ):
blendImg = cache.get('blend_black', 0);
opac = (64-blockY)/(blockY*64);
break;
case ( blockY >= 64 ):
blendImg = cache.get('blend_white', 0);
opac = (-64+blockY)/(blockY);
break;
};

await imageBuffer.composite( blendImg, {
comp_op: mapnik.compositeOp['overlay'], // comp_mode,
opacity: opac
}, (err, data) => {
imageBuffer = data;
} )
break;
} };
cache.save( name, value, imageBuffer, blockY );
};
};
13 changes: 6 additions & 7 deletions src/render/renderChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ module.exports = function( Chunk, Cache, size_texture, worldOffset, ZoomLevelMax
for( ix = 0; ix < 16; ix++ )
{
if ( chunk.get( ix, iy, iz ).name !== 'minecraft:air' ) {
await loadTexture( chunk.get( ix, iy, iz ).name, chunk.get( ix, iy, iz ).value, ix, iy, iz, cache );
textureBuffer = cache.get( chunk.get( ix, iy, iz ).name, chunk.get( ix, iy, iz ).value );
/*
if ( renderMode ) {
switch( renderMode ) {
case 'topdown_shaded':
const shadeTopDown = require( './shadeTopDown.js' );
await new Promise( ( resolve, reject ) => { shadeTopDown( textureBuffer, chunk.get( ix, iy, iz ).y ).then( ( data ) => { textureBuffer = data; resolve(); } ) } );
break;
await loadTexture( chunk.get( ix, iy, iz ).name, chunk.get( ix, iy, iz ).value, ix, iy, iz, chunk.get( ix, iy, iz ).y, cache );
textureBuffer = cache.get( chunk.get( ix, iy, iz ).name, chunk.get( ix, iy, iz ).value, chunk.get( ix, iy, iz ).y );
};
} else {
await loadTexture( chunk.get( ix, iy, iz ).name, chunk.get( ix, iy, iz ).value, ix, iy, iz, 0, cache );
textureBuffer = cache.get( chunk.get( ix, iy, iz ).name, chunk.get( ix, iy, iz ).value, 0 );
};
*/

composeArray.push( {
buffer: textureBuffer,
x: size_texture*ix,
Expand Down

0 comments on commit c40bdbf

Please sign in to comment.