Skip to content

Commit

Permalink
Merge pull request #200 from davidmtech/master
Browse files Browse the repository at this point in the history
Tile splitting with margin
  • Loading branch information
davidmtech authored Aug 24, 2020
2 parents a0d80a5 + a651c5a commit 6fe2e99
Show file tree
Hide file tree
Showing 11 changed files with 514 additions and 164 deletions.
3 changes: 3 additions & 0 deletions demos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ <h4 class="panel-title"><a href="./meshes/" target="blank">Meshes</a></h4>
<h4 class="panel-title"><a href="./meshes-hit-test/" target="blank">Meshes - hit</a></h4>
<p>In this demo demonstrates, how to implemnt hit test for custom polygon meshes. </p>

<h4 class="panel-title"><a href="./meshes-custom-shader/" target="blank">Meshes - Custom shader</a></h4>
<p>This demo demonstrates, how to use custom shaders for custom polygon meshes. </p>

<h4 class="panel-title"><a href="./meshes-obj-import/" target="blank">Meshes - OBJ Import</a></h4>
<p>Example showing how to import OBJ files and display them in the map.</p>

Expand Down
296 changes: 296 additions & 0 deletions demos/meshes-custom-shader/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
var browser = null;
var map = null;
var renderer = null;
var woodTexture = null;
var cubeMesh = null;
var cubeShader = null;
var cubeState = null;
var animationTime = 0;
var timeStart = 0;

(function startDemo() {
// create map in the html div with id 'map-div'
// parameter 'map' sets path to the map which will be displayed
// you can create your own map on melown.com
// position parameter is described in documentation
// https://github.com/Melown/vts-browser-js/wiki/VTS-Browser-Map-API#position
// view parameter is described in documentation
// https://github.com/Melown/vts-browser-js/wiki/VTS-Browser-Map-API#definition-of-view
browser = vts.browser('map-div', {
map: 'https://cdn.melown.com/mario/store/melown2015/map-config/melown/VTS-Tutorial-map/mapConfig.json',
position : [ 'obj', 15.401540091152043, 50.660724358366906, 'float', 0.00, -244.63, -28.56, 0.00, 175.37, 45.00 ]
});

//check whether browser is supported
if (!browser) {
console.log('Your web browser does not support WebGL');
return;
}

renderer = browser.renderer;

//callback once is map config loaded
browser.on('map-loaded', onMapLoaded);
browser.on('tick', onTick);

//load texture used for cubes
loadTexture();

//create cube mesh
createCube();
})();


function loadTexture() {
//load texture used for cubes
var woodImage = vts.utils.loadImage('https://i.imgur.com/vVtw6pA.png',
(function(){
woodTexture = renderer.createTexture({ source: woodImage });
}).bind(this)
);
}


