diff options
author | binji@chromium.org <binji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-07 23:44:00 +0000 |
---|---|---|
committer | binji@chromium.org <binji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-07 23:44:00 +0000 |
commit | 27a082491692af9b1aacb01522097a14140bb64f (patch) | |
tree | 75da5a8710030ee3b4f346f86d21485452f124d6 /native_client_sdk/src/gonacl_appengine | |
parent | 92b97b45f6a9f2fc2dec17c0b66dc10ecf71a618 (diff) | |
download | chromium_src-27a082491692af9b1aacb01522097a14140bb64f.zip chromium_src-27a082491692af9b1aacb01522097a14140bb64f.tar.gz chromium_src-27a082491692af9b1aacb01522097a14140bb64f.tar.bz2 |
[NaCl SDK] Some improvements to the PNaCl Bullet demo.
* Uses similar layout to pnacl-earth-demo
* Shares CSS in /static/common.css
* Automatically loads the 20 block tower.
BUG=none
R=jvoung@chromium.org, sbc@chromium.org, eliben@chromium.org
Review URL: https://codereview.chromium.org/26176009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@227389 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'native_client_sdk/src/gonacl_appengine')
6 files changed, 267 insertions, 241 deletions
diff --git a/native_client_sdk/src/gonacl_appengine/static/common.css b/native_client_sdk/src/gonacl_appengine/static/common.css new file mode 100644 index 0000000..e64fd0d --- /dev/null +++ b/native_client_sdk/src/gonacl_appengine/static/common.css @@ -0,0 +1,97 @@ +/* Copyright (c) 2013 The Chromium Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. */ + +body { + /* from http://css-tricks.com/snippets/css/better-helvetica/ */ + font-family: "HelveticaNeue-Light", "Helvetica Neue Light", + "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; + font-weight: 300; + overflow: hidden; +} +.absolute-fill-parent { + position: relative; +} +.absolute-fill { + bottom: 0; + left: 0; + position: absolute; + right: 0; + top: 0; +} +.flex-container { + margin: 0; + display: -moz-box; + display: -ms-flexbox; + display: -webkit-box; + display: -webkit-flex; + display: flex; + width: 100%; + height: 100%; +} +.main { + background-color: #000; + -moz-box-flex: 1; + -ms-flex: 1; + -webkit-box-flex: 1; + -webkit-flex: 1; + flex: 1; + position: relative; + width: 100%; + height: 100%; +} +.flex-column { + -moz-box-orient: vertical; + -ms-flex-direction: column; + -webkit-box-orient: vertical; + -webkit-flex-direction: column; + flex-direction: column; +} +.flex-justify-center { + -moz-box-pack: center; + -ms-flex-pack: center; + -webkit-box-pack: center; + -webkit-justify-content: center; + justify-content: center; +} +#message { + text-align: center; + width: 100%; +} +#statusField { + display: none; + color: #fff; + font-size: 34px; + text-align: center; + width: 100%; +} +#progress { + display: none; + margin: auto; + width: 300px; +} +#nacl_module { + height: 100%; + width: 100%; +} +.sidebar { + padding: 0 8px; + overflow-y: auto; + width: 350px; +} +#config { + width: 100%; +} +#config .name { + font-weight: bold; + padding-right: 8px; + text-align: right; + white-space: nowrap; + width: 1px; +} +#threadCount, #lightRange, #zoomRange { + width: 100%; +} +.small { + font-size: 12px; +} diff --git a/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/NaClAMBullet.js b/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/NaClAMBullet.js index 6164c36..7169f5c 100644 --- a/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/NaClAMBullet.js +++ b/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/NaClAMBullet.js @@ -36,7 +36,7 @@ function NaClAMBulletStepSceneHandler(msg) { return; } var simTime = msg.header.simtime; - document.getElementById('simulationTime').innerHTML = '<p>Simulation time: ' + simTime + ' microseconds</p>'; + document.getElementById('simulationTime').textContent = simTime; TransformBuffer = new Float32Array(msg.frames[0]); numTransforms = TransformBuffer.length/16; for (i = 0; i < numTransforms; i++) { diff --git a/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/index.html b/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/index.html index f153959..3ab6eab 100644 --- a/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/index.html +++ b/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/index.html @@ -5,22 +5,84 @@ Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. --> <head> - <title>NaCl AM Bullet</title> + <title>Bullet Physics</title> <script type="text/javascript" src="NaClAM.js"></script> <script type="text/javascript" src="NaClAMBullet.js"></script> <script type="text/javascript" src="http://commondatastorage.googleapis.com/gonacl/pnacl-demo-bullet/three.min.js"></script> <script type="text/javascript" src="scene.js"></script> <script type="text/javascript" src="world.js"></script> <script type="text/javascript" src="scenes.js"></script> + <link href="/static/common.css" rel="stylesheet" type="text/css"> + <style> + .main { + background-color: #fff; + } + #curtain { + background-color: rgba(0, 0, 0, 1.0); + } + #curtain.lift { + -webkit-transition: background-color 1s; + background-color: rgba(0, 0, 0, 0); + pointer-events: none; + } + .no-pointer-events { + pointer-events: none; + } + button { + display: block; + } + </style> </head> <body> - <div id="progress_div"> - Load Progress: - <progress id="progress"></progress> + <div class="absolute-fill"> + <div class="flex-container"> + <div class="main absolute-fill-parent"> + <div id="rendererContainer" class="absolute-fill"></div> + <div id="curtain" class="absolute-fill"></div> + <div class="absolute-fill no-pointer-events"> + <div class="flex-container flex-column flex-justify-center"> + <div id="message"> + <div id="statusField"></div> + <progress id="progress"></progress> + </div> + </div> + </div> + </div> + <div class="sidebar"> + <h1>Bullet Physics</h1> + <div id="config"> + <button id="jenga10">Block Tower (10 blocks)</button> + <button id="jenga20">Block Tower (20 blocks)</button> + <button id="randomShapes">Random Shapes</button> + <button id="randomCube250">Random Cubes (250)</button> + <button id="randomCylinder500">Random Cylinders (500)</button> + <button id="randomCube1000">Random Cubes (1000)</button> + <button id="randomCube2000">Random Cubes (2000)</button> + <button id="reload">Reload Scene</button> + </div> + <ul> + <li>Hold "h" to pick up an object</li> + <li>Click and drag to rotate the camera</li> + <li>Use the mousewheel to zoom in/out</li> + </ul> + <p class="small"> + More info about Bullet Physics can be found at <a + href="http://bulletphysics.org/">http://bulletphysics.org</a>. + </p> + <p class="small"> + This demo was implemented by <a + href="http://www.johnmccutchan.com">John McCutchan</a>. Read his + blog post about Native Client acceleration modules <a + href="http://www.johnmccutchan.com/2012/10/bullet-native-client-acceleration-module.html">here</a>, + and see the source for this demo <a + href="https://github.com/johnmccutchan/NaClAMBase">here</a>. + </p> + <p class="small"> + Simulation time: <span id="simulationTime">0</span>μs. + </p> + </div> + </div> </div> - <embed name="nacl_module" id="NaClAM" width=1 height=1 - src="http://commondatastorage.googleapis.com/gonacl/pnacl-demo-bullet/NaClAMBullet.portable.nmf" - type="application/x-pnacl" /> </body> <script type="text/javascript" src="main.js"></script> </html> diff --git a/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/main.js b/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/main.js index c66b213..b84c2ac 100644 --- a/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/main.js +++ b/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/main.js @@ -2,29 +2,25 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -NaClAMModule = null; aM = null; -statusText = 'NO-STATUS'; -startTimes = []; -endTimes = []; +function moduleLoad() { + hideStatus(); + init(); + animate(); + NaClAMBulletInit(); + loadJenga20(); -function moduleLoadStart() { - startTimes.push(new Date().getTime()); + document.querySelector('#curtain').classList.add('lift'); } -function moduleLoad() { - var bar = document.getElementById('progress'); - bar.value = 100; - bar.max = 100; - var bar_div = document.getElementById('progress_div'); - bar_div.innerHTML = 'Loaded!'; - - endTimes.push(new Date().getTime()); - console.log('loaded in ' + getLastLoadTime()); +function moduleLoadError() { + updateStatus('Load failed.'); } function moduleLoadProgress(event) { + $('progress').style.display = 'block'; + var loadPercent = 0.0; var bar = document.getElementById('progress'); if (event.lengthComputable && event.total > 0) { @@ -37,23 +33,50 @@ function moduleLoadProgress(event) { } } -function getLastLoadTime() { - return endTimes[endTimes.length - 1] - startTimes[startTimes.length - 1]; +function moduleCrash(event) { + if (naclModule.exitStatus == -1) { + updateStatus('CRASHED'); + } else { + updateStatus('EXITED [' + naclModule.exitStatus + ']'); + } +} + +function updateStatus(opt_message) { + document.querySelector('#curtain').classList.remove('lift'); + + var statusField = $('statusField'); + if (statusField) { + statusField.style.display = 'block'; + statusField.textContent = opt_message; + } +} + +function hideStatus() { + $('statusField').style.display = 'none'; + $('progress').style.display = 'none'; } function pageDidLoad() { + updateStatus('Loading...'); console.log('started'); aM = new NaClAM('NaClAM'); aM.enable(); - var AMDiv = document.getElementById('NaClAM'); - AMDiv.addEventListener("loadstart", moduleLoadStart, false); - AMDiv.addEventListener("progress", moduleLoadProgress, false); - AMDiv.addEventListener("load", moduleLoad, false); - init(); - animate(); - NaClAMBulletInit(); - NaClAMBulletLoadScene(sceneDescription); + + var embedWrap = document.createElement('div'); + embedWrap.addEventListener('load', moduleLoad, true); + embedWrap.addEventListener('error', moduleLoadError, true); + embedWrap.addEventListener('progress', moduleLoadProgress, true); + embedWrap.addEventListener('crash', moduleCrash, true); + document.body.appendChild(embedWrap); + + var embed = document.createElement('embed'); + embed.setAttribute('id', 'NaClAM'); + embed.setAttribute('width', '0'); + embed.setAttribute('height', '0'); + embed.setAttribute('type', 'application/x-pnacl'); + embed.setAttribute('src', 'http://commondatastorage.googleapis.com/gonacl/pnacl-demo-bullet/NaClAMBullet.portable.nmf'); + embedWrap.appendChild(embed); } window.addEventListener("load", pageDidLoad, false); diff --git a/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/scene.js b/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/scene.js index 8ff9826..6740155 100644 --- a/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/scene.js +++ b/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-bullet/scene.js @@ -142,18 +142,21 @@ function reloadScene() { loadWorld(lastSceneDescription); } +function $(id) { + return document.getElementById(id); +} + function init() { - container = document.createElement( 'div' ); - document.body.appendChild( container ); + var rendererContainer = $('rendererContainer'); + var rcW = rendererContainer.clientWidth; + var rcH = rendererContainer.clientHeight; camera = new THREE.PerspectiveCamera( 70, - window.innerWidth / window.innerHeight, 1, 10000); + rcW / rcH, 1, 10000); camera.position.y = 20.0; camera.position.z = 40; - - scene = new THREE.Scene(); scene.add( new THREE.AmbientLight( 0x505050 ) ); @@ -182,121 +185,53 @@ function init() { renderer = new THREE.WebGLRenderer( { antialias: true } ); renderer.sortObjects = false; - renderer.setSize( window.innerWidth, window.innerHeight ); + renderer.setSize( rcW, rcH ); + lastRendererWidth = rcW; + lastRendererWidth = rcH; renderer.shadowMapEnabled = true; renderer.shadowMapSoft = true; - container.appendChild( renderer.domElement ); - - /* - controls = new THREE.TrackballControls( camera ); - controls.rotateSpeed = 1.0; - controls.zoomSpeed = 5.0; - controls.panSpeed = 0.8; - controls.noZoom = false; - controls.noPan = false; - controls.staticMoving = true; - controls.dynamicDampingFactor = 0.3; - controls.handleResize(); - */ - controls = new THREE.OrbitControls (camera, container); - - var infoBar = document.createElement ('div'); - infoBar.style.position = 'absolute'; - infoBar.style.top = '10px'; - infoBar.style.width = '100%'; - infoBar.style.textAlign = 'center'; - container.appendChild( infoBar ); - - var info = document.createElement('div'); - info.innerHTML = '<a href="https://github.com/johnmccutchan/NaClAMBase" target="_blank">NaClAMBase</a> NaClAM - Bullet<p>Hold "h" to pick up an object</p>'; - infoBar.appendChild(info); - - var info = document.createElement('div'); - var innerInfoBar = document.createElement('div'); - info.innerHTML = '<a href="http://www.johnmccutchan.com/2012/10/bullet-native-client-acceleration-module.html">What am I looking at?</a>'; - innerInfoBar.appendChild(info); - infoBar.appendChild(innerInfoBar); - - var info = document.createElement('div'); - info.setAttribute("id", "simulationTime"); - info.innerHTML = '<p>Simulation time: 0 microseconds</p>'; - infoBar.appendChild(info); - - info = document.createElement ( 'div' ); - infoBar.appendChild(info); - - demoButton = document.createElement ('button'); - demoButton.innerHTML = 'Jenga 10'; - demoButton.addEventListener('click', loadJenga10, false); - info.appendChild(demoButton); - - demoButton = document.createElement ('button'); - demoButton.innerHTML = 'Jenga 20'; - demoButton.addEventListener('click', loadJenga20, false); - info.appendChild(demoButton); - - demoButton = document.createElement ('button'); - demoButton.innerHTML = 'Random Shapes'; - demoButton.addEventListener('click', loadRandomShapes, false); - info.appendChild(demoButton); - - demoButton = document.createElement ('button'); - demoButton.innerHTML = '250 Random Cubes'; - demoButton.addEventListener('click', load250RandomCubes, false); - info.appendChild(demoButton); - - demoButton = document.createElement ('button'); - demoButton.innerHTML = '500 Random Cylinders'; - demoButton.addEventListener('click', load500RandomCylinders, false); - info.appendChild(demoButton); - - demoButton = document.createElement ('button'); - demoButton.innerHTML = '1000 Random Cubes'; - demoButton.addEventListener('click', load1000RandomCubes, false); - info.appendChild(demoButton); - - demoButton = document.createElement ('button'); - demoButton.innerHTML = '2000 Random Cubes'; - demoButton.addEventListener('click', load2000RandomCubes, false); - info.appendChild(demoButton); - - demoButton = document.createElement('input'); - demoButton.innerHTML = 'Upload Scene'; - demoButton.setAttribute("type", "file"); - demoButton.setAttribute("id", "UploadButton"); - var uploadButton = demoButton; - demoButton.addEventListener('change', function(evt) { - var files = evt.target.files; - if (evt.target.files.length <= 0) { - return; - } - var f = files[0]; - var reader = new FileReader(); - reader.addEventListener('load', loadTextScene); - reader.readAsText(f, "UTF-8"); - uploadButton.setAttribute("value", ""); - }); - info.appendChild(demoButton); - - demoButton = document.createElement ('button'); - demoButton.innerHTML = 'Reload Scene'; - demoButton.addEventListener('click', reloadScene, false); - info.appendChild(demoButton); + rendererContainer.appendChild(renderer.domElement); + + controls = new THREE.OrbitControls (camera, rendererContainer); + + var idFuncHash = { + jenga10: loadJenga10, + jenga20: loadJenga20, + randomShapes: loadRandomShapes, + randomCube250: load250RandomCubes, + randomCylinder500: load500RandomCylinders, + randomCube1000: load1000RandomCubes, + randomCube2000: load2000RandomCubes + }; + + for (var id in idFuncHash) { + var func = idFuncHash[id]; + $(id).addEventListener('click', func, false); + } + + $('reload').addEventListener('click', reloadScene, false); renderer.domElement.addEventListener('mousemove', onDocumentMouseMove, false ); - window.addEventListener('resize', onWindowResize, false ); window.addEventListener('keydown', onDocumentKeyDown, false); window.addEventListener('keyup', onDocumentKeyUp, false); + + window.setInterval(pollForRendererResize, 10); } -function onWindowResize() { - console.log('onWindowResize w: ' + window.innerWidth + ' h: ' + window.innerHeight); - camera.aspect = window.innerWidth / window.innerHeight; +function pollForRendererResize() { + var rendererContainer = $('rendererContainer'); + var w = rendererContainer.clientWidth; + var h = rendererContainer.clientHeight; + if (w == lastRendererWidth && h == lastRendererHeight) + return; + + camera.aspect = w / h; camera.updateProjectionMatrix(); - renderer.setSize( window.innerWidth, window.innerHeight ); - //controls.handleResize(); + renderer.setSize( w, h ); + lastRendererWidth = w; + lastRendererHeight = h; } function onDocumentKeyDown(event) { @@ -329,8 +264,13 @@ function onDocumentKeyUp(event) { function onDocumentMouseMove( event ) { event.preventDefault(); - mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1; - mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1; + + var rendererContainer = $('rendererContainer'); + var w = rendererContainer.clientWidth; + var h = rendererContainer.clientHeight; + + mouse.x = ( event.clientX / w ) * 2 - 1; + mouse.y = - ( event.clientY / h ) * 2 + 1; var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 ); projector.unprojectVector( vector, camera ); offset.x = vector.x; diff --git a/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-earth/index.html b/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-earth/index.html index ad6922e..bb01c1d 100644 --- a/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-earth/index.html +++ b/native_client_sdk/src/gonacl_appengine/static/pnacl-demo-earth/index.html @@ -6,106 +6,10 @@ Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. --> <head> - <meta http-equiv="Pragma" content="no-cache"> - <meta http-equiv="Expires" content="-1"> <meta charset="UTF-8"> <title>Multi-Threaded Raycasted Earth</title> <script type="text/javascript" src="example.js"></script> - <style> - body { - /* from http://css-tricks.com/snippets/css/better-helvetica/ */ - font-family: "HelveticaNeue-Light", "Helvetica Neue Light", - "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; - font-weight: 300; - overflow: hidden; - } - .absolute-fill-parent { - position: relative; - } - .absolute-fill { - bottom: 0; - left: 0; - position: absolute; - right: 0; - top: 0; - } - .flex-container { - margin: 0; - display: -moz-box; - display: -ms-flexbox; - display: -webkit-box; - display: -webkit-flex; - display: flex; - width: 100%; - height: 100%; - } - .main { - background-color: #000; - -moz-box-flex: 1; - -ms-flex: 1; - -webkit-box-flex: 1; - -webkit-flex: 1; - flex: 1; - position: relative; - width: 100%; - height: 100%; - } - .flex-column { - -moz-box-orient: vertical; - -ms-flex-direction: column; - -webkit-box-orient: vertical; - -webkit-flex-direction: column; - flex-direction: column; - } - .flex-justify-center { - -moz-box-pack: center; - -ms-flex-pack: center; - -webkit-box-pack: center; - -webkit-justify-content: center; - justify-content: center; - } - #message { - text-align: center; - width: 100%; - } - #statusField { - display: none; - color: #fff; - font-size: 34px; - text-align: center; - width: 100%; - } - #progress { - display: none; - margin: auto; - width: 300px; - } - #nacl_module { - height: 100%; - width: 100%; - } - .sidebar { - padding: 0 8px; - overflow-y: auto; - width: 350px; - } - #config { - width: 100%; - } - #config .name { - font-weight: bold; - padding-right: 8px; - text-align: right; - white-space: nowrap; - width: 1px; - } - #threadCount, #lightRange, #zoomRange { - width: 100%; - } - .small { - font-size: 12px; - } - </style> + <link href="/static/common.css" rel="stylesheet" type="text/css"> </head> <body> <div class="absolute-fill"> |