From 4245bd033f2f4b24dfd7d9d66e21f24585f29d12 Mon Sep 17 00:00:00 2001 From: mccannd Date: Sun, 26 Mar 2017 19:52:35 -0400 Subject: [PATCH 1/4] basic implementation --- .gitignore | 2 + index.html | 19 +++ npm-debug.log | 47 ++++++++ package.json | 29 +++++ src/framework.js | 75 ++++++++++++ src/main.js | 299 ++++++++++++++++++++++++++++++++++++++++++++++ webpack.config.js | 28 +++++ 7 files changed, 499 insertions(+) create mode 100644 .gitignore create mode 100644 index.html create mode 100644 npm-debug.log create mode 100644 package.json create mode 100644 src/framework.js create mode 100644 src/main.js create mode 100644 webpack.config.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..8d4aaf40 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +*.map \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 00000000..f775186a --- /dev/null +++ b/index.html @@ -0,0 +1,19 @@ + + + + HW1: Noise + + + + + + \ No newline at end of file diff --git a/npm-debug.log b/npm-debug.log new file mode 100644 index 00000000..36998be0 --- /dev/null +++ b/npm-debug.log @@ -0,0 +1,47 @@ +0 info it worked if it ends with ok +1 verbose cli [ 'D:\\nodejs\\node.exe', +1 verbose cli 'D:\\nodejs\\node_modules\\npm\\bin\\npm-cli.js', +1 verbose cli 'start' ] +2 info using npm@3.10.10 +3 info using node@v6.9.4 +4 verbose run-script [ 'prestart', 'start', 'poststart' ] +5 info lifecycle @~prestart: @ +6 silly lifecycle @~prestart: no script for prestart, continuing +7 info lifecycle @~start: @ +8 verbose lifecycle @~start: unsafe-perm in lifecycle true +9 verbose lifecycle @~start: PATH: D:\nodejs\node_modules\npm\bin\node-gyp-bin;D:\Procedural\Project7-BioCrowds\node_modules\.bin;C:\Users\Daniel\AppData\Local\GitHub\PortableGit_f02737a78695063deace08e96d5042710d3e32db\cmd;C:\Users\Daniel\AppData\Local\GitHub\PortableGit_f02737a78695063deace08e96d5042710d3e32db\usr\bin;C:\Users\Daniel\AppData\Local\GitHub\PortableGit_f02737a78695063deace08e96d5042710d3e32db\usr\share\git-tfs;C:\Users\Daniel\AppData\Local\Apps\2.0\YOA1XVNR.8H2\8AP2TGYV.2O2\gith..tion_317444273a93ac29_0003.0003_5794af8169eeff14;C:\Users\Daniel\AppData\Local\GitHub\lfs-amd64_1.5.5;C:\Users\Daniel\AppData\Local\Apps\2.0\YOA1XVNR.8H2\8AP2TGYV.2O2\gith..tion_317444273a93ac29_0003.0003_5794af8169eeff14\NativeBinaries\x86;C:\Users\Daniel\introcs\java\bin;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\nodejs\;D:\MinGW\bin;C:\Users\Daniel\introcs\j3d\bin;C:\Users\Daniel\introcs\bin;C:\Users\Daniel\introcs\java\bin;D:\Qt\5.5\mingw492_32\bin;C:\Users\Daniel\AppData\Local\Microsoft\WindowsApps;C:\Users\Daniel\AppData\Roaming\npm;C:\Program Files (x86)\MSBuild\14.0\bin\;C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64;C:\Program Files (x86)\Microsoft SDKs\F#\3.1\Framework\v4.0\ +10 verbose lifecycle @~start: CWD: D:\Procedural\Project7-BioCrowds +11 silly lifecycle @~start: Args: [ '/d /s /c', 'webpack-dev-server --hot --inline' ] +12 silly lifecycle @~start: Returned: code: 3221225786 signal: null +13 info lifecycle @~start: Failed to exec start script +14 verbose stack Error: @ start: `webpack-dev-server --hot --inline` +14 verbose stack Exit status 3221225786 +14 verbose stack at EventEmitter. (D:\nodejs\node_modules\npm\lib\utils\lifecycle.js:255:16) +14 verbose stack at emitTwo (events.js:106:13) +14 verbose stack at EventEmitter.emit (events.js:191:7) +14 verbose stack at ChildProcess. (D:\nodejs\node_modules\npm\lib\utils\spawn.js:40:14) +14 verbose stack at emitTwo (events.js:106:13) +14 verbose stack at ChildProcess.emit (events.js:191:7) +14 verbose stack at maybeClose (internal/child_process.js:877:16) +14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5) +15 verbose pkgid @ +16 verbose cwd D:\Procedural\Project7-BioCrowds +17 error Windows_NT 10.0.14393 +18 error argv "D:\\nodejs\\node.exe" "D:\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start" +19 error node v6.9.4 +20 error npm v3.10.10 +21 error code ELIFECYCLE +22 error @ start: `webpack-dev-server --hot --inline` +22 error Exit status 3221225786 +23 error Failed at the @ start script 'webpack-dev-server --hot --inline'. +23 error Make sure you have the latest version of node.js and npm installed. +23 error If you do, this is most likely a problem with the package, +23 error not with npm itself. +23 error Tell the author that this fails on your system: +23 error webpack-dev-server --hot --inline +23 error You can get information on how to open an issue for this project with: +23 error npm bugs +23 error Or if that isn't available, you can get their info via: +23 error npm owner ls +23 error There is likely additional logging output above. +24 verbose exit [ 1, true ] diff --git a/package.json b/package.json new file mode 100644 index 00000000..370fadd3 --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "scripts": { + "start": "webpack-dev-server --hot --inline", + "build": "webpack", + "deploy": "gh-pages-deploy" + }, + "gh-pages-deploy": { + "prep": [ + "build" + ], + "noprompt": true + }, + "dependencies": { + "dat-gui": "^0.5.0", + "gl-matrix": "^2.3.2", + "stats-js": "^1.0.0-alpha1", + "three": "^0.82.1", + "three-orbit-controls": "^82.1.0" + }, + "devDependencies": { + "babel-core": "^6.18.2", + "babel-loader": "^6.2.8", + "babel-preset-es2015": "^6.18.0", + "gh-pages-deploy": "^0.4.2", + "webpack": "^1.13.3", + "webpack-dev-server": "^1.16.2", + "webpack-glsl-loader": "^1.0.1" + } +} diff --git a/src/framework.js b/src/framework.js new file mode 100644 index 00000000..9cfcd1b4 --- /dev/null +++ b/src/framework.js @@ -0,0 +1,75 @@ + +const THREE = require('three'); +const OrbitControls = require('three-orbit-controls')(THREE) +import Stats from 'stats-js' +import DAT from 'dat-gui' + +// when the scene is done initializing, the function passed as `callback` will be executed +// then, every frame, the function passed as `update` will be executed +function init(callback, update) { + var stats = new Stats(); + stats.setMode(1); + stats.domElement.style.position = 'absolute'; + stats.domElement.style.left = '0px'; + stats.domElement.style.top = '0px'; + document.body.appendChild(stats.domElement); + + var gui = new DAT.GUI(); + + var framework = { + gui: gui, + stats: stats + }; + + // run this function after the window loads + window.addEventListener('load', function() { + + var scene = new THREE.Scene(); + var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 ); + var renderer = new THREE.WebGLRenderer( { antialias: true } ); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setClearColor(0x020202, 0); + + var controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + controls.enableZoom = true; + controls.target.set(0, 0, 0); + controls.rotateSpeed = 0.3; + controls.zoomSpeed = 1.0; + controls.panSpeed = 2.0; + + document.body.appendChild(renderer.domElement); + + // resize the canvas when the window changes + window.addEventListener('resize', function() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + renderer.setSize(window.innerWidth, window.innerHeight); + }); + + // assign THREE.js objects to the object we will return + framework.scene = scene; + framework.camera = camera; + framework.renderer = renderer; + + // begin the animation loop + (function tick() { + stats.begin(); + update(framework); // perform any requested updates + renderer.render(scene, camera); // render the scene + stats.end(); + requestAnimationFrame(tick); // register to call this again when the browser renders a new frame + })(); + + // we will pass the scene, gui, renderer, camera, etc... to the callback function + return callback(framework); + }); +} + +export default { + init: init +} + +export const PI = 3.14159265 +export const e = 2.7181718 \ No newline at end of file diff --git a/src/main.js b/src/main.js new file mode 100644 index 00000000..d91d1597 --- /dev/null +++ b/src/main.js @@ -0,0 +1,299 @@ + +const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much +import Framework from './framework' + +var clock = new THREE.Clock(); +var t = 0.0; + +var grid = []; +var agents = []; +var markers = []; +var settings = { + minMarkersPerGrid: 1, + maxMarkersPerGrid: 1, + markersResolution: 2, + gridResolution: 40, + agentRadius: 0.5, + numAgents: 20, + displayMarkers: true +}; + + +// returns a pair of ints to access grid +function coordToGrid(x, y) { + return new THREE.Vector2(Math.floor((x + 5.0) * settings.gridResolution / 10.0), Math.floor((y + 5.0) * settings.gridResolution / 10.0)); +} + +// returns a centerpoint of the grid +function gridToCoord(x, y) { + return new THREE.Vector2((x + 0.5) * 10.0 / settings.gridResolution - 5.0, (y + 0.5) * 10.0 / settings.gridResolution - 5.0) +} + +var Agent = function(pos, vel, tgt) { + return { + position: new THREE.Vector3(pos.x, pos.y, pos.z), + velocity: new THREE.Vector3(vel.x, vel.y, vel.z), + target: new THREE.Vector3(tgt.x, tgt.y, tgt.z) + } +} + +var Marker = function(pos, agent) { + return { + pos: new THREE.Vector3(pos.x, pos.y, pos.z), + agent: null + } +} + +function GridSquare(m, a) { + this.markers = m; + this.agents = a; +} + +function generateGrid() { + grid = []; // reset grid + + for (var i = 0; i < settings.gridResolution; i++) { + var row = []; + for (var j = 0; j < settings.gridResolution; j++) { + var square = new GridSquare([], []); + var numMarkers = Math.floor(Math.random() * + (1 + settings.maxMarkersPerGrid - settings.minMarkersPerGrid)) + settings.minMarkersPerGrid; + var center = gridToCoord(i, j); + for (var m = 0; m < numMarkers; m++) { + var shift = new THREE.Vector2((Math.random() - 0.5) * 10.0 / settings.gridResolution, + (Math.random() - 0.5) * 10.0 / settings.gridResolution); + shift = shift.addVectors(shift, center); + var marker = new Marker(new THREE.Vector3(shift.x, 0, shift.y), null); + square.markers.push(marker); + markers.push(marker); + } + //console.log(square); + row.push(square); + } + grid.push(row); + //console.log(row); + } +} + +function generateAgents(scene) { + agents = []; // reset agents + + var agentsMat = new THREE.MeshPhongMaterial( { + side: THREE.DoubleSide, + color: 0xaa1111 + }); + var agentsGeo = new THREE.SphereGeometry(0.1); + + for (var i = 0; i < settings.numAgents; i++) { + var theta = Math.PI * 2 * i / settings.numAgents; + var p = (new THREE.Vector3(Math.cos(theta), 0.1, Math.sin(theta))).multiplyScalar(4.5); + var t = (new THREE.Vector3(Math.cos(theta + Math.PI), 0.1, Math.sin(theta + Math.PI))).multiplyScalar(4.5); + var cell = coordToGrid(p.x, p.z); + + var agent = new Agent(p, new THREE.Vector3(0,0,0), t); + var aMesh = new THREE.Mesh(agentsGeo, agentsMat); + aMesh.userData.agentID = i; + aMesh.position.set(p.x, 0.1, p.z); + scene.add(aMesh); + agents.push(agent); + grid[cell.x][cell.y].agents.push(agent); + } +} + +function generateAgentsRows(scene) { + agents = []; // reset agents + + var agentsMat = new THREE.MeshPhongMaterial( { + side: THREE.DoubleSide, + color: 0xaa1111 + }); + var agentsGeo = new THREE.SphereGeometry(0.1); + + for (var i = 0; i < settings.numAgents; i++) { + var sign = i % 2 == 0; + var s = sign?1:-1; + var p = new THREE.Vector3(i / settings.numAgents * 9 - 4.5, 0.1, s * -4.5); + var t = new THREE.Vector3(i / settings.numAgents * 9 - 4.5, 0.1, s * 4.5); + var cell = coordToGrid(p.x, p.z); + + var agent = new Agent(p, new THREE.Vector3(0,0,0), t); + var aMesh = new THREE.Mesh(agentsGeo, agentsMat); + aMesh.userData.agentID = i; + aMesh.position.set(p.x, 0.1, p.z); + scene.add(aMesh); + agents.push(agent); + grid[cell.x][cell.y].agents.push(agent); + } +} + + + +function getWeightedVel(pos, mpos, tgt) { + var toTarget = (new THREE.Vector3(0, 0, 0)).subVectors(tgt, pos); + + var toMark = (new THREE.Vector3(0, 0, 0)).subVectors(mpos, pos); + if (toTarget.length < toMark.length) { + return new THREE.Vector3(0, 0, 0); + } + + toTarget = toTarget.normalize(); + toMark = toMark.normalize(); + var alignment = toTarget.dot(toMark) * 2 + 1.0; + //alignment = 1; + var r2 = settings.agentRadius * settings.agentRadius; + var d = pos.distanceToSquared(mpos); + + d = (r2 - d) / r2; + d = d < 0 ? 0 : d; + var weight = 0.2 * d * alignment; + return toMark.multiplyScalar(weight); +} + +function updateSimulation(dt) { + + // clear all velocities + for (var a = 0; a < agents.length; a++) { + agents[a].velocity = new THREE.Vector3(0, 0, 0); + } + for (var m = 0; m < markers.length; m++) { + markers[m].agent = null; + } + + // list of active markers for this iteration + var activeMarkers = []; + var gridRadius = Math.round(settings.agentRadius * settings.gridResolution / 10.0); + //gridRadius = 1; + //console.log(gridRadius); + for (var a = 0; a < agents.length; a++) { + var g = coordToGrid(agents[a].position.x, agents[a].position.z); + //console.log(g); + for (var rx = -gridRadius; rx <= gridRadius; rx++) { + for (var ry = -gridRadius; ry <= gridRadius; ry++) { + if (rx + g.x < 0 || rx + g.x >= settings.gridResolution) continue; + if (ry + g.y < 0 || ry + g.y >= settings.gridResolution) continue; + var sq = grid[g.x + rx][g.y + ry]; + //console.log(sq); + for (var i = 0; i < sq.markers.length; i++) { + //console.log("heck"); + if (sq.markers[i].agent == null) { + sq.markers[i].agent = agents[a]; + activeMarkers.push(sq.markers[i]); + //console.log("bork"); + } else { + var currDist = sq.markers[i].pos.distanceToSquared(sq.markers[i].agent.position); + var newDist = sq.markers[i].pos.distanceToSquared(agents[a].position); + if (newDist < currDist) sq.markers[i].agent = agents[a]; + } + } + } + } + } + + //console.log(activeMarkers.length); + for (var am = 0; am < activeMarkers.length; am++) { + var ag = activeMarkers[am].agent; + + ag.velocity.add(getWeightedVel(ag.position, activeMarkers[am].pos, ag.target)); + } + + var min = new THREE.Vector3(-4.99, -5, -4.99); + var max = new THREE.Vector3(4.99, 5, 4.99); + + // update all positions + for (var a = 0; a < agents.length; a++) { + if (agents[a].velocity.length > settings.agentRadius) { + agents[a].velocity = agents[a].velocity.normalize().multiplyScalar(settings.agentRadius); + } + agents[a].position.addScaledVector(agents[a].velocity, dt); + agents[a].position.clamp(min, max) + } + + // clear cell agents + for (var i = 0; i < settings.gridResolution; i++) { + for (var j = 0; j < settings.gridResolution; j++) { + grid[i][j].agents = []; + } + } + // update cell agents + for (var a = 0; a < agents.length; a++) { + var g = coordToGrid(agents[a].position.x, agents[a].position.z); + grid[g.x][g.y].agents.push(agents[a]); + //console.log(g.x + ", " + g.y) + } +} + +// called after the scene loads +function onLoad(framework) { + var scene = framework.scene; + var camera = framework.camera; + var renderer = framework.renderer; + var gui = framework.gui; + var stats = framework.stats; + + camera.position.set(1, 1, 5); + camera.lookAt(new THREE.Vector3(0,0,0)); + + var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.9 ); + directionalLight.color.setHSL(0.1, 1, 0.95); + directionalLight.position.set(3, 2, 2); + directionalLight.position.multiplyScalar(10); + scene.add(directionalLight); + + var planeGeo = new THREE.PlaneGeometry(10, 10, 20, 20); + var planeMat = new THREE.MeshPhongMaterial( { + side: THREE.DoubleSide, + color: 0xaaaaaa + }); + planeGeo.applyMatrix( new THREE.Matrix4().makeRotationX(-Math.PI / 2.0)); + + var plane = new THREE.Mesh(planeGeo, planeMat); + scene.add(plane); + + // initialize sumulation data + generateGrid(); + console.log(grid); + generateAgentsRows(scene); + + + var marks = new THREE.Geometry(); + var marksMat = new THREE.PointsMaterial({ + color: 0xFFFFFF, + size: 0.1 + }); + for (var i = 0; i < settings.gridResolution; i++) { + for (var j = 0; j < settings.gridResolution; j++) { + var square = grid[i][j]; + for (var m = 0; m < square.markers.length; m++) { + marks.vertices.push(new THREE.Vector3(square.markers[m].pos.x, 0.0, square.markers[m].pos.z)); + } + } + } + var mMesh = new THREE.Points(marks, marksMat); + mMesh.name = "markers"; + scene.add(mMesh); + +} + +// called on frame updates +function onUpdate(framework) { + var dt = clock.getDelta(); + + if (grid.length > 0 && agents.length > 0) { + updateSimulation(dt); + framework.scene.traverse(function(object) { + if (object instanceof THREE.Mesh) { + + if ('agentID' in object.userData) { + //console.log('ech'); + var a = agents[object.userData.agentID]; + object.position.set(a.position.x, a.position.y, a.position.z); + } + } + + }); + } + +} + +// when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate +Framework.init(onLoad, onUpdate); \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 00000000..57dce485 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,28 @@ +const path = require('path'); + +module.exports = { + entry: path.join(__dirname, "src/main"), + output: { + filename: "./bundle.js" + }, + module: { + loaders: [ + { + test: /\.js$/, + exclude: /(node_modules|bower_components)/, + loader: 'babel', + query: { + presets: ['es2015'] + } + }, + { + test: /\.glsl$/, + loader: "webpack-glsl" + }, + ] + }, + devtool: 'source-map', + devServer: { + port: 7000 + } +} \ No newline at end of file From cf618e1d0d4285508b8f4461b75dc28d3f38d5ae Mon Sep 17 00:00:00 2001 From: mccannd Date: Tue, 28 Mar 2017 22:55:36 -0400 Subject: [PATCH 2/4] fixes --- src/main.js | 95 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 37 deletions(-) diff --git a/src/main.js b/src/main.js index d91d1597..4bdda11d 100644 --- a/src/main.js +++ b/src/main.js @@ -11,8 +11,8 @@ var markers = []; var settings = { minMarkersPerGrid: 1, maxMarkersPerGrid: 1, - markersResolution: 2, - gridResolution: 40, + //markersResolution: 2, + gridResolution: 80, agentRadius: 0.5, numAgents: 20, displayMarkers: true @@ -33,7 +33,8 @@ var Agent = function(pos, vel, tgt) { return { position: new THREE.Vector3(pos.x, pos.y, pos.z), velocity: new THREE.Vector3(vel.x, vel.y, vel.z), - target: new THREE.Vector3(tgt.x, tgt.y, tgt.z) + target: new THREE.Vector3(tgt.x, tgt.y, tgt.z), + markers: [] } } @@ -67,11 +68,11 @@ function generateGrid() { square.markers.push(marker); markers.push(marker); } - //console.log(square); + row.push(square); } grid.push(row); - //console.log(row); + } } @@ -128,32 +129,54 @@ function generateAgentsRows(scene) { -function getWeightedVel(pos, mpos, tgt) { +function setWeightedVel(agent) { + var pos = agent.position; + var tgt = agent.target; var toTarget = (new THREE.Vector3(0, 0, 0)).subVectors(tgt, pos); + if (toTarget.length() < 0.01) { + return; + } - var toMark = (new THREE.Vector3(0, 0, 0)).subVectors(mpos, pos); - if (toTarget.length < toMark.length) { - return new THREE.Vector3(0, 0, 0); + // sum and record all weights + var totalWeight = 0.0; + var weights = []; + var displacements = []; + for (var m = 0; m < agent.markers.length; m++) { + var mpos = agent.markers[m].pos; + var toMark = (new THREE.Vector3(0, 0, 0)).subVectors(mpos, pos); + var wf = 1.0 / (1.0 + toMark.length()); + + wf = wf * (1.0 + toMark.dot(toTarget) / toMark.length() / toTarget.length()); + totalWeight += wf; + weights.push(wf); + displacements.push(toMark); } - toTarget = toTarget.normalize(); - toMark = toMark.normalize(); - var alignment = toTarget.dot(toMark) * 2 + 1.0; - //alignment = 1; - var r2 = settings.agentRadius * settings.agentRadius; - var d = pos.distanceToSquared(mpos); - - d = (r2 - d) / r2; - d = d < 0 ? 0 : d; - var weight = 0.2 * d * alignment; - return toMark.multiplyScalar(weight); + + var totalVelocity = new THREE.Vector3(0, 0, 0); + if (totalWeight == 0.0) { + return; + } + + for (var i = 0; i < weights.length; i++) { + displacements[i].multiplyScalar(weights[i] / totalWeight); + totalVelocity.add(displacements[i]); + } + + // clamp maximum to max radial influence + if (totalVelocity.length() > settings.agentRadius) { + totalVelocity = totalVelocity.normalize().multiplyScalar(settings.agentRadius); + } + + agent.velocity = totalVelocity; } function updateSimulation(dt) { - // clear all velocities + // clear all velocities and ownership for (var a = 0; a < agents.length; a++) { agents[a].velocity = new THREE.Vector3(0, 0, 0); + agents[a].markers = []; } for (var m = 0; m < markers.length; m++) { markers[m].agent = null; @@ -162,23 +185,20 @@ function updateSimulation(dt) { // list of active markers for this iteration var activeMarkers = []; var gridRadius = Math.round(settings.agentRadius * settings.gridResolution / 10.0); - //gridRadius = 1; - //console.log(gridRadius); + for (var a = 0; a < agents.length; a++) { var g = coordToGrid(agents[a].position.x, agents[a].position.z); - //console.log(g); + for (var rx = -gridRadius; rx <= gridRadius; rx++) { for (var ry = -gridRadius; ry <= gridRadius; ry++) { if (rx + g.x < 0 || rx + g.x >= settings.gridResolution) continue; if (ry + g.y < 0 || ry + g.y >= settings.gridResolution) continue; var sq = grid[g.x + rx][g.y + ry]; - //console.log(sq); + for (var i = 0; i < sq.markers.length; i++) { - //console.log("heck"); if (sq.markers[i].agent == null) { sq.markers[i].agent = agents[a]; activeMarkers.push(sq.markers[i]); - //console.log("bork"); } else { var currDist = sq.markers[i].pos.distanceToSquared(sq.markers[i].agent.position); var newDist = sq.markers[i].pos.distanceToSquared(agents[a].position); @@ -189,11 +209,10 @@ function updateSimulation(dt) { } } - //console.log(activeMarkers.length); + // add all active markers to lists for (var am = 0; am < activeMarkers.length; am++) { var ag = activeMarkers[am].agent; - - ag.velocity.add(getWeightedVel(ag.position, activeMarkers[am].pos, ag.target)); + ag.markers.push(activeMarkers[am]); } var min = new THREE.Vector3(-4.99, -5, -4.99); @@ -201,6 +220,10 @@ function updateSimulation(dt) { // update all positions for (var a = 0; a < agents.length; a++) { + + // find velocity + setWeightedVel(agents[a]); + if (agents[a].velocity.length > settings.agentRadius) { agents[a].velocity = agents[a].velocity.normalize().multiplyScalar(settings.agentRadius); } @@ -214,11 +237,11 @@ function updateSimulation(dt) { grid[i][j].agents = []; } } + // update cell agents for (var a = 0; a < agents.length; a++) { var g = coordToGrid(agents[a].position.x, agents[a].position.z); grid[g.x][g.y].agents.push(agents[a]); - //console.log(g.x + ", " + g.y) } } @@ -242,7 +265,7 @@ function onLoad(framework) { var planeGeo = new THREE.PlaneGeometry(10, 10, 20, 20); var planeMat = new THREE.MeshPhongMaterial( { side: THREE.DoubleSide, - color: 0xaaaaaa + color: 0xdddddd }); planeGeo.applyMatrix( new THREE.Matrix4().makeRotationX(-Math.PI / 2.0)); @@ -251,14 +274,12 @@ function onLoad(framework) { // initialize sumulation data generateGrid(); - console.log(grid); - generateAgentsRows(scene); - + generateAgents(scene); var marks = new THREE.Geometry(); var marksMat = new THREE.PointsMaterial({ - color: 0xFFFFFF, - size: 0.1 + color: 0xaaddff, + size: 0.01 }); for (var i = 0; i < settings.gridResolution; i++) { for (var j = 0; j < settings.gridResolution; j++) { From a065efba382a1f0148a9ae583d9dae579a9b87c1 Mon Sep 17 00:00:00 2001 From: mccannd Date: Wed, 29 Mar 2017 01:04:39 -0400 Subject: [PATCH 3/4] gui control --- npm-debug.log | 54 ++++++++++++++++++++------------------------------- src/main.js | 21 +++++++++++++------- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/npm-debug.log b/npm-debug.log index 36998be0..2bf5f75e 100644 --- a/npm-debug.log +++ b/npm-debug.log @@ -12,36 +12,24 @@ 9 verbose lifecycle @~start: PATH: D:\nodejs\node_modules\npm\bin\node-gyp-bin;D:\Procedural\Project7-BioCrowds\node_modules\.bin;C:\Users\Daniel\AppData\Local\GitHub\PortableGit_f02737a78695063deace08e96d5042710d3e32db\cmd;C:\Users\Daniel\AppData\Local\GitHub\PortableGit_f02737a78695063deace08e96d5042710d3e32db\usr\bin;C:\Users\Daniel\AppData\Local\GitHub\PortableGit_f02737a78695063deace08e96d5042710d3e32db\usr\share\git-tfs;C:\Users\Daniel\AppData\Local\Apps\2.0\YOA1XVNR.8H2\8AP2TGYV.2O2\gith..tion_317444273a93ac29_0003.0003_5794af8169eeff14;C:\Users\Daniel\AppData\Local\GitHub\lfs-amd64_1.5.5;C:\Users\Daniel\AppData\Local\Apps\2.0\YOA1XVNR.8H2\8AP2TGYV.2O2\gith..tion_317444273a93ac29_0003.0003_5794af8169eeff14\NativeBinaries\x86;C:\Users\Daniel\introcs\java\bin;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\nodejs\;D:\MinGW\bin;C:\Users\Daniel\introcs\j3d\bin;C:\Users\Daniel\introcs\bin;C:\Users\Daniel\introcs\java\bin;D:\Qt\5.5\mingw492_32\bin;C:\Users\Daniel\AppData\Local\Microsoft\WindowsApps;C:\Users\Daniel\AppData\Roaming\npm;C:\Program Files (x86)\MSBuild\14.0\bin\;C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64;C:\Program Files (x86)\Microsoft SDKs\F#\3.1\Framework\v4.0\ 10 verbose lifecycle @~start: CWD: D:\Procedural\Project7-BioCrowds 11 silly lifecycle @~start: Args: [ '/d /s /c', 'webpack-dev-server --hot --inline' ] -12 silly lifecycle @~start: Returned: code: 3221225786 signal: null -13 info lifecycle @~start: Failed to exec start script -14 verbose stack Error: @ start: `webpack-dev-server --hot --inline` -14 verbose stack Exit status 3221225786 -14 verbose stack at EventEmitter. (D:\nodejs\node_modules\npm\lib\utils\lifecycle.js:255:16) -14 verbose stack at emitTwo (events.js:106:13) -14 verbose stack at EventEmitter.emit (events.js:191:7) -14 verbose stack at ChildProcess. (D:\nodejs\node_modules\npm\lib\utils\spawn.js:40:14) -14 verbose stack at emitTwo (events.js:106:13) -14 verbose stack at ChildProcess.emit (events.js:191:7) -14 verbose stack at maybeClose (internal/child_process.js:877:16) -14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5) -15 verbose pkgid @ -16 verbose cwd D:\Procedural\Project7-BioCrowds -17 error Windows_NT 10.0.14393 -18 error argv "D:\\nodejs\\node.exe" "D:\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start" -19 error node v6.9.4 -20 error npm v3.10.10 -21 error code ELIFECYCLE -22 error @ start: `webpack-dev-server --hot --inline` -22 error Exit status 3221225786 -23 error Failed at the @ start script 'webpack-dev-server --hot --inline'. -23 error Make sure you have the latest version of node.js and npm installed. -23 error If you do, this is most likely a problem with the package, -23 error not with npm itself. -23 error Tell the author that this fails on your system: -23 error webpack-dev-server --hot --inline -23 error You can get information on how to open an issue for this project with: -23 error npm bugs -23 error Or if that isn't available, you can get their info via: -23 error npm owner ls -23 error There is likely additional logging output above. -24 verbose exit [ 1, true ] +12 verbose stack Error: kill ENOSYS +12 verbose stack at exports._errnoException (util.js:1022:11) +12 verbose stack at process.kill (internal/process.js:172:13) +12 verbose stack at process.listener (D:\nodejs\node_modules\npm\node_modules\npmlog\node_modules\gauge\node_modules\signal-exit\index.js:86:15) +12 verbose stack at emitNone (events.js:91:20) +12 verbose stack at process.emit (events.js:185:7) +12 verbose stack at processEmit (D:\nodejs\node_modules\npm\node_modules\npmlog\node_modules\gauge\node_modules\signal-exit\index.js:146:32) +12 verbose stack at processEmit (D:\nodejs\node_modules\npm\node_modules\npm-registry-client\node_modules\npmlog\node_modules\gauge\node_modules\signal-exit\index.js:146:32) +12 verbose stack at Signal.wrap.onsignal (internal/process.js:199:44) +13 verbose cwd D:\Procedural\Project7-BioCrowds +14 error Windows_NT 10.0.14393 +15 error argv "D:\\nodejs\\node.exe" "D:\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start" +16 error node v6.9.4 +17 error npm v3.10.10 +18 error code ENOSYS +19 error errno ENOSYS +20 error syscall kill +21 error kill ENOSYS +22 error If you need help, you may report this error at: +22 error +23 verbose exit [ 1, true ] diff --git a/src/main.js b/src/main.js index 4bdda11d..06a421e0 100644 --- a/src/main.js +++ b/src/main.js @@ -87,8 +87,8 @@ function generateAgents(scene) { for (var i = 0; i < settings.numAgents; i++) { var theta = Math.PI * 2 * i / settings.numAgents; - var p = (new THREE.Vector3(Math.cos(theta), 0.1, Math.sin(theta))).multiplyScalar(4.5); - var t = (new THREE.Vector3(Math.cos(theta + Math.PI), 0.1, Math.sin(theta + Math.PI))).multiplyScalar(4.5); + var p = (new THREE.Vector3(Math.cos(theta), 0.01, Math.sin(theta))).multiplyScalar(4.5); + var t = (new THREE.Vector3(Math.cos(theta + Math.PI), 0.01, Math.sin(theta + Math.PI))).multiplyScalar(4.5); var cell = coordToGrid(p.x, p.z); var agent = new Agent(p, new THREE.Vector3(0,0,0), t); @@ -113,8 +113,8 @@ function generateAgentsRows(scene) { for (var i = 0; i < settings.numAgents; i++) { var sign = i % 2 == 0; var s = sign?1:-1; - var p = new THREE.Vector3(i / settings.numAgents * 9 - 4.5, 0.1, s * -4.5); - var t = new THREE.Vector3(i / settings.numAgents * 9 - 4.5, 0.1, s * 4.5); + var p = new THREE.Vector3(i / settings.numAgents * 9 - 4.5, 0.01, s * -4.5); + var t = new THREE.Vector3(i / settings.numAgents * 9 - 4.5, 0.01, s * 4.5); var cell = coordToGrid(p.x, p.z); var agent = new Agent(p, new THREE.Vector3(0,0,0), t); @@ -133,6 +133,7 @@ function setWeightedVel(agent) { var pos = agent.position; var tgt = agent.target; var toTarget = (new THREE.Vector3(0, 0, 0)).subVectors(tgt, pos); + toTarget.y = 0; if (toTarget.length() < 0.01) { return; } @@ -141,9 +142,11 @@ function setWeightedVel(agent) { var totalWeight = 0.0; var weights = []; var displacements = []; + for (var m = 0; m < agent.markers.length; m++) { var mpos = agent.markers[m].pos; var toMark = (new THREE.Vector3(0, 0, 0)).subVectors(mpos, pos); + toMark.y = 0.0; var wf = 1.0 / (1.0 + toMark.length()); wf = wf * (1.0 + toMark.dot(toTarget) / toMark.length() / toTarget.length()); @@ -292,6 +295,13 @@ function onLoad(framework) { var mMesh = new THREE.Points(marks, marksMat); mMesh.name = "markers"; scene.add(mMesh); + + var obj = { CircleScenario:function(){ generateAgents(scene)}, + RowsScenario:function(){ generateAgentsRows(scene)}}; + + gui.add(obj,'CircleScenario'); + gui.add(obj,'RowsScenario'); + gui.add(settings, 'numAgents', 10, 50); } @@ -303,14 +313,11 @@ function onUpdate(framework) { updateSimulation(dt); framework.scene.traverse(function(object) { if (object instanceof THREE.Mesh) { - if ('agentID' in object.userData) { - //console.log('ech'); var a = agents[object.userData.agentID]; object.position.set(a.position.x, a.position.y, a.position.z); } } - }); } From 00334033780bb7e29268cc021d756c4f7347b40b Mon Sep 17 00:00:00 2001 From: mccannd Date: Wed, 29 Mar 2017 01:05:54 -0400 Subject: [PATCH 4/4] ech --- index.html | 2 +- npm-debug.log | 35 ----------------------------------- 2 files changed, 1 insertion(+), 36 deletions(-) delete mode 100644 npm-debug.log diff --git a/index.html b/index.html index f775186a..842598b8 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@ - HW1: Noise + HW7: Biocrowds