function createCube() {

var vertices = [ 1,1,1, -1,1,1, -1,-1,1, //top
-1,-1,1, 1,-1,1, 1,1,1,

-1,1,-1, 1,1,-1, 1,-1,-1, //bottom
1,-1,-1, -1,-1,-1, -1,1,-1,

1,-1,1, -1,-1,1, -1,-1,-1, //front
-1,-1,-1, 1,-1,-1, 1,-1,1,

-1,1,1, 1,1,1, 1,1,-1, //back
1,1,-1, -1,1,-1, -1,1,1,

-1,-1,1, -1,1,1, -1,1,-1, //left
-1,1,-1, -1,-1,-1, -1,-1,1,

1,1,1, 1,-1,1, 1,-1,-1, //right
1,-1,-1, 1,1,-1, 1,1,1 ];

var uvs = [ 0,0, 1,0, 1,1, //top
1,1, 0,1, 0,0,

0,0, 1,0, 1,1, //bottom
1,1, 0,1, 0,0,

0,0, 1,0, 1,1, //front
1,1, 0,1, 0,0,

0,0, 1,0, 1,1, //back
1,1, 0,1, 0,0,

0,0, 1,0, 1,1, //left
1,1, 0,1, 0,0,

0,0, 1,0, 1,1, //right
1,1, 0,1, 0,0 ];

var normals = [ 0,0,1, 0,0,1, 0,0,1, //top
0,0,1, 0,0,1, 0,0,1,

0,0,-1, 0,0,-1, 0,0,-1, //bottom
0,0,-1, 0,0,-1, 0,0,-1,

0,-1,0, 0,-1,0, 0,-1,0, //front
0,-1,0, 0,-1,0, 0,-1,0,

0,1,0, 0,1,0, 0,1,0, //back
0,1,0, 0,1,0, 0,1,0,

-1,0,0, -1,0,0, -1,0,0, //left
-1,0,0, -1,0,0, -1,0,0,

1,0,0, 1,0,0, 1,0,0, //right
1,0,0, 1,0,0, 1,0,0 ];

cubeMesh = renderer.createMesh({ vertices: vertices, uvs: uvs, normals: normals });

cubeState = renderer.createState({
blend : false,
stencil : false,
zoffset : 0,
zwrite : true,
ztest : true, // z buffer test - less
zequal : true, // switch z buffer test from less to less or equal
culling : true });

cubeShader = renderer.createShader({

'vertexShader' : 'attribute vec3 aPosition;\n'+
'attribute vec2 aTexCoord;\n'+
'attribute vec3 aNormal;\n'+
'uniform mat4 uMV, uProj;\n'+
'uniform mat3 uNorm;\n'+
'uniform float uFogDensity;\n'+
'varying vec2 vTexCoord;\n'+
'varying vec4 vPosition;\n'+
'varying vec3 vNormal;\n'+
'varying float vFogFactor;\n'+
'void main() {\n'+
//compute camera space position and prejected position
'vec4 camSpacePos = uMV * vec4(aPosition, 1.0);\n'+
'gl_Position = uProj * camSpacePos;\n'+

//compute fog density
'float camDist = length(camSpacePos.xyz);\n'+
'vFogFactor = exp(uFogDensity * camDist);\n'+

//pass UVs, Positions and Normals to fragment shader
'vTexCoord = aTexCoord;\n'+
'vPosition = camSpacePos;\n'+
'vNormal = aNormal * uNorm;\n'+
'}',

'fragmentShader' :

'precision mediump float;\n'+

'varying vec2 vTexCoord;\n'+
'varying vec4 vPosition;\n'+
'varying vec3 vNormal;\n'+
'varying float vFogFactor;\n'+

'uniform sampler2D uSampler;\n'+
'uniform mat4 uMaterial;\n'+
'uniform float uTime;\n'+
'uniform vec4 uFogColor;\n'+

'void main() {\n'+
//compute light color
'vec3 ldir = normalize(-vPosition.xyz);\n'+
'vec3 normal = normalize(vNormal);\n'+
'vec3 eyeDir = ldir;\n'+
'vec3 refDir = reflect(-ldir, normal);\n'+
'float specW = min(1.0, pow(max(dot(refDir, eyeDir), 0.0), uMaterial[3][0]));\n'+
'float diffW = min(1.0, max(dot(normal, ldir), 0.0));\n'+
'vec4 lcolor = uMaterial[0]+(uMaterial[1]*diffW)+(uMaterial[2]*specW);\n'+

//texture color
'vec4 tcolor = texture2D(uSampler, vTexCoord);\n'+

//sine animation
'tcolor.rgb = mix(vec3(1.0,1.0,1.0), tcolor.rgb, abs(vTexCoord.y-0.5+sin(vTexCoord.x*3.14*2.0-uTime)*0.25)*2.0);\n'+

//apply fog
'gl_FragColor = mix(uFogColor, vec4(lcolor.xyz*(1.0/255.0), 1.0) * tcolor, vFogFactor); gl_FragColor.z *= uMaterial[3][1];\n'+
'}'

});

}


function drawCube(coords, scale, ambientColor, diffuseColor, specularColor, shininess, textured) {

//get camera transformations matrices
var cameraInfo = map.getCameraInfo();

//get local space matrix
//this matrix makes mesh
//perpendiculal to the ground
//and oriteted to the north
var spaceMatrix = map.getNED(coords);

//move cube above terain
coords[2] += scale * 2;

//we have coords in navigation coodinates,
//so we need to convert them to camera space.
//you can imagine camera space as physical space
//but reative to the camera coordinates
coords = map.convertCoordsFromNavToCameraSpace(coords, 'float');

// translate matrix
var translateMatrix = [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
coords[0], coords[1], coords[2], 1
];

// scale matrix
var scaleMatrix = [
scale, 0, 0, 0,
0, scale, 0, 0,
0, 0, scale, 0,
0, 0, 0, 1
];

//combine scale, space and translate matrices
var mv = vts.mat4.create();
vts.mat4.multiply(spaceMatrix, scaleMatrix, mv);
vts.mat4.multiply(translateMatrix, mv, mv);

//multiply cube matrix with camera view matrix
vts.mat4.multiply(cameraInfo.viewMatrix, mv, mv);

var norm = [
0,0,0,
0,0,0,
0,0,0
];

//extract normal transformation matrix from model view matrix
//this matrix is needed for corret lighting
vts.mat4.toInverseMat3(mv, norm);

//setup material
var material = [
ambientColor[0], ambientColor[1], ambientColor[2], 0,
diffuseColor[0], diffuseColor[1], diffuseColor[2], 0,
specularColor[0], specularColor[1], specularColor[2], 0,
shininess, 1, 0, 0
];

renderer.setState(cubeState);

//draw cube
renderer.drawMesh({
mesh : cubeMesh,
texture : woodTexture,
shader : cubeShader,
shaderVariables : {
'uProj' : ['mat4', cameraInfo.projectionMatrix],
'uMV' : ['mat4', mv],
'uNorm' : ['mat3', norm],
'uMaterial' : ['mat4', material],
'uTime' : ['float', animationTime]
}
});
}


