├── .gitattributes ├── 2D_fractal_generator.shader ├── 2D_fractal_generator_distortion.shader ├── 2d_signed_distance.shader ├── 3d_fractal_generator.shader ├── AmbientOcclusion.cs ├── AmbientOcclusion.shader ├── BakeTexture.cs ├── BakeTexture.shader ├── BakeVertexColorMap.cs ├── BakeVertexColorMap.shader ├── BezierSurface.cs ├── BezierSurface.shader ├── BitonicSorter.compute ├── BitonicSorter.cs ├── CAS.cs ├── CAS.shader ├── CatmullRomShader.shader ├── CatmullRomSurface.cs ├── ConstantBuffer.compute ├── ConstantBuffer.cs ├── DXR └── PathTracing │ ├── IndirectDiffuse.shader │ ├── PathTracing.cs │ ├── PathTracing.raytrace │ └── ProgressiveRendering.shader ├── DecalPainter.cs ├── DecalPainter.shader ├── DepthBufferToWorldSpace.cs ├── DepthBufferToWorldSpace.shader ├── DimensionConverter.compute ├── DimensionConverter.cs ├── DrawCircles.cs ├── DrawCircles.shader ├── DrawCubes.cs ├── DrawCubes.shader ├── DrawSphere.cs ├── DrawSphere.shader ├── FLD ├── Readme.md ├── acer.txt ├── fld_gpu.compute ├── fld_gpu.cs └── quercus.txt ├── Foam.shader ├── FogOfWar.cs ├── FogOfWar.shader ├── FurTextureGenerator.cs ├── FurTextureGenerator.shader ├── GLSL ├── LabyrinthGLES.cs ├── LabyrinthGLES.shader ├── Readme.md ├── ShaderStorageBufferObject.compute ├── ShaderStorageBufferObject.cs ├── basic_montecarlo.shader ├── divide_screen_effect.shader ├── geometry_shader_01.shader ├── quicksort.shader ├── quicksort_input.tga ├── quicksort_output.png └── tessellation.shader ├── GeometryShaders ├── Cloner.shader ├── Cube.shader ├── Quadrangulation.shader └── Wireframe.shader ├── HDR ├── Bloom.shader ├── EyeAdaptation.shader ├── EyeHistogram.compute ├── HDR.cs ├── Uber.shader └── readme.md ├── I.shader ├── ImageToSDFGenerator.cs ├── ImageToSDFRenderer.cs ├── ImageToSDFRenderer.shader ├── JumpFlooding3D.compute ├── JumpFlooding3D.cs ├── JumpFlooding3D.shader ├── Labyrinth.cs ├── Labyrinth.shader ├── MarchingCubes ├── MarchingCubes.compute ├── MarchingCubes.cs ├── MarchingCubes.shader ├── Readme.md └── ScalarField.compute ├── MeshShaderRTX ├── MeshShaderPlugin.cpp ├── MeshShaderPlugin.cs └── MeshShaderPlugin.txt ├── MipmapLevel.shader ├── Multipass ├── FluidMosaic.cs ├── FluidMosaic.shader ├── FluidSimulation.cs ├── FluidSimulation.shader ├── NoiseTexture.cs ├── NoiseTexture.shader ├── PseudoFluidSystem.cs ├── PseudoFluidSystem.shader ├── SatinFlow.cs ├── SatinFlow.shader ├── StrangeFluid.cs ├── StrangeFluid.shader ├── WindFlowMap.cs ├── WindFlowMap.shader └── info.txt ├── NurbsCurve.cs ├── NurbsCurve.shader ├── NurbsHair.cs ├── NurbsHair.shader ├── NurbsSurface.cs ├── NurbsSurface.shader ├── ParticleCollision.compute ├── ParticleCollision.cs ├── ParticleCollision.shader ├── Physics └── SpherePhysics │ ├── SpherePhysics.compute │ ├── SpherePhysics.cs │ ├── SpherePhysics.shader │ └── SpherePhysics.txt ├── Planet.cs ├── Planet.shader ├── ProceduralGeometry.cs ├── ProceduralGeometry.shader ├── ProceduralSkybox.shader ├── ProceduralSphereTessellation.cs ├── ProceduralSphereTessellation.shader ├── README.md ├── ReShade ├── AdvancedCRT.cs ├── AdvancedCRT.shader ├── Cartoon.cs ├── Cartoon.shader ├── DepthHaze.cs ├── DepthHaze.shader ├── PPFX_SSDO.cs └── PPFX_SSDO.shader ├── ReactionDiffusion.cs ├── ReactionDiffusion.shader ├── ShaderDebugging.cs ├── ShaderDebugging.shader ├── ShaderReplacement ├── MainShader.mat ├── MainShader.shader ├── SecondShader.shader ├── ShaderReplacement.cs ├── ShaderReplacement.renderTexture └── readme.md ├── ShadowMapping.cs ├── ShadowMapping.shader ├── ShadowVolumeForSphereOnly.shader ├── SquareRoot.compute ├── SquareRoot.cs ├── TerrainAssets ├── TerrainAssetsReadme.md └── Valley.asset ├── TetrahedralMesh.bin ├── TetrahedralMesh.compute ├── TetrahedralMesh.cs ├── TetrahedralMesh.shader ├── TextureToBlocks.compute ├── TextureToBlocks.cs ├── TextureToBlocks.shader ├── TriangleStrip.cs ├── TriangleStrip.shader ├── URP ├── BulletMarksURP.cs ├── BulletMarksURP.shader ├── ForwardLitURP.shader ├── HexagonsURP.shader ├── PaintURP-Unlit.shader ├── PaintURP.cs ├── PaintURP.shader ├── ProceduralDissolveURP.shader ├── RenderToTextureURP.cs └── RenderToTextureURP.shader ├── Unity2021 ├── BoundingBox.compute ├── BoundingBox.cs ├── Cobalt.shader ├── DeleteTriangles.compute ├── DeleteTriangles.cs ├── Derivatives.compute ├── Derivatives.cs ├── MeshClosestPoint.compute ├── MeshClosestPoint.cs ├── NurbsSurfaceMeshCompute.compute ├── NurbsSurfaceMeshCompute.cs ├── PhongTessellation.compute ├── PhongTessellation.cs ├── ProceduralGrass.compute ├── ProceduralGrass.cs └── ProceduralGrass.shader ├── UnsignedInteger64.compute ├── UnsignedInteger64.cs ├── VertexShaderImage.cs ├── VertexShaderImage.shader ├── VertexShaderTessellation.cs ├── VertexShaderTessellation.shader ├── VertexShaderTessellationStandard.cs ├── VertexShaderTessellationStandard.shader ├── VisualAI.compute ├── VisualAI.cs ├── VolumeParticlesSystem ├── VolumeParticleSystem.cs ├── VolumeParticleSystem.shader ├── VolumeParticlesSystem.jpg └── readme.md ├── VolumeRenderTexture ├── VolumeRenderTexture.compute ├── VolumeRenderTexture.cs └── VolumeRenderTexture.shader ├── VolumeSpotLight.cs ├── VolumeSpotLight.shader ├── VolumeTexture ├── GenerateVolumeTexture.cs ├── GenerateVolumeTextureAsync.cs ├── StanfordBunny.fbx ├── VolumeRendering.cs ├── VolumeRendering.shader └── readme.md ├── VoronoiCones.cs ├── VoronoiCones.shader ├── VoronoiDualGraph.compute ├── VoronoiDualGraph.cs ├── VoronoiDualGraph.shader ├── VoronoiEdges.compute ├── VoronoiEdges.cs ├── abstract.shader ├── abyss.cs ├── abyss.shader ├── accumulation.cs ├── accumulation.shader ├── apollonianII.shader ├── area.compute ├── area.cs ├── area.shader ├── atmospheric_scattering.shader ├── bezier.shader ├── billboard.shader ├── binormals.shader ├── bitwise_test.shader ├── bleach.shader ├── blur.shader ├── bounce_ball.shader ├── brightness_contrast_saturation.shader ├── buffer.cs ├── buffer.shader ├── chess.shader ├── chess_with_sky.shader ├── chess_with_sky_and_ssaa.shader ├── chromatic_aberration.shader ├── city.shader ├── clockwise_triangles.shader ├── clouds2D.shader ├── collision_mesh_plane.compute ├── collision_mesh_plane.cs ├── collision_mesh_plane.shader ├── colorblind.shader ├── colours.shader ├── concrete_texture.shader ├── cubemorph.shader ├── debug_vertex_index.cs ├── debug_vertex_index.shader ├── debugger.shader ├── deferred_metallic_gloss.shader ├── demoscene ├── readme.md └── shader_model_3 │ ├── main.cpp │ └── readme.md ├── depth.cs ├── depth.shader ├── diagonal_transition.cs ├── diagonal_transition.shader ├── directional_derivative.shader ├── disney.shader ├── distance.shader ├── distortion.shader ├── drunk.cs ├── drunk.shader ├── edge_detection.shader ├── effect_transition.shader ├── effect_transition_extended.shader ├── elevated.shader ├── face.shader ├── fbm_generator.shader ├── fbm_generator_version01.shader ├── fbm_generator_version02.shader ├── fibonacci.compute ├── fibonacci.cs ├── filmic_tonemapping.shader ├── fire.cs ├── fire.shader ├── fireworks.shader ├── flow_mapping.cs ├── flow_mapping.shader ├── fluid_dynamics.compute ├── fluid_dynamics.cs ├── fog.shader ├── font.png ├── font.shader ├── fractal_image.shader ├── fur.shader ├── gabor.shader ├── gear_wheel.shader ├── grayscale.shader ├── helix.shader ├── hell.shader ├── hsv.shader ├── hypsometric_map_generator.shader ├── image_compression.shader ├── implicit.shader ├── instancing.cs ├── instancing.shader ├── integer_hash.shader ├── interior_mapping.shader ├── isolines.shader ├── isolines2.shader ├── lava.shader ├── liquid.shader ├── mandelbulb.shader ├── map_projection.cs ├── map_projection.shader ├── map_trees.cs ├── map_trees.shader ├── matrix_example.shader ├── maze.shader ├── mesh_deformation.compute ├── mesh_deformation.cs ├── mesh_unwrapping.shader ├── metaballs.shader ├── mirror_effect.shader ├── mona_lisa.shader ├── mrt.cs ├── mrt.shader ├── negation.shader ├── no_interpolation.shader ├── noise.png ├── noise.shader ├── noise_transition.shader ├── normal_extrusion.shader ├── normal_map_diffuse.shader ├── normal_map_generator.shader ├── notebook_drawings.cs ├── notebook_drawings.shader ├── notepad.txt ├── ocean.cs ├── ocean.shader ├── oil.shader ├── oren_nayar.shader ├── orthogonal_sphere.shader ├── outline.cs ├── outline.shader ├── paint.cs ├── paint.shader ├── paint3D.cs ├── paint3D.shader ├── pbr_specular_roughness.shader ├── photoshop_blends.shader ├── pillar.shader ├── pixelation.shader ├── point_cloud.cs ├── point_cloud.shader ├── point_cloud_with_shadow.cs ├── point_cloud_with_shadow.shader ├── polygon.shader ├── pom.shader ├── posterize.shader ├── procedural textures ├── boxes.shader ├── feathers.shader ├── heptagon.shader ├── metal.shader ├── octagon.shader ├── random_tiles.shader ├── readme.txt ├── ribbons.shader ├── torn_fabric.shader └── wheat_color.shader ├── procedural_column_chart.shader ├── procedural_font.shader ├── quad_tessellation.shader ├── radial_blur_2k18.shader ├── raycasting_cylinder.shader ├── raymarched_plasma.shader ├── raymarching_cubemap.shader ├── raymarching_direct_compute.compute ├── raymarching_direct_compute.cs ├── raymarching_full_integration.cs ├── raymarching_full_integration.shader ├── raymarching_materials.shader ├── raymarching_materials_integration.shader ├── raymarching_point_lights.shader ├── raymarching_procedural_cubemap.shader ├── raymarching_repetition.shader ├── raymarching_room.jpg ├── raymarching_room.shader ├── raymarching_room_with_reflection.shader ├── rectangles3.shader ├── relief.shader ├── rhodium.shader ├── rw_structured_buffer.cs ├── rw_structured_buffer.shader ├── sample_texture.shader ├── save_material.compute ├── save_material.cs ├── save_material_editor.cs ├── save_render_texture.compute ├── save_render_texture.cs ├── scale.shader ├── screen_space_curvature.shader ├── seascape.shader ├── shader_GUI_variant_01.shader ├── shader_dynamic_array.cs ├── shader_dynamic_array.shader ├── shadowcaster.shader ├── shape_blending.shader ├── shape_blending_with_penumbra.shader ├── sierpinski.shader ├── skybox_blur.shader ├── smallpt.compute ├── smallpt.cs ├── snow.shader ├── soft_shadow.shader ├── sonar.shader ├── sphere_mapping.shader ├── spheres.shader ├── stadium.shader ├── strauss.shader ├── structured_buffer.compute ├── structured_buffer.cs ├── structured_buffer.shader ├── subsurface_scattering.shader ├── subtexture.shader ├── surface_shaders ├── BillboardMetallic.shader ├── Cellular.shader ├── Metallic.shader ├── Multitexturing.shader ├── PBR_double_sided_color.shader ├── Specular.shader ├── SubsurfaceScattering.shader ├── clip_box.shader ├── debris.shader ├── readme ├── terrain │ ├── TerrainShader-AddPass.shader │ ├── TerrainShader-Base.shader │ └── TerrainShader-FirstPass.shader ├── vface.shader └── world_space_uv_mapping_metallic.shader ├── terrain_procedural.shader ├── terrain_shader_tessellation_diffuse.shader ├── terrain_with_heigthmap.shader ├── tessellation.shader ├── texture_array.shader ├── texture_mapping.shader ├── texture_variation.shader ├── the_smallest_shader.shader ├── tiny_clouds.shader ├── tools ├── readme.md ├── rendering_transformation_matrices_calculator.xls ├── rendering_transformation_matrices_calculator_version2.xls └── rendering_transformation_matrices_help.jpg ├── toonify.cs ├── toonify.shader ├── transmission.shader ├── triangle.shader ├── triangle2.shader ├── tribute_to_marc_antoine_mathieu.shader ├── truchet.shader ├── tunnel.shader ├── twister.shader ├── unlit_double_sided.shader ├── unlit_geometry_shader.shader ├── unlit_lightmap.shader ├── unlit_see_through.shader ├── unlit_splat_mapping.shader ├── unlit_transparency.shader ├── uv.shader ├── vector_displacement_mapping.shader ├── vertex_colors.shader ├── vignette_filter.shader ├── volume_noise.shader ├── volume_texture.cs ├── volume_texture.shader ├── volumetric texture ├── Composite.shader ├── Ray Marching.shader ├── RayMarching.cs ├── Render Back Depth.shader ├── Render Front Depth.shader └── Volume.shader ├── volumetric_bunny.shader ├── volumetric_clouds.shader ├── volumetric_lighting.cs ├── volumetric_lighting.shader ├── volumetric_machine.shader ├── voronoi_pattern.shader ├── voxel_torus.shader ├── vtf.shader ├── water_caustics.shader ├── waveform.cs ├── waveform.shader ├── wireframe.shader └── wood_texture.shader /.gitattributes: -------------------------------------------------------------------------------- 1 | * linguist-vendored 2 | *.shader linguist-language=ShaderLab 3 | -------------------------------------------------------------------------------- /2d_signed_distance.shader: -------------------------------------------------------------------------------- 1 | Shader "2D signed distance" 2 | { 3 | Properties 4 | { 5 | _size ("Size", Range(0.0, 1.0)) = 0.5 6 | _smoothness ("Smoothness", Range(0.0, 0.1)) = 0.03 7 | _color ("Color ", Color) = (1,1,1,1) 8 | _edges ("Number of edges", Int) = 3 9 | } 10 | SubShader 11 | { 12 | Pass 13 | { 14 | CGPROGRAM 15 | #pragma vertex vertex_shader 16 | #pragma fragment pixel_shader 17 | 18 | struct structure 19 | { 20 | float4 vertex:SV_POSITION; 21 | float2 uv : TEXCOORD0; 22 | }; 23 | 24 | int _edges; 25 | float _size,_smoothness; 26 | float4 _color; 27 | 28 | void vertex_shader(float4 vertex:POSITION,float2 uv:TEXCOORD0,out structure vs) 29 | { 30 | vs.vertex = UnityObjectToClipPos(vertex); 31 | vs.uv = uv; 32 | } 33 | 34 | void pixel_shader(in structure ps, out float4 fragColor:SV_Target0) 35 | { 36 | float2 uv = float2(2.0*ps.uv.xy-1.0); 37 | float3 color = float3(0.0,0.0,0.0); 38 | float d = 0.0; 39 | float a = atan2(uv.x,uv.y)+3.14159265359; 40 | float r = 6.28318530718/float(_edges); 41 | d = cos(floor(0.5+a/r)*r-a)*length(uv); 42 | float k = 1.0-smoothstep(_size,_size+_smoothness,d); 43 | color = float3(k,k,k)*_color; 44 | fragColor = float4(color,1.0); 45 | } 46 | ENDCG 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /BakeVertexColorMap.cs: -------------------------------------------------------------------------------- 1 | // https://forum.unity.com/threads/is-it-possible-to-convert-mesh-vertex-colors-to-texture.1087835/#post-7010599 2 | using UnityEngine; 3 | 4 | public class BakeVertexColorMap : MonoBehaviour 5 | { 6 | public Mesh SourceMesh; 7 | public Shader BakeVertexColorMapShader; 8 | public int Resolution = 2048; 9 | 10 | void Start() 11 | { 12 | if (SourceMesh != null) 13 | { 14 | RenderTexture renderTexture = new RenderTexture(Resolution, Resolution, 0); 15 | renderTexture.Create(); 16 | Material material = new Material(BakeVertexColorMapShader); 17 | RenderTexture currentTexture = RenderTexture.active; 18 | RenderTexture.active = renderTexture; 19 | GL.Clear(false, true, Color.black, 1.0f); 20 | material.SetPass(0); 21 | Graphics.DrawMeshNow(SourceMesh, Vector3.zero, Quaternion.identity); 22 | Texture2D texture = new Texture2D(Resolution, Resolution, TextureFormat.ARGB32, false); 23 | texture.ReadPixels( new Rect(0, 0, Resolution, Resolution), 0, 0); 24 | RenderTexture.active = currentTexture; 25 | byte[] bytes = texture.EncodeToPNG(); 26 | System.IO.File.WriteAllBytes(System.IO.Path.Combine(Application.dataPath, "VertexColors.png"), bytes); 27 | Destroy(material); 28 | Destroy(texture); 29 | renderTexture.Release(); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /BakeVertexColorMap.shader: -------------------------------------------------------------------------------- 1 | Shader "Bake Vertex Color Map" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | ZTest Off 8 | ZWrite Off 9 | Cull Off 10 | CGPROGRAM 11 | #pragma vertex VSMain 12 | #pragma fragment PSMain 13 | 14 | void VSMain (inout float4 vertex:POSITION, inout float2 uv:TEXCOORD0, inout float4 color:COLOR) 15 | { 16 | float2 texcoord = uv.xy; 17 | texcoord.y = 1.0 - texcoord.y; 18 | texcoord = texcoord * 2.0 - 1.0; 19 | vertex = float4(texcoord, 0.0, 1.0); 20 | } 21 | 22 | float4 PSMain (float4 vertex:POSITION, float2 uv:TEXCOORD0, float4 color:COLOR) : SV_TARGET 23 | { 24 | return color; 25 | } 26 | ENDCG 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /BezierSurface.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class BezierSurface : MonoBehaviour 4 | { 5 | public Shader BezierSurfaceShader; 6 | [Range(1, 1024)] public int TessellationFactor = 32; 7 | private Material _Material; 8 | 9 | void Start() 10 | { 11 | if (BezierSurfaceShader == null) BezierSurfaceShader = Shader.Find("Bezier Surface"); 12 | _Material = new Material(BezierSurfaceShader); 13 | } 14 | 15 | void OnRenderObject() 16 | { 17 | _Material.SetInt("_TessellationFactor", TessellationFactor); 18 | _Material.SetPass(0); 19 | int vertexCount = TessellationFactor * TessellationFactor * 6; 20 | Graphics.DrawProceduralNow(MeshTopology.Triangles, vertexCount, 1); 21 | } 22 | } -------------------------------------------------------------------------------- /BitonicSorter.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel BitonicInit 2 | #pragma kernel BitonicMain 3 | 4 | #define THREAD_GROUP_SIZE 256 5 | #define MAX_PARALLEL_GROUPS 1024 6 | #define MAX_PARALLEL_THREADS (THREAD_GROUP_SIZE * MAX_PARALLEL_GROUPS) 7 | 8 | uint _Count; 9 | int _Block, _Dimension; 10 | RWStructuredBuffer _Keys; 11 | StructuredBuffer _Values; 12 | 13 | [numthreads(THREAD_GROUP_SIZE, 1, 1)] 14 | void BitonicInit (uint3 threadID : SV_DispatchThreadID) 15 | { 16 | uint i = threadID.x + threadID.y * MAX_PARALLEL_THREADS; 17 | if (i < _Count) _Keys[i] = i; 18 | } 19 | 20 | [numthreads(THREAD_GROUP_SIZE, 1, 1)] 21 | void BitonicMain (uint3 threadID : SV_DispatchThreadID) 22 | { 23 | uint x = threadID.x + threadID.y * MAX_PARALLEL_THREADS; 24 | uint y = x ^ _Block; 25 | if (y < x || x >= _Count) return; 26 | uint a = _Keys[x]; 27 | uint b = _Keys[y]; 28 | bool shift = ((_Values[a] - _Values[b]) * ((x & _Dimension) == 0 ? 1 : -1)) > 0; 29 | _Keys[x] = shift ? b : a; 30 | _Keys[y] = shift ? a : b; 31 | } -------------------------------------------------------------------------------- /CatmullRomSurface.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class CatmullRomSurface : MonoBehaviour 4 | { 5 | public Shader CatmullRomSurfaceShader; 6 | [Range(1, 1024)] public int TessellationFactor = 32; 7 | private Material _Material; 8 | 9 | void Start() 10 | { 11 | _Material = new Material(CatmullRomSurfaceShader); 12 | } 13 | 14 | void OnRenderObject() 15 | { 16 | _Material.SetInt("_TessellationFactor", TessellationFactor); 17 | _Material.SetPass(0); 18 | int vertexCount = TessellationFactor * TessellationFactor * 6; 19 | Graphics.DrawProceduralNow(MeshTopology.Triangles, vertexCount, 1); 20 | } 21 | } -------------------------------------------------------------------------------- /ConstantBuffer.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | 3 | RWStructuredBuffer _StructuredBuffer; 4 | 5 | cbuffer _ConstantBuffer 6 | { 7 | int Index; 8 | int Radius; 9 | }; 10 | 11 | [numthreads(1,1,1)] 12 | void CSMain (uint3 id : SV_DispatchThreadID) 13 | { 14 | _StructuredBuffer[0] = Index + Radius; 15 | } -------------------------------------------------------------------------------- /ConstantBuffer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Runtime.InteropServices; 3 | 4 | public class ConstantBuffer : MonoBehaviour 5 | { 6 | [SerializeField] ComputeShader _ComputeShader; 7 | 8 | struct Element 9 | { 10 | public int Index; 11 | public int Radius; 12 | } 13 | 14 | void Start() 15 | { 16 | if (_ComputeShader == null) return; 17 | ComputeBuffer constantBuffer = new ComputeBuffer(1, Marshal.SizeOf(typeof(Element)), ComputeBufferType.Constant); 18 | ComputeBuffer structuredBuffer = new ComputeBuffer(1, Marshal.SizeOf(typeof(System.Int32)), ComputeBufferType.Structured); 19 | _ComputeShader.SetConstantBuffer("_ConstantBuffer", constantBuffer, 0, Marshal.SizeOf(typeof(Element))); 20 | _ComputeShader.SetBuffer(0, "_StructuredBuffer", structuredBuffer); 21 | constantBuffer.SetData(new Element[]{new Element() {Index = 1, Radius = 2}}); 22 | _ComputeShader.Dispatch(0, 1, 1, 1 ); 23 | int[] result = new int[1]; 24 | structuredBuffer.GetData( result ); 25 | Debug.Log( "Result is: " + result[0] + ". Should be: " + ( 1 + 2 )); 26 | constantBuffer.Release(); 27 | structuredBuffer.Release(); 28 | } 29 | } -------------------------------------------------------------------------------- /DXR/PathTracing/PathTracing.raytrace: -------------------------------------------------------------------------------- 1 | #pragma max_recursion_depth 2 2 | 3 | RaytracingAccelerationStructure _AccelerationStructure; 4 | RWTexture2D _RenderTarget; 5 | float4 _AmbientColor; 6 | float4x4 _Frustum; 7 | float3 _WorldSpaceCameraPos; 8 | int _Frame; 9 | 10 | struct RayPayload 11 | { 12 | float3 color; 13 | uint seed; 14 | uint depth; 15 | }; 16 | 17 | uint Hash(uint2 p) 18 | { 19 | p = 1103515245U*((p >> 1U)^(p.yx)); 20 | uint h32 = 1103515245U*((p.x)^(p.y>>3U)); 21 | return h32^(h32 >> 16); 22 | } 23 | 24 | float Random(inout uint seed) 25 | { 26 | seed = 1664525u * seed + 1013904223u; 27 | return float(seed & 0x00FFFFFF) / float(0x01000000); 28 | } 29 | 30 | [shader("raygeneration")] 31 | void RayGenerationShader() 32 | { 33 | uint2 threadID = DispatchRaysIndex().xy; 34 | uint2 threadDim = DispatchRaysDimensions().xy; 35 | uint seed = Hash(uint2(threadID.x + threadID.y * threadDim.x, _Frame)); 36 | float2 uv = ((float2)threadID + float2(Random(seed), Random(seed))) / (float2)threadDim; 37 | float3 top = lerp(_Frustum[0].xyz, _Frustum[1].xyz, uv.x); 38 | float3 bottom = lerp(_Frustum[2].xyz, _Frustum[3].xyz, uv.x); 39 | float3 rayOrigin = _WorldSpaceCameraPos; 40 | float3 rayDirection = normalize(lerp(bottom, top, uv.y)); 41 | RayDesc ray = {rayOrigin, 0, rayDirection, 100}; 42 | RayPayload payload = {float3(0.0, 0.0, 0.0), seed, 0}; 43 | TraceRay(_AccelerationStructure, 0, 0x0f, 0, 1, 0, ray, payload); 44 | _RenderTarget[threadID] = float4(payload.color, 1.0); 45 | } 46 | 47 | [shader("miss")] 48 | void MissShader(inout RayPayload payload : SV_RayPayload) 49 | { 50 | payload.color = _AmbientColor; 51 | } -------------------------------------------------------------------------------- /DXR/PathTracing/ProgressiveRendering.shader: -------------------------------------------------------------------------------- 1 | Shader "ProgressiveRendering" 2 | { 3 | SubShader 4 | { 5 | Cull Off ZWrite Off ZTest Always 6 | Pass 7 | { 8 | CGPROGRAM 9 | #pragma vertex VSMain 10 | #pragma fragment PSMain 11 | 12 | sampler2D _MainImage; 13 | sampler2D _Accumulation; 14 | int _Frame; 15 | 16 | void VSMain (inout float4 vertex : POSITION, inout float2 uv : TEXCOORD0) 17 | { 18 | vertex = UnityObjectToClipPos(vertex); 19 | } 20 | 21 | void PSMain (float4 vertex : POSITION, float2 uv : TEXCOORD0, out float4 fragColor : SV_Target) 22 | { 23 | float4 currentFrame = tex2D(_MainImage, uv); 24 | float4 accumulation = tex2D(_Accumulation, uv); 25 | fragColor = (accumulation * _Frame + currentFrame) / (_Frame + 1); 26 | } 27 | ENDCG 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /DecalPainter.shader: -------------------------------------------------------------------------------- 1 | Shader "DecalPainter" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | ZTest Off 8 | ZWrite Off 9 | Cull Off 10 | CGPROGRAM 11 | #pragma vertex VSMain 12 | #pragma fragment PSMain 13 | 14 | float4x4 _MeshModelMatrix, _DecalViewMatrix, _DecalProjectionMatrix; 15 | sampler2D _DecalTexture; 16 | 17 | void VSMain (inout float4 vertex:POSITION, inout float2 uv:TEXCOORD0, out float4 worldPos:TEXCOORD1, out float4 clipPos:TEXCOORD2) 18 | { 19 | worldPos = mul(_MeshModelMatrix, vertex); 20 | clipPos = mul(_DecalViewMatrix, worldPos); 21 | clipPos = mul(_DecalProjectionMatrix, clipPos); 22 | float2 texcoord = uv.xy; 23 | texcoord.y = 1.0 - texcoord.y; 24 | texcoord = texcoord * 2.0 - 1.0; 25 | vertex = float4(texcoord, 0.0, 1.0); 26 | } 27 | 28 | float4 PSMain (float4 vertex:POSITION, float2 uv:TEXCOORD0, float4 worldPos:TEXCOORD1, float4 clipPos:TEXCOORD2) : SV_TARGET 29 | { 30 | float2 tc = float2( clipPos.x / clipPos.w, clipPos.y / clipPos.w) / 2.0 + 0.5; 31 | return (tc.x > 0.0 && tc.x < 1.0 && tc.y > 0.0 && tc.y < 1.0) ? tex2D(_DecalTexture, tc) : float4(1, 0, 0, 1); 32 | } 33 | ENDCG 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /DepthBufferToWorldSpace.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class DepthBufferToWorldSpace : MonoBehaviour 4 | { 5 | public Shader DepthBufferToWorldSpaceShader; 6 | 7 | private Camera _MainCamera; 8 | private Material _Material; 9 | 10 | void Start() 11 | { 12 | _Material = new Material(DepthBufferToWorldSpaceShader); 13 | _MainCamera = GetComponent(); 14 | _MainCamera.depthTextureMode = DepthTextureMode.Depth; 15 | } 16 | 17 | void OnRenderImage (RenderTexture source, RenderTexture destination) 18 | { 19 | Matrix4x4 m = GL.GetGPUProjectionMatrix(_MainCamera.projectionMatrix, false); 20 | m[2, 3] = m[3, 2] = 0.0f; m[3, 3] = 1.0f; 21 | Matrix4x4 ProjectionToWorld = Matrix4x4.Inverse(m * _MainCamera.worldToCameraMatrix) * Matrix4x4.TRS(new Vector3(0, 0, -m[2,2]), Quaternion.identity, Vector3.one); 22 | _Material.SetMatrix("unity_ProjectionToWorld", ProjectionToWorld); 23 | Graphics.Blit (source, destination, _Material); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /DepthBufferToWorldSpace.shader: -------------------------------------------------------------------------------- 1 | Shader "DepthBufferToWorldSpace" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex VSMain 9 | #pragma fragment PSMain 10 | #pragma target 5.0 11 | 12 | float4x4 unity_ProjectionToWorld; 13 | sampler2D _CameraDepthTexture; 14 | 15 | void VSMain (inout float4 vertex:POSITION, inout float2 uv:TEXCOORD0, out float3 direction:TEXCOORD1) 16 | { 17 | vertex = UnityObjectToClipPos(vertex); 18 | direction = mul(unity_ProjectionToWorld, float4(vertex.xy, 0.0, 1.0)) - _WorldSpaceCameraPos; 19 | } 20 | 21 | void PSMain (float4 vertex:POSITION, float2 uv:TEXCOORD0, float3 direction:TEXCOORD1, out float4 fragColor:SV_TARGET) 22 | { 23 | float depth = 1.0 / (_ZBufferParams.z * tex2D(_CameraDepthTexture, uv.xy) + _ZBufferParams.w); 24 | float3 worldspace = direction * depth + _WorldSpaceCameraPos; 25 | fragColor = float4(worldspace, 1.0); 26 | } 27 | 28 | ENDCG 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /DrawCircles.cs: -------------------------------------------------------------------------------- 1 | // Add script to camera and assign shader "DrawCircles". 2 | // Script renders 2048 circles with single draw call and their center coordinates 3 | // are calculated once on the CPU and sent to GPU array. 4 | // To use more than 2048 circles, you can use structured buffer, bake point data to texture, 5 | // or just calculate circles center coordinates procedurally (directly inside vertex shader). 6 | using UnityEngine; 7 | 8 | public class DrawCircles : MonoBehaviour 9 | { 10 | public Shader shader; 11 | protected Material material; 12 | 13 | void Awake() 14 | { 15 | material = new Material(shader); 16 | float[] bufferX = new float[2048]; 17 | float[] bufferY = new float[2048]; 18 | for (int i=0; i<2048; i++) 19 | { 20 | bufferX[i] = Random.Range(0.0f, 120.0f); 21 | bufferY[i] = Random.Range(0.0f, 120.0f); 22 | } 23 | material.SetFloatArray("BufferX", bufferX); 24 | material.SetFloatArray("BufferY", bufferY); 25 | } 26 | 27 | void OnRenderObject() 28 | { 29 | material.SetPass(0); 30 | Graphics.DrawProcedural(MeshTopology.Triangles, 6, 2048); 31 | } 32 | } -------------------------------------------------------------------------------- /DrawCircles.shader: -------------------------------------------------------------------------------- 1 | Shader "Draw Circles" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | Cull Off 8 | CGPROGRAM 9 | #pragma vertex VSMain 10 | #pragma fragment PSMain 11 | #pragma target 5.0 12 | 13 | float BufferX[2048]; 14 | float BufferY[2048]; 15 | 16 | float mod(float x, float y) 17 | { 18 | return x - y * floor(x/y); 19 | } 20 | 21 | float3 hash(float p) 22 | { 23 | float3 p3 = frac(p.xxx * float3(.1239, .1237, .2367)); 24 | p3 += dot(p3, p3.yzx+63.33); 25 | return frac((p3.xxy+p3.yzz)*p3.zyx); 26 | } 27 | 28 | float4 VSMain (uint id:SV_VertexID, out float2 uv:TEXCOORD0, inout uint instance:SV_INSTANCEID) : SV_POSITION 29 | { 30 | float3 center = float3(BufferX[instance], 0.0, BufferY[instance]); 31 | float u = mod(float(id),2.0); 32 | float v = sign(mod(126.0,mod(float(id),6.0)+6.0)); 33 | uv = float2(u,v); 34 | return UnityObjectToClipPos(float4(float3(sign(u)-0.5, 0.0, sign(v)-0.5) + center,1.0)); 35 | } 36 | 37 | float4 PSMain (float4 vertex:SV_POSITION, float2 uv:TEXCOORD0, uint instance:SV_INSTANCEID) : SV_Target 38 | { 39 | float2 S = uv*2.0-1.0; 40 | if (dot(S.xy, S.xy) > 1.0) discard; 41 | return float4(hash(float(instance)), 1.0); 42 | } 43 | ENDCG 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /DrawCubes.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class DrawCubes : MonoBehaviour 4 | { 5 | public Shader shader; 6 | protected Material material; 7 | 8 | void Start() 9 | { 10 | material = new Material(shader); 11 | } 12 | 13 | void OnRenderObject() 14 | { 15 | material.SetPass(0); 16 | Graphics.DrawProcedural(MeshTopology.Triangles, 36 * 100000, 1); 17 | } 18 | } -------------------------------------------------------------------------------- /DrawSphere.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class DrawSphere : MonoBehaviour 4 | { 5 | public Shader shader; 6 | protected Material material; 7 | 8 | void Start() 9 | { 10 | material = new Material(shader); 11 | } 12 | 13 | void OnRenderObject() 14 | { 15 | material.SetPass(0); 16 | Graphics.DrawProcedural(MeshTopology.Triangles, 12288, 1); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DrawSphere.shader: -------------------------------------------------------------------------------- 1 | // Draws full procedural sphere with per-face lighting 2 | Shader "Draw Sphere" 3 | { 4 | Subshader 5 | { 6 | Pass 7 | { 8 | Cull Off 9 | CGPROGRAM 10 | #pragma vertex VSMain 11 | #pragma fragment PSMain 12 | #pragma target 5.0 13 | 14 | float4 VSMain (uint id:SV_VertexID, out float3 p:TEXCOORD0) : SV_POSITION 15 | { 16 | float f = id; 17 | float v = f - 6.0 * floor(f/6.0); 18 | f = (f - v) / 6.; 19 | float a = f - 64.0 * floor(f/64.0); 20 | f = (f-a) / 64.; 21 | float b = f-16.; 22 | a += (v - 2.0 * floor(v/2.0)); 23 | b += v==2. || v>=4. ? 1.0 : 0.0; 24 | a = a/64.*6.28318; 25 | b = b/64.*6.28318; 26 | p = float3(cos(a)*cos(b), sin(b), sin(a)*cos(b)); 27 | return UnityObjectToClipPos(float4(p, 1.0)); 28 | } 29 | 30 | float4 PSMain (float4 s:SV_POSITION, float3 p:TEXCOORD0) : SV_Target 31 | { 32 | float3 dx = ddx_fine( p ); 33 | float3 dy = ddy_fine( p ); 34 | float3 light1 = normalize(float3(5,0,100)); 35 | float3 light2 = normalize(float3(-100,0,-102)); 36 | float3 light3 = normalize(float3(100,0,-100)); 37 | float3 normal = normalize(cross(dx,dy)); 38 | float3 diffuse1 = max(dot(light1,normal),0.0) * float3(0.9,0.0,0.0); 39 | float3 diffuse2 = max(dot(light2,normal),0.0) * float3(0.0,0.9,0.0); 40 | float3 diffuse3 = max(dot(light3,normal),0.0) * float3(0.0,0.0,1.0); 41 | return float4(diffuse1+diffuse2+diffuse3,1.0); 42 | } 43 | ENDCG 44 | } 45 | 46 | } 47 | } -------------------------------------------------------------------------------- /FLD/Readme.md: -------------------------------------------------------------------------------- 1 | Fisher Linear Discriminant. 2 | -------------------------------------------------------------------------------- /FLD/fld_gpu.compute: -------------------------------------------------------------------------------- 1 | //calculate Fisher Linear Discriminant 2 | 3 | #pragma kernel CSMain 4 | 5 | StructuredBuffer acer ; //read-only buffer 6 | StructuredBuffer quercus ; 7 | RWStructuredBuffer fisher ; //read-write buffer 8 | 9 | [numthreads(64,1,1)] // make 64 threads 10 | void CSMain (int3 id : SV_GroupThreadID) // id variable is thread index, every thread works parallel 11 | { 12 | float sredniaA = 0.0; //arithmetic mean A 13 | float sredniaB = 0.0; //arithmetic mean B 14 | float odchA = 0.0; //standard deviation A 15 | float odchB = 0.0; //standard deviation B 16 | 17 | for (int zz = 0; zz < 176; zz++) 18 | { 19 | sredniaA += acer[id.x+zz*64]; 20 | } 21 | sredniaA = sredniaA / 176.0; 22 | 23 | for (int zzz = 0; zzz < 176; zzz++) 24 | { 25 | odchA += ( (acer [id.x+zzz*64] - sredniaA) * (acer [id.x+zzz*64] - sredniaA)); 26 | } 27 | odchA /= 176.0; 28 | odchA = sqrt(odchA); 29 | 30 | for (int zzzz = 0; zzzz < 608; zzzz++) 31 | { 32 | sredniaB += quercus [id.x+zzzz*64]; 33 | } 34 | sredniaB = sredniaB / 608.0; 35 | 36 | for (int zzzzz = 0; zzzzz < 608; zzzzz++) 37 | { 38 | odchB += ( (quercus [id.x+zzzzz*64] - sredniaB) * (quercus [id.x+zzzzz*64] - sredniaB)); 39 | } 40 | odchB /= 608.0; 41 | odchB = sqrt(odchB); 42 | 43 | fisher [id.x] = (abs (sredniaA - sredniaB)) / (odchA + odchB); 44 | } 45 | -------------------------------------------------------------------------------- /FogOfWar.shader: -------------------------------------------------------------------------------- 1 | Shader "Hidden/FogOfWar" 2 | { 3 | SubShader 4 | { 5 | 6 | CGINCLUDE 7 | #pragma vertex SetVertexShader 8 | #pragma fragment SetPixelShader 9 | 10 | void SetVertexShader (inout float4 vertex:POSITION, inout float2 uv:TEXCOORD0) 11 | { 12 | vertex = UnityObjectToClipPos(vertex); 13 | } 14 | 15 | ENDCG 16 | 17 | Pass 18 | { 19 | CGPROGRAM 20 | float4 Center; 21 | float Radius; 22 | sampler2D _MainTex; 23 | 24 | float circle(float2 p, float2 c, float r) 25 | { 26 | return step(length(p-c)-r,0.0); 27 | } 28 | 29 | void SetPixelShader (float4 vertex:POSITION, float2 uv:TEXCOORD0, out float color:SV_TARGET) 30 | { 31 | color = max(circle(uv,Center.xy,Radius), tex2D(_MainTex,uv).r); 32 | } 33 | 34 | ENDCG 35 | } 36 | 37 | Pass 38 | { 39 | CGPROGRAM 40 | sampler2D _Buffer, _Map; 41 | 42 | void SetPixelShader (float4 vertex:POSITION, float2 uv:TEXCOORD0, out float4 color:SV_TARGET) 43 | { 44 | color = tex2D(_Buffer,uv).r > 0.0 ? tex2D(_Map,uv) : float4(0,0,0,1); 45 | } 46 | 47 | ENDCG 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /GLSL/Readme.md: -------------------------------------------------------------------------------- 1 | Unity3D GLSL shader programs. 2 | Enable OpenGL in Unity3D -> set -force-glcore parameter to editor executable, for instance: 3 | "C:\Program Files\Unity\Editor\Unity.exe" -force-glcore 4 | -------------------------------------------------------------------------------- /GLSL/divide_screen_effect.shader: -------------------------------------------------------------------------------- 1 | //https://www.shadertoy.com/view/XlfyWB 2 | Shader "Divide Screen Effect" 3 | { 4 | SubShader 5 | { 6 | Pass 7 | { 8 | GLSLPROGRAM 9 | 10 | uniform vec4 _Time; 11 | 12 | #ifdef VERTEX 13 | varying vec2 texcoord; 14 | void main() 15 | { 16 | gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; 17 | texcoord = gl_MultiTexCoord0.xy; 18 | } 19 | #endif 20 | 21 | const float lineScale = 40.0; 22 | 23 | float Color(vec2 pos) 24 | { 25 | float radius = atan(pos.x, pos.y); 26 | float num = abs(pos.x) + abs(pos.y); 27 | float line = floor(num * lineScale); 28 | float speed = sin(line * 24.3); 29 | float offset = fract(sin(speed)); 30 | return step(.4,tan(radius+offset+speed*(_Time.y+5.0)))*offset; 31 | } 32 | 33 | #ifdef FRAGMENT 34 | varying vec2 texcoord; 35 | void main() 36 | { 37 | vec2 uv = 2.0*texcoord-1.0; 38 | gl_FragColor = vec4(Color(uv),Color(uv-0.003),Color(uv),1.0); 39 | } 40 | #endif 41 | 42 | ENDGLSL 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /GLSL/geometry_shader_01.shader: -------------------------------------------------------------------------------- 1 | //reference: http://www.geeks3d.com/20111111/simple-introduction-to-geometry-shaders-glsl-opengl-tutorial-part1/ 2 | //Minimal exercise. You should see blue color. 3 | //Pass-through geometry shader sends the input primitives (a triangle) to the rasterizer without transformation. 4 | 5 | Shader "Geometry Shader #01" 6 | { 7 | SubShader 8 | { 9 | Pass 10 | { 11 | GLSLPROGRAM 12 | 13 | #ifdef VERTEX 14 | void main() 15 | { 16 | gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex; 17 | } 18 | #endif 19 | 20 | #ifdef GEOMETRY 21 | layout(triangles) in; 22 | layout(triangle_strip, max_vertices=3) out; 23 | void main() 24 | { 25 | for(int i=0; i<3; i++) 26 | { 27 | gl_Position = gl_in[i].gl_Position; 28 | EmitVertex(); 29 | } 30 | EndPrimitive(); 31 | } 32 | #endif 33 | 34 | #ifdef FRAGMENT 35 | out vec4 color; 36 | void main() 37 | { 38 | color = vec4(0.0, 0.0, 1.0, 1.0); 39 | } 40 | #endif 41 | 42 | ENDGLSL 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /GLSL/quicksort_input.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/GLSL/quicksort_input.tga -------------------------------------------------------------------------------- /GLSL/quicksort_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/GLSL/quicksort_output.png -------------------------------------------------------------------------------- /GeometryShaders/Cloner.shader: -------------------------------------------------------------------------------- 1 | Shader "Cloner" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "black" {} 6 | _Offset ("Offset", Vector) = (2,0,0,0) 7 | } 8 | SubShader 9 | { 10 | Cull Off 11 | Pass 12 | { 13 | CGPROGRAM 14 | #pragma vertex VSMain 15 | #pragma geometry GSMain 16 | #pragma fragment PSMain 17 | #pragma target 5.0 18 | 19 | sampler2D _MainTex; 20 | float4 _Offset; 21 | 22 | struct Structure 23 | { 24 | float4 vertex : SV_Position; 25 | float2 uv : TEXCOORD0; 26 | }; 27 | 28 | Structure VSMain(float4 vertex:POSITION, float2 uv:TEXCOORD0) 29 | { 30 | Structure VS; 31 | VS.uv = uv; 32 | VS.vertex = vertex; 33 | return VS; 34 | } 35 | 36 | [maxvertexcount(6)] 37 | void GSMain( triangle Structure patch[3], inout TriangleStream stream ) 38 | { 39 | Structure GS; 40 | for (uint i = 0; i < 3; i++) 41 | { 42 | GS.vertex = UnityObjectToClipPos(patch[i].vertex); 43 | GS.uv = patch[i].uv; 44 | stream.Append(GS); 45 | } 46 | stream.RestartStrip(); 47 | for (uint k = 0; k < 3; k++) 48 | { 49 | GS.vertex = UnityObjectToClipPos(patch[k].vertex + _Offset); 50 | GS.uv = patch[k].uv; 51 | stream.Append(GS); 52 | } 53 | stream.RestartStrip(); 54 | } 55 | 56 | float4 PSMain(Structure PS) : SV_Target 57 | { 58 | return tex2D(_MainTex,PS.uv); 59 | } 60 | ENDCG 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /GeometryShaders/Wireframe.shader: -------------------------------------------------------------------------------- 1 | Shader "Wireframe (Geometry Shader)" 2 | { 3 | SubShader 4 | { 5 | Tags { "RenderType" = "Transparent" "Queue" = "Transparent" } 6 | Pass 7 | { 8 | Blend SrcAlpha OneMinusSrcAlpha 9 | CGPROGRAM 10 | #pragma vertex VSMain 11 | #pragma geometry GSMain 12 | #pragma fragment PSMain 13 | #pragma target 5.0 14 | 15 | struct Data 16 | { 17 | float4 vertex : SV_Position; 18 | float2 barycentric : BARYCENTRIC; 19 | }; 20 | 21 | void VSMain(inout float4 vertex:POSITION) { } 22 | 23 | [maxvertexcount(3)] 24 | void GSMain( triangle float4 patch[3]:SV_Position, inout TriangleStream stream) 25 | { 26 | Data GS; 27 | for (uint i = 0; i < 3; i++) 28 | { 29 | GS.vertex = UnityObjectToClipPos(patch[i]); 30 | GS.barycentric = float2(fmod(i,2.0), step(2.0,i)); 31 | stream.Append(GS); 32 | } 33 | stream.RestartStrip(); 34 | } 35 | 36 | float4 PSMain(Data PS) : SV_Target 37 | { 38 | float3 coord = float3(PS.barycentric, 1.0 - PS.barycentric.x - PS.barycentric.y); 39 | coord = smoothstep(fwidth(coord)*0.1, fwidth(coord)*0.1 + fwidth(coord), coord); 40 | return float4(0..xxx, 1.0 - min(coord.x, min(coord.y, coord.z))); 41 | } 42 | ENDCG 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /HDR/EyeHistogram.compute: -------------------------------------------------------------------------------- 1 | #define HISTOGRAM_BINS 64 2 | #define HISTOGRAM_TEXELS HISTOGRAM_BINS / 4 3 | #define HISTOGRAM_THREAD_X 16 4 | #define HISTOGRAM_THREAD_Y 16 5 | 6 | RWStructuredBuffer _Histogram; 7 | Texture2D _Source; 8 | float4 _ScaleOffsetRes; 9 | groupshared uint gs_histogram[HISTOGRAM_BINS]; 10 | 11 | #pragma kernel KEyeHistogram 12 | [numthreads(HISTOGRAM_THREAD_X,HISTOGRAM_THREAD_Y,1)] 13 | void KEyeHistogram(uint2 dispatchThreadId : SV_DispatchThreadID, uint2 groupThreadId : SV_GroupThreadID) 14 | { 15 | const uint localThreadId = groupThreadId.y * HISTOGRAM_THREAD_X + groupThreadId.x; 16 | if (localThreadId < HISTOGRAM_BINS) gs_histogram[localThreadId] = 0u; 17 | GroupMemoryBarrierWithGroupSync(); 18 | if (dispatchThreadId.x < (uint)_ScaleOffsetRes.z && dispatchThreadId.y < (uint)_ScaleOffsetRes.w) 19 | { 20 | uint weight = 1u; 21 | float3 color = _Source[dispatchThreadId].xyz; 22 | float luminance = max(color.x, max(color.y, color.z)); 23 | float logLuminance = saturate(log2(luminance) * _ScaleOffsetRes.x + _ScaleOffsetRes.y); 24 | uint idx = (uint)(logLuminance * (HISTOGRAM_BINS - 1u)); 25 | InterlockedAdd(gs_histogram[idx], weight); 26 | } 27 | GroupMemoryBarrierWithGroupSync(); 28 | if (localThreadId < HISTOGRAM_BINS) InterlockedAdd(_Histogram[localThreadId], gs_histogram[localThreadId]); 29 | } -------------------------------------------------------------------------------- /HDR/readme.md: -------------------------------------------------------------------------------- 1 | HDR Bloom and Eye Adaptation (Automatic Exposure). 2 | 3 | Based on Unity Post Processing Stack. 4 | 5 | Compact code prepared for better understanding algorithm. 6 | 7 | Set Options: Rendering Path: Deferred, Allow HDR = True, Linear Color Space 8 | -------------------------------------------------------------------------------- /I.shader: -------------------------------------------------------------------------------- 1 | //620 chars 2 | //Apply material with shader to quad. 3 | //Compiled in Unity 2018.1.0f2 4 | //Author: Przemyslaw Zaworski 5 | 6 | Shader "I" 7 | { 8 | Subshader 9 | { 10 | Pass 11 | { 12 | Cull Off 13 | CGPROGRAM 14 | #pragma vertex V 15 | #pragma fragment P 16 | 17 | #define l lerp 18 | 19 | half k(half3 x) 20 | { 21 | half3 p=x-frac(x), f=x-p, n={1,0,0}, t={1,9,57}; f*=f*(3-2*f); 22 | #define h(m) frac(cos(dot(m,t))*1e5) 23 | return l(l(l(h(p),h(p+n.xyy),f.x),l(h(p+n.yxy),h(p+n.xxy),f.x),f.y),l(l(h(p+n.yyx),h(p+n.xyx),f.x),l(h(p+n.yxx),h(p+1),f.x),f.y),f.z); 24 | } 25 | 26 | void V(uint i:SV_VertexID,out half4 c:POSITION) {c=half4((i<<1&2)*2-1.,1-2.*(i&2),1,1);} 27 | 28 | void P(half4 u:POSITION,out half s:COLOR) 29 | { 30 | u = half4(9*u.xy/_ScreenParams,_Time.g,0); 31 | for (half i;i<1;i+=.02,u.y-=.1,u.w=(k(u)+k(u+9.5))/2,s=l(s,u.w,smoothstep(0,1,(u.w-i)/fwidth(u.w)))){} 32 | } 33 | 34 | ENDCG 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /ImageToSDFGenerator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using System.IO; 5 | 6 | public class ImageToSDFGenerator : MonoBehaviour 7 | { 8 | [SerializeField] private Collider2D _Collider2D; 9 | [SerializeField] private int _Resolution = 1024; 10 | 11 | void Start() 12 | { 13 | Texture2D texture = new Texture2D(_Resolution, _Resolution, TextureFormat.RFloat, -1, true); 14 | for (int y = 0; y < texture.height; y++) 15 | { 16 | for (int x = 0; x < texture.width; x++) 17 | { 18 | Vector2 position = new Vector2(x, y); 19 | Vector2 closestPoint = _Collider2D.ClosestPoint(position); 20 | float distance = Vector2.Distance(closestPoint, position); 21 | texture.SetPixel(x, y, new Color(distance, distance, distance, distance)); 22 | } 23 | } 24 | texture.Apply(); 25 | byte[] bytes = ImageConversion.EncodeToEXR(texture, Texture2D.EXRFlags.OutputAsFloat); 26 | File.WriteAllBytes(Application.dataPath + "/../texture.exr", bytes); 27 | Object.Destroy(texture); 28 | } 29 | } -------------------------------------------------------------------------------- /MarchingCubes/Readme.md: -------------------------------------------------------------------------------- 1 | Convert scalar field to triangle mesh. 2 | 3 | Add script to gameobject and assign public variables. Set camera position (0, 0, -15). 4 | 5 | 3D SDF function definition: file ScalarField.compute, function "Map". -------------------------------------------------------------------------------- /MeshShaderRTX/MeshShaderPlugin.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Collections; 4 | using System.Runtime.InteropServices; 5 | 6 | public class MeshShaderPlugin : MonoBehaviour 7 | { 8 | [DllImport("MeshShaderPlugin")] 9 | static extern IntPtr Execute(); 10 | 11 | IEnumerator Start() 12 | { 13 | yield return StartCoroutine("CallNativePlugin"); 14 | } 15 | 16 | IEnumerator CallNativePlugin() 17 | { 18 | while (true) 19 | { 20 | yield return new WaitForEndOfFrame(); 21 | GL.IssuePluginEvent(Execute(), 1); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /MeshShaderRTX/MeshShaderPlugin.txt: -------------------------------------------------------------------------------- 1 | Simple example of NVIDIA RTX Mesh Shader working with Unity OpenGL 4.5; 2 | Enable OpenGL in Unity3D -> set -force-glcore parameter to editor executable, for instance: "C:\Program Files\Unity\Editor\Unity.exe" -force-glcore 3 | Compile MeshShaderPlugin.cpp with Visual Studio x64 command line: cl.exe /LD MeshShaderPlugin.cpp opengl32.lib 4 | Copy compiled DLL file to Assets/Plugins, reload project and include MeshShaderPlugin.cs to camera. Play. 5 | You should see a triangle coloured by barycentric coordinates. 6 | More info: https://devblogs.nvidia.com/introduction-turing-mesh-shaders/ -------------------------------------------------------------------------------- /Multipass/NoiseTexture.cs: -------------------------------------------------------------------------------- 1 | // Add script to quad and assign material with shader. Play. 2 | 3 | using UnityEngine; 4 | 5 | public class NoiseTexture : MonoBehaviour 6 | { 7 | public Material material; 8 | RenderTexture RT; 9 | int property; 10 | 11 | void Blit(RenderTexture destination, Material mat) 12 | { 13 | RenderTexture.active = destination; 14 | GL.PushMatrix(); 15 | GL.LoadOrtho(); 16 | GL.invertCulling = true; 17 | mat.SetPass(0); 18 | GL.Begin(GL.QUADS); 19 | GL.MultiTexCoord2(0, 0.0f, 0.0f); 20 | GL.Vertex3(0.0f, 0.0f, 0.0f); 21 | GL.MultiTexCoord2(0, 1.0f, 0.0f); 22 | GL.Vertex3(1.0f, 0.0f, 0.0f); 23 | GL.MultiTexCoord2(0, 1.0f, 1.0f); 24 | GL.Vertex3(1.0f, 1.0f, 0.0f); 25 | GL.MultiTexCoord2(0, 0.0f, 1.0f); 26 | GL.Vertex3(0.0f, 1.0f, 0.0f); 27 | GL.End(); 28 | GL.invertCulling = false; 29 | GL.PopMatrix(); 30 | } 31 | 32 | void Start () 33 | { 34 | RT = new RenderTexture(256, 256, 0, RenderTextureFormat.ARGBFloat); 35 | RT.filterMode = FilterMode.Point; 36 | GetComponent().material = material; 37 | property = Shader.PropertyToID("_BufferA"); 38 | } 39 | 40 | void Update () 41 | { 42 | Blit(RT, material); 43 | material.SetTexture(property, RT); 44 | } 45 | 46 | void OnDestroy () 47 | { 48 | RT.Release(); 49 | } 50 | } -------------------------------------------------------------------------------- /Multipass/info.txt: -------------------------------------------------------------------------------- 1 | Advanced multipass shaders. 2 | -------------------------------------------------------------------------------- /NurbsSurface.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class NurbsSurface : MonoBehaviour 4 | { 5 | public Shader NurbsSurfaceShader; 6 | [Range(1, 1024)] public int TessellationFactor = 64; 7 | public enum Mode {SurfaceDerivatives = 0, ScreenSpaceDerivatives = 1} 8 | public Mode NormalsCalculation = Mode.SurfaceDerivatives; 9 | private Material _Material; 10 | 11 | void Start() 12 | { 13 | if (NurbsSurfaceShader == null) NurbsSurfaceShader = Shader.Find("Nurbs Surface"); 14 | _Material = new Material(NurbsSurfaceShader); 15 | } 16 | 17 | void OnRenderObject() 18 | { 19 | _Material.SetInt("_TessellationFactor", TessellationFactor); 20 | _Material.SetInt("_NormalsMode", (int)NormalsCalculation); 21 | _Material.SetPass(0); 22 | int vertexCount = TessellationFactor * TessellationFactor * 6; 23 | Graphics.DrawProceduralNow(MeshTopology.Triangles, vertexCount, 1); 24 | } 25 | } -------------------------------------------------------------------------------- /ParticleCollision.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class ParticleCollision : MonoBehaviour 4 | { 5 | public ComputeShader shader; 6 | public Material material; 7 | public int resolution = 1024; 8 | public int amount = 200; 9 | private ComputeBuffer buffer; 10 | private int counter = 0; 11 | 12 | void Start () 13 | { 14 | buffer = new ComputeBuffer(resolution*resolution, sizeof(float)*4, ComputeBufferType.Default); 15 | shader.SetBuffer(0, "buffer", buffer); 16 | material.SetBuffer("buffer", buffer); 17 | material.SetInt("amount",amount); 18 | shader.SetInt("amount",amount); 19 | } 20 | 21 | void Update () 22 | { 23 | material.SetInt("resolution",resolution); 24 | shader.SetInt("iFrame",counter); 25 | shader.SetFloat("iTimeDelta",Time.deltaTime); 26 | shader.SetInt("resolution",resolution); 27 | shader.Dispatch(0, resolution / 16, resolution / 16, 1); 28 | counter++; 29 | } 30 | 31 | void OnDestroy() 32 | { 33 | buffer.Release(); 34 | } 35 | } -------------------------------------------------------------------------------- /ParticleCollision.shader: -------------------------------------------------------------------------------- 1 | Shader "Particle Collision" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex SetVertexShader 9 | #pragma fragment SetPixelShader 10 | #pragma target 5.0 11 | 12 | uniform StructuredBuffer buffer; 13 | uniform int resolution; 14 | uniform int amount; 15 | 16 | void SetVertexShader (inout float4 vertex:POSITION, inout float2 uv:TEXCOORD0) 17 | { 18 | vertex = UnityObjectToClipPos(vertex); 19 | } 20 | 21 | void SetPixelShader (float4 vertex:POSITION, float2 uv:TEXCOORD0, out float4 color:SV_TARGET) 22 | { 23 | float2 texel = float2(round(uv.x*resolution),round(uv.y*resolution)); 24 | color = 0..xxxx; 25 | for (int i=0; i().sharedMesh; 22 | n = mesh.triangles.Length; 23 | Point[] points = new Point[n]; 24 | for (int i = 0; i < n; ++i) 25 | { 26 | points[i].vertex = mesh.vertices[mesh.triangles[i]]; 27 | points[i].normal = mesh.normals[mesh.triangles[i]]; 28 | points[i].tangent = mesh.tangents[mesh.triangles[i]]; 29 | points[i].uv = mesh.uv [mesh.triangles [i]]; 30 | } 31 | computebuffer= new ComputeBuffer (n, Marshal.SizeOf(typeof(Point)), ComputeBufferType.Default); 32 | computebuffer.SetData (points); 33 | material.SetBuffer ("points", computebuffer); 34 | } 35 | 36 | void OnRenderObject() 37 | { 38 | material.SetPass(0); 39 | Graphics.DrawProcedural(MeshTopology.Triangles, n, 1); 40 | } 41 | 42 | void OnDestroy() 43 | { 44 | computebuffer.Release (); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /ProceduralGeometry.shader: -------------------------------------------------------------------------------- 1 | Shader "ProceduralGeometry" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "white" {} 6 | } 7 | SubShader 8 | { 9 | Pass 10 | { 11 | CGPROGRAM 12 | #include "UnityCG.cginc" 13 | #pragma target 5.0 14 | #pragma vertex vertex_shader 15 | #pragma fragment pixel_shader 16 | 17 | sampler2D _MainTex; 18 | float4 _MainTex_ST; 19 | float4 _LightColor0; 20 | 21 | struct Point 22 | { 23 | float3 vertex; 24 | float3 normal; 25 | float4 tangent; 26 | float2 uv; 27 | }; 28 | 29 | StructuredBuffer points; 30 | 31 | struct custom_type 32 | { 33 | float4 position : SV_POSITION; 34 | float4 color : COLOR; 35 | float2 uv : TEXCOORD0; 36 | }; 37 | 38 | custom_type vertex_shader (uint id : SV_VertexID, uint inst : SV_InstanceID) 39 | { 40 | custom_type vs; 41 | float4 vertex_position = float4(points[id].vertex,1.0f); 42 | float4 vertex_normal = float4(points[id].normal, 1.0f); 43 | vertex_position.x+=sin(5.0*_Time.g); 44 | vs.position = mul (UNITY_MATRIX_VP, vertex_position); 45 | vs.uv = TRANSFORM_TEX(points[id].uv, _MainTex); 46 | float3 NormalDirection = normalize(vertex_normal.xyz); 47 | float4 AmbientLight = UNITY_LIGHTMODEL_AMBIENT; 48 | float4 LightDirection = normalize(_WorldSpaceLightPos0); 49 | vs.color = saturate(dot(LightDirection, NormalDirection))*_LightColor0+AmbientLight; 50 | return vs; 51 | } 52 | 53 | float4 pixel_shader (custom_type ps) : SV_Target 54 | { 55 | return tex2D(_MainTex, ps.uv)*ps.color; 56 | } 57 | 58 | ENDCG 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /ProceduralSkybox.shader: -------------------------------------------------------------------------------- 1 | Shader "Procedural Skybox" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex SetVertexShader 9 | #pragma fragment SetPixelShader 10 | 11 | void SetVertexShader (inout float4 Vertex:POSITION, out float3 Point:TEXCOORD0) 12 | { 13 | Point = mul(unity_ObjectToWorld, Vertex); // World Space coordinate 14 | Vertex = UnityObjectToClipPos (Vertex); // Screen Space coordinate 15 | } 16 | 17 | void SetPixelShader (float4 Vertex:POSITION, float3 Point:TEXCOORD0, out float4 Color:SV_TARGET) 18 | { 19 | float3 Direction = normalize(Point - _WorldSpaceCameraPos); //Direction vector 20 | Color = (Direction.y>0.0) ? Direction.yyyy : 0; //Pixel color 21 | } 22 | 23 | ENDCG 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /ProceduralSphereTessellation.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class ProceduralSphereTessellation : MonoBehaviour 4 | { 5 | public Shader ProceduralSphereTessellationShader; 6 | [Range(0f, 10f)] public float Radius = 2.0f; 7 | [Range(1, 256)] public int TessellationFactor = 64; 8 | private Material _Material; 9 | 10 | void Start() 11 | { 12 | _Material = new Material(ProceduralSphereTessellationShader); 13 | } 14 | 15 | void OnRenderObject() 16 | { 17 | _Material.SetPass(0); 18 | _Material.SetFloat("_Radius", Radius); 19 | _Material.SetFloat("_TessellationFactor", TessellationFactor); 20 | Graphics.DrawProceduralNow(MeshTopology.Triangles, TessellationFactor * TessellationFactor * 6, 1); 21 | } 22 | 23 | void OnDestroy() 24 | { 25 | Destroy(_Material); 26 | } 27 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Unity shader programming (HLSL & GLSL). 2 | 3 | Only for educational purposes. 4 | 5 | License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. 6 | -------------------------------------------------------------------------------- /ReShade/Cartoon.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Cartoon : MonoBehaviour 5 | { 6 | public Shader CartoonShader; 7 | public Camera MainCamera; 8 | 9 | [Range(0.1f, 10.0f)] public float Power = 1.5f; 10 | [Range(0.1f, 6.0f)] public float EdgeSlope = 1.5f; 11 | 12 | Material _Material; 13 | 14 | void Blit(RenderTexture source, RenderTexture destination, Material mat, int pass, string name) 15 | { 16 | RenderTexture.active = destination; 17 | mat.SetTexture(name, source); 18 | GL.PushMatrix(); 19 | GL.LoadOrtho(); 20 | GL.invertCulling = true; 21 | mat.SetPass(pass); 22 | GL.Begin(GL.QUADS); 23 | GL.MultiTexCoord2(0, 0.0f, 0.0f); 24 | GL.Vertex3(0.0f, 0.0f, 0.0f); 25 | GL.MultiTexCoord2(0, 1.0f, 0.0f); 26 | GL.Vertex3(1.0f, 0.0f, 0.0f); 27 | GL.MultiTexCoord2(0, 1.0f, 1.0f); 28 | GL.Vertex3(1.0f, 1.0f, 0.0f); 29 | GL.MultiTexCoord2(0, 0.0f, 1.0f); 30 | GL.Vertex3(0.0f, 1.0f, 0.0f); 31 | GL.End(); 32 | GL.invertCulling = false; 33 | GL.PopMatrix(); 34 | } 35 | 36 | void Start () 37 | { 38 | _Material = new Material(CartoonShader); 39 | } 40 | 41 | void Update () 42 | { 43 | _Material.SetFloat("Power", Power); 44 | _Material.SetFloat("EdgeSlope", EdgeSlope); 45 | } 46 | 47 | void OnRenderImage (RenderTexture source, RenderTexture destination) 48 | { 49 | Blit (source, destination, _Material, 0, "BackBuffer"); 50 | } 51 | } -------------------------------------------------------------------------------- /ShaderDebugging.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class ShaderDebugging : MonoBehaviour 4 | { 5 | public GameObject target; 6 | 7 | private Material material; 8 | private ComputeBuffer buffer; 9 | private Vector4[] element; 10 | private string label; 11 | private Renderer render; 12 | 13 | void Load () 14 | { 15 | buffer = new ComputeBuffer(1, 16, ComputeBufferType.Default); 16 | element = new Vector4[1]; 17 | label = string.Empty; 18 | render = target.GetComponent(); 19 | material = render.material; 20 | } 21 | 22 | void Start () 23 | { 24 | Load(); 25 | } 26 | 27 | void Update () 28 | { 29 | Graphics.ClearRandomWriteTargets(); 30 | material.SetPass(0); 31 | material.SetBuffer("buffer", buffer); 32 | Graphics.SetRandomWriteTarget(1, buffer, false); 33 | buffer.GetData(element); 34 | label = (element!=null && render.isVisible) ? element[0].ToString("F3") : string.Empty; 35 | } 36 | 37 | void OnGUI() 38 | { 39 | GUIStyle style = new GUIStyle(); 40 | style.fontSize = 32; 41 | GUI.Label(new Rect(50, 50, 400, 100), label, style); 42 | } 43 | 44 | void OnDestroy() 45 | { 46 | buffer.Dispose(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /ShaderDebugging.shader: -------------------------------------------------------------------------------- 1 | Shader "Shader Debugging" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 4.5 11 | 12 | RWStructuredBuffer buffer : register(u1); 13 | 14 | void vertex_shader (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0) 15 | { 16 | vertex = UnityObjectToClipPos(vertex); 17 | } 18 | 19 | float4 pixel_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) : SV_TARGET 20 | { 21 | float4 p = float4(sin(_Time.g)*0.5+0.5,0.2,0.321,0.789); //set custom variable 22 | buffer[0] = p; //write value to buffer 23 | return lerp(0..xxxx, float4(uv,0,1), step(uv.x,p.x)); 24 | } 25 | ENDCG 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /ShaderReplacement/MainShader.shader: -------------------------------------------------------------------------------- 1 | Shader "ShaderReplacement/MainShader" 2 | { 3 | Properties 4 | { 5 | [Header(Texture Maps)] 6 | _MainTex ("Albedo Map", 2D) = "black" {} 7 | _BumpMap ("Normal Map", 2D) = "black" {} 8 | _SpecGlossMap ("Specular (RGB) Smoothness(A) Map", 2D) = "gray" {} 9 | } 10 | Subshader 11 | { 12 | Tags {"RenderType" = "CustomRendering"} 13 | CGPROGRAM 14 | #pragma surface SurfaceShader StandardSpecular fullforwardshadows addshadow 15 | 16 | sampler2D _MainTex,_BumpMap,_SpecGlossMap; 17 | 18 | struct Input 19 | { 20 | float2 uv_MainTex; 21 | float2 uv_BumpMap; 22 | float2 uv_SpecGlossMap; 23 | }; 24 | 25 | void SurfaceShader (Input IN, inout SurfaceOutputStandardSpecular o) 26 | { 27 | o.Albedo = tex2D(_MainTex,IN.uv_MainTex) ; 28 | o.Normal = UnpackNormal (tex2D(_BumpMap, IN.uv_BumpMap)); 29 | o.Specular = tex2D(_SpecGlossMap, IN.uv_SpecGlossMap).rgb; 30 | o.Smoothness = tex2D(_SpecGlossMap, IN.uv_SpecGlossMap).a; 31 | } 32 | ENDCG 33 | } 34 | } -------------------------------------------------------------------------------- /ShaderReplacement/SecondShader.shader: -------------------------------------------------------------------------------- 1 | Shader "ShaderReplacement/SecondShader" 2 | { 3 | SubShader 4 | { 5 | Tags {"RenderType" = "CustomRendering"} 6 | Pass 7 | { 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | 12 | void vertex_shader (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0) 13 | { 14 | vertex = UnityObjectToClipPos(vertex); 15 | } 16 | 17 | float4 pixel_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) : SV_TARGET 18 | { 19 | return float4(0,0,1,1); 20 | } 21 | ENDCG 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /ShaderReplacement/ShaderReplacement.cs: -------------------------------------------------------------------------------- 1 | //Author: Przemyslaw Zaworski 2 | //Assign material with MainShader to Parent GameObject. 3 | //Second shader will be rendered to render texture. 4 | using System.Collections; 5 | using System.Collections.Generic; 6 | using UnityEngine; 7 | 8 | public class ShaderReplacement : MonoBehaviour 9 | { 10 | public Shader SecondShader; 11 | public RenderTexture Output; 12 | public GameObject Parent; 13 | 14 | void Start () 15 | { 16 | GameObject Child = new GameObject(); 17 | Child.name = "SubCamera"; 18 | Child.transform.parent = Parent.transform; 19 | Child.transform.localPosition = new Vector3(0.0f,5.0f,0.0f); 20 | Child.transform.localEulerAngles=new Vector3(90.0f,0.0f,0.0f); 21 | Camera SubCamera = Child.AddComponent(); 22 | SubCamera.clearFlags = CameraClearFlags.SolidColor; 23 | SubCamera.backgroundColor = Color.black; 24 | SubCamera.orthographic = true; 25 | SubCamera.orthographicSize = 5.0f; 26 | SubCamera.renderingPath = RenderingPath.VertexLit; 27 | SubCamera.useOcclusionCulling = false; 28 | SubCamera.allowMSAA = false; 29 | SubCamera.allowHDR = false; 30 | SubCamera.targetTexture = Output; 31 | SubCamera.SetReplacementShader (SecondShader, "RenderType"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /ShaderReplacement/ShaderReplacement.renderTexture: -------------------------------------------------------------------------------- 1 | %YAML 1.1 2 | %TAG !u! tag:unity3d.com,2011: 3 | --- !u!84 &8400000 4 | RenderTexture: 5 | m_ObjectHideFlags: 0 6 | m_PrefabParentObject: {fileID: 0} 7 | m_PrefabInternal: {fileID: 0} 8 | m_Name: New Render Texture 9 | m_ImageContentsHash: 10 | serializedVersion: 2 11 | Hash: 00000000000000000000000000000000 12 | m_ForcedFallbackFormat: 4 13 | m_DownscaleFallback: 0 14 | m_Width: 256 15 | m_Height: 256 16 | m_AntiAliasing: 1 17 | m_DepthFormat: 2 18 | m_ColorFormat: 0 19 | m_MipMap: 0 20 | m_GenerateMips: 1 21 | m_SRGB: 0 22 | m_UseDynamicScale: 0 23 | m_BindMS: 0 24 | m_TextureSettings: 25 | serializedVersion: 2 26 | m_FilterMode: 1 27 | m_Aniso: 0 28 | m_MipBias: 0 29 | m_WrapU: 1 30 | m_WrapV: 1 31 | m_WrapW: 1 32 | m_Dimension: 2 33 | m_VolumeDepth: 1 34 | -------------------------------------------------------------------------------- /ShaderReplacement/readme.md: -------------------------------------------------------------------------------- 1 | References: 2 | https://docs.unity3d.com/Manual/SL-ShaderReplacement.html 3 | -------------------------------------------------------------------------------- /SquareRoot.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | 3 | RWStructuredBuffer _ComputeBuffer; 4 | 5 | double SquareRoot (double x) // Newton–Raphson method 6 | { 7 | double n = 1.0; 8 | [unroll(12)] for (int i = 1; i <= 12; i++) n -= (n * n - x) / (2.0 * n); 9 | return n; 10 | } 11 | 12 | [numthreads(1,1,1)] 13 | void CSMain (uint3 id : SV_DispatchThreadID) 14 | { 15 | _ComputeBuffer[id.x] = SquareRoot (461.0); 16 | } 17 | -------------------------------------------------------------------------------- /SquareRoot.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class SquareRoot : MonoBehaviour 4 | { 5 | public ComputeShader SquareRootCS; 6 | 7 | void Start() 8 | { 9 | ComputeBuffer computeBuffer = new ComputeBuffer (1, sizeof(double)); 10 | SquareRootCS.SetBuffer (0, "_ComputeBuffer", computeBuffer); 11 | SquareRootCS.Dispatch (0, 1, 1, 1); 12 | double[] result = new double[1]; 13 | computeBuffer.GetData (result); 14 | computeBuffer.Release(); 15 | Debug.Log("HLSL: " + result[0].ToString("F20")); 16 | Debug.Log("C# : " + System.Math.Sqrt(461.0).ToString("F20")); 17 | } 18 | } -------------------------------------------------------------------------------- /TerrainAssets/TerrainAssetsReadme.md: -------------------------------------------------------------------------------- 1 | Various terrain assets for testing purposes. 2 | Drag and drop to "Scene" view. 3 | -------------------------------------------------------------------------------- /TerrainAssets/Valley.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/TerrainAssets/Valley.asset -------------------------------------------------------------------------------- /TetrahedralMesh.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/TetrahedralMesh.bin -------------------------------------------------------------------------------- /TetrahedralMesh.shader: -------------------------------------------------------------------------------- 1 | Shader "Hidden/TetrahedralMesh" 2 | { 3 | Subshader 4 | { 5 | CGPROGRAM 6 | #pragma target 5.0 7 | #pragma surface SurfaceShaderMain Standard vertex:VertexShaderMain fullforwardshadows addshadow 8 | 9 | struct appdata 10 | { 11 | float4 vertex : POSITION; 12 | float3 normal : NORMAL; 13 | float4 tangent : TANGENT; 14 | float2 texcoord1 : TEXCOORD1; 15 | float2 texcoord2 : TEXCOORD2; 16 | uint id : SV_VertexID; 17 | uint instance : SV_InstanceID; 18 | }; 19 | 20 | #ifdef SHADER_API_D3D11 21 | StructuredBuffer _Vertices; 22 | #endif 23 | 24 | struct Input 25 | { 26 | float vface : VFACE; 27 | }; 28 | 29 | void VertexShaderMain (inout appdata v, out Input o) 30 | { 31 | #ifdef SHADER_API_D3D11 32 | uint i = (v.id / 3u) * 3u; 33 | v.vertex = (_Vertices[v.id].w > 0.5) ? _Vertices[v.id] : asfloat(0x7fc00000); 34 | v.normal = normalize(cross(_Vertices[i+1] - _Vertices[i+0], _Vertices[i+2] - _Vertices[i+1])); 35 | o.vface = 0; 36 | #endif 37 | } 38 | 39 | void SurfaceShaderMain (Input IN, inout SurfaceOutputStandard o) 40 | { 41 | o.Albedo = float4((float3) 0.5, 1.0); 42 | o.Normal = float3(0, 0, IN.vface < 0 ? -1 : 1); 43 | o.Metallic = 0.0; 44 | o.Smoothness = 0.0; 45 | } 46 | 47 | ENDCG 48 | } 49 | } -------------------------------------------------------------------------------- /TriangleStrip.cs: -------------------------------------------------------------------------------- 1 | // Render triangle strip as triangle list 2 | // github.com/przemyslawzaworski 3 | using UnityEngine; 4 | using System.Runtime.InteropServices; 5 | 6 | public class TriangleStrip : MonoBehaviour 7 | { 8 | public Shader TriangleStripShader; 9 | 10 | private ComputeBuffer _ComputeBuffer; 11 | private Material _Material; 12 | private int _Count; 13 | 14 | // https://learn.microsoft.com/en-us/windows/win32/direct3d9/triangle-strips 15 | Vector3[] _Vertices = new Vector3[6] 16 | { 17 | new Vector3(-5.0f, -5.0f, 0.0f), 18 | new Vector3( 0.0f, 5.0f, 0.0f), 19 | new Vector3( 5.0f, -5.0f, 0.0f), 20 | new Vector3(10.0f, 5.0f, 0.0f), 21 | new Vector3(15.0f, -5.0f, 0.0f), 22 | new Vector3(20.0f, 5.0f, 0.0f), 23 | }; 24 | 25 | void Awake() 26 | { 27 | _Count = (_Vertices.Length - 2) * 3; 28 | _ComputeBuffer = new ComputeBuffer(_Count, Marshal.SizeOf(typeof(Vector3)), ComputeBufferType.Default); 29 | _Material = new Material(TriangleStripShader); 30 | _ComputeBuffer.SetData(_Vertices); 31 | } 32 | 33 | void OnRenderObject() 34 | { 35 | _Material.SetBuffer("_ComputeBuffer", _ComputeBuffer); 36 | _Material.SetPass(0); 37 | Graphics.DrawProceduralNow(MeshTopology.Triangles, _Count, 1); 38 | } 39 | 40 | void OnDestroy() 41 | { 42 | Destroy(_Material); 43 | _ComputeBuffer.Release(); 44 | } 45 | } -------------------------------------------------------------------------------- /TriangleStrip.shader: -------------------------------------------------------------------------------- 1 | Shader "TriangleStrip" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex VSMain 9 | #pragma fragment PSMain 10 | #pragma target 5.0 11 | 12 | StructuredBuffer _ComputeBuffer; 13 | 14 | float3 Hash(uint q) 15 | { 16 | uint3 n = q * uint3(1597334673u, 3812015801u, 2798796415U); 17 | n = (n.x ^ n.y ^ n.z) * uint3(1597334673u, 3812015801u, 2798796415U); 18 | return float3(n.x, n.y, n.z) * (1.0 / float(0xffffffffu)); 19 | } 20 | 21 | float4 VSMain (uint id : SV_VertexID, out float3 color : COLOR) : SV_POSITION 22 | { 23 | uint primitive = id / 3u; 24 | uint remainder = id % 3u; 25 | bool isEven = (primitive % 2u) == 0u; 26 | uint index = isEven ? primitive + remainder : primitive + abs(int(remainder) - 3) * sign(remainder); 27 | float3 position = _ComputeBuffer[index]; 28 | color = Hash(primitive); 29 | return UnityObjectToClipPos(position); 30 | } 31 | 32 | float4 PSMain (float4 vertex : SV_POSITION, float3 color : COLOR) : SV_Target 33 | { 34 | return float4(color, 1.0); 35 | } 36 | ENDCG 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /Unity2021/BoundingBox.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | 3 | float _Offset; 4 | uint _Dimension, _VertexCount; 5 | float4x4 _LocalToWorldMatrix; 6 | RWStructuredBuffer _ComputeBuffer; 7 | ByteAddressBuffer _GraphicsBuffer; 8 | 9 | [numthreads(64, 1, 1)] 10 | void CSMain (uint3 id : SV_DispatchThreadID) 11 | { 12 | if (id.x >= _VertexCount) return; 13 | float3 localPos = asfloat(_GraphicsBuffer.Load4((id.x * _Dimension) << 2)).xyz; 14 | float3 worldPos = mul(_LocalToWorldMatrix, float4(localPos, 1.0)).xyz; 15 | InterlockedMin(_ComputeBuffer[0], asuint(worldPos.x + _Offset)); 16 | InterlockedMin(_ComputeBuffer[1], asuint(worldPos.y + _Offset)); 17 | InterlockedMin(_ComputeBuffer[2], asuint(worldPos.z + _Offset)); 18 | InterlockedMax(_ComputeBuffer[3], asuint(worldPos.x + _Offset)); 19 | InterlockedMax(_ComputeBuffer[4], asuint(worldPos.y + _Offset)); 20 | InterlockedMax(_ComputeBuffer[5], asuint(worldPos.z + _Offset)); 21 | } -------------------------------------------------------------------------------- /Unity2021/Cobalt.shader: -------------------------------------------------------------------------------- 1 | // Test DirectX 12 Shader Model 6.6 Pack/Unpack Math Intrinsics 2 | // https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_Pack_Unpack_Intrinsics.html 3 | // Required Windows 11 and Unity 2023.2+ 4 | Shader "Cobalt" 5 | { 6 | SubShader 7 | { 8 | Pass 9 | { 10 | HLSLPROGRAM 11 | #pragma vertex VSMain 12 | #pragma fragment PSMain 13 | #include "UnityCG.cginc" 14 | #pragma require Int64BufferAtomics 15 | #pragma require Native16Bit 16 | 17 | float4 VSMain(float3 position : POSITION) : SV_Position 18 | { 19 | return UnityObjectToClipPos(position); 20 | } 21 | 22 | float4 PSMain(float4 p : SV_POSITION) : SV_Target 23 | { 24 | uint16_t4 cobalt = uint16_t4(0, 32, 194, 255); 25 | uint8_t4_packed encode = pack_u8(cobalt); 26 | uint16_t4 decode = unpack_u8u16(encode); 27 | return (float4)(decode / 255.0); 28 | } 29 | ENDHLSL 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Unity2021/Derivatives.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | #pragma require Int64BufferAtomics 3 | 4 | Texture2D _Reader; 5 | RWTexture2D _Writer; 6 | SamplerState _LinearRepeat; 7 | 8 | [numthreads(8,8,1)] 9 | void CSMain (uint3 id : SV_DispatchThreadID) 10 | { 11 | uint width, height; 12 | _Reader.GetDimensions(width, height); 13 | float2 uv = float2(id.x / (float)width, id.y / (float)height); 14 | float3 color = _Reader.Sample(_LinearRepeat, uv).rgb; 15 | float grayscale = 0.299 * color.r + 0.587 * color.g + 0.114 * color.b; 16 | float dx = ddx(grayscale * 5.0); 17 | float dy = ddy(grayscale * 5.0); 18 | float3 normal = normalize(float3(dx, dy, 1.0)); 19 | float3 result = (float3) 0.0; 20 | result.xy = normal.xy * 0.5 + 0.5; 21 | result.z = sqrt(1 - result.x * result.x - result.y * result.y); 22 | _Writer[id.xy] = float4(result, 1.0); 23 | } -------------------------------------------------------------------------------- /Unity2021/Derivatives.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class Derivatives : MonoBehaviour 4 | { 5 | [SerializeField] ComputeShader _ComputeShader; 6 | [SerializeField] Texture2D _Texture; 7 | RenderTexture _RenderTexture; 8 | Material _Material; 9 | bool _Init = false; 10 | 11 | void Start() 12 | { 13 | _RenderTexture = new RenderTexture(_Texture.width, _Texture.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear); 14 | _RenderTexture.enableRandomWrite = true; 15 | _RenderTexture.Create(); 16 | MeshRenderer meshRenderer = GetComponent(); 17 | _Init = meshRenderer != null; 18 | if (!_Init) return; 19 | _Material = new Material(Shader.Find("Standard")); 20 | _Material.SetTexture("_MainTex", _Texture); 21 | _Material.SetTexture("_BumpMap", _RenderTexture); 22 | meshRenderer.material = _Material; 23 | } 24 | 25 | void Update() 26 | { 27 | if (!_Init) return; 28 | _ComputeShader.SetTexture(0,"_Writer", _RenderTexture); 29 | _ComputeShader.SetTexture(0, "_Reader", _Texture, 0); 30 | _ComputeShader.Dispatch(0, _RenderTexture.width / 8, _RenderTexture.height / 8, 1); 31 | } 32 | 33 | void OnDestroy() 34 | { 35 | if (_RenderTexture != null) _RenderTexture.Release(); 36 | if (_Material != null) Destroy(_Material); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Unity2021/ProceduralGrass.shader: -------------------------------------------------------------------------------- 1 | Shader "ProceduralGrass" 2 | { 3 | Properties 4 | { 5 | _MainTex ("MainTex ", 2D) = "white" {} 6 | } 7 | Subshader 8 | { 9 | Cull Off 10 | CGPROGRAM 11 | #pragma surface SurfaceShader Standard fullforwardshadows addshadow 12 | #pragma target 5.0 13 | 14 | sampler2D _MainTex; 15 | 16 | struct Input 17 | { 18 | float2 uv_MainTex; 19 | }; 20 | 21 | void SurfaceShader (Input IN, inout SurfaceOutputStandard o) 22 | { 23 | float4 color = tex2D(_MainTex, IN.uv_MainTex); 24 | if (color.a < 0.5) discard; 25 | o.Albedo = color; 26 | } 27 | 28 | ENDCG 29 | } 30 | } -------------------------------------------------------------------------------- /UnsignedInteger64.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | #pragma use_dxc 3 | 4 | StructuredBuffer _Reader; 5 | RWStructuredBuffer _Writer; 6 | 7 | [numthreads(1,1,1)] 8 | void CSMain (uint3 id : SV_DispatchThreadID) 9 | { 10 | _Writer[id.x] = _Reader[id.x * 2] + _Reader[id.x * 2 + 1]; 11 | } -------------------------------------------------------------------------------- /UnsignedInteger64.cs: -------------------------------------------------------------------------------- 1 | // Example of using 64-bit unsigned integers in Unity compute shader 2 | // Requirements: Unity 2020.2.0a8 or later and active DX12 graphics API 3 | using UnityEngine; 4 | 5 | public class UnsignedInteger64 : MonoBehaviour 6 | { 7 | [SerializeField] ComputeShader _ComputeShader; 8 | 9 | void Start() 10 | { 11 | if (_ComputeShader == null) return; 12 | ComputeBuffer reader = new ComputeBuffer(4, sizeof(ulong), ComputeBufferType.Default); 13 | reader.SetData(new ulong[] {172439890993963ul, 657367095657329ul, 277347196953998ul, 844613309877278ul}); 14 | _ComputeShader.SetBuffer(0, "_Reader", reader); 15 | ComputeBuffer writer = new ComputeBuffer(2, sizeof(ulong), ComputeBufferType.Default); 16 | _ComputeShader.SetBuffer(0, "_Writer", writer); 17 | _ComputeShader.Dispatch(0, writer.count, 1, 1); // execute compute shader 18 | ulong[] result = new ulong[2]; 19 | writer.GetData( result ); 20 | Debug.Log( "172439890993963 + 657367095657329 = " + result[0].ToString()); // 829 806 986 651 292 21 | Debug.Log( "277347196953998 + 844613309877278 = " + result[1].ToString()); // 1 121 960 506 831 276 22 | reader.Release(); 23 | writer.Release(); 24 | } 25 | } -------------------------------------------------------------------------------- /VertexShaderImage.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | public class VertexShaderImage : MonoBehaviour 3 | { 4 | public Shader shader; 5 | protected Material material; 6 | void Awake() 7 | { 8 | material = new Material(shader); 9 | } 10 | void OnRenderObject() 11 | { 12 | material.SetPass(0); 13 | Graphics.DrawProcedural(MeshTopology.Triangles, 6 * 1024 * 1024, 1); 14 | } 15 | } -------------------------------------------------------------------------------- /VertexShaderImage.shader: -------------------------------------------------------------------------------- 1 | Shader "Vertex Shader Image" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | Cull Off 8 | CGPROGRAM 9 | #pragma vertex VSMain 10 | #pragma fragment PSMain 11 | #pragma target 5.0 12 | 13 | float2 PolarToCartesian (float2 p) 14 | { 15 | return p.x * float2(cos(p.y),sin(p.y)); 16 | } 17 | 18 | float2 CartesianToPolar (float2 p) 19 | { 20 | return float2(length(p), atan2(p.y, p.x)); 21 | } 22 | 23 | float3 Pattern (float2 uv) 24 | { 25 | uv = CartesianToPolar (abs(uv)); 26 | uv = (12.0*uv); 27 | uv = PolarToCartesian(uv); 28 | return float3(4.0/abs(uv),uv.x/uv.y); 29 | } 30 | 31 | float4 VSMain (uint id:SV_VertexID, out float3 color:TEXCOORD0) : SV_POSITION 32 | { 33 | float q = floor(float(id) / 6.0); 34 | float px = fmod(float(id), 2.0); 35 | float py = (float(id)-6.0 * floor(float(id)/6.0)) + 6.0; 36 | py = sign(126.0-py*floor(126.0/py)); 37 | float3 center = float3(fmod(q,1024.0), 0.0, floor(q/1024.0)); 38 | float error = 0.0000001; 39 | float2 uv = float2(center.x / 1024.0, center.z / 1024.0) + error; 40 | uv = float2(2.0*uv-1.0); 41 | color = Pattern(uv); 42 | float scale = 0.1; 43 | return UnityObjectToClipPos(float4(float3(sign(px)-0.5, 0.0, sign(py)-0.5) * scale + center * scale, 1.0)); 44 | } 45 | 46 | float4 PSMain (float4 vertex:SV_POSITION, float3 color:TEXCOORD0) : SV_Target 47 | { 48 | return float4(color, 1.0); 49 | } 50 | 51 | ENDCG 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /VisualAI.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | 3 | Texture2DArray _TextureArray; 4 | RWStructuredBuffer _CounterBuffer; 5 | 6 | [numthreads(8,8,1)] 7 | void CSMain (uint3 id : SV_DispatchThreadID) 8 | { 9 | float src = _TextureArray.Load(int4(id.xy, 0, 0)); 10 | float dst = _TextureArray.Load(int4(id.xy, 1, 0)); 11 | if (src > dst) _CounterBuffer.IncrementCounter(); 12 | } -------------------------------------------------------------------------------- /VolumeParticlesSystem/VolumeParticlesSystem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/VolumeParticlesSystem/VolumeParticlesSystem.jpg -------------------------------------------------------------------------------- /VolumeParticlesSystem/readme.md: -------------------------------------------------------------------------------- 1 | ![alt text](VolumeParticlesSystem.jpg) -------------------------------------------------------------------------------- /VolumeRenderTexture/VolumeRenderTexture.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | 3 | Texture3D VolumeReader; 4 | RWTexture3D VolumeWriter; 5 | SamplerState _PointClamp; 6 | float3 Center; 7 | 8 | float circle(float3 p, float3 c, float r) 9 | { 10 | return step(length(p-c)-r,0.0); 11 | } 12 | 13 | [numthreads(8,8,8)] 14 | void CSMain (uint3 id : SV_DispatchThreadID) 15 | { 16 | float3 uv = float3((float)id.x/256.0,(float)id.y/256.0,(float)id.z/256.0); 17 | float k = circle(uv,Center,0.03) + VolumeReader.SampleLevel(_PointClamp,uv,0); 18 | VolumeWriter[id] = float4(k,k,k,k); 19 | } 20 | -------------------------------------------------------------------------------- /VolumeRenderTexture/VolumeRenderTexture.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEngine.Rendering; 3 | 4 | public class VolumeRenderTexture : MonoBehaviour 5 | { 6 | public ComputeShader VolumeShader; 7 | public Material VolumeMaterial; 8 | RenderTexture VRTA, VRTB; 9 | float cx, cy, cz; 10 | bool swap = true; 11 | 12 | void Start() 13 | { 14 | RenderTextureDescriptor RTD = new RenderTextureDescriptor(256, 256, RenderTextureFormat.ARGB32); 15 | RTD.dimension = TextureDimension.Tex3D; 16 | RTD.volumeDepth = 256; 17 | VRTA = new RenderTexture(RTD); 18 | VRTA.enableRandomWrite = true; 19 | VRTA.Create(); 20 | VRTB = new RenderTexture(RTD); 21 | VRTB.enableRandomWrite = true; 22 | VRTB.Create(); 23 | cx = cy = cz = 0.5f; 24 | } 25 | 26 | void Update() 27 | { 28 | if (Input.GetKey(KeyCode.W)) cy+=0.001f; 29 | if (Input.GetKey(KeyCode.S)) cy-=0.001f; 30 | if (Input.GetKey(KeyCode.A)) cx+=0.001f; 31 | if (Input.GetKey(KeyCode.D)) cx-=0.001f; 32 | if (Input.GetKey(KeyCode.Q)) cz+=0.001f; 33 | if (Input.GetKey(KeyCode.E)) cz-=0.001f; 34 | VolumeShader.SetFloats("Center",new float[3] {cx, cy, cz}); 35 | if (swap) 36 | { 37 | VolumeShader.SetTexture(0, "VolumeReader", VRTA); 38 | VolumeShader.SetTexture(0, "VolumeWriter", VRTB); 39 | VolumeShader.Dispatch(0, 256 / 8, 256 / 8, 256 / 8); 40 | } 41 | else 42 | { 43 | VolumeShader.SetTexture(0, "VolumeReader", VRTB); 44 | VolumeShader.SetTexture(0, "VolumeWriter", VRTA); 45 | VolumeShader.Dispatch(0, 256 / 8, 256 / 8, 256 / 8); 46 | } 47 | swap = !swap; 48 | VolumeMaterial.SetTexture("_Volume",VRTB); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /VolumeRenderTexture/VolumeRenderTexture.shader: -------------------------------------------------------------------------------- 1 | Shader "Volume Render Texture" 2 | { 3 | SubShader 4 | { 5 | Cull Back 6 | Pass 7 | { 8 | CGPROGRAM 9 | #pragma vertex VSMain 10 | #pragma fragment PSMain 11 | #pragma target 5.0 12 | 13 | sampler3D _Volume; 14 | 15 | void VSMain(inout float4 vertex:POSITION, out float3 world:WORLD) 16 | { 17 | world = mul(unity_ObjectToWorld, vertex).xyz; 18 | vertex = UnityObjectToClipPos(vertex); 19 | } 20 | 21 | float4 PSMain(float4 vertex:POSITION, float3 world:WORLD) : SV_Target 22 | { 23 | float3 ro = mul(unity_WorldToObject, float4(world, 1)).xyz; 24 | float3 rd = normalize(mul((float3x3) unity_WorldToObject, normalize(world - _WorldSpaceCameraPos))); 25 | float3 tbot = (1.0 / rd) * (-0.5 - ro); 26 | float3 ttop = (1.0 / rd) * (0.5 - ro); 27 | float3 tmin = min(ttop, tbot); 28 | float3 tmax = max(ttop, tbot); 29 | float2 a = max(tmin.xx, tmin.yz); 30 | float tnear = max(0.0,max(a.x, a.y)); 31 | float2 b = min(tmax.xx, tmax.yz); 32 | float tfar = min(b.x, b.y); 33 | float3 d = normalize((ro + rd * tfar) - ro) * (abs(tfar - tnear) / 128.0); 34 | float4 t = float4(0, 0, 0, 0); 35 | [unroll] 36 | for (int i = 0; i < 128; i++) 37 | { 38 | float v = tex3D(_Volume, ro+0.5).r; 39 | float4 s = float4(v, v, v, v); 40 | s.a *= 0.5; 41 | s.rgb *= s.a; 42 | t = (1.0 - t.a) * s + t; 43 | ro += d; 44 | if (t.a > 0.99) break; 45 | } 46 | return saturate(t); 47 | } 48 | 49 | ENDCG 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /VolumeTexture/GenerateVolumeTexture.cs: -------------------------------------------------------------------------------- 1 | // Put in Editor directory. 2 | using UnityEngine; 3 | using UnityEditor; 4 | 5 | public class GenerateVolumeTexture : EditorWindow 6 | { 7 | Transform Cage; 8 | int size = 128; 9 | string filename = "noname.bin"; 10 | 11 | [MenuItem("Assets/Generate Volume Texture")] 12 | static void ShowWindow () 13 | { 14 | EditorWindow.GetWindow ( typeof(GenerateVolumeTexture)); 15 | } 16 | 17 | void SaveFloatArrayToFile(float[] x, string path) 18 | { 19 | byte[] a = new byte[x.Length * 4]; 20 | System.Buffer.BlockCopy(x, 0, a, 0, a.Length); 21 | System.IO.File.WriteAllBytes(path,a); 22 | } 23 | 24 | void OnGUI() 25 | { 26 | Cage = EditorGUILayout.ObjectField("Cage", Cage, typeof(Transform), true) as Transform; 27 | size = EditorGUILayout.IntField("Volume dimension:", size); 28 | filename = EditorGUILayout.TextField("File name: ", filename); 29 | if ( GUILayout.Button( "Make" ) ) GenerateVolume (); 30 | } 31 | 32 | void GenerateVolume () 33 | { 34 | float[] voxels = new float[size*size*size]; 35 | int i = 0; 36 | float s = 1.0f/size; 37 | Vector3 p = Cage.position; 38 | Vector3 o = new Vector3(p.x-0.5f,p.y-0.5f,p.z-0.5f); 39 | for (int z = 0; z < size; ++z) 40 | { 41 | for (int y = 0; y < size; ++y) 42 | { 43 | for (int x = 0; x < size; ++x, ++i) 44 | { 45 | if (Physics.CheckSphere(new Vector3(s*x+o.x,s*y+o.y,s*z+o.z), s*0.5f)) 46 | voxels[i] = 0.9f; 47 | else 48 | voxels[i] = 0.0f; 49 | } 50 | } 51 | } 52 | SaveFloatArrayToFile(voxels, Application.dataPath + "/StreamingAssets/" + filename); 53 | } 54 | } -------------------------------------------------------------------------------- /VolumeTexture/StanfordBunny.fbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/VolumeTexture/StanfordBunny.fbx -------------------------------------------------------------------------------- /VolumeTexture/readme.md: -------------------------------------------------------------------------------- 1 | Convert polygonal mesh into 3D texture and render with raymarching technique. 2 | -------------------------------------------------------------------------------- /VoronoiCones.shader: -------------------------------------------------------------------------------- 1 | Shader "Hidden/VoronoiCones" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex VSMain 9 | #pragma fragment PSMain 10 | #pragma target 5.0 11 | 12 | struct Seed 13 | { 14 | float2 Location; 15 | float3 Color; 16 | }; 17 | 18 | StructuredBuffer _Cone; 19 | StructuredBuffer _Seeds; 20 | float4x4 _ModelViewProjection; 21 | int _Animation; 22 | float _SeedSize, _ConeHeight; 23 | 24 | float4 VSMain (uint id : SV_VertexID, uint instance : SV_InstanceID, out float3 color : COLOR, out float4 worldPos : WORLDPOS) : SV_POSITION 25 | { 26 | worldPos = float4(_Cone[id] + float3(_Seeds[instance].Location, 0.0), 1.0); 27 | worldPos.xy += _Animation * float2(sin(_Time.g + instance), cos(_Time.g - instance)) * 0.5; 28 | color = _Seeds[instance].Color; 29 | return mul(_ModelViewProjection, worldPos); 30 | } 31 | 32 | float4 PSMain (float4 vertex : SV_POSITION, float3 color : COLOR, float4 worldPos : WORLDPOS) : SV_Target 33 | { 34 | return worldPos.z <= (-_ConeHeight + _SeedSize) ? float4(0, 0, 0, 1) : float4(color, 1); 35 | } 36 | ENDCG 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /VoronoiDualGraph.shader: -------------------------------------------------------------------------------- 1 | Shader "Hidden/VoronoiDualGraph" 2 | { 3 | SubShader 4 | { 5 | Cull Off 6 | Pass 7 | { 8 | CGPROGRAM 9 | #pragma vertex VSMain 10 | #pragma fragment PSMain 11 | #pragma target 5.0 12 | 13 | struct Triangle 14 | { 15 | float2 Vertices[3]; 16 | }; 17 | 18 | uniform StructuredBuffer _TriangleBuffer; 19 | 20 | float4 VSMain (float4 vertex : POSITION, uint id : SV_VertexID, out float2 barycentric : BARYCENTRIC) : SV_Position 21 | { 22 | uint index = id % 3u; 23 | float3 worldPos = float3(_TriangleBuffer[id / 3u].Vertices[index], 0.02); 24 | barycentric = float2(fmod(index, 2.0), step(2.0, index)); 25 | return UnityObjectToClipPos(float4(worldPos.xzy, 1.0)); 26 | } 27 | 28 | float4 PSMain (float4 vertex : SV_POSITION, float2 barycentric : BARYCENTRIC) : SV_Target 29 | { 30 | float3 coords = float3(barycentric, 1.0 - barycentric.x - barycentric.y); 31 | float3 df = fwidth(coords); 32 | float3 wireframe = smoothstep(df * 0.1, df * 0.1 + df, coords); 33 | if ((1.0 - min(wireframe.x, min(wireframe.y, wireframe.z))) < 0.01) discard; 34 | return float4(0.0, 0.0, 0.0, 1.0); 35 | } 36 | ENDCG 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /abyss.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class abyss : MonoBehaviour 5 | { 6 | public Material material; 7 | public Camera main_camera; 8 | GameObject quad; 9 | GameObject[] cubes; 10 | 11 | void environment () 12 | { 13 | cubes = new GameObject[20]; 14 | for (int i=0;i<20;i++) 15 | { 16 | cubes[i] = GameObject.CreatePrimitive(PrimitiveType.Cube); 17 | if (i % 2 == 0.0) cubes[i].transform.position=new Vector3(-3.0f,-2.0f*i+6.0f,5.0f); 18 | else cubes[i].transform.position=new Vector3(3.0f,-2.0f*i+6.0f,5.0f); 19 | Material blocks = new Material(Shader.Find("Unlit/Color")); 20 | cubes[i].GetComponent().material=blocks; 21 | cubes[i].GetComponent().material.color=Color.black; 22 | } 23 | } 24 | 25 | void Start () 26 | { 27 | material.SetFloat("_intensity",1.0f); 28 | main_camera.transform.position = new Vector3(0.0f,10.0f,0.0f); 29 | quad = GameObject.CreatePrimitive(PrimitiveType.Quad); 30 | quad.transform.parent=main_camera.transform; 31 | quad.transform.position=main_camera.transform.position + new Vector3(0.0f,0.0f,10.0f); 32 | quad.transform.localScale = new Vector3(100.0f,20.0f,100.0f); 33 | quad.GetComponent().material=material; 34 | environment(); 35 | } 36 | 37 | void Update () 38 | { 39 | main_camera.transform.position-= new Vector3(0, 2.0f*Time.deltaTime, 0); 40 | if (main_camera.transform.position.y<0.0f ) material.SetFloat("_intensity",1.0f+Mathf.Abs(transform.position.y)*0.33f); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /abyss.shader: -------------------------------------------------------------------------------- 1 | Shader "Abyss" 2 | { 3 | Properties 4 | { 5 | _intensity ("Intensity",Float) = 1.0 6 | } 7 | Subshader 8 | { 9 | Pass 10 | { 11 | CGPROGRAM 12 | #pragma vertex vertex_shader 13 | #pragma fragment pixel_shader 14 | #pragma target 3.0 15 | 16 | float _intensity; 17 | 18 | struct custom_type 19 | { 20 | float4 vertex : SV_POSITION; 21 | float2 uv : TEXCOORD0; 22 | }; 23 | 24 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 25 | { 26 | custom_type vs; 27 | vs.vertex = mul (UNITY_MATRIX_MVP,vertex); 28 | vs.uv = uv; 29 | return vs; 30 | } 31 | 32 | float4 pixel_shader (custom_type ps) : COLOR 33 | { 34 | float t = pow(ps.uv.y,_intensity); 35 | return float4(t,t,t,1.0); 36 | } 37 | ENDCG 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /accumulation.cs: -------------------------------------------------------------------------------- 1 | //Assign script to main camera, then bind material with accumulation.shader 2 | //It draws fullscreen quad. Shader output is set as shader input for next frame. 3 | using UnityEngine; 4 | using System.Collections; 5 | 6 | public class accumulation : MonoBehaviour 7 | { 8 | public Material material; 9 | 10 | void OnPostRender() 11 | { 12 | GL.PushMatrix(); 13 | material.SetPass(0); 14 | GL.LoadOrtho(); 15 | GL.Begin(GL.QUADS); 16 | GL.TexCoord2(0, 0); 17 | GL.Vertex3(0.0F, 0.0F, 0); 18 | GL.TexCoord2(0, 1); 19 | GL.Vertex3(0.0F, 1.0F, 0); 20 | GL.TexCoord2(1, 1); 21 | GL.Vertex3(1.0F, 1.0F, 0); 22 | GL.TexCoord2(1, 0); 23 | GL.Vertex3(1.0F, 0.0F, 0); 24 | GL.End(); 25 | GL.PopMatrix(); 26 | Texture2D buffer = new Texture2D(Screen.width, Screen.height); 27 | buffer.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); 28 | buffer.Apply(); 29 | material.mainTexture = buffer; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /accumulation.shader: -------------------------------------------------------------------------------- 1 | Shader "Accumulation" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "black" {} 6 | } 7 | Subshader 8 | { 9 | Pass 10 | { 11 | CGPROGRAM 12 | #pragma vertex vertex_shader 13 | #pragma fragment pixel_shader 14 | #pragma target 3.0 15 | 16 | sampler2D _MainTex; 17 | 18 | struct custom_type 19 | { 20 | float4 screen_vertex : SV_POSITION; 21 | float2 uv: TEXCOORD0; 22 | }; 23 | 24 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 25 | { 26 | custom_type vs; 27 | vs.screen_vertex = mul (UNITY_MATRIX_MVP, vertex); 28 | vs.uv=uv; 29 | return vs; 30 | } 31 | 32 | float4 pixel_shader (custom_type ps ) : SV_TARGET 33 | { 34 | float2 uv = ps.uv.xy; 35 | float4 c = tex2D(_MainTex,uv); 36 | if (uv.x>0.5) c.x+=0.005; 37 | return c; 38 | } 39 | 40 | ENDCG 41 | 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /area.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSInit 2 | #pragma kernel CSMain 3 | 4 | Texture2D image; 5 | SamplerState _LinearClamp; 6 | RWStructuredBuffer compute_buffer; 7 | 8 | [numthreads(64,1,1)] 9 | void CSInit(uint3 id : SV_DispatchThreadID) 10 | { 11 | compute_buffer[id.x] = 0.0; 12 | } 13 | 14 | [numthreads(8,8,1)] 15 | void CSMain (uint3 id : SV_DispatchThreadID) 16 | { 17 | float w,h; 18 | image.GetDimensions(w,h); 19 | float u = id.x/w; 20 | float v = id.y/h; 21 | float2 uv = float2 (u,v); 22 | float4 color = image.SampleLevel(_LinearClamp,uv,0); 23 | uint value = ceil(color.r); 24 | InterlockedAdd(compute_buffer[0], value); 25 | } -------------------------------------------------------------------------------- /billboard.shader: -------------------------------------------------------------------------------- 1 | Shader "Billboard" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "black" {} 6 | [Toggle] _flip("Flip UV", Float) = 0 7 | } 8 | Subshader 9 | { 10 | Pass 11 | { 12 | Cull Off 13 | CGPROGRAM 14 | #pragma vertex vertex_shader 15 | #pragma fragment pixel_shader 16 | #pragma target 3.0 17 | #define UNITY_SHADER_NO_UPGRADE 1 18 | 19 | sampler2D _MainTex; 20 | float _flip; 21 | 22 | struct structure 23 | { 24 | float4 vertex : SV_POSITION; 25 | float2 uv : TEXCOORD0; 26 | }; 27 | 28 | structure vertex_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) 29 | { 30 | structure vs; 31 | float4x4 m = UNITY_MATRIX_MV; 32 | m._m00 = -1.0/length(unity_WorldToObject[0].xyz); 33 | m._m10 = 0.0f; 34 | m._m20 = 0.0f; 35 | m._m01 = 0.0f; 36 | m._m11 = -1.0/length(unity_WorldToObject[1].xyz); 37 | m._m21 = 0.0f; 38 | m._m02 = 0.0f; 39 | m._m12 = 0.0f; 40 | m._m22 = -1.0/length(unity_WorldToObject[2].xyz); 41 | vs.vertex = mul(UNITY_MATRIX_P, mul(m,vertex)); 42 | vs.uv = uv; 43 | return vs; 44 | } 45 | 46 | float4 pixel_shader (structure ps ) : SV_TARGET 47 | { 48 | if (_flip==1.0) 49 | return tex2D(_MainTex,ps.uv); 50 | else 51 | return tex2D(_MainTex,float2(1.0-ps.uv.x,1.0-ps.uv.y)); 52 | } 53 | 54 | ENDCG 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /binormals.shader: -------------------------------------------------------------------------------- 1 | //Debug binormals (called also bitangents) 2 | 3 | Shader "Binormals" 4 | { 5 | Subshader 6 | { 7 | Pass 8 | { 9 | CGPROGRAM 10 | #pragma vertex vertex_shader 11 | #pragma fragment pixel_shader 12 | #pragma target 2.0 13 | 14 | struct structure 15 | { 16 | float4 vertex : SV_POSITION; 17 | float4 color : COLOR; 18 | }; 19 | 20 | float3 cross(float3 a, float3 b) 21 | { 22 | return a.yzx*b.zxy-a.zxy*b.yzx; 23 | } 24 | 25 | structure vertex_shader (float4 vertex:POSITION,float3 normal:NORMAL,float4 tangent:TANGENT) 26 | { 27 | structure vs; 28 | vs.vertex = UnityObjectToClipPos (vertex); 29 | float3 binormal = cross(normal,tangent.xyz)*tangent.w; 30 | vs.color = float4(binormal*0.5+0.5,1.0); 31 | return vs; 32 | } 33 | 34 | float4 pixel_shader (structure ps ) : SV_TARGET 35 | { 36 | return ps.color; 37 | } 38 | 39 | ENDCG 40 | 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /bitwise_test.shader: -------------------------------------------------------------------------------- 1 | Shader "Bitwise test" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 4.0 11 | 12 | struct custom_type 13 | { 14 | float4 screen_vertex : SV_POSITION; 15 | float2 uv: TEXCOORD0; 16 | }; 17 | 18 | custom_type vertex_shader (float4 vertex : POSITION, float2 uv:TEXCOORD0) 19 | { 20 | custom_type vs; 21 | vs.screen_vertex = mul (UNITY_MATRIX_MVP, vertex); 22 | vs.uv=uv; 23 | return vs; 24 | } 25 | 26 | float4 pixel_shader (custom_type ps ) : SV_TARGET 27 | { 28 | uint n = 1; 29 | return lerp (float4(0.0,0.0,1.0,1.0),float4(0.0,1.0,1.0,1.0),step(ps.uv.x,(0.1*(n<<=3)))); 30 | // left bit shift equivalent to 0.1 * n * pow(2,3) 31 | } 32 | 33 | ENDCG 34 | 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /bleach.shader: -------------------------------------------------------------------------------- 1 | Shader "Bleach Filter" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "white" {} 6 | } 7 | Subshader 8 | { 9 | Pass 10 | { 11 | CGPROGRAM 12 | #pragma vertex vertex_shader 13 | #pragma fragment pixel_shader 14 | #pragma target 3.0 15 | 16 | sampler2D _MainTex; 17 | static const float scale = 2.0; 18 | static const float4 c = float4(0.2124,0.7153,0.0722,0.0); 19 | 20 | float4 bleach(float4 p, float4 m, float4 s) 21 | { 22 | float4 a = float4(1.0,1.0,1.0,1.0); 23 | float4 b = float4(2.0,2.0,2.0,2.0); 24 | float l = dot(m,c); 25 | float x = clamp((l - 0.45) * 10.0, 0.0, 1.0); 26 | float4 t = b * m * p; 27 | float4 w = a - (b * (a - m) * (a - p)); 28 | float4 r = lerp(t, w, float4(x,x,x,x) ); 29 | return lerp(m, r, s); 30 | } 31 | 32 | struct custom_type 33 | { 34 | float4 vertex : SV_POSITION; 35 | float2 uv : TEXCOORD0; 36 | }; 37 | 38 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 39 | { 40 | custom_type vs; 41 | vs.vertex = UnityObjectToClipPos (vertex); 42 | vs.uv = uv; 43 | return vs; 44 | } 45 | 46 | float4 pixel_shader (custom_type ps) : COLOR 47 | { 48 | float2 uv = ps.uv.xy; 49 | float4 p = tex2D(_MainTex,uv); 50 | float4 k = float4(dot(p,c),dot(p,c),dot(p,c),p.a); 51 | return bleach(k,p,float4(scale,scale,scale,1)); 52 | } 53 | ENDCG 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /bounce_ball.shader: -------------------------------------------------------------------------------- 1 | Shader "Bounce Ball" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 2.0 11 | 12 | struct custom_type 13 | { 14 | float4 vertex : SV_POSITION; 15 | float2 uv : TEXCOORD0; 16 | }; 17 | 18 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 19 | { 20 | custom_type vs; 21 | vs.vertex = mul(UNITY_MATRIX_MVP,vertex); 22 | vs.uv = uv; 23 | return vs; 24 | } 25 | 26 | float4 pixel_shader (custom_type ps) : COLOR 27 | { 28 | float2 uv = float2(2.0*ps.uv.xy-1.0); 29 | float g = 1.0; 30 | float h = 1.8; 31 | float v,d ; 32 | if (_Time.g<0.94) 33 | { 34 | v=sqrt(2.0*g*h); 35 | d=length(uv+float2(0,-0.8+v*_Time.g)); 36 | } 37 | else 38 | if (_Time.g>=0.94 && _Time.g<1.61) 39 | { 40 | v=sqrt(2.0*g*0.5*h); 41 | d=length(uv+float2(0,1.0-v*(_Time.g-0.94))); 42 | } 43 | else 44 | if (_Time.g>=1.61 && _Time.g<2.5) 45 | { 46 | v=sqrt(2.0*g*0.25*h); 47 | d=length(uv+float2(0,0.1+v*(_Time.g-1.61))); 48 | } 49 | else d=length(uv+float2(0,0.9)); 50 | float4 s = lerp(float4(1,0,0,1),float4(0,0,0,1),step(0.1,d)); 51 | return max(s,float4(0,0,0,1)); 52 | } 53 | ENDCG 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /buffer.cs: -------------------------------------------------------------------------------- 1 | //Load buffer.shader into Unity engine 2 | using UnityEngine; 3 | 4 | public class buffer : MonoBehaviour 5 | { 6 | RenderTexture A; 7 | RenderTexture B; 8 | public Material material; 9 | 10 | void Start() 11 | { 12 | A = new RenderTexture(1024,1024,0); 13 | A.Create(); 14 | B = new RenderTexture(1024,1024,0); 15 | B.Create(); 16 | } 17 | 18 | void Update() 19 | { 20 | material.SetTexture("MainTex", A); 21 | Graphics.Blit(A,B,material); 22 | material.SetTexture("MainTex", B); 23 | Graphics.Blit(B,A,material); 24 | } 25 | } -------------------------------------------------------------------------------- /chromatic_aberration.shader: -------------------------------------------------------------------------------- 1 | Shader "Chromatic Aberration" 2 | { 3 | Properties 4 | { 5 | [HideInInspector] 6 | _MainTex ("Texture", 2D) = "white" {} 7 | _amount ("Amount",Range(0.0,0.01)) = 0.005 8 | } 9 | Subshader 10 | { 11 | Pass 12 | { 13 | CGPROGRAM 14 | #pragma vertex vertex_shader 15 | #pragma fragment pixel_shader 16 | #pragma target 3.0 17 | 18 | sampler2D _MainTex; 19 | float _amount; 20 | 21 | struct structure 22 | { 23 | float4 vertex : SV_POSITION; 24 | float2 uv : TEXCOORD0; 25 | }; 26 | 27 | structure vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 28 | { 29 | structure vs; 30 | vs.vertex = UnityObjectToClipPos (vertex); 31 | vs.uv = uv; 32 | return vs; 33 | } 34 | 35 | float4 pixel_shader (structure ps) : COLOR 36 | { 37 | float2 uv = ps.uv.xy; 38 | float3 color; 39 | color.r = tex2D( _MainTex, float2(uv.x+_amount,uv.y) ).r; 40 | color.g = tex2D( _MainTex, uv ).g; 41 | color.b = tex2D( _MainTex, float2(uv.x-_amount,uv.y) ).b; 42 | color *= (1.0 - _amount* 0.5); 43 | return float4(color,1.0); 44 | } 45 | ENDCG 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /clockwise_triangles.shader: -------------------------------------------------------------------------------- 1 | Shader "Clockwise triangles" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 2.0 11 | 12 | struct custom_type 13 | { 14 | float4 vertex : SV_POSITION; 15 | float2 uv : TEXCOORD0; 16 | }; 17 | 18 | custom_type vertex_shader (float4 vertex : POSITION, float2 uv : TEXCOORD0) 19 | { 20 | custom_type vs; 21 | vs.vertex = mul (UNITY_MATRIX_MVP,vertex); 22 | vs.uv=uv; 23 | return vs; 24 | } 25 | 26 | float4 pixel_shader (custom_type ps) : SV_TARGET 27 | { 28 | float2 uv = ps.uv.xy; 29 | float4 a =lerp(float4(1.0,1.0,1.0,1.0),float4(0.0,0.0,0.0,1.0),(step(step(uv.y,1.0-uv.x),step(uv.x,uv.y)))); 30 | float4 b =lerp(float4(1.0,1.0,1.0,1.0),float4(0.0,0.0,0.0,1.0),(step(step(uv.y,1.0-uv.x),step(uv.y,uv.x)))); 31 | float4 c =lerp(float4(1.0,1.0,1.0,1.0),float4(0.0,0.0,0.0,1.0),(step(step(1.0-uv.y,uv.x),step(uv.y,uv.x)))); 32 | float4 d =lerp(float4(1.0,1.0,1.0,1.0),float4(0.0,0.0,0.0,1.0),(step(step(1.0-uv.y,uv.x),step(uv.x,uv.y)))); 33 | float t = fmod(floor(_Time.g),4.0); 34 | return float4 (a *(1.0-abs(sign(t-0.0)))+ b*(1.0-abs(sign(t-1.0)))+c*(1.0-abs(sign(t-2.0)))+d*(1.0-abs(sign(t-3.0)))); 35 | } 36 | ENDCG 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /collision_mesh_plane.compute: -------------------------------------------------------------------------------- 1 | //reference book: "Real-Time Collision Detection" by Christer Ericson 2 | 3 | #pragma kernel CSMain 4 | #pragma kernel CSInit 5 | 6 | StructuredBuffer input; 7 | RWStructuredBuffer output; 8 | float4x4 ObjectToWorld; 9 | float4 A,B,C; 10 | 11 | struct Plane 12 | { 13 | float3 n; 14 | float d; 15 | }; 16 | 17 | Plane ComputePlane(float3 a, float3 b, float3 c) 18 | { 19 | Plane p; 20 | p.n = normalize(cross(b - a, c - a)); 21 | p.d = dot(p.n, a); 22 | return p; 23 | } 24 | 25 | float DistPointPlane(float3 q, Plane p) 26 | { 27 | return dot(q, p.n) - p.d; 28 | } 29 | 30 | [numthreads(8,1,1)] 31 | void CSInit(uint id : SV_DispatchThreadID) 32 | { 33 | output[id] = 0; 34 | } 35 | 36 | [numthreads(8,1,1)] 37 | void CSMain (uint id : SV_DispatchThreadID) 38 | { 39 | Plane plane = ComputePlane(A.xyz,B.xyz,C.xyz); 40 | float d = DistPointPlane(mul(ObjectToWorld,float4(input[id],1.0)).xyz,plane); 41 | uint value ; 42 | if (abs(d)<0.001) 43 | value = 1; 44 | else 45 | value = 0; 46 | InterlockedAdd(output[0], value); 47 | } 48 | -------------------------------------------------------------------------------- /collision_mesh_plane.shader: -------------------------------------------------------------------------------- 1 | Shader "Collision Mesh - Plane" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 5.0 11 | 12 | uniform float4 A; 13 | uniform float4 B; 14 | uniform float4 C; 15 | 16 | struct Plane 17 | { 18 | float3 n; 19 | float d; 20 | }; 21 | 22 | Plane ComputePlane(float3 a, float3 b, float3 c) 23 | { 24 | Plane p; 25 | p.n = normalize(cross(b - a, c - a)); 26 | p.d = dot(p.n, a); 27 | return p; 28 | } 29 | 30 | float DistPointPlane(float3 q, Plane p) 31 | { 32 | return dot(q, p.n) - p.d; 33 | } 34 | 35 | struct SHADERDATA 36 | { 37 | float4 vertex : SV_POSITION; 38 | float2 uv : TEXCOORD0; 39 | float dist : TEXCOORD1; 40 | }; 41 | 42 | SHADERDATA vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 43 | { 44 | SHADERDATA vs; 45 | vs.vertex = UnityObjectToClipPos (vertex); 46 | vs.uv = uv; 47 | Plane plane = ComputePlane(A.xyz,B.xyz,C.xyz); 48 | vs.dist = DistPointPlane(mul(unity_ObjectToWorld,vertex),plane); 49 | return vs; 50 | } 51 | 52 | float4 pixel_shader (SHADERDATA ps) : SV_TARGET 53 | { 54 | if (abs(ps.dist)<0.001) 55 | return float4(0,0,1,1); 56 | else 57 | return float4(0,0,0,1); 58 | } 59 | 60 | ENDCG 61 | 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /cubemorph.shader: -------------------------------------------------------------------------------- 1 | // Morph input shape (for example Sphere) to cube, with normal reconstruction and diffuse lighting 2 | 3 | Shader "Cubemorph" 4 | { 5 | SubShader 6 | { 7 | Pass 8 | { 9 | CGPROGRAM 10 | #pragma vertex VSMain 11 | #pragma fragment PSMain 12 | 13 | float4 _LightColor0; 14 | 15 | float3 morph (float3 base) 16 | { 17 | float3 offset = base; 18 | offset *= 2.0 / length(offset.xyz); 19 | offset = 0.5*clamp( offset.xyz, -1.0, 1.0 ); 20 | return lerp(base,offset,sin(_Time.g)*0.5+0.5); 21 | } 22 | 23 | void VSMain (inout float4 vertex:POSITION, inout float2 uv:TEXCOORD0, inout float3 normal:NORMAL, float4 tangent:TANGENT) 24 | { 25 | float3 position = morph( vertex ); 26 | float3 bitangent = cross( normal, tangent.xyz ); 27 | float3 nt = ( morph( vertex + tangent.xyz * 0.01 ) - position ); 28 | float3 nb = ( morph( vertex + bitangent * 0.01 ) - position ); 29 | normal = cross( nt, nb ); 30 | vertex = UnityObjectToClipPos(float4(position,vertex.w)); 31 | } 32 | 33 | float4 PSMain (float4 vertex:POSITION, float2 uv:TEXCOORD0, float3 normal:NORMAL) : SV_TARGET 34 | { 35 | float3 LightDirection = normalize( _WorldSpaceLightPos0 ); 36 | float3 NormalDirection = normalize(mul((float3x3)unity_ObjectToWorld,normal)); 37 | float3 diffuse = max(dot(LightDirection, NormalDirection),0.0) * _LightColor0; 38 | return float4(diffuse, 1); 39 | } 40 | ENDCG 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /debug_vertex_index.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class debug_vertex_index : MonoBehaviour 4 | { 5 | public Material material; 6 | public GameObject gameobject; 7 | 8 | void Start () 9 | { 10 | Mesh mesh = gameobject.GetComponent().sharedMesh; 11 | material.SetInt("amount",(int)mesh.vertices.Length); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /debug_vertex_index.shader: -------------------------------------------------------------------------------- 1 | Shader "Debug Vertex Index" 2 | { 3 | Properties 4 | { 5 | [HideInInspector] 6 | amount ("amount", Int) = 0 7 | } 8 | Subshader 9 | { 10 | Pass 11 | { 12 | CGPROGRAM 13 | #pragma vertex vertex_shader 14 | #pragma fragment pixel_shader 15 | #pragma target 3.0 16 | 17 | int amount; 18 | 19 | struct structure 20 | { 21 | float4 vertex : SV_POSITION; 22 | float2 uv : TEXCOORD0; 23 | uint id : TEXCOORD1; 24 | }; 25 | 26 | structure vertex_shader (uint id:SV_VertexID,float4 vertex:POSITION,float2 uv:TEXCOORD0) 27 | { 28 | structure vs; 29 | vs.vertex = UnityObjectToClipPos(vertex); 30 | vs.id = id; 31 | vs.uv = uv; 32 | return vs; 33 | } 34 | 35 | float4 pixel_shader (structure ps) : SV_TARGET 36 | { 37 | float t = float(ps.id)/float(amount); 38 | return float4(t,t,t,1.0); 39 | } 40 | 41 | ENDCG 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /demoscene/readme.md: -------------------------------------------------------------------------------- 1 | Directory contains HLSL and GLSL code for standalone executables. 2 | -------------------------------------------------------------------------------- /demoscene/shader_model_3/readme.md: -------------------------------------------------------------------------------- 1 | HLSL DirectX Shader Model 3.0 startup. 2 | -------------------------------------------------------------------------------- /depth.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class depth : MonoBehaviour 4 | { 5 | public Material material; 6 | public Camera main_camera; 7 | 8 | void Start () 9 | { 10 | main_camera.depthTextureMode = DepthTextureMode.Depth; 11 | main_camera.farClipPlane = 80f; //Draw distance 12 | } 13 | 14 | void OnRenderImage (RenderTexture source, RenderTexture destination) 15 | { 16 | Graphics.Blit (source, destination, material); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /depth.shader: -------------------------------------------------------------------------------- 1 | Shader "Depth" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 4.0 11 | 12 | sampler2D _CameraDepthTexture; 13 | 14 | struct custom_type 15 | { 16 | float4 vertex : SV_POSITION; 17 | float4 screen : TEXCOORD1; 18 | }; 19 | 20 | float4 ComputeScreenPos (float4 p) 21 | { 22 | float4 o = p * 0.5; 23 | o.xy = float2(o.x, o.y*_ProjectionParams.x) + o.w; 24 | o.zw = p.zw; 25 | return o; 26 | } 27 | 28 | custom_type vertex_shader (float4 vertex:POSITION) 29 | { 30 | custom_type vs; 31 | vs.vertex = UnityObjectToClipPos(vertex); 32 | vs.screen = ComputeScreenPos(vs.vertex); 33 | return vs; 34 | } 35 | 36 | float4 pixel_shader (custom_type ps) : SV_TARGET 37 | { 38 | float d = 1.0/(_ZBufferParams.x*(tex2D(_CameraDepthTexture,ps.screen.xy).r)+_ZBufferParams.y); 39 | return float4(d,d,d,1); 40 | } 41 | ENDCG 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /diagonal_transition.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class diagonal_transition : MonoBehaviour 5 | { 6 | public GameObject light; 7 | public Material material; 8 | 9 | void Update() 10 | { 11 | light.transform.Rotate(new Vector3(Time.deltaTime*4.0f,0.0f,0.0f)); 12 | } 13 | 14 | void OnRenderImage (RenderTexture source, RenderTexture destination) 15 | { 16 | Graphics.Blit (source, destination, material); 17 | } 18 | } -------------------------------------------------------------------------------- /diagonal_transition.shader: -------------------------------------------------------------------------------- 1 | Shader "Diagonal transition" 2 | { 3 | Properties 4 | { 5 | [HideInInspector] 6 | _MainTex ("Texture", 2D) = "black" {} 7 | _Background ("Background", 2D) = "black" {} 8 | } 9 | Subshader 10 | { 11 | Pass 12 | { 13 | CGPROGRAM 14 | #pragma vertex vertex_shader 15 | #pragma fragment pixel_shader 16 | #pragma target 2.0 17 | 18 | sampler2D _MainTex,_Background; 19 | 20 | float remap (float x, float a, float b, float c, float d) 21 | { 22 | return (x-a)/(b-a)*(d-c) + c; 23 | } 24 | 25 | float4 vertex_shader (float4 vertex:POSITION):SV_POSITION 26 | { 27 | return mul(UNITY_MATRIX_MVP,vertex); 28 | } 29 | 30 | float4 pixel_shader (float4 vertex:SV_POSITION):COLOR 31 | { 32 | vector uv = vertex.xy/_ScreenParams.xy; 33 | vector uv2 = vertex.xy/_ScreenParams.xy; 34 | uv2.y=1.0-uv2.y; 35 | vector a = tex2D(_MainTex,uv); 36 | vector b = tex2D(_Background,uv2); 37 | float time = fmod(_Time.g,12.0); 38 | if (fmod(floor((uv.y+(uv.x)*remap(cos(time),-1.0,1.0,0.0,2.0))*10.0),2.0)==0.0) 39 | { 40 | if (time<=6.0) return (uv.x uv = vertex.xy/_ScreenParams.xy; 26 | uv.x+=cos(uv.y*2.0+_Time.g)*0.05; 27 | uv.y+=sin(uv.x*2.0+_Time.g)*0.05; 28 | float offset = sin(_Time.g *0.5) * 0.01; 29 | float4 a = tex2D(_MainTex,uv); 30 | float4 b = tex2D(_MainTex,uv-float2(sin(offset),0.0)); 31 | float4 c = tex2D(_MainTex,uv+float2(sin(offset),0.0)); 32 | float4 d = tex2D(_MainTex,uv-float2(0.0,sin(offset))); 33 | float4 e = tex2D(_MainTex,uv+float2(0.0,sin(offset))); 34 | return (a+b+c+d+e)/5.0; 35 | } 36 | ENDCG 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /edge_detection.shader: -------------------------------------------------------------------------------- 1 | //reference: http://blog.ruofeidu.com/simplest-fatest-glsl-edge-detection-using-fwidth/ 2 | 3 | Shader "Edge detection" 4 | { 5 | Properties 6 | { 7 | _MainTex ("Texture", 2D) = "black" {} 8 | } 9 | Subshader 10 | { 11 | Pass 12 | { 13 | CGPROGRAM 14 | #pragma vertex vertex_shader 15 | #pragma fragment pixel_shader 16 | #pragma target 3.0 17 | 18 | sampler2D _MainTex; 19 | 20 | struct structure 21 | { 22 | float4 vertex : SV_POSITION; 23 | float2 uv : TEXCOORD0; 24 | }; 25 | 26 | float sigmoid(float a, float f) 27 | { 28 | return 1.0/(1.0+exp(-f*a)); 29 | } 30 | 31 | structure vertex_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) 32 | { 33 | structure vs; 34 | vs.vertex = UnityObjectToClipPos (vertex); 35 | vs.uv = uv; 36 | return vs; 37 | } 38 | 39 | float4 pixel_shader (structure ps ) : SV_TARGET 40 | { 41 | float2 uv = ps.uv.xy; 42 | float e = length(fwidth(tex2D(_MainTex, uv))); 43 | e = sigmoid(e - 0.2, 32.0); 44 | return float4(float3(e,e,e), 1.0); 45 | } 46 | 47 | ENDCG 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /face.shader: -------------------------------------------------------------------------------- 1 | Shader "Face" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | Cull Off 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | 12 | void vertex_shader (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0) 13 | { 14 | vertex = UnityObjectToClipPos(vertex); 15 | } 16 | 17 | float4 pixel_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0, bool IsFacing:SV_IsFrontFace) : SV_TARGET 18 | { 19 | return IsFacing ? float4(1,0,0,1) : float4(0,0,1,1); 20 | } 21 | ENDCG 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /fibonacci.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | RWStructuredBuffer buffer ; 3 | 4 | int Fibonacci(int n) 5 | { 6 | int a = 0; 7 | int b = 1; 8 | int c = 0; 9 | for (int i = 0; i < n; i++) 10 | { 11 | c = a; 12 | a = b; 13 | b = c + b; 14 | } 15 | return a; 16 | } 17 | 18 | [numthreads(32,1,1)] 19 | void CSMain (int3 id : SV_GroupThreadID) 20 | { 21 | buffer[id.x]=Fibonacci(id.x); 22 | } -------------------------------------------------------------------------------- /fibonacci.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Linq; 3 | 4 | public class fibonacci : MonoBehaviour 5 | { 6 | public ComputeShader computeshader; 7 | 8 | void Start () 9 | { 10 | int[] total = new int[32]; 11 | ComputeBuffer buffer = new ComputeBuffer (32, sizeof(int)); 12 | computeshader.SetBuffer (0, "buffer", buffer); 13 | computeshader.Dispatch (0, 1, 1, 1); 14 | buffer.GetData (total); 15 | buffer.Release(); 16 | Debug.Log("Sum of the first 32 elements of Fibonacci sequence= "+total.Sum()); 17 | } 18 | } -------------------------------------------------------------------------------- /filmic_tonemapping.shader: -------------------------------------------------------------------------------- 1 | //reference: "Filmic Tonemapping for Real-time Rendering" Siggraph 2010 Course by Haarm-Pieter Duiker. 2 | Shader "Filmic tonemapping" 3 | { 4 | Properties 5 | { 6 | _MainTex ("Texture", 2D) = "white" {} 7 | } 8 | Subshader 9 | { 10 | Pass 11 | { 12 | CGPROGRAM 13 | #pragma vertex vertex_shader 14 | #pragma fragment pixel_shader 15 | #pragma target 3.0 16 | 17 | sampler2D _MainTex; 18 | 19 | struct custom_type 20 | { 21 | float4 vertex : SV_POSITION; 22 | float2 uv : TEXCOORD0; 23 | }; 24 | 25 | float4 filmic_tonemapping(float3 x) 26 | { 27 | x = max(float3(0,0,0), x - float3(0.004,0.004,0.004)); 28 | x = (x * (6.2 * x + .5)) / (x * (6.2 * x + 1.7) + 0.06); 29 | return float4(x,1.0); 30 | } 31 | 32 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 33 | { 34 | custom_type vs; 35 | vs.vertex = mul (UNITY_MATRIX_MVP,vertex); 36 | vs.uv = uv; 37 | return vs; 38 | } 39 | 40 | float4 pixel_shader (custom_type ps) : COLOR 41 | { 42 | float2 u = ps.uv.xy; 43 | float4 color = tex2D(_MainTex,u); 44 | return filmic_tonemapping(color); 45 | } 46 | ENDCG 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /fire.cs: -------------------------------------------------------------------------------- 1 | //Apply directly to Main Camera. 2 | using UnityEngine; 3 | 4 | public class fire : MonoBehaviour 5 | { 6 | public Material material; 7 | 8 | void Update() 9 | { 10 | float x = transform.eulerAngles.x * Mathf.Deg2Rad; 11 | float y = transform.eulerAngles.y * Mathf.Deg2Rad; 12 | float z = transform.eulerAngles.z * Mathf.Deg2Rad; 13 | Vector4 angle = new Vector4 (-x,-y,-z,1.0f); 14 | material.SetVector("camera",angle); 15 | } 16 | 17 | void OnRenderImage (RenderTexture source, RenderTexture destination) 18 | { 19 | Graphics.Blit (source, destination, material); 20 | } 21 | } -------------------------------------------------------------------------------- /flow_mapping.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class flow_mapping : MonoBehaviour 4 | { 5 | public Material material; 6 | 7 | float FlowMapOffset0; 8 | float FlowMapOffset1; 9 | 10 | void Start () 11 | { 12 | FlowMapOffset0 = 0.000f; 13 | FlowMapOffset1 = 0.075f; 14 | } 15 | 16 | void Update () 17 | { 18 | FlowMapOffset0 += 0.05f * Time.deltaTime; 19 | FlowMapOffset1 += 0.05f * Time.deltaTime; 20 | if ( FlowMapOffset0 >= 0.15f ) FlowMapOffset0 = 0.0f; 21 | if ( FlowMapOffset1 >= 0.15f ) FlowMapOffset1 = 0.0f; 22 | material.SetFloat("FlowMapOffset0",FlowMapOffset0); 23 | material.SetFloat("FlowMapOffset1",FlowMapOffset1); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /flow_mapping.shader: -------------------------------------------------------------------------------- 1 | //references: 2 | //http://graphicsrunner.blogspot.com/2010/08/water-using-flow-maps.html 3 | //https://mtnphil.wordpress.com/2012/08/25/water-flow-shader/ 4 | 5 | Shader "Flow mapping" 6 | { 7 | Properties 8 | { 9 | _MainTex ("Color Map", 2D) = "black" {} 10 | _FlowMap ("Flow Map", 2D) = "black" {} 11 | } 12 | Subshader 13 | { 14 | Pass 15 | { 16 | CGPROGRAM 17 | #pragma vertex vertex_shader 18 | #pragma fragment pixel_shader 19 | #pragma target 3.0 20 | 21 | sampler2D _MainTex, _FlowMap; 22 | uniform float FlowMapOffset0; 23 | uniform float FlowMapOffset1; 24 | 25 | struct structure 26 | { 27 | float4 vertex : SV_POSITION; 28 | float2 uv : TEXCOORD0; 29 | }; 30 | 31 | structure vertex_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) 32 | { 33 | structure vs; 34 | vs.vertex = UnityObjectToClipPos (vertex); 35 | vs.uv = uv; 36 | return vs; 37 | } 38 | 39 | float4 pixel_shader (structure ps ) : SV_TARGET 40 | { 41 | float HalfCycle = 0.075; 42 | float2 flowmap = tex2D( _FlowMap, ps.uv ).rg * 2.0f - 1.0f; 43 | float phase0 = FlowMapOffset0; 44 | float phase1 = FlowMapOffset1; 45 | float3 A = tex2D(_MainTex, ps.uv + flowmap * phase0 ); 46 | float3 B = tex2D(_MainTex, ps.uv + flowmap * phase1 ); 47 | float flowLerp = ( abs( HalfCycle - FlowMapOffset0 ) / HalfCycle ); 48 | float3 offset = lerp( A, B, flowLerp ); 49 | return float4(offset,1.0); 50 | } 51 | 52 | ENDCG 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /fluid_dynamics.cs: -------------------------------------------------------------------------------- 1 | //Load fluid_dynamics.compute into Unity 3D engine. 2 | using UnityEngine; 3 | 4 | public class fluid_dynamics : MonoBehaviour 5 | { 6 | public ComputeShader compute_shader; 7 | RenderTexture A; 8 | RenderTexture B; 9 | public Material material; 10 | int handle_main; 11 | 12 | void Start() 13 | { 14 | A = new RenderTexture(1024,1024,0); 15 | A.enableRandomWrite = true; 16 | A.Create(); 17 | B = new RenderTexture(1024,1024,0); 18 | B.enableRandomWrite = true; 19 | B.Create(); 20 | handle_main = compute_shader.FindKernel("CSMain"); 21 | } 22 | 23 | void Update() 24 | { 25 | compute_shader.SetTexture(handle_main, "reader", A); 26 | compute_shader.SetTexture(handle_main, "writer", B); 27 | compute_shader.Dispatch(handle_main, A.width / 8, A.height / 8, 1); 28 | compute_shader.SetTexture(handle_main, "reader", B); 29 | compute_shader.SetTexture(handle_main, "writer", A); 30 | compute_shader.Dispatch(handle_main, B.width / 8, B.height / 8, 1); 31 | material.mainTexture = B; 32 | } 33 | } -------------------------------------------------------------------------------- /fog.shader: -------------------------------------------------------------------------------- 1 | Shader "Fog" //Exponential Squared Fog 2 | { 3 | Properties 4 | { 5 | _MainTex ("Base (RGB)", 2D) = "white" {} 6 | _FogColor ("Fog Color (RGB)", Color) = (0.5, 0.5, 0.5, 1.0) 7 | _FogDensity ("Fog Density", Float) = 0.005 8 | } 9 | SubShader 10 | { 11 | Fog { Mode off } 12 | Pass 13 | { 14 | CGPROGRAM 15 | #pragma vertex VS 16 | #pragma fragment PS 17 | 18 | sampler2D _MainTex; 19 | float4 _FogColor; 20 | float _FogDensity; 21 | 22 | void VS (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0,out float depth:TEXCOORD1) 23 | { 24 | vertex = UnityObjectToClipPos(vertex); 25 | depth = vertex.w; 26 | } 27 | 28 | float4 PS (float4 vertex:POSITION,float2 uv:TEXCOORD0,float depth:TEXCOORD1) : SV_TARGET 29 | { 30 | float FogFactor = (_FogDensity / sqrt(log(2.0))) * depth; 31 | FogFactor = saturate(exp2(-FogFactor*FogFactor)); 32 | return lerp(_FogColor, tex2D(_MainTex,uv), FogFactor); 33 | } 34 | ENDCG 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /font.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/font.png -------------------------------------------------------------------------------- /font.shader: -------------------------------------------------------------------------------- 1 | //source:https://www.shadertoy.com/view/4sBfRd 2 | Shader "Font" 3 | { 4 | Properties 5 | { 6 | _MainTex ("Texture", 2D) = "white" {} //assign font.png (texture source: www.shadertoy.com) 7 | } 8 | Subshader 9 | { 10 | Pass 11 | { 12 | CGPROGRAM 13 | 14 | #pragma vertex vertex_shader 15 | #pragma fragment pixel_shader 16 | #pragma target 2.0 17 | 18 | sampler2D _MainTex; 19 | 20 | struct custom_type 21 | { 22 | float4 vertex : SV_POSITION; 23 | float2 uv : TEXCOORD0; 24 | }; 25 | 26 | custom_type vertex_shader (float4 vertex : POSITION, float2 uv : TEXCOORD0) 27 | { 28 | custom_type vs; 29 | vs.vertex = UnityObjectToClipPos (vertex); 30 | vs.uv = uv; 31 | return vs; 32 | } 33 | 34 | float4 pixel_shader (custom_type ps) : COLOR 35 | { 36 | uint t[12] = {72,69,76,76,79,32,87,79,82,76,68,33}; //every number is another char 37 | float2 position = float2(0.5,1.0); //xy coordinates (x: 0.0->1.0 y: 0.0->2.0 ) 38 | float2 U = ps.uv.xy*2.0-position; 39 | float c = 0.0; 40 | float3 d = float3(1.0,1.0,1.0); //RGB font color 41 | for (int i=0; i<12;i++,U.x-=.1) 42 | { 43 | c +=(length(U-.06)<.06)?tex2D(_MainTex,U*.5+frac(float2(t[i],15-t[i]/16)/16.)).x:0.0; 44 | } 45 | return float4(c*d.x,c*d.y,c*d.z,1.0); 46 | } 47 | ENDCG 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /fractal_image.shader: -------------------------------------------------------------------------------- 1 | // Reference: http://glslsandbox.com/e#29611.0 2 | Shader "Fractal Image" 3 | { 4 | Subshader 5 | { 6 | Pass 7 | { 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | #pragma target 4.0 12 | 13 | struct custom_type 14 | { 15 | float4 vertex : SV_POSITION; 16 | float2 uv : TEXCOORD0; 17 | }; 18 | 19 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 20 | { 21 | custom_type vs; 22 | vs.vertex = UnityObjectToClipPos (vertex); 23 | vs.uv = uv; 24 | return vs; 25 | } 26 | 27 | float4 pixel_shader (custom_type ps) : SV_TARGET 28 | { 29 | float4 c = float4(ps.uv.xy,1.5,0); 30 | for (int i = 0; i < 234; i++) 31 | { 32 | c.xzy = float3(1.3,1.0,0.78)*abs(c.xyz/dot(c,c)-float3(1,1,0)); 33 | } 34 | return c; 35 | } 36 | 37 | ENDCG 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /grayscale.shader: -------------------------------------------------------------------------------- 1 | Shader "Grayscale" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "white" {} 6 | } 7 | Subshader 8 | { 9 | Pass 10 | { 11 | Cull off 12 | CGPROGRAM 13 | #pragma vertex vertex_shader 14 | #pragma fragment pixel_shader 15 | #pragma target 2.0 16 | 17 | sampler2D _MainTex; 18 | 19 | struct custom_type 20 | { 21 | float4 vertex : SV_POSITION; 22 | float2 uv : TEXCOORD0; 23 | }; 24 | 25 | custom_type vertex_shader (float4 vertex : POSITION, float2 uv : TEXCOORD0) 26 | { 27 | custom_type vs; 28 | vs.vertex = mul (UNITY_MATRIX_MVP,vertex); 29 | vs.uv = uv; 30 | return vs; 31 | } 32 | 33 | float4 pixel_shader (custom_type ps) : COLOR 34 | { 35 | float3 color = tex2D(_MainTex,ps.uv.xy).xyz; 36 | float grayscale = dot(color, float3(0.2126, 0.7152, 0.0722)); 37 | return float4(grayscale,grayscale,grayscale,1.0); 38 | } 39 | ENDCG 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /implicit.shader: -------------------------------------------------------------------------------- 1 | // Reference: http://iquilezles.org/www/articles/distance/distance.htm 2 | // Rendering 2D implicit equations 3 | 4 | Shader "Implicit" 5 | { 6 | SubShader 7 | { 8 | Pass 9 | { 10 | CGPROGRAM 11 | #pragma vertex VSMain 12 | #pragma fragment PSMain 13 | #pragma target 3.0 14 | 15 | float f (float2 p) 16 | { 17 | //for circle with radius 0.5 : 18 | return p.x * p.x + p.y * p.y - 0.5; 19 | //for line Ax+By+C=0, where A=8.0, B=4.0, C=1.0 : 20 | // return 8.0 * p.x + 4.0 * p.y + 1.0; 21 | } 22 | 23 | float2 grad (float2 x) 24 | { 25 | float2 h = float2( 0.01, 0.0 ); 26 | return float2( f(x+h.xy) - f(x-h.xy), f(x+h.yx) - f(x-h.yx) )/(2.0*h.x); 27 | } 28 | 29 | float shape (float2 x) 30 | { 31 | float v = f(x); 32 | float2 g = grad(x); 33 | float de = abs(v)/length(g); 34 | return 1.0 - smoothstep( 0.01, 0.02, de ); 35 | } 36 | 37 | void VSMain (inout float4 vertex:POSITION, inout float2 uv:TEXCOORD0) 38 | { 39 | vertex = UnityObjectToClipPos(vertex); 40 | } 41 | 42 | float4 PSMain (float4 vertex:POSITION, float2 uv:TEXCOORD0) : SV_TARGET 43 | { 44 | uv = float2(2.0*uv-1.0); 45 | return float4(shape(uv), 0.0, 0.0, 1.0); 46 | } 47 | ENDCG 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /integer_hash.shader: -------------------------------------------------------------------------------- 1 | //source: https://www.shadertoy.com/view/XlXcW4 2 | Shader "Integer Hash" 3 | { 4 | Subshader 5 | { 6 | Pass 7 | { 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | #pragma target 4.0 12 | 13 | static const uint k = 1103515245U; 14 | 15 | struct custom_type 16 | { 17 | float4 vertex : SV_POSITION; 18 | float2 uv : TEXCOORD0; 19 | }; 20 | 21 | float3 hash( uint3 x ) 22 | { 23 | x = ((x>>8U)^x.yzx)*k; 24 | x = ((x>>8U)^x.yzx)*k; 25 | x = ((x>>8U)^x.yzx)*k; 26 | return float3(x)*(1.0/float(0xffffffffU)); 27 | } 28 | 29 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 30 | { 31 | custom_type vs; 32 | vs.vertex = UnityObjectToClipPos (vertex); 33 | vs.uv = uv; 34 | return vs; 35 | } 36 | 37 | float4 pixel_shader (custom_type ps) : COLOR 38 | { 39 | uint2 resolution = uint2 (1024,1024); 40 | uint3 p = uint3(ps.uv.xy*resolution,_Time.g*60); 41 | return float4(hash(p),1.0); 42 | } 43 | ENDCG 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /isolines2.shader: -------------------------------------------------------------------------------- 1 | Shader "Isolines 2" 2 | { 3 | Properties 4 | { 5 | _BaseTexture ("Base Texture", 2D) = "black" {} 6 | _thickness("Contour Thickness",Range (0.1,2.0)) = 1.0 7 | _density("Contour Density",Range (1.0,50.0)) = 10.0 8 | } 9 | Subshader 10 | { 11 | Pass 12 | { 13 | CGPROGRAM 14 | #pragma vertex vertex_shader 15 | #pragma fragment pixel_shader 16 | #pragma target 3.0 17 | 18 | struct structure 19 | { 20 | float4 vertex : SV_POSITION; 21 | float2 uv : TEXCOORD0; 22 | }; 23 | 24 | sampler2D _BaseTexture; 25 | float _thickness, _density; 26 | 27 | structure vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 28 | { 29 | structure vs; 30 | vs.vertex = UnityObjectToClipPos (vertex); 31 | vs.uv = uv; 32 | return vs; 33 | } 34 | 35 | float4 pixel_shader (structure ps) : COLOR 36 | { 37 | float3 color = tex2Dgrad(_BaseTexture,ps.uv,ddx(ps.uv.x),ddy(ps.uv.y)).rgb; 38 | float3 f = abs(frac(color*_density)-0.5); 39 | float3 df = fwidth(color*_density); 40 | float a = max(0.0,_thickness-1.0); 41 | float b = max(1.0,_thickness); 42 | float3 g = clamp((f-df*a)/(df*(b-a)),max(0.0,1.0-_thickness),1.0); 43 | float c = g.x * g.y * g.z; 44 | return float4(c, c, c, 1.0); 45 | } 46 | ENDCG 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /map_trees.cs: -------------------------------------------------------------------------------- 1 | //Script with shader generate map of trees painted on selected terrain. 2 | //To see results, assign script with material to gameobject and set variables. 3 | //Then create custom render texture,with material unlit,initialization mode realtime, 4 | //source material,set material with shader("map_trees"), update mode realtime. 5 | //Finally, to show content of custom render texture, just set it as input texture 6 | //into any material. 7 | //Written by Przemyslaw Zaworski 8 | 9 | using UnityEngine; 10 | 11 | public class map_trees : MonoBehaviour 12 | { 13 | public Material material; 14 | public Terrain terrain; 15 | RenderTexture A; 16 | RenderTexture B; 17 | 18 | void Start() 19 | { 20 | TerrainData terrain_data = terrain.GetComponent().terrainData; 21 | A = new RenderTexture(1024,1024,0); 22 | A.Create(); 23 | B = new RenderTexture(1024,1024,0); 24 | B.Create(); 25 | 26 | foreach(TreeInstance tree in terrain_data.treeInstances) 27 | { 28 | Vector4 point = new Vector4 (tree.position.x,tree.position.z,0.0f,0.0f); 29 | material.SetVector("_tree", point); 30 | material.SetTexture("_map", A); 31 | Graphics.Blit(A,B,material); 32 | material.SetTexture("_map", B); 33 | Graphics.Blit(B,A,material); 34 | } 35 | } 36 | 37 | void OnDestroy() 38 | { 39 | A.Release(); 40 | B.Release(); 41 | } 42 | } -------------------------------------------------------------------------------- /map_trees.shader: -------------------------------------------------------------------------------- 1 | Shader "Map Trees" 2 | { 3 | Properties 4 | { 5 | [HideInInspector] 6 | _tree ("Tree", Vector) = (0.0,0.0,0.0,0.0) 7 | [HideInInspector] 8 | _map ("Texture", 2D) = "black" {} 9 | 10 | } 11 | SubShader 12 | { 13 | Pass 14 | { 15 | CGPROGRAM 16 | #pragma vertex vertex_shader 17 | #pragma fragment pixel_shader 18 | 19 | struct structure 20 | { 21 | float4 vertex:SV_POSITION; 22 | float2 uv : TEXCOORD0; 23 | }; 24 | 25 | sampler2D _map; 26 | float4 _tree; 27 | 28 | float circle(float2 d, float r) 29 | { 30 | return 1.0-smoothstep(r-(r*0.01),r+(r*0.01),dot(d,d)); 31 | } 32 | 33 | void vertex_shader(float4 vertex:POSITION,float2 uv:TEXCOORD0,out structure vs) 34 | { 35 | vs.vertex = UnityObjectToClipPos(vertex); 36 | vs.uv = uv; 37 | } 38 | 39 | void pixel_shader(in structure ps, out float4 fragColor:SV_Target0) 40 | { 41 | float2 uv = float2(ps.uv.xy); 42 | float k = circle(uv-_tree.xy,0.0002); 43 | float3 color = float3(k,k,k)*float3(0.0,1.0,0.0); 44 | color+=tex2Dlod(_map,float4(uv,0.0,0.0)); 45 | fragColor = float4(color,1.0); 46 | } 47 | ENDCG 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /matrix_example.shader: -------------------------------------------------------------------------------- 1 | Shader "Matrix example" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | Cull Off 8 | HLSLPROGRAM 9 | #pragma vertex v 10 | #pragma fragment p 11 | 12 | void v (uint i: SV_VertexID, out float4 t: position) 13 | { 14 | t = float4(float2((i<< 1) & 2, i & 2) * float2(2, -2) - float2(1, -1), 0, 1); 15 | } 16 | 17 | void p (out float4 c:COLOR) 18 | { 19 | matrix a = { 1.0, 2.0, 0.0, 1.0 }; 20 | matrix b = { 1.0, 0.0}; 21 | c = float4(mul(a,b),0,1); //red fullscreen quad 22 | } 23 | 24 | ENDHLSL 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /maze.shader: -------------------------------------------------------------------------------- 1 | Shader "Maze" 2 | { 3 | Properties 4 | { 5 | _scale ("Scale", Range(1.0,60.0)) = 30 6 | _hash ("Seed", Range(500.0,2000.0)) = 1000 7 | _color ("Color", Color) = (1.0,0.0,0.0,1.0) 8 | } 9 | Subshader 10 | { 11 | Pass 12 | { 13 | CGPROGRAM 14 | #pragma vertex vertex_shader 15 | #pragma fragment pixel_shader 16 | #pragma target 4.0 17 | 18 | struct SHADERDATA 19 | { 20 | float4 vertex : SV_POSITION; 21 | float2 uv : TEXCOORD0; 22 | }; 23 | 24 | float _scale, _hash; 25 | float4 _color; 26 | 27 | SHADERDATA vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 28 | { 29 | SHADERDATA vs; 30 | vs.vertex = UnityObjectToClipPos (vertex); 31 | vs.uv = uv; 32 | return vs; 33 | } 34 | 35 | float4 pixel_shader (SHADERDATA ps) : SV_TARGET 36 | { 37 | float2 uv = ps.uv * _scale; 38 | float hash = sin(12345.67*sin(_hash*length(ceil(uv)))); 39 | float pattern = cos(3.14159265*(uv.y+uv.x*sign(hash))); 40 | return _color / pattern; 41 | } 42 | 43 | ENDCG 44 | 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /mesh_deformation.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | 3 | RWStructuredBuffer local_vertices_buffer; 4 | RWStructuredBuffer world_vertices_buffer; 5 | RWStructuredBuffer brush_center_buffer; 6 | 7 | [numthreads(1024,1,1)] 8 | void CSMain (uint3 id : SV_DispatchThreadID) 9 | { 10 | float delta = distance(brush_center_buffer[0], world_vertices_buffer[id.x]); 11 | if (delta<0.75) 12 | local_vertices_buffer[id.x] = float3(local_vertices_buffer[id.x].x,local_vertices_buffer[id.x].y-0.001,local_vertices_buffer[id.x].z); 13 | else 14 | local_vertices_buffer[id.x] = float3(local_vertices_buffer[id.x].x,local_vertices_buffer[id.x].y,local_vertices_buffer[id.x].z); 15 | } 16 | -------------------------------------------------------------------------------- /mesh_unwrapping.shader: -------------------------------------------------------------------------------- 1 | Shader "Mesh unwrapping" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "black" {} 6 | } 7 | SubShader 8 | { 9 | Tags { "RenderType"="Opaque" } 10 | Pass 11 | { 12 | Cull Off 13 | CGPROGRAM 14 | #pragma vertex vertex_shader 15 | #pragma fragment pixel_shader 16 | #pragma target 3.0 17 | 18 | struct structure 19 | { 20 | float4 vertex:SV_POSITION; 21 | float2 uv : TEXCOORD0; 22 | }; 23 | 24 | Texture2D _MainTex; 25 | SamplerState sampler_MainTex; 26 | float4 _MainTex_ST; 27 | 28 | void vertex_shader(in float4 vertex:POSITION,in float2 uv:TEXCOORD0,out structure vs) 29 | { 30 | vs.vertex = float4(uv.x,1.0-uv.y, 0.01, vertex.w); 31 | vs.uv = uv; 32 | } 33 | 34 | void pixel_shader(in structure ps, out float4 fragColor:SV_Target0) 35 | { 36 | float2 uv = ps.uv.xy; 37 | fragColor =_MainTex.Sample(sampler_MainTex, ps.uv); 38 | } 39 | 40 | ENDCG 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /mirror_effect.shader: -------------------------------------------------------------------------------- 1 | Shader "Mirror Effect" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "white" {} 6 | [KeywordEnum(Vertical,Horizontal)] _mode("Mode", Float) = 0 7 | } 8 | SubShader 9 | { 10 | Cull Off 11 | Tags {"RenderType"="Opaque"} 12 | Pass 13 | { 14 | CGPROGRAM 15 | #pragma vertex vertex_shader 16 | #pragma fragment pixel_shader 17 | #pragma target 3.0 18 | 19 | sampler2D _MainTex; 20 | float _mode; 21 | 22 | struct structure 23 | { 24 | float4 vertex : SV_POSITION; 25 | float2 uv : TEXCOORD0; 26 | }; 27 | 28 | structure vertex_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) 29 | { 30 | structure vs; 31 | vs.vertex = UnityObjectToClipPos( vertex ); 32 | vs.uv = uv; 33 | return vs; 34 | } 35 | 36 | float4 pixel_shader(structure ps) : COLOR 37 | { 38 | float2 uv = ps.uv.xy; 39 | if (_mode==0.0) 40 | { 41 | if (uv.x>0.5) 42 | return tex2D(_MainTex,float2(0.5-(uv.x-0.5),uv.y)); 43 | else 44 | return tex2D(_MainTex,uv); 45 | } 46 | else 47 | { 48 | if (uv.y>0.5) 49 | return tex2D(_MainTex,float2(uv.x,0.5-(uv.y-0.5))); 50 | else 51 | return tex2D(_MainTex,uv); 52 | } 53 | } 54 | ENDCG 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /mrt.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class mrt : MonoBehaviour 4 | { 5 | public RenderTexture[] RenderTextures; 6 | public Material material; 7 | 8 | void MRT (RenderTexture[] rt, Material mat, int pass = 0) 9 | { 10 | RenderBuffer[] rb = new RenderBuffer[rt.Length]; 11 | for(int i = 0; i < rt.Length; i++) rb[i] = rt[i].colorBuffer; 12 | Graphics.SetRenderTarget(rb, rt[0].depthBuffer); 13 | GL.Clear(true, true, Color.clear); 14 | GL.PushMatrix(); 15 | GL.LoadOrtho(); 16 | mat.SetPass(pass); 17 | GL.Begin(GL.QUADS); 18 | GL.Vertex3(0.0f, 0.0f, 0.1f); 19 | GL.TexCoord2(0.0f, 0.0f); 20 | GL.Vertex3(1.0f, 0.0f, 0.1f); 21 | GL.TexCoord2(1.0f, 0.0f); 22 | GL.Vertex3(1.0f, 1.0f, 0.1f); 23 | GL.TexCoord2(1.0f, 1.0f); 24 | GL.Vertex3(0.0f, 1.0f, 0.1f); 25 | GL.TexCoord2(0.0f, 1.0f); 26 | GL.End(); 27 | GL.PopMatrix(); 28 | } 29 | 30 | void Update () 31 | { 32 | MRT(RenderTextures,material,0); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /mrt.shader: -------------------------------------------------------------------------------- 1 | Shader "MRT" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | ZTest Always Cull Off ZWrite Off 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | #pragma exclude_renderers nomrt 12 | #pragma target 3.0 13 | 14 | struct structureVS 15 | { 16 | float4 vertex : SV_POSITION; 17 | float2 uv : TEXCOORD0; 18 | }; 19 | 20 | struct structurePS 21 | { 22 | float4 target00 : SV_Target0; 23 | float4 target01 : SV_Target1; 24 | float4 target02 : SV_Target2; 25 | float4 target03 : SV_Target3; 26 | }; 27 | 28 | structureVS vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 29 | { 30 | structureVS vs; 31 | vs.vertex = UnityObjectToClipPos( vertex ); 32 | vs.uv = uv; 33 | return vs; 34 | } 35 | 36 | structurePS pixel_shader (structureVS vs) 37 | { 38 | structurePS ps; 39 | ps.target00 = float4(1,0,0,1); 40 | ps.target01 = float4(0,1,0,1); 41 | ps.target02 = float4(0,0,1,1); 42 | ps.target03 = float4(1,1,1,1); 43 | return ps; 44 | } 45 | ENDCG 46 | } 47 | } 48 | FallBack "Diffuse" 49 | } -------------------------------------------------------------------------------- /negation.shader: -------------------------------------------------------------------------------- 1 | Shader "Negation" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "white" {} 6 | } 7 | Subshader 8 | { 9 | Pass 10 | { 11 | CGPROGRAM 12 | #pragma vertex vertex_shader 13 | #pragma fragment pixel_shader 14 | #pragma target 3.0 15 | 16 | sampler2D _MainTex : register(s0); 17 | 18 | struct custom_type 19 | { 20 | float4 vertex : SV_POSITION; 21 | float2 uv : TEXCOORD0; 22 | }; 23 | 24 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 25 | { 26 | custom_type vs; 27 | vs.vertex = mul (UNITY_MATRIX_MVP,vertex); 28 | vs.uv=uv; 29 | return vs; 30 | } 31 | 32 | float4 pixel_shader (custom_type ps) : COLOR 33 | { 34 | return float4 (1,1,1,1)-tex2D(_MainTex,ps.uv); 35 | } 36 | 37 | ENDCG 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /no_interpolation.shader: -------------------------------------------------------------------------------- 1 | Shader "No interpolation" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 4.0 11 | 12 | struct SHADERDATA 13 | { 14 | linear float4 Vertex : SV_POSITION; 15 | nointerpolation float4 Point : TEXCOORD0; 16 | }; 17 | 18 | SHADERDATA vertex_shader (float4 vertex:POSITION) 19 | { 20 | SHADERDATA vs; 21 | vs.Vertex = UnityObjectToClipPos (vertex); 22 | vs.Point = vertex; 23 | return vs; 24 | } 25 | 26 | void pixel_shader (in SHADERDATA ps, out float4 result : SV_Target0 ) 27 | { 28 | result = ps.Point; 29 | } 30 | 31 | ENDCG 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /noise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/noise.png -------------------------------------------------------------------------------- /noise.shader: -------------------------------------------------------------------------------- 1 | Shader "Noise Grayscale" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 2.0 11 | 12 | struct custom_type 13 | { 14 | float4 clip_space_vertex : SV_POSITION; 15 | float2 uv : TEXCOORD0; 16 | }; 17 | 18 | custom_type vertex_shader (float4 object_space_vertex : POSITION, float2 uv : TEXCOORD0) 19 | { 20 | custom_type vs; 21 | vs.clip_space_vertex = mul (UNITY_MATRIX_MVP,object_space_vertex); 22 | vs.uv=uv; 23 | return vs; 24 | } 25 | 26 | float4 pixel_shader (custom_type ps) : SV_TARGET 27 | { 28 | float noise = frac(sin(dot(ps.uv.xy,float2(12.9898,78.233))) * 43758.5453); 29 | return float4 (noise,noise,noise,1.0); 30 | } 31 | 32 | ENDCG 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /noise_transition.shader: -------------------------------------------------------------------------------- 1 | //Play to see the effect. 2 | Shader "Noise Transition" 3 | { 4 | Subshader 5 | { 6 | Pass 7 | { 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | #pragma target 3.0 12 | 13 | struct type 14 | { 15 | float4 vertex : SV_POSITION; 16 | float2 uv : TEXCOORD0; 17 | }; 18 | 19 | float hash(float2 n) 20 | { 21 | return frac(sin(dot(n, float2(12.9898,4.1414)))*43758.5453); 22 | } 23 | 24 | type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 25 | { 26 | type vs; 27 | vs.vertex = mul (UNITY_MATRIX_MVP,vertex); 28 | vs.uv = uv; 29 | return vs; 30 | } 31 | 32 | float4 pixel_shader (type ps) : COLOR 33 | { 34 | float2 uv = ps.uv.xy+float2(floor(_Time.g),1.0); 35 | float2 s = ceil(uv*10.0)/10.0; 36 | float t = hash(s); 37 | float a = -sign(fmod(_Time.g,2.0)-t); 38 | float b = sign(fmod(_Time.g-1.0,2.0)-t); 39 | float4 c = {a,a,a,1.0}; 40 | float4 d = {b,b,b,1.0}; 41 | if (fmod(_Time.g,2.0)<1.0) return c; 42 | else return d; 43 | } 44 | ENDCG 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /normal_extrusion.shader: -------------------------------------------------------------------------------- 1 | Shader "Normal extrusion" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Extrusion map", 2D) = "white" {} 6 | _color("Color intensity",Range(0.0,1.0)) = 0.5 7 | _displacement("Displacement",Range(0.0,8.0)) = 2.0 8 | } 9 | Subshader 10 | { 11 | Pass 12 | { 13 | CGPROGRAM 14 | #pragma vertex vertex_shader 15 | #pragma fragment pixel_shader 16 | #pragma target 3.0 17 | 18 | sampler2D _MainTex; 19 | float _displacement; 20 | float _color; 21 | 22 | struct structure 23 | { 24 | float4 vertex : SV_POSITION; 25 | float2 uv : TEXCOORD0; 26 | float3 normal : TEXCOORD1; 27 | float height : TEXCOORD2; 28 | }; 29 | 30 | structure vertex_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0,float3 normal:NORMAL) 31 | { 32 | structure vs; 33 | float offset=tex2Dlod (_MainTex,float4(uv.xy,0,0)).r*_displacement; 34 | vertex.xyz+=normal*offset; 35 | vs.height=offset; 36 | vs.vertex=UnityObjectToClipPos(vertex); 37 | vs.uv=uv; 38 | vs.normal=normal; 39 | return vs; 40 | } 41 | 42 | float4 pixel_shader (structure ps) : COLOR 43 | { 44 | float2 uv = ps.uv.xy; 45 | float h = ps.height*_color; 46 | return float4(h,h,h,1.0); 47 | } 48 | ENDCG 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /notebook_drawings.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class notebook_drawings : MonoBehaviour 5 | { 6 | public Material material; 7 | 8 | void OnRenderImage (RenderTexture source, RenderTexture destination) 9 | { 10 | Graphics.Blit (source, destination, material); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /orthogonal_sphere.shader: -------------------------------------------------------------------------------- 1 | Shader "Orthogonal sphere" 2 | { 3 | Properties 4 | { 5 | _lightcolor("Light Color", Color) = (0.0,1.0,0.0,1.0) 6 | _lightposition("Light Position", Vector) = (8.0,0.0,10.0,1.0) 7 | } 8 | Subshader 9 | { 10 | Pass 11 | { 12 | CGPROGRAM 13 | #pragma vertex vertex_shader 14 | #pragma fragment pixel_shader 15 | #pragma target 2.0 16 | 17 | float4 _lightcolor, _lightposition; 18 | 19 | struct type 20 | { 21 | float4 vertex : SV_POSITION; 22 | float2 uv : TEXCOORD0; 23 | }; 24 | 25 | float3 sphere (float2 p, float s) 26 | { 27 | float z = sqrt(abs(s*s-dot(p,p))); 28 | return float3(p,z); 29 | } 30 | 31 | float3 map (float2 p) 32 | { 33 | return sphere(p,0.5); 34 | } 35 | 36 | float4 lighting (float2 p) 37 | { 38 | float3 AmbientLight = float3 (0.0,0.0,0.0); 39 | float3 LightDirection = normalize(_lightposition); 40 | float3 NormalDirection = normalize(map(p)); 41 | float3 LightColor = _lightcolor.xyz; 42 | float3 DiffuseColor = max(dot(NormalDirection,LightDirection),0.0)*LightColor+AmbientLight; 43 | return float4(DiffuseColor,1.0); 44 | } 45 | 46 | type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 47 | { 48 | type vs; 49 | vs.vertex = mul(UNITY_MATRIX_MVP,vertex); 50 | vs.uv = uv; 51 | return vs; 52 | } 53 | 54 | float4 pixel_shader (type ps) : SV_TARGET 55 | { 56 | float2 uv = 2.0*ps.uv.xy-1.0; 57 | return lighting(uv); 58 | } 59 | ENDCG 60 | } 61 | } 62 | } -------------------------------------------------------------------------------- /outline.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class outline : MonoBehaviour 6 | { 7 | public GameObject[] game_object; 8 | 9 | void Update () 10 | { 11 | RaycastHit hit; 12 | Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 13 | if ( Physics.Raycast (ray,out hit,100.0f)) 14 | { 15 | for (int i=0;i().material.SetFloat("_enable",1.0f); 20 | } 21 | else 22 | { 23 | game_object[i].GetComponent().material.SetFloat("_enable",0.0f); 24 | } 25 | } 26 | } 27 | else 28 | { 29 | for (int i=0;i().material.SetFloat("_enable",0.0f); 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /paint.cs: -------------------------------------------------------------------------------- 1 | //Assign paint script to Main Camera. Then set material with paint.shader 2 | using UnityEngine; 3 | 4 | [ExecuteInEditMode] 5 | public class paint : MonoBehaviour 6 | { 7 | public Material material; 8 | Vector4 iMouse; 9 | 10 | void Update () 11 | { 12 | if (Input.GetMouseButton(0)) 13 | { 14 | iMouse = new Vector4 (Input.mousePosition.x/Screen.width,1.0f-Input.mousePosition.y/Screen.height,-1.0f,-1.0f) ; 15 | material.SetVector("iMouse",iMouse); 16 | } 17 | } 18 | 19 | void OnRenderImage (RenderTexture source, RenderTexture destination) 20 | { 21 | Graphics.Blit (source, destination, material); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /paint.shader: -------------------------------------------------------------------------------- 1 | Shader "Paint" 2 | { 3 | Properties 4 | { 5 | [HideInInspector] 6 | iMouse("iMouse",Vector) = (-1.0,-1.0,0.0,0.0) 7 | brush("Brush Color",Color) = (1,1,1,1) 8 | size("Brush Size",Range(0.001,0.050)) = 0.005 9 | [KeywordEnum(Circle,Circle2,Rect)] shapes("Brush shapes", Float) = 0 10 | 11 | } 12 | Subshader 13 | { 14 | Pass 15 | { 16 | CGPROGRAM 17 | #pragma vertex vertex_shader 18 | #pragma fragment pixel_shader 19 | #pragma target 4.0 20 | 21 | float4 iMouse; 22 | float4 brush; 23 | float size; 24 | float shapes; 25 | 26 | float4 vertex_shader (float4 vertex:POSITION):SV_POSITION 27 | { 28 | return UnityObjectToClipPos (vertex); 29 | } 30 | 31 | float4 pixel_shader (float4 vertex:SV_POSITION):SV_TARGET 32 | { 33 | float2 uv=vertex.xy/_ScreenParams.xy; 34 | switch(shapes) 35 | { 36 | case 0: 37 | { 38 | float circle=length(iMouse.xy-uv.xy); 39 | if (circle>size) discard; 40 | return brush; 41 | } 42 | case 1: 43 | { 44 | float circle=length(iMouse.xy-uv.xy); 45 | circle = smoothstep(circle,size-0.002,size); 46 | if (circle>size) discard; 47 | return brush; 48 | } 49 | case 2: 50 | { 51 | float2 rect = abs(iMouse.xy-uv.xy); 52 | if (any(rect>size)) discard; 53 | return brush; 54 | } 55 | default: 56 | return float4(0,0,0,1); 57 | } 58 | 59 | } 60 | ENDCG 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /paint3D.cs: -------------------------------------------------------------------------------- 1 | //Basic template for painting materials in 3D. 2 | //Target game object must have attached mesh collider. 3 | 4 | using UnityEngine; 5 | 6 | public class paint3D : MonoBehaviour 7 | { 8 | public Camera MainCamera; 9 | public Material material; 10 | public GameObject target; 11 | 12 | void Update () 13 | { 14 | if (!Input.GetMouseButton(0)) 15 | return; 16 | RaycastHit hit; 17 | if (!Physics.Raycast(MainCamera.ScreenPointToRay(Input.mousePosition), out hit)) 18 | return; 19 | if (hit.distance<2.0f && (hit.collider.gameObject.name==target.name)) 20 | material.SetVector("_vector", new Vector4(hit.textureCoord.x,hit.textureCoord.y,0.0f,0.0f)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /paint3D.shader: -------------------------------------------------------------------------------- 1 | Shader "Paint 3D" 2 | { 3 | Properties 4 | { 5 | [HideInInspector] 6 | _vector ("Vector", Vector) = (-10,-10,0,0) 7 | } 8 | Subshader 9 | { 10 | Pass 11 | { 12 | CGPROGRAM 13 | #pragma vertex vertex_shader 14 | #pragma fragment pixel_shader 15 | #pragma target 3.0 16 | 17 | float4 _vector; 18 | 19 | struct structure 20 | { 21 | float4 vertex : SV_POSITION; 22 | float2 uv : TEXCOORD0; 23 | }; 24 | 25 | structure vertex_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) 26 | { 27 | structure vs; 28 | vs.vertex = UnityObjectToClipPos (vertex); 29 | vs.uv = uv; 30 | return vs; 31 | } 32 | 33 | float4 pixel_shader (structure ps ) : SV_TARGET 34 | { 35 | float t = length(ps.uv-_vector.xy)*3.0; 36 | return float4(t,t,0.0,1.0); 37 | } 38 | 39 | ENDCG 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /pixelation.shader: -------------------------------------------------------------------------------- 1 | Shader "Pixelation" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "white" {} 6 | size ("Pixel size",Range(1.0,1000.0)) = 100.0 7 | } 8 | Subshader 9 | { 10 | Pass 11 | { 12 | CGPROGRAM 13 | #pragma vertex vertex_shader 14 | #pragma fragment pixel_shader 15 | #pragma target 2.0 16 | 17 | sampler2D _MainTex; 18 | float size; 19 | 20 | struct custom_type 21 | { 22 | float4 vertex : SV_POSITION; 23 | float2 uv : TEXCOORD0; 24 | }; 25 | 26 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 27 | { 28 | custom_type vs; 29 | vs.vertex = UnityObjectToClipPos (vertex); 30 | vs.uv = uv; 31 | return vs; 32 | } 33 | 34 | float4 pixel_shader (custom_type ps) : COLOR 35 | { 36 | return tex2D(_MainTex,ceil(ps.uv.xy*size)/size); 37 | } 38 | ENDCG 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /point_cloud.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class point_cloud : MonoBehaviour 4 | { 5 | public Material material; 6 | protected int number = 20000000; 7 | protected ComputeBuffer compute_buffer; 8 | 9 | struct Point 10 | { 11 | public Vector3 position; 12 | } 13 | 14 | void Start () 15 | { 16 | compute_buffer = new ComputeBuffer(number, sizeof(float)*3, ComputeBufferType.Default); 17 | Point[] cloud = new Point[number]; 18 | for (uint i=0; i cloud; 25 | 26 | struct structurePS 27 | { 28 | half4 albedo : SV_Target0; 29 | half4 specular : SV_Target1; 30 | half4 normal : SV_Target2; 31 | half4 emission : SV_Target3; 32 | }; 33 | 34 | float4 vertex_shader (uint id : SV_VertexID) : SV_POSITION 35 | { 36 | float3 position = cloud[id]; 37 | position.xz+=2.0*length(position.xz)*cos(_Time.g); 38 | return UnityObjectToClipPos(float4(position,1.0)); 39 | } 40 | 41 | structurePS pixel_shader (void) 42 | { 43 | structurePS ps; 44 | ps.albedo = half4( 0,0,0,0 ); 45 | ps.specular = half4( 0,0,0,0 ); 46 | ps.normal = half4( 0,0,0,0 ); 47 | ps.emission = half4(_Color,1); 48 | return ps; 49 | } 50 | ENDCG 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /polygon.shader: -------------------------------------------------------------------------------- 1 | Shader "Polygon" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex VSMain 9 | #pragma fragment PSMain 10 | 11 | static const float2 vertices[4] = {{0.2f,0.6f},{0.7f,0.2f},{0.8f,0.5f},{0.4f,0.7f}}; 12 | 13 | float polygon( float2 v[4], float2 p ) 14 | { 15 | const int num = 4; 16 | float d = dot(p-v[0],p-v[0]); 17 | float s = 1.0; 18 | for( int i=0, j=num-1; i cond = { p.y>=v[i].y, p.ye.y*w.x }; 25 | if( all(cond) || all(!(cond)) ) s*=-1.0; 26 | } 27 | return s*sqrt(d); 28 | } 29 | 30 | void VSMain (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0) 31 | { 32 | vertex = UnityObjectToClipPos(vertex); 33 | } 34 | 35 | float4 PSMain (float4 vertex:POSITION,float2 uv:TEXCOORD0) : SV_TARGET 36 | { 37 | float d = polygon(vertices, uv); 38 | float3 k = step(sign(d),float3(0.0,0.0,0.0)); 39 | return float4(k,1); 40 | } 41 | ENDCG 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /posterize.shader: -------------------------------------------------------------------------------- 1 | Shader "Posterize" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "white" {} 6 | _steps ("Steps",Float) = 5.0 7 | } 8 | Subshader 9 | { 10 | Pass 11 | { 12 | CGPROGRAM 13 | #pragma vertex vertex_shader 14 | #pragma fragment pixel_shader 15 | #pragma target 3.0 16 | 17 | sampler2D _MainTex; 18 | float _steps; 19 | 20 | struct structure 21 | { 22 | float4 vertex : SV_POSITION; 23 | float2 uv : TEXCOORD0; 24 | }; 25 | 26 | float3 posterize (float3 c, float s) 27 | { 28 | return floor(c*s)/(s-1); 29 | } 30 | 31 | structure vertex_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) 32 | { 33 | structure vs; 34 | vs.vertex = UnityObjectToClipPos (vertex); 35 | vs.uv = uv; 36 | return vs; 37 | } 38 | 39 | float4 pixel_shader (structure ps ) : SV_TARGET 40 | { 41 | float3 input = tex2D(_MainTex,ps.uv); 42 | float3 color = posterize(input,_steps); 43 | return float4(color,1.0); 44 | } 45 | 46 | ENDCG 47 | 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /procedural textures/boxes.shader: -------------------------------------------------------------------------------- 1 | Shader "Boxes" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | Cull off 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | #pragma target 2.0 12 | 13 | struct custom_type 14 | { 15 | float4 vertex : SV_POSITION; 16 | float2 uv : TEXCOORD0; 17 | }; 18 | 19 | custom_type vertex_shader (float4 vertex : POSITION, float2 uv : TEXCOORD0) 20 | { 21 | custom_type vs; 22 | vs.vertex = UnityObjectToClipPos (vertex); 23 | vs.uv = uv; 24 | return vs; 25 | } 26 | 27 | float box (float2 uv,float2 o) 28 | { 29 | return abs(1.2-acos(abs(uv.x+o.x)+abs(uv.y+o.y))); 30 | } 31 | 32 | float4 pixel_shader (custom_type ps) : COLOR 33 | { 34 | float2 uv = 2.0*ps.uv.xy-1.0; 35 | float3 o = float3(0.0,1.0,-1.0); 36 | float c = (max(max(max(max(box(uv,o.xx),box(uv,o.yy)),box(uv,o.zz)),box(uv,o.zy)),box(uv,o.yz))); 37 | return float4(c,c,c,1.0); 38 | } 39 | ENDCG 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /procedural textures/feathers.shader: -------------------------------------------------------------------------------- 1 | Shader "Feathers" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | Cull off 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | #pragma target 2.0 12 | 13 | struct custom_type 14 | { 15 | float4 vertex : SV_POSITION; 16 | float2 uv : TEXCOORD0; 17 | }; 18 | 19 | custom_type vertex_shader (float4 vertex : POSITION, float2 uv : TEXCOORD0) 20 | { 21 | custom_type vs; 22 | vs.vertex = UnityObjectToClipPos (vertex); 23 | vs.uv = uv; 24 | return vs; 25 | } 26 | 27 | float4 pixel_shader (custom_type ps) : COLOR 28 | { 29 | float2 o = abs(7.5*float2(2.0*ps.uv.xy-1.0)); 30 | float shape = 10.0-abs(lerp(o.x,o.y,10.0*cos(o.x*o.y))); 31 | return float4(shape,0.0,0.0,1.0); 32 | } 33 | ENDCG 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /procedural textures/heptagon.shader: -------------------------------------------------------------------------------- 1 | Shader "Heptagon" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 3.0 11 | 12 | struct custom_type 13 | { 14 | float4 vertex : SV_POSITION; 15 | float2 uv : TEXCOORD0; 16 | }; 17 | 18 | float heptagon(float2 p, float r) 19 | { 20 | float k = atan2(p.y,p.x)+1.5708; 21 | return length(p)*cos(0.8976*floor(0.5+k*1.1141)-k)-r; 22 | } 23 | 24 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 25 | { 26 | custom_type vs; 27 | vs.vertex = UnityObjectToClipPos(vertex); 28 | vs.uv = uv; 29 | return vs; 30 | } 31 | 32 | float4 pixel_shader (custom_type ps) : SV_TARGET 33 | { 34 | float2 uv = float2(2.0*ps.uv.xy-1.0); 35 | float c = smoothstep(0.001,0.0,heptagon(uv,0.5)); 36 | return float4(c,0,0,1); 37 | } 38 | ENDCG 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /procedural textures/random_tiles.shader: -------------------------------------------------------------------------------- 1 | Shader "Random Tiles" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 3.0 11 | #pragma exclude_renderers gles 12 | 13 | float4 vertex_shader (float4 vertex : POSITION) : POSITION 14 | { 15 | return UnityObjectToClipPos (vertex); 16 | } 17 | 18 | float4 pixel_shader (float2 f:VPOS) : COLOR 19 | { 20 | float2 u = f/_ScreenParams.xy*10.; 21 | u.x += _Time.g*lerp(-2.4,.8,fmod(ceil(u.y),2.)); 22 | return frac(sin(mul(ceil(u)/8.,float2x4(90,29.5,49.2,0,0,13.3,31.9,1)))*4e5); 23 | } 24 | ENDCG 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /procedural textures/readme.txt: -------------------------------------------------------------------------------- 1 | Directory contains my procedural textures. 2 | -------------------------------------------------------------------------------- /procedural textures/ribbons.shader: -------------------------------------------------------------------------------- 1 | Shader "Ribbons" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 2.0 11 | 12 | struct custom_type 13 | { 14 | float4 vertex : SV_POSITION; 15 | float2 uv : TEXCOORD0; 16 | }; 17 | 18 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 19 | { 20 | custom_type vs; 21 | vs.vertex = mul(UNITY_MATRIX_MVP,vertex); 22 | vs.uv = uv; 23 | return vs; 24 | } 25 | 26 | float4 pixel_shader (custom_type ps) : SV_TARGET 27 | { 28 | float2 u = float2(2.0*ps.uv.xy-1.0); 29 | u=abs(fmod(u,fmod(atan(u.y),fmod(atan(u.x),atan(u.y))))); 30 | return float4 (u,u.x*u.y,1)*4.0; 31 | } 32 | ENDCG 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /procedural textures/torn_fabric.shader: -------------------------------------------------------------------------------- 1 | Shader "Torn Fabric" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | Cull off 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | #pragma target 2.0 12 | 13 | struct custom_type 14 | { 15 | float4 vertex : SV_POSITION; 16 | float2 uv : TEXCOORD0; 17 | }; 18 | 19 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 20 | { 21 | custom_type vs; 22 | vs.vertex = UnityObjectToClipPos (vertex); 23 | vs.uv = uv; 24 | return vs; 25 | } 26 | 27 | float4 pixel_shader (custom_type ps) : COLOR 28 | { 29 | float2 z = float2(2.0*ps.uv.xy-1.0); 30 | int i=0; 31 | float r=0; 32 | while(i++<9) z=float2(z.x*z.x-z.y*z.y+.1,z.x*z.y*4.+.4); 33 | r=dot(z,z)<3.?z.y:0.; 34 | return float4(r,0,0,1); 35 | } 36 | ENDCG 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /procedural textures/wheat_color.shader: -------------------------------------------------------------------------------- 1 | Shader "Wheat Color" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 2.0 11 | 12 | struct custom_type 13 | { 14 | float4 vertex : SV_POSITION; 15 | float2 uv : TEXCOORD0; 16 | }; 17 | 18 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 19 | { 20 | custom_type vs; 21 | vs.vertex = UnityObjectToClipPos (vertex); 22 | vs.uv = uv; 23 | return vs; 24 | } 25 | 26 | float4 pixel_shader (custom_type ps) : SV_TARGET 27 | { 28 | float2 u = ps.uv.xy; 29 | float2 p = float2(2.0*u-1.0); 30 | float3 c = float3(0.96,0.87,0.70); 31 | c = c*(1.0-0.15*length(p)); 32 | return float4 (pow(c,float3(0.555,0.555,0.555)),1.0); 33 | } 34 | ENDCG 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /raymarched_plasma.shader: -------------------------------------------------------------------------------- 1 | // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 2 | // source: https://www.shadertoy.com/view/ldSfzm 3 | Shader "Raymarched plasma" 4 | { 5 | Subshader 6 | { 7 | Pass 8 | { 9 | CGPROGRAM 10 | #pragma vertex vertex_shader 11 | #pragma fragment pixel_shader 12 | #pragma target 2.0 13 | 14 | struct custom_type 15 | { 16 | float4 vertex : SV_POSITION; 17 | float2 uv : TEXCOORD0; 18 | }; 19 | 20 | float m(float3 p) 21 | { 22 | p.z+=5.*_Time.g; 23 | return length(.2*sin(p.x-p.y)+cos(p/3.)-.1*sin(1.5*p.x))-.8; 24 | } 25 | 26 | custom_type vertex_shader (float4 vertex : POSITION, float2 uv : TEXCOORD0) 27 | { 28 | custom_type vs; 29 | vs.vertex = UnityObjectToClipPos (vertex); 30 | vs.uv=uv; 31 | return vs; 32 | } 33 | 34 | float4 pixel_shader (custom_type ps) : SV_TARGET 35 | { 36 | float2 u = ps.uv.xy; 37 | float3 d=.5-float3(u,0),o=d; 38 | for(int i=0;i<64;i++) o+=m(o)*d; 39 | return float4(abs(m(o+d)*float3(.3,.15,.1)+m(o*.5)*float3(.1,.05,0))*(8.-o.x/2.),1.0); 40 | } 41 | ENDCG 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /raymarching_direct_compute.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | 3 | RWTexture2D render_texture; 4 | 5 | float sphere (float3 p,float3 c,float r) 6 | { 7 | return length (p-c)-r; 8 | } 9 | 10 | float map (float3 p) 11 | { 12 | return sphere (p,float3(0.0,0.0,0.0),1.0); 13 | } 14 | 15 | float3 set_normal (float3 p) 16 | { 17 | float3 x = float3 (0.01,0.00,0.00); 18 | float3 y = float3 (0.00,0.01,0.00); 19 | float3 z = float3 (0.00,0.00,0.01); 20 | return normalize(float3(map(p+x)-map(p-x), map(p+y)-map(p-y), map(p+z)-map(p-z))); 21 | } 22 | 23 | float3 lighting ( float3 p) 24 | { 25 | float3 AmbientLight = float3 (0.1,0.1,0.1); 26 | float3 LightDirection = normalize(float3(4.0,10.0,-10.0)); 27 | float3 LightColor = float3 (1.0,1.0,1.0); 28 | float3 NormalDirection = set_normal(p); 29 | return max ( dot(LightDirection, NormalDirection),0.0) * LightColor + AmbientLight; 30 | } 31 | 32 | float4 raymarch (float3 ro,float3 rd) 33 | { 34 | for (int i=0;i<128;i++) 35 | { 36 | float t = map(ro); 37 | if (t<0.01) return float4(lighting(ro),1.0); else ro+=t*rd; 38 | } 39 | return float4(0.0,0.0,0.0,1.0); 40 | } 41 | 42 | [numthreads(8,8,1)] 43 | void CSMain (uint2 id : SV_DispatchThreadID) 44 | { 45 | float2 resolution = float2 (1024,1024); 46 | float2 coordinates = float2 (id.x,id.y); 47 | float2 p = (2.0*coordinates.xy-resolution.xy)/resolution.y; 48 | float3 ro = float3 (0.0,0.0,-10.0); 49 | float3 rd = normalize( float3(p.xy,2.0)); 50 | render_texture[id] = raymarch( ro, rd ); 51 | } 52 | -------------------------------------------------------------------------------- /raymarching_direct_compute.cs: -------------------------------------------------------------------------------- 1 | //In Unity3D editor, add 3D Object/Quad to Main Camera. Set quad position at (x=0 ; y=0; z=0.86;). Add current script into quad. Set window size to 512x512 or 1024x1024. 2 | //Put raymarching_direct_compute.compute into Assets/Resources directory. Play. 3 | using UnityEngine; 4 | using System.Collections; 5 | 6 | public class raymarching_direct_compute : MonoBehaviour 7 | { 8 | void Start () 9 | { 10 | RenderTexture render_texture = new RenderTexture(1024,1024,0); 11 | render_texture.enableRandomWrite = true; 12 | render_texture.Create(); 13 | ComputeShader compute_shader = (ComputeShader)Resources.Load("raymarching_direct_compute"); 14 | compute_shader.SetTexture(0,"render_texture",render_texture); 15 | compute_shader.Dispatch(0,render_texture.width/8,render_texture.height/8,1); 16 | Renderer renderer = GetComponent(); 17 | renderer.material = new Material(Shader.Find("Unlit/Texture")); 18 | renderer.material.mainTexture = render_texture; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /raymarching_room.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/raymarching_room.jpg -------------------------------------------------------------------------------- /rectangles3.shader: -------------------------------------------------------------------------------- 1 | //based on tutorial from http://theorangeduck.com/page/avoiding-shader-conditionals 2 | Shader "Rectangles3" 3 | { 4 | Subshader 5 | { 6 | Pass 7 | { 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | #pragma target 2.0 12 | 13 | struct custom_type 14 | { 15 | float4 vertex : SV_POSITION; 16 | float2 uv : TEXCOORD0; 17 | }; 18 | 19 | custom_type vertex_shader (float4 vertex : POSITION, float2 uv : TEXCOORD0) 20 | { 21 | custom_type vs; 22 | vs.vertex = mul (UNITY_MATRIX_MVP,vertex); 23 | vs.uv=uv; 24 | return vs; 25 | } 26 | 27 | float4 pixel_shader (custom_type ps) : SV_TARGET 28 | { 29 | float x = ps.uv.x; 30 | float y = ps.uv.y; 31 | float2 center1 = float2(0.2,0.8); 32 | float2 center2 = float2(0.5,0.8); 33 | float2 center3 = float2(0.8,0.8); 34 | return float4(1.0,0.0,0.0,1.0)*max(sign(0.1-abs(center1.x-x)),0.0)*max(sign(0.1-abs(center1.y-y)),0.0) 35 | +float4(1.0,0.0,0.0,1.0)*max(sign(0.1-abs(center2.x-x)),0.0)*max(sign(0.1-abs(center2.y-y)),0.0) 36 | +float4(1.0,0.0,0.0,1.0)*max(sign(0.1-abs(center3.x-x)),0.0)*max(sign(0.1-abs(center3.y-y)),0.0); 37 | } 38 | 39 | ENDCG 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /rw_structured_buffer.cs: -------------------------------------------------------------------------------- 1 | // https://github.com/przemyslawzaworski 2 | // Set plane position (Y=0.0) 3 | // Collider is generated from vertex shader, using RWStructuredBuffer to transfer data. 4 | 5 | using UnityEngine; 6 | 7 | public class rw_structured_buffer : MonoBehaviour 8 | { 9 | public Material material; 10 | public GameObject plane; 11 | ComputeBuffer compute_buffer; 12 | Mesh mesh; 13 | Vector3[] data; 14 | 15 | void Start() 16 | { 17 | mesh = plane.GetComponent().GetComponent().sharedMesh; 18 | data = mesh.vertices; 19 | compute_buffer = new ComputeBuffer(data.Length, sizeof(float)*3, ComputeBufferType.Default); 20 | } 21 | 22 | void Update() 23 | { 24 | Graphics.ClearRandomWriteTargets(); 25 | material.SetPass(0); 26 | material.SetBuffer("data", compute_buffer); 27 | Graphics.SetRandomWriteTarget(1, compute_buffer,false); 28 | compute_buffer.GetData(data); 29 | if (data!=null && plane.GetComponent().isVisible && Time.frameCount % 2 == 0) 30 | { 31 | mesh.vertices = data; 32 | DestroyImmediate(plane.GetComponent()); 33 | MeshCollider mesh_collider = plane.AddComponent(); 34 | mesh_collider.sharedMesh = mesh; 35 | } 36 | } 37 | 38 | void OnDestroy() 39 | { 40 | compute_buffer.Dispose(); 41 | } 42 | } -------------------------------------------------------------------------------- /rw_structured_buffer.shader: -------------------------------------------------------------------------------- 1 | //https://github.com/przemyslawzaworski 2 | //Assign displacement map (R) to properties. 3 | 4 | Shader "RW Structured Buffer" 5 | { 6 | Properties 7 | { 8 | _MainTex ("Texture", 2D) = "black" {} 9 | } 10 | Subshader 11 | { 12 | Pass 13 | { 14 | CGPROGRAM 15 | #pragma vertex vertex_shader 16 | #pragma fragment pixel_shader 17 | #pragma target 5.0 18 | 19 | sampler2D _MainTex; 20 | uniform RWStructuredBuffer data : register(u1); 21 | 22 | struct APPDATA 23 | { 24 | float4 vertex : POSITION; 25 | float2 uv : TEXCOORD0; 26 | uint id : SV_VertexID; 27 | }; 28 | 29 | struct SHADERDATA 30 | { 31 | float4 vertex : SV_POSITION; 32 | float2 uv : TEXCOORD0; 33 | }; 34 | 35 | SHADERDATA vertex_shader (APPDATA IN) 36 | { 37 | SHADERDATA vs; 38 | IN.vertex.y = 0.0-tex2Dlod(_MainTex,float4(IN.uv,0,0)).r*(sin(_Time.g)*0.5+0.5); 39 | data[IN.id] = IN.vertex.xyz; 40 | vs.vertex = UnityObjectToClipPos(IN.vertex); 41 | vs.uv = IN.uv; 42 | return vs; 43 | } 44 | 45 | float4 pixel_shader (SHADERDATA ps) : SV_TARGET 46 | { 47 | return tex2D(_MainTex,ps.uv); 48 | } 49 | 50 | ENDCG 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /save_material.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | 3 | Texture2D render_texture; 4 | RWStructuredBuffer compute_buffer; 5 | SamplerState _PointClamp; 6 | float color_space; 7 | float brightness; 8 | 9 | [numthreads(8,8,1)] 10 | void CSMain (uint3 id : SV_DispatchThreadID) 11 | { 12 | int ResX, ResY; 13 | render_texture.GetDimensions(ResX,ResY); 14 | float2 uv = float2 (id.x/(float)ResX,id.y/(float)ResY); 15 | compute_buffer[id.y*ResY+id.x] = pow(abs(render_texture.SampleLevel(_PointClamp,uv,0).rgb),color_space)+brightness ; 16 | } -------------------------------------------------------------------------------- /save_material_editor.cs: -------------------------------------------------------------------------------- 1 | //Put script in Editor directory. 2 | 3 | using UnityEngine; 4 | using UnityEditor; 5 | 6 | [CustomEditor(typeof(save_material))] 7 | public class save_material_editor : Editor 8 | { 9 | public override void OnInspectorGUI() 10 | { 11 | DrawDefaultInspector(); 12 | save_material T = (save_material)target; 13 | if(GUILayout.Button("Reset")) T.ResetBuffer(); 14 | if(GUILayout.Button("Generate")) T.GenerateImage(); 15 | } 16 | } -------------------------------------------------------------------------------- /save_render_texture.compute: -------------------------------------------------------------------------------- 1 | #pragma kernel CSMain 2 | 3 | RWTexture2D render_texture; 4 | RWStructuredBuffer compute_buffer; 5 | float time; 6 | 7 | [numthreads(8,8,1)] 8 | void CSMain (uint3 id : SV_DispatchThreadID) 9 | { 10 | int ResX, ResY; 11 | render_texture.GetDimensions(ResX,ResY); 12 | float2 uv = float2 (id.x/(float)ResX,id.y/(float)ResY); 13 | float t = sin(time)*0.5+0.5; 14 | if (uv.x>t) 15 | render_texture[id.xy] = float4 (1.0,0.0,0.0,1.0); 16 | else 17 | render_texture[id.xy] = float4 (0.0,0.0,1.0,1.0); 18 | compute_buffer[id.y*ResY+id.x] = render_texture[id.xy].rgb; 19 | } 20 | -------------------------------------------------------------------------------- /scale.shader: -------------------------------------------------------------------------------- 1 | // https://github.com/przemyslawzaworski 2 | 3 | Shader "Scale" 4 | { 5 | Subshader 6 | { 7 | Pass 8 | { 9 | CGPROGRAM 10 | #pragma vertex vertex_shader 11 | #pragma fragment pixel_shader 12 | #pragma target 3.0 13 | 14 | float4 vertex_shader (float4 vertex:POSITION) : SV_POSITION 15 | { 16 | float4 w = mul(unity_ObjectToWorld, vertex); //transform local vertex to world space 17 | w.y=w.y*3.0; //scale vertex in world coordinates 18 | return mul(UNITY_MATRIX_VP,w); //matrix multiplication (current view-projection matrix and world space vertex) 19 | } 20 | 21 | float4 pixel_shader (void) : SV_TARGET 22 | { 23 | return 0; 24 | } 25 | 26 | ENDCG 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /screen_space_curvature.shader: -------------------------------------------------------------------------------- 1 | //reference: http://madebyevan.com/shaders/curvature/ 2 | //It looks the best with high-poly geometry 3 | 4 | Shader "Screen Space Curvature Shader" 5 | { 6 | Subshader 7 | { 8 | Tags { "RenderType"="Opaque" } 9 | Pass 10 | { 11 | Tags{ "LightMode" = "ForwardBase" } 12 | CGPROGRAM 13 | #pragma vertex vertex_shader 14 | #pragma fragment pixel_shader 15 | #pragma target 3.0 16 | 17 | struct structure 18 | { 19 | float4 gl_Position : SV_POSITION; 20 | float3 normal : NORMAL; 21 | float3 vertex : TEXCOORD0; 22 | }; 23 | 24 | structure vertex_shader (float4 vertex:POSITION, float3 normal:NORMAL) 25 | { 26 | structure vs; 27 | vs.gl_Position = UnityObjectToClipPos (vertex); 28 | vs.normal = normal; 29 | vs.vertex = vertex; 30 | return vs; 31 | } 32 | 33 | float4 pixel_shader (structure ps) : COLOR 34 | { 35 | float3 n = normalize(ps.normal); 36 | float3 dx = ddx(n); 37 | float3 dy = ddy(n); 38 | float3 xneg = n - dx; 39 | float3 xpos = n + dx; 40 | float3 yneg = n - dy; 41 | float3 ypos = n + dy; 42 | float depth = length(ps.vertex); 43 | float curvature = (cross(xneg,xpos).y-cross(yneg,ypos).x)*4.0/depth; 44 | return (curvature+0.5); 45 | } 46 | ENDCG 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /shader_dynamic_array.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class shader_dynamic_array : MonoBehaviour 4 | { 5 | [Header("Set material with shader")] 6 | public Material material; 7 | [Header("Set coordinates in range [0..1] for points")] 8 | public Vector2[] elements; 9 | 10 | void Update() 11 | { 12 | int count = elements.Length; 13 | Texture2D input = new Texture2D (count, 1, TextureFormat.RGBA32, false); 14 | input.filterMode = FilterMode.Point; 15 | input.wrapMode = TextureWrapMode.Clamp; 16 | for (int i=0;i A; 4 | float time; 5 | int resolution; 6 | 7 | float2x2 rotationX( float x) 8 | { 9 | return float2x2(cos(x),sin(x),-sin(x),cos(x)); 10 | } 11 | 12 | float2x2 rotationY( float y) 13 | { 14 | return float2x2(cos(y),-sin(y),sin(y),cos(y)); 15 | } 16 | 17 | float map (float3 p) 18 | { 19 | p.yz=mul(rotationX(time),p.yz); 20 | p.xz=mul(rotationY(time),p.xz); 21 | p = abs(p); p = max(p, p.yzx); 22 | return max(max(p.x, p.y) - 1.0, 0.8 - min(p.x, min(p.y, p.z))); 23 | } 24 | 25 | float3 raymarch (float3 ro, float3 rd) 26 | { 27 | float t = 0.0; 28 | for (int i = 0; i < 32; ++i) 29 | { 30 | float3 p = ro + rd * t; 31 | t += map(p); 32 | } 33 | return float3(1.0,0.8,0.0) / (1.0+t*t*0.1) * 0.5; 34 | } 35 | 36 | [numthreads(16,16,1)] 37 | void CSMain (uint2 id : SV_DispatchThreadID) 38 | { 39 | float scale = 0.9; //motion blur scale 40 | float2 uv = (2.0*float2(id.x,id.y)-float2(resolution,resolution))/float(resolution); 41 | float3 ro = float3(0.0, 0.0, -5.0); //virtual camera position 42 | float3 rd = normalize(float3(uv, 2.0)); //ray direction 43 | float3 total = raymarch(ro, rd); //trace in current frame 44 | total += A[id.y*resolution+id.x].rgb * scale; //accumulate data 45 | A[id.y*resolution+id.x] = float4(total, 1.0); //set final color 46 | } 47 | -------------------------------------------------------------------------------- /structured_buffer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class structured_buffer : MonoBehaviour 4 | { 5 | public ComputeShader shader; 6 | public Material material; 7 | public int resolution = 1024; 8 | ComputeBuffer A; 9 | 10 | void Start () 11 | { 12 | A = new ComputeBuffer(resolution*resolution, sizeof(float)*4, ComputeBufferType.Default); 13 | shader.SetBuffer(0, "A", A); 14 | material.SetBuffer("A",A); 15 | } 16 | 17 | void Update () 18 | { 19 | material.SetInt("resolution",resolution); 20 | shader.SetFloat("time",Time.time); 21 | shader.SetInt("resolution",resolution); 22 | shader.Dispatch(0, resolution / 16, resolution / 16, 1); 23 | } 24 | 25 | void OnDestroy() 26 | { 27 | A.Release(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /structured_buffer.shader: -------------------------------------------------------------------------------- 1 | Shader "Structured Buffer" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 5.0 11 | 12 | uniform StructuredBuffer A; 13 | uniform int resolution; 14 | 15 | struct SHADERDATA 16 | { 17 | float4 vertex : SV_POSITION; 18 | float2 uv : TEXCOORD0; 19 | }; 20 | 21 | SHADERDATA vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 22 | { 23 | SHADERDATA vs; 24 | vs.vertex = UnityObjectToClipPos(vertex); 25 | vs.uv = uv; 26 | return vs; 27 | } 28 | 29 | float4 pixel_shader (SHADERDATA ps) : SV_Target 30 | { 31 | int x = int(round(ps.uv.x*resolution)); 32 | int y = int(round(ps.uv.y*resolution)); 33 | return A[y*resolution+x]; 34 | } 35 | 36 | ENDCG 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /surface_shaders/Cellular.shader: -------------------------------------------------------------------------------- 1 | Shader "Cellular" 2 | { 3 | Properties 4 | { 5 | [HideInInspector] _texcoord( "", 2D ) = "white" {} 6 | } 7 | Subshader 8 | { 9 | Tags { "RenderType" = "Opaque" } 10 | CGPROGRAM 11 | #pragma surface SurfaceShader Standard fullforwardshadows addshadow 12 | 13 | struct Input 14 | { 15 | float2 uv_texcoord; 16 | }; 17 | 18 | float shape( float2 p ) 19 | { 20 | return dot(frac(p)-0.5, frac(p)-0.5); 21 | } 22 | 23 | float cell( float2 p ) 24 | { 25 | float c = 0.5; 26 | c = min(c, shape(p - float2(0.80, 0.61))); 27 | c = min(c, shape(p - float2(0.36, 0.20))); 28 | c = min(c, shape(p - float2(0.60, 0.24))); 29 | c = min(c, shape(p - float2(0.18, 0.82))); 30 | p *= 1.4142; 31 | c = min(c, shape(p - float2(0.45, 0.30))); 32 | c = min(c, shape(p - float2(0.04, 0.88))); 33 | c = min(c, shape(p - float2(0.06, 0.54))); 34 | c = min(c, shape(p - float2(0.64, 0.12))); 35 | return sqrt(c*5.); 36 | } 37 | 38 | void SurfaceShader (Input IN, inout SurfaceOutputStandard o) 39 | { 40 | float2 uv = float2(2.0 * IN.uv_texcoord.xy - 1.0); 41 | o.Albedo = float4(cell(uv).xxx + (cell(uv*5.)) * 0.05, 1.0); 42 | o.Normal = float3(0.0,0.0,1.0); 43 | o.Metallic = 0.0; 44 | o.Smoothness = 0.0; 45 | } 46 | 47 | ENDCG 48 | } 49 | } -------------------------------------------------------------------------------- /surface_shaders/Metallic.shader: -------------------------------------------------------------------------------- 1 | Shader "Metallic" 2 | { 3 | Properties 4 | { 5 | _Color ("Color", Color) = (1,1,1,1) 6 | _MainTex ("Albedo Map", 2D) = "black" {} 7 | _BumpMap ("Normal Map", 2D) = "bump" {} 8 | _MetallicGlossMap ("Metallic (R) Smoothness(A) Map", 2D) = "black" {} 9 | } 10 | 11 | Subshader 12 | { 13 | Tags { "RenderType" = "Opaque" } 14 | CGPROGRAM 15 | #pragma surface SurfaceShader Standard fullforwardshadows addshadow 16 | 17 | sampler2D _MainTex, _BumpMap, _MetallicGlossMap; 18 | float4 _Color; 19 | 20 | struct Input 21 | { 22 | float2 uv_MainTex; 23 | float2 uv_BumpMap; 24 | float2 uv_MetallicGlossMap; 25 | }; 26 | 27 | void SurfaceShader (Input IN, inout SurfaceOutputStandard o) 28 | { 29 | o.Albedo = tex2D(_MainTex,IN.uv_MainTex) * _Color ; 30 | o.Normal = UnpackNormal (tex2D(_BumpMap, IN.uv_BumpMap)); 31 | o.Metallic = tex2D(_MetallicGlossMap, IN.uv_MetallicGlossMap).r; 32 | o.Smoothness = tex2D(_MetallicGlossMap, IN.uv_MetallicGlossMap).a; 33 | } 34 | 35 | ENDCG 36 | } 37 | } -------------------------------------------------------------------------------- /surface_shaders/PBR_double_sided_color.shader: -------------------------------------------------------------------------------- 1 | Shader "PBR Double Sided Color" 2 | { 3 | Properties 4 | { 5 | _color ("Color", Color) = (0.5,0.5,0.5,1.0) 6 | } 7 | Subshader 8 | { 9 | Cull Off 10 | CGPROGRAM 11 | #pragma surface surface_shader Standard fullforwardshadows 12 | 13 | float4 _color; 14 | 15 | struct Input 16 | { 17 | float2 uv_MainTex; 18 | }; 19 | 20 | void surface_shader (Input IN, inout SurfaceOutputStandard o) 21 | { 22 | o.Albedo = _color; 23 | } 24 | 25 | ENDCG 26 | } 27 | } -------------------------------------------------------------------------------- /surface_shaders/Specular.shader: -------------------------------------------------------------------------------- 1 | Shader "Specular" 2 | { 3 | Properties 4 | { 5 | _Color ("Color", Color) = (1,1,1,1) 6 | _MainTex ("Albedo Map", 2D) = "black" {} 7 | _BumpMap ("Normal Map", 2D) = "bump" {} 8 | _SpecGlossMap ("Specular (RGB) Smoothness(A) Map", 2D) = "black" {} 9 | } 10 | 11 | Subshader 12 | { 13 | Tags { "RenderType" = "Opaque" } 14 | CGPROGRAM 15 | #pragma surface SurfaceShader StandardSpecular fullforwardshadows addshadow 16 | 17 | sampler2D _MainTex, _BumpMap, _SpecGlossMap; 18 | float4 _Color; 19 | 20 | struct Input 21 | { 22 | float2 uv_MainTex; 23 | float2 uv_BumpMap; 24 | float2 uv_SpecGlossMap; 25 | }; 26 | 27 | void SurfaceShader (Input IN, inout SurfaceOutputStandardSpecular o) 28 | { 29 | o.Albedo = tex2D(_MainTex,IN.uv_MainTex) * _Color ; 30 | o.Normal = UnpackNormal (tex2D(_BumpMap, IN.uv_BumpMap)); 31 | o.Specular = tex2D(_SpecGlossMap, IN.uv_SpecGlossMap).rgb; 32 | o.Smoothness = tex2D(_SpecGlossMap, IN.uv_SpecGlossMap).a; 33 | } 34 | 35 | ENDCG 36 | } 37 | } -------------------------------------------------------------------------------- /surface_shaders/clip_box.shader: -------------------------------------------------------------------------------- 1 | Shader "Clip Box" 2 | { 3 | Properties 4 | { 5 | _center ("Box Position", Vector) = (0,0,0,0) 6 | _scale ("Box Scale", Vector) = (1,1,1,1) 7 | _MainTex ("Main Texture", 2D) = "white" {} 8 | _BumpMap ("Normal Map", 2D) = "bump" {} 9 | _Metallic ("Metallic", Range(0,1)) = 1.0 10 | _Smoothness ("Smoothness", Range(0,1)) = 0.0 11 | } 12 | SubShader 13 | { 14 | Tags { "RenderType" = "Opaque" } 15 | Cull Off 16 | CGPROGRAM 17 | #pragma surface surface_shader Standard fullforwardshadows 18 | #pragma target 3.0 19 | 20 | struct Input 21 | { 22 | float2 uv_MainTex; 23 | float2 uv_BumpMap; 24 | float3 worldPos; 25 | }; 26 | 27 | float3 _center; 28 | float3 _scale; 29 | sampler2D _MainTex; 30 | sampler2D _BumpMap; 31 | half _Smoothness; 32 | half _Metallic; 33 | 34 | void surface_shader (Input IN, inout SurfaceOutputStandard o) 35 | { 36 | float3 d = IN.worldPos - _center; 37 | float3 p = float3(abs(d.x),abs(d.y),abs(d.z)); 38 | p.x = p.x - _scale.x * 0.5; 39 | p.y = p.y - _scale.y * 0.5; 40 | p.z = p.z - _scale.z * 0.5; 41 | float t = max(max(min(1.0,p.x),p.y),p.z); 42 | clip(t); 43 | o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb ; 44 | o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap)); 45 | o.Metallic = _Metallic; 46 | o.Smoothness = _Smoothness; 47 | } 48 | ENDCG 49 | } 50 | } -------------------------------------------------------------------------------- /surface_shaders/debris.shader: -------------------------------------------------------------------------------- 1 | Shader "Debris" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Albedo Map", 2D) = "black" {} 6 | _BumpMap ("Normal Map", 2D) = "bump" {} 7 | } 8 | 9 | Subshader 10 | { 11 | Tags { "RenderType" = "Opaque" } 12 | CGPROGRAM 13 | #pragma surface SurfaceShader Standard fullforwardshadows addshadow vertex:vertexDataFunc 14 | #pragma target 4.5 15 | #pragma multi_compile_instancing 16 | 17 | sampler2D _MainTex, _BumpMap; 18 | 19 | struct Input 20 | { 21 | float2 uv_MainTex; 22 | float2 uv_BumpMap; 23 | }; 24 | 25 | void vertexDataFunc( inout appdata_full v, out Input o ) 26 | { 27 | UNITY_INITIALIZE_OUTPUT( Input, o ); 28 | #ifdef UNITY_INSTANCING_ENABLED 29 | float s = frac(sin(unity_InstanceID + 123) * 43758.5453123); 30 | v.vertex.xyz *= s.xxx; 31 | #endif 32 | } 33 | 34 | void SurfaceShader (Input IN, inout SurfaceOutputStandard o) 35 | { 36 | o.Albedo = tex2D(_MainTex, IN.uv_MainTex); 37 | o.Normal = UnpackNormal (tex2D(_BumpMap, IN.uv_BumpMap)); 38 | o.Metallic = 0.0; 39 | o.Smoothness = 0.0; 40 | } 41 | 42 | ENDCG 43 | } 44 | } -------------------------------------------------------------------------------- /surface_shaders/readme: -------------------------------------------------------------------------------- 1 | Unity surface shaders. 2 | -------------------------------------------------------------------------------- /surface_shaders/terrain/TerrainShader-Base.shader: -------------------------------------------------------------------------------- 1 | Shader "Hidden/TerrainShader-Base" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Base (RGB) Smoothness (A)", 2D) = "white" {} 6 | _MetallicTex ("Metallic (R)", 2D) = "white" {} 7 | _Color ("Main Color", Color) = (1,1,1,1) 8 | } 9 | SubShader 10 | { 11 | Tags 12 | { 13 | "RenderType" = "Opaque" 14 | "Queue" = "Geometry-100" 15 | } 16 | LOD 200 17 | 18 | CGPROGRAM 19 | #pragma surface surf Standard fullforwardshadows 20 | #pragma target 3.0 21 | #pragma exclude_renderers gles 22 | #include "UnityPBSLighting.cginc" 23 | 24 | sampler2D _MainTex; 25 | sampler2D _MetallicTex; 26 | 27 | struct Input 28 | { 29 | float2 uv_MainTex; 30 | }; 31 | 32 | void surf (Input IN, inout SurfaceOutputStandard o) 33 | { 34 | half4 c = tex2D (_MainTex, IN.uv_MainTex); 35 | o.Albedo = c.rgb; 36 | o.Alpha = 1; 37 | o.Smoothness = c.a; 38 | o.Metallic = tex2D (_MetallicTex, IN.uv_MainTex).r; 39 | } 40 | 41 | ENDCG 42 | } 43 | FallBack "Diffuse" 44 | } 45 | -------------------------------------------------------------------------------- /surface_shaders/vface.shader: -------------------------------------------------------------------------------- 1 | Shader "Vface" 2 | { 3 | SubShader 4 | { 5 | Cull Off 6 | CGPROGRAM 7 | #pragma target 5.0 8 | #pragma surface SurfaceShader Standard 9 | 10 | struct Input { float IsFacing:VFACE; }; 11 | 12 | void SurfaceShader( Input i , inout SurfaceOutputStandard o ) 13 | { 14 | float4 color = (i.IsFacing>0) ? float4(1,0,0,1) : float4(0,0,1,1); 15 | o.Emission = color.rgb; 16 | o.Alpha = color.a; 17 | } 18 | 19 | ENDCG 20 | } 21 | } -------------------------------------------------------------------------------- /surface_shaders/world_space_uv_mapping_metallic.shader: -------------------------------------------------------------------------------- 1 | Shader "World Space UV Mapping Metallic" 2 | { 3 | Properties 4 | { 5 | _AlbedoMap ("Albedo Map", 2D) = "black" {} 6 | _NormalMap ("Normal Map", 2D) = "black" {} 7 | _MetallicMap ("Metallic (R) Smoothness(A) Map", 2D) = "black" {} 8 | } 9 | Subshader 10 | { 11 | Tags { "RenderType" = "Opaque" } 12 | CGPROGRAM 13 | #pragma surface surface_shader Standard fullforwardshadows 14 | 15 | sampler2D _AlbedoMap,_NormalMap,_MetallicMap; 16 | 17 | struct Input 18 | { 19 | float3 worldPos; 20 | }; 21 | 22 | void surface_shader (Input IN, inout SurfaceOutputStandard o) 23 | { 24 | o.Albedo = tex2Dlod(_AlbedoMap,float4(IN.worldPos.xz,0,0)); 25 | o.Normal = UnpackNormal (tex2Dlod (_NormalMap, float4(IN.worldPos.xz,0,0))); 26 | o.Metallic = tex2Dlod(_MetallicMap, float4(IN.worldPos.xz,0,0)).r; 27 | o.Smoothness = tex2Dlod(_MetallicMap, float4(IN.worldPos.xz,0,0)).a; 28 | } 29 | ENDCG 30 | } 31 | } -------------------------------------------------------------------------------- /texture_array.shader: -------------------------------------------------------------------------------- 1 | Shader "Texture Array" 2 | { 3 | Properties 4 | { 5 | _TextureArray ("Texture Array", 2DArray) = "" {} 6 | _Index ("Slices", Range(0,1)) = 1 7 | } 8 | SubShader 9 | { 10 | Pass 11 | { 12 | CGPROGRAM 13 | #pragma vertex vertex_shader 14 | #pragma fragment pixel_shader 15 | #pragma target 5.0 16 | 17 | Texture2DArray _TextureArray : register(t0); 18 | float _Index; 19 | SamplerState sampler_linear_repeat; 20 | 21 | void vertex_shader (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0) 22 | { 23 | vertex = UnityObjectToClipPos(vertex); 24 | } 25 | 26 | float4 pixel_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) : SV_TARGET 27 | { 28 | return _TextureArray.Sample( sampler_linear_repeat, float3( uv, _Index ) ); 29 | } 30 | ENDCG 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /the_smallest_shader.shader: -------------------------------------------------------------------------------- 1 | //Probably the smallest Unity shaderlab program, which is visible in engine (you can even return zero in vertex shader, but object won't be visible). 2 | //Author: Przemyslaw Zaworski 10.11.2017 Unity version: 5.3.5 3 | Shader "A" 4 | { 5 | Subshader 6 | { 7 | Pass 8 | { 9 | CGPROGRAM 10 | #pragma vertex V 11 | #pragma fragment P 12 | half4 V(half4 X:POSITION):POSITION{return mul(UNITY_MATRIX_MVP,X);} 13 | half4 P():COLOR{return 0;} 14 | ENDCG 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /tools/readme.md: -------------------------------------------------------------------------------- 1 | Directory contains tools related with shader programming theory. 2 | -------------------------------------------------------------------------------- /tools/rendering_transformation_matrices_calculator.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/tools/rendering_transformation_matrices_calculator.xls -------------------------------------------------------------------------------- /tools/rendering_transformation_matrices_calculator_version2.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/tools/rendering_transformation_matrices_calculator_version2.xls -------------------------------------------------------------------------------- /tools/rendering_transformation_matrices_help.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/Unity3D-CG-programming/814894529818e54465060e092e28a18e6b6100dd/tools/rendering_transformation_matrices_help.jpg -------------------------------------------------------------------------------- /toonify.cs: -------------------------------------------------------------------------------- 1 | //Add script to camera and make material with shader 2 | 3 | using UnityEngine; 4 | 5 | public class toonify : MonoBehaviour 6 | { 7 | public Material material; 8 | 9 | void OnRenderImage (RenderTexture source, RenderTexture destination) 10 | { 11 | Graphics.Blit (source, destination, material); 12 | } 13 | } -------------------------------------------------------------------------------- /triangle.shader: -------------------------------------------------------------------------------- 1 | Shader "Triangle" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | Cull Off 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | 12 | struct structure 13 | { 14 | float4 vertex : SV_POSITION; 15 | float3 color : COLOR; 16 | }; 17 | 18 | structure vertex_shader(uint id : SV_VertexID) 19 | { 20 | structure vs; 21 | float4 vertices[3]; 22 | vertices[0] = float4( 0.0,-0.5,0.001,1.0); 23 | vertices[1] = float4( 0.5, 0.5,0.001,1.0); 24 | vertices[2] = float4(-0.5, 0.5,0.001,1.0); 25 | float3 colors[3]; 26 | colors[0] = float3(1,0,0); 27 | colors[1] = float3(0,1,0); 28 | colors[2] = float3(0,0,1); 29 | if (id>2) 30 | { 31 | vs.vertex = float4(0.0, -0.5, 0.001, 1.0); 32 | vs.color = float3(1.0, 0.0, 0.0); 33 | } 34 | else 35 | { 36 | vs.vertex = vertices[id]; 37 | vs.color = colors[id]; 38 | } 39 | return vs; 40 | } 41 | 42 | void pixel_shader(in structure ps, out float4 fragColor:SV_Target) 43 | { 44 | fragColor = float4(ps.color,1); 45 | } 46 | ENDCG 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /truchet.shader: -------------------------------------------------------------------------------- 1 | Shader "Truchet" 2 | { 3 | Properties 4 | { 5 | _scaleX("UV.X scale", Range (1.0,20.0)) = 18.0 6 | _scaleY("UV.Y scale", Range (1.0,20.0)) = 18.0 7 | color1 ("Color 1", Color) = (0,1,0,1) 8 | color2 ("Color2", Color) = (1,0,0,1) 9 | } 10 | Subshader 11 | { 12 | Pass 13 | { 14 | CGPROGRAM 15 | #pragma vertex vertex_shader 16 | #pragma fragment pixel_shader 17 | #pragma target 3.0 18 | 19 | struct custom_type 20 | { 21 | float4 vertex : SV_POSITION; 22 | float2 uv : TEXCOORD0; 23 | }; 24 | 25 | float _scaleX,_scaleY; 26 | float4 color1,color2; 27 | 28 | float pattern(float2 p) 29 | { 30 | float2 h = p + float2(0.58, 0.15)*p.y; 31 | float2 f = frac(h); 32 | h -= f; 33 | float v = frac((h.x + h.y) / 3.0); 34 | if (v<0.6 && v>=0.3) h++; 35 | if (v>=0.6) h+=step(f.yx,f); 36 | p += float2(0.5, 0.13)*h.y - h; 37 | v = sign(cos(1234.*cos(h.x+9.*h.y))); 38 | return 0.1 / abs(0.5 - min(min 39 | (length(p - v*float2(-1., 0.00) ), 40 | length(p - v*float2(0.5, 0.87)) ), 41 | length(p - v*float2(0.5,-0.87)))); 42 | } 43 | 44 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 45 | { 46 | custom_type vs; 47 | vs.vertex = mul(UNITY_MATRIX_MVP,vertex); 48 | vs.uv = uv; 49 | return vs; 50 | } 51 | 52 | float4 pixel_shader (custom_type ps) : SV_TARGET 53 | { 54 | float2 uv = ps.uv.xy; 55 | uv.x*=_scaleX; uv.y*=_scaleY; 56 | return lerp(color1, color2, pattern (uv)); 57 | } 58 | ENDCG 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /tunnel.shader: -------------------------------------------------------------------------------- 1 | Shader "Tunnel" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 3.0 11 | 12 | struct structure 13 | { 14 | float4 vertex : SV_POSITION; 15 | float2 uv : TEXCOORD0; 16 | }; 17 | 18 | float h(float2 n) 19 | { 20 | return frac(sin(dot(fmod(n, 9.), float2(12.9898, 4.1414))) * 43758.5453); 21 | } 22 | 23 | float n(float2 p) 24 | { 25 | float2 i = floor(p), u = frac(p); 26 | u = u*u*(3.-2.*u); 27 | float r = lerp( 28 | lerp(h(i),h(i+float2(1,0)),u.x), 29 | lerp(h(i+float2(0,1)),h(i+float2(1,1)),u.x),u.y); 30 | return r*r; 31 | } 32 | 33 | float t(float2 u) 34 | { 35 | float r = length( u ); 36 | u = float2( 1./r + (_Time.g*2.), atan2( u.y, u.x )/3.1415927 ); 37 | return n( 9.*u )*r; 38 | } 39 | 40 | 41 | 42 | structure vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 43 | { 44 | structure vs; 45 | vs.vertex = UnityObjectToClipPos(vertex); 46 | vs.uv = uv; 47 | return vs; 48 | } 49 | 50 | float4 pixel_shader (structure ps) : SV_TARGET 51 | { 52 | float2 u = float2(2.0*ps.uv.xy-1.0); 53 | u.y+=sin(_Time.g)*n(u); 54 | u.x-=cos(_Time.g); 55 | return float4(t(u),0,0,1); 56 | } 57 | ENDCG 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /twister.shader: -------------------------------------------------------------------------------- 1 | //reference: https://www.shadertoy.com/view/MdSfW1 2 | Shader "Twister" 3 | { 4 | Properties 5 | { 6 | pattern ("Texture", 2D) = "black" {} 7 | } 8 | Subshader 9 | { 10 | Pass 11 | { 12 | CGPROGRAM 13 | #pragma vertex vertex_shader 14 | #pragma fragment pixel_shader 15 | #pragma target 3.0 16 | 17 | sampler2D pattern; 18 | 19 | struct structure 20 | { 21 | float4 vertex : SV_POSITION; 22 | float2 uv : TEXCOORD0; 23 | }; 24 | 25 | structure vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 26 | { 27 | structure vs; 28 | vs.vertex = UnityObjectToClipPos (vertex); 29 | vs.uv = uv; 30 | return vs; 31 | } 32 | 33 | float4 pixel_shader (structure ps) : COLOR 34 | { 35 | float2 P = ps.uv*4.0-2.0; 36 | float4 c = float4(0.36,0.64,0.58,1.0); 37 | float T = _Time.g, a = P.y*sin(T) + T*2. + sin(P.y + T); 38 | for (int n = 0; n < 4; n++) 39 | { 40 | float A = cos(a), B = sin(a); 41 | if (P.x > A && P.x < -B) 42 | { 43 | float2 uv = float2((P.x - A)/(-B - A), P.y*.5); 44 | c = tex2Dlod(pattern, float4(uv,0,0))*-(B + A)*(uv.x< 0.02 ? 1.5 : 1.0); 45 | } 46 | a += 1.5707; 47 | } 48 | return c; 49 | } 50 | ENDCG 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /unlit_double_sided.shader: -------------------------------------------------------------------------------- 1 | Shader "Double Sided" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "white" {} 6 | } 7 | Subshader 8 | { 9 | Pass 10 | { 11 | Cull Off 12 | CGPROGRAM 13 | #pragma vertex vertex_shader 14 | #pragma fragment pixel_shader 15 | #pragma target 3.0 16 | 17 | sampler2D _MainTex; 18 | 19 | struct custom_type 20 | { 21 | float4 clip_space_vertex : SV_POSITION; 22 | float2 uv : TEXCOORD0; 23 | }; 24 | 25 | custom_type vertex_shader (float4 object_space_vertex : POSITION, float2 uv : TEXCOORD0) 26 | { 27 | custom_type vs; 28 | vs.clip_space_vertex = mul (UNITY_MATRIX_MVP,object_space_vertex); 29 | vs.uv=uv; 30 | return vs; 31 | } 32 | 33 | float4 pixel_shader (custom_type ps) : SV_TARGET 34 | { 35 | return tex2D (_MainTex,ps.uv.xy); 36 | } 37 | 38 | ENDCG 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /unlit_transparency.shader: -------------------------------------------------------------------------------- 1 | Shader "Transparency" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "white" {} 6 | } 7 | Subshader 8 | { 9 | Tags { "RenderType" = "Transparent" "Queue" = "Transparent" } 10 | Pass 11 | { 12 | ZWrite Off 13 | Blend SrcAlpha OneMinusSrcAlpha 14 | 15 | CGPROGRAM 16 | #pragma vertex vertex_shader 17 | #pragma fragment pixel_shader 18 | #pragma target 3.0 19 | 20 | sampler2D _MainTex; 21 | 22 | struct custom_type 23 | { 24 | float4 clip_space_vertex : SV_POSITION; 25 | float2 uv : TEXCOORD0; 26 | }; 27 | 28 | custom_type vertex_shader (float4 object_space_vertex : POSITION, float2 uv : TEXCOORD0) 29 | { 30 | custom_type vs; 31 | vs.clip_space_vertex = mul (UNITY_MATRIX_MVP,object_space_vertex); 32 | vs.uv=uv; 33 | return vs; 34 | } 35 | 36 | float4 pixel_shader (custom_type ps) : SV_TARGET 37 | { 38 | return tex2D (_MainTex,ps.uv); 39 | } 40 | 41 | ENDCG 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /uv.shader: -------------------------------------------------------------------------------- 1 | Shader "UV" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | 11 | void vertex_shader (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0) 12 | { 13 | vertex = UnityObjectToClipPos(vertex); 14 | } 15 | 16 | float4 pixel_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) : SV_TARGET 17 | { 18 | return float4(uv,0,1); 19 | } 20 | ENDCG 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /vertex_colors.shader: -------------------------------------------------------------------------------- 1 | Shader "Vertex Colors" 2 | { 3 | Subshader 4 | { 5 | Pass 6 | { 7 | CGPROGRAM 8 | #pragma vertex vertex_shader 9 | #pragma fragment pixel_shader 10 | #pragma target 2.0 11 | 12 | struct structure 13 | { 14 | float4 vertex : SV_POSITION; 15 | float2 uv : TEXCOORD0; 16 | float4 color :COLOR; 17 | }; 18 | 19 | structure vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0, float4 color:COLOR) 20 | { 21 | structure vs; 22 | vs.vertex = UnityObjectToClipPos (vertex); 23 | vs.uv = uv; 24 | vs.color = color; 25 | return vs; 26 | } 27 | 28 | float4 pixel_shader (structure ps ) : SV_TARGET 29 | { 30 | return ps.color; 31 | } 32 | 33 | ENDCG 34 | 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /vignette_filter.shader: -------------------------------------------------------------------------------- 1 | Shader "Vignette Filter" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Texture", 2D) = "white" {} 6 | _intensity ("Intensity",Float) = 15.0 7 | _range ("Range",Float) = 0.25 8 | } 9 | Subshader 10 | { 11 | Pass 12 | { 13 | CGPROGRAM 14 | #pragma vertex vertex_shader 15 | #pragma fragment pixel_shader 16 | #pragma target 3.0 17 | 18 | sampler2D _MainTex; 19 | float _intensity; 20 | float _range; 21 | 22 | struct custom_type 23 | { 24 | float4 vertex : SV_POSITION; 25 | float2 uv : TEXCOORD0; 26 | }; 27 | 28 | custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 29 | { 30 | custom_type vs; 31 | vs.vertex = UnityObjectToClipPos (vertex); 32 | vs.uv = uv; 33 | return vs; 34 | } 35 | 36 | float4 pixel_shader (custom_type ps) : COLOR 37 | { 38 | float2 u = ps.uv.xy; 39 | float2 t = u*(1.0-u); 40 | float v = pow(t.x*t.y*_intensity,_range); 41 | return tex2D(_MainTex,u)*float4(v,v,v,1); 42 | } 43 | ENDCG 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /volume_texture.cs: -------------------------------------------------------------------------------- 1 | //Load volume_texture.shader into Unity Engine. 2 | //Optionially, add fly script to Main Camera. 3 | //This is basic template, so feel free to replace boring hash function with 4 | //own voxel structure. 5 | using UnityEngine; 6 | 7 | public class volume_texture : MonoBehaviour 8 | { 9 | public Material material; 10 | public int dimension = 64; 11 | 12 | Vector3 hash (Vector3 p) 13 | { 14 | float x = p.x*95.43583f+p.y*93.32319f+p.z*94.99343f; 15 | float y = p.x*35.12345f+p.y*33.51525f+p.z*34.97865f; 16 | float z = p.x*65.41415f+p.y*63.18549f+p.z*64.17331f; 17 | Vector3 q = new Vector3(x,y,z); 18 | float a = Mathf.Abs( (Mathf.Sin( q.x) * 65536.32f) % 1); 19 | float b = Mathf.Abs( (Mathf.Sin( q.y) * 65536.32f) % 1); 20 | float c = Mathf.Abs( (Mathf.Sin( q.z) * 65536.32f) % 1); 21 | return new Vector3 (a,b,c); 22 | } 23 | 24 | void GenerateVolume (int size) 25 | { 26 | Texture3D volume = new Texture3D (size, size, size, TextureFormat.ARGB32, true); 27 | var voxels = new Color[size*size*size]; 28 | int i = 0; 29 | Color color = Color.black; 30 | for (int z = 0; z < size; ++z) 31 | { 32 | for (int y = 0; y < size; ++y) 33 | { 34 | for (int x = 0; x < size; ++x, ++i) 35 | { 36 | color.r = hash(new Vector3(x,y,z)).x; 37 | color.g = hash(new Vector3(x,y,z)).y; 38 | color.b = hash(new Vector3(x,y,z)).z; 39 | voxels[i] = color; 40 | } 41 | } 42 | } 43 | volume.SetPixels (voxels); 44 | volume.Apply (); 45 | material.SetTexture("volume",volume); 46 | } 47 | 48 | void Start () 49 | { 50 | GenerateVolume (dimension); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /volume_texture.shader: -------------------------------------------------------------------------------- 1 | //Add quad to Main Camera, then add volume_texture.cs to quad and material with shader (both to script and Mesh Renderer). 2 | //Set quad position to (0,0,0.4). Play. 3 | 4 | Shader "Volume Texture" 5 | { 6 | Properties 7 | { 8 | [HideInInspector] 9 | volume ("Texture 3D", 3D) = "black" {} 10 | } 11 | Subshader 12 | { 13 | Pass 14 | { 15 | CGPROGRAM 16 | #pragma vertex vertex_shader 17 | #pragma fragment pixel_shader 18 | #pragma target 3.0 19 | 20 | sampler3D volume; 21 | 22 | struct structure 23 | { 24 | float4 screen_vertex : SV_POSITION; 25 | float3 world_vertex : TEXCOORD0; 26 | }; 27 | 28 | structure vertex_shader (float4 vertex:POSITION) 29 | { 30 | structure vs; 31 | vs.screen_vertex = UnityObjectToClipPos (vertex); 32 | vs.world_vertex = mul (unity_ObjectToWorld, vertex); 33 | return vs; 34 | } 35 | 36 | float4 pixel_shader (structure ps) : SV_TARGET 37 | { 38 | float3 ro = ps.world_vertex; 39 | float3 rd = normalize(ps.world_vertex - _WorldSpaceCameraPos.xyz); 40 | float4 s = float4(0.0,0.0,0.0,0.0); 41 | for (float t = 1.0; t < 32.0; t++) 42 | { 43 | ro+=rd*t; 44 | s+=tex3Dlod(volume,float4(ro*.002,0))*.06; 45 | if (s.w >= 1.0) break; 46 | } 47 | return lerp(float4(0.0,0.0,0.0,0.0),s,s.w); 48 | } 49 | ENDCG 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /volumetric texture/Composite.shader: -------------------------------------------------------------------------------- 1 | Shader "Hidden/Ray Marching/Composite" 2 | { 3 | Properties 4 | { 5 | _MainTex ("Base (RGB)", 2D) = "" {} 6 | _BlendTex ("Blend (RGB)", 2D) = "" {} 7 | } 8 | Subshader 9 | { 10 | Pass 11 | { 12 | ZTest Always Cull Off ZWrite Off 13 | 14 | CGPROGRAM 15 | #pragma vertex vertex_shader 16 | #pragma fragment pixel_shader 17 | #pragma target 3.0 18 | 19 | struct custom_type 20 | { 21 | float4 vertex : POSITION; 22 | float2 uv : TEXCOORD0; 23 | }; 24 | 25 | sampler2D _MainTex; 26 | sampler2D _BlendTex; 27 | 28 | custom_type vertex_shader(float4 vertex:POSITION, float2 uv:TEXCOORD0) 29 | { 30 | custom_type vs; 31 | vs.vertex = UnityObjectToClipPos(vertex); 32 | vs.uv=uv; 33 | return vs; 34 | } 35 | 36 | float4 pixel_shader(custom_type ps) : COLOR 37 | { 38 | float4 src = tex2D(_MainTex, ps.uv); 39 | float4 dst = tex2D(_BlendTex, ps.uv); 40 | return lerp(src,dst,dst.a); 41 | } 42 | 43 | ENDCG 44 | 45 | } 46 | } 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /volumetric texture/Render Back Depth.shader: -------------------------------------------------------------------------------- 1 | Shader "Hidden/Ray Marching/Render Back Depth" 2 | { 3 | Subshader 4 | { 5 | Tags {"RenderType"="Volume"} 6 | Cull Front 7 | Pass 8 | { 9 | 10 | CGPROGRAM 11 | #pragma vertex vertex_shader 12 | #pragma fragment pixel_shader 13 | 14 | struct custom_type 15 | { 16 | float4 screen_vertex : SV_POSITION; 17 | float3 local_vertex : TEXCOORD0; 18 | }; 19 | 20 | custom_type vertex_shader(float4 vertex:POSITION) 21 | { 22 | custom_type vs; 23 | vs.screen_vertex = UnityObjectToClipPos(vertex); 24 | vs.local_vertex = vertex.xyz + 0.5; 25 | return vs; 26 | } 27 | 28 | float4 pixel_shader(custom_type ps) : COLOR 29 | { 30 | return float4(ps.local_vertex, 1.0); 31 | } 32 | 33 | ENDCG 34 | 35 | 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /volumetric texture/Render Front Depth.shader: -------------------------------------------------------------------------------- 1 | Shader "Hidden/Ray Marching/Render Front Depth" 2 | { 3 | Subshader 4 | { 5 | Tags {"RenderType"="Volume"} 6 | Pass 7 | { 8 | 9 | CGPROGRAM 10 | #pragma vertex vertex_shader 11 | #pragma fragment pixel_shader 12 | 13 | struct custom_type 14 | { 15 | float4 screen_vertex : SV_POSITION; 16 | float3 local_vertex : TEXCOORD0; 17 | }; 18 | 19 | custom_type vertex_shader(float4 vertex:POSITION) 20 | { 21 | custom_type vs; 22 | vs.screen_vertex = UnityObjectToClipPos(vertex); 23 | vs.local_vertex = vertex.xyz + 0.5; 24 | return vs; 25 | } 26 | 27 | float4 pixel_shader(custom_type ps) : COLOR 28 | { 29 | return float4(ps.local_vertex, 1.0); 30 | } 31 | 32 | ENDCG 33 | 34 | 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /volumetric texture/Volume.shader: -------------------------------------------------------------------------------- 1 | Shader "Ray Marching/Volume" 2 | { 3 | SubShader 4 | { 5 | Tags {"RenderType" = "Volume"} 6 | 7 | ZWrite Off 8 | 9 | Pass 10 | { 11 | ColorMask 0 12 | } 13 | } 14 | 15 | FallBack Off 16 | } 17 | -------------------------------------------------------------------------------- /volumetric_lighting.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class volumetric_lighting : MonoBehaviour 5 | { 6 | public Material material; 7 | 8 | void OnRenderImage (RenderTexture source, RenderTexture destination) 9 | { 10 | Graphics.Blit (source, destination, material); 11 | } 12 | } -------------------------------------------------------------------------------- /volumetric_lighting.shader: -------------------------------------------------------------------------------- 1 | Shader "Volumetric Lighting" 2 | { 3 | Properties 4 | { 5 | pattern ("Texture", 2D) = "white" {} 6 | } 7 | Subshader 8 | { 9 | Pass 10 | { 11 | CGPROGRAM 12 | #pragma vertex vertex_shader 13 | #pragma fragment pixel_shader 14 | #pragma target 3.0 15 | 16 | sampler2D pattern; 17 | 18 | struct structure 19 | { 20 | float4 vertex : SV_POSITION; 21 | float2 uv : TEXCOORD0; 22 | }; 23 | 24 | structure vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0) 25 | { 26 | structure vs; 27 | vs.vertex = UnityObjectToClipPos(vertex); 28 | vs.uv = uv; 29 | return vs; 30 | } 31 | 32 | float4 pixel_shader (structure ps) : COLOR 33 | { 34 | float2 uv = float2(2.0*ps.uv.xy-1.0); 35 | float color = 0.0; 36 | for(int i=0;i<192;++i) 37 | { 38 | float2 p=(uv*asin(dot(uv,uv)*float(i)*0.01)/dot(uv,uv)); 39 | p.x-=_Time.g*0.5; 40 | color+=tex2Dlod(pattern,float4(p*0.2,0.0,0.0)).r*0.01; 41 | } 42 | return float4(color,0.0,0.0,1.0); 43 | } 44 | ENDCG 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /voronoi_pattern.shader: -------------------------------------------------------------------------------- 1 | //source: https://www.shadertoy.com/view/MslGD8 2 | Shader "Voronoi Pattern" 3 | { 4 | Subshader 5 | { 6 | Pass 7 | { 8 | CGPROGRAM 9 | #pragma vertex vertex_shader 10 | #pragma fragment pixel_shader 11 | #pragma target 3.0 12 | 13 | struct custom_type 14 | { 15 | float4 vertex : SV_POSITION; 16 | float2 uv : TEXCOORD0; 17 | }; 18 | 19 | float2 hash (float2 p) 20 | { 21 | p=float2(dot(p,float2(127.1,311.7)),dot(p,float2(269.5,183.3))); 22 | return frac(sin(p)*18.5453); 23 | } 24 | 25 | float2 voronoi (float2 x) 26 | { 27 | float2 n = floor(x); 28 | float2 f = frac(x); 29 | float3 m = float3(8,8,8); 30 | for( int j=-1; j<=1; j++ ) 31 | for( int i=-1; i<=1; i++ ) 32 | { 33 | float2 g = float2( float(i), float(j) ); 34 | float2 o = hash( n + g ); 35 | float2 r = g - f + (0.5+0.5*sin(6.2831*o)); 36 | float d = dot( r, r ); 37 | if( d