├── ProjectSettings ├── ProjectVersion.txt ├── TagManager.asset ├── TimeManager.asset ├── AudioManager.asset ├── EditorSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NetworkManager.asset ├── DynamicsManager.asset ├── GraphicsSettings.asset ├── ProjectSettings.asset ├── QualitySettings.asset ├── UnityAdsSettings.asset ├── ClusterInputManager.asset ├── EditorBuildSettings.asset ├── Physics2DSettings.asset ├── UnityAnalyticsManager.asset └── UnityConnectSettings.asset ├── Assets ├── Example.unity ├── Materials │ ├── sinTest.mat │ ├── hullTest.mat │ ├── hullTest.mat.meta │ └── sinTest.mat.meta ├── Textures │ ├── 2000px-Checkerboard_pattern.svg.png │ └── 2000px-Checkerboard_pattern.svg.png.meta ├── Example.unity.meta ├── Addon.meta ├── Scripts.meta ├── Shaders.meta ├── Textures.meta ├── Materials.meta ├── Shaders │ ├── Sin Wave.shader.meta │ ├── Bezier Curve.shader.meta │ ├── Bezier Curve Functions.cginc.meta │ ├── Bezier Curve Functions.cginc │ ├── Bezier Curve.shader │ └── Sin Wave.shader ├── Addon │ ├── UCLA GameLab Wireframe Functions.cginc.meta │ └── UCLA GameLab Wireframe Functions.cginc └── Scripts │ ├── BezierSurface.cs.meta │ └── BezierSurface.cs ├── README.md ├── .gitignore └── SHADERANATOMY.md /ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 2017.1.1f1 2 | -------------------------------------------------------------------------------- /Assets/Example.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/Assets/Example.unity -------------------------------------------------------------------------------- /Assets/Materials/sinTest.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/Assets/Materials/sinTest.mat -------------------------------------------------------------------------------- /Assets/Materials/hullTest.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/Assets/Materials/hullTest.mat -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityAdsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/UnityAdsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ClusterInputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/ClusterInputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityAnalyticsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/UnityAnalyticsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityConnectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/ProjectSettings/UnityConnectSettings.asset -------------------------------------------------------------------------------- /Assets/Textures/2000px-Checkerboard_pattern.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkjohnson/unity-bezier-curve-shader/HEAD/Assets/Textures/2000px-Checkerboard_pattern.svg.png -------------------------------------------------------------------------------- /Assets/Example.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 37af856d46904934f906ab8e3fef7a47 3 | timeCreated: 1438316073 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Materials/hullTest.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9dd0f1561b545d84493a1d8d342c7242 3 | timeCreated: 1438140731 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Materials/sinTest.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d624680448a34064b963d5537d7e1ca6 3 | timeCreated: 1438058374 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Addon.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f6553e69b6d194345a5efb17c55659da 3 | folderAsset: yes 4 | timeCreated: 1507434377 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e8ebe11bb38be3345ad6be751a03c2c3 3 | folderAsset: yes 4 | timeCreated: 1506905191 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a2e1474858531b34baf83329fb9653c7 3 | folderAsset: yes 4 | timeCreated: 1438057877 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Textures.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ae30e009e4883c24891f3790bfb378b7 3 | folderAsset: yes 4 | timeCreated: 1506905186 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cadf5daeff1813d4291637b5c90a8006 3 | folderAsset: yes 4 | timeCreated: 1438058365 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Shaders/Sin Wave.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a59ec8a390f2e5e4492f247508390e1f 3 | timeCreated: 1438057890 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Shaders/Bezier Curve.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 413da8b3a4c918f47b94305f1fef6b12 3 | timeCreated: 1438140311 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # unity-bezier-curve-shader 2 | Unity experiment to explore the different shader stages available in the pipeline. 3 | 4 | ## TODO 5 | - Images 6 | - Document shader pipeline with imagery 7 | - Produce a displacement shader based on texture and relative camera position 8 | -------------------------------------------------------------------------------- /Assets/Shaders/Bezier Curve Functions.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 55fb7a4e6f41d0249bf3b984f380975a 3 | timeCreated: 1508130035 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Addon/UCLA GameLab Wireframe Functions.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 569e37ae241a1af40b8e6ed37256f914 3 | timeCreated: 1507434377 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Scripts/BezierSurface.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d2144cf6ac30ce0459006ca93121b378 3 | timeCreated: 1438139126 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /[Ll]ibrary/ 2 | /[Tt]emp/ 3 | /[Oo]bj/ 4 | /[Bb]uild/ 5 | /[Bb]uilds/ 6 | /Assets/AssetStoreTools* 7 | 8 | # Visual Studio 2015 cache directory 9 | /.vs/ 10 | 11 | # Autogenerated VS/MD/Consulo solution and project files 12 | ExportedObj/ 13 | .consulo/ 14 | *.csproj 15 | *.unityproj 16 | *.sln 17 | *.suo 18 | *.tmp 19 | *.user 20 | *.userprefs 21 | *.pidb 22 | *.booproj 23 | *.svd 24 | *.pdb 25 | 26 | # Unity3D generated meta files 27 | *.pidb.meta 28 | 29 | # Unity3D Generated File On Crash Reports 30 | sysinfo.txt 31 | 32 | # Builds 33 | *.apk 34 | *.unitypackage 35 | -------------------------------------------------------------------------------- /SHADERANATOMY.md: -------------------------------------------------------------------------------- 1 | # Anatomy of a Shader 2 | ## Pipeline 3 | ### Vertex Shader 4 | Called once per attribute structure in the vertex array. 5 | 6 | ### Hull Shader 7 | An interpolated result from the vertex shader over a particular "patch" with the whole set of interpolated patch data. More data can be produced here as control point inputs to the Domain Shader. 8 | 9 | ### Tesselation / Hull Constant Shader 10 | A shader run in parallel with the Hull Shader that defines the edge and internal patch tesselation for the non-programmable tesselation stage. 11 | 12 | ### Domain Shader 13 | Run once for every vertex produced by the tesselation stage along with the whole set of control points for the patch, and the uv location for the point relative to the patch. This can be thought of as the Vertex Shader for the tesselation output. 14 | 15 | ### Fragment Shader 16 | Run once for every rasterized pixel in the projected resultant triangle from the domain shader. 17 | -------------------------------------------------------------------------------- /Assets/Textures/2000px-Checkerboard_pattern.svg.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9ab1312fd2a44fa4e9b2c770ab25b7f0 3 | timeCreated: 1438063267 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: {} 7 | serializedVersion: 2 8 | mipmaps: 9 | mipMapMode: 0 10 | enableMipMap: 1 11 | linearTexture: 0 12 | correctGamma: 0 13 | fadeOut: 0 14 | borderMipMap: 0 15 | mipMapFadeDistanceStart: 1 16 | mipMapFadeDistanceEnd: 3 17 | bumpmap: 18 | convertToNormalMap: 0 19 | externalNormalMap: 0 20 | heightScale: .25 21 | normalMapFilter: 0 22 | isReadable: 0 23 | grayScaleToAlpha: 0 24 | generateCubemap: 0 25 | cubemapConvolution: 0 26 | cubemapConvolutionSteps: 8 27 | cubemapConvolutionExponent: 1.5 28 | seamlessCubemap: 0 29 | textureFormat: -1 30 | maxTextureSize: 2048 31 | textureSettings: 32 | filterMode: -1 33 | aniso: -1 34 | mipBias: -1 35 | wrapMode: -1 36 | nPOTScale: 1 37 | lightmap: 0 38 | rGBM: 0 39 | compressionQuality: 50 40 | spriteMode: 0 41 | spriteExtrude: 1 42 | spriteMeshType: 1 43 | alignment: 0 44 | spritePivot: {x: .5, y: .5} 45 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 46 | spritePixelsToUnits: 100 47 | alphaIsTransparency: 0 48 | textureType: -1 49 | buildTargetSettings: [] 50 | spriteSheet: 51 | sprites: [] 52 | spritePackingTag: 53 | userData: 54 | assetBundleName: 55 | assetBundleVariant: 56 | -------------------------------------------------------------------------------- /Assets/Addon/UCLA GameLab Wireframe Functions.cginc: -------------------------------------------------------------------------------- 1 | // Algorithms and shaders based on code from this journal 2 | // http://cgg-journal.com/2008-2/06/index.html 3 | // http://web.archive.org/web/20130322011415/http://cgg-journal.com/2008-2/06/index.html 4 | 5 | #ifndef UCLA_GAMELAB_WIREFRAME 6 | #define UCLA_GAMELAB_WIREFRAME 7 | 8 | #include "UnityCG.cginc" 9 | 10 | // For use in the Geometry Shader 11 | // Takes in 3 vectors and calculates the distance to 12 | // to center of the triangle for each vert 13 | float3 UCLAGL_CalculateDistToCenter(float4 v0, float4 v1, float4 v2) { 14 | // points in screen space 15 | float2 ss0 = _ScreenParams.xy * v0.xy / v0.w; 16 | float2 ss1 = _ScreenParams.xy * v1.xy / v1.w; 17 | float2 ss2 = _ScreenParams.xy * v2.xy / v2.w; 18 | 19 | // edge vectors 20 | float2 e0 = ss2 - ss1; 21 | float2 e1 = ss2 - ss0; 22 | float2 e2 = ss1 - ss0; 23 | 24 | // area of the triangle 25 | float area = abs(e1.x * e2.y - e1.y * e2.x); 26 | 27 | // values based on distance to the center of the triangle 28 | float dist0 = area / length(e0); 29 | float dist1 = area / length(e1); 30 | float dist2 = area / length(e2); 31 | 32 | return float3(dist0, dist1, dist2); 33 | } 34 | 35 | // Computes the intensity of the wireframe at a point 36 | // based on interpolated distances from center for the 37 | // fragment, thickness, firmness, and perspective correction 38 | // factor. 39 | // w = 1 gives screen-space consistent wireframe thickness 40 | float UCLAGL_GetWireframeAlpha(float3 dist, float thickness, float firmness, float w = 1) { 41 | // find the smallest distance 42 | float val = min(dist.x, min(dist.y, dist.z)); 43 | val *= w; 44 | 45 | // calculate power to 2 to thin the line 46 | val = exp2(-1 / thickness * val * val); 47 | val = min(val * firmness, 1); 48 | return val; 49 | } 50 | #endif -------------------------------------------------------------------------------- /Assets/Shaders/Bezier Curve Functions.cginc: -------------------------------------------------------------------------------- 1 | 2 | // Bezier Curve Functions 3 | // returns the factorial of n until "stop" is hit 4 | float factorial(int n, int stop) { 5 | if(n<=0) return 1; 6 | if(stop <= 0) stop = 1; 7 | 8 | float res = 1; 9 | for(int i = n; i >= stop; i--) { 10 | res *= i; 11 | } 12 | return res; 13 | } 14 | 15 | // returns the factorial of n 16 | float factorial(int n) { 17 | return factorial(n,2); 18 | } 19 | 20 | // Takes an array of 16 control points to solve 21 | // the points are expected to be in the following format (Pxy) 22 | // P00 P10 P20 P30 P01 P11 ... P23 P33 23 | float3 SurfaceSolve(float3 cps[16], float2 uv) { 24 | float u = uv.y; 25 | float v = uv.x; 26 | 27 | float n = 3; // x target 28 | float m = 3; // y target 29 | 30 | float3 pos = float3(0,0,0); 31 | for(float y = 0 ; y <= m ; y ++) 32 | { 33 | float ypoly = pow(1-v, m-y) * pow(v,y); // 0 34 | float ybc = factorial(m,y+1) / (factorial(m-y)); 35 | 36 | for( float x = 0 ; x <= n ; x ++) 37 | { 38 | float3 cp = cps[ y * 4 + x ]; 39 | 40 | float xpoly = pow(1-u, n-x) * pow(u,x); // 0 41 | float xbc = factorial(n,x+1) / (factorial(n-x)); 42 | 43 | pos += xbc * xpoly * ybc * ypoly * cp; 44 | 45 | } 46 | } 47 | return pos; 48 | } 49 | 50 | float3 SurfaceSolve(StructuredBuffer cps, float2 uv) { 51 | float3 arr[16]; 52 | for( int i = 0 ; i < 16 ; i ++ ) 53 | { 54 | arr[i] = cps[i]; 55 | } 56 | return SurfaceSolve(arr, uv); 57 | } 58 | 59 | // Solves for a cubic Bezier curve using the 60 | // four given control points and returns the point 61 | // at "t" interpolation on the curve 62 | float3 CurveSolve(float3 cp0, float3 cp1, float3 cp2, float3 cp3, float t) { 63 | // summed bezier curve formula 64 | float3 cps[4] = {cp0, cp1, cp2, cp3}; 65 | float3 pos = float3(0,0,0); 66 | int n = 3; 67 | for( int i = 0 ; i <= n ; i ++ ) 68 | { 69 | // the poly and binomial coefficient parts 70 | // of the Bernstein polynomial 71 | float poly = pow(1-t, n-i) * pow(t,i); 72 | 73 | // n!/i!(n-i)! = (n*n-1*...*i+1)/(n-i)!; 74 | float bc = factorial(n,i+1) / (factorial(n-i)); 75 | 76 | // multiplied 77 | pos += bc * poly * cps[i]; 78 | } 79 | return pos; 80 | 81 | // expanded cubic curve formula 82 | pos = 83 | pow(1.0f - t, 3.0f) * cp0 + 84 | 3.0f * pow(1.0f - t, 2.0f) * t * cp1 + 85 | 3.0f * (1.0f - t) * pow(t, 2.0f) * cp2 + 86 | pow(t, 3.0f) * cp3; 87 | 88 | return pos; 89 | } -------------------------------------------------------------------------------- /Assets/Scripts/BezierSurface.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | [RequireComponent(typeof(MeshFilter))] 4 | public class BezierSurface : MonoBehaviour { 5 | // Visual size of and whether to display the 6 | // control points 7 | public float _controlpointScale = 0.1f; 8 | public bool _showControlPoints = true; 9 | 10 | Transform[] _controlPoints = new Transform[16]; 11 | ComputeBuffer _buffer; 12 | 13 | void Awake () { 14 | Mesh mesh = new Mesh(); 15 | 16 | // 1----2 17 | // | | 18 | // 4----3 19 | mesh.vertices = new Vector3[] { 20 | Vector3.zero, 21 | Vector3.zero, 22 | Vector3.zero, 23 | Vector3.zero 24 | }; 25 | 26 | mesh.uv = new Vector2[] { 27 | new Vector2(1,0), 28 | new Vector2(0,0), 29 | new Vector2(0,1), 30 | new Vector2(1,1) 31 | }; 32 | 33 | // create the control points 34 | for( int y = 0; y < 4; y++ ) 35 | { 36 | for( int x = 0; x < 4; x++ ) 37 | { 38 | Transform cp = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform; 39 | cp.name = "control point " + (4 * y + x); 40 | cp.hideFlags = HideFlags.HideInHierarchy; 41 | Destroy(cp.GetComponent()); 42 | 43 | cp.parent = transform; 44 | cp.localScale *= 0.1f; 45 | cp.localPosition = new Vector3( -1 + 2 * x / 3.0f, 0, -1 + 2 * y / 3.0f); 46 | 47 | cp.GetComponent().material.color = Color.red; 48 | 49 | _controlPoints[y * 4 + x] = cp; 50 | } 51 | } 52 | 53 | // set the indices to quads so we our hull shader can use it 54 | mesh.SetIndices(new int[]{ 3, 2, 1, 0}, MeshTopology.Quads, 0); 55 | mesh.bounds = new Bounds(Vector3.zero, 10000 * Vector3.one); 56 | GetComponent().mesh = mesh; 57 | 58 | // initialize the buffer 59 | _buffer = new ComputeBuffer(16, 3 * 4); 60 | } 61 | 62 | // Update the buffer with the new control points 63 | void Update() 64 | { 65 | Vector3[] arr = new Vector3[16]; 66 | for( int i = 0 ; i < arr.Length ; i ++ ) 67 | { 68 | _controlPoints[i].localScale = Vector3.one * _controlpointScale; 69 | _controlPoints[i].GetComponent().enabled = _showControlPoints; 70 | arr[i] = _controlPoints[i].localPosition; 71 | } 72 | 73 | _buffer.SetData(arr); 74 | GetComponent().material.SetBuffer("_controlPoints", _buffer); 75 | } 76 | 77 | // Draw visuals for the connectors between 78 | // the grab points 79 | void OnDrawGizmos() 80 | { 81 | Gizmos.color = new Color(1,1,1,0.25f); 82 | 83 | // if we're not running, then don't draw 84 | // the control point connectors, only the outline 85 | if( _buffer == null) 86 | { 87 | Gizmos.matrix = transform.localToWorldMatrix; 88 | Gizmos.DrawLine( new Vector3(-1,0,1), new Vector3(1,0,1)); 89 | Gizmos.DrawLine( new Vector3(1,0,1), new Vector3(1,0,-1)); 90 | Gizmos.DrawLine( new Vector3(1,0,-1), new Vector3(-1,0,-1)); 91 | Gizmos.DrawLine( new Vector3(-1,0,-1), new Vector3(-1,0,1)); 92 | return; 93 | } 94 | 95 | if(_showControlPoints) 96 | { 97 | // draw the x lines 98 | for( int y = 0 ; y < 4 ;y ++) 99 | { 100 | for(int x = 1 ; x < 4 ; x ++) 101 | { 102 | int index = y * 4 + x; 103 | 104 | Vector3 prevPoint = _controlPoints[index-1].position; 105 | Vector3 point = _controlPoints[index].position; 106 | 107 | Gizmos.DrawLine(prevPoint, point); 108 | } 109 | } 110 | 111 | // draw the y lines 112 | for(int x = 0 ; x < 4 ; x ++) 113 | { 114 | for( int y = 1 ; y < 4 ;y ++) 115 | { 116 | int index = y * 4 + x; 117 | 118 | Vector3 prevPoint = _controlPoints[index-4].position; 119 | Vector3 point = _controlPoints[index].position; 120 | 121 | Gizmos.DrawLine(prevPoint, point); 122 | } 123 | } 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /Assets/Shaders/Bezier Curve.shader: -------------------------------------------------------------------------------- 1 | Shader "Tessellated/Bezier Curve" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Main Texture", 2D) = "white" {} 6 | _InternalTessellation("Internal Tessellation", Float) = 5 7 | _EdgeTessellation("Edge Tessellation", Float) = 5 8 | [Toggle]_HideWireframe("Hide Wireframe", Float) = 0 9 | } 10 | 11 | SubShader 12 | { 13 | Tags { "RenderType"="Opaque" "Queue"="Geometry" } 14 | 15 | Pass 16 | { 17 | Cull Off 18 | 19 | CGPROGRAM 20 | #pragma target 5.0 21 | #include "UnityCG.cginc" 22 | #include "../Addon/UCLA GameLab Wireframe Functions.cginc" 23 | #include "./Bezier Curve Functions.cginc" 24 | #pragma vertex vert 25 | #pragma hull hull 26 | #pragma domain dom 27 | #pragma geometry geom 28 | #pragma fragment frag 29 | 30 | // Quad 31 | #define MAX_POINTS 4 32 | 33 | sampler2D _MainTex; 34 | float4 _MainTex_ST; 35 | float _InternalTessellation; 36 | float _EdgeTessellation; 37 | float _HideWireframe; 38 | uniform StructuredBuffer _controlPoints; 39 | 40 | // Vertex to Hull 41 | struct VS_OUTPUT { 42 | float4 position : POSITION; // vertex position 43 | float2 uv : TEXCOORD0; 44 | }; 45 | 46 | // Hull to Domain 47 | // Output control point 48 | struct HS_OUTPUT { 49 | float3 position : BEZIERPOS; 50 | float2 uv : TEXCOORD0; 51 | }; 52 | 53 | // Hull Constant to Domain 54 | // Output patch constant data. 55 | struct HS_CONSTANT_OUTPUT 56 | { 57 | float Edges[4] : SV_TessFactor; 58 | float Inside[2] : SV_InsideTessFactor; 59 | 60 | //float3 vTangent[4] : TANGENT; 61 | //float2 vUV[4] : TEXCOORD; 62 | //float3 vTanUCorner[4] : TANUCORNER; 63 | //float3 vTanVCorner[4] : TANVCORNER; 64 | //float4 vCWts : TANWEIGHTS; 65 | }; 66 | 67 | // Domain to Geometry 68 | struct DS_OUTPUT 69 | { 70 | float4 position : POSITION; 71 | float4 col : COLOR; 72 | float2 uv : TEXCOORD0; 73 | }; 74 | 75 | // Geometry to Fragment 76 | struct GS_OUTPUT 77 | { 78 | float4 position : POSITION; // fragment position 79 | float4 col : COLOR; 80 | float2 uv : TEXCOORD0; 81 | float3 normal : NORMAL; 82 | float3 dist : TEXCOORD1; 83 | }; 84 | 85 | // Vertex Shader 86 | VS_OUTPUT vert(appdata_base v) 87 | { 88 | // Pass through shader 89 | // The control points define position 90 | VS_OUTPUT output; 91 | output.position = v.vertex; 92 | output.uv = v.texcoord.xy; 93 | return output; 94 | } 95 | 96 | // Patch Constant Function 97 | HS_CONSTANT_OUTPUT hsConstant( 98 | InputPatch ip, 99 | uint PatchID : SV_PrimitiveID ) 100 | { 101 | HS_CONSTANT_OUTPUT output; 102 | 103 | float edge = _EdgeTessellation; 104 | float inside = _InternalTessellation; 105 | 106 | // Set the tessellation factors for the inside 107 | // and outside edges of the quad 108 | output.Edges[0] = edge; 109 | output.Edges[1] = edge; 110 | output.Edges[2] = edge; 111 | output.Edges[3] = edge; 112 | 113 | output.Inside[0] = inside; 114 | output.Inside[1] = inside; 115 | 116 | return output; 117 | } 118 | 119 | // Domain Shader 120 | [domain("quad")] 121 | [partitioning("fractional_even")] 122 | [outputtopology("triangle_cw")] 123 | [outputcontrolpoints(MAX_POINTS)] 124 | [patchconstantfunc("hsConstant")] 125 | HS_OUTPUT hull(InputPatch ip, uint i : SV_OutputControlPointID, uint PatchID : SV_PrimitiveID ) { 126 | HS_OUTPUT output; 127 | output.uv = ip[i].uv; 128 | 129 | output.position = ip[i].position; 130 | 131 | return output; 132 | } 133 | 134 | // Domain Shader 135 | [domain("quad")] 136 | DS_OUTPUT dom(HS_CONSTANT_OUTPUT input, float2 UV : SV_DomainLocation, const OutputPatch patch ) { 137 | DS_OUTPUT output; 138 | 139 | // Why do we have to do this? 140 | float2 uv = UV; 141 | uv *= 0.9999998; 142 | uv.x += 0.0000001; 143 | uv.y += 0.0000001; 144 | 145 | float4 pos = float4(SurfaceSolve(_controlPoints, uv),1); 146 | output.position = pos; 147 | output.uv = UV; 148 | output.col = float4(1, 1, 1, 1); 149 | 150 | return output; 151 | } 152 | 153 | // Geometry Shader 154 | [maxvertexcount(6)] 155 | void geom(triangle DS_OUTPUT p[3], inout TriangleStream triStream) 156 | { 157 | float3 norm = cross(p[0].position - p[1].position, p[0].position - p[2].position); 158 | norm = normalize(mul(unity_ObjectToWorld, float4(norm, 0))).xyz; 159 | 160 | p[0].position = UnityObjectToClipPos(p[0].position); 161 | p[1].position = UnityObjectToClipPos(p[1].position); 162 | p[2].position = UnityObjectToClipPos(p[2].position); 163 | 164 | float3 dist = UCLAGL_CalculateDistToCenter(p[0].position, p[1].position, p[2].position); 165 | 166 | GS_OUTPUT i1, i2, i3; 167 | 168 | // Add the normal facing triangle 169 | i1.position = p[0].position; 170 | i1.col = p[0].col; 171 | i1.uv = p[0].uv; 172 | i1.normal = norm; 173 | i1.dist = float3(dist.x, 0, 0); 174 | 175 | i2.position = p[1].position; 176 | i2.col = p[1].col; 177 | i2.uv = p[1].uv; 178 | i2.normal = norm; 179 | i2.dist = float3(0, dist.y, 0); 180 | 181 | i3.position = p[2].position; 182 | i3.col = p[2].col; 183 | i3.uv = p[2].uv; 184 | i3.normal = norm; 185 | i3.dist = float3(0, 0, dist.z); 186 | 187 | triStream.Append(i1); 188 | triStream.Append(i2); 189 | triStream.Append(i3); 190 | } 191 | 192 | // Fragment Shader 193 | float4 frag(GS_OUTPUT input, fixed facing : VFACE) : COLOR 194 | { 195 | float alpha = UCLAGL_GetWireframeAlpha(input.dist, .25, 100, 1); 196 | clip(alpha - 0.9); 197 | 198 | float4 col = input.col; 199 | float2 uv = TRANSFORM_TEX (input.uv, _MainTex); 200 | col = tex2D(_MainTex, uv) * float4(input.normal * facing, 1); 201 | 202 | return col; 203 | } 204 | 205 | ENDCG 206 | } 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /Assets/Shaders/Sin Wave.shader: -------------------------------------------------------------------------------- 1 | // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 2 | 3 | // TODO : Something weird happens with this shader 4 | // when it's batched. 5 | // It could have something to do with the shader getting batched 6 | // and therefore not having the right transformed "forward" 7 | Shader "Tessellated/Sin Wave" 8 | { 9 | Properties 10 | { 11 | _SinOffset ("Sin Offset", Float) = 0 12 | _InternalTessellation("Internal Tessellation", Float) = 5 13 | _EdgeTessellation("Edge Tessellation", Float) = 5 14 | [Toggle]_HideWireframe("Hide Wireframe", Float) = 0 15 | } 16 | 17 | SubShader 18 | { 19 | Tags { "RenderType" = "Opaque" "Queue" = "Geometry" } 20 | 21 | Pass 22 | { 23 | CGPROGRAM 24 | #pragma target 5.0 25 | #include "UnityCG.cginc" 26 | #include "../Addon/UCLA GameLab Wireframe Functions.cginc" 27 | #pragma vertex vert 28 | #pragma hull hull 29 | #pragma domain dom 30 | #pragma geometry geom 31 | #pragma fragment frag 32 | 33 | // Triangle 34 | #define MAX_POINTS 3 35 | 36 | float _SinOffset; 37 | float _InternalTessellation; 38 | float _EdgeTessellation; 39 | float _HideWireframe; 40 | 41 | // Vertex to Hull 42 | struct VS_OUTPUT { float4 position : POSITION; }; 43 | 44 | // Hull to Domain 45 | // Output control point 46 | struct HS_OUTPUT { float3 position : BEZIERPOS; }; 47 | 48 | // Hull Constant to Domain 49 | // Output patch constant data. 50 | struct HS_CONSTANT_OUTPUT { 51 | float Edges[3] : SV_TessFactor; 52 | float Inside[1] : SV_InsideTessFactor; 53 | 54 | //float3 vTangent[4] : TANGENT; 55 | //float2 vUV[4] : TEXCOORD; 56 | //float3 vTanUCorner[4] : TANUCORNER; 57 | //float3 vTanVCorner[4] : TANVCORNER; 58 | //float4 vCWts : TANWEIGHTS; 59 | }; 60 | 61 | // Domain to Geometry 62 | struct DS_OUTPUT { 63 | float4 position : POSITION; 64 | float4 col : COLOR; 65 | }; 66 | 67 | // Geometry to Fragment 68 | struct GS_OUTPUT { 69 | float4 position : POSITION; 70 | float4 col : COLOR; 71 | float3 dist : TEXCOORD0; 72 | float3 normal : NORMAL; 73 | }; 74 | 75 | // Vertex Shader 76 | VS_OUTPUT vert(appdata_base v) { 77 | VS_OUTPUT output; 78 | output.position = UnityObjectToClipPos(v.vertex); 79 | output.position = v.vertex; 80 | return output; 81 | } 82 | 83 | // Hull Shader 84 | // Transform and output control points for the hull points 85 | // to then be transformed in the domain shader 86 | [domain("tri")] 87 | [partitioning("integer")] 88 | [outputtopology("triangle_cw")] 89 | [outputcontrolpoints(MAX_POINTS)] 90 | [patchconstantfunc("hsConstant")] 91 | HS_OUTPUT hull(InputPatch ip, uint i : SV_OutputControlPointID, uint PatchID : SV_PrimitiveID) { 92 | HS_OUTPUT output; 93 | output.position = ip[i].position; 94 | 95 | return output; 96 | } 97 | 98 | // Patch Constant Function 99 | // Outputs edge and internal tesellation values for the triangle 100 | // This could be driven by camera skew, distance from camera, texture sampling, etc 101 | // to change the detail that is tessellated to 102 | HS_CONSTANT_OUTPUT hsConstant(InputPatch ip, uint PatchID : SV_PrimitiveID) { 103 | HS_CONSTANT_OUTPUT output; 104 | 105 | float edge = _EdgeTessellation; 106 | float inside = _InternalTessellation; 107 | 108 | output.Edges[0] = edge; 109 | output.Edges[1] = edge; 110 | output.Edges[2] = edge; 111 | //output.Edges[3] = edge; 112 | 113 | output.Inside[0] = inside; 114 | //output.Inside[1] = inside; 115 | 116 | return output; 117 | } 118 | 119 | // Domain Shader 120 | // Transform all the points from the hull shader here 121 | // The detail from the tessellated patch and the hull control points 122 | // are available here 123 | [domain("tri")] 124 | DS_OUTPUT dom(HS_CONSTANT_OUTPUT input, float3 UV : SV_DomainLocation, const OutputPatch patch ) { 125 | DS_OUTPUT output; 126 | 127 | //float3 topMidpoint = lerp(patch[0].position, patch[1].position, UV.x); 128 | //float3 bottomMidpoint = lerp(patch[3].position, patch[2].position, UV.x); 129 | 130 | //float4 pos = float4(lerp(topMidpoint, bottomMidpoint, UV.y),1); 131 | //output.position = pos; 132 | //output.position = mul(UNITY_MATRIX_MVP, pos); 133 | 134 | //Output.color = float4(UV.yx, 1-UV.x, 1); 135 | 136 | float3 pos = 137 | UV.x * patch[0].position + 138 | UV.y * patch[1].position + 139 | UV.z * patch[2].position; 140 | 141 | pos.z += sin(UV.x * 10.0f + _SinOffset) + sin(UV.y * 10.0f + _SinOffset); 142 | pos.z *= 0.1f; 143 | 144 | output.position = float4(pos, 1); 145 | output.col = float4(UV.x,UV.y,0,1); 146 | 147 | return output; 148 | } 149 | 150 | // Geometry Shader 151 | // Produce new geometry if desired 152 | [maxvertexcount(6)] 153 | void geom(triangle DS_OUTPUT p[3], inout TriangleStream triStream) 154 | { 155 | GS_OUTPUT pIn; 156 | 157 | float3 norm = cross(p[0].position - p[1].position, p[0].position - p[2].position); 158 | norm = normalize(mul(unity_ObjectToWorld, float4(norm,0))).xyz; 159 | 160 | // TODO: Consider moving this to the 161 | // domain or the vertex shader 162 | p[0].position = UnityObjectToClipPos(p[0].position); 163 | p[1].position = UnityObjectToClipPos(p[1].position); 164 | p[2].position = UnityObjectToClipPos(p[2].position); 165 | 166 | float3 dist = UCLAGL_CalculateDistToCenter(p[0].position, p[1].position, p[2].position); 167 | 168 | // Add the normal facing triangle 169 | pIn.position = p[0].position; 170 | pIn.col = p[0].col; 171 | pIn.dist = float3(dist.x, 0 ,0); 172 | pIn.normal = norm; 173 | triStream.Append(pIn); 174 | 175 | pIn.position = p[1].position; 176 | pIn.col = p[1].col; 177 | pIn.dist = float3(0, dist.y, 0); 178 | pIn.normal = norm; 179 | triStream.Append(pIn); 180 | 181 | pIn.position = p[2].position; 182 | pIn.col = p[2].col; 183 | pIn.dist = float3(0, 0, dist.z); 184 | pIn.normal = norm; 185 | triStream.Append(pIn); 186 | 187 | // Add a reverse facing triangle 188 | pIn.position = p[0].position; 189 | pIn.col = p[0].col; 190 | pIn.dist = float3(dist.x, 0, 0); 191 | pIn.normal = norm; 192 | triStream.Append(pIn); 193 | 194 | pIn.position = p[1].position; 195 | pIn.col = p[1].col; 196 | pIn.dist = float3(0, dist.y, 0); 197 | pIn.normal = norm; 198 | triStream.Append(pIn); 199 | 200 | pIn.position = p[2].position; 201 | pIn.col = p[2].col; 202 | pIn.dist = float3(0, 0, dist.z); 203 | pIn.normal = norm; 204 | triStream.Append(pIn); 205 | } 206 | 207 | // Fragment Shader 208 | // Pixel Color 209 | float4 frag(GS_OUTPUT input) : COLOR 210 | { 211 | float alpha = UCLAGL_GetWireframeAlpha(input.dist, .25, 100, 1); 212 | clip(alpha - 0.5 + _HideWireframe); 213 | 214 | float4 col = input.col * (0.5 * alpha + 0.5); 215 | col = float4(input.normal, 1); 216 | return col; 217 | } 218 | 219 | ENDCG 220 | } 221 | } 222 | } 223 | --------------------------------------------------------------------------------