diff --git a/README.md b/README.md index d4ef264..98ad297 100644 --- a/README.md +++ b/README.md @@ -1,118 +1,53 @@ # [Project 1: Noise](https://github.com/CIS700-Procedural-Graphics/Project1-Noise) -## Objective +## Description -Get comfortable with using three.js and its shader support and generate an interesting 3D, continuous surface using a multi-octave noise algorithm. +Using three.js and its shader support to generate an interesting 3D, continuous surface using a multi-octave noise algorithm. -## Getting Started +## Implementation Details -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. + - Breakdown of value noise: + - `perlinNoise()` is an octave function that changes frequency and amplitude with each octave. The value of each octave is determined by a call to interpolatedNoise() and is summed together to achieve a final result -2. Fork and clone [this repository](https://github.com/CIS700-Procedural-Graphics/Project1-Noise). + - `interpolatedNoise()` is a function that does trilinear interpolation, upon running smoothedNoise() on the 8 vertices it interpolates between first to achieve a smoother curve. -3. In the root directory of your project, run `npm install`. This will download all of those dependencies. + - `randomNoise3D()` is a hash function used to obtain pseudo-random results between 0 and 1. -4. Do either of the following (but I highly recommend the first one for reasons I will explain later). + - `cosineInterp()` does cosine interpolate - a. Run `npm start` and then go to `localhost:7000` in your web browser + - `smoothedNoise()` is a function that smoothens vertices based on its 8 neighbors on each axis (for a total of 27 vertices), a configuration similar to that of a rubiks cube. - 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! + - Time: + - Time is passed in from main.js as a uniform variable. It is incorporated as an input in the noise function to animate the change in the icosahedron mesh's structure + - num_octaves: + - A uniform variable passed in to the vertex shader from main.js that allows users to change its value in the gui -## 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. + - perlin_persistence: + - A uniform variable passed in to the vertex shader from main.js that allows users to change its value in the gui -## 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. +## Results -### npm install -`npm install` will install all dependencies into a folder called `node_modules`. That's about it. +1. Control Panel: ![alt text](https://github.com/MegSesh/Project1-Noise/blob/proj1noise_branch1/images/controls.png "Image 1") -### 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 +2. Jagged Flower: +![alt text](https://github.com/MegSesh/Project1-Noise/blob/proj1noise_branch1/images/cleanjaggedflower_controls.png "Image 2") - - 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 +3. Ripples: +![alt text](https://github.com/MegSesh/Project1-Noise/blob/proj1noise_branch1/images/ripple_controls.png "Image 3") - - three-orbit-controls: Handles mouse / touchscreen camera controls +More Ripples: +![alt text](https://github.com/MegSesh/Project1-Noise/blob/proj1noise_branch1/images/globe_ripples.png "Image 4") - - 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 +4. Embryo: +![alt text](https://github.com/MegSesh/Project1-Noise/blob/proj1noise_branch1/images/smootherembryo_controls.png "Image 5") - - 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. +## Lessons - - 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 +Despite programming a noise function before and using it to build a terrain, I learned more about how interpolation works with this assignment. I also learned more on how much persistence, frequency, and amplitude can really affect the output of your noise function. I had a lot of fun creating the shapes above, and I hope to do more with this soon! diff --git a/images/cleanjaggedflower_controls.png b/images/cleanjaggedflower_controls.png new file mode 100644 index 0000000..7f37a62 Binary files /dev/null and b/images/cleanjaggedflower_controls.png differ diff --git a/images/controls.png b/images/controls.png new file mode 100644 index 0000000..55d82d4 Binary files /dev/null and b/images/controls.png differ diff --git a/images/embryo.png b/images/embryo.png new file mode 100644 index 0000000..e1fa168 Binary files /dev/null and b/images/embryo.png differ diff --git a/images/embryo_controls.png b/images/embryo_controls.png new file mode 100644 index 0000000..3561fbd Binary files /dev/null and b/images/embryo_controls.png differ diff --git a/images/flower_1.png b/images/flower_1.png new file mode 100644 index 0000000..2aea2e1 Binary files /dev/null and b/images/flower_1.png differ diff --git a/images/globe_ripples.png b/images/globe_ripples.png new file mode 100644 index 0000000..e709982 Binary files /dev/null and b/images/globe_ripples.png differ diff --git a/images/jaggedflower_2.png b/images/jaggedflower_2.png new file mode 100644 index 0000000..d395dee Binary files /dev/null and b/images/jaggedflower_2.png differ diff --git a/images/jaggedflower_controls.png b/images/jaggedflower_controls.png new file mode 100644 index 0000000..59619ea Binary files /dev/null and b/images/jaggedflower_controls.png differ diff --git a/images/ripple_controls.png b/images/ripple_controls.png new file mode 100644 index 0000000..13ad43f Binary files /dev/null and b/images/ripple_controls.png differ diff --git a/images/smootherembryo_controls.png b/images/smootherembryo_controls.png new file mode 100644 index 0000000..954bf85 Binary files /dev/null and b/images/smootherembryo_controls.png differ diff --git a/src/framework.js b/src/framework.js index 9cfcd1b..93f2abe 100644 --- a/src/framework.js +++ b/src/framework.js @@ -62,8 +62,12 @@ function init(callback, update) { requestAnimationFrame(tick); // register to call this again when the browser renders a new frame })(); + //CLASS NOTES: (); after the function calls it immediately + // we will pass the scene, gui, renderer, camera, etc... to the callback function return callback(framework); + + //CLASS NOTES: by passing in framework, you can change what gui and stats reference to }); } diff --git a/src/main.js b/src/main.js index 5fa221d..fde1846 100644 --- a/src/main.js +++ b/src/main.js @@ -2,6 +2,64 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' + +//PROJ 1: NOISE +//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, 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 for three.js's WebGLProgram class. + + +var time_update = 0; + +//adding new slider variables +var total_octaves = 8; +var max_persistence = 1.0; + +var start = Date.now(); + +//adding new slider variables +var p = { + explode : 20.0, + octaves : 1.0, + persistence : 0.25 +} + +var icosahedron_geo = new THREE.IcosahedronGeometry(0.25, 5);//5); //making the second var 1 adds more verts and makes it more spherical +icosahedron_geo.translate(2, 0, -1); + +var icosahedronMaterial = new THREE.ShaderMaterial({ + uniforms: { + time: { + type: "float", + value : time_update + }, + num_octaves: { + type: "float", + value : total_octaves + }, + perlin_persistence: { + type: "float", + value : max_persistence + } + + //to add a slider that'll change the mesh's texture + //add multiple images with their respective file names as uniform variables + //have a flag variable + }, + + vertexShader: require('./shaders/icosahedron-vert.glsl'), + fragmentShader: require('./shaders/icosahedron-frag.glsl') +}); +var icosahedronMesh = new THREE.Mesh(icosahedron_geo, icosahedronMaterial); + + + + // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -11,7 +69,7 @@ function onLoad(framework) { var stats = framework.stats; // LOOK: the line below is synyatic sugar for the code above. Optional, but I sort of recommend it. - // var {scene, camera, renderer, gui, stats} = framework; + // var {scene, camera, renderer, gui, stats} = framework; // initialize a simple box and material var box = new THREE.BoxGeometry(1, 1, 1); @@ -19,32 +77,65 @@ function onLoad(framework) { var adamMaterial = new THREE.ShaderMaterial({ uniforms: { image: { // Check the Three.JS documentation for the different allowed types and values - type: "t", + type: "t", value: THREE.ImageUtils.loadTexture('./adam.jpg') } }, vertexShader: require('./shaders/adam-vert.glsl'), fragmentShader: require('./shaders/adam-frag.glsl') - }); + }); //end adam material + 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); + //PROJ 1: NOISE + scene.add(icosahedronMesh); + + +//SLIDERS ON GUI + // 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(p, 'explode', 0, 100).onChange(function(newVal) { + p.explode = newVal; + }); + + gui.add(p, 'octaves', 1, total_octaves).onChange(function(newVal) { + p.octaves = newVal; + }); + + gui.add(p, 'persistence', 0.25, max_persistence).onChange(function(newVal) { + p.octaves = newVal; + }); + + //CLASS NOTES: get the camera object and bind to its fov attribute + + //CLASS NOTES: for homework, we'd want to update shader uniform variables } +// + // called on frame updates function onUpdate(framework) { console.log(`the time is ${new Date()}`); + + time_update = (Date.now() - start) * 0.00025; + + //adding the changing vars for the icosahedron material + icosahedronMaterial.uniforms.time.value = time_update; + icosahedronMaterial.uniforms.num_octaves.value = p.octaves; + icosahedronMaterial.uniforms.perlin_persistence.value = p.persistence; } // 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 +Framework.init(onLoad, onUpdate); diff --git a/src/noise.js b/src/noise.js new file mode 100644 index 0000000..ddd4589 --- /dev/null +++ b/src/noise.js @@ -0,0 +1,202 @@ +const THREE = require('three'); + +//PROJ 1: NOISE + +//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. + +//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. + +//Write an interpolation function. Lerp is fine, but for better results, we suggest cosine interpolation. + +//(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). + +//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. + +//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. + + + +function hash1(x) //returns a float +{ + //x = (x << 13) ^ x; + //return (1.0 - (x * (x * x * 15731 + 789221) + 1376312589) & 7fffffff) / 10737741824.0); +} + +function hash2(x, y) //returns a float +{ + //return fract(sin(dot(vec2(x, y), vec2(12.9898, 78.233))) * 43758.5453); +} + +/* +float Scene::randomNoise3D(float x, float z, float y) +{ + int int_x = (int)((x + y + z) * 57); + int_x = (int_x<<13) ^ int_x; + return ((1.0 - ( (int_x * (int_x * int_x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f) + 1) / 2.0f; +} + +float Scene::randomNoise3D_2(float x, float z, float y) +{ +// int int_x = (int)((x + y + z) * 157); +// int_x = (int_x>>13) ^ int_x; +// return ((1.0 - ( (int_x * (int_x * int_x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f) + 1) / 2.0f; + + glm::vec3 a = glm::vec3(x, y, z); + glm::vec3 b = glm::vec3(12.9898, 78.233, 140.394); + float val = std::sin(glm::dot(a, b)) * 43758.5453; + int int_val = (int)val; + return (1 + (val - int_val)) / 2.0f; +} +*/ + +//======================================================================================================================================================== + +function linear_interp(a, b, t) //returns a float +{ + //return a * (1 - t) + b * t; +} + +function cosine_interp(a, b, t) //returns a float +{ + //cos_t = (1 - cos(t * M_PI)) * 0.5f; + //return linear_interpolate(a, b, cos_t); +} + + +//======================================================================================================================================================== + +function perlinNoise2D(x, y) //returns a float +{ + //float total = 0; + //float persistence = 1 / 2.0f; + + //for(int i = 0; i < numOctaves; i++) + //{ + //float frequency = pow(2, i); + //float amplitude = pow(persistence, i); + + //total += sampleNoise(x, y, frequency); + //} + //return total; +} + + +/* +float Scene::perlinWorm(float x, float z, float y) +{ + float output_direction = 0.0f; + double persistence = .5; + int numOctaves = 2; + float frequency = 0; + float amplitude = 0; + + x = x * .3; + z = z * .3; + y = y * .3; + + for (int i = 0; i < numOctaves; i++) + { + frequency = std::pow(2, i); + amplitude = std::pow(persistence, i); + output_direction = output_direction + randomNoise3D(x * frequency, z * frequency, y * frequency) * amplitude; + } + + //std::cout<< "OUTPUT LEFT RIGHT VALUE: " << output_direction << std::endl; + + return output_direction; +} +*/ + + + + + + + +/* + + +//each sample point has a smoothed value, and then you interpolate between those smoothed values rather than the original ones +float interpolatedNoise(float x, float y, float z) +{ + float floored_x = floor(x); + float diff_x = x - floored_x; + + float floored_y = floor(y); + float diff_y = y - floored_y; + + float floored_z = floor(z); + float diff_z = z - floored_z; + + float v1 = smoothedNoise(floored_x, floored_y, floored_z); + float v2 = smoothedNoise(floored_x + 1, floored_y, floored_z); + float v3 = smoothedNoise(floored_x, floored_y + 1, floored_z); + float v4 = smoothedNoise(floored_x + 1, floored_y + 1, floored_z); + + float v5 = smoothedNoise(floored_x, floored_y, floored_z + 1); + float v6 = smoothedNoise(floored_x + 1, floored_y, floored_z + 1); + float v7 = smoothedNoise(floored_x, floored_y + 1, floored_z + 1); + float v8 = smoothedNoise(floored_x + 1, floored_y + 1, floored_z + 1); + + + float interp_1 = cosineInterp(v1, v2, difference_x); + float interp_2 = cosineInterp(v3, v4, difference_x); + + float interp_3 = cosineInterp(v5, v6, difference_y); + float interp_4 = cosineInterp(v7, v8, difference_y); + + float interp_5 = cosineInterp(interp_1, interp_2, difference_z); + float interp_6 = cosineInterp(interp_3, interp_4, difference_z); + + return cosineInterp(interp_5, interp_6, difference_z); +} + +float smoothedNoise(float x, float y) +{ + float corners = (randomNoise(x - 1, y - 1, z + 1) + randomNoise(x + 1, y - 1, z + 1) + randomNoise(x - 1, y + 1, z + 1) + randomNoise(x + 1, y + 1, z + 1)) + + (randomNoise(x - 1, y - 1, z - 1) + randomNoise(x + 1, y - 1, z - 1) + randomNoise(x - 1, y + 1, z - 1) + randomNoise(x + 1, y + 1, z - 1)) + / 16.0; + + float sides = (randomNoise(x - 1, y, z + 1) + randomNoise(x + 1, y, z + 1) + randomNoise(x , y - 1, z + 1) + randomNoise(x , y + 1, z + 1)) + (randomNoise(x - 1, y, z - 1) + randomNoise(x + 1, y, z - 1) + randomNoise(x , y - 1, z - 1) + randomNoise(x , y + 1, z - 1)) + / 8.0; + + + //edge connected (12) / 8 + //(x - 1, y + 1, z), (x, y + 1, z + 1), (x + 1, y + 1, z), (x, y + 1, z - 1) + //(x - 1, y, z + 1), (x + 1, y, z + 1), (x + 1, y, z - 1), (x - 1, y, z - 1) + //(x - 1, y - 1, z), (x, y - 1, z + 1), (x + 1, y - 1, z), (x, y - 1, z - 1) + + //point connected (8) / 16 + //(x - 1, y + 1, z + 1), (x + 1, y + 1, z + 1), (x - 1, y - 1, z + 1), (x + 1, y - 1, z + 1) + //(x - 1, y + 1, z - 1), (x + 1, y + 1, z - 1), (x - 1, y - 1, z - 1), (x + 1, y - 1, z - 1) + + //face connected (6) / 4 + //(x - 1, y , z), (x, y + 1, z), (x , y, z + 1), (x + 1, y, z), (x , y, z - 1), (x , y - 1, z) + + //center (1) / 2 + //(x, y , z) + + + float center = randomNoise(x , y, z) / 4.0; + + return corners + sides + center; +} + +float cosineInterp(float x, float y, float z) +{ + float t = (1 - std::cos(z * M_PI)) * 0.5; + return (x * (1 - t)) + (y * t); +} + + + +*/ diff --git a/src/shaders/adam-frag.glsl b/src/shaders/adam-frag.glsl index 64ca0e0..0b1b9f1 100644 --- a/src/shaders/adam-frag.glsl +++ b/src/shaders/adam-frag.glsl @@ -2,11 +2,15 @@ varying vec2 vUv; varying float noise; uniform sampler2D image; +//have multiple sampler2D variables for those image variables +//have flag, and have if statement for "if flag == some number", then do this image void main() { + + //vec2 uv = vec2(1,1) - vUv; vec4 color = texture2D( image, vUv ); gl_FragColor = vec4( color.rgb, 1.0 ); -} \ No newline at end of file +} 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..e7455d5 --- /dev/null +++ b/src/shaders/icosahedron-frag.glsl @@ -0,0 +1,10 @@ +varying vec3 vNormal; +varying vec3 perlin_color; + +void main() { + //PROJ1 : NOISE + //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. + + gl_FragColor = vec4(vNormal, 1.0); + //gl_FragColor = vec4(1.0 * perlin_color, 1.0); +} diff --git a/src/shaders/icosahedron-vert.glsl b/src/shaders/icosahedron-vert.glsl new file mode 100644 index 0000000..27f7d66 --- /dev/null +++ b/src/shaders/icosahedron-vert.glsl @@ -0,0 +1,131 @@ +varying vec3 vNormal; +uniform float time; +varying vec3 perlin_color; + +uniform float num_octaves; +uniform float perlin_persistence; + + +float cosineInterp(float x, float y, float z) +{ + float t = (1.0 - cos(z * 3.1459)) * 0.5; + return (x * (1.0 - t)) + (y * t); +} + +float randomNoise3D(float x, float y, float z) +{ + vec3 a = vec3(x, y, z); + vec3 b = vec3(12.9898, 78.233, 140.394); + + float dot_prod = (a.x * b.x) + (a.y * b.y) + (a.z * b.z); + float val = sin(dot_prod) * 43758.5453; + + float int_val = floor(val); + return (val - int_val) * 2.0 - 1.0; +} + +float smoothedNoise(float x, float y, float z) +{ + //edge connected (12) / 8 + float edges = (randomNoise3D(x - 1.0, y + 1.0, z) + randomNoise3D(x, y + 1.0, z + 1.0) + randomNoise3D(x + 1.0, y + 1.0, z) + randomNoise3D(x, y + 1.0, z - 1.0) + + randomNoise3D(x - 1.0, y, z + 1.0) + randomNoise3D(x + 1.0, y, z + 1.0) + randomNoise3D(x + 1.0, y, z - 1.0) + randomNoise3D(x - 1.0, y, z - 1.0) + + randomNoise3D(x - 1.0, y - 1.0, z) + randomNoise3D(x, y - 1.0, z + 1.0) + randomNoise3D(x + 1.0, y - 1.0, z) + randomNoise3D(x, y - 1.0, z - 1.0)) + / 8.0; + + //point connected (8) / 16 + float points = randomNoise3D(x - 1.0, y + 1.0, z + 1.0) + randomNoise3D(x + 1.0, y + 1.0, z + 1.0) + randomNoise3D(x - 1.0, y - 1.0, z + 1.0) + randomNoise3D(x + 1.0, y - 1.0, z + 1.0) + + randomNoise3D(x - 1.0, y + 1.0, z - 1.0) + randomNoise3D(x + 1.0, y + 1.0, z - 1.0) + randomNoise3D(x - 1.0, y - 1.0, z - 1.0) + randomNoise3D(x + 1.0, y - 1.0, z - 1.0) + / 16.0; + + //face connected (6) / 4 + float faces = randomNoise3D(x - 1.0, y , z) + randomNoise3D(x, y + 1.0, z) + randomNoise3D(x , y, z + 1.0) + + randomNoise3D(x + 1.0, y, z) + randomNoise3D(x , y, z - 1.0) + randomNoise3D(x , y - 1.0, z) + / 4.0; + + //center (1) / 2 + float center = randomNoise3D(x , y, z) / 2.0; + + return edges + points + faces + center; +} + +//each sample point has a smoothed value, and then you interpolate between those smoothed values rather than the original ones +float interpolatedNoise(float x, float y, float z) +{ + float floored_x = floor(x); + float difference_x = x - floored_x; + + float floored_y = floor(y); + float difference_y = y - floored_y; + + float floored_z = floor(z); + float difference_z = z - floored_z; + + float v1 = smoothedNoise(floored_x, floored_y, floored_z); + float v2 = smoothedNoise(floored_x + 1.0, floored_y, floored_z); + + float v3 = smoothedNoise(floored_x, floored_y + 1.0, floored_z); + float v4 = smoothedNoise(floored_x + 1.0, floored_y + 1.0, floored_z); + + float v5 = smoothedNoise(floored_x, floored_y, floored_z + 1.0); + float v6 = smoothedNoise(floored_x + 1.0, floored_y, floored_z + 1.0); + + float v7 = smoothedNoise(floored_x, floored_y + 1.0, floored_z + 1.0); + float v8 = smoothedNoise(floored_x + 1.0, floored_y + 1.0, floored_z + 1.0); + + + float interp_1 = cosineInterp(v1, v2, difference_x); + float interp_2 = cosineInterp(v3, v4, difference_x); + + float interp_3 = cosineInterp(v5, v6, difference_x); + float interp_4 = cosineInterp(v7, v8, difference_x); + + float interp_5 = cosineInterp(interp_1, interp_2, difference_y); + float interp_6 = cosineInterp(interp_3, interp_4, difference_y); + + return cosineInterp(interp_5, interp_6, difference_z); +} + +float perlinNoise(float x, float y, float z) +{ + float noise_total = 0.0; + float persistence = perlin_persistence;//0.75; //0.75 makes it spikier. 0.5 makes it more gaseous + float numOctaves = 2.0; + float frequency = 0.0; + float amplitude = 0.0; + + float i = 0.0; + + // const int octaves = int(num_octaves); //8; + for (int j = 0; j < 20; j+= 1) + { + if (j < int(num_octaves)) { + frequency = pow(2.0, i); + amplitude = pow(persistence, i); + + //call either randomNoise3D or interpolatedNoise here + noise_total += interpolatedNoise(x * frequency, y * frequency, z * frequency) * amplitude; + i++; + } + } + + return noise_total; +} + + +void main() { + vNormal = normal; + float noise_output = perlinNoise(position.x + sin(time), position.y + sin(time), position.z + sin(time)); + + //other calls to perlin that produce interesting results + //0.5 * perlinNoise(position.x + sin(time), position.y + sin(time), position.z + sin(time)) + 0.5; //this will make it more gaseous like + //perlinNoise(position.x + sin(time), position.y + sin(time), position.z + sin(time)) //taking sin of time will make it look like rewinding back + //perlinNoise(position.x * sin(time) * 4.0, position.y * time * 4.0, position.z + sin(time) * 4.0); + + //to send to frag shader + perlin_color = vec3(noise_output); + + //change position of mesh based on perlin output + vec3 new_pos = position; + new_pos = new_pos + (vNormal * 0.5 * noise_output); + gl_Position = projectionMatrix * modelViewMatrix * vec4( new_pos, 1.0 ); +}