diff --git a/README.md b/README.md index d4ef264..fb9b54b 100644 --- a/README.md +++ b/README.md @@ -1,118 +1,42 @@ -# [Project 1: Noise](https://github.com/CIS700-Procedural-Graphics/Project1-Noise) +# Texture Generation for Final Project -## Objective -Get comfortable with using three.js and its shader support and generate an interesting 3D, continuous surface using a multi-octave noise algorithm. +### Milestone-2 -## Getting Started +Finally we have the server thanks to Austin!! We haven't merged our code, so it is not yet deployed. The texture does not become more defined as you go closer as I have hardcoded the number of octaves. I will be changing them based on the viewer distance to change the levels of detail. -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. +![](images/shadercesium.gif) -2. Fork and clone [this repository](https://github.com/CIS700-Procedural-Graphics/Project1-Noise). +- `shaders/sinenoise-frag.glsl` fragment shader is an accidental marble texture that I did not intend to make. -3. In the root directory of your project, run `npm install`. This will download all of those dependencies. + I found articles on warping noise functions with other noise functions while looking up Voronoi diagrams. I thought I could use the Ridged noise with just sine functions to get cool streams and rivers. [cegaton](https://blender.stackexchange.com/questions/45892/is-it-possible-to-distort-a-voronoi-texture-like-the-wave-textures-distortion-sl) does it using voronoise. -4. Do either of the following (but I highly recommend the first one for reasons I will explain later). + So I tried it, and It looks more like marble than rivers. I don't think it will work for this project. - a. Run `npm start` and then go to `localhost:7000` in your web browser + I am still understanding voronoise and how to integrate it to the texture I already have. - b. Run `npm run build` and then go open `index.html` in your web browser +![](images/sinenoise.gif) - You should hopefully see the framework code with a 3D cube at the center of the screen! +- Demo (Preset2 is marble texture): https://rms13.github.io/Project1-Noise +### Milestone-1 -## 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. +`mountain-frag.glsl` fragment shader contains the shader that textures the mountain. Value Noise algorithm is used as the noise generator. It is a slightly modified version (the noise function and the matrices are different) of what IQ shows [here](http://www.iquilezles.org/www/articles/morenoise/morenoise.htm). The derivatives of noise are used to simulate erosion effects. It still needs some work. -## 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`. +`mountain-vert.glsl` vertex shader uses Ridged Value Noise to model mountains. This is *not* the terrain we will be using for the final version. It's used only for testing the texture. The terrain part (using Diamond Square algorithm) is being done by [Rudraksha](https://github.com/rudraksha20). -## 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. +#### GUI Controls +- Colors (Red, Green, Blue), and (Red1, Green1, Blue1), can be used to set the colors of layers based on height, which is useful for visualizing the noise output. +- NoiseType demos 3 variations of value noise I experimented with. This can only be used with Preset1 off. + 1. Value Noise. + 2. Absolute Value Noise. + 3. Ridged Value Noise. +- Increasing the Speed will animate the noise (and erosion effects with Preset1). Although increasing it too much will increase the frequency too much, which is not what the static texture looks like. +- Preset1 will show the mountain texture that I think looks good. The erosion effect needs some fine-tuning. Uses Value Noise no matter what is selected +- Terrain will toggle the geometry between Terrain and Sphere. +- Preset2 is the new marble shader done with sine of ridged value noise. -### package.json +### [Demo](https://rms13.github.io/Project1-Noise/) -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 +### [Final Project original repository](https://github.com/rms13/Final-Project) diff --git a/The_Kyoto_Connection_-_09_-_Hachiko_The_Faithtful_Dog.mp3 b/The_Kyoto_Connection_-_09_-_Hachiko_The_Faithtful_Dog.mp3 new file mode 100644 index 0000000..e511b2d Binary files /dev/null and b/The_Kyoto_Connection_-_09_-_Hachiko_The_Faithtful_Dog.mp3 differ diff --git a/helplog.md b/helplog.md new file mode 100644 index 0000000..3a45b5c --- /dev/null +++ b/helplog.md @@ -0,0 +1,39 @@ +1. dat.GUI: +https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage + +2. glsl array passingin three.js: +https://github.com/mrdoob/three.js/issues/389 + +3. JS arrays: +http://www.w3schools.com/js/js_arrays.asp + +4. three.js uniform types: +https://github.com/mrdoob/three.js/wiki/Uniforms-types + +5. Audio in JS: +https://www.patrick-wied.at/blog/how-to-create-audio-visualizations-with-javascript-html +https://w-labs.at/experiments/audioviz/ +AnalyserNode: +https://webaudio.github.io/web-audio-api/#the-analysernode-interface + +6. JS measure time: performance.now() +http://stackoverflow.com/questions/313893/how-to-measure-time-taken-by-a-function-to-execute +http://stackoverflow.com/questions/1210701/compute-elapsed-time + +7. simplex noise: +https://cmaher.github.io/posts/working-with-simplex-noise/ + +8. Plot of noise function: +http://www.wolframalpha.com/input/?i=plot(+mod(+sin(x*12.9898+%2B+y*78.233)+*+43758.5453,1)x%3D0..2,+y%3D0..2) + +9. GLSL: shader functions and stuff: +https://www.khronos.org/opengl/wiki/Core_Language_(GLSL)#Functions +http://relativity.net.au/gaming/glsl/Functions.html +http://www.lighthouse3d.com/tutorials/glsl-tutorial/statements-and-functions/ + +10. webgl shader stuff: +http://webglfundamentals.org/webgl/lessons/webgl-shaders-and-glsl.html + +11. webgl basics: +http://webglfundamentals.org/webgl/lessons/webgl-fundamentals.html +https://www.khronos.org/files/webgl/webgl-reference-card-1_0.pdf \ No newline at end of file diff --git a/images/shadercesium.gif b/images/shadercesium.gif new file mode 100644 index 0000000..fdbca66 Binary files /dev/null and b/images/shadercesium.gif differ diff --git a/images/sinenoise.gif b/images/sinenoise.gif new file mode 100644 index 0000000..9f2c130 Binary files /dev/null and b/images/sinenoise.gif differ diff --git a/index.html b/index.html index f775186..f60b886 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@ - HW1: Noise + Noise Cloud + diff --git a/src/framework.js b/src/framework.js index 9cfcd1b..6de7926 100644 --- a/src/framework.js +++ b/src/framework.js @@ -1,9 +1,27 @@ - const THREE = require('three'); const OrbitControls = require('three-orbit-controls')(THREE) import Stats from 'stats-js' import DAT from 'dat-gui' +////////////// +// Sound: +var analyser; +var data; +var aud; +window.onload = function() { + var audcon = new AudioContext(); + aud = document.getElementById('myAudio'); + var audsrc = audcon.createMediaElementSource(aud); + analyser = audcon.createAnalyser(); + + audsrc.connect(analyser); + audsrc.connect(audcon.destination); + data = new Uint8Array(analyser.frequencyBinCount); // read audio data.. 1024B by default.. + + //aud.play(); +}; +////////////// + // 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) { @@ -29,7 +47,7 @@ function init(callback, update) { var renderer = new THREE.WebGLRenderer( { antialias: true } ); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setClearColor(0x020202, 0); + renderer.setClearColor(0xcce5ff, 1); var controls = new OrbitControls(camera, renderer.domElement); controls.enableDamping = true; @@ -52,11 +70,18 @@ function init(callback, update) { framework.scene = scene; framework.camera = camera; framework.renderer = renderer; + framework.aud=aud; + // begin the animation loop (function tick() { stats.begin(); - update(framework); // perform any requested updates + + analyser.getByteFrequencyData(data); // read audio data.. 1024B by default.. + framework.data=data; + //console.log(data); + + 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 @@ -72,4 +97,4 @@ export default { } export const PI = 3.14159265 -export const e = 2.7181718 \ No newline at end of file +export const e = 2.7181718 diff --git a/src/main.js b/src/main.js index 92b19a4..3578eda 100644 --- a/src/main.js +++ b/src/main.js @@ -1,8 +1,70 @@ - const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' -import Noise from './noise' -import {other} from './noise' + +// r,g,b to pass to shaders +var r=0.1; +var g=0.3; +var b=0.4; +var r1=0.8; +var g1=0.8; +var b1=0.8; +var s=0.0; +var noisetype=0; +var preset1=false; +var preset2=true; +var terrain=false; + +// function to manipulate stuff from gui +var GUIoptions = function() +{ + this.Red=0.1; + this.Green=0.3; + this.Blue=0.4; + this.Red1=0.8; + this.Green1=0.8; + this.Blue1=0.8; + this.Speed=0.0; + this.NoiseType=0; + this.Preset1=false; + this.Preset2=true; + this.Terrain=false; + // this.Value=true; + // this.RidgedValue=false; + // this.Music=false; + this.Source=function(){ + window.location = "https://github.com/rms13/Project1-Noise";}; +} + +// for time calculations +var oldt=0.0; +var newt=0.0; +var time=0; + +// material for geometry +var icoshMaterial = 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') + // }, + time: {value : 0.0}, + Red: {value : r}, + Green: {value : g}, + Blue: {value : b}, + Red1: {value : r1}, + Green1: {value : g1}, + Blue1: {value : b1}, + NoiseType: {value : 0}, + Preset1: {value : false}, + Preset2: {value : true}, + Terrain: {value : false} + // data: { + // type : 'iv1', + // value : new Array} + }, + vertexShader: require('./shaders/voronoi-vert.glsl'), + fragmentShader: require('./shaders/voronoi-frag.glsl') + }); // called after the scene loads function onLoad(framework) { @@ -11,50 +73,128 @@ function onLoad(framework) { var renderer = framework.renderer; var gui = framework.gui; var stats = framework.stats; + var data= framework.data; // per frame audio data + var aud= framework.aud; // audio object to control play/pause - // 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, data, aud} = framework; + + // initialize an icosahedron and material + var icosh = new THREE.IcosahedronBufferGeometry(1, 5); + //var icosh = new THREE.PlaneBufferGeometry( 20, 20, 100, 100 ); + //icosh.rotateX(90*3.14/180); + //icosh.scale(1,0.0001); + //var material = new THREE.MeshBasicMaterial( {color: 0xffffff, side: THREE.DoubleSide} ); + var icosh = new THREE.Mesh(icosh, icoshMaterial); + + // var wireframe = new THREE.WireframeGeometry( icosh ); + // var icosh = new THREE.LineSegments( wireframe ); + // icosh.material.depthTest = false; + // icosh.material.opacity = 0.25; + // icosh.material.transparent = true; + + //var geometry = new THREE.PlaneBufferGeometry( 20, 20, 100, 100 ); +// var material = new THREE.MeshBasicMaterial( {color: 0xffff00, side: THREE.DoubleSide} ); +// var plane = new THREE.Mesh( geometry, material ); +// scene.add( plane ); - // initialize a simple box and material - 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); // set camera position - camera.position.set(1, 1, 2); + camera.position.set(1, 4, 2); camera.lookAt(new THREE.Vector3(0,0,0)); - scene.add(adamCube); + // add icosh to the scene + scene.add(icosh); - // edit params and listen to changes like this - // more information here: https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage + // Elements for the GUI: gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + var update= new GUIoptions(); + gui.add(update,'Red', 0.0, 1.0, 0.05).onChange(function(newVal) { + r=newVal; + }); + gui.add(update,'Green', 0.0, 1.0, 0.05).onChange(function(newVal) { + g=newVal; + }); + gui.add(update,'Blue', 0.0, 1.0, 0.05).onChange(function(newVal) { + b=newVal; + }); + gui.add(update,'Red1', 0.0, 1.0, 0.05).onChange(function(newVal) { + r1=newVal; + }); + gui.add(update,'Green1', 0.0, 1.0, 0.05).onChange(function(newVal) { + g1=newVal; + }); + gui.add(update,'Blue1', 0.0, 1.0, 0.05).onChange(function(newVal) { + b1=newVal; + }); + gui.add(update,'Speed', 0.0, 4.0, 0.05).onChange(function(newVal) { + s=newVal; + //newt=0; + }); + gui.add(update,'NoiseType', 0, 2, 1).onChange(function(newVal) + { + noisetype = newVal; + }); + gui.add(update,'Preset1').onChange(function(newVal) + { + if(newVal==true) + { + preset1=true; + // preset2=false; + } + else + preset1=false; + }); + gui.add(update,'Preset2').onChange(function(newVal) + { + if(newVal==true) + { + preset2=true; + // preset1=false; + } + else + preset2=false; + }); + gui.add(update,'Terrain').onChange(function(newVal) + { + if(newVal==true) + terrain=true; + else + terrain=false; + }); + // gui.add(update,'Music').onChange(function(newVal) + // + // if(newVal===false) + // aud.pause(); + // else + // aud.play(); + // }); + gui.add(update,'Source').onclick; } // called on frame updates function onUpdate(framework) { - // console.log(`the time is ${new Date()}`); -} - -// when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate -Framework.init(onLoad, onUpdate); + icoshMaterial.uniforms.Red.value=r; + icoshMaterial.uniforms.Green.value=g; + icoshMaterial.uniforms.Blue.value=b; + icoshMaterial.uniforms.Red1.value=r1; + icoshMaterial.uniforms.Green1.value=g1; + icoshMaterial.uniforms.Blue1.value=b1; + icoshMaterial.uniforms.NoiseType.value=noisetype; + icoshMaterial.uniforms.Preset1.value=preset1; + icoshMaterial.uniforms.Preset2.value=preset2; + icoshMaterial.uniforms.Terrain.value=terrain; -// console.log('hello world'); + oldt=newt; + newt=performance.now(); // measures time since the beginning of execution + time+=s*(newt-oldt); -// console.log(Noise.generateNoise()); + //icoshMaterial.uniforms.data.value=Int32Array.from(framework.data); // typed arrays casting -// Noise.whatever() + icoshMaterial.uniforms.time.value=(100000.0+time)/25000; // control the speed of cloud movement +} -// console.log(other()) \ No newline at end of file +// when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate +Framework.init(onLoad, onUpdate); diff --git a/src/shaders/adam-frag.glsl b/src/shaders/adam-frag.glsl deleted file mode 100644 index 5dfa18c..0000000 --- a/src/shaders/adam-frag.glsl +++ /dev/null @@ -1,13 +0,0 @@ -varying vec2 vUv; -varying float noise; -uniform sampler2D image; - - -void main() { - - vec2 uv = vec2(1,1) - vUv; - vec4 color = texture2D( image, uv ); - - 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 deleted file mode 100644 index e4b8cc0..0000000 --- a/src/shaders/adam-vert.glsl +++ /dev/null @@ -1,6 +0,0 @@ - -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/icosh-frag.glsl b/src/shaders/icosh-frag.glsl new file mode 100644 index 0000000..f97018e --- /dev/null +++ b/src/shaders/icosh-frag.glsl @@ -0,0 +1,4 @@ +varying vec3 col; +void main() { + gl_FragColor = vec4( col.rgb, 1.0 ); +} \ No newline at end of file diff --git a/src/shaders/icosh-vert.glsl b/src/shaders/icosh-vert.glsl new file mode 100644 index 0000000..948d9f3 --- /dev/null +++ b/src/shaders/icosh-vert.glsl @@ -0,0 +1,143 @@ +uniform float time; +uniform float Red; +uniform float Green; +uniform float Blue; +varying vec3 col; +//uniform int data[1024]; // audio data.. all 0s when music is not playing + +float Noise(in int x, in int y, in int z) +{ + float a; + a=fract(sin(dot(vec3(x,y,z),vec3(12.9898,78.233,138.531))) * 43758.5453); + return a; +} +/* +float hash( float n ) +{ + return fract(sin(n)*43758.5453); +} + +float Noise(in int x, in int y, in int z) +{ + vec3 p = floor(x); + vec3 f = fract(x); + + f = f*f*(3.0-2.0*f); + + float n = p.x + p.y*57.0 + 113.0*p.z; + + float res = mix(mix(mix( hash(n+ 0.0), hash(n+ 1.0),f.x), + mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y), + mix(mix( hash(n+113.0), hash(n+114.0),f.x), + mix( hash(n+170.0), hash(n+171.0),f.x),f.y),f.z); + return res; +} +*/ +float smoothnoise(int x2, int y2, int z2) +{ + float noise; + float div; + for(float i=-1.0;i<2.0;i++) + { + for(float j=-1.0;j<2.0;j++) + { + for(float k=-1.0;k<2.0;k++) + { + if(abs(i)+abs(j)+abs(k)==3.0) + div=32.0; + else if(abs(i)+abs(j)+abs(k)==2.0) + div=16.0; + else if(abs(i)+abs(j)+abs(k)==1.0) + div=12.0; + else + div=4.0; + + noise += Noise(x2+int(i),y2+int(j),z2+int(k)) / div; + } + } + } + return noise; +} + +float interp(float a, float b, float f) +{ + float f1 = f * 3.1415927; + f1 = (1.0 - cos(f1)) * 0.5; + return a*(1.0-f1) + b*f1; +} + +float intnoise(float x1, float y1, float z1) +{ + int ix = int(x1); + int iy = int(y1); + int iz = int(z1); + float fx = x1-float(ix); + float fy = y1-float(iy); + float fz = z1-float(iz); + + float v1 = smoothnoise(ix,iy,iz); + float v2 = smoothnoise(ix,iy+1,iz); + float v3 = smoothnoise(ix,iy,iz+1); + float v4 = smoothnoise(ix,iy+1,iz+1); + float v5 = smoothnoise(ix+1,iy,iz); + float v6 = smoothnoise(ix+1,iy+1,iz); + float v7 = smoothnoise(ix+1,iy,iz+1); + float v8 = smoothnoise(ix+1,iy+1,iz+1); + + float i1 = interp(v1, v2, fy); + float i2 = interp(v3, v4, fy); + float i3 = interp(v5, v6, fy); + float i4 = interp(v7, v8, fy); + + float i5=interp(i1, i2, fz); + float i6=interp(i3, i4, fz); + + float i7=interp(i5, i6, fx); + + return i7; +} + +float noise3D(float x, float y, float z) +{ + float total = 0.0; + for(float i=0.0; i<8.0; i++) // octaves = 4 + { + float freq = pow(2.0,i); + float amp = pow(0.5,i); // persistence = 0.25 + total += intnoise(x * freq*4.0, y* freq*4.0, z * freq*4.0) * amp; + } + return total; +} + +/* +float noise2D(float x, float y, float z) +{ + float total = 0.0; + for(float i=0.0; i<8.0; i++) // octaves = 4 + { + float freq = pow(2.0,i); + float amp = pow(0.5,i); // persistence = 0.25 + total += intnoise(x * freq, y * freq, z * freq) * amp; + } + return total; +} +*/ + +void main() { + float tx,ty,tz; + tx=time; ty=time; tz=time; + + float n = noise3D((tx+position.x),(ty+position.y),(tz+position.z)); + + int ind = int(n*1024.0/75.0); + float d = float(data[ind])/255.0; //using the sound data.. this is 0 when music is not playing. + + vec3 pos = (n+n*float(data[30])/255.0)*normal; + + //col=abs(normalize(vec3(pos))); + //col=abs(vec3(n,1-n,1-n)); + //pos += vec3(d,d,d); + + col=abs(mix(vec3(1,1,1),vec3(Red*(1.0-d),Green*(1.0-d),Blue*(1.0-d)),n)); + gl_Position= projectionMatrix * modelViewMatrix * vec4( pos, 1.0 ); +} diff --git a/src/shaders/mountain-frag.glsl b/src/shaders/mountain-frag.glsl new file mode 100644 index 0000000..90b3944 --- /dev/null +++ b/src/shaders/mountain-frag.glsl @@ -0,0 +1,154 @@ +// THIS IS THE SHADER WHICH WILL PROVIDE THE TEXTURE TO THE TERRAIN. +// VALUE NOISE IS THE NOISE GENERATOR USED HERE (THE TERRAIN USES RIDGED VERSION). IT IS A MODIFICATION OF IQ'S VALUE NOISE GENERATOR. +// DERIVATIVES OF THE NOISE ARE USED TO SIMULATE EROSION EFFECTS. +// CHANGING THE SPEED IN THE GUI WILL ANIMATE THE NOISE AND EROSION [FOR VISUALIZATION]. + +varying vec2 vUv; +varying vec3 pos; + +uniform float Red; +uniform float Green; +uniform float Blue; +uniform float Red1; +uniform float Green1; +uniform float Blue1; + +uniform float time; +uniform int NoiseType; +uniform bool Preset1; +//uniform sampler2D image; + +// NOISE FUNCTIONS FROM: +// http://www.science-and-fiction.org/rendering/noise.html +float rand2D(in vec2 co){ + return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); +} +float rand3D(in vec3 co){ + return fract(sin(dot(co.xyz ,vec3(12.9898,78.233,144.7272))) * 43758.5453); +} + +// iq's value noise algorithm: +vec4 noised( in vec3 x ) +{ + vec3 p = floor(x); + vec3 w = fract(x); + + vec3 u = w*w*w*(w*(w*6.0-15.0)+10.0); + vec3 du = 30.0*w*w*(w*(w-2.0)+1.0); + + float a = rand3D( p+vec3(0,0,0) ); + float b = rand3D( p+vec3(1,0,0) ); + float c = rand3D( p+vec3(0,1,0) ); + float d = rand3D( p+vec3(1,1,0) ); + float e = rand3D( p+vec3(0,0,1) ); + float f = rand3D( p+vec3(1,0,1) ); + float g = rand3D( p+vec3(0,1,1) ); + float h = rand3D( p+vec3(1,1,1) ); + + float k0 = a; + float k1 = b - a; + float k2 = c - a; + float k3 = e - a; + float k4 = a - b - c + d; + float k5 = a - c - e + g; + float k6 = a - b - e + f; + float k7 = - a + b + c - d + e - f - g + h; + + return vec4( -1.0+2.0*(k0 + k1*u.x + k2*u.y + k3*u.z + k4*u.x*u.y + k5*u.y*u.z + k6*u.z*u.x + k7*u.x*u.y*u.z), + 2.0* du * vec3( k1 + k4*u.y + k6*u.z + k7*u.y*u.z, + k2 + k5*u.z + k4*u.x + k7*u.z*u.x, + k3 + k6*u.x + k5*u.y + k7*u.x*u.y ) ); +} + + +const mat3 m3 = mat3( 0.00, 0.80, 0.60, + -0.80, 0.36,-0.48, + -0.60,-0.48, 0.64 ); +const mat3 m3i = mat3( 0.00,-0.80,-0.60, + 0.80, 0.36,-0.48, + 0.60,-0.48, 0.64 ); +// FBM - PROCESSING FOR OCTAVES AND ACCUMULATING DERIVATIVES +vec4 fbm( in vec3 x) +{ + float f = 2.0; // could be 2.0 + float s = 0.5; // could be 0.5 + float a = 0.0; + float b = 0.5; + vec3 d = vec3(0.0); + mat3 m = mat3( 1.00, 0.00, 0.00, + 0.00, 1.00, 0.00, + 0.00, 0.00, 1.00); + //int oct = octaves; + for( int i=0; i < 8; i++ ) // octaves = 8.. + { + vec4 n = noised(x); + // ADDING RIDGES + if(NoiseType==1 && !Preset1) // ABS NOISE + n = abs(n); + else if(NoiseType==2 && !Preset1) // RIDGED NOISE + n = 1.0-abs(n); + + a += b*n.x; // accumulate values + d += b*m*n.yzw; // accumulate derivatives + b *= s; + x = f*m3*x; + m = f*m3i*m; + } + return vec4( a, d ); +} + +void main() { + //gl_FragColor = vec4( noise3(pos), 1.0 ); + + vec4 n = fbm(time*pos); + //float random = rand3D(pos); + vec3 col; + vec3 colBase; + vec3 colSnow; + + if(Preset1) // MOUNTAINS SHADER + { + // EARTH TONES: http://www.varian.net/dreamview/dreamcolor/earth.html + // col = clamp(n[0]+1.0,0.5,1.0) * vec3(0.37,0.27,0.18); + + //colBase = mix(vec3(115.,69.,35.)/4.0,vec3(95.,71.,47.)/4.0,n[0]*length(pos)) / 255.; // BLENDING LAYERS: Grayish base + Brownish soil + fine-tuning using noise (height and n).. + + colBase = mix(vec3(0.2,0.2,0.2),vec3(0.4,0.35,0.3),n[2]/5.0); + colBase = mix(colBase,vec3(0.15,0.15,0.15),n[0]*length(pos)); + + colSnow = vec3(1.,0.98,0.98); // SNOW: 255,250,250 + + // COL : 1. Snow at higher altitudes (length(pos)) blended with some randomness (n[0]) to avoid hard edges at a fixed height. + // 2. Simulate erosion using derivatives (n[1],n[2]). + // - High altitude and snow accumulation (colSnow) -> low alt (colBase) + // - equivalent to (I guess) Higher noise value to lower noise value + // - equivalent to (I guess) subtracting the derivatives. Seems to work. + col = mix(colBase,colSnow,(length(pos)-3.0*n[0]-2.0*n[1]-n[2])/10.0); + + col = max(colBase,col); // CLAMPING LOWEST VALUES TO BASE COLOR + } + else // ADJUSTABLE COLORS FOR DEBUGGING + { + float no = n[0]; + float r = mix(Red,Red1,no); + float g = mix(Green,Green1,no); + float b = mix(Blue,Blue1,no); + + col = vec3(r,g,b); + + //col = vec3(n[0],n[0],n[0])*vec3(1.0,0.5,0.1)+vec3(0.2,0.2,0.2); + } + + col = sqrt(col); // gamma correction + gl_FragColor = vec4( col.rgb, 1.0 ); +} + + +/* // OLD SHADER - DOES NOTHING.. GETS THE COLOR FROM VERTEX SHADER AND PLOTS IT +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/mountain-vert.glsl b/src/shaders/mountain-vert.glsl new file mode 100644 index 0000000..44eaf8f --- /dev/null +++ b/src/shaders/mountain-vert.glsl @@ -0,0 +1,108 @@ +// THIS IS NOT THE TERRAIN WE ARE GOING TO USE FOR THE FINAL SUBMISSION. +// THIS IS JUST FOR TESTING THE TEXTURE. +// RUDRAKSHA IS WORKING ON THE TERRAIN MODELING PART. + +varying vec2 vUv; +varying vec3 pos; +uniform bool Terrain; + +// NOISE FUNCTIONS FROM: +// http://www.science-and-fiction.org/rendering/noise.html +float rand2D(in vec2 co){ + return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); +} +float rand3D(in vec3 co){ + return fract(sin(dot(co.xyz ,vec3(12.9898,78.233,144.7272))) * 43758.5453); +} + +// iq's value noise algorithm: +vec4 noised( in vec3 x ) +{ + vec3 p = floor(x); + vec3 w = fract(x); + + vec3 u = w*w*w*(w*(w*6.0-15.0)+10.0); + vec3 du = 30.0*w*w*(w*(w-2.0)+1.0); + + float a = rand3D( p+vec3(0,0,0) ); + float b = rand3D( p+vec3(1,0,0) ); + float c = rand3D( p+vec3(0,1,0) ); + float d = rand3D( p+vec3(1,1,0) ); + float e = rand3D( p+vec3(0,0,1) ); + float f = rand3D( p+vec3(1,0,1) ); + float g = rand3D( p+vec3(0,1,1) ); + float h = rand3D( p+vec3(1,1,1) ); + + float k0 = a; + float k1 = b - a; + float k2 = c - a; + float k3 = e - a; + float k4 = a - b - c + d; + float k5 = a - c - e + g; + float k6 = a - b - e + f; + float k7 = - a + b + c - d + e - f - g + h; + + return vec4( -1.0+2.0*(k0 + k1*u.x + k2*u.y + k3*u.z + k4*u.x*u.y + k5*u.y*u.z + k6*u.z*u.x + k7*u.x*u.y*u.z), + 2.0* du * vec3( k1 + k4*u.y + k6*u.z + k7*u.y*u.z, + k2 + k5*u.z + k4*u.x + k7*u.z*u.x, + k3 + k6*u.x + k5*u.y + k7*u.x*u.y ) ); +} + + +const mat3 m3 = mat3( 0.00, 0.80, 0.60, + -0.80, 0.36,-0.48, + -0.60,-0.48, 0.64 ); +const mat3 m3i = mat3( 0.00,-0.80,-0.60, + 0.80, 0.36,-0.48, + 0.60,-0.48, 0.64 ); +// FBM - PROCESSING FOR OCTAVES AND ACCUMULATING DERIVATIVES +vec4 fbm( in vec3 x) +{ + float f = 2.0; // could be 2.0 + float s = 0.5; // could be 0.5 + float a = 0.0; + float b = 0.5; + vec3 d = vec3(0.0); + mat3 m = mat3( 1.00, 0.00, 0.00, + 0.00, 1.00, 0.00, + 0.00, 0.00, 1.00); + //int oct = octaves; + for( int i=0; i < 8; i++ ) // octaves = 8.. + { + vec4 n = noised(x); + // ADDING RIDGES + n = 1.0-abs(n); + + a += b*n.x; // accumulate values + d += b*m*n.yzw; // accumulate derivatives + b *= s; + x = f*m3*x; + m = f*m3i*m; + } + return vec4( a, d ); +} + +void main() +{ + vUv = uv; + + if(Terrain) + { + vec4 n = fbm(position); + pos = (n[0]*0.5+0.5) * normal; + } + else + { + pos=position; + } + + gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 ); +} + +/* // OLD SHADER : UNCOMMENT THIS AND COMMENT EVERYTHING ELSE TO GET OLD SPHERE SHADER +void main() { + vUv = uv; + pos=position; + gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); +} +*/ diff --git a/src/shaders/sinenoise-frag.glsl b/src/shaders/sinenoise-frag.glsl new file mode 100644 index 0000000..f007fad --- /dev/null +++ b/src/shaders/sinenoise-frag.glsl @@ -0,0 +1,157 @@ +// THIS IS THE SHADER WHICH WILL PROVIDE THE TEXTURE TO THE TERRAIN. +// VALUE NOISE IS THE NOISE GENERATOR USED HERE (THE TERRAIN USES RIDGED VERSION). IT IS A MODIFICATION OF IQ'S VALUE NOISE GENERATOR. +// DERIVATIVES OF THE NOISE ARE USED TO SIMULATE EROSION EFFECTS. +// CHANGING THE SPEED IN THE GUI WILL ANIMATE THE NOISE AND EROSION [FOR VISUALIZATION]. + +varying vec2 vUv; +varying vec3 pos; + +uniform float Red; +uniform float Green; +uniform float Blue; +uniform float Red1; +uniform float Green1; +uniform float Blue1; + +uniform float time; +uniform int NoiseType; +uniform bool Preset1; +uniform bool Preset2; +//uniform sampler2D image; + +// NOISE FUNCTIONS FROM: +// http://www.science-and-fiction.org/rendering/noise.html +float rand2D(in vec2 co){ + return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); +} +float rand3D(in vec3 co){ + return fract(sin(dot(co.xyz ,vec3(12.9898,78.233,144.7272))) * 43758.5453); +} + +// iq's value noise algorithm: +vec4 noised( in vec3 x ) +{ + vec3 p = floor(x); + vec3 w = fract(x); + + vec3 u = w*w*w*(w*(w*6.0-15.0)+10.0); + vec3 du = 30.0*w*w*(w*(w-2.0)+1.0); + + float a = rand3D( p+vec3(0,0,0) ); + float b = rand3D( p+vec3(1,0,0) ); + float c = rand3D( p+vec3(0,1,0) ); + float d = rand3D( p+vec3(1,1,0) ); + float e = rand3D( p+vec3(0,0,1) ); + float f = rand3D( p+vec3(1,0,1) ); + float g = rand3D( p+vec3(0,1,1) ); + float h = rand3D( p+vec3(1,1,1) ); + + float k0 = a; + float k1 = b - a; + float k2 = c - a; + float k3 = e - a; + float k4 = a - b - c + d; + float k5 = a - c - e + g; + float k6 = a - b - e + f; + float k7 = - a + b + c - d + e - f - g + h; + + return vec4( -1.0+2.0*(k0 + k1*u.x + k2*u.y + k3*u.z + k4*u.x*u.y + k5*u.y*u.z + k6*u.z*u.x + k7*u.x*u.y*u.z), + 2.0* du * vec3( k1 + k4*u.y + k6*u.z + k7*u.y*u.z, + k2 + k5*u.z + k4*u.x + k7*u.z*u.x, + k3 + k6*u.x + k5*u.y + k7*u.x*u.y ) ); +} + + +const mat3 m3 = mat3( 0.00, 0.80, 0.60, + -0.80, 0.36,-0.48, + -0.60,-0.48, 0.64 ); +const mat3 m3i = mat3( 0.00,-0.80,-0.60, + 0.80, 0.36,-0.48, + 0.60,-0.48, 0.64 ); +// FBM - PROCESSING FOR OCTAVES AND ACCUMULATING DERIVATIVES +vec4 fbm( in vec3 x) +{ + float f = 2.0; // could be 2.0 + float s = 0.5; // could be 0.5 + float a = 0.0; + float b = 0.5; + vec3 d = vec3(0.0); + mat3 m = mat3( 1.00, 0.00, 0.00, + 0.00, 1.00, 0.00, + 0.00, 0.00, 1.00); + //int oct = octaves; + for( int i=0; i < 8; i++ ) // octaves = 8.. + { + vec4 n = noised(x); + // ADDING RIDGES + if(NoiseType==1 && !Preset1) // ABS NOISE + n = abs(n); + else if(NoiseType==2 && !Preset1) // RIDGED NOISE + n = 1.0-abs(n); + + a += b*n.x; // accumulate values + d += b*m*n.yzw; // accumulate derivatives + b *= s; + x = f*m3*x; + m = f*m3i*m; + } + return vec4( a, d ); +} + +void main() { + //gl_FragColor = vec4( noise3(pos), 1.0 ); + + vec4 n = fbm(time*pos); + //float random = rand3D(pos); + vec3 col; + vec3 colBase; + vec3 colSnow; + + if(Preset1) // MOUNTAINS SHADER + { + // EARTH TONES: http://www.varian.net/dreamview/dreamcolor/earth.html + // col = clamp(n[0]+1.0,0.5,1.0) * vec3(0.37,0.27,0.18); + + //n[0] =(1.0+(sin(n[0]*60.0)/2.0)); + + colBase = mix(vec3(115.,69.,35.)/4.0,vec3(95.,71.,47.)/4.0,n[0]*length(pos)) / 255.; // BLENDING LAYERS: Grayish base + Brownish soil + fine-tuning using noise (height and n).. + colSnow = vec3(1.,0.98,0.98); // SNOW: 255,250,250 + + // COL : 1. Snow at higher altitudes (length(pos)) blended with some randomness (n[0]) to avoid hard edges at a fixed height. + // 2. Simulate erosion using derivatives (n[1],n[2]). + // - High altitude and snow accumulation (colSnow) -> low alt (colBase) + // - equivalent to (I guess) Higher noise value to lower noise value + // - equivalent to (I guess) subtracting the derivatives. Seems to work. + col = mix(colBase,colSnow,(length(pos)-3.0*n[0]-2.0*n[1]-n[2])/5.0); + + col = max(colBase,col); // CLAMPING LOWEST VALUES TO BASE COLOR + } + else // ADJUSTABLE COLORS FOR DEBUGGING + { + float no = n[0]; + //col = vec3(1.0+(sin((pos.x*10.0+n[0])*60.0)/2.0)); + if(Preset2) + col = vec3(1.0+(sin((n[0])*60.0)/2.0)); + else + { + float r = mix(Red,Red1,no); + float g = mix(Green,Green1,no); + float b = mix(Blue,Blue1,no); + col = vec3(r,g,b); + } + //col = vec3(n[0],n[0],n[0])*vec3(1.0,0.5,0.1)+vec3(0.2,0.2,0.2); + } + + col = sqrt(col); // gamma correction + gl_FragColor = vec4( col.rgb, 1.0 ); +} + + +/* // OLD SHADER - DOES NOTHING.. GETS THE COLOR FROM VERTEX SHADER AND PLOTS IT +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/sinenoise-vert.glsl b/src/shaders/sinenoise-vert.glsl new file mode 100644 index 0000000..44eaf8f --- /dev/null +++ b/src/shaders/sinenoise-vert.glsl @@ -0,0 +1,108 @@ +// THIS IS NOT THE TERRAIN WE ARE GOING TO USE FOR THE FINAL SUBMISSION. +// THIS IS JUST FOR TESTING THE TEXTURE. +// RUDRAKSHA IS WORKING ON THE TERRAIN MODELING PART. + +varying vec2 vUv; +varying vec3 pos; +uniform bool Terrain; + +// NOISE FUNCTIONS FROM: +// http://www.science-and-fiction.org/rendering/noise.html +float rand2D(in vec2 co){ + return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); +} +float rand3D(in vec3 co){ + return fract(sin(dot(co.xyz ,vec3(12.9898,78.233,144.7272))) * 43758.5453); +} + +// iq's value noise algorithm: +vec4 noised( in vec3 x ) +{ + vec3 p = floor(x); + vec3 w = fract(x); + + vec3 u = w*w*w*(w*(w*6.0-15.0)+10.0); + vec3 du = 30.0*w*w*(w*(w-2.0)+1.0); + + float a = rand3D( p+vec3(0,0,0) ); + float b = rand3D( p+vec3(1,0,0) ); + float c = rand3D( p+vec3(0,1,0) ); + float d = rand3D( p+vec3(1,1,0) ); + float e = rand3D( p+vec3(0,0,1) ); + float f = rand3D( p+vec3(1,0,1) ); + float g = rand3D( p+vec3(0,1,1) ); + float h = rand3D( p+vec3(1,1,1) ); + + float k0 = a; + float k1 = b - a; + float k2 = c - a; + float k3 = e - a; + float k4 = a - b - c + d; + float k5 = a - c - e + g; + float k6 = a - b - e + f; + float k7 = - a + b + c - d + e - f - g + h; + + return vec4( -1.0+2.0*(k0 + k1*u.x + k2*u.y + k3*u.z + k4*u.x*u.y + k5*u.y*u.z + k6*u.z*u.x + k7*u.x*u.y*u.z), + 2.0* du * vec3( k1 + k4*u.y + k6*u.z + k7*u.y*u.z, + k2 + k5*u.z + k4*u.x + k7*u.z*u.x, + k3 + k6*u.x + k5*u.y + k7*u.x*u.y ) ); +} + + +const mat3 m3 = mat3( 0.00, 0.80, 0.60, + -0.80, 0.36,-0.48, + -0.60,-0.48, 0.64 ); +const mat3 m3i = mat3( 0.00,-0.80,-0.60, + 0.80, 0.36,-0.48, + 0.60,-0.48, 0.64 ); +// FBM - PROCESSING FOR OCTAVES AND ACCUMULATING DERIVATIVES +vec4 fbm( in vec3 x) +{ + float f = 2.0; // could be 2.0 + float s = 0.5; // could be 0.5 + float a = 0.0; + float b = 0.5; + vec3 d = vec3(0.0); + mat3 m = mat3( 1.00, 0.00, 0.00, + 0.00, 1.00, 0.00, + 0.00, 0.00, 1.00); + //int oct = octaves; + for( int i=0; i < 8; i++ ) // octaves = 8.. + { + vec4 n = noised(x); + // ADDING RIDGES + n = 1.0-abs(n); + + a += b*n.x; // accumulate values + d += b*m*n.yzw; // accumulate derivatives + b *= s; + x = f*m3*x; + m = f*m3i*m; + } + return vec4( a, d ); +} + +void main() +{ + vUv = uv; + + if(Terrain) + { + vec4 n = fbm(position); + pos = (n[0]*0.5+0.5) * normal; + } + else + { + pos=position; + } + + gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 ); +} + +/* // OLD SHADER : UNCOMMENT THIS AND COMMENT EVERYTHING ELSE TO GET OLD SPHERE SHADER +void main() { + vUv = uv; + pos=position; + gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); +} +*/ diff --git a/src/shaders/voronoi-frag.glsl b/src/shaders/voronoi-frag.glsl new file mode 100644 index 0000000..fef830a --- /dev/null +++ b/src/shaders/voronoi-frag.glsl @@ -0,0 +1,217 @@ +// THIS IS THE SHADER WHICH WILL PROVIDE THE TEXTURE TO THE TERRAIN. +// VALUE NOISE IS THE NOISE GENERATOR USED HERE (THE TERRAIN USES RIDGED VERSION). IT IS A MODIFICATION OF IQ'S VALUE NOISE GENERATOR. +// DERIVATIVES OF THE NOISE ARE USED TO SIMULATE EROSION EFFECTS. +// CHANGING THE SPEED IN THE GUI WILL ANIMATE THE NOISE AND EROSION [FOR VISUALIZATION]. + +varying vec2 vUv; +varying vec3 pos; + +uniform float Red; +uniform float Green; +uniform float Blue; +uniform float Red1; +uniform float Green1; +uniform float Blue1; + +uniform float time; +uniform int NoiseType; +uniform bool Preset1; +uniform bool Preset2; +//uniform sampler2D image; + +// NOISE FUNCTIONS FROM: +// http://www.science-and-fiction.org/rendering/noise.html +float rand2D(in vec2 co){ + return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); +} +float rand3D(in vec3 co){ + return fract(sin(dot(co.xyz ,vec3(12.9898,78.233,144.7272))) * 43758.5453); +} + +// iq's value noise algorithm: +vec4 noised( in vec3 x ) +{ + vec3 p = floor(x); + vec3 w = fract(x); + + vec3 u = w*w*w*(w*(w*6.0-15.0)+10.0); + vec3 du = 30.0*w*w*(w*(w-2.0)+1.0); + + float a = rand3D( p+vec3(0,0,0) ); + float b = rand3D( p+vec3(1,0,0) ); + float c = rand3D( p+vec3(0,1,0) ); + float d = rand3D( p+vec3(1,1,0) ); + float e = rand3D( p+vec3(0,0,1) ); + float f = rand3D( p+vec3(1,0,1) ); + float g = rand3D( p+vec3(0,1,1) ); + float h = rand3D( p+vec3(1,1,1) ); + + float k0 = a; + float k1 = b - a; + float k2 = c - a; + float k3 = e - a; + float k4 = a - b - c + d; + float k5 = a - c - e + g; + float k6 = a - b - e + f; + float k7 = - a + b + c - d + e - f - g + h; + + return vec4( -1.0+2.0*(k0 + k1*u.x + k2*u.y + k3*u.z + k4*u.x*u.y + k5*u.y*u.z + k6*u.z*u.x + k7*u.x*u.y*u.z), + 2.0* du * vec3( k1 + k4*u.y + k6*u.z + k7*u.y*u.z, + k2 + k5*u.z + k4*u.x + k7*u.z*u.x, + k3 + k6*u.x + k5*u.y + k7*u.x*u.y ) ); +} + + +const mat3 m3 = mat3( 0.00, 0.80, 0.60, + -0.80, 0.36,-0.48, + -0.60,-0.48, 0.64 ); +const mat3 m3i = mat3( 0.00,-0.80,-0.60, + 0.80, 0.36,-0.48, + 0.60,-0.48, 0.64 ); +// FBM - PROCESSING FOR OCTAVES AND ACCUMULATING DERIVATIVES +vec4 fbm( in vec3 x) +{ + float f = 2.0; // could be 2.0 + float s = 0.5; // could be 0.5 + float a = 0.0; + float b = 0.5; + vec3 d = vec3(0.0); + mat3 m = mat3( 1.00, 0.00, 0.00, + 0.00, 1.00, 0.00, + 0.00, 0.00, 1.00); + //int oct = octaves; + for( int i=0; i < 10; i++ ) // octaves = 8.. + { + vec4 n = noised(x); + // ADDING RIDGES + if(NoiseType==1 && !Preset1) // ABS NOISE + n = abs(n); + else if(NoiseType==2 && !Preset1) // RIDGED NOISE + n = 1.0-abs(n); + + a += b*n.x; // accumulate values + d += b*m*n.yzw; // accumulate derivatives + b *= s; + x = f*m3*x; + m = f*m3i*m; + } + return vec4( a, d ); +} + +vec3 voronoi( in vec3 x , in vec4 n ) +{ + vec3 p = floor( x ); + vec3 f = fract( x ); + + float id = 0.0; + vec2 res = vec2( 8.0 ); + for( int k=-1; k<=1; k++ ) + for( int j=-1; j<=1; j++ ) + for( int i=-1; i<=1; i++ ) + { + vec3 b = vec3( float(i), float(j), float(k) ); + vec3 r = vec3( b ) - f + rand3D( p + b ); + float d = dot( r, r ); + + if( d < res.x ) + { + id = dot( p+b , vec3(1.0,57.0,113.0 ) ); + res = vec2( d, res.x ); + } + else if( d < res.y ) + { + res.y = d; + } + } + + //return pow( 1.0/res, 1.0/16.0 ); + return vec3( sqrt( res ), abs(id) ); +} + +// SMOOTH VORONOI +float voronoi( in vec3 x ) +{ + vec3 p = floor( x ); + vec3 f = fract( x ); + + float res = 100.0; + for( int k=-1; k<=1; k++ ) + for( int j=-1; j<=1; j++ ) + for( int i=-1; i<=1; i++ ) + { + vec3 b = vec3( float(i), float(j), float(k) ); + vec3 r = vec3( b ) - f + rand3D( p + b ); + float d = dot( r, r ); + + //res = min(d,res); + res += 1.0/pow( d, 16.0 ); + } + + return pow( 1.0/res, 1.0/32.0 ); + //return sqrt( res ); +} + +void main() +{ + vec4 n = fbm(time*pos); + vec3 voro = voronoi((150.0*pos),n); //90 + + // SMOOTH VORONOI + //float voro = voronoi((5.0*pos)); + + vec3 col; + vec3 colBase; + vec3 colSnow; + vec3 colForest; + + if(Preset1) + { + //float vn = voro; + float vn = voro[1]*voro[0];//smoothstep(0.0,0.9,voro[0]); + + // BASE + colBase = mix(vec3(0.2,0.2,0.2),vec3(0.4,0.35,0.3),vn); + colBase = mix(colBase,vec3(0.15,0.15,0.15),n[0]*length(pos)); + + // FOREST + colForest = mix(vec3(0.1,0.15,0.05),vec3(0.15,0.24,0.13),vn); + colForest = mix(colForest,colBase,n[0]*length(pos)); + colBase = mix(colForest,colBase,0.5+0.5*n[0]*length(pos)); + + // SNOW + colSnow = vec3(1.,0.98,0.98); // SNOW: 255,250,250 + col = mix(colBase,colSnow,(length(pos)-3.0*n[0]-2.0*n[1]-n[2])/10.0); + + col = max(colBase,col); // CLAMPING LOWEST VALUES TO BASE COLOR + } + else if(!Preset2) + { + col = vec3(1.0+(sin((voro[1]-voro[0])*120.0)/2.0)); + } + else // ADJUSTABLE COLORS FOR DEBUGGING + { + float no = voro[1]*voro[0]; + //float no = voro;//[1]-voro[0];//(1.0+(sin((1.0-voro[0]))/2.0)); + + float r = mix(Red,Red1,no); + float g = mix(Green,Green1,no); + float b = mix(Blue,Blue1,no); + col = vec3(r,g,b); + + //col = vec3(n[0],n[0],n[0])*vec3(1.0,0.5,0.1)+vec3(0.2,0.2,0.2); + //col = voronoi(time*pos); + } + + col = sqrt(col); // gamma correction + gl_FragColor = vec4( col.rgb, 1.0 ); +} + + +/* // OLD SHADER - DOES NOTHING.. GETS THE COLOR FROM VERTEX SHADER AND PLOTS IT +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/voronoi-vert.glsl b/src/shaders/voronoi-vert.glsl new file mode 100644 index 0000000..44eaf8f --- /dev/null +++ b/src/shaders/voronoi-vert.glsl @@ -0,0 +1,108 @@ +// THIS IS NOT THE TERRAIN WE ARE GOING TO USE FOR THE FINAL SUBMISSION. +// THIS IS JUST FOR TESTING THE TEXTURE. +// RUDRAKSHA IS WORKING ON THE TERRAIN MODELING PART. + +varying vec2 vUv; +varying vec3 pos; +uniform bool Terrain; + +// NOISE FUNCTIONS FROM: +// http://www.science-and-fiction.org/rendering/noise.html +float rand2D(in vec2 co){ + return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); +} +float rand3D(in vec3 co){ + return fract(sin(dot(co.xyz ,vec3(12.9898,78.233,144.7272))) * 43758.5453); +} + +// iq's value noise algorithm: +vec4 noised( in vec3 x ) +{ + vec3 p = floor(x); + vec3 w = fract(x); + + vec3 u = w*w*w*(w*(w*6.0-15.0)+10.0); + vec3 du = 30.0*w*w*(w*(w-2.0)+1.0); + + float a = rand3D( p+vec3(0,0,0) ); + float b = rand3D( p+vec3(1,0,0) ); + float c = rand3D( p+vec3(0,1,0) ); + float d = rand3D( p+vec3(1,1,0) ); + float e = rand3D( p+vec3(0,0,1) ); + float f = rand3D( p+vec3(1,0,1) ); + float g = rand3D( p+vec3(0,1,1) ); + float h = rand3D( p+vec3(1,1,1) ); + + float k0 = a; + float k1 = b - a; + float k2 = c - a; + float k3 = e - a; + float k4 = a - b - c + d; + float k5 = a - c - e + g; + float k6 = a - b - e + f; + float k7 = - a + b + c - d + e - f - g + h; + + return vec4( -1.0+2.0*(k0 + k1*u.x + k2*u.y + k3*u.z + k4*u.x*u.y + k5*u.y*u.z + k6*u.z*u.x + k7*u.x*u.y*u.z), + 2.0* du * vec3( k1 + k4*u.y + k6*u.z + k7*u.y*u.z, + k2 + k5*u.z + k4*u.x + k7*u.z*u.x, + k3 + k6*u.x + k5*u.y + k7*u.x*u.y ) ); +} + + +const mat3 m3 = mat3( 0.00, 0.80, 0.60, + -0.80, 0.36,-0.48, + -0.60,-0.48, 0.64 ); +const mat3 m3i = mat3( 0.00,-0.80,-0.60, + 0.80, 0.36,-0.48, + 0.60,-0.48, 0.64 ); +// FBM - PROCESSING FOR OCTAVES AND ACCUMULATING DERIVATIVES +vec4 fbm( in vec3 x) +{ + float f = 2.0; // could be 2.0 + float s = 0.5; // could be 0.5 + float a = 0.0; + float b = 0.5; + vec3 d = vec3(0.0); + mat3 m = mat3( 1.00, 0.00, 0.00, + 0.00, 1.00, 0.00, + 0.00, 0.00, 1.00); + //int oct = octaves; + for( int i=0; i < 8; i++ ) // octaves = 8.. + { + vec4 n = noised(x); + // ADDING RIDGES + n = 1.0-abs(n); + + a += b*n.x; // accumulate values + d += b*m*n.yzw; // accumulate derivatives + b *= s; + x = f*m3*x; + m = f*m3i*m; + } + return vec4( a, d ); +} + +void main() +{ + vUv = uv; + + if(Terrain) + { + vec4 n = fbm(position); + pos = (n[0]*0.5+0.5) * normal; + } + else + { + pos=position; + } + + gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 ); +} + +/* // OLD SHADER : UNCOMMENT THIS AND COMMENT EVERYTHING ELSE TO GET OLD SPHERE SHADER +void main() { + vUv = uv; + pos=position; + gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); +} +*/