diff options
| -rw-r--r-- | Shaders/grass/grass.mat | 93 | ||||
| -rw-r--r-- | Shaders/grass/grass.shader | 91 | ||||
| -rw-r--r-- | Shaders/grass/grass_10m_tile.fbx | bin | 0 -> 3712460 bytes | |||
| -rw-r--r-- | Shaders/grass/grass_10m_tile.prefab | 93 | ||||
| -rw-r--r-- | Shaders/grass/grass_lighting.cginc | 212 | ||||
| -rw-r--r-- | Shaders/grass/iq_sdf.cginc | 102 | ||||
| -rw-r--r-- | Shaders/grass/math.cginc | 87 | ||||
| -rw-r--r-- | Shaders/grass/motion.cginc | 90 | ||||
| -rw-r--r-- | Shaders/grass/pbr.cginc | 93 | ||||
| -rw-r--r-- | Shaders/grass/pema99.cginc | 36 | ||||
| -rw-r--r-- | Shaders/grass/poi.cginc | 60 | ||||
| -rw-r--r-- | Shaders/grass/shadertoy.cginc | 36 |
12 files changed, 993 insertions, 0 deletions
diff --git a/Shaders/grass/grass.mat b/Shaders/grass/grass.mat new file mode 100644 index 0000000..4104da6 --- /dev/null +++ b/Shaders/grass/grass.mat @@ -0,0 +1,93 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: grass + m_Shader: {fileID: 4800000, guid: 5c6a928fc1e5a034691b8b8960ddb65d, type: 3} + m_ShaderKeywords: + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BaseColor: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _Metallic: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _Roughness: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _Wind_Speed: + m_Texture: {fileID: 2800000, guid: 175a6cc087d481447800366ca5b21523, type: 3} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Floats: + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} diff --git a/Shaders/grass/grass.shader b/Shaders/grass/grass.shader new file mode 100644 index 0000000..512d0f3 --- /dev/null +++ b/Shaders/grass/grass.shader @@ -0,0 +1,91 @@ +Shader "yum_food/grass" +{ + Properties + { + _BaseColor("Base color", 2D) = "white" {} + //_Normal("Normal", 2D) = "bump" {} + //_Disable_Normal_Texture("Disable normal texture", float) = 1.0 + _Metallic("Metallic", 2D) = "white" {} + _Roughness("Roughness", 2D) = "black" {} + + _Wind_Speed("Wind Speed", 2D) = "black" {} + + //_Cubemap("Cubemap", Cube) = "" {} + + //_Height("Height", 2D) = "black" {} + //_Height_LOD("Height LOD", float) = 8.0 + //_Height_Scale("Height scale", float) = 1.0 + //_Height_Exponent("Height exponent", float) = 1.0 + //_Height_Speed_X("Height speed (X axis)", float) = 0.0 + //_Height_Speed_Y("Height speed (Y axis)", float) = 0.0 + //_Height_AA_Sample_Scale("Height anti-alias UV scale", float) = 0.001 + + //_Height_Mask("Height mask", 2D) = "white" {} + //_Height_Mask_Exponent("Height mask exponent", float) = 1.0 + + //_Center_Out_Speed("Center out speed", float) = 0.0 + //_Center_Out_Sharpness("Center out sharpness", float) = 4.0 + //_Center_Out_Min_Radius("Center out min radius", float) = -1.0 + //_Center_Out_Max_Radius("Center out max radius", float) = 2.7 + } + SubShader + { + Pass { + Tags { + "RenderType"="Opaque" + "Queue"="AlphaTest+499" + "LightMode" = "ForwardBase" + } + Blend SrcAlpha OneMinusSrcAlpha + ZWrite On + ZTest LEqual + Cull Off + + CGPROGRAM + #pragma target 5.0 + + #pragma multi_compile _ VERTEXLIGHT_ON + + #pragma vertex vert + #pragma geometry geom + #pragma fragment frag + + #define FORWARD_BASE_PASS + + // Three anti-aliasing levels. + // 0: no anti-aliasing + // 1: sample 4 neighbors (diagonals) + // 2: sample 8 neighbors (diagonals + cartesian) + #define HEIGHT_AA_LEVEL 2 + + #include "grass_lighting.cginc" + ENDCG + } + Pass { + Tags { + "RenderType" = "Opaque" + "LightMode" = "ForwardAdd" + "Queue"="AlphaTest+499" + } + Blend One One + ZWrite On + ZTest LEqual + Cull Off + + CGPROGRAM + #pragma target 5.0 + + #pragma multi_compile_fwdadd + + #pragma vertex vert + #pragma geometry geom + #pragma fragment frag + + #define HEIGHT_AA_LEVEL 2 + + #include "grass_lighting.cginc" + ENDCG + } + } +} + diff --git a/Shaders/grass/grass_10m_tile.fbx b/Shaders/grass/grass_10m_tile.fbx Binary files differnew file mode 100644 index 0000000..ffb2d57 --- /dev/null +++ b/Shaders/grass/grass_10m_tile.fbx diff --git a/Shaders/grass/grass_10m_tile.prefab b/Shaders/grass/grass_10m_tile.prefab new file mode 100644 index 0000000..d68b1b2 --- /dev/null +++ b/Shaders/grass/grass_10m_tile.prefab @@ -0,0 +1,93 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &1151066318006196413 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 338535348900282887} + - component: {fileID: 3683209533558246818} + - component: {fileID: 231936920441164820} + m_Layer: 0 + m_Name: grass_10m_tile + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &338535348900282887 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1151066318006196413} + m_LocalRotation: {x: -0.7071068, y: 0, z: -0, w: 0.7071067} + m_LocalPosition: {x: -0, y: 0, z: -0} + m_LocalScale: {x: 100, y: 100, z: 100} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!33 &3683209533558246818 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1151066318006196413} + m_Mesh: {fileID: 6416302070336371891, guid: ea8524dd6534dd44e9cc862b8a164079, type: 3} +--- !u!137 &231936920441164820 +SkinnedMeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1151066318006196413} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: 25e0d389e623bcd47899268643dbd06f, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + serializedVersion: 2 + m_Quality: 0 + m_UpdateWhenOffscreen: 0 + m_SkinnedMotionVectors: 1 + m_Mesh: {fileID: 6416302070336371891, guid: ea8524dd6534dd44e9cc862b8a164079, type: 3} + m_Bones: [] + m_BlendShapeWeights: [] + m_RootBone: {fileID: 0} + m_AABB: + m_Center: {x: 0, y: 0, z: 0} + m_Extent: {x: 0.3, y: 0.3, z: 0.05} + m_DirtyAABB: 0 diff --git a/Shaders/grass/grass_lighting.cginc b/Shaders/grass/grass_lighting.cginc new file mode 100644 index 0000000..b657354 --- /dev/null +++ b/Shaders/grass/grass_lighting.cginc @@ -0,0 +1,212 @@ +#ifndef GRASS_LIGHTING +#define GRASS_LIGHTING + +#include "AutoLight.cginc" +#include "iq_sdf.cginc" +#include "math.cginc" +#include "motion.cginc" +#include "pbr.cginc" +#include "poi.cginc" +#include "shadertoy.cginc" + +sampler2D _BaseColor; +sampler2D _Normal; +sampler2D _Metallic; +sampler2D _Roughness; +sampler2D _Wind_Speed; + +bool _Disable_Normal_Texture; + +sampler2D _Height; +float _Height_LOD; +float _Height_Exponent; +float _Height_Scale; +float _Height_Speed_X; +float _Height_Speed_Y; +float _Height_AA_Sample_Scale; + +sampler2D _Height_Mask; +float _Height_Mask_Exponent; + +float _Center_Out_Speed; +float _Center_Out_Sharpness; +float _Center_Out_Min_Radius; +float _Center_Out_Max_Radius; + +struct appdata +{ + float4 position : POSITION; + float2 uv : TEXCOORD0; + float3 normal : NORMAL; +}; + +void getVertexLightColor(inout v2f i) +{ + #if defined(VERTEXLIGHT_ON) + float3 light_pos = float3(unity_4LightPosX0.x, unity_4LightPosY0.x, + unity_4LightPosZ0.x); + float3 light_float = light_pos - i.worldPos; + float3 light_dir = normalize(light_float); + float ndotl = DotClamped(i.normal, light_dir); + // Light fills an expanding sphere with surface area 4 * pi * r^2. + // By conservation of energy, this means that at distance r, light intensity + // is proportional to 1/(r^2). + float attenuation = 1 / (1 + dot(light_float, light_float) * unity_4LightAtten0.x); + i.vertexLightColor = unity_LightColor[0].rgb * ndotl * attenuation; + + i.vertexLightColor = Shade4PointLights( + unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, + unity_LightColor[0].rgb, + unity_LightColor[1].rgb, + unity_LightColor[2].rgb, + unity_LightColor[3].rgb, + unity_4LightAtten0, i.worldPos, i.normal + ); + #endif +} + +v2f vert(appdata v) +{ + v2f o; + + o.objPos = v.position; + o.clipPos = UnityObjectToClipPos(v.position); + o.worldPos = mul(unity_ObjectToWorld, v.position); + + o.normal = UnityObjectToWorldNormal(v.normal); + o.uv = v.uv; + + getVertexLightColor(o); + + return o; +} + +// maxvertexcount == the number of vertices we create +[maxvertexcount(48)] +void geom(triangle v2f tri_in[3], + uint pid: SV_PrimitiveID, + inout TriangleStream<v2f> tri_out) +{ + // Generate copies. + for (int x = 0; x < 4; x++) + for (int y = 0; y < 4; y++) { + float xoff = (x - 1) * 10; + float yoff = (y - 1) * 10; + v2f v0 = tri_in[0]; + v2f v1 = tri_in[1]; + v2f v2 = tri_in[2]; + + v0.worldPos += float3(xoff, 0, yoff); + v1.worldPos += float3(xoff, 0, yoff); + v2.worldPos += float3(xoff, 0, yoff); + + // Apply wind using a noise texture. + for (int i = 0; i < 3; i++) + { + float3 vertex_pos; + [forcecase] switch (i) { + case 0: + vertex_pos = v0.worldPos; + break; + case 1: + vertex_pos = v1.worldPos; + break; + case 2: + vertex_pos = v2.worldPos; + break; + default: + vertex_pos = 0; + break; + } + float3 wind_point = vertex_pos; + wind_point *= .02; + wind_point.x -= _Time[0]/2; + float3 dx = float3(.02, 0, 0); + + float w0 = tex2Dlod(_Wind_Speed, float4(wind_point.x, wind_point.z, 0, 0)); + float w1 = tex2Dlod(_Wind_Speed, float4(wind_point.x + dx.x, wind_point.z, 0, 0)); + float w2 = tex2Dlod(_Wind_Speed, float4(wind_point.x, wind_point.z + dx.x, 0, 0)); + + float2 wind_speed = float2(w1 - w0, w2 - w0); + wind_speed *= 4; + wind_speed *= vertex_pos.y; + + switch (i) { + case 0: + v0.worldPos += float3(wind_speed.x, 0, wind_speed.y); + break; + case 1: + v1.worldPos += float3(wind_speed.x, 0, wind_speed.y); + break; + case 2: + v2.worldPos += float3(wind_speed.x, 0, wind_speed.y); + break; + } + } + + v0.objPos = mul(unity_WorldToObject, v0.worldPos); + v1.objPos = mul(unity_WorldToObject, v1.worldPos); + v2.objPos = mul(unity_WorldToObject, v2.worldPos); + + v0.clipPos = UnityObjectToClipPos(v0.objPos); + v1.clipPos = UnityObjectToClipPos(v1.objPos); + v2.clipPos = UnityObjectToClipPos(v2.objPos); + + tri_out.Append(v0); + tri_out.Append(v1); + tri_out.Append(v2); + tri_out.RestartStrip(); + } +} + +float getWorldSpaceDepth(in const float3 worldPos) +{ + float4 clip_pos = mul(UNITY_MATRIX_VP, float4(worldPos, 1.0)); + return clip_pos.z / clip_pos.w; +} + +float4 effect(inout v2f i, out float depth) +{ + depth = getWorldSpaceDepth(i.worldPos); + + //float4 albedo = tex2D(_BaseColor, i.uv); + float4 albedo; + { + float3 green = HSVtoRGB(float3(.333, .60, .70)); + float3 brown = HSVtoRGB(float3(.113, .59, .65)); + float uv_t0 = .85; + float uv_t1 = .50; + float uv_phase = (i.uv.y - uv_t0) / (uv_t1 - uv_t0); + uv_phase = clamp(uv_phase, 0, 1); + float3 c = lerp(brown, green, uv_phase); + albedo = float4(c, 1.0); + } + + float3 normal = i.normal; + // Rotate the normals a little to make the blades of grass appear more + // rounded. + { + float3 nt = mul(unity_WorldToObject, float4(normal, 1.0)).xyz; + float theta = 1.2; + theta = (i.uv.x - 0.5) * 2 * theta; + float2x2 rot = float2x2( + cos(theta), -sin(theta), + sin(theta), cos(theta)); + nt.xz = mul(rot, nt.xz); + normal = mul(unity_ObjectToWorld, float4(nt, 1.0)).xyz; + } + + float metallic = 0.5; + float roughness = 0.2; + + return getLitColor(i, albedo, i.worldPos, normal, metallic, 1.0 - roughness, + /*custom_cubemap=*/true); +} + +fixed4 frag(v2f i, out float depth : SV_DepthLessEqual) : SV_Target +{ + return effect(i, depth); +} + +#endif // GRASS_LIGHTING + diff --git a/Shaders/grass/iq_sdf.cginc b/Shaders/grass/iq_sdf.cginc new file mode 100644 index 0000000..472256c --- /dev/null +++ b/Shaders/grass/iq_sdf.cginc @@ -0,0 +1,102 @@ +#include "pema99.cginc" + +// The MIT License +// Copyright © 2019-2021 Inigo Quilez +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +float distance_from_octahedron(in float3 p) +{ + float s = 1.0; + float3 pp = abs(p); + float m = pp.x+pp.y+pp.z-s; + float3 q; + if( 3.0*pp.x < m ) q = pp.xyz; + else if( 3.0*pp.y < m ) q = pp.yzx; + else if( 3.0*pp.z < m ) q = pp.zxy; + else return m*0.57735027; + + float k = clamp(0.5*(q.z-q.y+s),0.0,s); + return length(float3(q.x,q.y-s+k,q.z-k)); +} + +float distance_from_sphere(float3 p, float r) +{ + return length(p) - r; +} + +float distance_from_cut_sphere( in float3 p, in float r, in float h ) +{ + float w = sqrt(r*r-h*h); // constant for a given shape + + float2 q = float2( length(p.xz), p.y ); + + float s = max( (h-r)*q.x*q.x+w*w*(h+r-2.0*q.y), h*q.x-w*q.y ); + + return (s<0.0) ? length(q)-r : + (q.x<w) ? h - q.y : + length(q-float2(w,h)); +} + +float distance_from_cut_hollow_sphere( float3 p, float r, float h, float t ) +{ + float2 q = float2( length(p.xz), p.y ); + + float w = sqrt(r*r-h*h); + + return ((h*q.x<w*q.y) ? length(q-float2(w,h)) : + abs(length(q)-r) ) - t; +} + +float distance_from_box(float3 p, float3 b) +{ + float3 q = abs(p) - b; + return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0); +} + +float distance_from_box_frame(float3 p, float3 b, float e) +{ + p = abs(p)-b; + float3 q = abs(p+e)-e; + + return min(min( + length(max(float3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), + length(max(float3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), + length(max(float3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); +} + +float distance_from_pyramid(float3 p, float h, bool invert) +{ + float m2 = h*h + 0.25; + + // symmetry + p.xz = abs(p.xz); // do p=abs(p) instead for double pyramid + p.xz = (p.z>p.x) ? p.zx : p.xz; + p.xz -= 0.5; + + // project into face plane (2D) + float3 q = float3( p.z, h*p.y-0.5*p.x, h*p.x+0.5*p.y); + + float s = max(-q.x,0.0); + float t = clamp( (q.y-0.5*q.x)/(m2+0.25), 0.0, 1.0 ); + + float a = m2*(q.x+s)*(q.x+s) + q.y*q.y; + float b = m2*(q.x+0.5*t)*(q.x+0.5*t) + (q.y-m2*t)*(q.y-m2*t); + + float d2 = max(-q.y,q.x*m2+q.y*0.5) < 0.0 ? 0.0 : min(a,b); + + // recover 3D and scale, and add sign + return sqrt( (d2+q.z*q.z)/m2 ) * sign(max(q.z,-p.y));; +} + +float distance_from_plane(float3 p, float3 n, float h) +{ + // n must be normalized + return dot(p,n) + h; +} + +float3 op_rep(in float3 p, in float3 c) +{ + return glsl_mod(p+0.5*c,c)-0.5*c; +} + +// End licensed section diff --git a/Shaders/grass/math.cginc b/Shaders/grass/math.cginc new file mode 100644 index 0000000..c8fdf13 --- /dev/null +++ b/Shaders/grass/math.cginc @@ -0,0 +1,87 @@ +#ifndef __MATH_INC +#define __MATH_INC + +#include "pema99.cginc" + +// Differentiable approximation of the standard `max` function. +float dmax(float a, float b, float k) +{ + return log2(exp2(k * a) + exp2(k * b)) / k; +} + +// Differentiable approximation of the standard `min` function. +float dmin(float a, float b, float k) +{ + return -1.0 * dmax(-1.0 * a, -1.0 * b, k); +} + +float dabs(float a, float k) +{ + return log2(exp2(k * a) + exp2(-1.0 * k * a)); +} + +// Generate a random number on [0, 1]. +float rand2(float3 p) +{ + return frac(sin(dot(p, float2(561.0, 885.0))) * 776.2) / 2.0; +} + +// Generate a random number on [0, 1]. +float rand3(float3 p) +{ + return frac(sin(dot(p, float3(897.0, 367.0, 197.0))) * 1073.6) / 2.0; +} + +float length2(float2 p) +{ + return p.x * p.x + p.y * p.y; +} + +// 3 dimensional value noise. `p` is assumed to be a point inside a unit cube. +// Theory: https://en.wikipedia.org/wiki/Value_noise +float vnoise3d(float3 p) +{ + float3 pu = floor(p); + float3 pv = glsl_mod(frac(p), 1.0); + + // Assign random numbers to the corner of a cube. + float n000 = rand3(pu + float3(0,0,0)); + float n001 = rand3(pu + float3(0,0,1)); + float n010 = rand3(pu + float3(0,1,0)); + float n011 = rand3(pu + float3(0,1,1)); + float n100 = rand3(pu + float3(1,0,0)); + float n101 = rand3(pu + float3(1,0,1)); + float n110 = rand3(pu + float3(1,1,0)); + float n111 = rand3(pu + float3(1,1,1)); + + float n00 = lerp(n000, n001, pv.z); + float n01 = lerp(n010, n011, pv.z); + float n10 = lerp(n100, n101, pv.z); + float n11 = lerp(n110, n111, pv.z); + + float n0 = lerp(n00, n01, pv.y); + float n1 = lerp(n10, n11, pv.y); + + float n = lerp(n0, n1, pv.x); + + return n; +} + +float fbm(float3 p, const int n_octaves, float w) +{ + float g = exp2(-w); + float a = 1.0; + float p_scale = 1.0; + + float res = 0.0; + for (int i = 0; i < n_octaves; i++) { + res += a * vnoise3d(p * p_scale); + + p_scale /= w; + a *= g; + } + return res; +} + +#endif // __MATH_INC + diff --git a/Shaders/grass/motion.cginc b/Shaders/grass/motion.cginc new file mode 100644 index 0000000..d6458e9 --- /dev/null +++ b/Shaders/grass/motion.cginc @@ -0,0 +1,90 @@ +#ifndef MOTION_ +#define MOTION_ + +// xyz represent quaternion vector, w represents theta. +typedef float4 Quaternion; + +// Math from here: +// https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation +float3 qrot(in float3 v, in Quaternion q) +{ + float a = q.w; + float b = q.x; + float c = q.y; + float d = q.z; + + float a2 = a*a; + float b2 = b*b; + float c2 = c*c; + float d2 = d*d; + + float3x3 rot = float3x3( + (a2 + b2 - c2) - d2, 2*b*c - 2*a*d, 2*b*d + 2*a*c, + 2*b*c + 2*a*d, (a2 - b2) + (c2 - d2), 2*c*d - 2*a*b, + 2*b*d - 2*a*c, 2*c*d + 2*a*b, ((a2 - b2) - c2) + d2 + ); + + return mul(rot, v); +} + +Quaternion qinv(in Quaternion q) +{ + return Quaternion(q.xyz, -q.w); +} + +// Multiply two quaternions. +// Math from here: https://www.haroldserrano.com/blog/quaternions-in-computer-graphics +Quaternion qmul(in Quaternion a, in Quaternion b) +{ + return Quaternion(a.w * b.xyz + b.w * a.xyz + cross(a.xyz, b.xyz), a.w * b.w - dot(a.xyz, b.xyz)); +} + +float4 affine3(in float3 m) +{ + return float4(m, 1.0); +} + +float4x4 affine3x3(in float3x3 m) +{ + return float4x4( + m[0][0], m[0][1], m[0][2], 0, + m[1][0], m[1][1], m[1][2], 0, + m[2][0], m[2][1], m[2][2], 0, + 0, 0, 0, 1 + ); +} + +float4x4 eye() +{ + return float4x4( + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + ); +} + +// Return affine translation matrix. +float4x4 translate(in float dx, in float dy, in float dz) +{ + return float4x4( + 1, 0, 0, dx, + 0, 1, 0, dy, + 0, 0, 1, dz, + 0, 0, 0, 1 + ); +} + +// Return affine scaling matrix. +float4x4 scale(in float sx, in float sy, in float sz) +{ + return float4x4( + sx, 0, 0, 0, + 0, sy, 0, 0, + 0, 0, sz, 0, + 0, 0, 0, 1 + ); +} + +#endif // MOTION_ + diff --git a/Shaders/grass/pbr.cginc b/Shaders/grass/pbr.cginc new file mode 100644 index 0000000..669c691 --- /dev/null +++ b/Shaders/grass/pbr.cginc @@ -0,0 +1,93 @@ +#ifndef __PBR_INC +#define __PBR_INC + +#include "UnityPBSLighting.cginc" + +UNITY_DECLARE_TEXCUBE(_Cubemap); + +struct v2f +{ + float4 clipPos : SV_POSITION; + float2 uv : TEXCOORD0; + float3 normal : TEXCOORD1; + float3 worldPos : TEXCOORD2; + + float3 objPos : TEXCOORD3; + + #if defined(VERTEXLIGHT_ON) + float3 vertexLightColor : TEXCOORD3; + #endif +}; + +UnityLight GetLight(v2f i, float3 worldPos, float3 normal) +{ + UNITY_LIGHT_ATTENUATION(attenuation, 0, worldPos); + float3 light_color = _LightColor0.rgb * attenuation; + + UnityLight light; + light.color = light_color; + #if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT) + light.dir = normalize(_WorldSpaceLightPos0.xyz - worldPos); + #else + light.dir = _WorldSpaceLightPos0.xyz; + #endif + light.ndotl = DotClamped(normal, light.dir); + + return light; +} + +UnityIndirect GetIndirect(v2f i, float3 view_dir, float3 normal, + float smoothness, bool custom_cubemap) { + UnityIndirect indirect; + indirect.diffuse = 0; + indirect.specular = 0; + + #if defined(VERTEXLIGHT_ON) + indirect.diffuse = i.vertexLightColor; + #endif + + #if defined(FORWARD_BASE_PASS) + indirect.diffuse += max(0, ShadeSH9(float4(normal, 1))); + float3 reflect_dir = reflect(-view_dir, normal); + // There's a nonlinear relationship between mipmap level and roughness. + float roughness = 1 - smoothness; + roughness *= 1.7 - .7 * roughness; + if (custom_cubemap) { + float3 env_sample = UNITY_SAMPLE_TEXCUBE_LOD( + _Cubemap, + reflect_dir, + roughness * UNITY_SPECCUBE_LOD_STEPS); + indirect.specular = env_sample; + } else { + float3 env_sample = UNITY_SAMPLE_TEXCUBE_LOD( + unity_SpecCube0, + reflect_dir, + roughness * UNITY_SPECCUBE_LOD_STEPS); + indirect.specular = env_sample; + } + #endif + + return indirect; +} + +float4 getLitColor(v2f i, float4 albedo, float3 worldPos, float3 normal, + float metallic, float smoothness, + bool custom_cubemap) +{ + float3 specular_tint; + float one_minus_reflectivity; + albedo.rgb = DiffuseAndSpecularFromMetallic( + albedo, metallic, specular_tint, one_minus_reflectivity); + + float3 view_dir = normalize(_WorldSpaceCameraPos - i.worldPos); + + float3 pbr = UNITY_BRDF_PBS(albedo, specular_tint, + one_minus_reflectivity, smoothness, + view_dir, normal, + GetLight(i, worldPos, normal), + GetIndirect(i, view_dir, normal, smoothness, custom_cubemap)).rgb; + + return float4(saturate(pbr), albedo.a); +} + +#endif // __PBR_INC diff --git a/Shaders/grass/pema99.cginc b/Shaders/grass/pema99.cginc new file mode 100644 index 0000000..1c4b746 --- /dev/null +++ b/Shaders/grass/pema99.cginc @@ -0,0 +1,36 @@ +/* +MIT License + +Copyright (c) 2022 Pema Malling + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef __PEMA_99_INC +#define __PEMA_99_INC + +#define glsl_mod(x,y) (((x)-(y)*floor((x)/(y)))) + +bool isInMirror() +{ + return unity_CameraProjection[2][0] != 0.f || unity_CameraProjection[2][1] + != 0.f; +} + +#endif // __PEMA_99_INC diff --git a/Shaders/grass/poi.cginc b/Shaders/grass/poi.cginc new file mode 100644 index 0000000..5e31a42 --- /dev/null +++ b/Shaders/grass/poi.cginc @@ -0,0 +1,60 @@ +#ifndef __POI_INC +#define __POI_INC + +/* +MIT License + +Copyright (c) 2018 King Arthur + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +static const float Epsilon = 1e-10; +float3 RGBtoHCV(in float3 RGB) +{ + // Based on work by Sam Hocevar and Emil Persson + float4 P = (RGB.g < RGB.b) ? float4(RGB.bg, -1.0, 2.0 / 3.0) : float4(RGB.gb, 0.0, -1.0 / 3.0); + float4 Q = (RGB.r < P.x) ? float4(P.xyw, RGB.r) : float4(RGB.r, P.yzx); + float C = Q.x - min(Q.w, Q.y); + float H = abs((Q.w - Q.y) / (6 * C + Epsilon) + Q.z); + return float3(H, C, Q.x); +} + +float3 RGBtoHSV(in float3 RGB) +{ + float3 HCV = RGBtoHCV(RGB); + float S = HCV.y / (HCV.z + Epsilon); + return float3(HCV.x, S, HCV.z); +} + +float3 HUEtoRGB(in float H) +{ + float R = abs(H * 6 - 3) - 1; + float G = 2 - abs(H * 6 - 2); + float B = 2 - abs(H * 6 - 4); + return saturate(float3(R, G, B)); +} + +float3 HSVtoRGB(in float3 HSV) +{ + float3 RGB = HUEtoRGB(HSV.x); + return ((RGB - 1) * HSV.y + 1) * HSV.z; +} + +#endif // __POI_INC diff --git a/Shaders/grass/shadertoy.cginc b/Shaders/grass/shadertoy.cginc new file mode 100644 index 0000000..b19193d --- /dev/null +++ b/Shaders/grass/shadertoy.cginc @@ -0,0 +1,36 @@ +#ifndef __SHADERTOY_INC +#define __SHADERTOY_INC + +#include "AutoLight.cginc" +#include "pema99.cginc" +#include "poi.cginc" + +// https://www.shadertoy.com/view/3ttSzr +void effect_crumpled_wave( out float4 fragColor, in float2 uv ){ + for(float i = 1.0; i < 8.0; i++) { + uv.y += i * 0.1 / i * + sin(uv.x * i * i + _Time[3] * 0.5) * + sin(uv.y * i * i + _Time[3] * 0.5); + } + + float3 col; + col.r = uv.y - 0.1; + col.g = uv.y + 0.3; + col.b = uv.y + 0.95; + + col = RGBtoHSV(col); + + float x_diff = 0.50 - col.x; + col.x = 0.80 + x_diff * 0.5; + col.x += uv.x / 16.0 + .05; + + col.y *= 0.8; + col.z *= 0.7; // Darken + + col.x = glsl_mod(col.x, 1.0); + col = HSVtoRGB(col); + + fragColor = float4(col,1.0); +} + +#endif // __SHADERTOY_INC |
