diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..30726f5 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 8d4aaf4..7efe013 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -*.map \ No newline at end of file +*.map +*.un~ diff --git a/README.md b/README.md index d4ef264..a395247 100644 --- a/README.md +++ b/README.md @@ -1,118 +1,18 @@ # [Project 1: Noise](https://github.com/CIS700-Procedural-Graphics/Project1-Noise) -## Objective - -Get comfortable with using three.js and its shader support and generate an interesting 3D, continuous surface using a multi-octave noise algorithm. - -## Getting Started - -1. [Install Node.js](https://nodejs.org/en/download/). Node.js is a JavaScript runtime. It basically allows you to run JavaScript when not in a browser. For our purposes, this is not necessary. The important part is that with it comes `npm`, the Node Package Manager. This allows us to easily declare and install external dependencies such as [three.js](https://threejs.org/), [dat.GUI](https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage), and [glMatrix](http://glmatrix.net/). Some other packages we'll be using make it significantly easier to develop your code and create modules for better code reuse and clarity. These tools make it _signficantly_ easier to write code in multiple `.js` files without globally defining everything. - -2. Fork and clone [this repository](https://github.com/CIS700-Procedural-Graphics/Project1-Noise). - -3. In the root directory of your project, run `npm install`. This will download all of those dependencies. - -4. Do either of the following (but I highly recommend the first one for reasons I will explain later). - - a. Run `npm start` and then go to `localhost:7000` in your web browser - - b. Run `npm run build` and then go open `index.html` in your web browser - - You should hopefully see the framework code with a 3D cube at the center of the screen! - - -## Developing Your Code -All of the JavaScript code is living inside the `src` directory. The main file that gets executed when you load the page as you may have guessed is `main.js`. Here, you can make any changes you want, import functions from other files, etc. The reason that I highly suggest you build your project with `npm start` is that doing so will start a process that watches for any changes you make to your code. If it detects anything, it'll automagically rebuild your project and then refresh your browser window for you. Wow. That's cool. If you do it the other way, you'll need to run `npm build` and then refresh your page every time you want to test something. - -## Publishing Your Code -We highly suggest that you put your code on GitHub. One of the reasons we chose to make this course using JavaScript is that the Web is highly accessible and making your awesome work public and visible can be a huge benefit when you're looking to score a job or internship. To aid you in this process, running `npm run deploy` will automatically build your project and push it to `gh-pages` where it will be visible at `username.github.io/repo-name`. - -## What is Actually Happening? -You can skip this part if you really want, but I highly suggest you read it. - -### npm install -`npm install` will install all dependencies into a folder called `node_modules`. That's about it. - -### package.json - -This is the important file that `npm` looks at. In it, you can see the commands it's using for the `start`, `build`, and `deploy` scripts mentioned above. You can also see all of the dependencies the project requires. I will briefly go through what each of these is. - - dat-gui: Gives us a nice and simple GUI for modifying variables in our program - - - gl-matrix: Useful library for linear algebra, much like glm - - - stats-js: Gives us a nice graph for timing things. We use it to report how long it takes to render each frame - - - three: Three.js is the main library we're using to draw stuff - - - three-orbit-controls: Handles mouse / touchscreen camera controls - - - babel-core, babel-loader, babel-preset-es2015: JavaScript is a a really fast moving language. It is constantly, constantly changing. Unfortunately, web browsers don't keep up nearly as quickly. Babel does the job of converting your code to a form that current browsers support. This allows us to use newer JavaScript features such as classes and imports without worrying about compatibility. - - - gh-pages-deploy: This is the library that automates publishing your code to Github - - - webpack: Webpack serves the role of packaging your project into a single file. Browsers don't actually support "importing" from other files, so without Webpack, to access data and functions in other files we would need to globally define EVERYTHING. This is an extremely bad idea. Webpack lets us use imports and develop code in separate files. Running `npm build` or `npm start` is what bundles all of your code together. - -- webpack-dev-server: This is an extremely useful tool for development. It essentially creates a file watcher and rebuilds your project whenever you make changes. It also injects code into your page that gets notified when these changes occur so it can automatically refresh your page. - - - webpack-glsl-loader: Webpack does much more than just JavaScript. We can use it to load glsl, css, images, etc. For whatever you want to import, somebody has probably made a webpack loader for it. - -### webpack.config.js - -This is the configuration file in webpack. The most important part is `entry` and `output`. These define the input and output for webpack. It will start from `entry`, explore all dependencies, and package them all into `output`. Here, the `output` is `bundle.js`. If you look in `index.html`, you can see that the page is loading `bundle.js`, not `main.js`. - -The other sections are just configuration settings for `webpack-dev-server` and setup for loading different types of files. - -## Setting up a shader - -Using the provided framework code, create a new three.js material which references a vertex and fragment shader. Look at the adamMaterial for reference. It should reference at least one uniform variable (you'll need a time variable to animate your mesh later on). - -Create [an icosahedron](https://threejs.org/docs/index.html#Reference/Geometries/IcosahedronBufferGeometry), instead of the default cube geometry provided in the scene. Test your shader setup by applying the material to the icosahedron and color the mesh in the fragment shader using the normals' XYZ components as RGB. - -Note that three.js automatically injects several uniform and attribute variables into your shaders by default; they are listed in the [documentation](https://threejs.org/docs/api/renderers/webgl/WebGLProgram.html) for three.js's WebGLProgram class. - -## Noise Generation - -In the shader, write a 3D multi-octave lattice-value noise function that takes three input parameters and generates output in a controlled range, say [0,1] or [-1, 1]. This will require the following steps. - -1. Write several (for however many octaves of noise you want) basic pseudo-random 3D noise functions (the hash-like functions we discussed in class). It's fine to reference one from the slides or elsewhere on the Internet. Again, this should just be a set of math operations, often using large prime numbers to random-looking output from three input parameters. - -2. Write an interpolation function. Lerp is fine, but for better results, we suggest cosine interpolation. - -3. (Optional) Write a smoothing function that will average the results of the noise value at some (x, y, z) with neighboring values, that is (x+-1, y+-1, z+-1). - -4. Write an 'interpolate noise' function that takes some (x, y, z) point as input and produces a noise value for that point by interpolating the surrounding lattice values (for 3D, this means the surrounding eight 'corner' points). Use your interpolation function and pseudo-random noise generator to accomplish this. - -5. Write a multi-octave noise generation function that sums multiple noise functions together, with each subsequent noise function increasing in frequency and decreasing in amplitude. You should use the interpolate noise function you wrote previously to accomplish this, as it generates a single octave of noise. The slides contain pseudocode for writing your multi-octave noise function. - - -## Noise Application - -View your noise in action by applying it as a displacement on the surface of your icosahedron, giving your icosahedron a bumpy, cloud-like appearance. Simply take the noise value as a height, and offset the vertices along the icosahedron's surface normals. You are, of course, free to alter the way your noise perturbs your icosahedron's surface as you see fit; we are simply recommending an easy way to visualize your noise. You could even apply a couple of different noise functions to perturb your surface to make it even less spherical. - -In order to animate the vertex displacement, use time as the third dimension or as some offset to the (x, y, z) input to the noise function. Pass the current time since start of program as a uniform to the shaders. - -For both visual impact and debugging help, also apply color to your geometry using the noise value at each point. There are several ways to do this. For example, you might use the noise value to create UV coordinates to read from a texture (say, a simple gradient image), or just compute the color by hand by lerping between values. - -## Interactivity - -Using dat.GUI and the examples provided in the reference code, make some aspect of your demo an interactive variable. For example, you could add a slider to adjust the strength or scale of the noise, change the number of noise octaves, etc. - -## For the overachievers (extra credit) - -- More interactivity (easy): pretty self-explanatory. Make more aspects of your demo interactive by adding more controlable variables in the GUI. - -- Custom mesh (easy): Figure out how to import a custom mesh rather than using an icosahedron for a fancy-shaped cloud. - -- Mouse interactivity (medium): Find out how to get the current mouse position in your scene and use it to deform your cloud, such that users can deform the cloud with their cursor. - -- Music (hard): Figure out a way to use music to drive your noise animation in some way, such that your noise cloud appears to dance. - -## Submission - -- Update README.md to contain a solid description of your project - -- Publish your project to gh-pages. `npm run deploy`. It should now be visible at http://username.github.io/repo-name - -- Create a [pull request](https://help.github.com/articles/creating-a-pull-request/) to this repository, and in the comment, include a link to your published project. - -- Submit the link to your pull request on Canvas. \ No newline at end of file +## Features implemented +- 2 new shaders: icosahedron-vert and icosahedron-frag +- 3 noise functions: noise1, noise2 and noise3 +- 2 interpolation functions: linear and cosine +- interpolate noise function that uses noise1 to perform a 3D lattice-based interpolation +- multi-ocatave noise generation function that uses the the interpolate noise function, but this time uses noise2 and 3 as well rather than just noise1 +- applied the noise in action and made a jelly-bean like deformation +- animation applied +- reading from explosion png provided by adam for visual impact +- added a noise scale slider to adjust the influence of the noise on the icosahedron + +## Features to come +- Music +- Better multi-octave noise + +## Screenshots to come soon as well \ No newline at end of file diff --git a/explosion.png b/explosion.png new file mode 100644 index 0000000..797b65c Binary files /dev/null and b/explosion.png differ diff --git a/marble.jpg b/marble.jpg new file mode 100644 index 0000000..8cc5b0a Binary files /dev/null and b/marble.jpg differ diff --git a/npm-debug.log b/npm-debug.log new file mode 100644 index 0000000..bb9eed5 --- /dev/null +++ b/npm-debug.log @@ -0,0 +1,45 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'deploy' ] +2 info using npm@3.10.10 +3 info using node@v6.9.4 +4 verbose run-script [ 'predeploy', 'deploy', 'postdeploy' ] +5 info lifecycle @~predeploy: @ +6 silly lifecycle @~predeploy: no script for predeploy, continuing +7 info lifecycle @~deploy: @ +8 verbose lifecycle @~deploy: unsafe-perm in lifecycle true +9 verbose lifecycle @~deploy: PATH: /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/Users/josephgao/Dropbox/developer/cis700_6/Project1-Noise/node_modules/.bin:/Users/josephgao/.rvm/gems/ruby-2.3.1/bin:/Users/josephgao/.rvm/gems/ruby-2.3.1@global/bin:/Users/josephgao/.rvm/rubies/ruby-2.3.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/josephgao/.rvm/bin:/Users/josephgao/.rvm/bin +10 verbose lifecycle @~deploy: CWD: /Users/josephgao/Dropbox/developer/cis700_6/Project1-Noise +11 silly lifecycle @~deploy: Args: [ '-c', 'gh-pages-deploy' ] +12 silly lifecycle @~deploy: Returned: code: 1 signal: null +13 info lifecycle @~deploy: Failed to exec deploy script +14 verbose stack Error: @ deploy: `gh-pages-deploy` +14 verbose stack Exit status 1 +14 verbose stack at EventEmitter. (/usr/local/lib/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. (/usr/local/lib/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 /Users/josephgao/Dropbox/developer/cis700_6/Project1-Noise +17 error Darwin 16.3.0 +18 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "deploy" +19 error node v6.9.4 +20 error npm v3.10.10 +21 error code ELIFECYCLE +22 error @ deploy: `gh-pages-deploy` +22 error Exit status 1 +23 error Failed at the @ deploy script 'gh-pages-deploy'. +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 gh-pages-deploy +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/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..8e53ab6 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main.js b/src/main.js index 92b19a4..52eafae 100644 --- a/src/main.js +++ b/src/main.js @@ -4,6 +4,31 @@ import Framework from './framework' import Noise from './noise' import {other} from './noise' +var global_time = 1.0; +var start_time = Date.now(); + +var noise_scale = function() { + this.scale = 50.0; +}; +var my_scale = new noise_scale(); + +var icosa = new THREE.IcosahedronBufferGeometry(1, 6); +var icosaMaterial = new THREE.ShaderMaterial({ + uniforms: { + time: { value: global_time }, + noise_scale: { value: my_scale.scale }, + image: { + type: "t", + value: THREE.ImageUtils.loadTexture('./.jpg') + } + //resolution: { value: new THREE.Vector2() } + }, + vertexShader: require('./shaders/icosahedron-vert.glsl'), + fragmentShader: require('./shaders/icosahedron-frag.glsl') +}); + + + // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -16,36 +41,47 @@ function onLoad(framework) { // var {scene, camera, renderer, gui, stats} = framework; // initialize a simple box and material - var box = new THREE.BoxGeometry(1, 1, 1); + //var box = new THREE.BoxGeometry(1, 1, 1); - var adamMaterial = new THREE.ShaderMaterial({ - uniforms: { - image: { // Check the Three.JS documentation for the different allowed types and values - type: "t", - value: THREE.ImageUtils.loadTexture('./adam.jpg') - } - }, - vertexShader: require('./shaders/adam-vert.glsl'), - fragmentShader: require('./shaders/adam-frag.glsl') - }); - var adamCube = new THREE.Mesh(box, adamMaterial); + var myIcosa = new THREE.Mesh(icosa, icosaMaterial); + + //var adamMaterial = new THREE.ShaderMaterial({ + //uniforms: { + //image: { // Check the Three.JS documentation for the different allowed types and values + //type: "t", + //value: THREE.ImageUtils.loadTexture('./adam.jpg') + //} + //}, + //vertexShader: require('./shaders/adam-vert.glsl'), + //fragmentShader: require('./shaders/adam-frag.glsl') + //}); + + //var adamCube = new THREE.Mesh(box, adamMaterial); // set camera position camera.position.set(1, 1, 2); camera.lookAt(new THREE.Vector3(0,0,0)); - scene.add(adamCube); + //scene.add(adamCube); + scene.add(myIcosa); // edit params and listen to changes like this // more information here: https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + + gui.add(my_scale, 'scale', 0, 100).onChange(function(newVal) { + my_scale.scale = newVal; + }); + } // called on frame updates function onUpdate(framework) { - // console.log(`the time is ${new Date()}`); + global_time += 1; + icosaMaterial.uniforms.time.value = 0.001 * (Date.now() - start_time);//global_time; + icosaMaterial.uniforms.noise_scale.value = my_scale.scale; } // when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate @@ -57,4 +93,4 @@ Framework.init(onLoad, onUpdate); // Noise.whatever() -// console.log(other()) \ No newline at end of file +// console.log(other()) diff --git a/src/npm-debug.log b/src/npm-debug.log new file mode 100644 index 0000000..50f34d6 --- /dev/null +++ b/src/npm-debug.log @@ -0,0 +1,45 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'deploy' ] +2 info using npm@3.10.10 +3 info using node@v6.9.4 +4 verbose run-script [ 'predeploy', 'deploy', 'postdeploy' ] +5 info lifecycle @~predeploy: @ +6 silly lifecycle @~predeploy: no script for predeploy, continuing +7 info lifecycle @~deploy: @ +8 verbose lifecycle @~deploy: unsafe-perm in lifecycle true +9 verbose lifecycle @~deploy: PATH: /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/Users/josephgao/Dropbox/developer/cis700_6/Project1-Noise/node_modules/.bin:/Users/josephgao/.rvm/gems/ruby-2.3.1/bin:/Users/josephgao/.rvm/gems/ruby-2.3.1@global/bin:/Users/josephgao/.rvm/rubies/ruby-2.3.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/josephgao/.rvm/bin:/Users/josephgao/.rvm/bin +10 verbose lifecycle @~deploy: CWD: /Users/josephgao/Dropbox/developer/cis700_6/Project1-Noise +11 silly lifecycle @~deploy: Args: [ '-c', 'gh-pages-deploy' ] +12 silly lifecycle @~deploy: Returned: code: 1 signal: null +13 info lifecycle @~deploy: Failed to exec deploy script +14 verbose stack Error: @ deploy: `gh-pages-deploy` +14 verbose stack Exit status 1 +14 verbose stack at EventEmitter. (/usr/local/lib/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. (/usr/local/lib/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 /Users/josephgao/Dropbox/developer/cis700_6/Project1-Noise/src +17 error Darwin 16.3.0 +18 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "deploy" +19 error node v6.9.4 +20 error npm v3.10.10 +21 error code ELIFECYCLE +22 error @ deploy: `gh-pages-deploy` +22 error Exit status 1 +23 error Failed at the @ deploy script 'gh-pages-deploy'. +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 gh-pages-deploy +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/src/shaders/.icosahedron-vert.glsl (levyvlan502.1443.wlan.wireless-resnet.upenn.edu's conflicted copy 2017-01-24).swp b/src/shaders/.icosahedron-vert.glsl (levyvlan502.1443.wlan.wireless-resnet.upenn.edu's conflicted copy 2017-01-24).swp new file mode 100644 index 0000000..846b212 Binary files /dev/null and b/src/shaders/.icosahedron-vert.glsl (levyvlan502.1443.wlan.wireless-resnet.upenn.edu's conflicted copy 2017-01-24).swp differ diff --git a/src/shaders/adam-vert.glsl b/src/shaders/adam-vert.glsl index e4b8cc0..764a488 100644 --- a/src/shaders/adam-vert.glsl +++ b/src/shaders/adam-vert.glsl @@ -3,4 +3,4 @@ varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); -} \ No newline at end of file +} diff --git a/src/shaders/icosahedron-frag.glsl b/src/shaders/icosahedron-frag.glsl new file mode 100644 index 0000000..e16fee9 --- /dev/null +++ b/src/shaders/icosahedron-frag.glsl @@ -0,0 +1,9 @@ +varying vec3 norm; +varying vec2 vUv; +uniform sampler2D image; + +void main() { + vec2 uv = vec2(1,1) - vUv; + vec4 color = texture2D(image, uv); + gl_FragColor = vec4(color.rgb, 1.0); +} diff --git a/src/shaders/icosahedron-vert.glsl b/src/shaders/icosahedron-vert.glsl new file mode 100644 index 0000000..4a48233 --- /dev/null +++ b/src/shaders/icosahedron-vert.glsl @@ -0,0 +1,69 @@ +varying vec3 norm; +varying vec2 vUv; +uniform float time; +uniform float noise_scale; + + +float noise_1(float x, float y, float z) { + return cos(z * sin(dot(vec2(x, y), vec2(12.9898, 78.233))) * 43758.5453); +} + +float noise_2(float x, float y, float z) { + return dot(vec2(sin(x), 14591), vec2(cos(y), 179)) * tan(z); +} + +float noise_3(float x, float y, float z) { + float sum = (x * 13.0) + (y * 17.0) + (z * 19.0); + return sin(sum); +} + +float linear_interpolate(float a, float b, float t) { + return a * (1.0 - t) + b * t; +} + +float cosine_interpolate(float a, float b, float t) { + float cos_t = (1.0 - cos(t * 3.1459)) * 0.5; + return linear_interpolate(a, b, cos_t); +} + +float interpolate_noise(float x, float y, float z) { + //pos + float pos_NE = noise_1(ceil(x), ceil(y), ceil(z)); + float pos_NW = noise_1(floor(x), ceil(y), ceil(z)); + float pos_SW = noise_1(floor(x), ceil(y), floor(z)); + float pos_SE = noise_1(ceil(x), ceil(y), floor(z)); + + //neg + float neg_NE = noise_1(ceil(x), floor(y), ceil(z)); + float neg_NW = noise_1(floor(x), floor(y), ceil(z)); + float neg_SW = noise_1(floor(x), floor(y), floor(z)); + float neg_SE = noise_1(ceil(x), floor(y), floor(z)); + + float x_t = ceil(x) - x; + float z_t = ceil(z) - z; + float y_t = ceil(y) - y; + + float pos_north = cosine_interpolate(pos_NE, pos_NW, x_t); + float pos_south = cosine_interpolate(pos_SE, pos_SW, x_t); + float pos_ns = cosine_interpolate(pos_north, pos_south, z_t); + + float neg_north = cosine_interpolate(neg_NE, neg_NW, x_t); + float neg_south = cosine_interpolate(neg_SE, neg_SW, x_t); + float neg_ns = cosine_interpolate(neg_north, neg_south, z_t); + + float res_noise = cosine_interpolate(pos_ns, neg_ns, y_t); + + return res_noise; +} + +void main() { + float pos_x = position[0]; + float pos_y = position[1]; + float pos_z = position[2]; + float noise = interpolate_noise(pos_x, pos_y, time); + float adj_noise = noise_scale / 100.0 * noise; + vec3 new_pos = position + (adj_noise) * normal; + vUv = vec2(uv[0] * abs(noise), uv[1] * abs(noise)); + gl_Position = projectionMatrix * modelViewMatrix * vec4( new_pos, 1.0 ); +} + diff --git a/water.jpg b/water.jpg new file mode 100644 index 0000000..611536f Binary files /dev/null and b/water.jpg differ