diff --git a/README.md b/README.md index d4ef264..3d3b1f6 100644 --- a/README.md +++ b/README.md @@ -1,118 +1,17 @@ # [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. +## Overview +A music visualizer "blob" with lambertian and ambient shading. ## 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. +Used multi-octave noise for the displacement of vertices on the icosahedron, as well as some cloud-like coloring on the surface of the icosaherdron. ## 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. +Added an amplitude parameter to adjust how much the vertices are displaced as the frequency of music fluctuates. ## 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 +Reads in music and takes the average frequency at a current time as a parameter of the displacement of vertices. diff --git a/src/main.js b/src/main.js index 92b19a4..1951c05 100644 --- a/src/main.js +++ b/src/main.js @@ -4,20 +4,94 @@ import Framework from './framework' import Noise from './noise' import {other} from './noise' +var scene, +camera, +renderer, +gui, +stats; + +// create start time +var start = Date.now(); + +var AmplitudeText = function() { + this.amplitude = 10.0; +}; +var amplitudeText = new AmplitudeText(); + +// Audio initialization +var context = new AudioContext(), +source = context.createBufferSource(), +analyser = context.createAnalyser(); +analyser.smoothingTimeConstant = 0.3; +analyser.fftSize = 1024; //there are (fftSize/2) bins when getByteFrequencyData(array) is called +var xhr = new XMLHttpRequest(); +xhr.responseType = 'arraybuffer'; +xhr.open('GET', 'https://echiu1997.github.io/musicvisualizer/testing.mp3', true); +xhr.onload = function() { + context.decodeAudioData(this.response, function(buffer) { + source.connect(analyser); + analyser.connect(context.destination); + source.buffer = buffer; + source.start(0); + }); +} +xhr.send(); + +// create the shader material +var material = new THREE.ShaderMaterial( { + uniforms: THREE.UniformsUtils.merge([ + THREE.UniformsLib['lights'], { + // float initialized to 0 + time: { type: "f", value: 0.0 }, + // float initialized to 0 + freq: { type: "f", value: 0.0 }, + //float initialized to 25 + amp: { type: "f", value: 10.0 } + } + ]), + vertexShader: require('./shaders/iridescence.glsl'), + fragmentShader: require('./shaders/iridescence.glsl') +} ); + +// create a sphere and assign the material +var mesh = new THREE.Mesh( + new THREE.IcosahedronGeometry( 15, 5 ), + material +); + +//decides if your domain has permission to use the image and if you do have permission it sends certain headers back to the browser. +//The browser, if it sees those headers will then let you use the image. +THREE.ImageUtils.crossOrigin = ''; +//for more info: http://blog.mastermaps.com/2013/09/creating-webgl-earth-with-threejs.html +var sky = new THREE.Mesh( + new THREE.SphereGeometry(90, 64, 64), + new THREE.MeshBasicMaterial({ + map: THREE.ImageUtils.loadTexture('https://echiu1997.github.io/musicvisualizer/mars7.png'), + side: THREE.BackSide + }) +); + +// create light +var light = new THREE.PointLight(0xffffff, 1.0); +light.position.set(-10.0, 20.0, 30.0); + // 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; + scene = framework.scene; + camera = framework.camera; + renderer = framework.renderer; + gui = framework.gui; + 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; - // initialize a simple box and material - var box = new THREE.BoxGeometry(1, 1, 1); + scene.add( mesh ); + scene.add( sky ); + scene.add( light ); + /* + // initialize a simple box and material var adamMaterial = new THREE.ShaderMaterial({ uniforms: { image: { // Check the Three.JS documentation for the different allowed types and values @@ -28,24 +102,51 @@ function onLoad(framework) { vertexShader: require('./shaders/adam-vert.glsl'), fragmentShader: require('./shaders/adam-frag.glsl') }); + var box = new THREE.BoxGeometry(1, 1, 1); var adamCube = new THREE.Mesh(box, adamMaterial); + */ // set camera position - camera.position.set(1, 1, 2); + camera.position.set(1, 1, 40); camera.lookAt(new THREE.Vector3(0,0,0)); - scene.add(adamCube); - // 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(); }); + + //add amplitude slider for user to adjust + gui.add(amplitudeText, 'amplitude', 5.0, 40.0); } // called on frame updates function onUpdate(framework) { - // console.log(`the time is ${new Date()}`); + + //rotates the mesh and sky around + mesh.rotation.y += 0.005; + sky.rotation.y -= 0.005; + + //passing time into shader + material.uniforms[ 'time' ].value = 0.00025 * ( Date.now() - start ); + + //pass music data into shader + var array = new Uint8Array(analyser.frequencyBinCount); + analyser.getByteFrequencyData(array); + + //Audio Analysis + //the array contains frequency elements with values ranging from [0,255] + //in total there are 512 bins because analyser.fftSize is 1024 + var sum = 0.0; + for(var j = 0.0; j < analyser.frequencyBinCount; j++) { + sum += array[j]; + } + //average is normalized to [0, 1] + var avg = sum / analyser.frequencyBinCount / 255.0; + material.uniforms[ 'freq' ].value = avg; + + //pass the slider amplitude the user modified + material.uniforms[ 'amp' ].value = amplitudeText.amplitude; } // when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate diff --git a/src/shaders/adam-frag.glsl b/src/shaders/adam-frag.glsl index 5dfa18c..cf0b573 100644 --- a/src/shaders/adam-frag.glsl +++ b/src/shaders/adam-frag.glsl @@ -1,8 +1,46 @@ +//varying vec2 vUv; +varying float noise; +varying vec3 normColor; +varying vec3 vecPos; +varying vec3 vecNormal; + +uniform vec3 pointLightColor[NUM_POINT_LIGHTS]; +uniform vec3 pointLightPosition[NUM_POINT_LIGHTS]; +uniform float pointLightDistance[NUM_POINT_LIGHTS]; + +void main() { + + //Color using UV coordinate, modulate with the noise like ambient occlusion + //vec3 color = vec3( vUv * ( 1. - 2. * noise ), 0.0 ); + //gl_FragColor = vec4( color.rgb, 1.0 ); + + //Color using normals, modulate with noise like ambient occlusion + vec3 color = vec3( normColor * ( 1.0 - 1.5 * noise )); + //gl_FragColor = vec4( color, 1.0 ); + + //Lambertian lighting + vec4 addedLights = vec4(0.0, 0.0, 0.0, 1.0); + for(int i = 0; i < NUM_POINT_LIGHTS; i++) { + vec3 lightDirection = normalize(vecPos - pointLightPosition[i]); + addedLights.rgb += clamp( dot(-lightDirection, vecNormal), 0.0, 1.0) * pointLightColor[i]; + } + + //Ambient Lighting + //this ensures faces that are not lit by point light are not compeltely black + addedLights.rgb += 0.25 * vec3(1.0, 1.0, 1.0); + + //vec3 color = vec3( vec3(0.7, 0.025, 0.1) * ( 1.0 - 2.0 * noise )); + gl_FragColor = vec4(addedLights.x * color.x, addedLights.y * color.y, addedLights.z * color.z, 1.0); +} + + + + +/* varying vec2 vUv; varying float noise; uniform sampler2D image; - void main() { vec2 uv = vec2(1,1) - vUv; @@ -10,4 +48,5 @@ void main() { gl_FragColor = vec4( color.rgb, 1.0 ); -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/src/shaders/adam-vert.glsl b/src/shaders/adam-vert.glsl index e4b8cc0..f4412e4 100644 --- a/src/shaders/adam-vert.glsl +++ b/src/shaders/adam-vert.glsl @@ -1,6 +1,307 @@ +///////////////////////////////////////////////////////////////// +//noise function taken from: +//https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83 +float mod289(float x) { + return x - floor(x * (1.0 / 289.0)) * 289.0; +} +vec4 mod289(vec4 x) { + return x - floor(x * (1.0 / 289.0)) * 289.0; +} + +vec4 perm(vec4 x) { + return mod289(((x * 34.0) + 1.0) * x); +} + +float sample_noise(vec3 p) { + vec3 a = floor(p); + vec3 d = p - a; + d = d * d * (3.0 - 2.0 * d); + + vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0); + vec4 k1 = perm(b.xyxy); + vec4 k2 = perm(k1.xyxy + b.zzww); + + vec4 c = k2 + a.zzzz; + vec4 k3 = perm(c); + vec4 k4 = perm(c + 1.0); + + vec4 o1 = fract(k3 * (1.0 / 41.0)); + vec4 o2 = fract(k4 * (1.0 / 41.0)); + + vec4 o3 = o2 * d.z + o1 * (1.0 - d.z); + vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x); + + return o4.y * d.y + o4.x * (1.0 - d.y); +} + +////////////////////////////////////////////////////////////////// + +float linear_interpolate(float a, float b, float t) { + return a * (1.0 - t) + b * t; +} + +#define M_PI 3.1415926535897932384626433832795 +float cosine_interpolate(float a, float b, float t) { + float cos_t = (1.0 - cos(t * M_PI)) * 0.5; + return linear_interpolate(a, b, cos_t); +} + +float lattice_interpolate(vec3 pos) +{ + //get the 8 corners of the cube + vec3 P1 = vec3(floor(pos.x), floor(pos.y), floor(pos.z)); + vec3 P2 = vec3(P1.x + 1.0, P1.y, P1.z); + vec3 P3 = vec3(P1.x, P1.y + 1.0, P1.z); + vec3 P4 = vec3(P1.x, P1.y, P1.z + 1.0); + vec3 P5 = vec3(P1.x + 1.0, P1.y + 1.0, P1.z); + vec3 P6 = vec3(P1.x + 1.0, P1.y, P1.z + 1.0); + vec3 P7 = vec3(P1.x, P1.y + 1.0, P1.z + 1.0); + vec3 P8 = vec3(P1.x + 1.0, P1.y + 1.0, P1.z + 1.0); + + //get the noise values of the 8 corners + float p1 = sample_noise(P1); + float p2 = sample_noise(P2); + float p3 = sample_noise(P3); + float p4 = sample_noise(P4); + float p5 = sample_noise(P5); + float p6 = sample_noise(P6); + float p7 = sample_noise(P7); + float p8 = sample_noise(P8); + + //get the interpolated noise value of the 8 corners + float xT = distance(P1.x, pos.x) / distance(P1.x, P2.x); + float c1 = cosine_interpolate(p1, p2, xT); + float c2 = cosine_interpolate(p3, p5, xT); + float c3 = cosine_interpolate(p4, p6, xT); + float c4 = cosine_interpolate(p7, p8, xT); + + float yT = distance(P1.y, pos.y) / distance(P1.y, P3.y); + float b1 = cosine_interpolate(c1, c2, yT); + float b2 = cosine_interpolate(c3, c4, yT); + + float zT = distance(P1.z, pos.z) / distance(P1.z, P4.z); + float a = cosine_interpolate(b1, b2, zT); + + return a; +} + +float multioctave_noise(vec3 pos) { + float persistence = 0.8; + float total = 0.0; + for (float octave = 0.0; octave < 3.0; octave++) { + float frequency = pow(2.0, octave); + float amplitude = pow(persistence, octave); + total += lattice_interpolate(vec3(frequency) * pos) * amplitude; + } + return total; +} + +///////////////////////////////////////////////////////////////// + + +//for more given uniform variables, go to: +//https://threejs.org/docs/api/renderers/webgl/WebGLProgram.html + +//varying vec2 vUv; +varying float noise; +varying vec3 normColor; +varying vec3 vecPos; +varying vec3 vecNormal; + +uniform float time; +uniform float freq; //ranges from 0 to 255 +uniform float amp; + +float turbulence( vec3 p ) { + float w = 100.0; + float t = -0.5; + for (float f = 1.0 ; f <= 10.0 ; f++ ){ + float power = pow( 2.0, f ); + t += abs( multioctave_noise( vec3( power * p )) / power ); + } + return t; +} + +void main() { + + // add time to the noise parameters so it's animated + noise = 10.0 * -0.10 * turbulence( 0.5 * normal + time ); + // amp can be changed to by slider to change magnitude of fluctuation + float b = amp * freq * multioctave_noise( 0.05 * position + vec3( 2.0 * time ) ); + float displacement = - noise + b; + + vec3 newPosition = position + normal * displacement; + gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 ); + + //passing to fragment shader + //vUv = uv; + normColor = vec3( 0.6*abs(normal.x), 0.6*abs(normal.y), 0.6*abs(normal.z) ); + vecPos = (modelMatrix * vec4(newPosition, 1.0)).xyz; + vecNormal = normalMatrix * normal; + + //rotate light along with blob + //vecPos = newPosition; + //vecNormal = normal; +} + + + + + +/* +///////////////////// Better Noise Generation ////////////////////// +//based on https://github.com/ashima/webgl-noise + +vec3 mod289(vec3 x) +{ + return x - floor(x * (1.0 / 289.0)) * 289.0; +} + +vec4 mod289(vec4 x) +{ + return x - floor(x * (1.0 / 289.0)) * 289.0; +} + +vec4 permute(vec4 x) +{ + return mod289(((x*34.0)+1.0)*x); +} + +vec4 taylorInvSqrt(vec4 r) +{ + return 1.79284291400159 - 0.85373472095314 * r; +} + +vec3 fade(vec3 t) { + return t*t*t*(t*(t*6.0-15.0)+10.0); +} + +// Classic Perlin noise, periodic variant +float pnoise(vec3 P, vec3 rep) +{ + vec3 Pi0 = mod(floor(P), rep); // Integer part, modulo period + vec3 Pi1 = mod(Pi0 + vec3(1.0), rep); // Integer part + 1, mod period + Pi0 = mod289(Pi0); + Pi1 = mod289(Pi1); + vec3 Pf0 = fract(P); // Fractional part for interpolation + vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0 + vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); + vec4 iy = vec4(Pi0.yy, Pi1.yy); + vec4 iz0 = Pi0.zzzz; + vec4 iz1 = Pi1.zzzz; + + vec4 ixy = permute(permute(ix) + iy); + vec4 ixy0 = permute(ixy + iz0); + vec4 ixy1 = permute(ixy + iz1); + + vec4 gx0 = ixy0 * (1.0 / 7.0); + vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5; + gx0 = fract(gx0); + vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0); + vec4 sz0 = step(gz0, vec4(0.0)); + gx0 -= sz0 * (step(0.0, gx0) - 0.5); + gy0 -= sz0 * (step(0.0, gy0) - 0.5); + + vec4 gx1 = ixy1 * (1.0 / 7.0); + vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5; + gx1 = fract(gx1); + vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1); + vec4 sz1 = step(gz1, vec4(0.0)); + gx1 -= sz1 * (step(0.0, gx1) - 0.5); + gy1 -= sz1 * (step(0.0, gy1) - 0.5); + + vec3 g000 = vec3(gx0.x,gy0.x,gz0.x); + vec3 g100 = vec3(gx0.y,gy0.y,gz0.y); + vec3 g010 = vec3(gx0.z,gy0.z,gz0.z); + vec3 g110 = vec3(gx0.w,gy0.w,gz0.w); + vec3 g001 = vec3(gx1.x,gy1.x,gz1.x); + vec3 g101 = vec3(gx1.y,gy1.y,gz1.y); + vec3 g011 = vec3(gx1.z,gy1.z,gz1.z); + vec3 g111 = vec3(gx1.w,gy1.w,gz1.w); + + vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); + g000 *= norm0.x; + g010 *= norm0.y; + g100 *= norm0.z; + g110 *= norm0.w; + vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); + g001 *= norm1.x; + g011 *= norm1.y; + g101 *= norm1.z; + g111 *= norm1.w; + + float n000 = dot(g000, Pf0); + float n100 = dot(g100, vec3(Pf1.x, Pf0.yz)); + float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z)); + float n110 = dot(g110, vec3(Pf1.xy, Pf0.z)); + float n001 = dot(g001, vec3(Pf0.xy, Pf1.z)); + float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z)); + float n011 = dot(g011, vec3(Pf0.x, Pf1.yz)); + float n111 = dot(g111, Pf1); + + vec3 fade_xyz = fade(Pf0); + vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z); + vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y); + float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x); + return 2.2 * n_xyz; +} +/////////////////////////////////////////////////////// + +//for more given uniform variables, go to: +//https://threejs.org/docs/api/renderers/webgl/WebGLProgram.html + +//varying vec2 vUv; +varying float noise; +//varying vec3 normColor; +varying vec3 vecPos; +varying vec3 vecNormal; + +uniform float time; +uniform float freq; //ranges from 0 to 255 +uniform float amp; + +float turbulence( vec3 p ) { + float w = 100.0; + float t = -0.5; + for (float f = 1.0 ; f <= 10.0 ; f++ ){ + float power = pow( 2.0, f ); + t += abs( pnoise( vec3( power * p ), vec3( 10.0, 10.0, 10.0 ) ) / power ); + } + return t; +} + +void main() { + + // add time to the noise parameters so it's animated + noise = 10.0 * -0.10 * turbulence( .5 * normal + time ); + // amp can be changed to by slider to change magnitude of fluctuation + float b = amp * freq * pnoise( 0.05 * position + vec3( 2.0 * time ), vec3( 100.0 ) ); + float displacement = - noise + b; + + vec3 newPosition = position + normal * displacement; + gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 ); + + //passing to fragment shader + //vUv = uv; + //normColor = vec3( 0.6*abs(normal.x), 0.6*abs(normal.y), 0.6*abs(normal.z) ); + vecPos = (modelMatrix * vec4(newPosition, 1.0)).xyz; + vecNormal = normalMatrix * normal; + + //rotate light along with blob + //vecPos = newPosition; + //vecNormal = normal; +} +*/ + + + + + +/* varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); -} \ No newline at end of file +} +*/ \ No newline at end of file