From d840e3a126185f523d0e929a284f9e683c8d2aa5 Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Mon, 23 Jan 2017 18:02:29 -0500 Subject: [PATCH 1/7] inital commit, custom shader working --- explosion.png | Bin 0 -> 3768 bytes npm-debug.log | 36 ++++++++++++++++++++++++++++ src/main.js | 14 ++++++++++- src/shaders/adam-frag.glsl | 1 - src/shaders/sphere-frag.glsl | 10 ++++++++ src/shaders/sphere-vert.glsl | 45 +++++++++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 explosion.png create mode 100644 npm-debug.log create mode 100644 src/shaders/sphere-frag.glsl create mode 100644 src/shaders/sphere-vert.glsl diff --git a/explosion.png b/explosion.png new file mode 100644 index 0000000000000000000000000000000000000000..797b65c35d1011114e6e0f848efdb2e25630dbe6 GIT binary patch literal 3768 zcmV;p4oC5cP)KLZ*U+IBfRsybQWXdwQbLP>6pAqfylh#{fb6;Z(vMMVS~$e@S=j*ftg6;Uhf59&ghTmgWD0l;*T zI709Y^p6lP1rIRMx#05C~cW=H_Aw*bJ-5DT&Z2n+x)QHX^p z00esgV8|mQcmRZ%02D^@S3L16t`O%c004NIvOKvYIYoh62rY33S640`D9%Y2D-rV&neh&#Q1i z007~1e$oCcFS8neI|hJl{-P!B1ZZ9hpmq0)X0i`JwE&>$+E?>%_LC6RbVIkUx0b+_+BaR3cnT7Zv!AJxW zizFb)h!jyGOOZ85F;a?DAXP{m@;!0_IfqH8(HlgRxt7s3}k3K`kFu>>-2Q$QMFfPW!La{h336o>X zu_CMttHv6zR;&ZNiS=X8v3CR#fknUxHUxJ0uoBa_M6WNWeqIg~6QE69c9o#eyhGvpiOA@W-aonk<7r1(?fC{oI5N*U!4 zfg=2N-7=cNnjjOr{yriy6mMFgG#l znCF=fnQv8CDz++o6_Lscl}eQ+l^ZHARH>?_s@|##Rr6KLRFA1%Q+=*RRWnoLsR`7U zt5vFIcfW3@?wFpwUVxrVZ>QdQz32KIeJ}k~{cZZE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l2n)VO8ep>HE~>+IMV&w_IoxFT_Nd13Zv1j>*y<%+N0RY)eTkOptokqsk#qn}1Qt&bDHu4QDn-Kb zlfPmTyF5ojmyhcxVP(FVjH#8&^4yelun0C?ZdkSJ*=4G1C+)=ncFDRK-lSA*=AaLn zZCj0D?tOBY;sLhg)zHCB?CSm92Y^=DdAV^V^%$NCu@|ZvuutB{-S|0?z@EMPp (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:212:16) +10 verbose stack at EventEmitter.emit (events.js:98:17) +10 verbose stack at ChildProcess. (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:14:12) +10 verbose stack at ChildProcess.emit (events.js:98:17) +10 verbose stack at maybeClose (child_process.js:766:16) +10 verbose stack at Process.ChildProcess._handle.onexit (child_process.js:833:5) +11 verbose pkgid @ +12 verbose cwd /Users/nicknewberg/School/Senior/CIS700/HW1/Project1-Noise +13 error Darwin 15.6.0 +14 error argv "node" "/usr/local/bin/npm" "start" +15 error node v0.10.35 +16 error npm v2.1.18 +17 error code ELIFECYCLE +18 error @ start: `webpack-dev-server --hot --inline` +18 error Exit status 8 +19 error Failed at the @ start script 'webpack-dev-server --hot --inline'. +19 error This is most likely a problem with the package, +19 error not with npm itself. +19 error Tell the author that this fails on your system: +19 error webpack-dev-server --hot --inline +19 error You can get their info via: +19 error npm owner ls +19 error There is likely additional logging output above. +20 verbose exit [ 1, true ] diff --git a/src/main.js b/src/main.js index 92b19a4..a639cf0 100644 --- a/src/main.js +++ b/src/main.js @@ -30,11 +30,23 @@ function onLoad(framework) { }); var adamCube = new THREE.Mesh(box, adamMaterial); + var sphereMaterial = new THREE.ShaderMaterial({ + uniforms: { + uTime: {value: 1.0} + }, + vertexShader: require('./shaders/sphere-vert.glsl'), + fragmentShader: require('./shaders/sphere-frag.glsl') + }); + + var sphere = new THREE.IcosahedronBufferGeometry(1, 1); + var mySphere =new THREE.Mesh(sphere, sphereMaterial); + // set camera position camera.position.set(1, 1, 2); camera.lookAt(new THREE.Vector3(0,0,0)); - scene.add(adamCube); + //scene.add(adamCube); + scene.add(mySphere); // edit params and listen to changes like this // more information here: https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage diff --git a/src/shaders/adam-frag.glsl b/src/shaders/adam-frag.glsl index 5dfa18c..cc00487 100644 --- a/src/shaders/adam-frag.glsl +++ b/src/shaders/adam-frag.glsl @@ -2,7 +2,6 @@ varying vec2 vUv; varying float noise; uniform sampler2D image; - void main() { vec2 uv = vec2(1,1) - vUv; diff --git a/src/shaders/sphere-frag.glsl b/src/shaders/sphere-frag.glsl new file mode 100644 index 0000000..fa27aff --- /dev/null +++ b/src/shaders/sphere-frag.glsl @@ -0,0 +1,10 @@ +varying vec2 vUv; +varying vec3 vNormal; +varying float noise; + +void main() { + vec2 uv = vec2(1,1) - vUv; + vec4 color = vec4( vNormal, 1.0 ); + + gl_FragColor = vec4( color.rgb, 1.0 ); +} \ No newline at end of file diff --git a/src/shaders/sphere-vert.glsl b/src/shaders/sphere-vert.glsl new file mode 100644 index 0000000..a8d845c --- /dev/null +++ b/src/shaders/sphere-vert.glsl @@ -0,0 +1,45 @@ +#define M_PI 3.1415926535897932384626433832795 +#define N_OCTAVES 5 +varying vec2 vUv; +varying vec3 vNormal; +uniform float uTime; + + +float rand(vec2 co){ + return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); +} + +float lerp(float a, float b, float t){ + return (a * (1.0 - t) + b * t); +} + +float cosine_interpolate(float a, float b, float t){ + float cos_t = (1.0 - cos(t * M_PI)) * 0.5; + return lerp(a, b, cos_t); +} + +float PerlinNoise3D(float x, float y, float z){ + float total = 0.0; + float persistance = 1.0 / 2.0; + + for (int i = 0; i < N_OCTAVES; ++i){ + + float frequency = pow(2.0, float(i)); + float amplitude = pow(persistance, float(i)); + + //TODO: + //sum the total from a noise function + } + + return 0.0; + +} + +void main() { + vUv = uv; + vNormal = normal; + float noiseOffset = rand(uv); + gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0 ); +} + + From 9daf5d9539f8ff74dc8cdf7cbea7c394280640f9 Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Mon, 23 Jan 2017 22:46:50 -0500 Subject: [PATCH 2/7] Perlin Noise working --- src/main.js | 10 ++++- src/shaders/sphere-frag.glsl | 3 +- src/shaders/sphere-vert.glsl | 73 +++++++++++++++++++++++++++++++----- 3 files changed, 75 insertions(+), 11 deletions(-) diff --git a/src/main.js b/src/main.js index a639cf0..160aa74 100644 --- a/src/main.js +++ b/src/main.js @@ -18,6 +18,7 @@ function onLoad(framework) { // 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 @@ -32,13 +33,17 @@ function onLoad(framework) { var sphereMaterial = new THREE.ShaderMaterial({ uniforms: { + image: { + type: "t", + value: THREE.ImageUtils.loadTexture('./explosion.png') + }, uTime: {value: 1.0} }, vertexShader: require('./shaders/sphere-vert.glsl'), fragmentShader: require('./shaders/sphere-frag.glsl') }); - var sphere = new THREE.IcosahedronBufferGeometry(1, 1); + var sphere = new THREE.IcosahedronBufferGeometry(1, 5); var mySphere =new THREE.Mesh(sphere, sphereMaterial); // set camera position @@ -55,9 +60,12 @@ function onLoad(framework) { }); } + // called on frame updates function onUpdate(framework) { // console.log(`the time is ${new Date()}`); + //var time = new Date().getMilliseconds(); + //console.log(time); } // when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate diff --git a/src/shaders/sphere-frag.glsl b/src/shaders/sphere-frag.glsl index fa27aff..02be156 100644 --- a/src/shaders/sphere-frag.glsl +++ b/src/shaders/sphere-frag.glsl @@ -1,10 +1,11 @@ varying vec2 vUv; varying vec3 vNormal; varying float noise; +uniform sampler2D image; void main() { vec2 uv = vec2(1,1) - vUv; - vec4 color = vec4( vNormal, 1.0 ); + vec4 color = vec4(vNormal, 1.0 ); gl_FragColor = vec4( color.rgb, 1.0 ); } \ No newline at end of file diff --git a/src/shaders/sphere-vert.glsl b/src/shaders/sphere-vert.glsl index a8d845c..f342036 100644 --- a/src/shaders/sphere-vert.glsl +++ b/src/shaders/sphere-vert.glsl @@ -5,8 +5,23 @@ varying vec3 vNormal; uniform float uTime; -float rand(vec2 co){ - return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); +float noise1(vec3 seed){ + return fract(sin(dot(seed ,vec3(12.9898,78.233, 157.179))) * 43758.5453); +} + +float noise2(vec3 seed){ + return fract(sin(dot(seed ,vec3(12.9898,78.233, 157.179))) * 131070.5453); +} + +float noise3(vec3 seed){ + return fract(sin(dot(seed ,vec3(12.9898,78.233, 157.179))) * 524286.5453); +} + +float TotalNoise(vec3 seed, float frequency, float amplitude){ //inside frequency outside amplit + float n1 = noise1(seed * frequency) * amplitude; + float n2 = noise2(seed * frequency*2.0) * amplitude/2.0; + float n3 = noise3(seed * frequency*3.0) * amplitude/3.0; + return n1; } float lerp(float a, float b, float t){ @@ -18,28 +33,68 @@ float cosine_interpolate(float a, float b, float t){ return lerp(a, b, cos_t); } -float PerlinNoise3D(float x, float y, float z){ + +float trilinearInterpolation(float frequency, float amplitude){ + + vec3 pd = position; + + //8 adjacent vec3 positions on lattice + vec3 v000 = vec3(floor(pd.x),floor(pd.y),floor(pd.z)); + vec3 v100 = vec3(ceil(pd.x),floor(pd.y),floor(pd.z)); + vec3 v010 = vec3(floor(pd.x), ceil(pd.y), floor(pd.z)); + vec3 v001 = vec3(floor(pd.x), floor(pd.y), ceil(pd.z)); + vec3 v101 = vec3(ceil(pd.x), floor(pd.y), ceil(pd.z)); + vec3 v110 = vec3(ceil(pd.x), ceil(pd.y), floor(pd.z)); + vec3 v011 = vec3(floor(pd.x), ceil(pd.y), ceil(pd.z)); + vec3 v111 = vec3(ceil(pd.x), ceil(pd.y), ceil(pd.z)); + + //noise of cooresponding positions on lattice + float n000 = TotalNoise(v000, frequency, amplitude); + float n100 = TotalNoise(v100, frequency, amplitude); + float n010 = TotalNoise(v010, frequency, amplitude); + float n001 = TotalNoise(v001, frequency, amplitude); + float n101 = TotalNoise(v101, frequency, amplitude); + float n110 = TotalNoise(v110, frequency, amplitude); + float n011 = TotalNoise(v011, frequency, amplitude); + float n111 = TotalNoise(v111, frequency, amplitude); + + //time val for interpolation + float tX = pd.x - floor(pd.x); + float tY = pd.y - floor(pd.y); + float tZ = pd.z - floor(pd.z); + + float n00 = cosine_interpolate(n000, n100, tX); + float n10 = cosine_interpolate(n001, n101, tX); + float n11 = cosine_interpolate(n011, n111, tX); + float n01 = cosine_interpolate(n010, n110, tX); + float n0 = cosine_interpolate(n00, n10, tZ); + float n1 = cosine_interpolate(n01, n11, tZ); + float n = cosine_interpolate(n0, n1, tY); + + return n; +} + + +float PerlinNoise3D(){ float total = 0.0; float persistance = 1.0 / 2.0; - for (int i = 0; i < N_OCTAVES; ++i){ + for (int i = 0 ; i < N_OCTAVES; i++){ float frequency = pow(2.0, float(i)); float amplitude = pow(persistance, float(i)); + total += trilinearInterpolation(frequency, amplitude); - //TODO: - //sum the total from a noise function } - return 0.0; + return total; } void main() { vUv = uv; vNormal = normal; - float noiseOffset = rand(uv); - gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0 ); + gl_Position = projectionMatrix * modelViewMatrix * vec4(position + normal*PerlinNoise3D(), 1.0 ); } From 5de6ca3452054bbce184010851c42f52ba13ef86 Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Mon, 23 Jan 2017 23:25:42 -0500 Subject: [PATCH 3/7] animating --- src/main.js | 15 ++++++++++----- src/shaders/sphere-frag.glsl | 5 +++-- src/shaders/sphere-vert.glsl | 13 ++++++++++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main.js b/src/main.js index 160aa74..4b46232 100644 --- a/src/main.js +++ b/src/main.js @@ -4,6 +4,9 @@ import Framework from './framework' import Noise from './noise' import {other} from './noise' +var time = 0.0; +var sphereMaterial; + // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -31,13 +34,13 @@ function onLoad(framework) { }); var adamCube = new THREE.Mesh(box, adamMaterial); - var sphereMaterial = new THREE.ShaderMaterial({ + sphereMaterial = new THREE.ShaderMaterial({ uniforms: { image: { type: "t", value: THREE.ImageUtils.loadTexture('./explosion.png') }, - uTime: {value: 1.0} + uTime: {value: time} }, vertexShader: require('./shaders/sphere-vert.glsl'), fragmentShader: require('./shaders/sphere-frag.glsl') @@ -63,9 +66,11 @@ function onLoad(framework) { // called on frame updates function onUpdate(framework) { - // console.log(`the time is ${new Date()}`); - //var time = new Date().getMilliseconds(); - //console.log(time); + time += 0.025; + if (sphereMaterial){ + sphereMaterial.uniforms.uTime.value = time; + } + } // when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate diff --git a/src/shaders/sphere-frag.glsl b/src/shaders/sphere-frag.glsl index 02be156..9345724 100644 --- a/src/shaders/sphere-frag.glsl +++ b/src/shaders/sphere-frag.glsl @@ -1,11 +1,12 @@ varying vec2 vUv; varying vec3 vNormal; -varying float noise; +varying float vNoise; uniform sampler2D image; void main() { vec2 uv = vec2(1,1) - vUv; - vec4 color = vec4(vNormal, 1.0 ); + vec3 noise_col = vec3(vNoise,vNoise,vNoise); + vec4 color = vec4(noise_col, 1.0 ); gl_FragColor = vec4( color.rgb, 1.0 ); } \ No newline at end of file diff --git a/src/shaders/sphere-vert.glsl b/src/shaders/sphere-vert.glsl index f342036..11ff389 100644 --- a/src/shaders/sphere-vert.glsl +++ b/src/shaders/sphere-vert.glsl @@ -2,6 +2,7 @@ #define N_OCTAVES 5 varying vec2 vUv; varying vec3 vNormal; +varying float vNoise; uniform float uTime; @@ -19,8 +20,8 @@ float noise3(vec3 seed){ float TotalNoise(vec3 seed, float frequency, float amplitude){ //inside frequency outside amplit float n1 = noise1(seed * frequency) * amplitude; - float n2 = noise2(seed * frequency*2.0) * amplitude/2.0; - float n3 = noise3(seed * frequency*3.0) * amplitude/3.0; + //float n2 = noise2(seed * frequency*2.0) * amplitude/2.0; + //float n3 = noise3(seed * frequency*3.0) * amplitude/3.0; return n1; } @@ -87,14 +88,20 @@ float PerlinNoise3D(){ } + vNoise = total/float(N_OCTAVES); + return total; } +float animatedNoise(){ + return (1.0 + sin(uTime)*PerlinNoise3D()); +} + void main() { vUv = uv; vNormal = normal; - gl_Position = projectionMatrix * modelViewMatrix * vec4(position + normal*PerlinNoise3D(), 1.0 ); + gl_Position = projectionMatrix * modelViewMatrix * vec4(position + normal*animatedNoise(), 1.0 ); } From 2a86828f278ca180f2c1b865e17bd78a54f67496 Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Tue, 24 Jan 2017 15:59:33 -0500 Subject: [PATCH 4/7] animation working --- src/shaders/sphere-frag.glsl | 14 +++++++++++--- src/shaders/sphere-vert.glsl | 6 +++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/shaders/sphere-frag.glsl b/src/shaders/sphere-frag.glsl index 9345724..30be91b 100644 --- a/src/shaders/sphere-frag.glsl +++ b/src/shaders/sphere-frag.glsl @@ -3,10 +3,18 @@ varying vec3 vNormal; varying float vNoise; uniform sampler2D image; +vec3 lerp(vec3 a, vec3 b, float t){ + return (a * (1.0 - t) + b * t); +} + void main() { vec2 uv = vec2(1,1) - vUv; - vec3 noise_col = vec3(vNoise,vNoise,vNoise); - vec4 color = vec4(noise_col, 1.0 ); + vec3 noise_col = vec3(vNoise,vNoise,vNoise); + + vec3 start_col = vec3(255.0,255.0,0.0); + vec3 end_col = vec3(255.0,0.0,0.0); - gl_FragColor = vec4( color.rgb, 1.0 ); + noise_col = lerp(start_col, end_col, vNoise*3.0); + + gl_FragColor = vec4( noise_col.rgb, 1.0 ); } \ No newline at end of file diff --git a/src/shaders/sphere-vert.glsl b/src/shaders/sphere-vert.glsl index 11ff389..ffb35cb 100644 --- a/src/shaders/sphere-vert.glsl +++ b/src/shaders/sphere-vert.glsl @@ -80,7 +80,7 @@ float PerlinNoise3D(){ float total = 0.0; float persistance = 1.0 / 2.0; - for (int i = 0 ; i < N_OCTAVES; i++){ + for (int i = 0 ; i < 1; i++){ float frequency = pow(2.0, float(i)); float amplitude = pow(persistance, float(i)); @@ -88,14 +88,14 @@ float PerlinNoise3D(){ } - vNoise = total/float(N_OCTAVES); + vNoise = total; return total; } float animatedNoise(){ - return (1.0 + sin(uTime)*PerlinNoise3D()); + return (sin(uTime) + 1.0)/2.0*(PerlinNoise3D()); } void main() { From 88f71aa8b86e819680dd931b5b02941c7af89b18 Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Tue, 24 Jan 2017 16:33:39 -0500 Subject: [PATCH 5/7] better animation --- src/shaders/sphere-frag.glsl | 3 ++- src/shaders/sphere-vert.glsl | 13 +++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/shaders/sphere-frag.glsl b/src/shaders/sphere-frag.glsl index 30be91b..b055f26 100644 --- a/src/shaders/sphere-frag.glsl +++ b/src/shaders/sphere-frag.glsl @@ -14,7 +14,8 @@ void main() { vec3 start_col = vec3(255.0,255.0,0.0); vec3 end_col = vec3(255.0,0.0,0.0); - noise_col = lerp(start_col, end_col, vNoise*3.0); + //noise_col = lerp(start_col, end_col, vNoise); + gl_FragColor = vec4( noise_col.rgb, 1.0 ); } \ No newline at end of file diff --git a/src/shaders/sphere-vert.glsl b/src/shaders/sphere-vert.glsl index ffb35cb..51d5bf8 100644 --- a/src/shaders/sphere-vert.glsl +++ b/src/shaders/sphere-vert.glsl @@ -35,9 +35,9 @@ float cosine_interpolate(float a, float b, float t){ } -float trilinearInterpolation(float frequency, float amplitude){ +float trilinearInterpolation(vec3 pos, float frequency, float amplitude){ - vec3 pd = position; + vec3 pd = pos * frequency; //8 adjacent vec3 positions on lattice vec3 v000 = vec3(floor(pd.x),floor(pd.y),floor(pd.z)); @@ -76,15 +76,15 @@ float trilinearInterpolation(float frequency, float amplitude){ } -float PerlinNoise3D(){ +float PerlinNoise3D(vec3 pos){ float total = 0.0; float persistance = 1.0 / 2.0; - for (int i = 0 ; i < 1; i++){ + for (int i = 0 ; i < N_OCTAVES; i++){ float frequency = pow(2.0, float(i)); float amplitude = pow(persistance, float(i)); - total += trilinearInterpolation(frequency, amplitude); + total += trilinearInterpolation(pos, frequency, amplitude); } @@ -95,7 +95,8 @@ float PerlinNoise3D(){ } float animatedNoise(){ - return (sin(uTime) + 1.0)/2.0*(PerlinNoise3D()); + //return (sin(uTime) + 1.0)/2.0*(PerlinNoise3D()); + return PerlinNoise3D(position + uTime / 5.0); } void main() { From 0cd94f9bca9889160176946a27dfe0c5b5a3e3fd Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Tue, 24 Jan 2017 16:41:52 -0500 Subject: [PATCH 6/7] blue cloud --- src/shaders/sphere-frag.glsl | 4 ++-- src/shaders/sphere-vert.glsl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/shaders/sphere-frag.glsl b/src/shaders/sphere-frag.glsl index b055f26..6743eda 100644 --- a/src/shaders/sphere-frag.glsl +++ b/src/shaders/sphere-frag.glsl @@ -10,12 +10,12 @@ vec3 lerp(vec3 a, vec3 b, float t){ void main() { vec2 uv = vec2(1,1) - vUv; vec3 noise_col = vec3(vNoise,vNoise,vNoise); - + vec3 blue = vec3(0.0,0.0,255.0); vec3 start_col = vec3(255.0,255.0,0.0); vec3 end_col = vec3(255.0,0.0,0.0); //noise_col = lerp(start_col, end_col, vNoise); - gl_FragColor = vec4( noise_col.rgb, 1.0 ); + gl_FragColor = vec4( (noise_col.rgb/2.0) + blue*0.001, 1.0 ); } \ No newline at end of file diff --git a/src/shaders/sphere-vert.glsl b/src/shaders/sphere-vert.glsl index 51d5bf8..1614036 100644 --- a/src/shaders/sphere-vert.glsl +++ b/src/shaders/sphere-vert.glsl @@ -96,7 +96,7 @@ float PerlinNoise3D(vec3 pos){ float animatedNoise(){ //return (sin(uTime) + 1.0)/2.0*(PerlinNoise3D()); - return PerlinNoise3D(position + uTime / 5.0); + return PerlinNoise3D(position + uTime / 2.0); } void main() { From d634d1ac280fae6902f1bc88554eb3e4b56b528e Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Tue, 24 Jan 2017 21:21:17 -0500 Subject: [PATCH 7/7] added README --- README.md | 119 +---------------------------------- src/main.js | 38 +++++------ src/shaders/sphere-frag.glsl | 6 +- src/shaders/sphere-vert.glsl | 7 ++- 4 files changed, 29 insertions(+), 141 deletions(-) diff --git a/README.md b/README.md index d4ef264..d6b8495 100644 --- a/README.md +++ b/README.md @@ -1,118 +1,5 @@ -# [Project 1: Noise](https://github.com/CIS700-Procedural-Graphics/Project1-Noise) +README -## Objective +This is my submission for CIS700 Project 1: Noise -Get comfortable with using three.js and its shader support and generate an interesting 3D, continuous surface using a multi-octave noise algorithm. - -## Getting Started - -1. [Install Node.js](https://nodejs.org/en/download/). Node.js is a JavaScript runtime. It basically allows you to run JavaScript when not in a browser. For our purposes, this is not necessary. The important part is that with it comes `npm`, the Node Package Manager. This allows us to easily declare and install external dependencies such as [three.js](https://threejs.org/), [dat.GUI](https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage), and [glMatrix](http://glmatrix.net/). Some other packages we'll be using make it significantly easier to develop your code and create modules for better code reuse and clarity. These tools make it _signficantly_ easier to write code in multiple `.js` files without globally defining everything. - -2. Fork and clone [this repository](https://github.com/CIS700-Procedural-Graphics/Project1-Noise). - -3. In the root directory of your project, run `npm install`. This will download all of those dependencies. - -4. Do either of the following (but I highly recommend the first one for reasons I will explain later). - - a. Run `npm start` and then go to `localhost:7000` in your web browser - - b. Run `npm run build` and then go open `index.html` in your web browser - - You should hopefully see the framework code with a 3D cube at the center of the screen! - - -## Developing Your Code -All of the JavaScript code is living inside the `src` directory. The main file that gets executed when you load the page as you may have guessed is `main.js`. Here, you can make any changes you want, import functions from other files, etc. The reason that I highly suggest you build your project with `npm start` is that doing so will start a process that watches for any changes you make to your code. If it detects anything, it'll automagically rebuild your project and then refresh your browser window for you. Wow. That's cool. If you do it the other way, you'll need to run `npm build` and then refresh your page every time you want to test something. - -## Publishing Your Code -We highly suggest that you put your code on GitHub. One of the reasons we chose to make this course using JavaScript is that the Web is highly accessible and making your awesome work public and visible can be a huge benefit when you're looking to score a job or internship. To aid you in this process, running `npm run deploy` will automatically build your project and push it to `gh-pages` where it will be visible at `username.github.io/repo-name`. - -## What is Actually Happening? -You can skip this part if you really want, but I highly suggest you read it. - -### npm install -`npm install` will install all dependencies into a folder called `node_modules`. That's about it. - -### package.json - -This is the important file that `npm` looks at. In it, you can see the commands it's using for the `start`, `build`, and `deploy` scripts mentioned above. You can also see all of the dependencies the project requires. I will briefly go through what each of these is. - - dat-gui: Gives us a nice and simple GUI for modifying variables in our program - - - gl-matrix: Useful library for linear algebra, much like glm - - - stats-js: Gives us a nice graph for timing things. We use it to report how long it takes to render each frame - - - three: Three.js is the main library we're using to draw stuff - - - three-orbit-controls: Handles mouse / touchscreen camera controls - - - babel-core, babel-loader, babel-preset-es2015: JavaScript is a a really fast moving language. It is constantly, constantly changing. Unfortunately, web browsers don't keep up nearly as quickly. Babel does the job of converting your code to a form that current browsers support. This allows us to use newer JavaScript features such as classes and imports without worrying about compatibility. - - - gh-pages-deploy: This is the library that automates publishing your code to Github - - - webpack: Webpack serves the role of packaging your project into a single file. Browsers don't actually support "importing" from other files, so without Webpack, to access data and functions in other files we would need to globally define EVERYTHING. This is an extremely bad idea. Webpack lets us use imports and develop code in separate files. Running `npm build` or `npm start` is what bundles all of your code together. - -- webpack-dev-server: This is an extremely useful tool for development. It essentially creates a file watcher and rebuilds your project whenever you make changes. It also injects code into your page that gets notified when these changes occur so it can automatically refresh your page. - - - webpack-glsl-loader: Webpack does much more than just JavaScript. We can use it to load glsl, css, images, etc. For whatever you want to import, somebody has probably made a webpack loader for it. - -### webpack.config.js - -This is the configuration file in webpack. The most important part is `entry` and `output`. These define the input and output for webpack. It will start from `entry`, explore all dependencies, and package them all into `output`. Here, the `output` is `bundle.js`. If you look in `index.html`, you can see that the page is loading `bundle.js`, not `main.js`. - -The other sections are just configuration settings for `webpack-dev-server` and setup for loading different types of files. - -## Setting up a shader - -Using the provided framework code, create a new three.js material which references a vertex and fragment shader. Look at the adamMaterial for reference. It should reference at least one uniform variable (you'll need a time variable to animate your mesh later on). - -Create [an icosahedron](https://threejs.org/docs/index.html#Reference/Geometries/IcosahedronBufferGeometry), instead of the default cube geometry provided in the scene. Test your shader setup by applying the material to the icosahedron and color the mesh in the fragment shader using the normals' XYZ components as RGB. - -Note that three.js automatically injects several uniform and attribute variables into your shaders by default; they are listed in the [documentation](https://threejs.org/docs/api/renderers/webgl/WebGLProgram.html) for three.js's WebGLProgram class. - -## Noise Generation - -In the shader, write a 3D multi-octave lattice-value noise function that takes three input parameters and generates output in a controlled range, say [0,1] or [-1, 1]. This will require the following steps. - -1. Write several (for however many octaves of noise you want) basic pseudo-random 3D noise functions (the hash-like functions we discussed in class). It's fine to reference one from the slides or elsewhere on the Internet. Again, this should just be a set of math operations, often using large prime numbers to random-looking output from three input parameters. - -2. Write an interpolation function. Lerp is fine, but for better results, we suggest cosine interpolation. - -3. (Optional) Write a smoothing function that will average the results of the noise value at some (x, y, z) with neighboring values, that is (x+-1, y+-1, z+-1). - -4. Write an 'interpolate noise' function that takes some (x, y, z) point as input and produces a noise value for that point by interpolating the surrounding lattice values (for 3D, this means the surrounding eight 'corner' points). Use your interpolation function and pseudo-random noise generator to accomplish this. - -5. Write a multi-octave noise generation function that sums multiple noise functions together, with each subsequent noise function increasing in frequency and decreasing in amplitude. You should use the interpolate noise function you wrote previously to accomplish this, as it generates a single octave of noise. The slides contain pseudocode for writing your multi-octave noise function. - - -## Noise Application - -View your noise in action by applying it as a displacement on the surface of your icosahedron, giving your icosahedron a bumpy, cloud-like appearance. Simply take the noise value as a height, and offset the vertices along the icosahedron's surface normals. You are, of course, free to alter the way your noise perturbs your icosahedron's surface as you see fit; we are simply recommending an easy way to visualize your noise. You could even apply a couple of different noise functions to perturb your surface to make it even less spherical. - -In order to animate the vertex displacement, use time as the third dimension or as some offset to the (x, y, z) input to the noise function. Pass the current time since start of program as a uniform to the shaders. - -For both visual impact and debugging help, also apply color to your geometry using the noise value at each point. There are several ways to do this. For example, you might use the noise value to create UV coordinates to read from a texture (say, a simple gradient image), or just compute the color by hand by lerping between values. - -## Interactivity - -Using dat.GUI and the examples provided in the reference code, make some aspect of your demo an interactive variable. For example, you could add a slider to adjust the strength or scale of the noise, change the number of noise octaves, etc. - -## For the overachievers (extra credit) - -- More interactivity (easy): pretty self-explanatory. Make more aspects of your demo interactive by adding more controlable variables in the GUI. - -- Custom mesh (easy): Figure out how to import a custom mesh rather than using an icosahedron for a fancy-shaped cloud. - -- Mouse interactivity (medium): Find out how to get the current mouse position in your scene and use it to deform your cloud, such that users can deform the cloud with their cursor. - -- Music (hard): Figure out a way to use music to drive your noise animation in some way, such that your noise cloud appears to dance. - -## Submission - -- Update README.md to contain a solid description of your project - -- Publish your project to gh-pages. `npm run deploy`. It should now be visible at http://username.github.io/repo-name - -- Create a [pull request](https://help.github.com/articles/creating-a-pull-request/) to this repository, and in the comment, include a link to your published project. - -- Submit the link to your pull request on Canvas. \ No newline at end of file +I created a stormy looking cloud whose frequency and speed of animation can be manipulated through the GUI. \ No newline at end of file diff --git a/src/main.js b/src/main.js index 4b46232..d81a7ea 100644 --- a/src/main.js +++ b/src/main.js @@ -5,6 +5,10 @@ import Noise from './noise' import {other} from './noise' var time = 0.0; +var params = { + speed: 1.0, + frequency: 1.0 +}; var sphereMaterial; // called after the scene loads @@ -17,30 +21,15 @@ function onLoad(framework) { // 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); - - - 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); - sphereMaterial = new THREE.ShaderMaterial({ uniforms: { image: { type: "t", value: THREE.ImageUtils.loadTexture('./explosion.png') }, - uTime: {value: time} + uTime: {value: time}, + uSpeed: {value: params.speed}, + uFreq: {value: params.frequency} }, vertexShader: require('./shaders/sphere-vert.glsl'), fragmentShader: require('./shaders/sphere-frag.glsl') @@ -50,7 +39,7 @@ function onLoad(framework) { var mySphere =new THREE.Mesh(sphere, sphereMaterial); // set camera position - camera.position.set(1, 1, 2); + camera.position.set(1, 5, 2); camera.lookAt(new THREE.Vector3(0,0,0)); //scene.add(adamCube); @@ -61,6 +50,14 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + + gui.add(params, 'speed', 0, 3 ).onChange(function(newVal) { + }); + + + gui.add(params, 'frequency', 0.1, 5 ).onChange(function(newVal) { + }); + } @@ -69,6 +66,9 @@ function onUpdate(framework) { time += 0.025; if (sphereMaterial){ sphereMaterial.uniforms.uTime.value = time; + sphereMaterial.uniforms.uSpeed.value = params.speed; + sphereMaterial.uniforms.uFreq.value = params.frequency; + } } diff --git a/src/shaders/sphere-frag.glsl b/src/shaders/sphere-frag.glsl index 6743eda..b606e88 100644 --- a/src/shaders/sphere-frag.glsl +++ b/src/shaders/sphere-frag.glsl @@ -10,12 +10,12 @@ vec3 lerp(vec3 a, vec3 b, float t){ void main() { vec2 uv = vec2(1,1) - vUv; vec3 noise_col = vec3(vNoise,vNoise,vNoise); - vec3 blue = vec3(0.0,0.0,255.0); + vec3 blue = vec3(35.0,206.0,235.0); vec3 start_col = vec3(255.0,255.0,0.0); vec3 end_col = vec3(255.0,0.0,0.0); //noise_col = lerp(start_col, end_col, vNoise); - - gl_FragColor = vec4( (noise_col.rgb/2.0) + blue*0.001, 1.0 ); + + gl_FragColor = vec4( (noise_col.rgb/2.0) + blue*0.0005, 1.0 ); } \ No newline at end of file diff --git a/src/shaders/sphere-vert.glsl b/src/shaders/sphere-vert.glsl index 1614036..7b86a04 100644 --- a/src/shaders/sphere-vert.glsl +++ b/src/shaders/sphere-vert.glsl @@ -4,6 +4,8 @@ varying vec2 vUv; varying vec3 vNormal; varying float vNoise; uniform float uTime; +uniform float uSpeed; +uniform float uFreq; float noise1(vec3 seed){ @@ -82,7 +84,7 @@ float PerlinNoise3D(vec3 pos){ for (int i = 0 ; i < N_OCTAVES; i++){ - float frequency = pow(2.0, float(i)); + float frequency = pow(2.0, float(i)) * uFreq; float amplitude = pow(persistance, float(i)); total += trilinearInterpolation(pos, frequency, amplitude); @@ -95,8 +97,7 @@ float PerlinNoise3D(vec3 pos){ } float animatedNoise(){ - //return (sin(uTime) + 1.0)/2.0*(PerlinNoise3D()); - return PerlinNoise3D(position + uTime / 2.0); + return PerlinNoise3D(position + (uTime / 2.0)*uSpeed); } void main() {