├── Fire ├── .gitignore ├── Assets │ ├── Common.meta │ ├── Common │ │ ├── Shaders.meta │ │ └── Shaders │ │ │ ├── ClassicNoise3D.cginc │ │ │ ├── ClassicNoise3D.cginc.meta │ │ │ ├── SimplexNoise3D.cginc │ │ │ └── SimplexNoise3D.cginc.meta │ ├── Fire.meta │ └── Fire │ │ ├── Fire.unity │ │ ├── Fire.unity.meta │ │ ├── Materials.meta │ │ ├── Materials │ │ ├── Lighting.mat │ │ ├── Lighting.mat.meta │ │ ├── Lighting2.mat │ │ ├── Lighting2.mat.meta │ │ ├── Normal.mat │ │ └── Normal.mat.meta │ │ ├── Scripts.meta │ │ ├── Scripts │ │ ├── Controller.cs │ │ ├── Controller.cs.meta │ │ ├── Rotater.cs │ │ └── Rotater.cs.meta │ │ ├── Shaders.meta │ │ ├── Shaders │ │ ├── Fire.cginc │ │ ├── Fire.cginc.meta │ │ ├── Lighting.shader │ │ ├── Lighting.shader.meta │ │ ├── Normal.shader │ │ └── Normal.shader.meta │ │ ├── Textures.meta │ │ └── Textures │ │ ├── Maskonaive2.jpg │ │ ├── Maskonaive2.jpg.meta │ │ ├── SaintPetersBasilica.jpg │ │ ├── SaintPetersBasilica.jpg.meta │ │ ├── fire.png │ │ ├── fire.png.meta │ │ ├── skybox.jpg │ │ ├── skybox.jpg.meta │ │ ├── skybox2.jpg │ │ ├── skybox2.jpg.meta │ │ ├── skybox3.jpg │ │ └── skybox3.jpg.meta ├── Captures │ └── Capture.gif └── README.md ├── Fish ├── .gitignore ├── assets │ ├── particle.frag │ ├── particle.geom │ ├── particle.vert │ └── particleUpdate.vs ├── include │ └── Resources.h ├── resources │ └── CinderApp.icns ├── src │ └── FishApp.cpp └── xcode │ ├── Fish.xcodeproj │ └── project.pbxproj │ ├── Fish_Prefix.pch │ └── Info.plist ├── GeneticAlgorithm ├── .gitignore ├── Assets │ ├── Common.meta │ ├── Common │ │ ├── Scripts.meta │ │ ├── Scripts │ │ │ ├── FboPingpong.cs │ │ │ └── FboPingpong.cs.meta │ │ ├── Shaders.meta │ │ └── Shaders │ │ │ ├── ClassicNoise3D.cginc │ │ │ ├── ClassicNoise3D.cginc.meta │ │ │ ├── Random.cginc │ │ │ ├── Random.cginc.meta │ │ │ ├── SimplexNoise3D.cginc │ │ │ └── SimplexNoise3D.cginc.meta │ ├── GeneticAlgorithm.meta │ └── GeneticAlgorithm │ │ ├── GeneticAlgorithm.unity │ │ ├── GeneticAlgorithm.unity.meta │ │ ├── Materials.meta │ │ ├── Materials │ │ ├── Quad.mat │ │ ├── Quad.mat.meta │ │ ├── Visualizer.mat │ │ └── Visualizer.mat.meta │ │ ├── Scripts.meta │ │ ├── Scripts │ │ ├── Creature.cs │ │ ├── Creature.cs.meta │ │ ├── DNA.cs │ │ ├── DNA.cs.meta │ │ ├── Gaussian.cs │ │ ├── Gaussian.cs.meta │ │ ├── GeneticAlgorithm.cs │ │ ├── GeneticAlgorithm.cs.meta │ │ ├── Nematode.cs │ │ ├── Nematode.cs.meta │ │ ├── Spline.cs │ │ ├── Spline.cs.meta │ │ ├── Visualizer.cs │ │ └── Visualizer.cs.meta │ │ ├── Shaders.meta │ │ ├── Shaders │ │ ├── Lines.shader │ │ ├── Lines.shader.meta │ │ ├── Quad.shader │ │ ├── Quad.shader.meta │ │ ├── Visualizer.shader │ │ └── Visualizer.shader.meta │ │ ├── Textures.meta │ │ └── Textures │ │ ├── arrow.png │ │ ├── arrow.png.meta │ │ ├── circle.png │ │ └── circle.png.meta ├── Captures │ └── Capture.gif └── README.md ├── LorenzSystem ├── .gitignore ├── Captures │ ├── Capture.gif │ └── Capture2.gif ├── README.md ├── assets │ ├── particle.frag │ ├── particle.geom │ ├── particle.vert │ └── particleUpdate.vs ├── include │ └── Resources.h ├── resources │ └── CinderApp.icns ├── src │ └── LorenzSystemApp.cpp └── xcode │ ├── Info.plist │ ├── LorenzSystem.xcodeproj │ └── project.pbxproj │ └── LorenzSystem_Prefix.pch ├── Noise ├── .gitignore ├── Assets │ ├── Common.meta │ ├── Common │ │ ├── Scripts.meta │ │ ├── Scripts │ │ │ ├── FboPingPong.cs │ │ │ └── FboPingPong.cs.meta │ │ ├── Shaders.meta │ │ └── Shaders │ │ │ ├── ClassicNoise2D.cginc │ │ │ ├── ClassicNoise2D.cginc.meta │ │ │ ├── ClassicNoise3D.cginc │ │ │ ├── ClassicNoise3D.cginc.meta │ │ │ ├── NoiseTest.shader │ │ │ ├── NoiseTest.shader.meta │ │ │ ├── SimplexNoise2D.cginc │ │ │ ├── SimplexNoise2D.cginc.meta │ │ │ ├── SimplexNoise3D.cginc │ │ │ ├── SimplexNoise3D.cginc.meta │ │ │ ├── SimplexNoiseGrad2D.cginc │ │ │ ├── SimplexNoiseGrad2D.cginc.meta │ │ │ ├── SimplexNoiseGrad3D.cginc │ │ │ └── SimplexNoiseGrad3D.cginc.meta │ ├── Noise.meta │ └── Noise │ │ ├── Noise.unity │ │ ├── Noise.unity.meta │ │ ├── Scripts.meta │ │ ├── Scripts │ │ ├── Noise.cs │ │ └── Noise.cs.meta │ │ ├── Shaders.meta │ │ ├── Shaders │ │ ├── Sketch.shader │ │ └── Sketch.shader.meta │ │ ├── Textures.meta │ │ └── Textures │ │ ├── IMG_5469.JPG │ │ ├── IMG_5469.JPG.meta │ │ ├── ellipse.jpg │ │ ├── ellipse.jpg.meta │ │ ├── skybox.jpg │ │ ├── skybox.jpg.meta │ │ ├── square.png │ │ ├── square.png.meta │ │ ├── voronoi.png │ │ └── voronoi.png.meta ├── Captures │ └── Capture.gif └── README.md ├── README.md └── THREE_D ├── README.md ├── THREE_D.pde ├── Ticker.pde └── data ├── capture.gif ├── sample.frag ├── sample.vert └── sphere.obj /Fire/.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | [Bb]uild/ 5 | [Pp]rojectSettings/ 6 | Papers/ 7 | 8 | # Autogenerated VS/MD solution and project files 9 | *.csproj 10 | *.unityproj 11 | *.sln 12 | *.suo 13 | *.user 14 | *.userprefs 15 | *.pidb 16 | *.booproj 17 | 18 | # Unity3D Generated File On Crash Reports 19 | sysinfo.txt -------------------------------------------------------------------------------- /Fire/Assets/Common.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 43c8801d83fad486187a0a21cdfda09a 3 | folderAsset: yes 4 | timeCreated: 1492531382 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Fire/Assets/Common/Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a3b0cf0a994a3488e9ec79c802b40840 3 | folderAsset: yes 4 | timeCreated: 1492531386 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Fire/Assets/Common/Shaders/ClassicNoise3D.cginc: -------------------------------------------------------------------------------- 1 | // 2 | // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader 3 | // 4 | // Original work (webgl-noise) Copyright (C) 2011 Stefan Gustavson 5 | // Translation and modification was made by Keijiro Takahashi. 6 | // 7 | // This shader is based on the webgl-noise GLSL shader. For further details 8 | // of the original shader, please see the following description from the 9 | // original source code. 10 | // 11 | 12 | // 13 | // GLSL textureless classic 3D noise "cnoise", 14 | // with an RSL-style periodic variant "pnoise". 15 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 16 | // Version: 2011-10-11 17 | // 18 | // Many thanks to Ian McEwan of Ashima Arts for the 19 | // ideas for permutation and gradient selection. 20 | // 21 | // Copyright (c) 2011 Stefan Gustavson. All rights reserved. 22 | // Distributed under the MIT license. See LICENSE file. 23 | // https://github.com/ashima/webgl-noise 24 | // 25 | 26 | float3 mod(float3 x, float3 y) 27 | { 28 | return x - y * floor(x / y); 29 | } 30 | 31 | float3 mod289(float3 x) 32 | { 33 | return x - floor(x / 289.0) * 289.0; 34 | } 35 | 36 | float4 mod289(float4 x) 37 | { 38 | return x - floor(x / 289.0) * 289.0; 39 | } 40 | 41 | float4 permute(float4 x) 42 | { 43 | return mod289(((x*34.0)+1.0)*x); 44 | } 45 | 46 | float4 taylorInvSqrt(float4 r) 47 | { 48 | return (float4)1.79284291400159 - r * 0.85373472095314; 49 | } 50 | 51 | float3 fade(float3 t) { 52 | return t*t*t*(t*(t*6.0-15.0)+10.0); 53 | } 54 | 55 | // Classic Perlin noise 56 | float cnoise(float3 P) 57 | { 58 | float3 Pi0 = floor(P); // Integer part for indexing 59 | float3 Pi1 = Pi0 + (float3)1.0; // Integer part + 1 60 | Pi0 = mod289(Pi0); 61 | Pi1 = mod289(Pi1); 62 | float3 Pf0 = frac(P); // Fractional part for interpolation 63 | float3 Pf1 = Pf0 - (float3)1.0; // Fractional part - 1.0 64 | float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 65 | float4 iy = float4(Pi0.y, Pi0.y, Pi1.y, Pi1.y); 66 | float4 iz0 = (float4)Pi0.z; 67 | float4 iz1 = (float4)Pi1.z; 68 | 69 | float4 ixy = permute(permute(ix) + iy); 70 | float4 ixy0 = permute(ixy + iz0); 71 | float4 ixy1 = permute(ixy + iz1); 72 | 73 | float4 gx0 = ixy0 / 7.0; 74 | float4 gy0 = frac(floor(gx0) / 7.0) - 0.5; 75 | gx0 = frac(gx0); 76 | float4 gz0 = (float4)0.5 - abs(gx0) - abs(gy0); 77 | float4 sz0 = step(gz0, (float4)0.0); 78 | gx0 -= sz0 * (step((float4)0.0, gx0) - 0.5); 79 | gy0 -= sz0 * (step((float4)0.0, gy0) - 0.5); 80 | 81 | float4 gx1 = ixy1 / 7.0; 82 | float4 gy1 = frac(floor(gx1) / 7.0) - 0.5; 83 | gx1 = frac(gx1); 84 | float4 gz1 = (float4)0.5 - abs(gx1) - abs(gy1); 85 | float4 sz1 = step(gz1, (float4)0.0); 86 | gx1 -= sz1 * (step((float4)0.0, gx1) - 0.5); 87 | gy1 -= sz1 * (step((float4)0.0, gy1) - 0.5); 88 | 89 | float3 g000 = float3(gx0.x,gy0.x,gz0.x); 90 | float3 g100 = float3(gx0.y,gy0.y,gz0.y); 91 | float3 g010 = float3(gx0.z,gy0.z,gz0.z); 92 | float3 g110 = float3(gx0.w,gy0.w,gz0.w); 93 | float3 g001 = float3(gx1.x,gy1.x,gz1.x); 94 | float3 g101 = float3(gx1.y,gy1.y,gz1.y); 95 | float3 g011 = float3(gx1.z,gy1.z,gz1.z); 96 | float3 g111 = float3(gx1.w,gy1.w,gz1.w); 97 | 98 | float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); 99 | g000 *= norm0.x; 100 | g010 *= norm0.y; 101 | g100 *= norm0.z; 102 | g110 *= norm0.w; 103 | 104 | float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); 105 | g001 *= norm1.x; 106 | g011 *= norm1.y; 107 | g101 *= norm1.z; 108 | g111 *= norm1.w; 109 | 110 | float n000 = dot(g000, Pf0); 111 | float n100 = dot(g100, float3(Pf1.x, Pf0.y, Pf0.z)); 112 | float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z)); 113 | float n110 = dot(g110, float3(Pf1.x, Pf1.y, Pf0.z)); 114 | float n001 = dot(g001, float3(Pf0.x, Pf0.y, Pf1.z)); 115 | float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z)); 116 | float n011 = dot(g011, float3(Pf0.x, Pf1.y, Pf1.z)); 117 | float n111 = dot(g111, Pf1); 118 | 119 | float3 fade_xyz = fade(Pf0); 120 | float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z); 121 | float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y); 122 | float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x); 123 | return 2.2 * n_xyz; 124 | } 125 | 126 | // Classic Perlin noise, periodic variant 127 | float pnoise(float3 P, float3 rep) 128 | { 129 | float3 Pi0 = mod(floor(P), rep); // Integer part, modulo period 130 | float3 Pi1 = mod(Pi0 + (float3)1.0, rep); // Integer part + 1, mod period 131 | Pi0 = mod289(Pi0); 132 | Pi1 = mod289(Pi1); 133 | float3 Pf0 = frac(P); // Fractional part for interpolation 134 | float3 Pf1 = Pf0 - (float3)1.0; // Fractional part - 1.0 135 | float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 136 | float4 iy = float4(Pi0.y, Pi0.y, Pi1.y, Pi1.y); 137 | float4 iz0 = (float4)Pi0.z; 138 | float4 iz1 = (float4)Pi1.z; 139 | 140 | float4 ixy = permute(permute(ix) + iy); 141 | float4 ixy0 = permute(ixy + iz0); 142 | float4 ixy1 = permute(ixy + iz1); 143 | 144 | float4 gx0 = ixy0 / 7.0; 145 | float4 gy0 = frac(floor(gx0) / 7.0) - 0.5; 146 | gx0 = frac(gx0); 147 | float4 gz0 = (float4)0.5 - abs(gx0) - abs(gy0); 148 | float4 sz0 = step(gz0, (float4)0.0); 149 | gx0 -= sz0 * (step((float4)0.0, gx0) - 0.5); 150 | gy0 -= sz0 * (step((float4)0.0, gy0) - 0.5); 151 | 152 | float4 gx1 = ixy1 / 7.0; 153 | float4 gy1 = frac(floor(gx1) / 7.0) - 0.5; 154 | gx1 = frac(gx1); 155 | float4 gz1 = (float4)0.5 - abs(gx1) - abs(gy1); 156 | float4 sz1 = step(gz1, (float4)0.0); 157 | gx1 -= sz1 * (step((float4)0.0, gx1) - 0.5); 158 | gy1 -= sz1 * (step((float4)0.0, gy1) - 0.5); 159 | 160 | float3 g000 = float3(gx0.x,gy0.x,gz0.x); 161 | float3 g100 = float3(gx0.y,gy0.y,gz0.y); 162 | float3 g010 = float3(gx0.z,gy0.z,gz0.z); 163 | float3 g110 = float3(gx0.w,gy0.w,gz0.w); 164 | float3 g001 = float3(gx1.x,gy1.x,gz1.x); 165 | float3 g101 = float3(gx1.y,gy1.y,gz1.y); 166 | float3 g011 = float3(gx1.z,gy1.z,gz1.z); 167 | float3 g111 = float3(gx1.w,gy1.w,gz1.w); 168 | 169 | float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); 170 | g000 *= norm0.x; 171 | g010 *= norm0.y; 172 | g100 *= norm0.z; 173 | g110 *= norm0.w; 174 | float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); 175 | g001 *= norm1.x; 176 | g011 *= norm1.y; 177 | g101 *= norm1.z; 178 | g111 *= norm1.w; 179 | 180 | float n000 = dot(g000, Pf0); 181 | float n100 = dot(g100, float3(Pf1.x, Pf0.y, Pf0.z)); 182 | float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z)); 183 | float n110 = dot(g110, float3(Pf1.x, Pf1.y, Pf0.z)); 184 | float n001 = dot(g001, float3(Pf0.x, Pf0.y, Pf1.z)); 185 | float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z)); 186 | float n011 = dot(g011, float3(Pf0.x, Pf1.y, Pf1.z)); 187 | float n111 = dot(g111, Pf1); 188 | 189 | float3 fade_xyz = fade(Pf0); 190 | float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z); 191 | float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y); 192 | float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x); 193 | return 2.2 * n_xyz; 194 | } 195 | -------------------------------------------------------------------------------- /Fire/Assets/Common/Shaders/ClassicNoise3D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c93bae1f719584c7ebe5f197adbc5df9 3 | timeCreated: 1455502254 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Fire/Assets/Common/Shaders/SimplexNoise3D.cginc: -------------------------------------------------------------------------------- 1 | // 2 | // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader 3 | // 4 | // Original work (webgl-noise) Copyright (C) 2011 Ashima Arts. 5 | // Translation and modification was made by Keijiro Takahashi. 6 | // 7 | // This shader is based on the webgl-noise GLSL shader. For further details 8 | // of the original shader, please see the following description from the 9 | // original source code. 10 | // 11 | 12 | // 13 | // Description : Array and textureless GLSL 2D/3D/4D simplex 14 | // noise functions. 15 | // Author : Ian McEwan, Ashima Arts. 16 | // Maintainer : ijm 17 | // Lastmod : 20110822 (ijm) 18 | // License : Copyright (C) 2011 Ashima Arts. All rights reserved. 19 | // Distributed under the MIT License. See LICENSE file. 20 | // https://github.com/ashima/webgl-noise 21 | // 22 | 23 | float3 mod289(float3 x) 24 | { 25 | return x - floor(x / 289.0) * 289.0; 26 | } 27 | 28 | float4 mod289(float4 x) 29 | { 30 | return x - floor(x / 289.0) * 289.0; 31 | } 32 | 33 | float4 permute(float4 x) 34 | { 35 | return mod289((x * 34.0 + 1.0) * x); 36 | } 37 | 38 | float4 taylorInvSqrt(float4 r) 39 | { 40 | return 1.79284291400159 - r * 0.85373472095314; 41 | } 42 | 43 | float snoise(float3 v) 44 | { 45 | const float2 C = float2(1.0 / 6.0, 1.0 / 3.0); 46 | 47 | // First corner 48 | float3 i = floor(v + dot(v, C.yyy)); 49 | float3 x0 = v - i + dot(i, C.xxx); 50 | 51 | // Other corners 52 | float3 g = step(x0.yzx, x0.xyz); 53 | float3 l = 1.0 - g; 54 | float3 i1 = min(g.xyz, l.zxy); 55 | float3 i2 = max(g.xyz, l.zxy); 56 | 57 | // x1 = x0 - i1 + 1.0 * C.xxx; 58 | // x2 = x0 - i2 + 2.0 * C.xxx; 59 | // x3 = x0 - 1.0 + 3.0 * C.xxx; 60 | float3 x1 = x0 - i1 + C.xxx; 61 | float3 x2 = x0 - i2 + C.yyy; 62 | float3 x3 = x0 - 0.5; 63 | 64 | // Permutations 65 | i = mod289(i); // Avoid truncation effects in permutation 66 | float4 p = 67 | permute(permute(permute(i.z + float4(0.0, i1.z, i2.z, 1.0)) 68 | + i.y + float4(0.0, i1.y, i2.y, 1.0)) 69 | + i.x + float4(0.0, i1.x, i2.x, 1.0)); 70 | 71 | // Gradients: 7x7 points over a square, mapped onto an octahedron. 72 | // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) 73 | float4 j = p - 49.0 * floor(p / 49.0); // mod(p,7*7) 74 | 75 | float4 x_ = floor(j / 7.0); 76 | float4 y_ = floor(j - 7.0 * x_); // mod(j,N) 77 | 78 | float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0; 79 | float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0; 80 | 81 | float4 h = 1.0 - abs(x) - abs(y); 82 | 83 | float4 b0 = float4(x.xy, y.xy); 84 | float4 b1 = float4(x.zw, y.zw); 85 | 86 | //float4 s0 = float4(lessThan(b0, 0.0)) * 2.0 - 1.0; 87 | //float4 s1 = float4(lessThan(b1, 0.0)) * 2.0 - 1.0; 88 | float4 s0 = floor(b0) * 2.0 + 1.0; 89 | float4 s1 = floor(b1) * 2.0 + 1.0; 90 | float4 sh = -step(h, 0.0); 91 | 92 | float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; 93 | float4 a1 = b1.xzyw + s1.xzyw * sh.zzww; 94 | 95 | float3 g0 = float3(a0.xy, h.x); 96 | float3 g1 = float3(a0.zw, h.y); 97 | float3 g2 = float3(a1.xy, h.z); 98 | float3 g3 = float3(a1.zw, h.w); 99 | 100 | // Normalise gradients 101 | float4 norm = taylorInvSqrt(float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3))); 102 | g0 *= norm.x; 103 | g1 *= norm.y; 104 | g2 *= norm.z; 105 | g3 *= norm.w; 106 | 107 | // Mix final noise value 108 | float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0); 109 | m = m * m; 110 | m = m * m; 111 | 112 | float4 px = float4(dot(x0, g0), dot(x1, g1), dot(x2, g2), dot(x3, g3)); 113 | return 42.0 * dot(m, px); 114 | } 115 | -------------------------------------------------------------------------------- /Fire/Assets/Common/Shaders/SimplexNoise3D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e19895d8fa6244b9f9c50761cddd8ad5 3 | timeCreated: 1455502254 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Fire/Assets/Fire.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 84033ada8b6884b38900a00ebc634c41 3 | folderAsset: yes 4 | timeCreated: 1492504507 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Fire.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fire/Assets/Fire/Fire.unity -------------------------------------------------------------------------------- /Fire/Assets/Fire/Fire.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ccfb6c8d542e9e0458d106adcd865307 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1c316609d3909403594d50b68c39905d 3 | folderAsset: yes 4 | timeCreated: 1455504479 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Materials/Lighting.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fire/Assets/Fire/Materials/Lighting.mat -------------------------------------------------------------------------------- /Fire/Assets/Fire/Materials/Lighting.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d6126b65e7814b94a93e2d29d52e0e8d 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Materials/Lighting2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fire/Assets/Fire/Materials/Lighting2.mat -------------------------------------------------------------------------------- /Fire/Assets/Fire/Materials/Lighting2.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 915978b06aa0b46a6853847bb66900d9 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Materials/Normal.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fire/Assets/Fire/Materials/Normal.mat -------------------------------------------------------------------------------- /Fire/Assets/Fire/Materials/Normal.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0b3b6d5c9adc64037831839eed26121d 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 01df30e963e537a43b0171b50b9a283f 3 | folderAsset: yes 4 | timeCreated: 1492574825 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Scripts/Controller.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | using UnityEngine; 6 | 7 | public class Controller : MonoBehaviour { 8 | 9 | [SerializeField] List renderers; 10 | List blocks; 11 | 12 | [SerializeField, Range(-2f, 2f)] float gain = 0.29f; 13 | [SerializeField, Range(-3f, 3f)] float magnitude = 1.3f; 14 | 15 | class MPB 16 | { 17 | Renderer renderer; 18 | MaterialPropertyBlock block; 19 | 20 | public MPB(Renderer renderer) 21 | { 22 | this.renderer = renderer; 23 | block = new MaterialPropertyBlock(); 24 | this.renderer.GetPropertyBlock(block); 25 | } 26 | 27 | public void Update () 28 | { 29 | renderer.SetPropertyBlock(block); 30 | } 31 | 32 | public void SetFloat(string key, float v, float dt) 33 | { 34 | var v0 = block.GetFloat(key); 35 | block.SetFloat(key, Mathf.Lerp(v0, v, dt)); 36 | } 37 | 38 | } 39 | 40 | void Awake () 41 | { 42 | blocks = renderers.Select(renderer => 43 | { 44 | var block = new MPB(renderer); 45 | block.SetFloat("_Gain", gain, 1f); 46 | block.SetFloat("_Magnitude", magnitude, 1f); 47 | return block; 48 | }).ToList(); 49 | } 50 | 51 | void Update () { 52 | if (blocks == null) return; 53 | 54 | var dt = Time.deltaTime; 55 | 56 | blocks.ForEach(block => 57 | { 58 | block.SetFloat("_Gain", gain, dt); 59 | block.SetFloat("_Magnitude", magnitude, dt); 60 | block.Update(); 61 | }); 62 | 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Scripts/Controller.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 033ca512b5c582b43ba7629c6533c718 3 | timeCreated: 1492579250 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Scripts/Rotater.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Rotater : MonoBehaviour { 6 | 7 | [SerializeField] float speed = 30f; 8 | 9 | void Update () { 10 | } 11 | 12 | void FixedUpdate () { 13 | var t = Time.timeSinceLevelLoad; 14 | var nx = Mathf.PerlinNoise(t, -10f); 15 | var ny = Mathf.PerlinNoise(10f, t); 16 | var nz = Mathf.PerlinNoise(t, -t); 17 | transform.rotation *= Quaternion.AngleAxis(Time.fixedDeltaTime * speed, (new Vector3(nx, ny, nz)).normalized); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Scripts/Rotater.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9de55ec99117b404fae3eace42faf58e 3 | timeCreated: 1492574833 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c4fb1492af1d19e48b39e671657e9ae9 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Shaders/Fire.cginc: -------------------------------------------------------------------------------- 1 | 2 | #define FIRE_OCTIVES 3 3 | #define ITERATIONS 10 4 | 5 | sampler2D _FireTex; 6 | fixed4 _Scale; 7 | float _Lacunarity, _Gain, _Magnitude; 8 | float _Epsilon; 9 | 10 | float turbulence(float3 pos) { 11 | float sum = 0.0; 12 | float freq = 1.0; 13 | float amp = 1.0; 14 | 15 | for(int i = 0; i < FIRE_OCTIVES; i++) { 16 | sum += abs(FIRE_NOISE(pos * freq)) * amp; 17 | freq *= _Lacunarity; 18 | amp *= _Gain; 19 | } 20 | return sum; 21 | } 22 | 23 | float sample_fire (float3 loc, float4 scale) { 24 | float2 st = float2(sqrt(dot(loc.xz, loc.xz)), loc.y); 25 | 26 | loc.y -= _Time.y * scale.w; 27 | loc *= scale.xyz; 28 | 29 | st.y += sqrt(st.y) * _Magnitude * turbulence(loc); 30 | // st.y -= 0.1; 31 | 32 | if(st.y <= 0.0 || st.y >= 1.0) { 33 | return 0; 34 | } 35 | 36 | return tex2D(_FireTex, st).r; 37 | } 38 | 39 | 40 | float sample(float3 rayPos) { 41 | float3 rayDir = -UNITY_MATRIX_V[2].xyz; 42 | 43 | float rayLen = 0.1; 44 | 45 | float output = 0; 46 | for(int i = 0; i < ITERATIONS; i++) { 47 | rayPos += rayDir * rayLen; 48 | 49 | float3 lp = (mul(unity_WorldToObject, float4(rayPos, 1.0))).xyz; 50 | lp.y += 0.5; 51 | lp.xz *= 2.0; 52 | output += sample_fire(lp, _Scale); 53 | } 54 | 55 | return output; 56 | } 57 | 58 | float3 sample_normal(float3 world) { 59 | float3 rayDir = -normalize(UNITY_MATRIX_V[2].xyz); 60 | float3 rayUp = normalize(UNITY_MATRIX_V[1].xyz); 61 | float3 rayRight = normalize(UNITY_MATRIX_V[0].xyz); 62 | 63 | float right = sample(world.xyz + rayRight * _Epsilon); 64 | float left = sample(world.xyz - rayRight * _Epsilon); 65 | 66 | float up = sample(world.xyz + rayUp * _Epsilon); 67 | float down = sample(world.xyz - rayUp * _Epsilon); 68 | 69 | float forward = sample(world.xyz + rayDir * _Epsilon); 70 | float back = sample(world.xyz - rayDir * _Epsilon); 71 | 72 | float dx = right - left; 73 | float dy = up - down; 74 | float dz = forward - back; 75 | 76 | return normalize(float3(dx, dy, dz)); 77 | } 78 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Shaders/Fire.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5a47b7e74836e4c8fa954e115969ec2a 3 | timeCreated: 1455502254 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Shaders/Lighting.shader: -------------------------------------------------------------------------------- 1 | Shader "Custom/Lighting" { 2 | 3 | Properties { 4 | _Color ("Color", Color) = (1, 1, 1, 1) 5 | _FireTex ("Fire Texture", 2D) = "white" {} 6 | 7 | _CubeMap ("Cubemap", Cube) = "" {} 8 | _Brightness ("Brightness", Range(0.0, 2.0)) = 1.25 9 | 10 | _Scale ("Fire Scale", Vector) = (1, 3, 1, 0.5) 11 | _Lacunarity ("_Lacunarity", float) = 2.0 12 | _Gain ("_Gain", float) = 0.5 13 | _Magnitude ("_Magnitude", float) = 1.3 14 | _Epsilon ("Epsilon", Range(0.0001, 0.1)) = 0.01 15 | 16 | _SpecularColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0.2) 17 | _SpecularPower ("Specular", Range(1.0, 50.0)) = 16.3 18 | } 19 | 20 | SubShader { 21 | Tags { 22 | "RenderType" = "Opaque" 23 | "LightMode"="ForwardBase" 24 | } 25 | 26 | LOD 100 27 | 28 | CGINCLUDE 29 | 30 | #include "UnityCG.cginc" 31 | #pragma multi_compile_fwdbase 32 | 33 | #include "Assets/Common/Shaders/SimplexNoise3D.cginc" 34 | #define FIRE_NOISE snoise 35 | 36 | // #include "Assets/Common/Shaders/ClassicNoise3D.cginc" 37 | // #define FIRE_NOISE cnoise 38 | 39 | #include "./Fire.cginc" 40 | 41 | half _Brightness; 42 | samplerCUBE _CubeMap; 43 | 44 | float4 _Color, _SpecularColor; 45 | float _SpecularPower; 46 | 47 | struct v2f { 48 | float4 pos: POSITION; 49 | float3 world: NORMAL; 50 | float3 viewDir: TEXCOORD0; 51 | float3 lightDir: TEXCOORD1; 52 | }; 53 | 54 | v2f vert (appdata_full v) { 55 | v2f OUT; 56 | OUT.pos = UnityObjectToClipPos(v.vertex); 57 | OUT.world = (mul(unity_ObjectToWorld, v.vertex)).xyz; 58 | 59 | // world space 60 | TANGENT_SPACE_ROTATION; 61 | OUT.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)); 62 | OUT.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)); 63 | 64 | return OUT; 65 | } 66 | 67 | float4 frag (v2f IN) : COLOR { 68 | float alpha = sample(IN.world.xyz); 69 | float3 norm = sample_normal(IN.world.xyz); 70 | float3 viewDir = normalize(IN.viewDir); 71 | float3 lightDir = normalize(IN.lightDir); 72 | 73 | float3 h = normalize(lightDir + viewDir); 74 | float diff = saturate(max(0.5, dot(lightDir, norm))); 75 | float spec = pow(saturate(dot(h, norm)), _SpecularPower); 76 | 77 | float3 refl = normalize(reflect(viewDir, norm)); 78 | float4 col = _Color * saturate(texCUBE(_CubeMap, refl) * _Brightness) * diff; 79 | col += spec * _SpecularColor; 80 | 81 | return float4(col.rgb, alpha * 10); 82 | } 83 | 84 | ENDCG 85 | 86 | Pass { 87 | Cull Off 88 | Blend SrcAlpha One 89 | 90 | CGPROGRAM 91 | #pragma target 3.0 92 | #pragma vertex vert 93 | #pragma fragment frag 94 | ENDCG 95 | } 96 | 97 | } 98 | 99 | } 100 | 101 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Shaders/Lighting.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c092228d1b30a47dc9905f002cfd0138 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Shaders/Normal.shader: -------------------------------------------------------------------------------- 1 | Shader "Custom/Normal" { 2 | 3 | Properties { 4 | _FireTex ("Fire Texture", 2D) = "white" {} 5 | _Scale ("Fire Scale", Vector) = (1, 3, 1, 0.5) 6 | _Lacunarity ("_Lacunarity", float) = 2.0 7 | _Gain ("_Gain", float) = 0.5 8 | _Magnitude ("_Magnitude", float) = 1.3 9 | _Epsilon ("Epsilon", Range(0.0001, 0.1)) = 0.01 10 | } 11 | 12 | SubShader { 13 | Tags { "RenderType" = "Opaque" } 14 | 15 | LOD 200 16 | 17 | CGINCLUDE 18 | 19 | #include "UnityCG.cginc" 20 | 21 | #include "Assets/Common/Shaders/SimplexNoise3D.cginc" 22 | #define FIRE_NOISE snoise 23 | 24 | // #include "Assets/Common/Shaders/ClassicNoise3D.cginc" 25 | // #define FIRE_NOISE cnoise 26 | 27 | #include "./Fire.cginc" 28 | 29 | struct v2f { 30 | float4 pos: POSITION; 31 | float3 world: NORMAL; 32 | }; 33 | 34 | v2f vert (appdata_full v) { 35 | v2f OUT; 36 | OUT.pos = UnityObjectToClipPos(v.vertex); 37 | OUT.world = (mul(unity_ObjectToWorld, v.vertex)).xyz; 38 | return OUT; 39 | } 40 | 41 | float4 frag (v2f IN) : COLOR { 42 | float3 norm = sample_normal(IN.world.xyz); 43 | norm = (norm + 1) * 0.5; 44 | return float4(norm, 1); 45 | } 46 | 47 | ENDCG 48 | 49 | Pass { 50 | Cull Off 51 | Blend One One 52 | 53 | CGPROGRAM 54 | #pragma target 3.0 55 | #pragma vertex vert 56 | #pragma fragment frag 57 | ENDCG 58 | } 59 | 60 | } 61 | 62 | } 63 | 64 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Shaders/Normal.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 53927ccf325cea045b0f4ce2e7ee6215 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 26e155fe82d5ca24b8d6da3993b4655a 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/Maskonaive2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fire/Assets/Fire/Textures/Maskonaive2.jpg -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/Maskonaive2.jpg.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1fa5ca441c27b408f80152e79abfc796 3 | timeCreated: 1492526687 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: 7 | 8900000: generatedCubemap 8 | serializedVersion: 4 9 | mipmaps: 10 | mipMapMode: 0 11 | enableMipMap: 1 12 | sRGBTexture: 1 13 | linearTexture: 0 14 | fadeOut: 0 15 | borderMipMap: 0 16 | mipMapFadeDistanceStart: 1 17 | mipMapFadeDistanceEnd: 3 18 | bumpmap: 19 | convertToNormalMap: 0 20 | externalNormalMap: 0 21 | heightScale: 0.25 22 | normalMapFilter: 0 23 | isReadable: 0 24 | grayScaleToAlpha: 0 25 | generateCubemap: 6 26 | cubemapConvolution: 0 27 | seamlessCubemap: 1 28 | textureFormat: 1 29 | maxTextureSize: 2048 30 | textureSettings: 31 | filterMode: -1 32 | aniso: -1 33 | mipBias: -1 34 | wrapMode: -1 35 | nPOTScale: 1 36 | lightmap: 0 37 | compressionQuality: 50 38 | spriteMode: 0 39 | spriteExtrude: 1 40 | spriteMeshType: 1 41 | alignment: 0 42 | spritePivot: {x: 0.5, y: 0.5} 43 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 44 | spritePixelsToUnits: 100 45 | alphaUsage: 1 46 | alphaIsTransparency: 0 47 | spriteTessellationDetail: -1 48 | textureType: 0 49 | textureShape: 2 50 | maxTextureSizeSet: 0 51 | compressionQualitySet: 0 52 | textureFormatSet: 0 53 | platformSettings: 54 | - buildTarget: DefaultTexturePlatform 55 | maxTextureSize: 2048 56 | textureFormat: -1 57 | textureCompression: 1 58 | compressionQuality: 50 59 | crunchedCompression: 0 60 | allowsAlphaSplitting: 0 61 | overridden: 0 62 | - buildTarget: Standalone 63 | maxTextureSize: 2048 64 | textureFormat: -1 65 | textureCompression: 1 66 | compressionQuality: 50 67 | crunchedCompression: 0 68 | allowsAlphaSplitting: 0 69 | overridden: 0 70 | - buildTarget: WebGL 71 | maxTextureSize: 2048 72 | textureFormat: -1 73 | textureCompression: 1 74 | compressionQuality: 50 75 | crunchedCompression: 0 76 | allowsAlphaSplitting: 0 77 | overridden: 0 78 | spriteSheet: 79 | serializedVersion: 2 80 | sprites: [] 81 | outline: [] 82 | spritePackingTag: 83 | userData: 84 | assetBundleName: 85 | assetBundleVariant: 86 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/SaintPetersBasilica.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fire/Assets/Fire/Textures/SaintPetersBasilica.jpg -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/SaintPetersBasilica.jpg.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 740108d29e66d461b92b1d4aa5fdfbdc 3 | timeCreated: 1492526691 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: 7 | 8900000: generatedCubemap 8 | serializedVersion: 4 9 | mipmaps: 10 | mipMapMode: 0 11 | enableMipMap: 1 12 | sRGBTexture: 1 13 | linearTexture: 0 14 | fadeOut: 0 15 | borderMipMap: 0 16 | mipMapFadeDistanceStart: 1 17 | mipMapFadeDistanceEnd: 3 18 | bumpmap: 19 | convertToNormalMap: 0 20 | externalNormalMap: 0 21 | heightScale: 0.25 22 | normalMapFilter: 0 23 | isReadable: 0 24 | grayScaleToAlpha: 0 25 | generateCubemap: 6 26 | cubemapConvolution: 0 27 | seamlessCubemap: 1 28 | textureFormat: 1 29 | maxTextureSize: 2048 30 | textureSettings: 31 | filterMode: -1 32 | aniso: -1 33 | mipBias: -1 34 | wrapMode: -1 35 | nPOTScale: 1 36 | lightmap: 0 37 | compressionQuality: 50 38 | spriteMode: 0 39 | spriteExtrude: 1 40 | spriteMeshType: 1 41 | alignment: 0 42 | spritePivot: {x: 0.5, y: 0.5} 43 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 44 | spritePixelsToUnits: 100 45 | alphaUsage: 1 46 | alphaIsTransparency: 0 47 | spriteTessellationDetail: -1 48 | textureType: 0 49 | textureShape: 2 50 | maxTextureSizeSet: 0 51 | compressionQualitySet: 0 52 | textureFormatSet: 0 53 | platformSettings: 54 | - buildTarget: DefaultTexturePlatform 55 | maxTextureSize: 2048 56 | textureFormat: -1 57 | textureCompression: 1 58 | compressionQuality: 50 59 | crunchedCompression: 0 60 | allowsAlphaSplitting: 0 61 | overridden: 0 62 | - buildTarget: Standalone 63 | maxTextureSize: 2048 64 | textureFormat: -1 65 | textureCompression: 1 66 | compressionQuality: 50 67 | crunchedCompression: 0 68 | allowsAlphaSplitting: 0 69 | overridden: 0 70 | - buildTarget: WebGL 71 | maxTextureSize: 2048 72 | textureFormat: -1 73 | textureCompression: 1 74 | compressionQuality: 50 75 | crunchedCompression: 0 76 | allowsAlphaSplitting: 0 77 | overridden: 0 78 | spriteSheet: 79 | serializedVersion: 2 80 | sprites: [] 81 | outline: [] 82 | spritePackingTag: 83 | userData: 84 | assetBundleName: 85 | assetBundleVariant: 86 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/fire.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fire/Assets/Fire/Textures/fire.png -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/fire.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 70c42d55f8a32494a86cfa698b59ff97 3 | TextureImporter: 4 | fileIDToRecycleName: {} 5 | serializedVersion: 4 6 | mipmaps: 7 | mipMapMode: 0 8 | enableMipMap: 0 9 | sRGBTexture: 1 10 | linearTexture: 0 11 | fadeOut: 0 12 | borderMipMap: 0 13 | mipMapFadeDistanceStart: 1 14 | mipMapFadeDistanceEnd: 3 15 | bumpmap: 16 | convertToNormalMap: 0 17 | externalNormalMap: 0 18 | heightScale: 0.25 19 | normalMapFilter: 0 20 | isReadable: 1 21 | grayScaleToAlpha: 0 22 | generateCubemap: 6 23 | cubemapConvolution: 0 24 | seamlessCubemap: 0 25 | textureFormat: -2 26 | maxTextureSize: 1024 27 | textureSettings: 28 | filterMode: 2 29 | aniso: -1 30 | mipBias: -1 31 | wrapMode: 1 32 | nPOTScale: 1 33 | lightmap: 0 34 | compressionQuality: 50 35 | spriteMode: 0 36 | spriteExtrude: 1 37 | spriteMeshType: 1 38 | alignment: 0 39 | spritePivot: {x: 0.5, y: 0.5} 40 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 41 | spritePixelsToUnits: 100 42 | alphaUsage: 1 43 | alphaIsTransparency: 1 44 | spriteTessellationDetail: -1 45 | textureType: 0 46 | textureShape: 1 47 | maxTextureSizeSet: 0 48 | compressionQualitySet: 0 49 | textureFormatSet: 0 50 | platformSettings: 51 | - buildTarget: DefaultTexturePlatform 52 | maxTextureSize: 1024 53 | textureFormat: 2 54 | textureCompression: 1 55 | compressionQuality: 50 56 | crunchedCompression: 0 57 | allowsAlphaSplitting: 0 58 | overridden: 0 59 | - buildTarget: Standalone 60 | maxTextureSize: 1024 61 | textureFormat: 2 62 | textureCompression: 1 63 | compressionQuality: 50 64 | crunchedCompression: 0 65 | allowsAlphaSplitting: 0 66 | overridden: 0 67 | - buildTarget: WebGL 68 | maxTextureSize: 1024 69 | textureFormat: 2 70 | textureCompression: 1 71 | compressionQuality: 50 72 | crunchedCompression: 0 73 | allowsAlphaSplitting: 0 74 | overridden: 0 75 | spriteSheet: 76 | serializedVersion: 2 77 | sprites: [] 78 | outline: [] 79 | spritePackingTag: 80 | userData: 81 | assetBundleName: 82 | assetBundleVariant: 83 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/skybox.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fire/Assets/Fire/Textures/skybox.jpg -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/skybox.jpg.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: dd3edab9032f648a6bdca88833873a9d 3 | timeCreated: 1492524483 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: 7 | 8900000: generatedCubemap 8 | serializedVersion: 4 9 | mipmaps: 10 | mipMapMode: 0 11 | enableMipMap: 0 12 | sRGBTexture: 1 13 | linearTexture: 0 14 | fadeOut: 0 15 | borderMipMap: 0 16 | mipMapFadeDistanceStart: 1 17 | mipMapFadeDistanceEnd: 3 18 | bumpmap: 19 | convertToNormalMap: 0 20 | externalNormalMap: 0 21 | heightScale: 0.25 22 | normalMapFilter: 0 23 | isReadable: 0 24 | grayScaleToAlpha: 0 25 | generateCubemap: 6 26 | cubemapConvolution: 0 27 | seamlessCubemap: 0 28 | textureFormat: 1 29 | maxTextureSize: 2048 30 | textureSettings: 31 | filterMode: -1 32 | aniso: -1 33 | mipBias: -1 34 | wrapMode: -1 35 | nPOTScale: 1 36 | lightmap: 0 37 | compressionQuality: 50 38 | spriteMode: 0 39 | spriteExtrude: 1 40 | spriteMeshType: 1 41 | alignment: 0 42 | spritePivot: {x: 0.5, y: 0.5} 43 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 44 | spritePixelsToUnits: 100 45 | alphaUsage: 1 46 | alphaIsTransparency: 1 47 | spriteTessellationDetail: -1 48 | textureType: 0 49 | textureShape: 2 50 | maxTextureSizeSet: 0 51 | compressionQualitySet: 0 52 | textureFormatSet: 0 53 | platformSettings: 54 | - buildTarget: DefaultTexturePlatform 55 | maxTextureSize: 2048 56 | textureFormat: -1 57 | textureCompression: 1 58 | compressionQuality: 50 59 | crunchedCompression: 0 60 | allowsAlphaSplitting: 0 61 | overridden: 0 62 | - buildTarget: Standalone 63 | maxTextureSize: 2048 64 | textureFormat: -1 65 | textureCompression: 1 66 | compressionQuality: 50 67 | crunchedCompression: 0 68 | allowsAlphaSplitting: 0 69 | overridden: 0 70 | - buildTarget: WebGL 71 | maxTextureSize: 2048 72 | textureFormat: -1 73 | textureCompression: 1 74 | compressionQuality: 50 75 | crunchedCompression: 0 76 | allowsAlphaSplitting: 0 77 | overridden: 0 78 | spriteSheet: 79 | serializedVersion: 2 80 | sprites: [] 81 | outline: [] 82 | spritePackingTag: 83 | userData: 84 | assetBundleName: 85 | assetBundleVariant: 86 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/skybox2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fire/Assets/Fire/Textures/skybox2.jpg -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/skybox2.jpg.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 721e85d8a686d488097d0729f6e256d5 3 | timeCreated: 1492526950 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: 7 | 8900000: generatedCubemap 8 | serializedVersion: 4 9 | mipmaps: 10 | mipMapMode: 0 11 | enableMipMap: 1 12 | sRGBTexture: 1 13 | linearTexture: 0 14 | fadeOut: 0 15 | borderMipMap: 0 16 | mipMapFadeDistanceStart: 1 17 | mipMapFadeDistanceEnd: 3 18 | bumpmap: 19 | convertToNormalMap: 0 20 | externalNormalMap: 0 21 | heightScale: 0.25 22 | normalMapFilter: 0 23 | isReadable: 0 24 | grayScaleToAlpha: 0 25 | generateCubemap: 6 26 | cubemapConvolution: 0 27 | seamlessCubemap: 0 28 | textureFormat: 1 29 | maxTextureSize: 2048 30 | textureSettings: 31 | filterMode: -1 32 | aniso: -1 33 | mipBias: -1 34 | wrapMode: -1 35 | nPOTScale: 1 36 | lightmap: 0 37 | compressionQuality: 50 38 | spriteMode: 0 39 | spriteExtrude: 1 40 | spriteMeshType: 1 41 | alignment: 0 42 | spritePivot: {x: 0.5, y: 0.5} 43 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 44 | spritePixelsToUnits: 100 45 | alphaUsage: 1 46 | alphaIsTransparency: 0 47 | spriteTessellationDetail: -1 48 | textureType: 0 49 | textureShape: 2 50 | maxTextureSizeSet: 0 51 | compressionQualitySet: 0 52 | textureFormatSet: 0 53 | platformSettings: 54 | - buildTarget: DefaultTexturePlatform 55 | maxTextureSize: 2048 56 | textureFormat: -1 57 | textureCompression: 1 58 | compressionQuality: 50 59 | crunchedCompression: 0 60 | allowsAlphaSplitting: 0 61 | overridden: 0 62 | - buildTarget: Standalone 63 | maxTextureSize: 2048 64 | textureFormat: -1 65 | textureCompression: 1 66 | compressionQuality: 50 67 | crunchedCompression: 0 68 | allowsAlphaSplitting: 0 69 | overridden: 0 70 | - buildTarget: WebGL 71 | maxTextureSize: 2048 72 | textureFormat: -1 73 | textureCompression: 1 74 | compressionQuality: 50 75 | crunchedCompression: 0 76 | allowsAlphaSplitting: 0 77 | overridden: 0 78 | spriteSheet: 79 | serializedVersion: 2 80 | sprites: [] 81 | outline: [] 82 | spritePackingTag: 83 | userData: 84 | assetBundleName: 85 | assetBundleVariant: 86 | -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/skybox3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fire/Assets/Fire/Textures/skybox3.jpg -------------------------------------------------------------------------------- /Fire/Assets/Fire/Textures/skybox3.jpg.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9504395333e0b4e39bea3e451f2251f9 3 | timeCreated: 1492527024 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: 7 | 8900000: generatedCubemap 8 | serializedVersion: 4 9 | mipmaps: 10 | mipMapMode: 0 11 | enableMipMap: 1 12 | sRGBTexture: 1 13 | linearTexture: 0 14 | fadeOut: 0 15 | borderMipMap: 0 16 | mipMapFadeDistanceStart: 1 17 | mipMapFadeDistanceEnd: 3 18 | bumpmap: 19 | convertToNormalMap: 0 20 | externalNormalMap: 0 21 | heightScale: 0.25 22 | normalMapFilter: 0 23 | isReadable: 0 24 | grayScaleToAlpha: 0 25 | generateCubemap: 6 26 | cubemapConvolution: 0 27 | seamlessCubemap: 0 28 | textureFormat: 1 29 | maxTextureSize: 2048 30 | textureSettings: 31 | filterMode: -1 32 | aniso: -1 33 | mipBias: -1 34 | wrapMode: -1 35 | nPOTScale: 1 36 | lightmap: 0 37 | compressionQuality: 50 38 | spriteMode: 0 39 | spriteExtrude: 1 40 | spriteMeshType: 1 41 | alignment: 0 42 | spritePivot: {x: 0.5, y: 0.5} 43 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 44 | spritePixelsToUnits: 100 45 | alphaUsage: 1 46 | alphaIsTransparency: 0 47 | spriteTessellationDetail: -1 48 | textureType: 0 49 | textureShape: 2 50 | maxTextureSizeSet: 0 51 | compressionQualitySet: 0 52 | textureFormatSet: 0 53 | platformSettings: 54 | - buildTarget: DefaultTexturePlatform 55 | maxTextureSize: 2048 56 | textureFormat: -1 57 | textureCompression: 1 58 | compressionQuality: 50 59 | crunchedCompression: 0 60 | allowsAlphaSplitting: 0 61 | overridden: 0 62 | - buildTarget: Standalone 63 | maxTextureSize: 2048 64 | textureFormat: -1 65 | textureCompression: 1 66 | compressionQuality: 50 67 | crunchedCompression: 0 68 | allowsAlphaSplitting: 0 69 | overridden: 0 70 | - buildTarget: WebGL 71 | maxTextureSize: 2048 72 | textureFormat: -1 73 | textureCompression: 1 74 | compressionQuality: 50 75 | crunchedCompression: 0 76 | allowsAlphaSplitting: 0 77 | overridden: 0 78 | spriteSheet: 79 | serializedVersion: 2 80 | sprites: [] 81 | outline: [] 82 | spritePackingTag: 83 | userData: 84 | assetBundleName: 85 | assetBundleVariant: 86 | -------------------------------------------------------------------------------- /Fire/Captures/Capture.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fire/Captures/Capture.gif -------------------------------------------------------------------------------- /Fire/README.md: -------------------------------------------------------------------------------- 1 | Fire 2 | ================= 3 | 4 | 炎をテーマにしたスケッチ 5 | 6 | Object spaceでのRaymarhingとCubemapを組み合わせて、Specularなサーフェイスを持つ有機的なジオメトリを得る 7 | 8 | ![Capture](https://raw.githubusercontent.com/mattatz/InteractiveCoding/master/Fire/Captures/Capture.gif) 9 | 10 | ## Tools 11 | 12 | - Unity 13 | 14 | ## Sources 15 | 16 | - mattatz / unity-procedural-volumetric-fire - https://github.com/mattatz/unity-procedural-volumetric-fire 17 | - keijiro / NoiseShader - https://github.com/keijiro/NoiseShader 18 | -------------------------------------------------------------------------------- /Fish/.gitignore: -------------------------------------------------------------------------------- 1 | # Mac OS X cruft 2 | *.pbxuser 3 | *.mode1v3 4 | *.mode2v3 5 | *.user 6 | project.xcworkspace 7 | xcuserdata/ 8 | *.xccheckout 9 | .DS_Store 10 | 11 | build/ 12 | lib/libcinder*.a 13 | lib/msw/x86/cinder*.lib 14 | lib/msw/x64/cinder*.lib 15 | lib/msw/x86/cinder*.idb 16 | lib/msw/x64/cinder*.idb 17 | lib/msw/x86/cinder*.pdb 18 | lib/msw/x64/cinder*.pdb 19 | lib/winrt/x86/cinder*.lib 20 | lib/winrt/x64/cinder*.lib 21 | 22 | # Windows cruft 23 | *.suo 24 | *.ncb 25 | *.sdf 26 | *.VC.opendb 27 | *.VC.db 28 | *.opensdf 29 | Debug/ 30 | Release/ 31 | Debug_ANGLE/ 32 | Release_ANGLE/ 33 | Debug_Shared/ 34 | Release_Shared/ 35 | ipch/ 36 | **/vc2013/enc_temp_folder/** 37 | 38 | # doxygen generated files 39 | docs/html 40 | docs/xml 41 | docs/doxygen/cinder.tag 42 | docs/*/node_modules 43 | *.pyc 44 | 45 | # Android cruft 46 | *.swp 47 | .idea 48 | lib/android/android-19/armeabi-v7a/libcinder*.a 49 | lib/android/android-19/armeabi/libcinder*.a 50 | lib/android/android-19/mips/libcinder*.a 51 | lib/android/android-19/x86/libcinder*.a 52 | lib/android/android-21/arm64-v8a/libcinder*.a 53 | lib/android/android-21/armeabi-v7a/libcinder*.a 54 | lib/android/android-21/armeabi/libcinder*.a 55 | lib/android/android-21/mips/libcinder*.a 56 | lib/android/android-21/mips64/libcinder*.a 57 | lib/android/android-21/x86/libcinder*.a 58 | lib/android/android-21/x86_64/libcinder*.a 59 | 60 | *.iml 61 | local.properties 62 | proguard-rules.pro 63 | .gradle/ 64 | jniLibs/ 65 | 66 | # cmake generated files 67 | cinderConfig.cmake 68 | 69 | # CinderBlocks 70 | blocks/ 71 | !blocks/[__AppTemplates,Box2D,Cairo,FMOD,LocationManager,MotionManager,OSC,QuickTime,TUIO] 72 | -------------------------------------------------------------------------------- /Fish/assets/particle.frag: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 gColor; 4 | in vec3 gPosition; 5 | 6 | out vec4 oColor; 7 | 8 | void main() 9 | { 10 | vec3 dx = dFdx(gPosition); 11 | vec3 dy = dFdy(gPosition); 12 | vec3 normal = normalize(cross(normalize(dx), normalize(dy))); 13 | 14 | oColor = vec4((normal + 1.0) * 0.5, 1.0); 15 | } 16 | -------------------------------------------------------------------------------- /Fish/assets/particle.geom: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | layout(points) in; 4 | layout(triangle_strip, max_vertices = 128) out; 5 | 6 | in vec4 vColor[]; 7 | in vec3 vVelocity[]; 8 | in float vSeed[]; 9 | in mat4 vMatrix[]; 10 | 11 | out vec4 gColor; 12 | out vec3 gPosition; 13 | 14 | // uniform mat4 ciProjectionMatrix; 15 | // uniform mat4 ciViewMatrix; 16 | // uniform mat4 ciModelView; 17 | uniform mat4 ciModelViewProjection; 18 | 19 | uniform float uT; 20 | 21 | // Constructing 3D Geometric Fish Models 22 | // http://www.dgp.toronto.edu/~tu/thesis/node63.html 23 | 24 | void corner(vec3 spine, float width, float height, out vec3 lt, out vec3 rt, out vec3 rb, out vec3 lb) { 25 | float hw = width * 0.5; 26 | float hh = height * 0.5; 27 | lt = spine + vec3(-hw, hh, 0); 28 | rt = spine + vec3(hw, hh, 0); 29 | rb = spine + vec3(hw, -hh, 0); 30 | lb = spine + vec3(-hw, -hh, 0); 31 | } 32 | 33 | void outputVertex() { 34 | gPosition = gl_Position.xyz; 35 | EmitVertex(); 36 | } 37 | 38 | void triangle(vec3 p0, vec3 p1, vec3 p2) { 39 | // mat4 m = ciModelViewProjection; 40 | mat4 m = vMatrix[0]; 41 | gl_Position = m * vec4(p0, 1.0); outputVertex(); 42 | gl_Position = m * vec4(p1, 1.0); outputVertex(); 43 | gl_Position = m * vec4(p2, 1.0); outputVertex(); 44 | EndPrimitive(); 45 | } 46 | 47 | void quad(vec3 p0, vec3 p1, vec3 p2, vec3 p3) { 48 | // mat4 m = ciModelViewProjection; 49 | mat4 m = vMatrix[0]; 50 | gl_Position = m * vec4(p0, 1.0); outputVertex(); 51 | gl_Position = m * vec4(p1, 1.0); outputVertex(); 52 | gl_Position = m * vec4(p3, 1.0); outputVertex(); 53 | gl_Position = m * vec4(p2, 1.0); outputVertex(); 54 | EndPrimitive(); 55 | } 56 | 57 | void side( 58 | vec3 lt0, vec3 rt0, vec3 rb0, vec3 lb0, 59 | vec3 lt1, vec3 rt1, vec3 rb1, vec3 lb1 60 | ) { 61 | quad(lt0, rt0, rt1, lt1); 62 | quad(rt0, rb0, rb1, rt1); 63 | quad(rb0, lb0, lb1, rb1); 64 | quad(lb0, lt0, lt1, lb1); 65 | } 66 | 67 | vec3 wave(vec3 ori, vec3 vel, vec3 p) { 68 | /* 69 | vec3 right = vec3(1.0, 0.0, 0.0); 70 | float intensity = length(vel); 71 | // float intensity = 0.1; 72 | float d = length(p - ori); 73 | float offset = vSeed[0] * 10.0; 74 | return p + right * sin((offset + uT) + d * 1.45) * intensity * pow(d, 0.75); 75 | */ 76 | return p; 77 | } 78 | 79 | void main() 80 | { 81 | gColor = vColor[0]; 82 | 83 | // vec4 position = gl_in[0].gl_Position; 84 | vec4 position = vec4(0, 0, 0, 1); 85 | 86 | vec3 vel = vVelocity[0]; 87 | 88 | // head 89 | vec3 head = position.xyz + vec3(0, 0, 0.5); 90 | vec3 spine = position.xyz; 91 | 92 | vec3 lt0, rt0, rb0, lb0; 93 | corner(wave(head, vel, spine), 0.7, 0.8, lt0, rt0, rb0, lb0); 94 | 95 | triangle(lt0, head, rt0); 96 | triangle(rt0, head, rb0); 97 | triangle(rb0, head, lb0); 98 | triangle(lb0, head, lt0); 99 | 100 | vec3 lt1, rt1, rb1, lb1; 101 | spine += vec3(0, 0, -0.5); 102 | corner(wave(head, vel, spine), 0.8, 1.0, lt1, rt1, rb1, lb1); 103 | 104 | side( 105 | lt0, rt0, rb0, lb0, 106 | lt1, rt1, rb1, lb1 107 | ); 108 | 109 | vec3 lt2, rt2, rb2, lb2; 110 | spine += vec3(0, 0, -0.5); 111 | corner(wave(head, vel, spine), 0.7, 0.8, lt2, rt2, rb2, lb2); 112 | 113 | side( 114 | lt1, rt1, rb1, lb1, 115 | lt2, rt2, rb2, lb2 116 | ); 117 | 118 | vec3 lt3, rt3, rb3, lb3; 119 | spine += vec3(0, 0, -0.2); 120 | corner(wave(head, vel, spine), 0.5, 0.5, lt3, rt3, rb3, lb3); 121 | 122 | side( 123 | lt2, rt2, rb2, lb2, 124 | lt3, rt3, rb3, lb3 125 | ); 126 | 127 | vec3 lt4, rt4, rb4, lb4; 128 | spine += vec3(0, 0, -0.3); 129 | corner(wave(head, vel, spine), 0.2, 0.2, lt4, rt4, rb4, lb4); 130 | 131 | side( 132 | lt3, rt3, rb3, lb3, 133 | lt4, rt4, rb4, lb4 134 | ); 135 | 136 | vec3 tailt = wave(head, vel, spine + vec3(0, 0.5, -0.5)); 137 | vec3 tailb = wave(head, vel, spine + vec3(0, -0.5, -0.5)); 138 | 139 | triangle(lt4, rt4, tailt); 140 | quad(rt4, rb4, tailb, tailt); 141 | triangle(rb4, lb4, tailb); 142 | quad(lb4, lt4, tailt, tailb); 143 | 144 | } 145 | -------------------------------------------------------------------------------- /Fish/assets/particle.vert: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 ciPosition; 4 | in vec4 ciColor; 5 | 6 | in vec3 Velocity; 7 | in float Seed; 8 | 9 | out vec4 vColor; 10 | out vec3 vVelocity; 11 | out float vSeed; 12 | out mat4 vMatrix; 13 | 14 | uniform mat4 ciProjectionMatrix; 15 | uniform mat4 ciModelMatrix; 16 | uniform mat4 ciModelView; 17 | uniform mat4 ciModelViewProjection; 18 | uniform mat3 ciNormalMatrix; 19 | uniform mat4 ciViewMatrix; 20 | uniform mat4 ciViewMatrixInverse; 21 | 22 | uniform float uT; 23 | 24 | mat4 rotate_angle_axis(vec3 axis, float angle) 25 | { 26 | axis = normalize(axis); 27 | float s = sin(angle); 28 | float c = cos(angle); 29 | float oc = 1.0 - c; 30 | 31 | return mat4( 32 | oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0, 33 | oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0, 34 | oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0, 35 | 0.0, 0.0, 0.0, 1.0 36 | ); 37 | } 38 | 39 | // http://stackoverflow.com/questions/349050/calculating-a-lookat-matrix 40 | mat4 look_at_matrix(vec3 forward, vec3 up) { 41 | vec3 xaxis = cross(forward, up); 42 | vec3 yaxis = up; 43 | vec3 zaxis = forward; 44 | return mat4( 45 | xaxis.x, yaxis.x, zaxis.x, 0, 46 | xaxis.y, yaxis.y, zaxis.y, 0, 47 | xaxis.z, yaxis.z, zaxis.z, 0, 48 | 0, 0, 0, 1 49 | ); 50 | } 51 | 52 | void main( void ) 53 | { 54 | gl_Position = vec4(0, 0, 0, 1.0); 55 | vColor = ciColor; 56 | vVelocity = Velocity; 57 | vSeed = Seed; 58 | 59 | // mat4 rot = rotate_angle_axis(normalize(Velocity), uT); 60 | mat4 rot = look_at_matrix(vec3(0, 0, 1), vec3(0, 1, 0)); 61 | 62 | mat4 model = mat4( 63 | 1, 0, 0, 0, 64 | 0, 1, 0, 0, 65 | 0, 0, 1, 0, 66 | ciPosition.x, ciPosition.y, ciPosition.z, 1 67 | ) * rot; 68 | // ); 69 | 70 | /* 71 | mat4 model = mat4( 72 | rot[0][0], rot[1][0], rot[2][0], 0, 73 | rot[0][1], rot[1][1], rot[2][1], 0, 74 | rot[0][2], rot[1][2], rot[2][2], 0, 75 | ciPosition.x, ciPosition.y, ciPosition.z, 1 76 | ); 77 | */ 78 | 79 | vMatrix = ciProjectionMatrix * ciViewMatrix * model; 80 | // vMatrix = ciProjectionMatrix * ciViewMatrix * ciModelMatrix; 81 | // vMatrix = ciModelViewProjection; 82 | } 83 | -------------------------------------------------------------------------------- /Fish/assets/particleUpdate.vs: -------------------------------------------------------------------------------- 1 | #version 150 core 2 | 3 | in vec3 iPosition; 4 | in vec4 iColor; 5 | in vec3 iVelocity; 6 | in float iSeed; 7 | 8 | out vec3 position; 9 | out vec4 color; 10 | out vec3 velocity; 11 | out float seed; 12 | 13 | uniform float uDt; 14 | 15 | void main() 16 | { 17 | position = iPosition + iVelocity * uDt; 18 | color = iColor; 19 | velocity = iVelocity; 20 | seed = iSeed; 21 | } 22 | -------------------------------------------------------------------------------- /Fish/include/Resources.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cinder/CinderResources.h" 3 | 4 | //#define RES_MY_RES CINDER_RESOURCE( ../resources/, image_name.png, 128, IMAGE ) 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Fish/resources/CinderApp.icns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Fish/resources/CinderApp.icns -------------------------------------------------------------------------------- /Fish/src/FishApp.cpp: -------------------------------------------------------------------------------- 1 | #include "cinder/app/App.h" 2 | #include "cinder/app/RendererGl.h" 3 | #include "cinder/Rand.h" 4 | #include "cinder/gl/gl.h" 5 | #include "cinder/params/Params.h" 6 | #include "cinder/CameraUi.h" 7 | #include "cinder/Log.h" 8 | 9 | using namespace ci; 10 | using namespace ci::app; 11 | using namespace std; 12 | 13 | struct Particle 14 | { 15 | vec3 position; 16 | ColorA color; 17 | vec3 velocity; 18 | float seed; 19 | }; 20 | 21 | const int NUM_PARTICLES = 100; 22 | const int POSITION_INDEX = 0; 23 | const int COLOR_INDEX = 1; 24 | const int VELOCITY_INDEX = 2; 25 | const int SEED_INDEX = 3; 26 | 27 | class FishApp : public App { 28 | public: 29 | void setup() override; 30 | void update() override; 31 | void draw() override; 32 | 33 | void keyDown(KeyEvent event) override; 34 | void mouseDown(MouseEvent event) override; 35 | void mouseMove(MouseEvent event) override; 36 | void mouseUp(MouseEvent event) override; 37 | void mouseDrag(MouseEvent event) override; 38 | void mouseWheel(MouseEvent event) override; 39 | 40 | private: 41 | gl::GlslProgRef mRenderProg; 42 | gl::GlslProgRef mUpdateProg; 43 | 44 | gl::VaoRef mAttributes[2]; 45 | gl::VboRef mParticleBuffer[2]; 46 | 47 | std::uint32_t mSourceIndex = 0; 48 | std::uint32_t mDestinationIndex = 1; 49 | 50 | CameraPersp mViewCam; 51 | CameraUi mCamUi; 52 | 53 | float mWindowScale; 54 | float mPrev; 55 | }; 56 | 57 | void FishApp::setup() 58 | { 59 | mWindowScale = getWindowContentScale(); 60 | 61 | mViewCam.setEyePoint(vec3(0.0f, 0.0f, -20.0f)); 62 | mViewCam.setPerspective(60, getWindowWidth() / getWindowHeight(), 1, 10000); 63 | mViewCam.lookAt(vec3(0)); 64 | mCamUi = CameraUi(&mViewCam); 65 | 66 | vector particles; 67 | particles.assign(NUM_PARTICLES, Particle()); 68 | 69 | vector colors; 70 | colors.push_back(Color(0.6f, 0.62f, 0.9f)); 71 | colors.push_back(Color(0.8f, 0.2f, 0.55f)); 72 | colors.push_back(Color(0.9f, 0.9f, 0.45f)); 73 | 74 | for(int i = 0; i < particles.size(); i++) { 75 | auto &p = particles.at(i); 76 | p.position = Rand::randVec3() * Rand::randFloat(30.0f); 77 | p.velocity = Rand::randVec3() * Rand::randFloat(1.0f); 78 | p.seed = Rand::randFloat(); 79 | p.color = colors.at(Rand::randInt(colors.size())); 80 | } 81 | 82 | mParticleBuffer[mSourceIndex] = gl::Vbo::create(GL_ARRAY_BUFFER, particles.size() * sizeof(Particle), particles.data(), GL_STATIC_DRAW); 83 | mParticleBuffer[mDestinationIndex] = gl::Vbo::create( GL_ARRAY_BUFFER, particles.size() * sizeof(Particle), nullptr, GL_STATIC_DRAW); 84 | 85 | for(int i = 0; i < 2; ++i) { 86 | mAttributes[i] = gl::Vao::create(); 87 | gl::ScopedVao vao(mAttributes[i]); 88 | 89 | gl::ScopedBuffer buffer(mParticleBuffer[i]); 90 | gl::enableVertexAttribArray(POSITION_INDEX); 91 | gl::enableVertexAttribArray(COLOR_INDEX); 92 | gl::enableVertexAttribArray(VELOCITY_INDEX); 93 | gl::enableVertexAttribArray(SEED_INDEX); 94 | 95 | gl::vertexAttribPointer(POSITION_INDEX, 3, GL_FLOAT, GL_FALSE, sizeof(Particle), (const GLvoid*)offsetof(Particle, position) ); 96 | gl::vertexAttribPointer(COLOR_INDEX, 4, GL_FLOAT, GL_FALSE, sizeof(Particle), (const GLvoid*)offsetof(Particle, color) ); 97 | gl::vertexAttribPointer(VELOCITY_INDEX, 3, GL_FLOAT, GL_FALSE, sizeof(Particle), (const GLvoid*)offsetof(Particle, velocity) ); 98 | gl::vertexAttribPointer(SEED_INDEX, 1, GL_FLOAT, GL_FALSE, sizeof(Particle), (const GLvoid*)offsetof(Particle, seed) ); 99 | } 100 | 101 | mRenderProg = gl::GlslProg::create(gl::GlslProg::Format() 102 | .vertex(loadAsset("particle.vert")) 103 | .geometry(loadAsset("particle.geom")) 104 | .fragment(loadAsset("particle.frag")) 105 | .attribLocation("Velocity", VELOCITY_INDEX) 106 | .attribLocation("Seed", SEED_INDEX) 107 | ); 108 | 109 | mUpdateProg = gl::GlslProg::create(gl::GlslProg::Format().vertex(loadAsset("particleUpdate.vs")) 110 | .feedbackFormat(GL_INTERLEAVED_ATTRIBS) 111 | .feedbackVaryings({ 112 | "position", 113 | "color", 114 | "velocity", 115 | "seed" 116 | }) 117 | .attribLocation("iPosition", POSITION_INDEX) 118 | .attribLocation("iColor", COLOR_INDEX) 119 | .attribLocation("iVelocity", VELOCITY_INDEX) 120 | .attribLocation("iSeed", SEED_INDEX) 121 | ); 122 | 123 | } 124 | 125 | void FishApp::update() 126 | { 127 | gl::ScopedGlslProg prog(mUpdateProg); 128 | 129 | float t = getElapsedSeconds(); 130 | mUpdateProg->uniform("uDt", t - mPrev); 131 | 132 | gl::ScopedState rasterizer(GL_RASTERIZER_DISCARD, true); // turn off fragment stage 133 | 134 | gl::ScopedVao source(mAttributes[mSourceIndex]); 135 | 136 | gl::bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, mParticleBuffer[mDestinationIndex]); 137 | gl::beginTransformFeedback(GL_POINTS); 138 | 139 | gl::drawArrays(GL_POINTS, 0, NUM_PARTICLES); 140 | 141 | gl::endTransformFeedback(); 142 | 143 | std::swap(mSourceIndex, mDestinationIndex); 144 | 145 | mPrev = t; 146 | } 147 | 148 | void FishApp::draw() 149 | { 150 | gl::clear(Color(0, 0, 0)); 151 | 152 | gl::ScopedGlslProg render(mRenderProg); 153 | gl::ScopedDepthWrite write(true); 154 | gl::ScopedDepthTest test(true); 155 | gl::ScopedFaceCulling cull(GL_FRONT, true); 156 | 157 | gl::ScopedVao vao(mAttributes[mSourceIndex]); 158 | 159 | vec3 center = vec3(0.0f, 0.0f, 0.0f); 160 | 161 | float t = getElapsedSeconds(); 162 | mRenderProg->uniform("uT", t); 163 | 164 | // gl::ScopedBlend blendScope(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 165 | 166 | // gl::pushModelMatrix(); 167 | 168 | // auto t = getElapsedSeconds(); 169 | // gl::rotate(t * 0.0025f, vec3(0.1, 1, 0)); 170 | 171 | gl::pushMatrices(); 172 | gl::setMatrices(mViewCam); 173 | 174 | gl::translate(center); 175 | gl::scale(vec3(2.2f)); 176 | gl::rotate(t * 0.1f, vec3(0, 1, 0)); 177 | 178 | // gl::viewport(0.0f, 0.0f, getWindowWidth() * mWindowScale, getWindowHeight() * mWindowScale); 179 | gl::context()->setDefaultShaderVars(); 180 | 181 | gl::drawArrays(GL_POINTS, 0, NUM_PARTICLES); 182 | gl::popMatrices(); 183 | 184 | // mParams->draw(); 185 | } 186 | 187 | void FishApp::keyDown(KeyEvent event) 188 | { 189 | if (event.getCode() == KeyEvent::KEY_ESCAPE) quit(); 190 | } 191 | 192 | void FishApp::mouseDown(MouseEvent event) 193 | { 194 | mCamUi.mouseDown(event); 195 | } 196 | 197 | void FishApp::mouseMove(MouseEvent event) 198 | { 199 | } 200 | 201 | void FishApp::mouseUp(MouseEvent event) 202 | { 203 | mCamUi.mouseUp(event); 204 | } 205 | 206 | void FishApp::mouseDrag(MouseEvent event) 207 | { 208 | Rectf r = Rectf(0, 0, getWindowWidth() * mWindowScale, getWindowHeight() * mWindowScale); 209 | if (r.contains(event.getPos())) { 210 | mCamUi.mouseDrag(event); 211 | } 212 | } 213 | 214 | void FishApp::mouseWheel(MouseEvent event) 215 | { 216 | mCamUi.mouseWheel(event); 217 | } 218 | 219 | CINDER_APP( FishApp, RendererGl ) 220 | -------------------------------------------------------------------------------- /Fish/xcode/Fish_Prefix.pch: -------------------------------------------------------------------------------- 1 | #if defined( __cplusplus ) 2 | #include "cinder/Cinder.h" 3 | 4 | #include "cinder/app/App.h" 5 | 6 | #include "cinder/gl/gl.h" 7 | 8 | #include "cinder/CinderMath.h" 9 | #include "cinder/Matrix.h" 10 | #include "cinder/Vector.h" 11 | #include "cinder/Quaternion.h" 12 | #endif 13 | -------------------------------------------------------------------------------- /Fish/xcode/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | CinderApp.icns 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | ${MACOSX_DEPLOYMENT_TARGET} 27 | NSHumanReadableCopyright 28 | Copyright © 2015 __MyCompanyName__. All rights reserved. 29 | NSMainNibFile 30 | MainMenu 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /GeneticAlgorithm/.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | [Bb]uild/ 5 | [Pp]rojectSettings/ 6 | Papers/ 7 | 8 | # Autogenerated VS/MD solution and project files 9 | *.csproj 10 | *.unityproj 11 | *.sln 12 | *.suo 13 | *.user 14 | *.userprefs 15 | *.pidb 16 | *.booproj 17 | 18 | # Unity3D Generated File On Crash Reports 19 | sysinfo.txt -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/Common.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1eb95e1a54fe34c628f727301fb0ce34 3 | folderAsset: yes 4 | timeCreated: 1493100374 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/Common/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 98f84c12c0a4b4a4c963228a5f101aa3 3 | folderAsset: yes 4 | timeCreated: 1493100679 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/Common/Scripts/FboPingpong.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | [System.Serializable] 5 | public class FboPingpong : System.IDisposable { 6 | 7 | public RenderTexture ReadTex { get { return RTs[readIndex]; } } 8 | public RenderTexture WriteTex { get { return RTs[writeIndex]; } } 9 | 10 | protected int readIndex = 0; 11 | protected int writeIndex = 1; 12 | 13 | [SerializeField] RenderTexture[] RTs; 14 | 15 | public FboPingpong(int width, int height, RenderTextureFormat format = RenderTextureFormat.ARGBFloat, FilterMode filterMode = FilterMode.Point) { 16 | RTs = new RenderTexture[2]; 17 | RTs[0] = CreateRenderTex(width, height); 18 | RTs[1] = CreateRenderTex(width, height); 19 | Clear(); 20 | } 21 | 22 | RenderTexture CreateRenderTex (int width, int height, RenderTextureFormat format = RenderTextureFormat.ARGBFloat, FilterMode filterMode = FilterMode.Point) { 23 | var rt = new RenderTexture(width, height, 0); 24 | rt.format = format; 25 | rt.filterMode = filterMode; 26 | rt.wrapMode = TextureWrapMode.Clamp; 27 | rt.autoGenerateMips = false; 28 | rt.hideFlags = HideFlags.DontSave; 29 | rt.Create(); 30 | return rt; 31 | } 32 | 33 | public void Swap () { 34 | var tmp = readIndex; readIndex = writeIndex; writeIndex = tmp; 35 | } 36 | 37 | public void Clear () { 38 | Graphics.SetRenderTarget(RTs[0]); 39 | GL.Clear(false, true, Color.black); 40 | Graphics.SetRenderTarget(null); 41 | 42 | Graphics.SetRenderTarget(RTs[1]); 43 | GL.Clear(false, true, Color.black); 44 | Graphics.SetRenderTarget (null); 45 | } 46 | 47 | public void Dispose () { 48 | RTs[0].DiscardContents(); 49 | RTs[0].Release(); 50 | 51 | RTs[1].DiscardContents(); 52 | RTs[1].Release(); 53 | } 54 | 55 | } 56 | 57 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/Common/Scripts/FboPingpong.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8eaf41cf5588d421d97eeebbdb6ed5ea 3 | timeCreated: 1493100685 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/Common/Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a3b0cf0a994a3488e9ec79c802b40840 3 | folderAsset: yes 4 | timeCreated: 1492531386 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/Common/Shaders/ClassicNoise3D.cginc: -------------------------------------------------------------------------------- 1 | // 2 | // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader 3 | // 4 | // Original work (webgl-noise) Copyright (C) 2011 Stefan Gustavson 5 | // Translation and modification was made by Keijiro Takahashi. 6 | // 7 | // This shader is based on the webgl-noise GLSL shader. For further details 8 | // of the original shader, please see the following description from the 9 | // original source code. 10 | // 11 | 12 | // 13 | // GLSL textureless classic 3D noise "cnoise", 14 | // with an RSL-style periodic variant "pnoise". 15 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 16 | // Version: 2011-10-11 17 | // 18 | // Many thanks to Ian McEwan of Ashima Arts for the 19 | // ideas for permutation and gradient selection. 20 | // 21 | // Copyright (c) 2011 Stefan Gustavson. All rights reserved. 22 | // Distributed under the MIT license. See LICENSE file. 23 | // https://github.com/ashima/webgl-noise 24 | // 25 | 26 | float3 mod(float3 x, float3 y) 27 | { 28 | return x - y * floor(x / y); 29 | } 30 | 31 | float3 mod289(float3 x) 32 | { 33 | return x - floor(x / 289.0) * 289.0; 34 | } 35 | 36 | float4 mod289(float4 x) 37 | { 38 | return x - floor(x / 289.0) * 289.0; 39 | } 40 | 41 | float4 permute(float4 x) 42 | { 43 | return mod289(((x*34.0)+1.0)*x); 44 | } 45 | 46 | float4 taylorInvSqrt(float4 r) 47 | { 48 | return (float4)1.79284291400159 - r * 0.85373472095314; 49 | } 50 | 51 | float3 fade(float3 t) { 52 | return t*t*t*(t*(t*6.0-15.0)+10.0); 53 | } 54 | 55 | // Classic Perlin noise 56 | float cnoise(float3 P) 57 | { 58 | float3 Pi0 = floor(P); // Integer part for indexing 59 | float3 Pi1 = Pi0 + (float3)1.0; // Integer part + 1 60 | Pi0 = mod289(Pi0); 61 | Pi1 = mod289(Pi1); 62 | float3 Pf0 = frac(P); // Fractional part for interpolation 63 | float3 Pf1 = Pf0 - (float3)1.0; // Fractional part - 1.0 64 | float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 65 | float4 iy = float4(Pi0.y, Pi0.y, Pi1.y, Pi1.y); 66 | float4 iz0 = (float4)Pi0.z; 67 | float4 iz1 = (float4)Pi1.z; 68 | 69 | float4 ixy = permute(permute(ix) + iy); 70 | float4 ixy0 = permute(ixy + iz0); 71 | float4 ixy1 = permute(ixy + iz1); 72 | 73 | float4 gx0 = ixy0 / 7.0; 74 | float4 gy0 = frac(floor(gx0) / 7.0) - 0.5; 75 | gx0 = frac(gx0); 76 | float4 gz0 = (float4)0.5 - abs(gx0) - abs(gy0); 77 | float4 sz0 = step(gz0, (float4)0.0); 78 | gx0 -= sz0 * (step((float4)0.0, gx0) - 0.5); 79 | gy0 -= sz0 * (step((float4)0.0, gy0) - 0.5); 80 | 81 | float4 gx1 = ixy1 / 7.0; 82 | float4 gy1 = frac(floor(gx1) / 7.0) - 0.5; 83 | gx1 = frac(gx1); 84 | float4 gz1 = (float4)0.5 - abs(gx1) - abs(gy1); 85 | float4 sz1 = step(gz1, (float4)0.0); 86 | gx1 -= sz1 * (step((float4)0.0, gx1) - 0.5); 87 | gy1 -= sz1 * (step((float4)0.0, gy1) - 0.5); 88 | 89 | float3 g000 = float3(gx0.x,gy0.x,gz0.x); 90 | float3 g100 = float3(gx0.y,gy0.y,gz0.y); 91 | float3 g010 = float3(gx0.z,gy0.z,gz0.z); 92 | float3 g110 = float3(gx0.w,gy0.w,gz0.w); 93 | float3 g001 = float3(gx1.x,gy1.x,gz1.x); 94 | float3 g101 = float3(gx1.y,gy1.y,gz1.y); 95 | float3 g011 = float3(gx1.z,gy1.z,gz1.z); 96 | float3 g111 = float3(gx1.w,gy1.w,gz1.w); 97 | 98 | float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); 99 | g000 *= norm0.x; 100 | g010 *= norm0.y; 101 | g100 *= norm0.z; 102 | g110 *= norm0.w; 103 | 104 | float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); 105 | g001 *= norm1.x; 106 | g011 *= norm1.y; 107 | g101 *= norm1.z; 108 | g111 *= norm1.w; 109 | 110 | float n000 = dot(g000, Pf0); 111 | float n100 = dot(g100, float3(Pf1.x, Pf0.y, Pf0.z)); 112 | float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z)); 113 | float n110 = dot(g110, float3(Pf1.x, Pf1.y, Pf0.z)); 114 | float n001 = dot(g001, float3(Pf0.x, Pf0.y, Pf1.z)); 115 | float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z)); 116 | float n011 = dot(g011, float3(Pf0.x, Pf1.y, Pf1.z)); 117 | float n111 = dot(g111, Pf1); 118 | 119 | float3 fade_xyz = fade(Pf0); 120 | float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z); 121 | float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y); 122 | float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x); 123 | return 2.2 * n_xyz; 124 | } 125 | 126 | // Classic Perlin noise, periodic variant 127 | float pnoise(float3 P, float3 rep) 128 | { 129 | float3 Pi0 = mod(floor(P), rep); // Integer part, modulo period 130 | float3 Pi1 = mod(Pi0 + (float3)1.0, rep); // Integer part + 1, mod period 131 | Pi0 = mod289(Pi0); 132 | Pi1 = mod289(Pi1); 133 | float3 Pf0 = frac(P); // Fractional part for interpolation 134 | float3 Pf1 = Pf0 - (float3)1.0; // Fractional part - 1.0 135 | float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 136 | float4 iy = float4(Pi0.y, Pi0.y, Pi1.y, Pi1.y); 137 | float4 iz0 = (float4)Pi0.z; 138 | float4 iz1 = (float4)Pi1.z; 139 | 140 | float4 ixy = permute(permute(ix) + iy); 141 | float4 ixy0 = permute(ixy + iz0); 142 | float4 ixy1 = permute(ixy + iz1); 143 | 144 | float4 gx0 = ixy0 / 7.0; 145 | float4 gy0 = frac(floor(gx0) / 7.0) - 0.5; 146 | gx0 = frac(gx0); 147 | float4 gz0 = (float4)0.5 - abs(gx0) - abs(gy0); 148 | float4 sz0 = step(gz0, (float4)0.0); 149 | gx0 -= sz0 * (step((float4)0.0, gx0) - 0.5); 150 | gy0 -= sz0 * (step((float4)0.0, gy0) - 0.5); 151 | 152 | float4 gx1 = ixy1 / 7.0; 153 | float4 gy1 = frac(floor(gx1) / 7.0) - 0.5; 154 | gx1 = frac(gx1); 155 | float4 gz1 = (float4)0.5 - abs(gx1) - abs(gy1); 156 | float4 sz1 = step(gz1, (float4)0.0); 157 | gx1 -= sz1 * (step((float4)0.0, gx1) - 0.5); 158 | gy1 -= sz1 * (step((float4)0.0, gy1) - 0.5); 159 | 160 | float3 g000 = float3(gx0.x,gy0.x,gz0.x); 161 | float3 g100 = float3(gx0.y,gy0.y,gz0.y); 162 | float3 g010 = float3(gx0.z,gy0.z,gz0.z); 163 | float3 g110 = float3(gx0.w,gy0.w,gz0.w); 164 | float3 g001 = float3(gx1.x,gy1.x,gz1.x); 165 | float3 g101 = float3(gx1.y,gy1.y,gz1.y); 166 | float3 g011 = float3(gx1.z,gy1.z,gz1.z); 167 | float3 g111 = float3(gx1.w,gy1.w,gz1.w); 168 | 169 | float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); 170 | g000 *= norm0.x; 171 | g010 *= norm0.y; 172 | g100 *= norm0.z; 173 | g110 *= norm0.w; 174 | float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); 175 | g001 *= norm1.x; 176 | g011 *= norm1.y; 177 | g101 *= norm1.z; 178 | g111 *= norm1.w; 179 | 180 | float n000 = dot(g000, Pf0); 181 | float n100 = dot(g100, float3(Pf1.x, Pf0.y, Pf0.z)); 182 | float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z)); 183 | float n110 = dot(g110, float3(Pf1.x, Pf1.y, Pf0.z)); 184 | float n001 = dot(g001, float3(Pf0.x, Pf0.y, Pf1.z)); 185 | float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z)); 186 | float n011 = dot(g011, float3(Pf0.x, Pf1.y, Pf1.z)); 187 | float n111 = dot(g111, Pf1); 188 | 189 | float3 fade_xyz = fade(Pf0); 190 | float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z); 191 | float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y); 192 | float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x); 193 | return 2.2 * n_xyz; 194 | } 195 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/Common/Shaders/ClassicNoise3D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c93bae1f719584c7ebe5f197adbc5df9 3 | timeCreated: 1455502254 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/Common/Shaders/Random.cginc: -------------------------------------------------------------------------------- 1 | #ifndef RANDOM_INCLUDED 2 | #define RANDOM_INCLUDED 3 | 4 | float nrand(float2 co){ 5 | return frac(sin(dot(co.xy, float2(12.9898, 78.233))) * 43758.5453); 6 | } 7 | 8 | float nrand(float2 uv, float salt) { 9 | uv += float2(salt, 0.0); 10 | return nrand(uv); 11 | } 12 | 13 | float3 nrand3(float2 seed){ 14 | float t = sin(seed.x + seed.y * 1e3); 15 | return float3(frac(t*1e4), frac(t*1e6), frac(t*1e5)); 16 | } 17 | 18 | float3 nrand3(float2 seed, float salt){ 19 | seed += float2(salt, 0.0); 20 | float t = sin(seed.x + seed.y * 1e3); 21 | return float3(frac(t*1e4), frac(t*1e6), frac(t*1e5)); 22 | } 23 | 24 | // Uniformaly distributed points on a unit sphere 25 | // http://mathworld.wolfram.com/SpherePointPicking.html 26 | float3 random_point_on_sphere(float2 uv) { 27 | float u = nrand(uv) * 2 - 1; 28 | float theta = nrand(uv + 0.333) * UNITY_PI * 2; 29 | float u2 = sqrt(1 - u * u); 30 | return float3(u2 * cos(theta), u2 * sin(theta), u); 31 | } 32 | 33 | #endif // RANDOM_INCLUDED 34 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/Common/Shaders/Random.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 14156a3c23ea241cfb1628cf525d8ad4 3 | timeCreated: 1493100413 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/Common/Shaders/SimplexNoise3D.cginc: -------------------------------------------------------------------------------- 1 | // 2 | // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader 3 | // 4 | // Original work (webgl-noise) Copyright (C) 2011 Ashima Arts. 5 | // Translation and modification was made by Keijiro Takahashi. 6 | // 7 | // This shader is based on the webgl-noise GLSL shader. For further details 8 | // of the original shader, please see the following description from the 9 | // original source code. 10 | // 11 | 12 | // 13 | // Description : Array and textureless GLSL 2D/3D/4D simplex 14 | // noise functions. 15 | // Author : Ian McEwan, Ashima Arts. 16 | // Maintainer : ijm 17 | // Lastmod : 20110822 (ijm) 18 | // License : Copyright (C) 2011 Ashima Arts. All rights reserved. 19 | // Distributed under the MIT License. See LICENSE file. 20 | // https://github.com/ashima/webgl-noise 21 | // 22 | 23 | float3 mod289(float3 x) 24 | { 25 | return x - floor(x / 289.0) * 289.0; 26 | } 27 | 28 | float4 mod289(float4 x) 29 | { 30 | return x - floor(x / 289.0) * 289.0; 31 | } 32 | 33 | float4 permute(float4 x) 34 | { 35 | return mod289((x * 34.0 + 1.0) * x); 36 | } 37 | 38 | float4 taylorInvSqrt(float4 r) 39 | { 40 | return 1.79284291400159 - r * 0.85373472095314; 41 | } 42 | 43 | float snoise(float3 v) 44 | { 45 | const float2 C = float2(1.0 / 6.0, 1.0 / 3.0); 46 | 47 | // First corner 48 | float3 i = floor(v + dot(v, C.yyy)); 49 | float3 x0 = v - i + dot(i, C.xxx); 50 | 51 | // Other corners 52 | float3 g = step(x0.yzx, x0.xyz); 53 | float3 l = 1.0 - g; 54 | float3 i1 = min(g.xyz, l.zxy); 55 | float3 i2 = max(g.xyz, l.zxy); 56 | 57 | // x1 = x0 - i1 + 1.0 * C.xxx; 58 | // x2 = x0 - i2 + 2.0 * C.xxx; 59 | // x3 = x0 - 1.0 + 3.0 * C.xxx; 60 | float3 x1 = x0 - i1 + C.xxx; 61 | float3 x2 = x0 - i2 + C.yyy; 62 | float3 x3 = x0 - 0.5; 63 | 64 | // Permutations 65 | i = mod289(i); // Avoid truncation effects in permutation 66 | float4 p = 67 | permute(permute(permute(i.z + float4(0.0, i1.z, i2.z, 1.0)) 68 | + i.y + float4(0.0, i1.y, i2.y, 1.0)) 69 | + i.x + float4(0.0, i1.x, i2.x, 1.0)); 70 | 71 | // Gradients: 7x7 points over a square, mapped onto an octahedron. 72 | // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) 73 | float4 j = p - 49.0 * floor(p / 49.0); // mod(p,7*7) 74 | 75 | float4 x_ = floor(j / 7.0); 76 | float4 y_ = floor(j - 7.0 * x_); // mod(j,N) 77 | 78 | float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0; 79 | float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0; 80 | 81 | float4 h = 1.0 - abs(x) - abs(y); 82 | 83 | float4 b0 = float4(x.xy, y.xy); 84 | float4 b1 = float4(x.zw, y.zw); 85 | 86 | //float4 s0 = float4(lessThan(b0, 0.0)) * 2.0 - 1.0; 87 | //float4 s1 = float4(lessThan(b1, 0.0)) * 2.0 - 1.0; 88 | float4 s0 = floor(b0) * 2.0 + 1.0; 89 | float4 s1 = floor(b1) * 2.0 + 1.0; 90 | float4 sh = -step(h, 0.0); 91 | 92 | float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; 93 | float4 a1 = b1.xzyw + s1.xzyw * sh.zzww; 94 | 95 | float3 g0 = float3(a0.xy, h.x); 96 | float3 g1 = float3(a0.zw, h.y); 97 | float3 g2 = float3(a1.xy, h.z); 98 | float3 g3 = float3(a1.zw, h.w); 99 | 100 | // Normalise gradients 101 | float4 norm = taylorInvSqrt(float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3))); 102 | g0 *= norm.x; 103 | g1 *= norm.y; 104 | g2 *= norm.z; 105 | g3 *= norm.w; 106 | 107 | // Mix final noise value 108 | float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0); 109 | m = m * m; 110 | m = m * m; 111 | 112 | float4 px = float4(dot(x0, g0), dot(x1, g1), dot(x2, g2), dot(x3, g3)); 113 | return 42.0 * dot(m, px); 114 | } 115 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/Common/Shaders/SimplexNoise3D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e19895d8fa6244b9f9c50761cddd8ad5 3 | timeCreated: 1455502254 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c34fa7376e418254d91732f938c54041 3 | folderAsset: yes 4 | timeCreated: 1492594071 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/GeneticAlgorithm.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/GeneticAlgorithm/Assets/GeneticAlgorithm/GeneticAlgorithm.unity -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/GeneticAlgorithm.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 579cded196f9cf846aa874cbe627f0e3 3 | timeCreated: 1492594080 4 | licenseType: Pro 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8b6a34959b1814270a4bbb411709c2f2 3 | folderAsset: yes 4 | timeCreated: 1493046920 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Materials/Quad.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/GeneticAlgorithm/Assets/GeneticAlgorithm/Materials/Quad.mat -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Materials/Quad.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1e005809503c64aabbcef032d751ba65 3 | timeCreated: 1493128295 4 | licenseType: Free 5 | NativeFormatImporter: 6 | mainObjectFileID: 2100000 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Materials/Visualizer.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/GeneticAlgorithm/Assets/GeneticAlgorithm/Materials/Visualizer.mat -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Materials/Visualizer.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 78ae1e407b54348e1b3526200ccf04b0 3 | timeCreated: 1493046945 4 | licenseType: Free 5 | NativeFormatImporter: 6 | mainObjectFileID: 2100000 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: abb389c103be0264bba8767eaa0c4457 3 | folderAsset: yes 4 | timeCreated: 1492594138 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/Creature.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | namespace mattatz.GeneticAlgorithm { 5 | 6 | public abstract class Creature { 7 | 8 | public float Fitness { 9 | get; set; 10 | } 11 | 12 | public float NormalizedFitness 13 | { 14 | get; set; 15 | } 16 | 17 | public DNA DNA { get { return dna; } } 18 | 19 | protected DNA dna; 20 | 21 | public Creature () { } 22 | 23 | public Creature (DNA dna) { 24 | this.dna = dna; 25 | } 26 | 27 | } 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/Creature.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3ae2e15b214198d41aaa6309de4d2232 3 | timeCreated: 1492594340 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/DNA.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using Random = UnityEngine.Random; 3 | 4 | using System.Collections; 5 | using mattatz.Utils; 6 | 7 | namespace mattatz.GeneticAlgorithm { 8 | 9 | public class DNA { 10 | 11 | public float[] genes; 12 | 13 | public DNA (int n, Vector2 range) { 14 | genes = new float[n]; 15 | for(int i = 0; i < n; i++) { 16 | genes[i] = Random.Range(range.x, range.y); 17 | } 18 | } 19 | 20 | public DNA (float[] genes) { 21 | this.genes = genes; 22 | } 23 | 24 | // CROSSOVER 25 | // Creates new DNA sequence from two (this & and a partner) 26 | public DNA Crossover(DNA partner) { 27 | float[] child = new float[genes.Length]; 28 | 29 | int mid = Mathf.FloorToInt(Random.value * genes.Length); 30 | mid = (Mathf.FloorToInt(mid / 2) * 2); 31 | 32 | // Take "half" from one and "half" from the other 33 | for (int i = 0, n = genes.Length; i < n; i++) { 34 | if (i > mid) { 35 | child[i] = genes[i]; 36 | } else { 37 | child[i] = partner.genes[i]; 38 | } 39 | } 40 | 41 | return new DNA(child); 42 | } 43 | 44 | // Based on a mutation probability, picks a new random value 45 | public void Mutate (float m, float s = 1f) { 46 | for (int i = 0, n = genes.Length; i < n; i++) { 47 | if (Random.value < m) { 48 | genes[i] += Gaussian.Std(0f, 0.75f) * s; 49 | } 50 | } 51 | } 52 | 53 | } 54 | 55 | } 56 | 57 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/DNA.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 69f504dc8a60f0648882f75660815e2e 3 | timeCreated: 1492594257 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/Gaussian.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using Random = UnityEngine.Random; 3 | 4 | using System.Collections; 5 | 6 | namespace mattatz.Utils { 7 | 8 | public class Gaussian { 9 | 10 | /* 11 | * standard normal distribution 12 | * https://en.wikipedia.org/wiki/Normal_distribution 13 | */ 14 | public static float Std (float mu = 0f, float sigma = 1f) { 15 | var u1 = Random.value; 16 | var u2 = Random.value; 17 | var rand_std_normal = Mathf.Sqrt(-2.0f * Mathf.Log(u1)) * Mathf.Sin(2.0f * Mathf.PI * u2); 18 | return mu + sigma * rand_std_normal; 19 | } 20 | 21 | } 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/Gaussian.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 40ccd219ae4f8d54aa5fa174c202da0b 3 | timeCreated: 1492594249 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/GeneticAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | using UnityEngine; 6 | 7 | using mattatz.Utils; 8 | using mattatz.GeneticAlgorithm; 9 | 10 | public class GeneticAlgorithm : MonoBehaviour { 11 | 12 | public int Generations { get { return generations; } } 13 | public int Strokes { get { return strokes; } } 14 | public List Nematodes { get { return nematodes; } } 15 | 16 | [SerializeField] float mutationRate = 0.2f; 17 | [SerializeField, Range(0f, 0.2f)] float mutationScale = 0.05f; 18 | [SerializeField] int count = 50; 19 | [SerializeField] int strokes = 20; 20 | int generations = 0; 21 | 22 | [SerializeField] Texture2D source = null; 23 | [SerializeField] Texture2D dst = null; 24 | [SerializeField] int resolution = 32; 25 | [SerializeField] Gradient grad; 26 | 27 | [SerializeField, Range(2, 10)] int frequency = 4; 28 | [SerializeField] bool automatic = true; 29 | [SerializeField] bool showTexture; 30 | 31 | List nematodes; 32 | Color[] pixels; 33 | 34 | public void Setup () { 35 | if(nematodes != null) nematodes.Clear(); 36 | 37 | generations = 0; 38 | 39 | nematodes = new List(); 40 | 41 | for(int i = 0; i < count; i++) { 42 | var painter = new Nematode(strokes); 43 | nematodes.Add(painter); 44 | } 45 | 46 | dst = Compress(source); 47 | pixels = dst.GetPixels(); 48 | 49 | ComputeFitnesses(); 50 | } 51 | 52 | void Awake () { 53 | Setup(); 54 | } 55 | 56 | void Update () { 57 | if(Input.GetKey(KeyCode.E)) { 58 | Evolve(); 59 | } else if(automatic && Time.frameCount % frequency == 0) { 60 | Evolve(); 61 | } 62 | 63 | if(Input.GetKeyDown(KeyCode.Space)) { 64 | Setup(); 65 | } 66 | } 67 | 68 | Texture2D Compress(Texture2D source) 69 | { 70 | var prev = RenderTexture.active; 71 | 72 | var rt = new RenderTexture(resolution, resolution, 0); 73 | var dst = new Texture2D(resolution, resolution); 74 | Graphics.Blit(source, rt); 75 | { 76 | RenderTexture.active = rt; 77 | dst.ReadPixels(new Rect(0, 0, resolution, resolution), 0, 0); 78 | dst.Apply(); 79 | } 80 | 81 | RenderTexture.active = prev; 82 | 83 | rt.Release(); 84 | return dst; 85 | } 86 | 87 | void Evolve() { 88 | generations++; 89 | 90 | nematodes = Reproduction(); 91 | ComputeFitnesses(); 92 | } 93 | 94 | List Selection () { 95 | var pool = new List(); 96 | nematodes.ForEach(c => { 97 | int n = Mathf.FloorToInt(c.NormalizedFitness * 50); 98 | for(int j = 0; j < n; j++) { 99 | pool.Add(c); 100 | } 101 | }); 102 | return pool; 103 | } 104 | 105 | List Reproduction () { 106 | var pool = Selection(); 107 | if(pool.Count <= 0) { 108 | Debug.LogWarning("mating pool is empty."); 109 | } 110 | 111 | var next = new List(); 112 | 113 | for(int i = 0, n = nematodes.Count; i < n; i++) { 114 | int m = Random.Range(0, pool.Count); 115 | int d = Random.Range(0, pool.Count); 116 | 117 | DNA mom = pool[m].DNA; 118 | DNA dad = pool[d].DNA; 119 | 120 | DNA child = mom.Crossover(dad); 121 | child.Mutate(mutationRate, mutationScale); 122 | 123 | next.Add(new Nematode(child)); 124 | } 125 | 126 | return next; 127 | } 128 | 129 | float GetMaxFitness() { 130 | float max = 0f; 131 | nematodes.ForEach(creature => { 132 | var fitness = creature.Fitness; 133 | if(fitness > max) { 134 | max = fitness; 135 | } 136 | }); 137 | return max; 138 | } 139 | 140 | bool Fit(int x, int y) { 141 | if(x < 0 || x >= resolution || y < 0 || y >= resolution) return false; 142 | var color = pixels[y * resolution + x]; 143 | return color.a > 0.5f; 144 | // return color.a < 0.1f; 145 | } 146 | 147 | void ComputeFitnesses() 148 | { 149 | nematodes.ForEach(painter => { ComputeFitness(painter); }); 150 | float maxFitness = GetMaxFitness(); 151 | nematodes.ForEach(painter => 152 | { 153 | painter.NormalizedFitness = painter.Fitness / maxFitness; 154 | }); 155 | } 156 | 157 | void ComputeFitness(Nematode painter) 158 | { 159 | var fitness = new Dictionary(); 160 | var points = painter.GetPoints(resolution); 161 | 162 | const int fineness = 10; 163 | const float step = 1f / fineness; 164 | 165 | for(int i = 1, n = points.Count; i < n; i++) 166 | { 167 | var i0 = i - 1; 168 | var i1 = i; 169 | var i2 = (i + 1) % n; 170 | var i3 = (i + 2) % n; 171 | 172 | for(var t = 0f; t < 1f; t += step) 173 | { 174 | var p0 = Spline.GetPosition(t, points[i0], points[i1], points[i2], points[i3]); 175 | var p1 = Spline.GetPosition(t + step, points[i0], points[i1], points[i2], points[i3]); 176 | var dir = (p1 - p0); 177 | var norm = dir.normalized; 178 | var m = dir.magnitude; 179 | for(var tt = 0f; tt < 1f; tt += step) 180 | { 181 | var p = p0 + norm * m * tt; 182 | int x = Mathf.FloorToInt(p.x); 183 | int y = Mathf.FloorToInt(p.y); 184 | int address = y * resolution + x; 185 | /* 186 | if(Fit(x, y) && !fitness.ContainsKey(address)) { 187 | fitness[address] = true; 188 | } 189 | 190 | */ 191 | bool fit = Fit(x, y); 192 | if(!fitness.ContainsKey(address)) { 193 | fitness[address] = fit; 194 | } else { 195 | fitness[address] &= fit; 196 | } 197 | } 198 | } 199 | } 200 | 201 | // painter.Fitness = (1f * fitness.Values.ToList().FindAll((v) => v).Count) / (points.Count * fineness * fineness); 202 | var values = fitness.Values.ToList(); 203 | painter.Fitness = (1f * values.FindAll((v) => v).Count) / values.Count; 204 | } 205 | 206 | void DrawPainterGizmos (Nematode painter) 207 | { 208 | var points = painter.GetPoints(resolution); 209 | const float step = 0.1f; 210 | 211 | for(int i = 1, n = points.Count; i < n; i++) 212 | { 213 | var i0 = i - 1; 214 | var i1 = i; 215 | var i2 = (i + 1) % n; 216 | var i3 = (i + 2) % n; 217 | 218 | for(var t = 0f; t < 1f; t += step) 219 | { 220 | var p0 = Spline.GetPosition(t, points[i0], points[i1], points[i2], points[i3]); 221 | var p1 = Spline.GetPosition(t + step, points[i0], points[i1], points[i2], points[i3]); 222 | Gizmos.DrawLine(p0, p1); 223 | 224 | /* 225 | var dir = (p1 - p0); 226 | var norm = dir.normalized; 227 | var m = dir.magnitude; 228 | for(var tt = 0f; tt < 1f; tt += step) 229 | { 230 | var p = p0 + norm * m * tt; 231 | int x = Mathf.FloorToInt(p.x); 232 | int y = Mathf.FloorToInt(p.y); 233 | int address = y * resolution + x; 234 | if(Fit(x, y)) { 235 | Gizmos.DrawSphere(p, 0.1f); 236 | } 237 | } 238 | */ 239 | } 240 | } 241 | } 242 | 243 | void OnDrawGizmos () 244 | { 245 | #if UNITY_EDITOR 246 | 247 | UnityEditor.Handles.Label(Vector3.zero, "generations: " + generations.ToString()); 248 | UnityEditor.Handles.Label(new Vector3(0f, -1f, 0f), "populations: " + count); 249 | 250 | #endif 251 | 252 | /* 253 | var l = resolution - 1; 254 | for(int y = 0; y < resolution; y++) 255 | { 256 | for(int x = 0; x < resolution; x++) 257 | { 258 | if(x < l) 259 | { 260 | Gizmos.DrawLine(new Vector3(x, y, 0f), new Vector3(x + 1f, y, 0f)); 261 | } 262 | if(y < l) 263 | { 264 | Gizmos.DrawLine(new Vector3(x, y, 0f), new Vector3(x, y + 1f, 0f)); 265 | } 266 | } 267 | } 268 | */ 269 | 270 | /* 271 | if (nematodes == null) return; 272 | for(int i = 0, n = painters.Count; i < n; i++) { 273 | var painter = painters[i]; 274 | var t = 1f * i / n; 275 | var color = grad.Evaluate(t); 276 | color.a *= Mathf.Lerp(0.5f, 1f, t); 277 | Gizmos.color = color; 278 | DrawPainterGizmos(painter); 279 | } 280 | */ 281 | 282 | if(showTexture) { 283 | Gizmos.DrawGUITexture(new Rect(-0.5f, -0.5f, 1, 1), source); 284 | } 285 | 286 | } 287 | 288 | } 289 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/GeneticAlgorithm.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6f3f3d2d0e0051a4780c27cf81e78d4d 3 | timeCreated: 1492595228 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/Nematode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | using UnityEngine; 6 | using Random = UnityEngine.Random; 7 | 8 | using mattatz.GeneticAlgorithm; 9 | 10 | public class Nematode : Creature { 11 | 12 | List points; 13 | 14 | public Nematode(int count) 15 | { 16 | var genes = new float[count * 2]; 17 | for(int i = 0, n = genes.Length; i < n; i += 2) 18 | { 19 | genes[i] = Random.value; 20 | genes[i + 1] = Random.value; 21 | } 22 | dna = new DNA(genes); 23 | Normalize(); 24 | } 25 | 26 | public Nematode(DNA dna): base(dna) { 27 | Normalize(); 28 | } 29 | 30 | void Normalize() { 31 | for(int i = 0, n = dna.genes.Length; i < n; i++) { 32 | dna.genes[i] = Mathf.Clamp01(dna.genes[i]); 33 | } 34 | } 35 | 36 | public List GetPoints (int resolution) 37 | { 38 | if (points != null) return points; 39 | points = new List(); 40 | var genes = this.dna.genes; 41 | for(int i = 0, n = genes.Length; i < n; i+=2) 42 | { 43 | var x = genes[i]; 44 | var y = genes[i + 1]; 45 | points.Add(new Vector2(x * resolution, y * resolution)); 46 | } 47 | return points; 48 | } 49 | 50 | public Texture2D GetTexture () { 51 | int w = this.dna.genes.Length / 2; 52 | var tex = new Texture2D(w, 1, TextureFormat.RGBAFloat, false); 53 | for(int x = 0; x < w; x++) { 54 | int idx = x * 2; 55 | 56 | // 0.0 ~ 1.0 57 | var nx = this.dna.genes[idx]; 58 | var ny = this.dna.genes[idx + 1]; 59 | tex.SetPixel(x, 0, new Color(nx, ny, NormalizedFitness)); 60 | } 61 | tex.Apply(); 62 | 63 | return tex; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/Nematode.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 23c220d5b2d2f49d093e3de47a906d23 3 | timeCreated: 1493046416 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/Spline.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Spline { 6 | 7 | public static Vector3 GetPosition(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3) 8 | { 9 | var tm1 = t - 1f; 10 | var tm2 = tm1 * tm1; 11 | var t2 = t * t; 12 | 13 | var m1 = 0.5f * (p2 - p0); 14 | var m2 = 0.5f * (p3 - p1); 15 | 16 | return (1f + 2f * t) * tm2 * p1 + t * tm2 * m1 + t2 * (3 - 2f * t) * p2 + t2 * tm1 * m2; 17 | } 18 | 19 | public static Vector2 GetPosition(float t, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3) 20 | { 21 | var tm1 = t - 1f; 22 | var tm2 = tm1 * tm1; 23 | var t2 = t * t; 24 | 25 | var m1 = 0.5f * (p2 - p0); 26 | var m2 = 0.5f * (p3 - p1); 27 | 28 | return (1f + 2f * t) * tm2 * p1 + t * tm2 * m1 + t2 * (3 - 2f * t) * p2 + t2 * tm1 * m2; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/Spline.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 065362af93961ce46a0236b8a98cfbd6 3 | timeCreated: 1492596766 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/Visualizer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | 4 | using UnityEngine; 5 | using UnityEngine.UI; 6 | 7 | [RequireComponent (typeof(MeshFilter), typeof(MeshRenderer))] 8 | public class Visualizer : MonoBehaviour { 9 | 10 | [SerializeField] GeneticAlgorithm ga; 11 | [SerializeField] Text generationsLabel; 12 | 13 | [SerializeField] int count = 64; 14 | [SerializeField] Shader shader; 15 | 16 | [SerializeField] Texture2DArray array; 17 | [SerializeField] FboPingpong lines; 18 | [SerializeField] Gradient grad; 19 | 20 | [SerializeField] Texture2D feedback; 21 | 22 | Material updateMat; 23 | Material visualizeMat; 24 | 25 | enum LinesRenderMode { 26 | Init = 0, 27 | Update = 1, 28 | Birth = 2, 29 | }; 30 | 31 | void Start () { 32 | updateMat = new Material(shader); 33 | visualizeMat = GetComponent().sharedMaterial; 34 | 35 | Build(); 36 | } 37 | 38 | void LateUpdate () { 39 | generationsLabel.text = ga.Generations.ToString(); 40 | 41 | Graphics.Blit(lines.ReadTex, lines.WriteTex, updateMat, (int)LinesRenderMode.Update); 42 | lines.Swap(); 43 | 44 | var prev = RenderTexture.active; 45 | { 46 | RenderTexture.active = lines.ReadTex; 47 | feedback.ReadPixels(new Rect(0.0f, 0.0f, lines.ReadTex.width, lines.ReadTex.height), 0, 0, false); 48 | feedback.Apply(); 49 | 50 | bool flag = false; 51 | for(int x = 0; x < count; x++) { 52 | var line = feedback.GetPixel(x, 0); 53 | if(line.r >= 1f) { 54 | flag = true; 55 | Reset(x); 56 | } 57 | } 58 | if(flag) { 59 | array.Apply(); 60 | } 61 | } 62 | RenderTexture.active = prev; 63 | 64 | Graphics.Blit(lines.ReadTex, lines.WriteTex, updateMat, (int)LinesRenderMode.Birth); 65 | lines.Swap(); 66 | } 67 | 68 | void Reset(int index) { 69 | var cr = ga.Nematodes[index % ga.Nematodes.Count]; 70 | Graphics.CopyTexture(cr.GetTexture(), 0, 0, array, index, 0); 71 | } 72 | 73 | void Build() { 74 | array = new Texture2DArray(ga.Strokes, 1, count, TextureFormat.RGBAFloat, false); 75 | array.Apply(); 76 | 77 | lines = new FboPingpong(count, 2, RenderTextureFormat.ARGBFloat, FilterMode.Point); 78 | Graphics.Blit(null, lines.ReadTex, updateMat, (int)LinesRenderMode.Init); 79 | 80 | feedback = new Texture2D(count, 2, TextureFormat.RGBAFloat, false); 81 | feedback.filterMode = FilterMode.Point; 82 | feedback.Apply(); 83 | 84 | for(int i = 0; i < count; i++) { 85 | var cr = ga.Nematodes[i % ga.Nematodes.Count]; 86 | Graphics.CopyTexture(cr.GetTexture(), 0, 0, array, i, 0); 87 | } 88 | array.Apply(); 89 | 90 | var gradTex = new Texture2D(count, 1); 91 | for(int i = 0; i < count; i++) { 92 | gradTex.SetPixel(i, 0, grad.Evaluate(1f * i / count)); 93 | } 94 | gradTex.Apply(); 95 | 96 | visualizeMat.SetTexture("_Lines", lines.ReadTex); 97 | visualizeMat.SetTexture("_Nematodes", array); 98 | visualizeMat.SetFloat("_Depth", count); 99 | visualizeMat.SetVector("_Strokes", new Vector4(ga.Strokes, 1f / ga.Strokes, (1f / ga.Strokes) * 0.5f, -1f)); 100 | visualizeMat.SetTexture("_Gradient", gradTex); 101 | 102 | var mesh = new Mesh(); 103 | var vertices = new Vector3[count]; 104 | var uv = new Vector2[count]; 105 | var inv = 1f / count; 106 | var hinv = inv * 0.5f; 107 | var indices = new int[count]; 108 | 109 | for(int i = 0; i < count; i++) { 110 | var idx = i; 111 | var t = i * inv + hinv; 112 | vertices[idx] = Random.insideUnitSphere; 113 | uv[idx] = new Vector2(0f, t); 114 | indices[idx] = idx; 115 | } 116 | 117 | mesh.vertices = vertices; 118 | mesh.uv = uv; 119 | mesh.SetIndices(indices, MeshTopology.Points, 0); 120 | mesh.RecalculateBounds(); 121 | 122 | GetComponent().sharedMesh = mesh; 123 | } 124 | 125 | void OnGUI () { 126 | // GUI.DrawTexture(new Rect(10, 10, 100, 100), points); 127 | for(int i = 0, n = array.depth; i < n; i++) { 128 | } 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Scripts/Visualizer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9d33ddcf221ef4c3db7dfc42d02fd254 3 | timeCreated: 1493100335 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: 8 | - ga: {instanceID: 0} 9 | - shader: {fileID: 4800000, guid: 733a7f5af26d844178d266444e6c4a1a, type: 3} 10 | - array: {instanceID: 0} 11 | - lines: {instanceID: 0} 12 | executionOrder: 0 13 | icon: {instanceID: 0} 14 | userData: 15 | assetBundleName: 16 | assetBundleVariant: 17 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f596d62a18bdc48f9bb6dfa3c209ae77 3 | folderAsset: yes 4 | timeCreated: 1493046935 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Shaders/Lines.shader: -------------------------------------------------------------------------------- 1 | Shader "Sketch/Lines" 2 | { 3 | Properties { 4 | _MainTex ("Texture", 2D) = "white" {} 5 | _Speed ("Speed", Float) = 0.01 6 | } 7 | 8 | CGINCLUDE 9 | 10 | #include "UnityCG.cginc" 11 | #include "Assets/Common/Shaders/Random.cginc" 12 | 13 | #pragma vertex vert 14 | 15 | struct appdata 16 | { 17 | float4 vertex : POSITION; 18 | float2 uv : TEXCOORD0; 19 | }; 20 | 21 | struct v2f 22 | { 23 | float2 uv : TEXCOORD0; 24 | float4 vertex : SV_POSITION; 25 | }; 26 | 27 | v2f vert (appdata v) 28 | { 29 | v2f o; 30 | o.vertex = UnityObjectToClipPos(v.vertex); 31 | o.uv = v.uv; 32 | return o; 33 | } 34 | 35 | sampler2D _MainTex; 36 | float _Speed; 37 | 38 | ENDCG 39 | 40 | SubShader 41 | { 42 | Cull Off ZWrite Off ZTest Always 43 | 44 | Pass // init 45 | { 46 | CGPROGRAM 47 | #pragma fragment frag 48 | 49 | float4 frag (v2f IN) : SV_Target 50 | { 51 | float r = nrand(float2(IN.uv.x, 0)); 52 | return float4(0, lerp(0.3, 1.0, abs(r)), 0, 1); 53 | } 54 | ENDCG 55 | } 56 | 57 | Pass // update 58 | { 59 | CGPROGRAM 60 | #pragma fragment frag 61 | 62 | float4 frag (v2f IN) : SV_Target 63 | { 64 | float4 ln = tex2D(_MainTex, IN.uv); 65 | ln.x += max(0.25, ln.y) * _Time.x * _Speed; 66 | return ln; 67 | } 68 | ENDCG 69 | } 70 | 71 | Pass // birth 72 | { 73 | CGPROGRAM 74 | #pragma fragment frag 75 | 76 | float4 frag (v2f IN) : SV_Target 77 | { 78 | float4 ln = tex2D(_MainTex, IN.uv); 79 | if(ln.x >= 1) { 80 | ln.x = 0; 81 | ln.y = lerp(0.3, 1.0, abs(nrand(float2(0, IN.uv.x), _Time.x))); 82 | } 83 | return ln; 84 | } 85 | ENDCG 86 | } 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Shaders/Lines.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 733a7f5af26d844178d266444e6c4a1a 3 | timeCreated: 1493100186 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Shaders/Quad.shader: -------------------------------------------------------------------------------- 1 | Shader "Sketch/Quad" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "white" {} 6 | _Alpha ("Alpha", Range(0.0, 1.0)) = 0.5 7 | } 8 | SubShader 9 | { 10 | Tags { "RenderType"="Opaque" } 11 | LOD 100 12 | 13 | Blend SrcAlpha One 14 | 15 | Pass 16 | { 17 | CGPROGRAM 18 | #pragma vertex vert 19 | #pragma fragment frag 20 | 21 | #include "UnityCG.cginc" 22 | 23 | struct appdata 24 | { 25 | float4 vertex : POSITION; 26 | float2 uv : TEXCOORD0; 27 | }; 28 | 29 | struct v2f 30 | { 31 | float2 uv : TEXCOORD0; 32 | float4 vertex : SV_POSITION; 33 | }; 34 | 35 | sampler2D _MainTex; 36 | float4 _MainTex_ST; 37 | half _Alpha; 38 | 39 | v2f vert (appdata v) 40 | { 41 | v2f o; 42 | o.vertex = UnityObjectToClipPos(v.vertex); 43 | o.uv = TRANSFORM_TEX(v.uv, _MainTex); 44 | return o; 45 | } 46 | 47 | fixed4 frag (v2f IN) : SV_Target 48 | { 49 | fixed4 col = tex2D(_MainTex, IN.uv); 50 | // col.a = 1.0 - col.r; 51 | col.rgb = col.a * _Alpha; 52 | col.a *= _Alpha; 53 | return col; 54 | } 55 | 56 | ENDCG 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Shaders/Quad.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a2b16b2ca52c04ac58668ba0e52f0ab1 3 | timeCreated: 1493128370 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Shaders/Visualizer.shader: -------------------------------------------------------------------------------- 1 | // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 2 | 3 | // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 4 | 5 | Shader "Sketch/Visualizer" 6 | { 7 | Properties 8 | { 9 | _Lines ("Lines", 2D) = "" {} 10 | _Nematodes ("Nematodes", 2DArray) = "" {} 11 | _Depth ("Depth", Float) = 1.0 12 | _Strokes ("Strokes", Vector) = (0, 0, 0, -1) 13 | _Gradient ("Gradient", 2D) = "" {} 14 | } 15 | 16 | SubShader 17 | { 18 | Tags { "RenderType"="Opaque" } 19 | LOD 100 20 | 21 | // Blend SrcAlpha OneMinusSrcAlpha 22 | Blend SrcAlpha One 23 | 24 | Pass 25 | { 26 | CGPROGRAM 27 | 28 | #pragma target 4.0 29 | #pragma vertex vert 30 | #pragma geometry geom 31 | #pragma fragment frag 32 | 33 | #include "UnityCG.cginc" 34 | 35 | struct appdata 36 | { 37 | float4 vertex : POSITION; 38 | float2 uv : TEXCOORD0; 39 | }; 40 | 41 | struct v2g { 42 | float4 pos : SV_POSITION; 43 | float2 uv : TEXCOORD0; 44 | }; 45 | 46 | struct g2f { 47 | float4 pos : SV_POSITION; 48 | float2 uv : TEXCOORD0; 49 | float lifetime : TEXCOORD1; 50 | float fitness : TEXCOORD2; 51 | }; 52 | 53 | sampler2D _Lines; 54 | 55 | UNITY_DECLARE_TEX2DARRAY(_Nematodes); 56 | 57 | float _Depth; 58 | float4 _Strokes; // x = count, y = 1 / count, z = (1 / count) * 0.5 59 | sampler2D _Gradient; 60 | 61 | float3 spline(float t, float3 p0, float3 p1, float3 p2, float3 p3) 62 | { 63 | float tm1 = t - 1.0; 64 | float tm2 = tm1 * tm1; 65 | float t2 = t * t; 66 | 67 | float3 m1 = 0.5 * (p2 - p0); 68 | float3 m2 = 0.5 * (p3 - p1); 69 | 70 | return (1 + 2 * t) * tm2 * p1 + t * tm2 * m1 + t2 * (3 - 2 * t) * p2 + t2 * tm1 * m2; 71 | } 72 | 73 | void fetch(int idx, float depth, out float3 p0, out float3 p1, out float3 p2, out float3 p3) { 74 | p0 = UNITY_SAMPLE_TEX2DARRAY_LOD(_Nematodes, float3(_Strokes.z + _Strokes.y * (idx - 1), 0.5, depth), 0).xyz; 75 | p1 = UNITY_SAMPLE_TEX2DARRAY_LOD(_Nematodes, float3(_Strokes.z + _Strokes.y * idx, 0.5, depth), 0).xyz; 76 | p2 = UNITY_SAMPLE_TEX2DARRAY_LOD(_Nematodes, float3(_Strokes.z + _Strokes.y * (idx + 1), 0.5, depth), 0).xyz; 77 | p3 = UNITY_SAMPLE_TEX2DARRAY_LOD(_Nematodes, float3(_Strokes.z + _Strokes.y * (idx + 2), 0.5, depth), 0).xyz; 78 | } 79 | 80 | void append (inout LineStream stream, float lifetime, float2 uv, float start, float last, float3 p0, float3 p1, float3 p2, float3 p3) { 81 | g2f pIn; 82 | pIn.lifetime = lifetime; 83 | 84 | float t; 85 | float3 p; 86 | 87 | float d = (last - start) * 0.1; 88 | // for(t = start; t < last; t += 0.1) { 89 | for(t = start; t < last; t += d) { 90 | float3 p = spline(t, p0, p1, p2, p3); 91 | p.xy -= 0.5; 92 | 93 | pIn.pos = UnityObjectToClipPos(float4(p, 1)); 94 | pIn.uv = float2(uv.x, uv.y + t * _Strokes.y); 95 | pIn.fitness = p.z; 96 | stream.Append(pIn); 97 | } 98 | 99 | p = spline(last, p0, p1, p2, p3); 100 | p.xy -= 0.5; 101 | 102 | pIn.pos = UnityObjectToClipPos(float4(p, 1)); 103 | pIn.uv = float2(uv.x, uv.y + t * _Strokes.y); 104 | pIn.fitness = p.z; 105 | stream.Append(pIn); 106 | } 107 | 108 | v2g vert (appdata v) 109 | { 110 | v2g OUT; 111 | OUT.pos = float4(0, 0, 0, 1); 112 | OUT.uv = v.uv; 113 | return OUT; 114 | } 115 | 116 | [maxvertexcount(128)] 117 | void geom(point v2g IN[1], inout LineStream stream) { 118 | float3 pos = IN[0].pos.xyz; 119 | float t = IN[0].uv.y; 120 | 121 | float4 ln = tex2Dlod(_Lines, float4(t, 0, 0, 0)); 122 | 123 | g2f pIn; 124 | 125 | float depth = t * _Depth; 126 | 127 | float lifetime = saturate(ln.x); 128 | float len = 0.2 * (smoothstep(0.0, 0.2, lifetime) * smoothstep(0.0, 0.2, 1.0 - lifetime)); 129 | 130 | float interval = (_Strokes.x - 2); 131 | 132 | float last = 1 + interval * lifetime; 133 | float start = last - interval * len; 134 | start = max(1, start); 135 | 136 | int from = floor(start); 137 | float sfr = frac(start); 138 | 139 | int lfl = floor(last); 140 | float lfr = frac(last); 141 | 142 | int i = from; 143 | 144 | float3 p0, p1, p2, p3; 145 | 146 | // first segment 147 | if(lfl > 1) { 148 | fetch(i, depth, p0, p1, p2, p3); 149 | append(stream, lifetime, float2(t, i * _Strokes.y), sfr, 1, p0, p1, p2, p3); 150 | if(from == lfl) { 151 | // first segment only 152 | return; 153 | } 154 | } 155 | 156 | for(i = from + 1; i < lfl; i++) { 157 | fetch(i, depth, p0, p1, p2, p3); 158 | append(stream, lifetime, float2(t, i * _Strokes.y), 0, 1, p0, p1, p2, p3); 159 | } 160 | 161 | // last segment 162 | fetch(lfl, depth, p0, p1, p2, p3); 163 | append(stream, lifetime, float2(t, lfl * _Strokes.y), 0, lfr, p0, p1, p2, p3); 164 | } 165 | 166 | fixed4 frag (g2f IN) : SV_Target 167 | { 168 | float lm = smoothstep(0.0, 0.1, IN.lifetime) * smoothstep(1.0, 0.9, IN.lifetime); 169 | float fitness = lerp(0.5, 1.0, saturate(IN.fitness)); 170 | float4 grad = tex2D(_Gradient, float2(IN.uv.x, 0.5)); 171 | return fixed4(1, 1, 1, lm * fitness) * grad; 172 | } 173 | 174 | ENDCG 175 | } 176 | } 177 | 178 | } 179 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Shaders/Visualizer.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ff053d8bd88a54240bf15f9c8290c427 3 | timeCreated: 1493046929 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Textures.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 58fec7ffab1257347994deef7c9b5aa1 3 | folderAsset: yes 4 | timeCreated: 1492595194 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Textures/arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/GeneticAlgorithm/Assets/GeneticAlgorithm/Textures/arrow.png -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Textures/arrow.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: eba7653fd6f6d45f2b33a2e1df364dfc 3 | timeCreated: 1493137322 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: {} 7 | serializedVersion: 4 8 | mipmaps: 9 | mipMapMode: 0 10 | enableMipMap: 1 11 | sRGBTexture: 1 12 | linearTexture: 0 13 | fadeOut: 0 14 | borderMipMap: 0 15 | mipMapFadeDistanceStart: 1 16 | mipMapFadeDistanceEnd: 3 17 | bumpmap: 18 | convertToNormalMap: 0 19 | externalNormalMap: 0 20 | heightScale: 0.25 21 | normalMapFilter: 0 22 | isReadable: 0 23 | grayScaleToAlpha: 0 24 | generateCubemap: 6 25 | cubemapConvolution: 0 26 | seamlessCubemap: 0 27 | textureFormat: 1 28 | maxTextureSize: 2048 29 | textureSettings: 30 | filterMode: -1 31 | aniso: -1 32 | mipBias: -1 33 | wrapMode: -1 34 | nPOTScale: 1 35 | lightmap: 0 36 | compressionQuality: 50 37 | spriteMode: 0 38 | spriteExtrude: 1 39 | spriteMeshType: 1 40 | alignment: 0 41 | spritePivot: {x: 0.5, y: 0.5} 42 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 43 | spritePixelsToUnits: 100 44 | alphaUsage: 1 45 | alphaIsTransparency: 1 46 | spriteTessellationDetail: -1 47 | textureType: 0 48 | textureShape: 1 49 | maxTextureSizeSet: 0 50 | compressionQualitySet: 0 51 | textureFormatSet: 0 52 | platformSettings: 53 | - buildTarget: DefaultTexturePlatform 54 | maxTextureSize: 2048 55 | textureFormat: -1 56 | textureCompression: 1 57 | compressionQuality: 50 58 | crunchedCompression: 0 59 | allowsAlphaSplitting: 0 60 | overridden: 0 61 | - buildTarget: Standalone 62 | maxTextureSize: 2048 63 | textureFormat: -1 64 | textureCompression: 1 65 | compressionQuality: 50 66 | crunchedCompression: 0 67 | allowsAlphaSplitting: 0 68 | overridden: 0 69 | - buildTarget: WebGL 70 | maxTextureSize: 2048 71 | textureFormat: -1 72 | textureCompression: 1 73 | compressionQuality: 50 74 | crunchedCompression: 0 75 | allowsAlphaSplitting: 0 76 | overridden: 0 77 | spriteSheet: 78 | serializedVersion: 2 79 | sprites: [] 80 | outline: [] 81 | spritePackingTag: 82 | userData: 83 | assetBundleName: 84 | assetBundleVariant: 85 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Textures/circle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/GeneticAlgorithm/Assets/GeneticAlgorithm/Textures/circle.png -------------------------------------------------------------------------------- /GeneticAlgorithm/Assets/GeneticAlgorithm/Textures/circle.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f189039a1f6be48249246b8093a825ec 3 | timeCreated: 1493136723 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: {} 7 | serializedVersion: 4 8 | mipmaps: 9 | mipMapMode: 0 10 | enableMipMap: 1 11 | sRGBTexture: 1 12 | linearTexture: 0 13 | fadeOut: 0 14 | borderMipMap: 0 15 | mipMapFadeDistanceStart: 1 16 | mipMapFadeDistanceEnd: 3 17 | bumpmap: 18 | convertToNormalMap: 0 19 | externalNormalMap: 0 20 | heightScale: 0.25 21 | normalMapFilter: 0 22 | isReadable: 0 23 | grayScaleToAlpha: 0 24 | generateCubemap: 6 25 | cubemapConvolution: 0 26 | seamlessCubemap: 0 27 | textureFormat: 1 28 | maxTextureSize: 2048 29 | textureSettings: 30 | filterMode: -1 31 | aniso: -1 32 | mipBias: -1 33 | wrapMode: -1 34 | nPOTScale: 1 35 | lightmap: 0 36 | compressionQuality: 50 37 | spriteMode: 0 38 | spriteExtrude: 1 39 | spriteMeshType: 1 40 | alignment: 0 41 | spritePivot: {x: 0.5, y: 0.5} 42 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 43 | spritePixelsToUnits: 100 44 | alphaUsage: 1 45 | alphaIsTransparency: 1 46 | spriteTessellationDetail: -1 47 | textureType: 0 48 | textureShape: 1 49 | maxTextureSizeSet: 0 50 | compressionQualitySet: 0 51 | textureFormatSet: 0 52 | platformSettings: 53 | - buildTarget: DefaultTexturePlatform 54 | maxTextureSize: 2048 55 | textureFormat: -1 56 | textureCompression: 1 57 | compressionQuality: 50 58 | crunchedCompression: 0 59 | allowsAlphaSplitting: 0 60 | overridden: 0 61 | - buildTarget: Standalone 62 | maxTextureSize: 2048 63 | textureFormat: -1 64 | textureCompression: 1 65 | compressionQuality: 50 66 | crunchedCompression: 0 67 | allowsAlphaSplitting: 0 68 | overridden: 0 69 | - buildTarget: WebGL 70 | maxTextureSize: 2048 71 | textureFormat: -1 72 | textureCompression: 1 73 | compressionQuality: 50 74 | crunchedCompression: 0 75 | allowsAlphaSplitting: 0 76 | overridden: 0 77 | spriteSheet: 78 | serializedVersion: 2 79 | sprites: [] 80 | outline: [] 81 | spritePackingTag: 82 | userData: 83 | assetBundleName: 84 | assetBundleVariant: 85 | -------------------------------------------------------------------------------- /GeneticAlgorithm/Captures/Capture.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/GeneticAlgorithm/Captures/Capture.gif -------------------------------------------------------------------------------- /GeneticAlgorithm/README.md: -------------------------------------------------------------------------------- 1 | GeneticAlgorithm 2 | ================= 3 | 4 | 遺伝的アルゴリズムをテーマにしたスケッチ 5 | 6 | ![Capture](https://raw.githubusercontent.com/mattatz/InteractiveCoding/master/GeneticAlgorithm/Captures/Capture.gif) 7 | 8 | ## メモ 9 | * 渡された画像の形に沿うようにSpline曲線のハンドルポイントが移動していく 10 | * CPU側で計算したハンドルポイントはTexture2DArrayを使ってGPU側に送信 11 | * Spline曲線の可視化にはgeometry shaderでライン化したpoint meshを利用 12 | 13 | ## Tools 14 | 15 | - Unity 16 | 17 | -------------------------------------------------------------------------------- /LorenzSystem/.gitignore: -------------------------------------------------------------------------------- 1 | # Mac OS X cruft 2 | *.pbxuser 3 | *.mode1v3 4 | *.mode2v3 5 | *.user 6 | project.xcworkspace 7 | xcuserdata/ 8 | *.xccheckout 9 | .DS_Store 10 | 11 | build/ 12 | lib/libcinder*.a 13 | lib/msw/x86/cinder*.lib 14 | lib/msw/x64/cinder*.lib 15 | lib/msw/x86/cinder*.idb 16 | lib/msw/x64/cinder*.idb 17 | lib/msw/x86/cinder*.pdb 18 | lib/msw/x64/cinder*.pdb 19 | lib/winrt/x86/cinder*.lib 20 | lib/winrt/x64/cinder*.lib 21 | 22 | # Windows cruft 23 | *.suo 24 | *.ncb 25 | *.sdf 26 | *.VC.opendb 27 | *.VC.db 28 | *.opensdf 29 | Debug/ 30 | Release/ 31 | Debug_ANGLE/ 32 | Release_ANGLE/ 33 | Debug_Shared/ 34 | Release_Shared/ 35 | ipch/ 36 | **/vc2013/enc_temp_folder/** 37 | 38 | # doxygen generated files 39 | docs/html 40 | docs/xml 41 | docs/doxygen/cinder.tag 42 | docs/*/node_modules 43 | *.pyc 44 | 45 | # Android cruft 46 | *.swp 47 | .idea 48 | lib/android/android-19/armeabi-v7a/libcinder*.a 49 | lib/android/android-19/armeabi/libcinder*.a 50 | lib/android/android-19/mips/libcinder*.a 51 | lib/android/android-19/x86/libcinder*.a 52 | lib/android/android-21/arm64-v8a/libcinder*.a 53 | lib/android/android-21/armeabi-v7a/libcinder*.a 54 | lib/android/android-21/armeabi/libcinder*.a 55 | lib/android/android-21/mips/libcinder*.a 56 | lib/android/android-21/mips64/libcinder*.a 57 | lib/android/android-21/x86/libcinder*.a 58 | lib/android/android-21/x86_64/libcinder*.a 59 | 60 | *.iml 61 | local.properties 62 | proguard-rules.pro 63 | .gradle/ 64 | jniLibs/ 65 | 66 | # cmake generated files 67 | cinderConfig.cmake 68 | 69 | # CinderBlocks 70 | blocks/ 71 | !blocks/[__AppTemplates,Box2D,Cairo,FMOD,LocationManager,MotionManager,OSC,QuickTime,TUIO] 72 | -------------------------------------------------------------------------------- /LorenzSystem/Captures/Capture.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/LorenzSystem/Captures/Capture.gif -------------------------------------------------------------------------------- /LorenzSystem/Captures/Capture2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/LorenzSystem/Captures/Capture2.gif -------------------------------------------------------------------------------- /LorenzSystem/README.md: -------------------------------------------------------------------------------- 1 | LorenzSystem 2 | ================= 3 | 4 | ローレンツシステムをテーマにしたスケッチ 5 | 6 | ![Capture](https://raw.githubusercontent.com/mattatz/InteractiveCoding/master/LorenzSystem/Captures/Capture.gif) 7 | 8 | ![Capture2](https://raw.githubusercontent.com/mattatz/InteractiveCoding/master/LorenzSystem/Captures/Capture2.gif) 9 | 10 | ## メモ 11 | * 初Cinder 12 | * point meshをgeometry shaderを使ってラインに変形 (GeneticAlgorithmのプロジェクトで使ったのと同じテクニック) 13 | * transform feedbackでpointごとに初期位置を更新 (CinderサンプルプロジェクトのParticleSphereGPUを参考) 14 | 15 | ## Tools 16 | 17 | - Cinder 18 | 19 | ## Sources 20 | 21 | - Lorenz system - Wikipedia / https://en.wikipedia.org/wiki/Lorenz_system 22 | - GPU Particle | Lorenz System / http://mattatz.github.io/LorenzSystem/ 23 | 24 | -------------------------------------------------------------------------------- /LorenzSystem/assets/particle.frag: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 gColor; 4 | 5 | out vec4 oColor; 6 | 7 | void main( void ) 8 | { 9 | oColor = gColor; 10 | oColor.a *= 0.7; 11 | } 12 | -------------------------------------------------------------------------------- /LorenzSystem/assets/particle.geom: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | layout(points) in; 4 | layout(line_strip, max_vertices = 64) out; 5 | 6 | in vec4 vColor[]; // Output from vertex shader for each vertex 7 | out vec4 gColor; // Output to fragment shader 8 | 9 | uniform mat4 ciModelViewProjection; 10 | uniform mat4 ciProjectionMatrix; 11 | uniform mat4 ciModelView; 12 | 13 | uniform float uSigma; 14 | uniform float uRho; 15 | uniform float uBeta; 16 | uniform int uIteration; 17 | uniform float uDt; 18 | 19 | void main() 20 | { 21 | gColor = vColor[0]; 22 | 23 | vec4 position = gl_in[0].gl_Position; 24 | for(int i = 0; i < uIteration; i++) { 25 | position.xyz += vec3( 26 | uSigma * (-position.x + position.y), 27 | position.x * (uRho - position.z) - position.y, 28 | position.x * position.y - uBeta * position.z 29 | ) * uDt; 30 | gl_Position = ciModelViewProjection * vec4(position.xyz, 1.0); 31 | EmitVertex(); 32 | } 33 | 34 | EndPrimitive(); 35 | } 36 | -------------------------------------------------------------------------------- /LorenzSystem/assets/particle.vert: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 ciPosition; 4 | in vec4 ciColor; 5 | 6 | out vec4 vColor; 7 | 8 | void main( void ) 9 | { 10 | gl_Position = ciPosition; 11 | vColor = ciColor; 12 | } 13 | -------------------------------------------------------------------------------- /LorenzSystem/assets/particleUpdate.vs: -------------------------------------------------------------------------------- 1 | #version 150 core 2 | 3 | in vec3 iPosition; 4 | in vec4 iColor; 5 | 6 | out vec3 position; 7 | out vec4 color; 8 | 9 | uniform float uSigma; 10 | uniform float uRho; 11 | uniform float uBeta; 12 | uniform float uDt; 13 | 14 | void main() 15 | { 16 | position = iPosition; 17 | position += vec3( 18 | uSigma * (-position.x + position.y), 19 | position.x * (uRho - position.z) - position.y, 20 | position.x * position.y - uBeta * position.z 21 | ) * uDt; 22 | 23 | color = iColor; 24 | } 25 | -------------------------------------------------------------------------------- /LorenzSystem/include/Resources.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cinder/CinderResources.h" 3 | 4 | //#define RES_MY_RES CINDER_RESOURCE( ../resources/, image_name.png, 128, IMAGE ) 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /LorenzSystem/resources/CinderApp.icns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/LorenzSystem/resources/CinderApp.icns -------------------------------------------------------------------------------- /LorenzSystem/src/LorenzSystemApp.cpp: -------------------------------------------------------------------------------- 1 | #include "cinder/app/App.h" 2 | #include "cinder/app/RendererGl.h" 3 | #include "cinder/Rand.h" 4 | #include "cinder/gl/gl.h" 5 | #include "cinder/params/Params.h" 6 | #include "cinder/CameraUi.h" 7 | #include "cinder/Log.h" 8 | 9 | using namespace ci; 10 | using namespace ci::app; 11 | using namespace std; 12 | 13 | struct Particle 14 | { 15 | vec3 pos; 16 | ColorA color; 17 | }; 18 | 19 | const int NUM_PARTICLES = 4000; 20 | 21 | class LorenzSystemApp : public App { 22 | public: 23 | void setup() override; 24 | void update() override; 25 | void draw() override; 26 | 27 | void keyDown(KeyEvent event) override; 28 | void mouseDown(MouseEvent event) override; 29 | void mouseMove(MouseEvent event) override; 30 | void mouseUp(MouseEvent event) override; 31 | void mouseDrag(MouseEvent event) override; 32 | void mouseWheel(MouseEvent event) override; 33 | 34 | params::InterfaceGlRef mParams; 35 | 36 | private: 37 | void setLorentzProps(gl::GlslProgRef prog); 38 | 39 | gl::GlslProgRef mRenderProg; 40 | gl::GlslProgRef mUpdateProg; 41 | 42 | // Descriptions of particle data layout. 43 | gl::VaoRef mAttributes[2]; 44 | 45 | // Buffers holding raw particle data on GPU. 46 | gl::VboRef mParticleBuffer[2]; 47 | 48 | // Current source and destination buffers for transform feedback. 49 | // Source and destination are swapped each frame after update. 50 | std::uint32_t mSourceIndex = 0; 51 | std::uint32_t mDestinationIndex = 1; 52 | 53 | CameraPersp mViewCam; 54 | CameraUi mCamUi; 55 | 56 | float uSigma; 57 | float uRho; 58 | float uBeta; 59 | int uIteration; 60 | float uDt; 61 | 62 | float mWindowScale; 63 | }; 64 | 65 | void prepareSettings(LorenzSystemApp::Settings *settings) 66 | { 67 | // settings->setHighDensityDisplayEnabled(); 68 | settings->setMultiTouchEnabled(false); 69 | settings->setWindowSize(1024, 768); 70 | settings->setFrameRate(45.0f); 71 | 72 | // on iOS we want to make a Window per monitor 73 | #if defined( CINDER_COCOA_TOUCH ) 74 | for(auto display : Display::getDisplays()) 75 | settings->prepareWindow(Window::Format().display(display)); 76 | #endif 77 | } 78 | 79 | void LorenzSystemApp::setup() 80 | { 81 | mWindowScale = getWindowContentScale(); 82 | 83 | uSigma = 10.0f; 84 | uRho = 30.2f; 85 | uBeta = 8.0f / 3.0f; 86 | uIteration = 64; 87 | uDt = 0.004f; 88 | 89 | mParams = params::InterfaceGl::create(getWindow(), "parameters", toPixels(ivec2( 200, 400))); 90 | mParams->addParam("sigma", &uSigma).min(0.5f).max(20.0f).step(0.5f); 91 | mParams->addParam("rho", &uRho).min(10.0f).max(40.0f).step(1.0f); 92 | mParams->addParam("beta", &uBeta).min(0.1f).max(10.0f).step(0.1f); 93 | 94 | mParams->addParam("iteration", &uIteration).min(16).max(128).step(1); 95 | mParams->addParam("dt", &uDt).min(0.0005f).max(0.01f).step(0.0002f); 96 | 97 | vector particles; 98 | particles.assign(NUM_PARTICLES, Particle()); 99 | 100 | vector colors; 101 | colors.push_back(Color(0.6f, 0.62f, 0.9f)); 102 | colors.push_back(Color(0.8f, 0.2f, 0.55f)); 103 | colors.push_back(Color(0.9f, 0.9f, 0.45f)); 104 | 105 | for(int i = 0; i < particles.size(); i++) { 106 | auto &p = particles.at(i); 107 | p.pos = Rand::randVec3() * 10.0f; 108 | // p.color = Color(CM_HSV, lmap(i, 0.0f, particles.size(), 0.0f, 0.66f), 1.0f, 1.0f); 109 | p.color = colors.at(Rand::randInt(colors.size())); 110 | } 111 | 112 | mParticleBuffer[mSourceIndex] = gl::Vbo::create(GL_ARRAY_BUFFER, particles.size() * sizeof(Particle), particles.data(), GL_STATIC_DRAW); 113 | mParticleBuffer[mDestinationIndex] = gl::Vbo::create( GL_ARRAY_BUFFER, particles.size() * sizeof(Particle), nullptr, GL_STATIC_DRAW); 114 | 115 | for( int i = 0; i < 2; ++i ) 116 | { // Describe the particle layout for OpenGL. 117 | mAttributes[i] = gl::Vao::create(); 118 | gl::ScopedVao vao( mAttributes[i] ); 119 | 120 | // Define attributes as offsets into the bound particle buffer 121 | gl::ScopedBuffer buffer( mParticleBuffer[i] ); 122 | gl::enableVertexAttribArray(0); 123 | gl::enableVertexAttribArray(1); 124 | 125 | gl::vertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Particle), (const GLvoid*)offsetof(Particle, pos) ); 126 | gl::vertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Particle), (const GLvoid*)offsetof(Particle, color) ); 127 | } 128 | 129 | // Load our update program. 130 | // Match up our attribute locations with the description we gave. 131 | mRenderProg = gl::GlslProg::create(gl::GlslProg::Format() 132 | .vertex(loadAsset("particle.vert")) 133 | .geometry(loadAsset("particle.geom")) 134 | .fragment(loadAsset("particle.frag"))); 135 | 136 | mUpdateProg = gl::GlslProg::create(gl::GlslProg::Format().vertex(loadAsset("particleUpdate.vs")). feedbackFormat(GL_INTERLEAVED_ATTRIBS).feedbackVaryings({ "position", "color" }).attribLocation("iPosition", 0).attribLocation("iColor", 1)); 137 | 138 | mViewCam.setEyePoint(vec3(0.0f, 0.0f, -20.0f)); 139 | mViewCam.setPerspective(60, getWindowWidth() / getWindowHeight(), 1, 10000); 140 | mViewCam.lookAt(vec3(0)); 141 | mCamUi = CameraUi(&mViewCam); 142 | } 143 | 144 | void LorenzSystemApp::keyDown(KeyEvent event) 145 | { 146 | if (event.getCode() == KeyEvent::KEY_ESCAPE) quit(); 147 | } 148 | 149 | void LorenzSystemApp::mouseDown(MouseEvent event) 150 | { 151 | mCamUi.mouseDown(event); 152 | } 153 | 154 | void LorenzSystemApp::mouseMove(MouseEvent event) 155 | { 156 | } 157 | 158 | void LorenzSystemApp::mouseUp(MouseEvent event) 159 | { 160 | mCamUi.mouseUp(event); 161 | } 162 | 163 | void LorenzSystemApp::mouseDrag(MouseEvent event) 164 | { 165 | Rectf r = Rectf(0, 0, getWindowWidth() * mWindowScale, getWindowHeight() * mWindowScale); 166 | if (r.contains(event.getPos())) { 167 | mCamUi.mouseDrag(event); 168 | } 169 | } 170 | 171 | void LorenzSystemApp::mouseWheel(MouseEvent event) 172 | { 173 | mCamUi.mouseWheel(event); 174 | } 175 | 176 | void LorenzSystemApp::update() 177 | { 178 | gl::ScopedGlslProg prog(mUpdateProg); 179 | setLorentzProps(mUpdateProg); 180 | 181 | gl::ScopedState rasterizer(GL_RASTERIZER_DISCARD, true); // turn off fragment stage 182 | 183 | // Bind the source data (Attributes refer to specific buffers). 184 | gl::ScopedVao source(mAttributes[mSourceIndex]); 185 | 186 | // Bind destination as buffer base. 187 | gl::bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, mParticleBuffer[mDestinationIndex]); 188 | gl::beginTransformFeedback(GL_POINTS); 189 | 190 | // Draw source into destination, performing our vertex transformations. 191 | gl::drawArrays(GL_POINTS, 0, NUM_PARTICLES); 192 | 193 | gl::endTransformFeedback(); 194 | 195 | // Swap source and destination for next loop 196 | std::swap(mSourceIndex, mDestinationIndex); 197 | } 198 | 199 | void LorenzSystemApp::draw() 200 | { 201 | gl::clear(Color(0, 0, 0)); 202 | gl::setMatrices(mViewCam); 203 | gl::viewport(0.0f, 0.0f, getWindowWidth() * mWindowScale, getWindowHeight() * mWindowScale); 204 | // gl::enableDepthRead(); 205 | // gl::enableDepthWrite(); 206 | 207 | gl::ScopedGlslProg render(mRenderProg); 208 | gl::ScopedDepthTest(false); 209 | 210 | gl::ScopedVao vao(mAttributes[mSourceIndex]); 211 | 212 | // vec3 center = vec3(getWindowCenter(), 0.0f); 213 | vec3 center = vec3(0.0f, 0.0f, 0.0f); 214 | mRenderProg->uniform("uIteration", uIteration); 215 | setLorentzProps(mRenderProg); 216 | gl::ScopedLineWidth(3.0f); 217 | gl::ScopedBlend blendScope(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 218 | 219 | auto t = getElapsedSeconds(); 220 | gl::pushModelMatrix(); 221 | gl::translate(center); 222 | gl::scale(vec3(16.2f)); 223 | gl::rotate(t * 0.0025f, vec3(0.1, 1, 0)); 224 | 225 | gl::context()->setDefaultShaderVars(); 226 | 227 | gl::drawArrays(GL_POINTS, 0, NUM_PARTICLES); 228 | gl::popModelMatrix(); 229 | 230 | mParams->draw(); 231 | } 232 | 233 | void LorenzSystemApp::setLorentzProps(gl::GlslProgRef prog) { 234 | prog->uniform("uSigma", uSigma); 235 | prog->uniform("uRho", uRho); 236 | prog->uniform("uBeta", uBeta); 237 | prog->uniform("uDt", uDt); 238 | } 239 | 240 | CINDER_APP(LorenzSystemApp, RendererGl, prepareSettings) 241 | -------------------------------------------------------------------------------- /LorenzSystem/xcode/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | CinderApp.icns 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | ${MACOSX_DEPLOYMENT_TARGET} 27 | NSHumanReadableCopyright 28 | Copyright © 2015 __MyCompanyName__. All rights reserved. 29 | NSMainNibFile 30 | MainMenu 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /LorenzSystem/xcode/LorenzSystem_Prefix.pch: -------------------------------------------------------------------------------- 1 | #if defined( __cplusplus ) 2 | #include "cinder/Cinder.h" 3 | 4 | #include "cinder/app/App.h" 5 | 6 | #include "cinder/gl/gl.h" 7 | 8 | #include "cinder/CinderMath.h" 9 | #include "cinder/Matrix.h" 10 | #include "cinder/Vector.h" 11 | #include "cinder/Quaternion.h" 12 | #endif 13 | -------------------------------------------------------------------------------- /Noise/.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | [Bb]uild/ 5 | [Pp]rojectSettings/ 6 | Papers/ 7 | 8 | # Autogenerated VS/MD solution and project files 9 | *.csproj 10 | *.unityproj 11 | *.sln 12 | *.suo 13 | *.user 14 | *.userprefs 15 | *.pidb 16 | *.booproj 17 | 18 | # Unity3D Generated File On Crash Reports 19 | sysinfo.txt 20 | 21 | -------------------------------------------------------------------------------- /Noise/Assets/Common.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 27de65ce684de4509b111deed3915a1c 3 | folderAsset: yes 4 | timeCreated: 1491981794 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 702a99df42be142a293b8d88dd6cd8d1 3 | folderAsset: yes 4 | timeCreated: 1491981980 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Scripts/FboPingPong.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace Utils { 6 | 7 | public class FboPingPong { 8 | 9 | private int _readTex = 0; 10 | private int _writeTex = 1; 11 | private RenderTexture[] _buffer; 12 | 13 | public FboPingPong (int width_, int height_, FilterMode filterMode = FilterMode.Point, TextureWrapMode wrapMode = TextureWrapMode.Repeat){ 14 | 15 | _readTex = 0; 16 | _writeTex = 1; 17 | 18 | _buffer = new RenderTexture [2]; 19 | 20 | for (int i = 0; i < 2; i++){ 21 | _buffer [i] = new RenderTexture (width_, height_, 0, RenderTextureFormat.ARGBFloat); 22 | _buffer [i].hideFlags = HideFlags.DontSave; 23 | _buffer [i].filterMode = filterMode; 24 | _buffer [i].wrapMode = wrapMode; 25 | _buffer [i].Create (); 26 | } 27 | 28 | Clear (); 29 | } 30 | 31 | public void Swap (){ 32 | int t = _readTex; 33 | _readTex = _writeTex; 34 | _writeTex = t; 35 | } 36 | 37 | public void Clear (){ 38 | for (int i = 0; i < _buffer.Length; i++){ 39 | RenderTexture.active = _buffer [i]; 40 | GL.Clear (false, true, Color.black); 41 | RenderTexture.active = null; 42 | } 43 | } 44 | 45 | public void Delete (){ 46 | if (_buffer != null) { 47 | for (int i = 0; i < _buffer.Length; i++){ 48 | _buffer[i].Release (); 49 | _buffer[i].DiscardContents (); 50 | _buffer[i] = null; 51 | } 52 | } 53 | } 54 | 55 | public RenderTexture GetReadTex (){ 56 | return _buffer [_readTex]; 57 | } 58 | 59 | public RenderTexture GetWriteTex (){ 60 | return _buffer [_writeTex]; 61 | } 62 | 63 | } 64 | 65 | } 66 | 67 | 68 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Scripts/FboPingPong.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7379ea55dd3e141698bd1e91d889964e 3 | timeCreated: 1491982055 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 705fe876b9dfe474390d3803eb5474ba 3 | folderAsset: yes 4 | timeCreated: 1491981802 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/ClassicNoise2D.cginc: -------------------------------------------------------------------------------- 1 | // 2 | // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader 3 | // 4 | // Original work (webgl-noise) Copyright (C) 2011 Stefan Gustavson 5 | // Translation and modification was made by Keijiro Takahashi. 6 | // 7 | // This shader is based on the webgl-noise GLSL shader. For further details 8 | // of the original shader, please see the following description from the 9 | // original source code. 10 | // 11 | 12 | // 13 | // GLSL textureless classic 2D noise "cnoise", 14 | // with an RSL-style periodic variant "pnoise". 15 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 16 | // Version: 2011-08-22 17 | // 18 | // Many thanks to Ian McEwan of Ashima Arts for the 19 | // ideas for permutation and gradient selection. 20 | // 21 | // Copyright (c) 2011 Stefan Gustavson. All rights reserved. 22 | // Distributed under the MIT license. See LICENSE file. 23 | // https://github.com/ashima/webgl-noise 24 | // 25 | 26 | float4 mod(float4 x, float4 y) 27 | { 28 | return x - y * floor(x / y); 29 | } 30 | 31 | float4 mod289(float4 x) 32 | { 33 | return x - floor(x / 289.0) * 289.0; 34 | } 35 | 36 | float4 permute(float4 x) 37 | { 38 | return mod289(((x*34.0)+1.0)*x); 39 | } 40 | 41 | float4 taylorInvSqrt(float4 r) 42 | { 43 | return (float4)1.79284291400159 - r * 0.85373472095314; 44 | } 45 | 46 | float2 fade(float2 t) { 47 | return t*t*t*(t*(t*6.0-15.0)+10.0); 48 | } 49 | 50 | // Classic Perlin noise 51 | float cnoise(float2 P) 52 | { 53 | float4 Pi = floor(P.xyxy) + float4(0.0, 0.0, 1.0, 1.0); 54 | float4 Pf = frac (P.xyxy) - float4(0.0, 0.0, 1.0, 1.0); 55 | Pi = mod289(Pi); // To avoid truncation effects in permutation 56 | float4 ix = Pi.xzxz; 57 | float4 iy = Pi.yyww; 58 | float4 fx = Pf.xzxz; 59 | float4 fy = Pf.yyww; 60 | 61 | float4 i = permute(permute(ix) + iy); 62 | 63 | float4 gx = frac(i / 41.0) * 2.0 - 1.0 ; 64 | float4 gy = abs(gx) - 0.5 ; 65 | float4 tx = floor(gx + 0.5); 66 | gx = gx - tx; 67 | 68 | float2 g00 = float2(gx.x,gy.x); 69 | float2 g10 = float2(gx.y,gy.y); 70 | float2 g01 = float2(gx.z,gy.z); 71 | float2 g11 = float2(gx.w,gy.w); 72 | 73 | float4 norm = taylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11))); 74 | g00 *= norm.x; 75 | g01 *= norm.y; 76 | g10 *= norm.z; 77 | g11 *= norm.w; 78 | 79 | float n00 = dot(g00, float2(fx.x, fy.x)); 80 | float n10 = dot(g10, float2(fx.y, fy.y)); 81 | float n01 = dot(g01, float2(fx.z, fy.z)); 82 | float n11 = dot(g11, float2(fx.w, fy.w)); 83 | 84 | float2 fade_xy = fade(Pf.xy); 85 | float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x); 86 | float n_xy = lerp(n_x.x, n_x.y, fade_xy.y); 87 | return 2.3 * n_xy; 88 | } 89 | 90 | // Classic Perlin noise, periodic variant 91 | float pnoise(float2 P, float2 rep) 92 | { 93 | float4 Pi = floor(P.xyxy) + float4(0.0, 0.0, 1.0, 1.0); 94 | float4 Pf = frac (P.xyxy) - float4(0.0, 0.0, 1.0, 1.0); 95 | Pi = mod(Pi, rep.xyxy); // To create noise with explicit period 96 | Pi = mod289(Pi); // To avoid truncation effects in permutation 97 | float4 ix = Pi.xzxz; 98 | float4 iy = Pi.yyww; 99 | float4 fx = Pf.xzxz; 100 | float4 fy = Pf.yyww; 101 | 102 | float4 i = permute(permute(ix) + iy); 103 | 104 | float4 gx = frac(i / 41.0) * 2.0 - 1.0 ; 105 | float4 gy = abs(gx) - 0.5 ; 106 | float4 tx = floor(gx + 0.5); 107 | gx = gx - tx; 108 | 109 | float2 g00 = float2(gx.x,gy.x); 110 | float2 g10 = float2(gx.y,gy.y); 111 | float2 g01 = float2(gx.z,gy.z); 112 | float2 g11 = float2(gx.w,gy.w); 113 | 114 | float4 norm = taylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11))); 115 | g00 *= norm.x; 116 | g01 *= norm.y; 117 | g10 *= norm.z; 118 | g11 *= norm.w; 119 | 120 | float n00 = dot(g00, float2(fx.x, fy.x)); 121 | float n10 = dot(g10, float2(fx.y, fy.y)); 122 | float n01 = dot(g01, float2(fx.z, fy.z)); 123 | float n11 = dot(g11, float2(fx.w, fy.w)); 124 | 125 | float2 fade_xy = fade(Pf.xy); 126 | float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x); 127 | float n_xy = lerp(n_x.x, n_x.y, fade_xy.y); 128 | return 2.3 * n_xy; 129 | } 130 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/ClassicNoise2D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e1b0b5fd6a4e44923b40d856017562e1 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/ClassicNoise3D.cginc: -------------------------------------------------------------------------------- 1 | // 2 | // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader 3 | // 4 | // Original work (webgl-noise) Copyright (C) 2011 Stefan Gustavson 5 | // Translation and modification was made by Keijiro Takahashi. 6 | // 7 | // This shader is based on the webgl-noise GLSL shader. For further details 8 | // of the original shader, please see the following description from the 9 | // original source code. 10 | // 11 | 12 | // 13 | // GLSL textureless classic 3D noise "cnoise", 14 | // with an RSL-style periodic variant "pnoise". 15 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 16 | // Version: 2011-10-11 17 | // 18 | // Many thanks to Ian McEwan of Ashima Arts for the 19 | // ideas for permutation and gradient selection. 20 | // 21 | // Copyright (c) 2011 Stefan Gustavson. All rights reserved. 22 | // Distributed under the MIT license. See LICENSE file. 23 | // https://github.com/ashima/webgl-noise 24 | // 25 | 26 | float3 mod(float3 x, float3 y) 27 | { 28 | return x - y * floor(x / y); 29 | } 30 | 31 | float3 mod289(float3 x) 32 | { 33 | return x - floor(x / 289.0) * 289.0; 34 | } 35 | 36 | float4 mod289(float4 x) 37 | { 38 | return x - floor(x / 289.0) * 289.0; 39 | } 40 | 41 | float4 permute(float4 x) 42 | { 43 | return mod289(((x*34.0)+1.0)*x); 44 | } 45 | 46 | float4 taylorInvSqrt(float4 r) 47 | { 48 | return (float4)1.79284291400159 - r * 0.85373472095314; 49 | } 50 | 51 | float3 fade(float3 t) { 52 | return t*t*t*(t*(t*6.0-15.0)+10.0); 53 | } 54 | 55 | // Classic Perlin noise 56 | float cnoise(float3 P) 57 | { 58 | float3 Pi0 = floor(P); // Integer part for indexing 59 | float3 Pi1 = Pi0 + (float3)1.0; // Integer part + 1 60 | Pi0 = mod289(Pi0); 61 | Pi1 = mod289(Pi1); 62 | float3 Pf0 = frac(P); // Fractional part for interpolation 63 | float3 Pf1 = Pf0 - (float3)1.0; // Fractional part - 1.0 64 | float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 65 | float4 iy = float4(Pi0.y, Pi0.y, Pi1.y, Pi1.y); 66 | float4 iz0 = (float4)Pi0.z; 67 | float4 iz1 = (float4)Pi1.z; 68 | 69 | float4 ixy = permute(permute(ix) + iy); 70 | float4 ixy0 = permute(ixy + iz0); 71 | float4 ixy1 = permute(ixy + iz1); 72 | 73 | float4 gx0 = ixy0 / 7.0; 74 | float4 gy0 = frac(floor(gx0) / 7.0) - 0.5; 75 | gx0 = frac(gx0); 76 | float4 gz0 = (float4)0.5 - abs(gx0) - abs(gy0); 77 | float4 sz0 = step(gz0, (float4)0.0); 78 | gx0 -= sz0 * (step((float4)0.0, gx0) - 0.5); 79 | gy0 -= sz0 * (step((float4)0.0, gy0) - 0.5); 80 | 81 | float4 gx1 = ixy1 / 7.0; 82 | float4 gy1 = frac(floor(gx1) / 7.0) - 0.5; 83 | gx1 = frac(gx1); 84 | float4 gz1 = (float4)0.5 - abs(gx1) - abs(gy1); 85 | float4 sz1 = step(gz1, (float4)0.0); 86 | gx1 -= sz1 * (step((float4)0.0, gx1) - 0.5); 87 | gy1 -= sz1 * (step((float4)0.0, gy1) - 0.5); 88 | 89 | float3 g000 = float3(gx0.x,gy0.x,gz0.x); 90 | float3 g100 = float3(gx0.y,gy0.y,gz0.y); 91 | float3 g010 = float3(gx0.z,gy0.z,gz0.z); 92 | float3 g110 = float3(gx0.w,gy0.w,gz0.w); 93 | float3 g001 = float3(gx1.x,gy1.x,gz1.x); 94 | float3 g101 = float3(gx1.y,gy1.y,gz1.y); 95 | float3 g011 = float3(gx1.z,gy1.z,gz1.z); 96 | float3 g111 = float3(gx1.w,gy1.w,gz1.w); 97 | 98 | float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); 99 | g000 *= norm0.x; 100 | g010 *= norm0.y; 101 | g100 *= norm0.z; 102 | g110 *= norm0.w; 103 | 104 | float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); 105 | g001 *= norm1.x; 106 | g011 *= norm1.y; 107 | g101 *= norm1.z; 108 | g111 *= norm1.w; 109 | 110 | float n000 = dot(g000, Pf0); 111 | float n100 = dot(g100, float3(Pf1.x, Pf0.y, Pf0.z)); 112 | float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z)); 113 | float n110 = dot(g110, float3(Pf1.x, Pf1.y, Pf0.z)); 114 | float n001 = dot(g001, float3(Pf0.x, Pf0.y, Pf1.z)); 115 | float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z)); 116 | float n011 = dot(g011, float3(Pf0.x, Pf1.y, Pf1.z)); 117 | float n111 = dot(g111, Pf1); 118 | 119 | float3 fade_xyz = fade(Pf0); 120 | float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z); 121 | float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y); 122 | float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x); 123 | return 2.2 * n_xyz; 124 | } 125 | 126 | // Classic Perlin noise, periodic variant 127 | float pnoise(float3 P, float3 rep) 128 | { 129 | float3 Pi0 = mod(floor(P), rep); // Integer part, modulo period 130 | float3 Pi1 = mod(Pi0 + (float3)1.0, rep); // Integer part + 1, mod period 131 | Pi0 = mod289(Pi0); 132 | Pi1 = mod289(Pi1); 133 | float3 Pf0 = frac(P); // Fractional part for interpolation 134 | float3 Pf1 = Pf0 - (float3)1.0; // Fractional part - 1.0 135 | float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 136 | float4 iy = float4(Pi0.y, Pi0.y, Pi1.y, Pi1.y); 137 | float4 iz0 = (float4)Pi0.z; 138 | float4 iz1 = (float4)Pi1.z; 139 | 140 | float4 ixy = permute(permute(ix) + iy); 141 | float4 ixy0 = permute(ixy + iz0); 142 | float4 ixy1 = permute(ixy + iz1); 143 | 144 | float4 gx0 = ixy0 / 7.0; 145 | float4 gy0 = frac(floor(gx0) / 7.0) - 0.5; 146 | gx0 = frac(gx0); 147 | float4 gz0 = (float4)0.5 - abs(gx0) - abs(gy0); 148 | float4 sz0 = step(gz0, (float4)0.0); 149 | gx0 -= sz0 * (step((float4)0.0, gx0) - 0.5); 150 | gy0 -= sz0 * (step((float4)0.0, gy0) - 0.5); 151 | 152 | float4 gx1 = ixy1 / 7.0; 153 | float4 gy1 = frac(floor(gx1) / 7.0) - 0.5; 154 | gx1 = frac(gx1); 155 | float4 gz1 = (float4)0.5 - abs(gx1) - abs(gy1); 156 | float4 sz1 = step(gz1, (float4)0.0); 157 | gx1 -= sz1 * (step((float4)0.0, gx1) - 0.5); 158 | gy1 -= sz1 * (step((float4)0.0, gy1) - 0.5); 159 | 160 | float3 g000 = float3(gx0.x,gy0.x,gz0.x); 161 | float3 g100 = float3(gx0.y,gy0.y,gz0.y); 162 | float3 g010 = float3(gx0.z,gy0.z,gz0.z); 163 | float3 g110 = float3(gx0.w,gy0.w,gz0.w); 164 | float3 g001 = float3(gx1.x,gy1.x,gz1.x); 165 | float3 g101 = float3(gx1.y,gy1.y,gz1.y); 166 | float3 g011 = float3(gx1.z,gy1.z,gz1.z); 167 | float3 g111 = float3(gx1.w,gy1.w,gz1.w); 168 | 169 | float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); 170 | g000 *= norm0.x; 171 | g010 *= norm0.y; 172 | g100 *= norm0.z; 173 | g110 *= norm0.w; 174 | float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); 175 | g001 *= norm1.x; 176 | g011 *= norm1.y; 177 | g101 *= norm1.z; 178 | g111 *= norm1.w; 179 | 180 | float n000 = dot(g000, Pf0); 181 | float n100 = dot(g100, float3(Pf1.x, Pf0.y, Pf0.z)); 182 | float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z)); 183 | float n110 = dot(g110, float3(Pf1.x, Pf1.y, Pf0.z)); 184 | float n001 = dot(g001, float3(Pf0.x, Pf0.y, Pf1.z)); 185 | float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z)); 186 | float n011 = dot(g011, float3(Pf0.x, Pf1.y, Pf1.z)); 187 | float n111 = dot(g111, Pf1); 188 | 189 | float3 fade_xyz = fade(Pf0); 190 | float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z); 191 | float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y); 192 | float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x); 193 | return 2.2 * n_xyz; 194 | } 195 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/ClassicNoise3D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3ba1abd1cb44c47b68fbb577c6c4ba3e 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/NoiseTest.shader: -------------------------------------------------------------------------------- 1 | // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 2 | 3 | Shader "NoiseTest/HLSL/NoiseTest" 4 | { 5 | CGINCLUDE 6 | 7 | #pragma multi_compile CNOISE PNOISE SNOISE SNOISE_AGRAD SNOISE_NGRAD 8 | #pragma multi_compile _ THREED 9 | #pragma multi_compile _ FRACTAL 10 | 11 | #include "UnityCG.cginc" 12 | 13 | #if defined(SNOISE) || defined(SNOISE_NGRAD) 14 | #if defined(THREED) 15 | #include "SimplexNoise3D.cginc" 16 | #else 17 | #include "SimplexNoise2D.cginc" 18 | #endif 19 | #elif defined(SNOISE_AGRAD) 20 | #if defined(THREED) 21 | #include "SimplexNoiseGrad3D.cginc" 22 | #else 23 | #include "SimplexNoiseGrad2D.cginc" 24 | #endif 25 | #else 26 | #if defined(THREED) 27 | #include "ClassicNoise3D.cginc" 28 | #else 29 | #include "ClassicNoise2D.cginc" 30 | #endif 31 | #endif 32 | 33 | v2f_img vert(appdata_base v) 34 | { 35 | v2f_img o; 36 | o.pos = UnityObjectToClipPos(v.vertex); 37 | o.uv = v.texcoord.xy; 38 | return o; 39 | } 40 | 41 | float4 frag(v2f_img i) : SV_Target 42 | { 43 | const float epsilon = 0.0001; 44 | 45 | float2 uv = i.uv * 4.0 + float2(0.2, 1) * _Time.y; 46 | 47 | #if defined(SNOISE_AGRAD) || defined(SNOISE_NGRAD) 48 | #if defined(THREED) 49 | float3 o = 0.5; 50 | #else 51 | float2 o = 0.5; 52 | #endif 53 | #else 54 | float o = 0.5; 55 | #endif 56 | 57 | float s = 1.0; 58 | 59 | #if defined(SNOISE) 60 | float w = 0.25; 61 | #else 62 | float w = 0.5; 63 | #endif 64 | 65 | #ifdef FRACTAL 66 | for (int i = 0; i < 6; i++) 67 | #endif 68 | { 69 | #if defined(THREED) 70 | float3 coord = float3(uv * s, _Time.y); 71 | float3 period = float3(s, s, 1.0) * 2.0; 72 | #else 73 | float2 coord = uv * s; 74 | float2 period = s * 2.0; 75 | #endif 76 | 77 | #if defined(CNOISE) 78 | o += cnoise(coord) * w; 79 | #elif defined(PNOISE) 80 | o += pnoise(coord, period) * w; 81 | #elif defined(SNOISE) 82 | o += snoise(coord) * w; 83 | #elif defined(SNOISE_AGRAD) 84 | o += snoise_grad(coord) * w; 85 | #else // SNOISE_NGRAD 86 | #if defined(THREED) 87 | float v0 = snoise(coord); 88 | float vx = snoise(coord + float3(epsilon, 0, 0)); 89 | float vy = snoise(coord + float3(0, epsilon, 0)); 90 | float vz = snoise(coord + float3(0, 0, epsilon)); 91 | o += w * float3(vx - v0, vy - v0, vz - v0) / epsilon; 92 | #else 93 | float v0 = snoise(coord); 94 | float vx = snoise(coord + float2(epsilon, 0)); 95 | float vy = snoise(coord + float2(0, epsilon)); 96 | o += w * float2(vx - v0, vy - v0) / epsilon; 97 | #endif 98 | #endif 99 | 100 | s *= 2.0; 101 | w *= 0.5; 102 | } 103 | 104 | #if defined(SNOISE_AGRAD) || defined(SNOISE_NGRAD) 105 | #if defined(THREED) 106 | return float4(o, 1); 107 | #else 108 | return float4(o, 1, 1); 109 | #endif 110 | #else 111 | return float4(o, o, o, 1); 112 | #endif 113 | } 114 | 115 | ENDCG 116 | SubShader 117 | { 118 | Pass 119 | { 120 | CGPROGRAM 121 | #pragma target 3.0 122 | #pragma vertex vert 123 | #pragma fragment frag 124 | ENDCG 125 | } 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/NoiseTest.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d5df9882eb4ac420cb9650be25a83b62 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/SimplexNoise2D.cginc: -------------------------------------------------------------------------------- 1 | // 2 | // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader 3 | // 4 | // Original work (webgl-noise) Copyright (C) 2011 Ashima Arts. 5 | // Translation and modification was made by Keijiro Takahashi. 6 | // 7 | // This shader is based on the webgl-noise GLSL shader. For further details 8 | // of the original shader, please see the following description from the 9 | // original source code. 10 | // 11 | 12 | // 13 | // Description : Array and textureless GLSL 2D simplex noise function. 14 | // Author : Ian McEwan, Ashima Arts. 15 | // Maintainer : ijm 16 | // Lastmod : 20110822 (ijm) 17 | // License : Copyright (C) 2011 Ashima Arts. All rights reserved. 18 | // Distributed under the MIT License. See LICENSE file. 19 | // https://github.com/ashima/webgl-noise 20 | // 21 | 22 | float3 mod289(float3 x) 23 | { 24 | return x - floor(x / 289.0) * 289.0; 25 | } 26 | 27 | float2 mod289(float2 x) 28 | { 29 | return x - floor(x / 289.0) * 289.0; 30 | } 31 | 32 | float3 permute(float3 x) 33 | { 34 | return mod289((x * 34.0 + 1.0) * x); 35 | } 36 | 37 | float3 taylorInvSqrt(float3 r) 38 | { 39 | return 1.79284291400159 - 0.85373472095314 * r; 40 | } 41 | 42 | float snoise(float2 v) 43 | { 44 | const float4 C = float4( 0.211324865405187, // (3.0-sqrt(3.0))/6.0 45 | 0.366025403784439, // 0.5*(sqrt(3.0)-1.0) 46 | -0.577350269189626, // -1.0 + 2.0 * C.x 47 | 0.024390243902439); // 1.0 / 41.0 48 | // First corner 49 | float2 i = floor(v + dot(v, C.yy)); 50 | float2 x0 = v - i + dot(i, C.xx); 51 | 52 | // Other corners 53 | float2 i1; 54 | i1.x = step(x0.y, x0.x); 55 | i1.y = 1.0 - i1.x; 56 | 57 | // x1 = x0 - i1 + 1.0 * C.xx; 58 | // x2 = x0 - 1.0 + 2.0 * C.xx; 59 | float2 x1 = x0 + C.xx - i1; 60 | float2 x2 = x0 + C.zz; 61 | 62 | // Permutations 63 | i = mod289(i); // Avoid truncation effects in permutation 64 | float3 p = 65 | permute(permute(i.y + float3(0.0, i1.y, 1.0)) 66 | + i.x + float3(0.0, i1.x, 1.0)); 67 | 68 | float3 m = max(0.5 - float3(dot(x0, x0), dot(x1, x1), dot(x2, x2)), 0.0); 69 | m = m * m; 70 | m = m * m; 71 | 72 | // Gradients: 41 points uniformly over a line, mapped onto a diamond. 73 | // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287) 74 | float3 x = 2.0 * frac(p * C.www) - 1.0; 75 | float3 h = abs(x) - 0.5; 76 | float3 ox = floor(x + 0.5); 77 | float3 a0 = x - ox; 78 | 79 | // Normalise gradients implicitly by scaling m 80 | m *= taylorInvSqrt(a0 * a0 + h * h); 81 | 82 | // Compute final noise value at P 83 | float3 g; 84 | g.x = a0.x * x0.x + h.x * x0.y; 85 | g.y = a0.y * x1.x + h.y * x1.y; 86 | g.z = a0.z * x2.x + h.z * x2.y; 87 | return 130.0 * dot(m, g); 88 | } 89 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/SimplexNoise2D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0a38e0bb6f84203469f3e9cb8545ffce 3 | timeCreated: 1448775185 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/SimplexNoise3D.cginc: -------------------------------------------------------------------------------- 1 | // 2 | // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader 3 | // 4 | // Original work (webgl-noise) Copyright (C) 2011 Ashima Arts. 5 | // Translation and modification was made by Keijiro Takahashi. 6 | // 7 | // This shader is based on the webgl-noise GLSL shader. For further details 8 | // of the original shader, please see the following description from the 9 | // original source code. 10 | // 11 | 12 | // 13 | // Description : Array and textureless GLSL 2D/3D/4D simplex 14 | // noise functions. 15 | // Author : Ian McEwan, Ashima Arts. 16 | // Maintainer : ijm 17 | // Lastmod : 20110822 (ijm) 18 | // License : Copyright (C) 2011 Ashima Arts. All rights reserved. 19 | // Distributed under the MIT License. See LICENSE file. 20 | // https://github.com/ashima/webgl-noise 21 | // 22 | 23 | float3 mod289(float3 x) 24 | { 25 | return x - floor(x / 289.0) * 289.0; 26 | } 27 | 28 | float4 mod289(float4 x) 29 | { 30 | return x - floor(x / 289.0) * 289.0; 31 | } 32 | 33 | float4 permute(float4 x) 34 | { 35 | return mod289((x * 34.0 + 1.0) * x); 36 | } 37 | 38 | float4 taylorInvSqrt(float4 r) 39 | { 40 | return 1.79284291400159 - r * 0.85373472095314; 41 | } 42 | 43 | float snoise(float3 v) 44 | { 45 | const float2 C = float2(1.0 / 6.0, 1.0 / 3.0); 46 | 47 | // First corner 48 | float3 i = floor(v + dot(v, C.yyy)); 49 | float3 x0 = v - i + dot(i, C.xxx); 50 | 51 | // Other corners 52 | float3 g = step(x0.yzx, x0.xyz); 53 | float3 l = 1.0 - g; 54 | float3 i1 = min(g.xyz, l.zxy); 55 | float3 i2 = max(g.xyz, l.zxy); 56 | 57 | // x1 = x0 - i1 + 1.0 * C.xxx; 58 | // x2 = x0 - i2 + 2.0 * C.xxx; 59 | // x3 = x0 - 1.0 + 3.0 * C.xxx; 60 | float3 x1 = x0 - i1 + C.xxx; 61 | float3 x2 = x0 - i2 + C.yyy; 62 | float3 x3 = x0 - 0.5; 63 | 64 | // Permutations 65 | i = mod289(i); // Avoid truncation effects in permutation 66 | float4 p = 67 | permute(permute(permute(i.z + float4(0.0, i1.z, i2.z, 1.0)) 68 | + i.y + float4(0.0, i1.y, i2.y, 1.0)) 69 | + i.x + float4(0.0, i1.x, i2.x, 1.0)); 70 | 71 | // Gradients: 7x7 points over a square, mapped onto an octahedron. 72 | // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) 73 | float4 j = p - 49.0 * floor(p / 49.0); // mod(p,7*7) 74 | 75 | float4 x_ = floor(j / 7.0); 76 | float4 y_ = floor(j - 7.0 * x_); // mod(j,N) 77 | 78 | float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0; 79 | float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0; 80 | 81 | float4 h = 1.0 - abs(x) - abs(y); 82 | 83 | float4 b0 = float4(x.xy, y.xy); 84 | float4 b1 = float4(x.zw, y.zw); 85 | 86 | //float4 s0 = float4(lessThan(b0, 0.0)) * 2.0 - 1.0; 87 | //float4 s1 = float4(lessThan(b1, 0.0)) * 2.0 - 1.0; 88 | float4 s0 = floor(b0) * 2.0 + 1.0; 89 | float4 s1 = floor(b1) * 2.0 + 1.0; 90 | float4 sh = -step(h, 0.0); 91 | 92 | float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; 93 | float4 a1 = b1.xzyw + s1.xzyw * sh.zzww; 94 | 95 | float3 g0 = float3(a0.xy, h.x); 96 | float3 g1 = float3(a0.zw, h.y); 97 | float3 g2 = float3(a1.xy, h.z); 98 | float3 g3 = float3(a1.zw, h.w); 99 | 100 | // Normalise gradients 101 | float4 norm = taylorInvSqrt(float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3))); 102 | g0 *= norm.x; 103 | g1 *= norm.y; 104 | g2 *= norm.z; 105 | g3 *= norm.w; 106 | 107 | // Mix final noise value 108 | float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0); 109 | m = m * m; 110 | m = m * m; 111 | 112 | float4 px = float4(dot(x0, g0), dot(x1, g1), dot(x2, g2), dot(x3, g3)); 113 | return 42.0 * dot(m, px); 114 | } 115 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/SimplexNoise3D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b577a94f58a1398409bfcfe9cd5dbf99 3 | timeCreated: 1448788105 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/SimplexNoiseGrad2D.cginc: -------------------------------------------------------------------------------- 1 | // 2 | // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader 3 | // 4 | // Original work (webgl-noise) Copyright (C) 2011 Ashima Arts. 5 | // Translation and modification was made by Keijiro Takahashi. 6 | // 7 | // This shader is based on the webgl-noise GLSL shader. For further details 8 | // of the original shader, please see the following description from the 9 | // original source code. 10 | // 11 | 12 | // 13 | // Description : Array and textureless GLSL 2D simplex noise function. 14 | // Author : Ian McEwan, Ashima Arts. 15 | // Maintainer : ijm 16 | // Lastmod : 20110822 (ijm) 17 | // License : Copyright (C) 2011 Ashima Arts. All rights reserved. 18 | // Distributed under the MIT License. See LICENSE file. 19 | // https://github.com/ashima/webgl-noise 20 | // 21 | 22 | float3 mod289(float3 x) 23 | { 24 | return x - floor(x / 289.0) * 289.0; 25 | } 26 | 27 | float2 mod289(float2 x) 28 | { 29 | return x - floor(x / 289.0) * 289.0; 30 | } 31 | 32 | float3 permute(float3 x) 33 | { 34 | return mod289((x * 34.0 + 1.0) * x); 35 | } 36 | 37 | float3 taylorInvSqrt(float3 r) 38 | { 39 | return 1.79284291400159 - 0.85373472095314 * r; 40 | } 41 | 42 | float2 snoise_grad(float2 v) 43 | { 44 | const float4 C = float4( 0.211324865405187, // (3.0-sqrt(3.0))/6.0 45 | 0.366025403784439, // 0.5*(sqrt(3.0)-1.0) 46 | -0.577350269189626, // -1.0 + 2.0 * C.x 47 | 0.024390243902439); // 1.0 / 41.0 48 | // First corner 49 | float2 i = floor(v + dot(v, C.yy)); 50 | float2 x0 = v - i + dot(i, C.xx); 51 | 52 | // Other corners 53 | float2 i1; 54 | i1.x = step(x0.y, x0.x); 55 | i1.y = 1.0 - i1.x; 56 | 57 | // x1 = x0 - i1 + 1.0 * C.xx; 58 | // x2 = x0 - 1.0 + 2.0 * C.xx; 59 | float2 x1 = x0 + C.xx - i1; 60 | float2 x2 = x0 + C.zz; 61 | 62 | // Permutations 63 | i = mod289(i); // Avoid truncation effects in permutation 64 | float3 p = 65 | permute(permute(i.y + float3(0.0, i1.y, 1.0)) 66 | + i.x + float3(0.0, i1.x, 1.0)); 67 | 68 | float3 m = max(0.5 - float3(dot(x0, x0), dot(x1, x1), dot(x2, x2)), 0.0); 69 | float3 m2 = m * m; 70 | float3 m3 = m2 * m; 71 | float3 m4 = m2 * m2; 72 | 73 | // Gradients: 41 points uniformly over a line, mapped onto a diamond. 74 | // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287) 75 | float3 x = 2.0 * frac(p * C.www) - 1.0; 76 | float3 h = abs(x) - 0.5; 77 | float3 ox = floor(x + 0.5); 78 | float3 a0 = x - ox; 79 | 80 | // Normalise gradients 81 | float3 norm = taylorInvSqrt(a0 * a0 + h * h); 82 | float2 g0 = float2(a0.x, h.x) * norm.x; 83 | float2 g1 = float2(a0.y, h.y) * norm.y; 84 | float2 g2 = float2(a0.z, h.z) * norm.z; 85 | 86 | // Compute gradient of noise function at P 87 | float2 grad = 88 | -6.0 * m3.x * x0 * dot(x0, g0) + m4.x * g0 + 89 | -6.0 * m3.y * x1 * dot(x1, g1) + m4.y * g1 + 90 | -6.0 * m3.z * x2 * dot(x2, g2) + m4.z * g2; 91 | return 130.0 * grad; 92 | } 93 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/SimplexNoiseGrad2D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 33c8157a2aca37849bc1f21250315a93 3 | timeCreated: 1449292284 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/SimplexNoiseGrad3D.cginc: -------------------------------------------------------------------------------- 1 | // 2 | // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader 3 | // 4 | // Original work (webgl-noise) Copyright (C) 2011 Ashima Arts. 5 | // Translation and modification was made by Keijiro Takahashi. 6 | // 7 | // This shader is based on the webgl-noise GLSL shader. For further details 8 | // of the original shader, please see the following description from the 9 | // original source code. 10 | // 11 | 12 | // 13 | // Description : Array and textureless GLSL 2D/3D/4D simplex 14 | // noise functions. 15 | // Author : Ian McEwan, Ashima Arts. 16 | // Maintainer : ijm 17 | // Lastmod : 20110822 (ijm) 18 | // License : Copyright (C) 2011 Ashima Arts. All rights reserved. 19 | // Distributed under the MIT License. See LICENSE file. 20 | // https://github.com/ashima/webgl-noise 21 | // 22 | 23 | float3 mod289(float3 x) 24 | { 25 | return x - floor(x / 289.0) * 289.0; 26 | } 27 | 28 | float4 mod289(float4 x) 29 | { 30 | return x - floor(x / 289.0) * 289.0; 31 | } 32 | 33 | float4 permute(float4 x) 34 | { 35 | return mod289((x * 34.0 + 1.0) * x); 36 | } 37 | 38 | float4 taylorInvSqrt(float4 r) 39 | { 40 | return 1.79284291400159 - r * 0.85373472095314; 41 | } 42 | 43 | float3 snoise_grad(float3 v) 44 | { 45 | const float2 C = float2(1.0 / 6.0, 1.0 / 3.0); 46 | 47 | // First corner 48 | float3 i = floor(v + dot(v, C.yyy)); 49 | float3 x0 = v - i + dot(i, C.xxx); 50 | 51 | // Other corners 52 | float3 g = step(x0.yzx, x0.xyz); 53 | float3 l = 1.0 - g; 54 | float3 i1 = min(g.xyz, l.zxy); 55 | float3 i2 = max(g.xyz, l.zxy); 56 | 57 | // x1 = x0 - i1 + 1.0 * C.xxx; 58 | // x2 = x0 - i2 + 2.0 * C.xxx; 59 | // x3 = x0 - 1.0 + 3.0 * C.xxx; 60 | float3 x1 = x0 - i1 + C.xxx; 61 | float3 x2 = x0 - i2 + C.yyy; 62 | float3 x3 = x0 - 0.5; 63 | 64 | // Permutations 65 | i = mod289(i); // Avoid truncation effects in permutation 66 | float4 p = 67 | permute(permute(permute(i.z + float4(0.0, i1.z, i2.z, 1.0)) 68 | + i.y + float4(0.0, i1.y, i2.y, 1.0)) 69 | + i.x + float4(0.0, i1.x, i2.x, 1.0)); 70 | 71 | // Gradients: 7x7 points over a square, mapped onto an octahedron. 72 | // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) 73 | float4 j = p - 49.0 * floor(p / 49.0); // mod(p,7*7) 74 | 75 | float4 x_ = floor(j / 7.0); 76 | float4 y_ = floor(j - 7.0 * x_); // mod(j,N) 77 | 78 | float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0; 79 | float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0; 80 | 81 | float4 h = 1.0 - abs(x) - abs(y); 82 | 83 | float4 b0 = float4(x.xy, y.xy); 84 | float4 b1 = float4(x.zw, y.zw); 85 | 86 | //float4 s0 = float4(lessThan(b0, 0.0)) * 2.0 - 1.0; 87 | //float4 s1 = float4(lessThan(b1, 0.0)) * 2.0 - 1.0; 88 | float4 s0 = floor(b0) * 2.0 + 1.0; 89 | float4 s1 = floor(b1) * 2.0 + 1.0; 90 | float4 sh = -step(h, 0.0); 91 | 92 | float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; 93 | float4 a1 = b1.xzyw + s1.xzyw * sh.zzww; 94 | 95 | float3 g0 = float3(a0.xy, h.x); 96 | float3 g1 = float3(a0.zw, h.y); 97 | float3 g2 = float3(a1.xy, h.z); 98 | float3 g3 = float3(a1.zw, h.w); 99 | 100 | // Normalise gradients 101 | float4 norm = taylorInvSqrt(float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3))); 102 | g0 *= norm.x; 103 | g1 *= norm.y; 104 | g2 *= norm.z; 105 | g3 *= norm.w; 106 | 107 | // Compute gradient of noise function at P 108 | float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0); 109 | float4 m2 = m * m; 110 | float4 m3 = m2 * m; 111 | float4 m4 = m2 * m2; 112 | float3 grad = 113 | -6.0 * m3.x * x0 * dot(x0, g0) + m4.x * g0 + 114 | -6.0 * m3.y * x1 * dot(x1, g1) + m4.y * g1 + 115 | -6.0 * m3.z * x2 * dot(x2, g2) + m4.z * g2 + 116 | -6.0 * m3.w * x3 * dot(x3, g3) + m4.w * g3; 117 | return 42.0 * grad; 118 | } 119 | -------------------------------------------------------------------------------- /Noise/Assets/Common/Shaders/SimplexNoiseGrad3D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a7111eafc2463224faacf7be3bddb9a4 3 | timeCreated: 1449292284 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Noise.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 31b1c1bce05164765aa82a9d05230472 3 | folderAsset: yes 4 | timeCreated: 1491981794 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Noise.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Noise/Assets/Noise/Noise.unity -------------------------------------------------------------------------------- /Noise/Assets/Noise/Noise.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2442d37cd1a334739a5fb3f8c2914835 3 | timeCreated: 1491981842 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 63017dfd651be4487b46fc7dc79e7dba 3 | folderAsset: yes 4 | timeCreated: 1491981808 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Scripts/Noise.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | namespace InteractiveCoding { 6 | 7 | using Utils; 8 | 9 | [RequireComponent (typeof(Camera))] 10 | public class Noise : MonoBehaviour { 11 | 12 | public enum Pass { 13 | Displace = 0, 14 | Visualize = 1 15 | }; 16 | 17 | protected Texture2D source { 18 | get { 19 | return sources[current % sources.Count]; 20 | } 21 | } 22 | 23 | [SerializeField] List sources; 24 | [SerializeField] Cubemap map; 25 | [SerializeField] protected Shader shader; 26 | [SerializeField] Vector3 lightDir; 27 | 28 | [SerializeField] Material m; 29 | 30 | protected int current = 0; 31 | FboPingPong fbo; 32 | 33 | void Start () { 34 | CheckInit(); 35 | } 36 | 37 | void Update () { 38 | lightDir.x = Mathf.Sin(Time.timeSinceLevelLoad) * 0.5f; 39 | lightDir.y = Mathf.Cos(Time.timeSinceLevelLoad * 0.25f); 40 | m.SetVector("_LightDirection", lightDir); 41 | Graphics.Blit(fbo.GetReadTex(), fbo.GetWriteTex(), m, (int)Pass.Displace); 42 | fbo.Swap(); 43 | } 44 | 45 | void OnRenderImage(RenderTexture src, RenderTexture dst) { 46 | CheckInit(); 47 | 48 | var ratio = (1f * Screen.width) / Screen.height; 49 | m.SetFloat("_Ratio", ratio); 50 | Graphics.Blit(fbo.GetReadTex(), dst, m, (int)Pass.Visualize); 51 | } 52 | 53 | void CheckInit () { 54 | if(m == null) { 55 | m = new Material(shader); 56 | m.SetTexture("_OriginTex", source); 57 | m.SetTexture("_CubeMap", map); 58 | lightDir = m.GetVector("_LightDirection"); 59 | } 60 | 61 | if(fbo == null) { 62 | fbo = new FboPingPong(Screen.width, Screen.height, FilterMode.Trilinear, TextureWrapMode.Repeat); 63 | Graphics.Blit(source, fbo.GetReadTex()); 64 | } 65 | } 66 | 67 | public void Pressure () { 68 | current++; 69 | m.SetTexture("_OriginTex", source); 70 | StartCoroutine(IPressure(3f)); 71 | } 72 | 73 | IEnumerator IPressure (float duration) { 74 | float time = 0f; 75 | while(time < duration) { 76 | yield return 0; 77 | 78 | time += Time.deltaTime; 79 | m.SetFloat("_Beta", 0.05f); 80 | } 81 | 82 | m.SetFloat("_Beta", 0f); 83 | } 84 | 85 | void OnDestroy () { 86 | Destroy(m); 87 | } 88 | 89 | } 90 | 91 | } 92 | 93 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Scripts/Noise.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b7fd61115a4234668aa65dca8c2a7b3a 3 | timeCreated: 1491990049 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: 8 | - source: {fileID: 2800000, guid: 1aaedb6f678dd497faf812f705737906, type: 3} 9 | - map: {fileID: 8900000, guid: 9baa88d4df9444851944b0b8f490caba, type: 3} 10 | - shader: {fileID: 4800000, guid: 4f1c3b831dafe42b8a1355a8f247d504, type: 3} 11 | - m: {instanceID: 0} 12 | executionOrder: 0 13 | icon: {instanceID: 0} 14 | userData: 15 | assetBundleName: 16 | assetBundleVariant: 17 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b25abaf6ec438408fa2f0f7bbb69d474 3 | folderAsset: yes 4 | timeCreated: 1491981812 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Shaders/Sketch.shader: -------------------------------------------------------------------------------- 1 | Shader "InteractiveCoding/Sketch" { 2 | 3 | Properties 4 | { 5 | _Color ("Color", Color) = (0.5, 0.5, 0.5, 1) 6 | _MainTex ("Texture", 2D) = "bump" {} 7 | _OriginTex ("Origin", 2D) = "bump" {} 8 | _CubeMap ("Cubemap", Cube) = "" {} 9 | 10 | _Alpha ("Alpha", Range(0.1, 0.725)) = 0.5 11 | _Beta ("Beta", Range(0.0, 0.1)) = 0.0 12 | 13 | _Intensity ("Intensity", Range(0.0, 1.0)) = 0.75 14 | _Speed ("Speed", Float) = 0.5 15 | _Scale ("Scale", Float) = 16.5 16 | 17 | _SpecularColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0.2) 18 | _SpecularPower ("Specular", Range(1.0, 20.0)) = 16.3 19 | _LightDirection ("Light Direction", Vector) = (0.51, 0.38, 0.97, -1.0) 20 | 21 | _Ratio ("Ratio", Float) = 1.0 22 | } 23 | 24 | CGINCLUDE 25 | 26 | #include "UnityCG.cginc" 27 | // #include "Assets/Common/Shaders/SimplexNoise3D.cginc" 28 | #include "Assets/Common/Shaders/ClassicNoise3D.cginc" 29 | 30 | struct appdata 31 | { 32 | float4 vertex : POSITION; 33 | float2 uv : TEXCOORD0; 34 | }; 35 | 36 | struct v2f 37 | { 38 | float2 uv : TEXCOORD0; 39 | float4 vertex : SV_POSITION; 40 | }; 41 | 42 | sampler2D _MainTex, _OriginTex; 43 | float4 _MainTex_TexelSize; 44 | 45 | float _Intensity, _Speed, _Scale; 46 | 47 | v2f vert (appdata v) 48 | { 49 | v2f o; 50 | o.vertex = UnityObjectToClipPos(v.vertex); 51 | o.uv = v.uv; 52 | return o; 53 | } 54 | 55 | float2 noise(float2 uv, float time) { 56 | float t = time * _Speed; 57 | float2 seed = uv * _Scale; 58 | // float nx = snoise(float3(seed, t)); 59 | // float ny = snoise(float3(t + 1.3, seed)); 60 | float nx = cnoise(float3(seed, t)); 61 | float ny = cnoise(float3(t + 1.3, seed)); 62 | return float2(nx, ny) * _Intensity; 63 | } 64 | 65 | ENDCG 66 | 67 | SubShader { 68 | Cull Off ZWrite Off ZTest Always 69 | 70 | // displace 71 | Pass 72 | { 73 | CGPROGRAM 74 | 75 | #pragma vertex vert 76 | #pragma fragment displace 77 | 78 | float _Alpha, _Beta; 79 | 80 | float4 displace (v2f i) : SV_Target 81 | { 82 | float2 uv = i.uv + noise(i.uv, _Time.y) * _MainTex_TexelSize.xy; 83 | float4 org = tex2D(_OriginTex, i.uv); 84 | float4 src = tex2D(_MainTex, i.uv); 85 | float4 dst = tex2D(_MainTex, uv); 86 | return lerp(lerp(src, dst, _Alpha), org, _Beta); 87 | } 88 | 89 | ENDCG 90 | } 91 | 92 | // visualize 93 | Pass 94 | { 95 | CGPROGRAM 96 | 97 | #pragma vertex vert 98 | #pragma fragment frag 99 | 100 | float4 _Color, _SpecularColor; 101 | float _SpecularPower; 102 | float3 _LightDirection; 103 | samplerCUBE _CubeMap; 104 | 105 | float _Ratio; 106 | 107 | float4 frag (v2f i) : SV_Target 108 | { 109 | float2 uv = i.uv; 110 | uv.x *= _Ratio; 111 | uv.x += (1.0 - _Ratio) * 0.5; 112 | 113 | float3 normal = UnpackNormal(tex2D (_MainTex, uv)); 114 | float diff = dot(normalize(_LightDirection), normal); 115 | float p = pow(saturate(diff), _SpecularPower); 116 | 117 | float3 viewDir = float3(0, 0, -1); 118 | float3 refl = reflect(viewDir, normal); 119 | float4 col = _Color * texCUBE(_CubeMap, refl); 120 | col += p * _SpecularColor; 121 | 122 | // return float4(diff, diff, diff, 1.0); 123 | // return float4(p, p, p, 1.0); 124 | // return float4((normal + 1.0) * 0.5, 1.0); 125 | return col; 126 | } 127 | 128 | ENDCG 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Shaders/Sketch.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4f1c3b831dafe42b8a1355a8f247d504 3 | timeCreated: 1491982452 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Textures.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a493ddfe9314d4748a00434ab8d2a1f9 3 | folderAsset: yes 4 | timeCreated: 1491982313 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Textures/IMG_5469.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Noise/Assets/Noise/Textures/IMG_5469.JPG -------------------------------------------------------------------------------- /Noise/Assets/Noise/Textures/IMG_5469.JPG.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e102b639857e642a0bdfabffc4c9d7bf 3 | timeCreated: 1491983096 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: {} 7 | serializedVersion: 4 8 | mipmaps: 9 | mipMapMode: 0 10 | enableMipMap: 1 11 | sRGBTexture: 1 12 | linearTexture: 0 13 | fadeOut: 0 14 | borderMipMap: 0 15 | mipMapFadeDistanceStart: 1 16 | mipMapFadeDistanceEnd: 3 17 | bumpmap: 18 | convertToNormalMap: 0 19 | externalNormalMap: 0 20 | heightScale: 0.25 21 | normalMapFilter: 0 22 | isReadable: 0 23 | grayScaleToAlpha: 0 24 | generateCubemap: 6 25 | cubemapConvolution: 0 26 | seamlessCubemap: 0 27 | textureFormat: 1 28 | maxTextureSize: 2048 29 | textureSettings: 30 | filterMode: -1 31 | aniso: -1 32 | mipBias: -1 33 | wrapMode: -1 34 | nPOTScale: 1 35 | lightmap: 0 36 | compressionQuality: 50 37 | spriteMode: 0 38 | spriteExtrude: 1 39 | spriteMeshType: 1 40 | alignment: 0 41 | spritePivot: {x: 0.5, y: 0.5} 42 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 43 | spritePixelsToUnits: 100 44 | alphaUsage: 1 45 | alphaIsTransparency: 0 46 | spriteTessellationDetail: -1 47 | textureType: 0 48 | textureShape: 1 49 | maxTextureSizeSet: 0 50 | compressionQualitySet: 0 51 | textureFormatSet: 0 52 | platformSettings: 53 | - buildTarget: DefaultTexturePlatform 54 | maxTextureSize: 2048 55 | textureFormat: -1 56 | textureCompression: 1 57 | compressionQuality: 50 58 | crunchedCompression: 0 59 | allowsAlphaSplitting: 0 60 | overridden: 0 61 | spriteSheet: 62 | serializedVersion: 2 63 | sprites: [] 64 | outline: [] 65 | spritePackingTag: 66 | userData: 67 | assetBundleName: 68 | assetBundleVariant: 69 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Textures/ellipse.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Noise/Assets/Noise/Textures/ellipse.jpg -------------------------------------------------------------------------------- /Noise/Assets/Noise/Textures/ellipse.jpg.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 02c151ceba55b43fba2fefde78839360 3 | timeCreated: 1491998491 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: {} 7 | serializedVersion: 4 8 | mipmaps: 9 | mipMapMode: 0 10 | enableMipMap: 0 11 | sRGBTexture: 0 12 | linearTexture: 0 13 | fadeOut: 0 14 | borderMipMap: 0 15 | mipMapFadeDistanceStart: 1 16 | mipMapFadeDistanceEnd: 3 17 | bumpmap: 18 | convertToNormalMap: 0 19 | externalNormalMap: 0 20 | heightScale: 0.25 21 | normalMapFilter: 0 22 | isReadable: 0 23 | grayScaleToAlpha: 0 24 | generateCubemap: 6 25 | cubemapConvolution: 0 26 | seamlessCubemap: 0 27 | textureFormat: 1 28 | maxTextureSize: 2048 29 | textureSettings: 30 | filterMode: -1 31 | aniso: -1 32 | mipBias: -1 33 | wrapMode: -1 34 | nPOTScale: 1 35 | lightmap: 0 36 | compressionQuality: 50 37 | spriteMode: 0 38 | spriteExtrude: 1 39 | spriteMeshType: 1 40 | alignment: 0 41 | spritePivot: {x: 0.5, y: 0.5} 42 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 43 | spritePixelsToUnits: 100 44 | alphaUsage: 1 45 | alphaIsTransparency: 0 46 | spriteTessellationDetail: -1 47 | textureType: 1 48 | textureShape: 1 49 | maxTextureSizeSet: 0 50 | compressionQualitySet: 0 51 | textureFormatSet: 0 52 | platformSettings: 53 | - buildTarget: DefaultTexturePlatform 54 | maxTextureSize: 2048 55 | textureFormat: -1 56 | textureCompression: 1 57 | compressionQuality: 50 58 | crunchedCompression: 0 59 | allowsAlphaSplitting: 0 60 | overridden: 0 61 | - buildTarget: Standalone 62 | maxTextureSize: 2048 63 | textureFormat: -1 64 | textureCompression: 1 65 | compressionQuality: 50 66 | crunchedCompression: 0 67 | allowsAlphaSplitting: 0 68 | overridden: 0 69 | - buildTarget: WebGL 70 | maxTextureSize: 2048 71 | textureFormat: -1 72 | textureCompression: 1 73 | compressionQuality: 50 74 | crunchedCompression: 0 75 | allowsAlphaSplitting: 0 76 | overridden: 0 77 | spriteSheet: 78 | serializedVersion: 2 79 | sprites: [] 80 | outline: [] 81 | spritePackingTag: 82 | userData: 83 | assetBundleName: 84 | assetBundleVariant: 85 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Textures/skybox.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Noise/Assets/Noise/Textures/skybox.jpg -------------------------------------------------------------------------------- /Noise/Assets/Noise/Textures/skybox.jpg.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9baa88d4df9444851944b0b8f490caba 3 | timeCreated: 1491990006 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: 7 | 8900000: generatedCubemap 8 | serializedVersion: 4 9 | mipmaps: 10 | mipMapMode: 0 11 | enableMipMap: 1 12 | sRGBTexture: 1 13 | linearTexture: 0 14 | fadeOut: 0 15 | borderMipMap: 0 16 | mipMapFadeDistanceStart: 1 17 | mipMapFadeDistanceEnd: 3 18 | bumpmap: 19 | convertToNormalMap: 0 20 | externalNormalMap: 0 21 | heightScale: 0.25 22 | normalMapFilter: 0 23 | isReadable: 0 24 | grayScaleToAlpha: 0 25 | generateCubemap: 6 26 | cubemapConvolution: 0 27 | seamlessCubemap: 0 28 | textureFormat: 1 29 | maxTextureSize: 2048 30 | textureSettings: 31 | filterMode: -1 32 | aniso: -1 33 | mipBias: -1 34 | wrapMode: 1 35 | nPOTScale: 1 36 | lightmap: 0 37 | compressionQuality: 50 38 | spriteMode: 0 39 | spriteExtrude: 1 40 | spriteMeshType: 1 41 | alignment: 0 42 | spritePivot: {x: 0.5, y: 0.5} 43 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 44 | spritePixelsToUnits: 100 45 | alphaUsage: 0 46 | alphaIsTransparency: 0 47 | spriteTessellationDetail: -1 48 | textureType: 0 49 | textureShape: 2 50 | maxTextureSizeSet: 0 51 | compressionQualitySet: 0 52 | textureFormatSet: 0 53 | platformSettings: 54 | - buildTarget: DefaultTexturePlatform 55 | maxTextureSize: 2048 56 | textureFormat: -1 57 | textureCompression: 1 58 | compressionQuality: 50 59 | crunchedCompression: 0 60 | allowsAlphaSplitting: 0 61 | overridden: 0 62 | - buildTarget: Standalone 63 | maxTextureSize: 2048 64 | textureFormat: -1 65 | textureCompression: 1 66 | compressionQuality: 50 67 | crunchedCompression: 0 68 | allowsAlphaSplitting: 0 69 | overridden: 0 70 | - buildTarget: WebGL 71 | maxTextureSize: 2048 72 | textureFormat: -1 73 | textureCompression: 1 74 | compressionQuality: 50 75 | crunchedCompression: 0 76 | allowsAlphaSplitting: 0 77 | overridden: 0 78 | spriteSheet: 79 | serializedVersion: 2 80 | sprites: [] 81 | outline: [] 82 | spritePackingTag: 83 | userData: 84 | assetBundleName: 85 | assetBundleVariant: 86 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Textures/square.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Noise/Assets/Noise/Textures/square.png -------------------------------------------------------------------------------- /Noise/Assets/Noise/Textures/square.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e4b20a1b54b424b5eae3ca24c47508a0 3 | timeCreated: 1491998493 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: {} 7 | serializedVersion: 4 8 | mipmaps: 9 | mipMapMode: 0 10 | enableMipMap: 0 11 | sRGBTexture: 0 12 | linearTexture: 0 13 | fadeOut: 0 14 | borderMipMap: 0 15 | mipMapFadeDistanceStart: 1 16 | mipMapFadeDistanceEnd: 3 17 | bumpmap: 18 | convertToNormalMap: 0 19 | externalNormalMap: 0 20 | heightScale: 0.25 21 | normalMapFilter: 0 22 | isReadable: 0 23 | grayScaleToAlpha: 0 24 | generateCubemap: 6 25 | cubemapConvolution: 0 26 | seamlessCubemap: 0 27 | textureFormat: 1 28 | maxTextureSize: 2048 29 | textureSettings: 30 | filterMode: -1 31 | aniso: -1 32 | mipBias: -1 33 | wrapMode: -1 34 | nPOTScale: 1 35 | lightmap: 0 36 | compressionQuality: 50 37 | spriteMode: 0 38 | spriteExtrude: 1 39 | spriteMeshType: 1 40 | alignment: 0 41 | spritePivot: {x: 0.5, y: 0.5} 42 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 43 | spritePixelsToUnits: 100 44 | alphaUsage: 1 45 | alphaIsTransparency: 0 46 | spriteTessellationDetail: -1 47 | textureType: 1 48 | textureShape: 1 49 | maxTextureSizeSet: 0 50 | compressionQualitySet: 0 51 | textureFormatSet: 0 52 | platformSettings: 53 | - buildTarget: DefaultTexturePlatform 54 | maxTextureSize: 2048 55 | textureFormat: -1 56 | textureCompression: 1 57 | compressionQuality: 50 58 | crunchedCompression: 0 59 | allowsAlphaSplitting: 0 60 | overridden: 0 61 | - buildTarget: Standalone 62 | maxTextureSize: 2048 63 | textureFormat: -1 64 | textureCompression: 1 65 | compressionQuality: 50 66 | crunchedCompression: 0 67 | allowsAlphaSplitting: 0 68 | overridden: 0 69 | - buildTarget: WebGL 70 | maxTextureSize: 2048 71 | textureFormat: -1 72 | textureCompression: 1 73 | compressionQuality: 50 74 | crunchedCompression: 0 75 | allowsAlphaSplitting: 0 76 | overridden: 0 77 | spriteSheet: 78 | serializedVersion: 2 79 | sprites: [] 80 | outline: [] 81 | spritePackingTag: 82 | userData: 83 | assetBundleName: 84 | assetBundleVariant: 85 | -------------------------------------------------------------------------------- /Noise/Assets/Noise/Textures/voronoi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Noise/Assets/Noise/Textures/voronoi.png -------------------------------------------------------------------------------- /Noise/Assets/Noise/Textures/voronoi.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: db517d0920ec948cab4c15bf47e50bf1 3 | timeCreated: 1491999624 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: {} 7 | serializedVersion: 4 8 | mipmaps: 9 | mipMapMode: 0 10 | enableMipMap: 0 11 | sRGBTexture: 0 12 | linearTexture: 0 13 | fadeOut: 0 14 | borderMipMap: 0 15 | mipMapFadeDistanceStart: 1 16 | mipMapFadeDistanceEnd: 3 17 | bumpmap: 18 | convertToNormalMap: 0 19 | externalNormalMap: 0 20 | heightScale: 0.25 21 | normalMapFilter: 0 22 | isReadable: 0 23 | grayScaleToAlpha: 0 24 | generateCubemap: 6 25 | cubemapConvolution: 0 26 | seamlessCubemap: 0 27 | textureFormat: 1 28 | maxTextureSize: 2048 29 | textureSettings: 30 | filterMode: -1 31 | aniso: -1 32 | mipBias: -1 33 | wrapMode: -1 34 | nPOTScale: 1 35 | lightmap: 0 36 | compressionQuality: 50 37 | spriteMode: 0 38 | spriteExtrude: 1 39 | spriteMeshType: 1 40 | alignment: 0 41 | spritePivot: {x: 0.5, y: 0.5} 42 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 43 | spritePixelsToUnits: 100 44 | alphaUsage: 1 45 | alphaIsTransparency: 0 46 | spriteTessellationDetail: -1 47 | textureType: 1 48 | textureShape: 1 49 | maxTextureSizeSet: 0 50 | compressionQualitySet: 0 51 | textureFormatSet: 0 52 | platformSettings: 53 | - buildTarget: DefaultTexturePlatform 54 | maxTextureSize: 2048 55 | textureFormat: -1 56 | textureCompression: 1 57 | compressionQuality: 50 58 | crunchedCompression: 0 59 | allowsAlphaSplitting: 0 60 | overridden: 0 61 | - buildTarget: Standalone 62 | maxTextureSize: 2048 63 | textureFormat: -1 64 | textureCompression: 1 65 | compressionQuality: 50 66 | crunchedCompression: 0 67 | allowsAlphaSplitting: 0 68 | overridden: 0 69 | - buildTarget: WebGL 70 | maxTextureSize: 2048 71 | textureFormat: -1 72 | textureCompression: 1 73 | compressionQuality: 50 74 | crunchedCompression: 0 75 | allowsAlphaSplitting: 0 76 | overridden: 0 77 | spriteSheet: 78 | serializedVersion: 2 79 | sprites: [] 80 | outline: [] 81 | spritePackingTag: 82 | userData: 83 | assetBundleName: 84 | assetBundleVariant: 85 | -------------------------------------------------------------------------------- /Noise/Captures/Capture.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/Noise/Captures/Capture.gif -------------------------------------------------------------------------------- /Noise/README.md: -------------------------------------------------------------------------------- 1 | Noise 2 | ================= 3 | 4 | ノイズをテーマにしたスケッチ 5 | 6 | BumpMapをノイズでグニャグニャさせる 7 | 8 | ![Capture](https://raw.githubusercontent.com/mattatz/InteractiveCoding/master/Noise/Captures/Capture.gif) 9 | 10 | ## Tools 11 | 12 | - Unity 13 | 14 | 15 | ## Sources 16 | 17 | - keijiro / NoiseShader - https://github.com/keijiro/NoiseShader 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | InteractiveCoding 2 | ================= 3 | 4 | [インタラクティブコーディング勉強会](https://www.facebook.com/groups/1478118689119745)と社内でやってるビジュアルコーディング勉強会のスケッチ 5 | 6 | -------------------------------------------------------------------------------- /THREE_D/README.md: -------------------------------------------------------------------------------- 1 | 3D 2 | ================= 3 | 4 | 3Dのサンプルスケッチ. 5 | マウスかキーをクリックするとジオメトリが変わっていきます. 6 | 7 | ![capture](https://raw.githubusercontent.com/mattatz/InteractiveCoding/master/THREE_D/data/capture.gif) 8 | 9 | ProcessingでShaderを使いたいと思い, 10 | [PShaderのチュートリアル記事](https://processing.org/tutorials/pshader/)を参考にしながら作りました. 11 | 12 | # 実装メモ 13 | * メッシュ(PShape)をProcessingで作ろうと思ったのですが,時間がなかったのでobjファイルを[ネット](http://graphics.stanford.edu/hackliszt/meshes/sphere.obj)から拝借しました. 14 | * vertex shader(sample.vert)では,4次元ベクトルをシードとして用いる[simplex noiseのアルゴリズム](https://github.com/ashima/webgl-noise/blob/master/src/noise4D.glsl)を使いました.このノイズの値に応じて頂点を法線方向に動かしてグニャグニャさせています. 15 | * グニャグニャの状態から遷移する形はモデル座標をインプットとして計算を行い,球型を箱型にしたり,花のようなジオメトリになるように調整しました.(box(vec3)関数やflower(vec3)関数) 16 | * ジオメトリをvertex shaderでグニャグニャにしたりすると法線をモデルからそのまま使うのは面倒になるので,法線はfragment shader(sample.frag)で求めるようにしています.これにより,いくらジオメトリを崩壊させても,パリッとした法線を得ることができます. 17 | * 色は法線ベクトルの値を元に,2色を適当に補完して求めています. 18 | 19 | ## Tools 20 | 21 | - Processing 22 | 23 | -------------------------------------------------------------------------------- /THREE_D/THREE_D.pde: -------------------------------------------------------------------------------- 1 | 2 | PShape shp; 3 | PShader shr; 4 | 5 | float noiseIntensity = 0.42f; 6 | float noiseScale = 0.91f; 7 | float noiseSpeed = 0.2f; 8 | float shininess = 20.0f; 9 | 10 | float interpolation = 0.0f; 11 | int shapeMode = 2; 12 | 13 | Ticker ticker; 14 | 15 | boolean mutation = false; 16 | 17 | void setup() { 18 | size(640, 640, P3D); 19 | 20 | shp = loadShape("sphere.obj"); // http://graphics.stanford.edu/hackliszt/meshes/sphere.obj 21 | shr = loadShader("sample.frag", "sample.vert"); 22 | 23 | ticker = new Ticker(); 24 | } 25 | 26 | void draw() { 27 | background(0); 28 | 29 | interpolation = ticker.getT(); 30 | 31 | shr.set("uTime", frameCount * 0.1f); 32 | shr.set("uNoiseIntensity", noiseIntensity); 33 | shr.set("uNoiseScale", noiseScale); 34 | shr.set("uNoiseSpeed", noiseSpeed); 35 | shr.set("uShininess", shininess); 36 | shr.set("uInterpolation", interpolation); 37 | shr.set("uShapeMode", shapeMode); 38 | 39 | shader(shr); 40 | 41 | float dirY = (mouseY / float(height) - 0.5) * 2; 42 | float dirX = (mouseX / float(width) - 0.5) * 2; 43 | directionalLight(204, 204, 204, -dirX, -dirY, -1); 44 | 45 | translate(width / 2f, height / 2f); 46 | rotateX(frameCount * 0.01f); 47 | rotateY(frameCount * 0.015f); 48 | 49 | scale(120f, 120f, 120f); 50 | 51 | shape(shp); 52 | 53 | ticker.update(0.03f); 54 | } 55 | 56 | void keyPressed() { 57 | mutate(); 58 | } 59 | 60 | void mouseClicked() { 61 | mutate(); 62 | } 63 | 64 | void mutate() { 65 | mutation = !mutation; 66 | if(mutation) { 67 | ticker.start(3f, 0f, 1f); 68 | shapeMode++; 69 | } else { 70 | ticker.start(3f, 1f, 0f); 71 | } 72 | } -------------------------------------------------------------------------------- /THREE_D/Ticker.pde: -------------------------------------------------------------------------------- 1 | 2 | class Ticker { 3 | 4 | float ticker = 0f; 5 | float duration = 0f; 6 | 7 | float from = 0f; 8 | float to = 1f; 9 | 10 | public void start(float duration, float from, float to) { 11 | this.ticker = 0f; 12 | this.duration = duration; 13 | this.from = from; 14 | this.to = to; 15 | } 16 | 17 | public void update(float dt) { 18 | ticker += dt; 19 | ticker = min(ticker, duration); 20 | } 21 | 22 | public float getT() { 23 | if(duration <= 0f) return 0f; 24 | return lerp(from, to, ticker / duration); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /THREE_D/data/capture.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattatz/InteractiveCoding/c5fe0f0b7ee4baf3f735b6bd5af52c796a9f25a5/THREE_D/data/capture.gif -------------------------------------------------------------------------------- /THREE_D/data/sample.frag: -------------------------------------------------------------------------------- 1 | #ifdef GL_ES 2 | precision mediump float; 3 | precision mediump int; 4 | #endif 5 | 6 | #define PROCESSING_LIGHT_SHADER 7 | 8 | uniform float uShininess; 9 | // uniform vec3 uColor0; 10 | // uniform vec3 uColor1; 11 | 12 | varying vec3 vPosition; 13 | varying vec3 vViewPosition; 14 | varying vec3 vLightDir; 15 | 16 | const vec3 lightDir0 = vec3(0.1, 0.3, 0.5); 17 | const vec3 lightDir1 = vec3(-0.8, -1.0, 1.5); 18 | const vec3 ambient = vec3(0.1); 19 | 20 | const vec3 color0 = vec3(13.0 / 255.0, 163.0 / 255.0, 222.0 / 255.0); 21 | const vec3 color1 = vec3(222.0 / 255.0, 107.0 / 255.0, 13.0 / 255.0); 22 | 23 | float specular(vec3 lightDir, vec3 viewDir, vec3 norm, float shininess) { 24 | vec3 halfDir = normalize(lightDir + viewDir); 25 | return pow(max(0.0, dot(halfDir, norm)), shininess); 26 | } 27 | 28 | void main() { 29 | 30 | vec3 dx = dFdx(vPosition); 31 | vec3 dy = dFdy(vPosition); 32 | vec3 normal = normalize(cross(normalize(dx), normalize(dy))); 33 | 34 | vec3 viewDir = normalize(vViewPosition); 35 | 36 | float spec = 0.0; 37 | spec += specular(normalize(vLightDir), viewDir, normal, uShininess); 38 | // spec += specular(normalize(lightDir0), viewDir, normal, uShininess); 39 | // spec += specular(normalize(lightDir1), viewDir, normal, uShininess); 40 | 41 | vec4 color = vec4(vec3(spec) + ambient, 1.0); 42 | color.rgb *= mix(color0, color1, length(normal.xy)); 43 | gl_FragColor = color; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /THREE_D/data/sample.vert: -------------------------------------------------------------------------------- 1 | 2 | uniform mat4 modelview; 3 | uniform mat4 transform; 4 | uniform mat3 normalMatrix; 5 | uniform vec3 lightNormal; 6 | 7 | uniform float uTime; 8 | 9 | uniform float uNoiseIntensity; 10 | uniform float uNoiseScale; 11 | uniform float uNoiseSpeed; 12 | 13 | uniform float uInterpolation; 14 | uniform int uShapeMode; 15 | 16 | attribute vec4 vertex; 17 | attribute vec4 color; 18 | attribute vec3 normal; 19 | 20 | varying vec3 vPosition; 21 | varying vec3 vViewPosition; 22 | varying vec3 vLightDir; 23 | 24 | // 25 | // Description : Array and textureless GLSL 2D/3D/4D simplex 26 | // noise functions. 27 | // Author : Ian McEwan, Ashima Arts. 28 | // Maintainer : ijm 29 | // Lastmod : 20110822 (ijm) 30 | // License : Copyright (C) 2011 Ashima Arts. All rights reserved. 31 | // Distributed under the MIT License. See LICENSE file. 32 | // https://github.com/ashima/webgl-noise 33 | // 34 | 35 | vec4 mod289(vec4 x) { 36 | return x - floor(x * (1.0 / 289.0)) * 289.0; } 37 | 38 | float mod289(float x) { 39 | return x - floor(x * (1.0 / 289.0)) * 289.0; } 40 | 41 | vec4 permute(vec4 x) { 42 | return mod289(((x*34.0)+1.0)*x); 43 | } 44 | 45 | float permute(float x) { 46 | return mod289(((x*34.0)+1.0)*x); 47 | } 48 | 49 | vec4 taylorInvSqrt(vec4 r) 50 | { 51 | return 1.79284291400159 - 0.85373472095314 * r; 52 | } 53 | 54 | float taylorInvSqrt(float r) 55 | { 56 | return 1.79284291400159 - 0.85373472095314 * r; 57 | } 58 | 59 | vec4 grad4(float j, vec4 ip) 60 | { 61 | const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0); 62 | vec4 p,s; 63 | 64 | p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0; 65 | p.w = 1.5 - dot(abs(p.xyz), ones.xyz); 66 | s = vec4(lessThan(p, vec4(0.0))); 67 | p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www; 68 | 69 | return p; 70 | } 71 | 72 | // (sqrt(5) - 1)/4 = F4, used once below 73 | #define F4 0.309016994374947451 74 | 75 | float snoise(vec4 v) 76 | { 77 | const vec4 C = vec4( 0.138196601125011, // (5 - sqrt(5))/20 G4 78 | 0.276393202250021, // 2 * G4 79 | 0.414589803375032, // 3 * G4 80 | -0.447213595499958); // -1 + 4 * G4 81 | 82 | // First corner 83 | vec4 i = floor(v + dot(v, vec4(F4)) ); 84 | vec4 x0 = v - i + dot(i, C.xxxx); 85 | 86 | // Other corners 87 | 88 | // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI) 89 | vec4 i0; 90 | vec3 isX = step( x0.yzw, x0.xxx ); 91 | vec3 isYZ = step( x0.zww, x0.yyz ); 92 | // i0.x = dot( isX, vec3( 1.0 ) ); 93 | i0.x = isX.x + isX.y + isX.z; 94 | i0.yzw = 1.0 - isX; 95 | // i0.y += dot( isYZ.xy, vec2( 1.0 ) ); 96 | i0.y += isYZ.x + isYZ.y; 97 | i0.zw += 1.0 - isYZ.xy; 98 | i0.z += isYZ.z; 99 | i0.w += 1.0 - isYZ.z; 100 | 101 | // i0 now contains the unique values 0,1,2,3 in each channel 102 | vec4 i3 = clamp( i0, 0.0, 1.0 ); 103 | vec4 i2 = clamp( i0-1.0, 0.0, 1.0 ); 104 | vec4 i1 = clamp( i0-2.0, 0.0, 1.0 ); 105 | 106 | // x0 = x0 - 0.0 + 0.0 * C.xxxx 107 | // x1 = x0 - i1 + 1.0 * C.xxxx 108 | // x2 = x0 - i2 + 2.0 * C.xxxx 109 | // x3 = x0 - i3 + 3.0 * C.xxxx 110 | // x4 = x0 - 1.0 + 4.0 * C.xxxx 111 | vec4 x1 = x0 - i1 + C.xxxx; 112 | vec4 x2 = x0 - i2 + C.yyyy; 113 | vec4 x3 = x0 - i3 + C.zzzz; 114 | vec4 x4 = x0 + C.wwww; 115 | 116 | // Permutations 117 | i = mod289(i); 118 | float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x); 119 | vec4 j1 = permute( permute( permute( permute ( 120 | i.w + vec4(i1.w, i2.w, i3.w, 1.0 )) 121 | + i.z + vec4(i1.z, i2.z, i3.z, 1.0 )) 122 | + i.y + vec4(i1.y, i2.y, i3.y, 1.0 )) 123 | + i.x + vec4(i1.x, i2.x, i3.x, 1.0 )); 124 | 125 | // Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope 126 | // 7*7*6 = 294, which is close to the ring size 17*17 = 289. 127 | vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ; 128 | 129 | vec4 p0 = grad4(j0, ip); 130 | vec4 p1 = grad4(j1.x, ip); 131 | vec4 p2 = grad4(j1.y, ip); 132 | vec4 p3 = grad4(j1.z, ip); 133 | vec4 p4 = grad4(j1.w, ip); 134 | 135 | // Normalise gradients 136 | vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3))); 137 | p0 *= norm.x; 138 | p1 *= norm.y; 139 | p2 *= norm.z; 140 | p3 *= norm.w; 141 | p4 *= taylorInvSqrt(dot(p4,p4)); 142 | 143 | // Mix contributions from the five corners 144 | vec3 m0 = max(0.6 - vec3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0); 145 | vec2 m1 = max(0.6 - vec2(dot(x3,x3), dot(x4,x4) ), 0.0); 146 | m0 = m0 * m0; 147 | m1 = m1 * m1; 148 | return 49.0 * ( dot(m0*m0, vec3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 ))) 149 | + dot(m1*m1, vec2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ; 150 | } 151 | 152 | /* 153 | * shape functions 154 | */ 155 | 156 | const int shapes = 3; 157 | 158 | vec3 flower(vec3 pos) { 159 | vec3 p = pos; 160 | p.xy = normalize(pos.xy) * p.z; 161 | p.xz = normalize(pos.xz) * p.y; 162 | p.yz = normalize(pos.yz) * p.x; 163 | p *= 4.0; 164 | return p; 165 | } 166 | 167 | const vec3 boxSize = vec3(1.0); 168 | vec3 box(vec3 pos) { 169 | vec3 p = pos; 170 | vec3 n = normalize(pos); 171 | p += n * 2.0; 172 | return max(- boxSize, min(p, boxSize)); 173 | } 174 | 175 | vec3 ring(vec3 pos) { 176 | return normalize(pos) * length(pos.xz); 177 | } 178 | 179 | void main() { 180 | vec4 position = vertex; 181 | 182 | vec3 p0 = position.xyz + normal * snoise(vec4(vertex.xyz * uNoiseScale, uTime * uNoiseSpeed)) * uNoiseIntensity; 183 | 184 | vec3 p1; 185 | 186 | int shape = uShapeMode % shapes; 187 | if(shape == 0) { 188 | p1 = box(position.xyz); 189 | } else if(shape == 1) { 190 | p1 = flower(position.xyz); 191 | } else if(shape == 2) { 192 | p1 = ring(position.xyz); 193 | } 194 | 195 | position.xyz = mix(p0, p1, uInterpolation); 196 | 197 | gl_Position = transform * position; 198 | 199 | vPosition = gl_Position.xyz; 200 | vViewPosition = - (modelview * position).xyz; 201 | 202 | vLightDir = -lightNormal; 203 | } 204 | 205 | --------------------------------------------------------------------------------