├── public ├── Images │ └── ajax-loader.gif ├── main.js ├── index.html ├── CesiumThree.css └── CesiumThree.js ├── .gitignore ├── README.md ├── package.json ├── .jshintrc ├── server.js ├── gulpfile.js └── LICENSE /public/Images/ajax-loader.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CesiumGS/cesium-threejs-experiment/HEAD/public/Images/ajax-loader.gif -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Build output 2 | build 3 | 4 | # NPM 5 | node_modules 6 | npm-debug.log 7 | 8 | # NPM-sourced third-party client libraries 9 | /public/ThirdParty 10 | -------------------------------------------------------------------------------- /public/main.js: -------------------------------------------------------------------------------- 1 | /*global require*/ 2 | require({ 3 | baseUrl : '.', 4 | paths : { 5 | Cesium : 'ThirdParty/Cesium', 6 | Three : 'ThirdParty/Three' 7 | } 8 | }, ['CesiumThree'], function() { 9 | }); 10 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | CesiumJS+ThreeJS 11 | 12 | 13 | 14 |
15 |
16 |
17 |
18 |
19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cesium-threejs-experiment 2 | A small experiment using Three JS on Cesium to emulate a combined scene. 3 | Adapted from code provided by [Wilson Muktar](son_coolz91@hotmail.com). 4 | 5 | [Here on github pages](https://analyticalgraphicsinc.github.io/cesium-threejs-experiment/public/). 6 | 7 | ## Getting Started 8 | Requires Node.js, we recommend version 4.4+. From the root directory, run: 9 | ``` 10 | npm install 11 | ``` 12 | This will automatically download Cesium and Threejs, then copy the relevant files 13 | over to `public/Thirdparty`. 14 | Then, run: 15 | ``` 16 | npm run start 17 | ``` 18 | or 19 | ``` 20 | node server.js 21 | ``` 22 | 23 | You should see: 24 | ``` 25 | restify listening at http://127.0.0.1:8080 26 | ``` 27 | 28 | Navigate to `http://localhost:8080` to view the experiment! You can also use 29 | other ports: 30 | ``` 31 | node server.js --port=4040 32 | restify listening at http://127.0.0.1:4040 33 | ``` 34 | 35 | Most of the interesting code is in `public/CesiumThree.js`. 36 | To view changes to the demo, just refresh (or cache refresh) your browser. 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cesium-threejs-experiment", 3 | "version": "1.0.0", 4 | "private": true, 5 | "description": "Cesium + Three JS Experiment", 6 | "author": "AGI", 7 | "devDependencies": { 8 | "bluebird": "3.4.7", 9 | "cesium": "^1.45.0", 10 | "chroma-js": "1.0.1", 11 | "fs-extra": "2.0.0", 12 | "gulp": "3.9.1", 13 | "gulp-jshint": "2.0.4", 14 | "gulp-uglify": "2.0.1", 15 | "jshint": "2.9.4", 16 | "jshint-stylish": "2.2.1", 17 | "knockout": "3.4.1", 18 | "mime": "1.3.4", 19 | "nodemon": "1.11.0", 20 | "pepjs": "0.4.2", 21 | "requirejs": "2.3.2", 22 | "requirejs-text": "2.0.15", 23 | "restify": "4.3.0", 24 | "three": "0.87.1", 25 | "yargs": "6.6.0" 26 | }, 27 | "scripts": { 28 | "start": "node --max-old-space-size=4096 server.js", 29 | "watch": "nodemon --max-old-space-size=4096 --watch lib --watch config.json --watch server.js server.js", 30 | "jsHint": "gulp jsHint", 31 | "jsHint-watch": "gulp jsHint-watch", 32 | "postinstall": "gulp postInstall", 33 | "website-combine": "gulp website-combine", 34 | "website-release": "gulp website-release" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /public/CesiumThree.css: -------------------------------------------------------------------------------- 1 | @import url(ThirdParty/Cesium/Widgets/widgets.css); 2 | 3 | body { 4 | height: 100%; 5 | width: 100%; 6 | margin: 0; 7 | overflow: hidden; 8 | padding: 0; 9 | background: #000; 10 | } 11 | 12 | #cesiumContainer{ 13 | position: absolute; 14 | top: 0; 15 | left: 0; 16 | height: 100%; 17 | width: 100%; 18 | margin: 0; 19 | overflow: hidden; 20 | padding: 0; 21 | font-family: sans-serif; 22 | } 23 | 24 | #ThreeContainer{ 25 | position: absolute; 26 | top: 0; 27 | left: 0; 28 | height: 100%; 29 | width: 100%; 30 | margin: 0; 31 | overflow: hidden; 32 | padding: 0; 33 | font-family: sans-serif; 34 | pointer-events:none; 35 | } 36 | 37 | .fullWindow { 38 | position: absolute; 39 | top: 0; 40 | left: 0; 41 | height: 100%; 42 | width: 100%; 43 | margin: 0; 44 | overflow: hidden; 45 | padding: 0; 46 | font-family: sans-serif; 47 | } 48 | 49 | .loadingIndicator { 50 | display: block; 51 | position: absolute; 52 | top: 50%; 53 | left: 50%; 54 | margin-top: -33px; 55 | margin-left: -33px; 56 | width: 66px; 57 | height: 66px; 58 | background-position: center; 59 | background-repeat: no-repeat; 60 | background-image: url(Images/ajax-loader.gif); 61 | } 62 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "bitwise": false, 3 | "camelcase": false, 4 | "curly": true, 5 | "eqeqeq": true, 6 | "forin": true, 7 | "freeze": true, 8 | "immed": true, 9 | "latedef": "nofunc", 10 | "newcap": true, 11 | "noarg": true, 12 | "noempty": true, 13 | "nonbsp": true, 14 | "nonew": true, 15 | "plusplus": false, 16 | "quotmark": false, 17 | "undef": true, 18 | "unused": "strict", 19 | "strict": true, 20 | "asi": false, 21 | "boss": false, 22 | "debug": false, 23 | "eqnull": false, 24 | "moz": false, 25 | "evil": false, 26 | "expr": false, 27 | "funcscope": false, 28 | "globalstrict": false, 29 | "iterator": false, 30 | "lastsemic": false, 31 | "laxbreak": false, 32 | "laxcomma": false, 33 | "loopfunc": false, 34 | "multistr": true, 35 | "noyield": false, 36 | "notypeof": false, 37 | "proto": false, 38 | "scripturl": false, 39 | "shadow": false, 40 | "sub": false, 41 | "supernew": false, 42 | "validthis": false, 43 | "browser": false, 44 | "browserify": false, 45 | "couch": false, 46 | "devel": true, 47 | "dojo": false, 48 | "jasmine": false, 49 | "jquery": false, 50 | "mocha": true, 51 | "mootools": false, 52 | "node": true, 53 | "nonstandard": false, 54 | "prototypejs": false, 55 | "qunit": false, 56 | "rhino": false, 57 | "shelljs": false, 58 | "worker": false, 59 | "wsh": false, 60 | "yui": false 61 | } 62 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var mime = require('mime'); 4 | var restify = require('restify'); 5 | var yargs = require('yargs'); 6 | 7 | yargs.options({ 8 | 'port': { 9 | 'default': 8080, 10 | 'description': 'Port to listen on.' 11 | }, 12 | 'public': { 13 | 'type': 'boolean', 14 | 'default': false, 15 | 'description': 'Run a public server that listens on all interfaces.' 16 | }, 17 | 'production': { 18 | 'type': 'boolean', 19 | 'default': false, 20 | 'description': 'Run the built version of the code.' 21 | } 22 | }); 23 | 24 | var argv = yargs.argv; 25 | var server = restify.createServer(); 26 | 27 | //The built in gzipResponse gzips everything, including compressed files. 28 | //This disables gzipping for specific routes and files. 29 | var gzipResponse = restify.gzipResponse(); 30 | server.use(function (req, res, next) { 31 | var url = req.url; 32 | 33 | //Tiles are pre-gzipped. 34 | if (/\/3DTiles\/(.*)/.test(url)) { 35 | res.header('Content-Encoding', 'gzip'); 36 | next(); 37 | return; 38 | } 39 | 40 | //Don't gzip things that are likely gzipped already. 41 | var contentType = mime.lookup(url); 42 | if (/^(image|audio|video)\//.test(contentType)) { 43 | next(); 44 | return; 45 | } 46 | 47 | gzipResponse(req, res, next); 48 | }); 49 | 50 | server.get(/.*/, restify.serveStatic({ 51 | directory: argv.production ? 'build' : 'public', 52 | default: 'index.html', 53 | maxAge: 0 54 | })); 55 | 56 | server.listen(argv.port, argv.public ? undefined : 'localhost', function () { 57 | console.log('%s listening at %s', server.name, server.url); 58 | }); 59 | 60 | var shuttingDown = false; 61 | function shutdown() { 62 | if (shuttingDown) { 63 | return; 64 | } 65 | shuttingDown = true; 66 | 67 | console.log("Closing server connections..."); 68 | server.close(function () { 69 | console.log("Shutdown successfull."); 70 | process.exit(0); 71 | }); 72 | } 73 | 74 | process.on('SIGTERM', shutdown); 75 | process.on('SIGINT', shutdown); 76 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var fsExtra = require('fs-extra'); 4 | var gulp = require('gulp'); 5 | var gulpJshint = require('gulp-jshint'); 6 | var gulpUglify = require('gulp-uglify'); 7 | var path = require('path'); 8 | var Promise = require('bluebird'); 9 | var requirejs = require('requirejs'); 10 | var yargs = require('yargs'); 11 | 12 | var argv = yargs.argv; 13 | 14 | var jsHintFiles = ['**/*.js', '!build/**', '!node_modules/**', '!public/ThirdParty/**', '!public/ThirdParty/Cesium/**']; 15 | 16 | gulp.task('jsHint', function () { 17 | var stream = gulp.src(jsHintFiles) 18 | .pipe(gulpJshint()) 19 | .pipe(gulpJshint.reporter('jshint-stylish')); 20 | 21 | if (argv.failTaskOnError) { 22 | stream = stream.pipe(gulpJshint.reporter('fail')); 23 | } 24 | 25 | return stream; 26 | }); 27 | 28 | gulp.task('jsHint-watch', function () { 29 | gulp.watch(jsHintFiles).on('change', function (event) { 30 | gulp.src(event.path) 31 | .pipe(gulpJshint.extract('auto')) 32 | .pipe(gulpJshint()) 33 | .pipe(gulpJshint.reporter('jshint-stylish')); 34 | }); 35 | }); 36 | 37 | //Copies relevant parts of client JS libraries to `public/ThirdParty` for local development 38 | gulp.task('postInstall', function () { 39 | fsExtra.removeSync('public/ThirdParty'); 40 | 41 | var webSiteLibs = [ 42 | { 43 | name: 'chroma-js', 44 | glob: [ 45 | 'node_modules/chroma-js/chroma.js' 46 | ] 47 | }, { 48 | name: 'knockout', 49 | glob: [ 50 | 'node_modules/knockout/build/output/knockout-latest.js' 51 | ] 52 | }, { 53 | name: 'pepjs', 54 | glob: [ 55 | 'node_modules/pepjs/dist/pep.js' 56 | ] 57 | }, { 58 | name: 'requirejs', 59 | glob: [ 60 | 'node_modules/requirejs/require.js' 61 | ] 62 | }, { 63 | name: 'requirejs-text', 64 | glob: [ 65 | 'node_modules/requirejs-text/text.js' 66 | ] 67 | }, { 68 | name: 'Cesium', 69 | glob: [ 70 | 'node_modules/cesium/Source/**', 71 | '!node_modules/cesium/Source/Workers/**' 72 | ], 73 | subDir: true 74 | }, { 75 | name: 'Three', 76 | glob: [ 77 | 'node_modules/three/build/three.min.js', 78 | ], 79 | subDir: true 80 | }, 81 | { 82 | name: 'Cesium/Workers', 83 | glob: [ 84 | 'node_modules/cesium/Build/Cesium/Workers/**' 85 | ], 86 | subDir: true 87 | } 88 | ]; 89 | 90 | var promises = []; 91 | var thirdPartyDirectory = 'public/ThirdParty'; 92 | webSiteLibs.forEach(function (module) { 93 | var dest = thirdPartyDirectory; 94 | if (module.subDir) { 95 | dest += '/' + module.name; 96 | } 97 | 98 | var options = { 99 | nodir: true, 100 | base: module.base 101 | }; 102 | promises.push(streamToPromise(gulp.src(module.glob, options).pipe(gulp.dest(dest)))); 103 | }); 104 | 105 | return Promise.all(promises); 106 | }); 107 | 108 | //Outputs a combined website file into the build directory without minification or removing debug code. 109 | gulp.task('website-combine', function () { 110 | return buildSite({ 111 | removePragmas: false, 112 | optimizer: 'none', 113 | outputDirectory: path.join('build') 114 | }); 115 | }); 116 | 117 | //Outputs the combined and minified website, with debug code removed, to the build directory. 118 | gulp.task('website-release', function () { 119 | return buildSite({ 120 | removePragmas: true, 121 | optimizer: 'uglify2', 122 | outputDirectory: path.join('build') 123 | }); 124 | }); 125 | 126 | function buildSite(options) { 127 | fsExtra.removeSync('build'); 128 | 129 | var optimizer = options.optimizer; 130 | var outputDirectory = options.outputDirectory; 131 | var removePragmas = options.removePragmas; 132 | 133 | var promise = combineSite(!removePragmas, optimizer, outputDirectory); 134 | 135 | return promise.then(function () { 136 | //copy to build folder with copyright header added at the top 137 | var everythingElse = [ 138 | 'public/**', 139 | '!**/*.html', 140 | '!**/*.js', 141 | '!**/*.glsl', 142 | '!**/*.txt', 143 | '!**/LICENSE', 144 | '!**/*.map', 145 | '!**/*.less', 146 | '!**/*.scss', 147 | '!**/*.swf', 148 | '!**/*.md', 149 | '!public/ThirdParty/bootstrap/fonts/**' 150 | ]; 151 | 152 | var cssPromise = Promise.resolve(); 153 | if (optimizer === 'uglify2') { 154 | cssPromise = Promise.join( 155 | minifyCSS('public/index.css', path.join(outputDirectory, 'index.css')), 156 | minifyCSS('public/ThirdParty/Cesium/Widgets/InfoBox/InfoBoxDescription.css', path.join(outputDirectory, 'ThirdParty/Cesium/Widgets/InfoBox/InfoBoxDescription.css')) 157 | ); 158 | everythingElse.push('!**/**.css'); 159 | } 160 | 161 | return Promise.join( 162 | streamToPromise(gulp.src(everythingElse, {nodir: true}) 163 | .pipe(gulp.dest(outputDirectory))), 164 | 165 | streamToPromise(gulp.src('public/ThirdParty/require.js') 166 | .pipe(gulpUglify()) 167 | .pipe(gulp.dest(outputDirectory + '/ThirdParty/'))), 168 | 169 | //index.html is the only html file that needs to be copied. 170 | streamToPromise(gulp.src('public/index.html') 171 | .pipe(gulp.dest(outputDirectory))), 172 | 173 | //We still need the built Cesium workers in the final output. 174 | streamToPromise(gulp.src('public/ThirdParty/Cesium/Workers/**/*.js', {nodir: true}) 175 | .pipe(gulp.dest(path.join(outputDirectory, 'ThirdParty/Cesium/Workers')))), 176 | 177 | cssPromise); 178 | 179 | }); 180 | } 181 | 182 | function combineSite(debug, optimizer, combineOutput) { 183 | return new Promise(function (resolve, reject) { 184 | requirejs.optimize({ 185 | wrap: true, 186 | useStrict: true, 187 | optimize: optimizer, 188 | optimizeCss: 'standard', 189 | pragmas: { 190 | debug: debug 191 | }, 192 | mainConfigFile: 'public/main.js', 193 | name: 'main', 194 | out: path.join(combineOutput, 'main.js') 195 | }, resolve, reject); 196 | }); 197 | } 198 | 199 | function minifyCSS(inFile, outFile) { 200 | return new Promise(function (resolve, reject) { 201 | requirejs.optimize({ 202 | wrap: true, 203 | useStrict: true, 204 | optimizeCss: 'standard', 205 | pragmas: { 206 | debug: true 207 | }, 208 | cssIn: inFile, 209 | out: outFile 210 | }, resolve, reject); 211 | }); 212 | } 213 | 214 | function streamToPromise(stream) { 215 | return new Promise(function (resolve, reject) { 216 | stream.on('finish', resolve); 217 | stream.on('end', reject); 218 | }); 219 | } 220 | -------------------------------------------------------------------------------- /public/CesiumThree.js: -------------------------------------------------------------------------------- 1 | var Cesium = {}; 2 | 3 | define([ 4 | 'Cesium/Core/Cartesian3', 5 | 'Cesium/Widgets/Viewer/Viewer', 6 | 'Cesium/Scene/ShadowMode', 7 | 'Cesium/Core/Color', 8 | 'Cesium/Core/Math', 9 | 'Three/three.min' 10 | ], function( 11 | CesiumCartesian3, 12 | CesiumViewer, 13 | CesiumShadowMode, 14 | CesiumColor, 15 | CesiumMath, 16 | THREE) { 17 | 'use strict'; 18 | 19 | var loadingIndicator = document.getElementById('loadingIndicator'); 20 | loadingIndicator.style.display = 'none'; 21 | 22 | // boundaries in WGS84 around the object 23 | var minWGS84 = [115.23,39.55]; 24 | var maxWGS84 = [116.23,41.55]; 25 | var cesiumContainer = document.getElementById("cesiumContainer"); 26 | var ThreeContainer = document.getElementById("ThreeContainer"); 27 | 28 | var _3Dobjects = []; //Could be any Three.js object mesh 29 | var three = { 30 | renderer: null, 31 | camera: null, 32 | scene: null 33 | }; 34 | 35 | var cesium = { 36 | viewer: null 37 | }; 38 | 39 | function _3DObject(){ 40 | //THREEJS 3DObject.mesh 41 | this.threeMesh = null; 42 | //location bounding box 43 | this.minWGS84 = null; 44 | this.maxWGS84 = null; 45 | } 46 | 47 | function initCesium(){ 48 | cesium.viewer = new CesiumViewer(cesiumContainer,{ 49 | useDefaultRenderLoop: false, 50 | selectionIndicator : false, 51 | homeButton:false, 52 | sceneModePicker:false, 53 | navigationHelpButton:false, 54 | infoBox : false, 55 | navigationHelpButton:false, 56 | navigationInstructionsInitiallyVisible:false, 57 | animation : false, 58 | timeline : false, 59 | fullscreenButton : false, 60 | allowTextureFilterAnisotropic:false, 61 | contextOptions:{ 62 | webgl: { 63 | alpha: false, 64 | antialias: true, 65 | preserveDrawingBuffer : true, 66 | failIfMajorPerformanceCaveat: false, 67 | depth:true, 68 | stencil:false, 69 | anialias:false 70 | }, 71 | }, 72 | targetFrameRate:60, 73 | resolutionScale:0.1, 74 | orderIndependentTranslucency : true, 75 | imageryProvider : undefined, 76 | baseLayerPicker : false, 77 | geocoder : false, 78 | automaticallyTrackDataSourceClocks: false, 79 | dataSources: null, 80 | clock: null, 81 | terrainShadows: CesiumShadowMode.DISABLED 82 | }); 83 | 84 | var center = CesiumCartesian3.fromDegrees( 85 | (minWGS84[0] + maxWGS84[0]) / 2, 86 | ((minWGS84[1] + maxWGS84[1]) / 2)-1, 87 | 200000 88 | ); 89 | cesium.viewer.camera.flyTo({ 90 | destination : center, 91 | orientation : { 92 | heading : CesiumMath.toRadians(0), 93 | pitch : CesiumMath.toRadians(-60), 94 | roll : CesiumMath.toRadians(0) 95 | }, 96 | duration: 3 97 | }); 98 | } 99 | 100 | function initThree(){ 101 | var fov = 45; 102 | var width = window.innerWidth; 103 | var height = window.innerHeight; 104 | var aspect = width / height; 105 | var near = 1; 106 | var far = 10*1000*1000; 107 | 108 | three.scene = new THREE.Scene(); 109 | three.camera = new THREE.PerspectiveCamera(fov, aspect, near, far); 110 | three.renderer = new THREE.WebGLRenderer({alpha: true}); 111 | ThreeContainer.appendChild(three.renderer.domElement); 112 | } 113 | 114 | function init3DObject(){ 115 | //Cesium entity 116 | var entity = { 117 | name : 'Polygon', 118 | polygon : { 119 | hierarchy : CesiumCartesian3.fromDegreesArray([ 120 | minWGS84[0], minWGS84[1], 121 | maxWGS84[0], minWGS84[1], 122 | maxWGS84[0], maxWGS84[1], 123 | minWGS84[0], maxWGS84[1], 124 | ]), 125 | material : CesiumColor.RED.withAlpha(0.2) 126 | } 127 | }; 128 | var Polygon = cesium.viewer.entities.add(entity); 129 | 130 | //Three.js Objects 131 | // Lathe geometry 132 | var doubleSideMaterial = new THREE.MeshNormalMaterial({ 133 | side: THREE.DoubleSide 134 | }); 135 | var segments = 10; 136 | var points = []; 137 | for ( var i = 0; i < segments; i ++ ) { 138 | points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * segments + 5, ( i - 5 ) * 2 ) ); 139 | } 140 | var geometry = new THREE.LatheGeometry( points ); 141 | var latheMesh = new THREE.Mesh( geometry, doubleSideMaterial ) ; 142 | latheMesh.scale.set(1500,1500,1500); //scale object to be visible at planet scale 143 | latheMesh.position.z += 15000.0; // translate "up" in Three.js space so the "bottom" of the mesh is the handle 144 | latheMesh.rotation.x = Math.PI / 2; // rotate mesh for Cesium's Y-up system 145 | var latheMeshYup = new THREE.Group(); 146 | latheMeshYup.add(latheMesh) 147 | three.scene.add(latheMeshYup); // don’t forget to add it to the Three.js scene manually 148 | 149 | //Assign Three.js object mesh to our object array 150 | var _3DOB = new _3DObject(); 151 | _3DOB.threeMesh = latheMeshYup; 152 | _3DOB.minWGS84 = minWGS84; 153 | _3DOB.maxWGS84 = maxWGS84; 154 | _3Dobjects.push(_3DOB); 155 | 156 | // dodecahedron 157 | geometry = new THREE.DodecahedronGeometry(); 158 | var dodecahedronMesh = new THREE.Mesh(geometry, new THREE.MeshNormalMaterial()) ; 159 | dodecahedronMesh.scale.set(5000,5000,5000); //scale object to be visible at planet scale 160 | dodecahedronMesh.position.z += 15000.0; // translate "up" in Three.js space so the "bottom" of the mesh is the handle 161 | dodecahedronMesh.rotation.x = Math.PI / 2; // rotate mesh for Cesium's Y-up system 162 | var dodecahedronMeshYup = new THREE.Group(); 163 | dodecahedronMeshYup.add(dodecahedronMesh) 164 | three.scene.add(dodecahedronMeshYup); // don’t forget to add it to the Three.js scene manually 165 | 166 | //Assign Three.js object mesh to our object array 167 | _3DOB = new _3DObject(); 168 | _3DOB.threeMesh = dodecahedronMeshYup; 169 | _3DOB.minWGS84 = minWGS84; 170 | _3DOB.maxWGS84 = maxWGS84; 171 | _3Dobjects.push(_3DOB); 172 | } 173 | 174 | // Looping Renderer 175 | function renderCesium(){ 176 | cesium.viewer.render(); 177 | } 178 | 179 | function renderThreeObj(){ 180 | // register Three.js scene with Cesium 181 | three.camera.fov = CesiumMath.toDegrees(cesium.viewer.camera.frustum.fovy) // ThreeJS FOV is vertical 182 | three.camera.updateProjectionMatrix(); 183 | 184 | var cartToVec = function(cart){ 185 | return new THREE.Vector3(cart.x, cart.y, cart.z); 186 | }; 187 | 188 | // Configure Three.js meshes to stand against globe center position up direction 189 | for(var id in _3Dobjects){ 190 | minWGS84 = _3Dobjects[id].minWGS84; 191 | maxWGS84 = _3Dobjects[id].maxWGS84; 192 | // convert lat/long center position to Cartesian3 193 | var center = CesiumCartesian3.fromDegrees((minWGS84[0] + maxWGS84[0]) / 2, (minWGS84[1] + maxWGS84[1]) / 2); 194 | 195 | // get forward direction for orienting model 196 | var centerHigh = CesiumCartesian3.fromDegrees((minWGS84[0] + maxWGS84[0]) / 2, (minWGS84[1] + maxWGS84[1]) / 2,1); 197 | 198 | // use direction from bottom left to top left as up-vector 199 | var bottomLeft = cartToVec(CesiumCartesian3.fromDegrees(minWGS84[0], minWGS84[1])); 200 | var topLeft = cartToVec(CesiumCartesian3.fromDegrees(minWGS84[0], maxWGS84[1])); 201 | var latDir = new THREE.Vector3().subVectors(bottomLeft,topLeft ).normalize(); 202 | 203 | // configure entity position and orientation 204 | _3Dobjects[id].threeMesh.position.copy(center); 205 | _3Dobjects[id].threeMesh.lookAt(centerHigh); 206 | _3Dobjects[id].threeMesh.up.copy(latDir); 207 | } 208 | 209 | // Clone Cesium Camera projection position so the 210 | // Three.js Object will appear to be at the same place as above the Cesium Globe 211 | three.camera.matrixAutoUpdate = false; 212 | var cvm = cesium.viewer.camera.viewMatrix; 213 | var civm = cesium.viewer.camera.inverseViewMatrix; 214 | three.camera.matrixWorld.set( 215 | civm[0], civm[4], civm[8 ], civm[12], 216 | civm[1], civm[5], civm[9 ], civm[13], 217 | civm[2], civm[6], civm[10], civm[14], 218 | civm[3], civm[7], civm[11], civm[15] 219 | ); 220 | three.camera.matrixWorldInverse.set( 221 | cvm[0], cvm[4], cvm[8 ], cvm[12], 222 | cvm[1], cvm[5], cvm[9 ], cvm[13], 223 | cvm[2], cvm[6], cvm[10], cvm[14], 224 | cvm[3], cvm[7], cvm[11], cvm[15] 225 | ); 226 | three.camera.lookAt(new THREE.Vector3(0,0,0)); 227 | 228 | var width = ThreeContainer.clientWidth; 229 | var height = ThreeContainer.clientHeight; 230 | var aspect = width / height; 231 | three.camera.aspect = aspect; 232 | three.camera.updateProjectionMatrix(); 233 | 234 | three.renderer.setSize(width, height); 235 | three.renderer.render(three.scene, three.camera); 236 | } 237 | 238 | function loop(){ 239 | requestAnimationFrame(loop); 240 | renderCesium(); 241 | renderThreeObj(); 242 | } 243 | 244 | initCesium(); // Initialize Cesium renderer 245 | initThree(); // Initialize Three.js renderer 246 | init3DObject(); // Initialize Three.js object mesh with Cesium Cartesian coordinate system 247 | loop(); // Looping renderer 248 | }); 249 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------