function onMapLoaded() {
//add render slots
//render slots are called during map render
map = browser.map;
map.addRenderSlot('custom-meshes', onDrawMeshes, true);
map.moveRenderSlotAfter('after-map-render', 'custom-meshes');
timeStart = Date.now();
};


function onDrawMeshes(renderChannel) {
if (renderChannel != 'base') {
return; //draw only in base channel
}

if (woodTexture) { //check whether texture is loded
//draw textured cube
drawCube([15.401273646044713, 50.66085886059055, 3], 10, [0,0,0], [255,255,255], [255,255,255], 90, true);
}
}

function onTick() {
if (map && woodTexture) { //check whether texture is loded

animationTime = (Date.now() - timeStart) * 0.002;

map.redraw();
}
}

14 changes: 14 additions & 0 deletions demos/meshes-custom-shader/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<head>
<title>VTS Browser - Meshes</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="../../build/vts-browser.css" />
<script type="text/javascript" src="../../build/vts-browser.js"></script>
</head>

<body style = "padding: 0; margin: 0;">
<div id="map-div" style="width:100%; height:100%;">
</div>
<script type="text/javascript" src="demo.js"></script>
</body>
</html>
Binary file added demos/meshes-custom-shader/wood.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions demos/meshes-hit-test/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function createCube() {

function drawCube(coords, scale, ambientColor, diffuseColor, specularColor, shininess, shader) {
//get camera transformations matrices
var cameInfo = map.getCameraInfo();
var cameraInfo = map.getCameraInfo();

//get local space matrix
//this matrix makes mesh
Expand Down Expand Up @@ -159,7 +159,7 @@ function drawCube(coords, scale, ambientColor, diffuseColor, specularColor, shin
vts.mat4.multiply(translateMatrix, mv, mv);

//multiply cube matrix with camera view matrix
vts.mat4.multiply(cameInfo.viewMatrix, mv, mv);
vts.mat4.multiply(cameraInfo.viewMatrix, mv, mv);

var norm = [
0,0,0,
Expand Down
4 changes: 2 additions & 2 deletions demos/meshes/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function createCube() {

function drawCube(coords, scale, ambientColor, diffuseColor, specularColor, shininess, textured) {
//get camera transformations matrices
var cameInfo = map.getCameraInfo();
var cameraInfo = map.getCameraInfo();

//get local space matrix
//this matrix makes mesh
Expand Down Expand Up @@ -146,7 +146,7 @@ function drawCube(coords, scale, ambientColor, diffuseColor, specularColor, shin
vts.mat4.multiply(translateMatrix, mv, mv);

//multiply cube matrix with camera view matrix
vts.mat4.multiply(cameInfo.viewMatrix, mv, mv);
vts.mat4.multiply(cameraInfo.viewMatrix, mv, mv);

var norm = [
0,0,0,
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "vts-browser-js",
"version": "2.23.3",
"version": "2.23.6",
"description": "JavaScript WebGL 3D maps rendering engine",
"main": "src/browser/index.js",
"scripts": {
"test": "echo 'There are no tests implemented yet'",
"install": "",
"clean": "node_modules/.bin/rimraf build",
"dev": "WEBPACK_ENV=dev node_modules/.bin/webpack --progress --colors --watch --hide-modules",
"dev2": "WEBPACK_ENV=dev node_modules/.bin/webpack --progress --colors --hide-modules",
"dist": "NODE_ENV=production node_modules/.bin/webpack --progress --colors --hide-modules",
"start": "node_modules/.bin/webpack-dev-server --content-base ./ --inline"
},
Expand Down Expand Up @@ -36,12 +37,12 @@
"chai": "^4.2.0",
"copy-webpack-plugin": "^6.0.2",
"css-loader": "^3.5.3",
"eslint": "^7.2.0",
"eslint": "^7.7.0",
"eslint-config-airbnb": "^18.1.0",
"eslint-loader": "^4.0.2",
"eslint-plugin-import": "^2.21.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.20.0",
"eslint-plugin-react": "^7.20.6",
"html-webpack-plugin": "^4.3.0",
"install": "^0.13.0",
"license-webpack-plugin": "^2.2.0",
Expand Down
Loading

0 comments on commit 6fe2e99

Please sign in to comment.