diff --git a/CGIncludes/HLSLSupport.cginc b/CGIncludes/HLSLSupport.cginc index 3e8fbb5..06cfdde 100644 --- a/CGIncludes/HLSLSupport.cginc +++ b/CGIncludes/HLSLSupport.cginc @@ -85,17 +85,17 @@ # endif #endif #if !defined(SV_Target1) -# if !defined(SHADER_API_XBOXONE) && !defined(SHADER_API_PSP2) +# if !defined(SHADER_API_XBOXONE) # define SV_Target1 COLOR1 # endif #endif #if !defined(SV_Target2) -# if !defined(SHADER_API_XBOXONE) && !defined(SHADER_API_PSP2) +# if !defined(SHADER_API_XBOXONE) # define SV_Target2 COLOR2 # endif #endif #if !defined(SV_Target3) -# if !defined(SHADER_API_XBOXONE) && !defined(SHADER_API_PSP2) +# if !defined(SHADER_API_XBOXONE) # define SV_Target3 COLOR3 # endif #endif @@ -125,13 +125,13 @@ # endif #endif -#if (defined(SHADER_API_GLES3) && !defined(SHADER_API_DESKTOP)) || defined(SHADER_API_GLES) || defined(SHADER_API_PSP2) || defined(SHADER_API_N3DS) +#if (defined(SHADER_API_GLES3) && !defined(SHADER_API_DESKTOP)) || defined(SHADER_API_GLES) || defined(SHADER_API_N3DS) #define UNITY_ALLOWED_MRT_COUNT 4 #else #define UNITY_ALLOWED_MRT_COUNT 8 #endif -#if (SHADER_TARGET < 30) || defined(SHADER_API_GLES3) || defined(SHADER_API_GLES) || defined(SHADER_API_PSP2) || defined(SHADER_API_N3DS) +#if (SHADER_TARGET < 30) || defined(SHADER_API_GLES3) || defined(SHADER_API_GLES) || defined(SHADER_API_N3DS) //no fast coherent dynamic branching on these hardware #else #define UNITY_FAST_COHERENT_DYNAMIC_BRANCHING 1 @@ -164,8 +164,14 @@ #define sampler3D_half sampler3D #define Texture2D_half Texture2D #define Texture2D_float Texture2D +#define Texture2DArray_half Texture2DArray +#define Texture2DArray_float Texture2DArray +#define Texture2DMS_half Texture2DMS +#define Texture2DMS_float Texture2DMS #define TextureCube_half TextureCube #define TextureCube_float TextureCube +#define TextureCubeArray_half TextureCubeArray +#define TextureCubeArray_float TextureCubeArray #define Texture3D_float Texture3D #define Texture3D_half Texture3D #endif @@ -254,13 +260,6 @@ #define asuint(x) asint(x) #endif -#if defined(SHADER_API_PSP2) -// The PSP2 cg compiler does not define uint -#define uint2 unsigned int2 -#define uint3 unsigned int3 -#define uint4 unsigned int4 -#endif - // specifically for samplers that are provided as arguments to entry functions #if defined(SHADER_API_PSSL) #define SAMPLER_UNIFORM uniform @@ -270,6 +269,10 @@ #endif #if defined(SHADER_API_PSSL) +// variable modifiers +#define nointerpolation nointerp +#define noperspective nopersp + #define CBUFFER_START(name) ConstantBuffer name { #define CBUFFER_END }; #elif defined(SHADER_API_D3D11) @@ -304,34 +307,7 @@ #define UNITY_REQUIRE_ADVANCED_BLEND(mode) #endif - -#if defined(SHADER_API_PSP2) - // For tex2Dproj the PSP2 cg compiler doesn't like casting half3/4 to - // float3/4 with swizzle (optimizer generates invalid assembly), so declare - // explicit versions for half3/4 - half4 tex2Dproj(sampler2D s, in half3 t) { return tex2D(s, t.xy / t.z); } - half4 tex2Dproj(sampler2D s, in half4 t) { return tex2D(s, t.xy / t.w); } - - // As above but for sampling from single component textures, e.g. depth textures. - // NOTE that hardware PCF does not work with these versions, currently we have to ensure - // that tex coords for shadow sampling use float, not half; and for some reason casting half - // to float and using tex2Dproj also does not work. - half4 tex2DprojShadow(sampler2D s, in half3 t) { return tex2D(s, t.xy / t.z); } - half4 tex2DprojShadow(sampler2D s, in half4 t) { return tex2D(s, t.xy / t.w); } - - // ...and versions of tex2DprojShadow for float uv. - half4 tex2DprojShadow(sampler2D s, in float3 t) { return tex2Dproj(s, t); } - half4 tex2DprojShadow(sampler2D s, in float4 t) { return tex2Dproj(s, t); } -#endif - - -#if defined(SHADER_API_PSP2) -#define UNITY_BUGGY_TEX2DPROJ4 -#define UNITY_PROJ_COORD(a) (a).xyw -#else #define UNITY_PROJ_COORD(a) a -#endif - // Depth texture sampling helpers. // On most platforms you can just sample them, but some (e.g. PSP2) need special handling. @@ -340,17 +316,6 @@ // SAMPLE_DEPTH_TEXTURE_PROJ(sampler,uv): projected sample // SAMPLE_DEPTH_TEXTURE_LOD(sampler,uv): sample with LOD level -#if defined(SHADER_API_PSP2) - half SAMPLE_DEPTH_TEXTURE(sampler2D s, float4 uv) { return tex2D(s, (float3)uv); } - half SAMPLE_DEPTH_TEXTURE(sampler2D s, float3 uv) { return tex2D(s, uv); } - half SAMPLE_DEPTH_TEXTURE(sampler2D s, float2 uv) { return tex2D(s, uv); } -# define SAMPLE_DEPTH_TEXTURE_PROJ(sampler, uv) (tex2DprojShadow(sampler, uv)) -# define SAMPLE_DEPTH_TEXTURE_LOD(sampler, uv) (tex2Dlod(sampler, uv)) -# define SAMPLE_RAW_DEPTH_TEXTURE(sampler, uv) SAMPLE_DEPTH_TEXTURE(sampler, uv) -# define SAMPLE_RAW_DEPTH_TEXTURE_PROJ(sampler, uv) SAMPLE_DEPTH_TEXTURE_PROJ(sampler, uv) -# define SAMPLE_RAW_DEPTH_TEXTURE_LOD(sampler, uv) SAMPLE_DEPTH_TEXTURE_LOD(sampler, uv) - half SAMPLE_DEPTH_CUBE_TEXTURE(samplerCUBE s, float3 uv) { return texCUBE(s, uv); } -#else // Sample depth, just the red component. # define SAMPLE_DEPTH_TEXTURE(sampler, uv) (tex2D(sampler, uv).r) # define SAMPLE_DEPTH_TEXTURE_PROJ(sampler, uv) (tex2Dproj(sampler, uv).r) @@ -360,14 +325,9 @@ # define SAMPLE_RAW_DEPTH_TEXTURE_PROJ(sampler, uv) (tex2Dproj(sampler, uv)) # define SAMPLE_RAW_DEPTH_TEXTURE_LOD(sampler, uv) (tex2Dlod(sampler, uv)) # define SAMPLE_DEPTH_CUBE_TEXTURE(sampler, uv) (texCUBE(sampler, uv).r) -#endif // Deprecated; use SAMPLE_DEPTH_TEXTURE & SAMPLE_DEPTH_TEXTURE_PROJ instead -#if defined(SHADER_API_PSP2) -# define UNITY_SAMPLE_DEPTH(value) (value).r -#else -# define UNITY_SAMPLE_DEPTH(value) (value).r -#endif +#define UNITY_SAMPLE_DEPTH(value) (value).r // Macros to declare and sample shadow maps. @@ -411,15 +371,6 @@ #define UNITY_SAMPLE_SHADOW(tex,coord) tex.SampleCmpLOD0(sampler##tex,(coord).xy,(coord).z) #define UNITY_SAMPLE_SHADOW_PROJ(tex,coord) tex.SampleCmpLOD0(sampler##tex,(coord).xy/(coord).w,(coord).z/(coord).w) #define UNITY_SAMPLE_TEXCUBE_SHADOW(tex,coord) tex.SampleCmpLOD0(sampler##tex,(coord).xyz,(coord).w) -#elif defined(SHADER_API_PSP2) - // Vita - #define UNITY_DECLARE_SHADOWMAP(tex) sampler2D tex - #define UNITY_DECLARE_TEXCUBE_SHADOWMAP(tex) samplerCUBE tex - // tex2d shadow comparison on Vita returns 0 instead of 1 when shadowCoord.z >= 1 causing artefacts in some tests. - // Clamping Z to the range 0.0 <= Z < 1.0 solves this. - #define UNITY_SAMPLE_SHADOW(tex,coord) tex2D(tex, float3((coord).xy, clamp((coord).z, 0.0, 0.999999))) - #define UNITY_SAMPLE_SHADOW_PROJ(tex,coord) tex2DprojShadow(tex, coord) - #define UNITY_SAMPLE_TEXCUBE_SHADOW(tex,coord) texCUBE(tex, float3((coord).xyz, clamp((coord).w, 0.0, 0.999999))) #else // Fallback / No built-in shadowmap comparison sampling: regular texture sample and do manual depth comparison #define UNITY_DECLARE_SHADOWMAP(tex) sampler2D_float tex @@ -631,18 +582,13 @@ #endif -// Declare position that is also available for read in fragment shader -#if defined(SHADER_API_PSP2) && defined(SHADER_STAGE_FRAGMENT) && SHADER_TARGET >= 30 -#define UNITY_POSITION(pos) float4 pos : VPOS -#else // On D3D reading screen space coordinates from fragment shader requires SM3.0 #define UNITY_POSITION(pos) float4 pos : SV_POSITION -#endif // Kept for backwards-compatibility #define UNITY_ATTEN_CHANNEL r -#if defined(SHADER_API_D3D11) || defined(SHADER_API_PSP2) || defined(SHADER_API_PSSL) || defined(SHADER_API_METAL) || defined(SHADER_API_VULKAN) || defined(SHADER_API_SWITCH) +#if defined(SHADER_API_D3D11) || defined(SHADER_API_PSSL) || defined(SHADER_API_METAL) || defined(SHADER_API_VULKAN) || defined(SHADER_API_SWITCH) #define UNITY_UV_STARTS_AT_TOP 1 #endif @@ -665,19 +611,11 @@ // UNITY_ENABLE_REFLECTION_BUFFERS - render reflection probes in deferred way, when using deferred shading -#if defined(SHADER_API_PSP2) -// To get acceptable precision from the SGX interpolators when decoding RGBM type -// textures we have to disable sRGB reads and then convert to gamma space in the shader -// explicitly. -#define UNITY_FORCE_LINEAR_READ_FOR_RGBM -#endif - - // On most platforms, use floating point render targets to store depth of point // light shadowmaps. However, on some others they either have issues, or aren't widely // supported; in which case fallback to encoding depth into RGBA channels. // Make sure this define matches GraphicsCaps.useRGBAForPointShadows. -#if defined(SHADER_API_GLES) || defined(SHADER_API_GLES3) || defined(SHADER_API_PSP2) +#if defined(SHADER_API_GLES) || defined(SHADER_API_GLES3) #define UNITY_USE_RGBA_FOR_POINT_SHADOWS #endif @@ -730,7 +668,7 @@ #endif // define use to identify platform with modern feature like texture 3D with filtering, texture array etc... -#define UNITY_SM40_PLUS_PLATFORM (!((SHADER_TARGET < 30) || defined (SHADER_API_MOBILE) || defined (SHADER_API_PSP2) || defined(SHADER_API_GLES))) +#define UNITY_SM40_PLUS_PLATFORM (!((SHADER_TARGET < 30) || defined (SHADER_API_MOBILE) || defined(SHADER_API_GLES))) // Ability to manually set descriptor set and binding numbers (Vulkan only) #if defined(SHADER_API_VULKAN) diff --git a/CGIncludes/SpeedTree8Common.cginc b/CGIncludes/SpeedTree8Common.cginc new file mode 100644 index 0000000..e881c60 --- /dev/null +++ b/CGIncludes/SpeedTree8Common.cginc @@ -0,0 +1,317 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +/////////////////////////////////////////////////////////////////////// +// SpeedTree8Common.cginc + +#ifndef SPEEDTREE8_COMMON_INCLUDED +#define SPEEDTREE8_COMMON_INCLUDED + +#include "UnityCG.cginc" +#include "UnityPBSLighting.cginc" + +#if defined(ENABLE_WIND) && !defined(_WINDQUALITY_NONE) + #define SPEEDTREE_Y_UP + #include "SpeedTreeWind.cginc" + float _WindEnabled; + UNITY_INSTANCING_BUFFER_START(STWind) + UNITY_DEFINE_INSTANCED_PROP(float, _GlobalWindTime) + UNITY_INSTANCING_BUFFER_END(STWind) +#endif + +struct Input +{ + half2 uv_MainTex : TEXCOORD0; + fixed4 color : COLOR; + + #ifdef EFFECT_BACKSIDE_NORMALS + fixed facing : VFACE; + #endif +}; + +sampler2D _MainTex; +fixed4 _Color; +int _TwoSided; + +#ifdef EFFECT_BUMP + sampler2D _BumpMap; +#endif + +#ifdef EFFECT_EXTRA_TEX + sampler2D _ExtraTex; +#else + half _Glossiness; + half _Metallic; +#endif + +#ifdef EFFECT_HUE_VARIATION + half4 _HueVariationColor; +#endif + +#ifdef EFFECT_BILLBOARD + half _BillboardShadowFade; +#endif + +#ifdef EFFECT_SUBSURFACE + sampler2D _SubsurfaceTex; + fixed4 _SubsurfaceColor; + half _SubsurfaceIndirect; +#endif + +#define GEOM_TYPE_BRANCH 0 +#define GEOM_TYPE_FROND 1 +#define GEOM_TYPE_LEAF 2 +#define GEOM_TYPE_FACINGLEAF 3 + + +/////////////////////////////////////////////////////////////////////// +// OffsetSpeedTreeVertex + +void OffsetSpeedTreeVertex(inout appdata_full data, float lodValue) +{ + // smooth LOD + #if defined(LOD_FADE_PERCENTAGE) && !defined(EFFECT_BILLBOARD) + data.vertex.xyz = lerp(data.vertex.xyz, data.texcoord2.xyz, lodValue); + #endif + + // wind + #if defined(ENABLE_WIND) && !defined(_WINDQUALITY_NONE) + if (_WindEnabled > 0) + { + float3 rotatedWindVector = mul(_ST_WindVector.xyz, (float3x3)unity_ObjectToWorld); + float windLength = length(rotatedWindVector); + if (windLength < 1e-5) + { + // sanity check that wind data is available + return; + } + rotatedWindVector /= windLength; + + float3 treePos = float3(unity_ObjectToWorld[0].w, unity_ObjectToWorld[1].w, unity_ObjectToWorld[2].w); + float3 windyPosition = data.vertex.xyz; + + #ifndef EFFECT_BILLBOARD + // geometry type + float geometryType = (int)(data.texcoord3.w + 0.25); + bool leafTwo = false; + if (geometryType > GEOM_TYPE_FACINGLEAF) + { + geometryType -= 2; + leafTwo = true; + } + + // leaves + if (geometryType > GEOM_TYPE_FROND) + { + // remove anchor position + float3 anchor = float3(data.texcoord1.zw, data.texcoord2.w); + windyPosition -= anchor; + + if (geometryType == GEOM_TYPE_FACINGLEAF) + { + // face camera-facing leaf to camera + float offsetLen = length(windyPosition); + windyPosition = mul(windyPosition.xyz, (float3x3)UNITY_MATRIX_IT_MV); // inv(MV) * windyPosition + windyPosition = normalize(windyPosition) * offsetLen; // make sure the offset vector is still scaled + } + + // leaf wind + #if defined(_WINDQUALITY_FAST) || defined(_WINDQUALITY_BETTER) || defined(_WINDQUALITY_BEST) + #ifdef _WINDQUALITY_BEST + bool bBestWind = true; + #else + bool bBestWind = false; + #endif + float leafWindTrigOffset = anchor.x + anchor.y; + windyPosition = LeafWind(bBestWind, leafTwo, windyPosition, data.normal, data.texcoord3.x, float3(0,0,0), data.texcoord3.y, data.texcoord3.z, leafWindTrigOffset, rotatedWindVector); + #endif + + // move back out to anchor + windyPosition += anchor; + } + + // frond wind + bool bPalmWind = false; + #ifdef _WINDQUALITY_PALM + bPalmWind = true; + if (geometryType == GEOM_TYPE_FROND) + { + windyPosition = RippleFrond(windyPosition, data.normal, data.texcoord.x, data.texcoord.y, data.texcoord3.x, data.texcoord3.y, data.texcoord3.z); + } + #endif + + // branch wind (applies to all 3D geometry) + #if defined(_WINDQUALITY_BETTER) || defined(_WINDQUALITY_BEST) || defined(_WINDQUALITY_PALM) + float3 rotatedBranchAnchor = normalize(mul(_ST_WindBranchAnchor.xyz, (float3x3)unity_ObjectToWorld)) * _ST_WindBranchAnchor.w; + windyPosition = BranchWind(bPalmWind, windyPosition, treePos, float4(data.texcoord.zw, 0, 0), rotatedWindVector, rotatedBranchAnchor); + #endif + + #endif // !EFFECT_BILLBOARD + + // global wind + float globalWindTime = _ST_WindGlobal.x; + #if defined(EFFECT_BILLBOARD) && defined(UNITY_INSTANCING_ENABLED) + globalWindTime += UNITY_ACCESS_INSTANCED_PROP(STWind, _GlobalWindTime); + #endif + windyPosition = GlobalWind(windyPosition, treePos, true, rotatedWindVector, globalWindTime); + data.vertex.xyz = windyPosition; + } + #endif +} + + +/////////////////////////////////////////////////////////////////////// +// vertex program + +void SpeedTreeVert(inout appdata_full v) +{ + // handle speedtree wind and lod + OffsetSpeedTreeVertex(v, unity_LODFade.x); + + float3 treePos = float3(unity_ObjectToWorld[0].w, unity_ObjectToWorld[1].w, unity_ObjectToWorld[2].w); + + #if defined(EFFECT_BILLBOARD) + + // crossfade faces + bool topDown = (v.texcoord.z > 0.5); + float3 viewDir = UNITY_MATRIX_IT_MV[2].xyz; + float3 cameraDir = normalize(mul((float3x3)unity_WorldToObject, _WorldSpaceCameraPos - treePos)); + float viewDot = max(dot(viewDir, v.normal), dot(cameraDir, v.normal)); + viewDot *= viewDot; + viewDot *= viewDot; + viewDot += topDown ? 0.38 : 0.18; // different scales for horz and vert billboards to fix transition zone + v.color = float4(1, 1, 1, clamp(viewDot, 0, 1)); + + // if invisible, avoid overdraw + if (viewDot < 0.3333) + { + v.vertex.xyz = float3(0,0,0); + } + + // adjust lighting on billboards to prevent seams between the different faces + if (topDown) + { + v.normal += cameraDir; + } + else + { + half3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w; + float3 right = cross(cameraDir, binormal); + v.normal = cross(binormal, right); + } + v.normal = normalize(v.normal); + + #endif + + // color already contains (ao, ao, ao, blend) + // put hue variation amount in there + #ifdef EFFECT_HUE_VARIATION + float hueVariationAmount = frac(treePos.x + treePos.y + treePos.z); + v.color.g = saturate(hueVariationAmount * _HueVariationColor.a); + #endif +} + + +/////////////////////////////////////////////////////////////////////// +// lighting function to add subsurface + +half4 LightingSpeedTreeSubsurface(inout SurfaceOutputStandard s, half3 viewDir, UnityGI gi) +{ + #ifdef EFFECT_SUBSURFACE + half fSubsurfaceRough = 0.7 - s.Smoothness * 0.5; + half fSubsurface = GGXTerm(clamp(-dot(gi.light.dir, viewDir), 0, 1), fSubsurfaceRough); + + // put modulated subsurface back into emission + s.Emission *= (gi.indirect.diffuse * _SubsurfaceIndirect + gi.light.color * fSubsurface); + #endif + + return LightingStandard(s, viewDir, gi); +} + +void LightingSpeedTreeSubsurface_GI(inout SurfaceOutputStandard s, UnityGIInput data, inout UnityGI gi) +{ + #ifdef EFFECT_BILLBOARD + // fade off the shadows on billboards to avoid artifacts + data.atten = lerp(data.atten, 1.0, _BillboardShadowFade); + #endif + + LightingStandard_GI(s, data, gi); +} + +half4 LightingSpeedTreeSubsurface_Deferred(SurfaceOutputStandard s, half3 viewDir, UnityGI gi, out half4 outGBuffer0, out half4 outGBuffer1, out half4 outGBuffer2) +{ + // no light/shadow info in deferred, so stop subsurface + s.Emission = half3(0,0,0); + + return LightingStandard_Deferred(s, viewDir, gi, outGBuffer0, outGBuffer1, outGBuffer2); +} + + +/////////////////////////////////////////////////////////////////////// +// surface shader + +void SpeedTreeSurf(Input IN, inout SurfaceOutputStandard OUT) +{ + fixed4 color = tex2D(_MainTex, IN.uv_MainTex) * _Color; + + // transparency + OUT.Alpha = color.a * IN.color.a; + clip(OUT.Alpha - 0.3333); + + // color + OUT.Albedo = color.rgb; + + // hue variation + #ifdef EFFECT_HUE_VARIATION + half3 shiftedColor = lerp(OUT.Albedo, _HueVariationColor.rgb, IN.color.g); + + // preserve vibrance + half maxBase = max(OUT.Albedo.r, max(OUT.Albedo.g, OUT.Albedo.b)); + half newMaxBase = max(shiftedColor.r, max(shiftedColor.g, shiftedColor.b)); + maxBase /= newMaxBase; + maxBase = maxBase * 0.5f + 0.5f; + shiftedColor.rgb *= maxBase; + + OUT.Albedo = saturate(shiftedColor); + #endif + + // normal + #ifdef EFFECT_BUMP + OUT.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex)); + #elif defined(EFFECT_BACKSIDE_NORMALS) || defined(EFFECT_BILLBOARD) + OUT.Normal = float3(0, 0, 1); + #endif + + // flip normal on backsides + #ifdef EFFECT_BACKSIDE_NORMALS + if (IN.facing < 0.5) + { + OUT.Normal.z = -OUT.Normal.z; + } + #endif + + // adjust billboard normals to improve GI and matching + #ifdef EFFECT_BILLBOARD + OUT.Normal.z *= 0.5; + OUT.Normal = normalize(OUT.Normal); + #endif + + // extra + #ifdef EFFECT_EXTRA_TEX + fixed4 extra = tex2D(_ExtraTex, IN.uv_MainTex); + OUT.Smoothness = extra.r; + OUT.Metallic = extra.g; + OUT.Occlusion = extra.b * IN.color.r; + #else + OUT.Smoothness = _Glossiness; + OUT.Metallic = _Metallic; + OUT.Occlusion = IN.color.r; + #endif + + // subsurface (hijack emissive) + #ifdef EFFECT_SUBSURFACE + OUT.Emission = tex2D(_SubsurfaceTex, IN.uv_MainTex) * _SubsurfaceColor; + #endif +} + + +#endif // SPEEDTREE8_COMMON_INCLUDED diff --git a/CGIncludes/TerrainPreview.cginc b/CGIncludes/TerrainPreview.cginc new file mode 100644 index 0000000..b962514 --- /dev/null +++ b/CGIncludes/TerrainPreview.cginc @@ -0,0 +1,84 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +#ifndef TERRAIN_PREVIEW_INCLUDED +#define TERRAIN_PREVIEW_INCLUDED + + +// function to convert paint context pixels to heightmap uv +sampler2D _Heightmap; +float2 _HeightmapUV_PCPixelsX; +float2 _HeightmapUV_PCPixelsY; +float2 _HeightmapUV_Offset; +float2 PaintContextPixelsToHeightmapUV(float2 pcPixels) +{ + return _HeightmapUV_PCPixelsX * pcPixels.x + + _HeightmapUV_PCPixelsY * pcPixels.y + + _HeightmapUV_Offset; +} + +// function to convert paint context pixels to object position (terrain position) +float3 _ObjectPos_PCPixelsX; +float3 _ObjectPos_PCPixelsY; +float3 _ObjectPos_HeightMapSample; +float3 _ObjectPos_Offset; +float3 PaintContextPixelsToObjectPosition(float2 pcPixels, float heightmapSample) +{ + // note: we could assume no object space rotation and make this dramatically simpler + return _ObjectPos_PCPixelsX * pcPixels.x + + _ObjectPos_PCPixelsY * pcPixels.y + + _ObjectPos_HeightMapSample * heightmapSample + + _ObjectPos_Offset; +} + +// function to convert paint context pixels to brush uv +float2 _BrushUV_PCPixelsX; +float2 _BrushUV_PCPixelsY; +float2 _BrushUV_Offset; +float2 PaintContextPixelsToBrushUV(float2 pcPixels) +{ + return _BrushUV_PCPixelsX * pcPixels.x + + _BrushUV_PCPixelsY * pcPixels.y + + _BrushUV_Offset; +} + +// function to convert terrain object position to world position +// We would normally use the ObjectToWorld / ObjectToClip calls to do this, but DrawProcedural does not set them +// 'luckily' terrains cannot be rotated or scaled, so this transform is very simple +float3 _TerrainObjectToWorldOffset; +float3 TerrainObjectToWorldPosition(float3 objectPosition) +{ + return objectPosition + _TerrainObjectToWorldOffset; +} + +// function to build a procedural quad mesh +// based on the quad resolution defined by _QuadRez +// returns integer positions, starting with (0, 0), and ending with (_QuadRez.xy - 1) +float3 _QuadRez; // quads X, quads Y, vertexCount +float2 BuildProceduralQuadMeshVertex(uint vertexID) +{ + int quadIndex = vertexID / 6; // quad index, each quad is made of 6 vertices + int vertIndex = vertexID - quadIndex * 6; // vertex index within the quad [0..5] + int qY = floor((quadIndex + 0.5f) / _QuadRez.x); // quad coords for current quad (Y) + int qX = round(quadIndex - qY * _QuadRez.x); // quad coords for current quad (X) + + // each quad is defined by 6 vertices (two triangles), offset from (qX,qY) as follows: + // vX = 0, 0, 1, 1, 1, 0 + // vY = 0, 1, 1, 1, 0, 0 + float sequence[6] = { 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f }; + float vX = sequence[vertIndex]; + float vY = sequence[5 - vertIndex]; // vY is just vX reversed + float2 coord = float2(qX + vX, qY + vY); + return coord; +} + + +float Stripe(in float x, in float stripeX, in float pixelWidth) +{ + // compute derivatives to get ddx / pixel + float2 derivatives = float2(ddx(x), ddy(x)); + float derivLen = length(derivatives); + float sharpen = 1.0f / max(derivLen, 0.00001f); + return saturate(0.5f + 0.5f * (0.5f * pixelWidth - sharpen * abs(x - stripeX))); +} + +#endif diff --git a/CGIncludes/TerrainSplatmapCommon.cginc b/CGIncludes/TerrainSplatmapCommon.cginc index 9b44d87..8be0939 100644 --- a/CGIncludes/TerrainSplatmapCommon.cginc +++ b/CGIncludes/TerrainSplatmapCommon.cginc @@ -3,44 +3,102 @@ #ifndef TERRAIN_SPLATMAP_COMMON_CGINC_INCLUDED #define TERRAIN_SPLATMAP_COMMON_CGINC_INCLUDED +#ifdef _NORMALMAP + // Since 2018.3 we changed from _TERRAIN_NORMAL_MAP to _NORMALMAP to save 1 keyword. + #define _TERRAIN_NORMAL_MAP +#endif + struct Input { - float2 uv_Splat0 : TEXCOORD0; - float2 uv_Splat1 : TEXCOORD1; - float2 uv_Splat2 : TEXCOORD2; - float2 uv_Splat3 : TEXCOORD3; - float2 tc_Control : TEXCOORD4; // Not prefixing '_Contorl' with 'uv' allows a tighter packing of interpolators, which is necessary to support directional lightmap. - UNITY_FOG_COORDS(5) + float4 tc; + #ifndef TERRAIN_BASE_PASS + UNITY_FOG_COORDS(0) // needed because finalcolor oppresses fog code generation. + #endif }; sampler2D _Control; float4 _Control_ST; -sampler2D _Splat0,_Splat1,_Splat2,_Splat3; +float4 _Control_TexelSize; +sampler2D _Splat0, _Splat1, _Splat2, _Splat3; +float4 _Splat0_ST, _Splat1_ST, _Splat2_ST, _Splat3_ST; + +#if defined(UNITY_INSTANCING_ENABLED) && !defined(SHADER_API_D3D11_9X) + sampler2D _TerrainHeightmapTexture; + sampler2D _TerrainNormalmapTexture; + float4 _TerrainHeightmapRecipSize; // float4(1.0f/width, 1.0f/height, 1.0f/(width-1), 1.0f/(height-1)) + float4 _TerrainHeightmapScale; // float4(hmScale.x, hmScale.y / (float)(kMaxHeight), hmScale.z, 0.0f) +#endif -#ifdef _TERRAIN_NORMAL_MAP +UNITY_INSTANCING_BUFFER_START(Terrain) + UNITY_DEFINE_INSTANCED_PROP(float4, _TerrainPatchInstanceData) // float4(xBase, yBase, skipScale, ~) +UNITY_INSTANCING_BUFFER_END(Terrain) + +#ifdef _NORMALMAP sampler2D _Normal0, _Normal1, _Normal2, _Normal3; + float _NormalScale0, _NormalScale1, _NormalScale2, _NormalScale3; +#endif + +#if defined(TERRAIN_BASE_PASS) && defined(UNITY_PASS_META) + // When we render albedo for GI baking, we actually need to take the ST + float4 _MainTex_ST; #endif void SplatmapVert(inout appdata_full v, out Input data) { UNITY_INITIALIZE_OUTPUT(Input, data); - data.tc_Control = TRANSFORM_TEX(v.texcoord, _Control); // Need to manually transform uv here, as we choose not to use 'uv' prefix for this texcoord. - float4 pos = UnityObjectToClipPos(v.vertex); - UNITY_TRANSFER_FOG(data, pos); -#ifdef _TERRAIN_NORMAL_MAP +#if defined(UNITY_INSTANCING_ENABLED) && !defined(SHADER_API_D3D11_9X) + + float2 patchVertex = v.vertex.xy; + float4 instanceData = UNITY_ACCESS_INSTANCED_PROP(Terrain, _TerrainPatchInstanceData); + + float4 uvscale = instanceData.z * _TerrainHeightmapRecipSize; + float4 uvoffset = instanceData.xyxy * uvscale; + uvoffset.xy += 0.5f * _TerrainHeightmapRecipSize.xy; + float2 sampleCoords = (patchVertex.xy * uvscale.xy + uvoffset.xy); + + float hm = UnpackHeightmap(tex2Dlod(_TerrainHeightmapTexture, float4(sampleCoords, 0, 0))); + v.vertex.xz = (patchVertex.xy + instanceData.xy) * _TerrainHeightmapScale.xz * instanceData.z; //(x + xBase) * hmScale.x * skipScale; + v.vertex.y = hm * _TerrainHeightmapScale.y; + v.vertex.w = 1.0f; + + v.texcoord.xy = (patchVertex.xy * uvscale.zw + uvoffset.zw); + v.texcoord3 = v.texcoord2 = v.texcoord1 = v.texcoord; + + #ifdef TERRAIN_INSTANCED_PERPIXEL_NORMAL + v.normal = float3(0, 1, 0); // TODO: reconstruct the tangent space in the pixel shader. Seems to be hard with surface shader especially when other attributes are packed together with tSpace. + data.tc.zw = sampleCoords; + #else + float3 nor = tex2Dlod(_TerrainNormalmapTexture, float4(sampleCoords, 0, 0)).xyz; + v.normal = 2.0f * nor - 1.0f; + #endif +#endif + v.tangent.xyz = cross(v.normal, float3(0,0,1)); v.tangent.w = -1; + + data.tc.xy = v.texcoord; +#ifdef TERRAIN_BASE_PASS + #ifdef UNITY_PASS_META + data.tc.xy = v.texcoord * _MainTex_ST.xy + _MainTex_ST.zw; + #endif +#else + float4 pos = UnityObjectToClipPos(v.vertex); + UNITY_TRANSFER_FOG(data, pos); #endif } +#ifndef TERRAIN_BASE_PASS + #ifdef TERRAIN_STANDARD_SHADER void SplatmapMix(Input IN, half4 defaultAlpha, out half4 splat_control, out half weight, out fixed4 mixedDiffuse, inout fixed3 mixedNormal) #else void SplatmapMix(Input IN, out half4 splat_control, out half weight, out fixed4 mixedDiffuse, inout fixed3 mixedNormal) #endif { - splat_control = tex2D(_Control, IN.tc_Control); + // adjust splatUVs so the edges of the terrain tile lie on pixel centers + float2 splatUV = (IN.tc.xy * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy; + splat_control = tex2D(_Control, splatUV); weight = dot(splat_control, half4(1,1,1,1)); #if !defined(SHADER_API_MOBILE) && defined(TERRAIN_SPLAT_ADDPASS) @@ -51,26 +109,48 @@ void SplatmapMix(Input IN, out half4 splat_control, out half weight, out fixed4 // lighting result can be correctly weighted. splat_control /= (weight + 1e-3f); + float2 uvSplat0 = TRANSFORM_TEX(IN.tc.xy, _Splat0); + float2 uvSplat1 = TRANSFORM_TEX(IN.tc.xy, _Splat1); + float2 uvSplat2 = TRANSFORM_TEX(IN.tc.xy, _Splat2); + float2 uvSplat3 = TRANSFORM_TEX(IN.tc.xy, _Splat3); + mixedDiffuse = 0.0f; #ifdef TERRAIN_STANDARD_SHADER - mixedDiffuse += splat_control.r * tex2D(_Splat0, IN.uv_Splat0) * half4(1.0, 1.0, 1.0, defaultAlpha.r); - mixedDiffuse += splat_control.g * tex2D(_Splat1, IN.uv_Splat1) * half4(1.0, 1.0, 1.0, defaultAlpha.g); - mixedDiffuse += splat_control.b * tex2D(_Splat2, IN.uv_Splat2) * half4(1.0, 1.0, 1.0, defaultAlpha.b); - mixedDiffuse += splat_control.a * tex2D(_Splat3, IN.uv_Splat3) * half4(1.0, 1.0, 1.0, defaultAlpha.a); + mixedDiffuse += splat_control.r * tex2D(_Splat0, uvSplat0) * half4(1.0, 1.0, 1.0, defaultAlpha.r); + mixedDiffuse += splat_control.g * tex2D(_Splat1, uvSplat1) * half4(1.0, 1.0, 1.0, defaultAlpha.g); + mixedDiffuse += splat_control.b * tex2D(_Splat2, uvSplat2) * half4(1.0, 1.0, 1.0, defaultAlpha.b); + mixedDiffuse += splat_control.a * tex2D(_Splat3, uvSplat3) * half4(1.0, 1.0, 1.0, defaultAlpha.a); #else - mixedDiffuse += splat_control.r * tex2D(_Splat0, IN.uv_Splat0); - mixedDiffuse += splat_control.g * tex2D(_Splat1, IN.uv_Splat1); - mixedDiffuse += splat_control.b * tex2D(_Splat2, IN.uv_Splat2); - mixedDiffuse += splat_control.a * tex2D(_Splat3, IN.uv_Splat3); + mixedDiffuse += splat_control.r * tex2D(_Splat0, uvSplat0); + mixedDiffuse += splat_control.g * tex2D(_Splat1, uvSplat1); + mixedDiffuse += splat_control.b * tex2D(_Splat2, uvSplat2); + mixedDiffuse += splat_control.a * tex2D(_Splat3, uvSplat3); + #endif + + #ifdef _NORMALMAP + mixedNormal = UnpackNormalWithScale(tex2D(_Normal0, uvSplat0), _NormalScale0) * splat_control.r; + mixedNormal += UnpackNormalWithScale(tex2D(_Normal1, uvSplat1), _NormalScale1) * splat_control.g; + mixedNormal += UnpackNormalWithScale(tex2D(_Normal2, uvSplat2), _NormalScale2) * splat_control.b; + mixedNormal += UnpackNormalWithScale(tex2D(_Normal3, uvSplat3), _NormalScale3) * splat_control.a; + mixedNormal.z += 1e-5f; // to avoid nan after normalizing #endif - #ifdef _TERRAIN_NORMAL_MAP - fixed4 nrm = 0.0f; - nrm += splat_control.r * tex2D(_Normal0, IN.uv_Splat0); - nrm += splat_control.g * tex2D(_Normal1, IN.uv_Splat1); - nrm += splat_control.b * tex2D(_Normal2, IN.uv_Splat2); - nrm += splat_control.a * tex2D(_Normal3, IN.uv_Splat3); - mixedNormal = UnpackNormal(nrm); + #if defined(INSTANCING_ON) && defined(SHADER_TARGET_SURFACE_ANALYSIS) && defined(TERRAIN_INSTANCED_PERPIXEL_NORMAL) + mixedNormal = float3(0, 0, 1); // make sure that surface shader compiler realizes we write to normal, as UNITY_INSTANCING_ENABLED is not defined for SHADER_TARGET_SURFACE_ANALYSIS. + #endif + + #if defined(UNITY_INSTANCING_ENABLED) && !defined(SHADER_API_D3D11_9X) && defined(TERRAIN_INSTANCED_PERPIXEL_NORMAL) + float3 geomNormal = normalize(tex2D(_TerrainNormalmapTexture, IN.tc.zw).xyz * 2 - 1); + #ifdef _NORMALMAP + float3 geomTangent = normalize(cross(geomNormal, float3(0, 0, 1))); + float3 geomBitangent = normalize(cross(geomTangent, geomNormal)); + mixedNormal = mixedNormal.x * geomTangent + + mixedNormal.y * geomBitangent + + mixedNormal.z * geomNormal; + #else + mixedNormal = geomNormal; + #endif + mixedNormal = mixedNormal.xzy; #endif } @@ -99,4 +179,6 @@ void SplatmapFinalGBuffer(Input IN, TERRAIN_SURFACE_OUTPUT o, inout half4 outGBu emission *= o.Alpha; } +#endif // TERRAIN_BASE_PASS + #endif // TERRAIN_SPLATMAP_COMMON_CGINC_INCLUDED diff --git a/CGIncludes/TerrainTool.cginc b/CGIncludes/TerrainTool.cginc new file mode 100644 index 0000000..989afde --- /dev/null +++ b/CGIncludes/TerrainTool.cginc @@ -0,0 +1,24 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +#ifndef TERRAIN_TOOL_INCLUDED +#define TERRAIN_TOOL_INCLUDED + + +// function to convert paint context UV to brush uv +float4 _PCUVToBrushUVScales; +float2 _PCUVToBrushUVOffset; +float2 PaintContextUVToBrushUV(float2 pcUV) +{ + return _PCUVToBrushUVScales.xy * pcUV.x + + _PCUVToBrushUVScales.zw * pcUV.y + + _PCUVToBrushUVOffset; +} + + +float2 PaintContextUVToHeightmapUV(float2 pcUV) +{ + return pcUV; +} + + +#endif diff --git a/CGIncludes/UnityCG.cginc b/CGIncludes/UnityCG.cginc index 86bccbc..20894c4 100644 --- a/CGIncludes/UnityCG.cginc +++ b/CGIncludes/UnityCG.cginc @@ -292,7 +292,7 @@ float3 Shade4PointLights ( // to calculate but lights are treated as spot lights otherwise they are treated as point lights. float3 ShadeVertexLightsFull (float4 vertex, float3 normal, int lightCount, bool spotLight) { - float3 viewpos = UnityObjectToViewPos (vertex); + float3 viewpos = UnityObjectToViewPos (vertex.xyz); float3 viewN = normalize (mul ((float3x3)UNITY_MATRIX_IT_MV, normal)); float3 lightColor = UNITY_LIGHTMODEL_AMBIENT.xyz; @@ -688,6 +688,18 @@ inline fixed3 UnpackNormal(fixed4 packednormal) #endif } +fixed3 UnpackNormalWithScale(fixed4 packednormal, float scale) +{ +#ifndef UNITY_NO_DXT5nm + // Unpack normal as DXT5nm (1, y, 1, x) or BC5 (x, y, 0, 1) + // Note neutral texture like "bump" is (0, 0, 1, 1) to work with both plain RGB normal and DXT5nm/BC5 + packednormal.x *= packednormal.w; +#endif + fixed3 normal; + normal.xy = (packednormal.xy * 2 - 1) * scale; + normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy))); + return normal; +} // Z buffer to linear 0..1 depth inline float Linear01Depth( float z ) @@ -1175,4 +1187,26 @@ UNITY_DECLARE_SHADOWMAP(_ShadowMapTexture); #define UNITY_OUTPUT_DEPTH(i) return 0 + +#define API_HAS_GUARANTEED_R16_SUPPORT !(SHADER_API_VULKAN || SHADER_API_GLES || SHADER_API_GLES3) + +float4 PackHeightmap(float height) +{ + #if (API_HAS_GUARANTEED_R16_SUPPORT) + return height; + #else + uint a = (uint)(65535.0f * height); + return float4((a >> 0) & 0xFF, (a >> 8) & 0xFF, 0, 0) / 255.0f; + #endif +} + +float UnpackHeightmap(float4 height) +{ + #if (API_HAS_GUARANTEED_R16_SUPPORT) + return height.r; + #else + return (height.r + height.g * 256.0f) / 257.0f; // (255.0f * height.r + 255.0f * 256.0f * height.g) / 65535.0f + #endif +} + #endif // UNITY_CG_INCLUDED diff --git a/CGIncludes/UnityInstancing.cginc b/CGIncludes/UnityInstancing.cginc index df42240..83b57ed 100644 --- a/CGIncludes/UnityInstancing.cginc +++ b/CGIncludes/UnityInstancing.cginc @@ -237,6 +237,11 @@ #define UNITY_USE_LODFADE_ARRAY #endif + #if defined(UNITY_INSTANCED_RENDERING_LAYER) + #define UNITY_USE_RENDERINGLAYER_ARRAY + #endif + + #ifdef UNITY_INSTANCED_LIGHTMAPSTS #ifdef LIGHTMAP_ON #define UNITY_USE_LIGHTMAPST_ARRAY @@ -267,6 +272,10 @@ // the quantized fade value (unity_LODFade.y) is automatically used for cross-fading instances #define unity_LODFade UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_LODFadeArray).xyxx #endif + #if defined(UNITY_USE_RENDERINGLAYER_ARRAY) && defined(UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE) + UNITY_DEFINE_INSTANCED_PROP(float, unity_RenderingLayerArray) + #define unity_RenderingLayer UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_RenderingLayerArray).xxxx + #endif UNITY_INSTANCING_BUFFER_END(unity_Builtins0) UNITY_INSTANCING_BUFFER_START(PerDraw1) @@ -278,6 +287,10 @@ // the quantized fade value (unity_LODFade.y) is automatically used for cross-fading instances #define unity_LODFade UNITY_ACCESS_INSTANCED_PROP(unity_Builtins1, unity_LODFadeArray).xyxx #endif + #if defined(UNITY_USE_RENDERINGLAYER_ARRAY) && !defined(UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE) + UNITY_DEFINE_INSTANCED_PROP(float, unity_RenderingLayerArray) + #define unity_RenderingLayer UNITY_ACCESS_INSTANCED_PROP(unity_Builtins1, unity_RenderingLayerArray).xxxx + #endif UNITY_INSTANCING_BUFFER_END(unity_Builtins1) UNITY_INSTANCING_BUFFER_START(PerDraw2) diff --git a/CGIncludes/UnityMetaPass.cginc b/CGIncludes/UnityMetaPass.cginc index d0b108a..b3b5585 100644 --- a/CGIncludes/UnityMetaPass.cginc +++ b/CGIncludes/UnityMetaPass.cginc @@ -24,14 +24,23 @@ struct UnityMetaInput half3 Albedo; half3 Emission; half3 SpecularColor; +#ifdef EDITOR_VISUALIZATION + float2 VizUV; + float4 LightCoord; +#endif }; -#if defined(EDITOR_VISUALIZATION) +#ifdef EDITOR_VISUALIZATION //Visualization defines // Should be kept in sync with the EditorVisualizationMode enum in EditorCameraDrawing.cpp -#define PBR_VALIDATION_ALBEDO 0 -#define PBR_VALIDATION_METALSPECULAR 1 +#define EDITORVIZ_PBR_VALIDATION_ALBEDO 0 +#define EDITORVIZ_PBR_VALIDATION_METALSPECULAR 1 +#define EDITORVIZ_TEXTURE 2 +#define EDITORVIZ_SHOWLIGHTMASK 3 +// Old names... +#define PBR_VALIDATION_ALBEDO EDITORVIZ_PBR_VALIDATION_ALBEDO +#define PBR_VALIDATION_METALSPECULAR EDITORVIZ_PBR_VALIDATION_METALSPECULAR uniform int _CheckPureMetal = 0;// flag to check only full metal, not partial metal, known because it has metallic features and pure black albedo uniform int _CheckAlbedo = 0; // if 0, pass through untouched color @@ -41,6 +50,20 @@ uniform half _AlbedoMaxLuminance = 1.0; uniform half _AlbedoHueTolerance = 0.1; uniform half _AlbedoSaturationTolerance = 0.1; +uniform sampler2D unity_EditorViz_Texture; +uniform half4 unity_EditorViz_Texture_ST; +uniform int unity_EditorViz_UVIndex; +uniform half4 unity_EditorViz_Decode_HDR; +uniform bool unity_EditorViz_ConvertToLinearSpace; +uniform half4 unity_EditorViz_ColorMul; +uniform half4 unity_EditorViz_ColorAdd; +uniform sampler2D unity_EditorViz_LightTexture; +uniform sampler2D unity_EditorViz_LightTextureB; +#define unity_EditorViz_ChannelSelect unity_EditorViz_ColorMul +#define unity_EditorViz_Color unity_EditorViz_ColorAdd +#define unity_EditorViz_LightType unity_EditorViz_UVIndex +uniform float4x4 unity_EditorViz_WorldToLight; + uniform half4 unity_MaterialValidateLowColor = half4(1.0f, 0.0f, 0.0f, 0.0f); uniform half4 unity_MaterialValidateHighColor = half4(0.0f, 0.0f, 1.0f, 0.0f); uniform half4 unity_MaterialValidatePureMetalColor = half4(1.0f, 1.0f, 0.0f, 0.0f); @@ -109,8 +132,6 @@ half3 UnityMeta_RGBToHSV(half3 rgbColor) return UnityMeta_RGBToHSVHelper(0.0, rgbColor.r, rgbColor.g, rgbColor.b); } - - // Pass 0 - Albedo half4 UnityMeta_pbrAlbedo(UnityMetaInput IN) { @@ -224,10 +245,21 @@ half4 UnityMeta_pbrMetalspec(UnityMetaInput IN) return outColor; } -#endif +#endif // EDITOR_VISUALIZATION -float4 UnityMetaVertexPosition (float4 vertex, float2 uv1, float2 uv2, float4 lightmapST, float4 dynlightmapST) +float2 UnityMetaVizUV(int uvIndex, float2 uv0, float2 uv1, float2 uv2, float4 st) { + if (uvIndex == 0) + return uv0 * st.xy + st.zw; + else if (uvIndex == 1) + return uv1 * st.xy + st.zw; + else + return uv2 * st.xy + st.zw; +} + +float4 UnityMetaVertexPosition(float4 vertex, float2 uv1, float2 uv2, float4 lightmapST, float4 dynlightmapST) +{ +#if !defined(EDITOR_VISUALIZATION) if (unity_MetaVertexControl.x) { vertex.xy = uv1 * lightmapST.xy + lightmapST.zw; @@ -243,6 +275,9 @@ float4 UnityMetaVertexPosition (float4 vertex, float2 uv1, float2 uv2, float4 li vertex.z = vertex.z > 0 ? 1.0e-4f : 0.0f; } return mul(UNITY_MATRIX_VP, float4(vertex.xyz, 1.0)); +#else + return UnityObjectToClipPos(vertex); +#endif } float unity_OneOverOutputBoost; @@ -252,7 +287,7 @@ float unity_UseLinearSpace; half4 UnityMetaFragment (UnityMetaInput IN) { half4 res = 0; - #if !defined(EDITOR_VISUALIZATION) +#if !defined(EDITOR_VISUALIZATION) if (unity_MetaFragmentControl.x) { res = half4(IN.Albedo,1); @@ -273,16 +308,56 @@ half4 UnityMetaFragment (UnityMetaInput IN) res = half4(emission, 1.0); } - #else - if ( unity_VisualizationMode == PBR_VALIDATION_ALBEDO ) +#else + if ( unity_VisualizationMode == EDITORVIZ_PBR_VALIDATION_ALBEDO) { res = UnityMeta_pbrAlbedo(IN); } - else if (unity_VisualizationMode == PBR_VALIDATION_METALSPECULAR) + else if (unity_VisualizationMode == EDITORVIZ_PBR_VALIDATION_METALSPECULAR) { res = UnityMeta_pbrMetalspec(IN); } - #endif + else if (unity_VisualizationMode == EDITORVIZ_TEXTURE) + { + res = tex2D(unity_EditorViz_Texture, IN.VizUV); + + if (unity_EditorViz_Decode_HDR.x > 0) + res = half4(DecodeHDR(res, unity_EditorViz_Decode_HDR), 1); + + if (unity_EditorViz_ConvertToLinearSpace) + res.rgb = LinearToGammaSpace(res.rgb); + + res *= unity_EditorViz_ColorMul; + res += unity_EditorViz_ColorAdd; + } + else if (unity_VisualizationMode == EDITORVIZ_SHOWLIGHTMASK) + { + float result = dot(unity_EditorViz_ChannelSelect, tex2D(unity_EditorViz_Texture, IN.VizUV).rgba); + if (result == 0) + discard; + + float atten = 1; + if (unity_EditorViz_LightType == 0) + { + // directional: no attenuation + } + else if (unity_EditorViz_LightType == 1) + { + // point + atten = tex2D(unity_EditorViz_LightTexture, dot(IN.LightCoord.xyz, IN.LightCoord.xyz).xx).r; + } + else if (unity_EditorViz_LightType == 2) + { + // spot + atten = tex2D(unity_EditorViz_LightTexture, dot(IN.LightCoord.xyz, IN.LightCoord.xyz).xx).r; + float cookie = tex2D(unity_EditorViz_LightTextureB, IN.LightCoord.xy / IN.LightCoord.w + 0.5).w; + atten *= (IN.LightCoord.z > 0) * cookie; + } + clip(atten - 0.001f); + + res = float4(unity_EditorViz_Color.xyz * result, unity_EditorViz_Color.w); + } +#endif // EDITOR_VISUALIZATION return res; } diff --git a/CGIncludes/UnityShaderVariables.cginc b/CGIncludes/UnityShaderVariables.cginc index d80c988..7eb0d7e 100644 --- a/CGIncludes/UnityShaderVariables.cginc +++ b/CGIncludes/UnityShaderVariables.cginc @@ -168,6 +168,7 @@ CBUFFER_START(UnityPerDraw) float4x4 unity_WorldToObject; float4 unity_LODFade; // x is the fade value ranging within [0,1]. y is x quantized into 16 levels float4 unity_WorldTransformParams; // w is usually 1.0, or -1.0 for odd-negative scale transforms + float4 unity_RenderingLayer; CBUFFER_END #if defined(USING_STEREO_MATRICES) diff --git a/CGIncludes/UnityStandardBRDF.cginc b/CGIncludes/UnityStandardBRDF.cginc index 62a0abd..2fb3d58 100644 --- a/CGIncludes/UnityStandardBRDF.cginc +++ b/CGIncludes/UnityStandardBRDF.cginc @@ -127,7 +127,7 @@ inline half SmithBeckmannVisibilityTerm (half NdotL, half NdotV, half roughness) } // Ref: http://jcgt.org/published/0003/02/03/paper.pdf -inline half SmithJointGGXVisibilityTerm (half NdotL, half NdotV, half roughness) +inline float SmithJointGGXVisibilityTerm (float NdotL, float NdotV, float roughness) { #if 0 // Original formulation: @@ -147,12 +147,17 @@ inline half SmithJointGGXVisibilityTerm (half NdotL, half NdotV, half roughness) // therefore epsilon is smaller than can be represented by half #else // Approximation of the above formulation (simplify the sqrt, not mathematically correct but close enough) - half a = roughness; - half lambdaV = NdotL * (NdotV * (1 - a) + a); - half lambdaL = NdotV * (NdotL * (1 - a) + a); + float a = roughness; + float lambdaV = NdotL * (NdotV * (1 - a) + a); + float lambdaL = NdotV * (NdotL * (1 - a) + a); +#if defined(SHADER_API_SWITCH) + return 0.5f / (lambdaV + lambdaL + 1e-4f); // work-around against hlslcc rounding error +#else return 0.5f / (lambdaV + lambdaL + 1e-5f); #endif + +#endif } inline float GGXTerm (float NdotH, float roughness) @@ -249,12 +254,12 @@ half4 BRDF1_Unity_PBS (half3 diffColor, half3 specColor, half oneMinusReflectivi // A re-normalization should be applied here but as the shift is small we don't do it to save ALU. //normal = normalize(normal); - half nv = saturate(dot(normal, viewDir)); // TODO: this saturate should no be necessary here + float nv = saturate(dot(normal, viewDir)); // TODO: this saturate should no be necessary here #else half nv = abs(dot(normal, viewDir)); // This abs allow to limit artifact #endif - half nl = saturate(dot(normal, light.dir)); + float nl = saturate(dot(normal, light.dir)); float nh = saturate(dot(normal, halfDir)); half lv = saturate(dot(light.dir, viewDir)); @@ -271,7 +276,7 @@ half4 BRDF1_Unity_PBS (half3 diffColor, half3 specColor, half oneMinusReflectivi #if UNITY_BRDF_GGX // GGX with roughtness to 0 would mean no specular at all, using max(roughness, 0.002) here to match HDrenderloop roughtness remapping. roughness = max(roughness, 0.002); - half V = SmithJointGGXVisibilityTerm (nl, nv, roughness); + float V = SmithJointGGXVisibilityTerm (nl, nv, roughness); float D = GGXTerm (nh, roughness); #else // Legacy @@ -279,7 +284,7 @@ half4 BRDF1_Unity_PBS (half3 diffColor, half3 specColor, half oneMinusReflectivi half D = NDFBlinnPhongNormalizedTerm (nh, PerceptualRoughnessToSpecPower(perceptualRoughness)); #endif - half specularTerm = V*D * UNITY_PI; // Torrance-Sparrow model, Fresnel is applied later + float specularTerm = V*D * UNITY_PI; // Torrance-Sparrow model, Fresnel is applied later # ifdef UNITY_COLORSPACE_GAMMA specularTerm = sqrt(max(1e-4h, specularTerm)); diff --git a/CGIncludes/UnityStandardConfig.cginc b/CGIncludes/UnityStandardConfig.cginc index 5baeab9..dd41b89 100644 --- a/CGIncludes/UnityStandardConfig.cginc +++ b/CGIncludes/UnityStandardConfig.cginc @@ -41,7 +41,7 @@ #undef _PARALLAXMAP #endif #endif -#if (SHADER_TARGET < 30) || defined(SHADER_API_GLES) || defined (SHADER_API_PSP2) +#if (SHADER_TARGET < 30) || defined(SHADER_API_GLES) #undef UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS #endif diff --git a/CGIncludes/UnityStandardMeta.cginc b/CGIncludes/UnityStandardMeta.cginc index 5220934..14415bd 100644 --- a/CGIncludes/UnityStandardMeta.cginc +++ b/CGIncludes/UnityStandardMeta.cginc @@ -13,8 +13,12 @@ struct v2f_meta { - float4 uv : TEXCOORD0; float4 pos : SV_POSITION; + float4 uv : TEXCOORD0; +#ifdef EDITOR_VISUALIZATION + float2 vizUV : TEXCOORD1; + float4 lightCoord : TEXCOORD2; +#endif }; v2f_meta vert_meta (VertexInput v) @@ -22,6 +26,17 @@ v2f_meta vert_meta (VertexInput v) v2f_meta o; o.pos = UnityMetaVertexPosition(v.vertex, v.uv1.xy, v.uv2.xy, unity_LightmapST, unity_DynamicLightmapST); o.uv = TexCoords(v); +#ifdef EDITOR_VISUALIZATION + o.vizUV = 0; + o.lightCoord = 0; + if (unity_VisualizationMode == EDITORVIZ_TEXTURE) + o.vizUV = UnityMetaVizUV(unity_EditorViz_UVIndex, v.uv0.xy, v.uv1.xy, v.uv2.xy, unity_EditorViz_Texture_ST); + else if (unity_VisualizationMode == EDITORVIZ_SHOWLIGHTMASK) + { + o.vizUV = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw; + o.lightCoord = mul(unity_EditorViz_WorldToLight, mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1))); + } +#endif return o; } @@ -45,8 +60,10 @@ float4 frag_meta (v2f_meta i) : SV_Target UnityMetaInput o; UNITY_INITIALIZE_OUTPUT(UnityMetaInput, o); -#if defined(EDITOR_VISUALIZATION) +#ifdef EDITOR_VISUALIZATION o.Albedo = data.diffColor; + o.VizUV = i.vizUV; + o.LightCoord = i.lightCoord; #else o.Albedo = UnityLightmappingAlbedo (data.diffColor, data.specColor, data.smoothness); #endif diff --git a/CGIncludes/UnityStandardParticleEditor.cginc b/CGIncludes/UnityStandardParticleEditor.cginc new file mode 100644 index 0000000..c17accc --- /dev/null +++ b/CGIncludes/UnityStandardParticleEditor.cginc @@ -0,0 +1,101 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +#ifndef UNITY_STANDARD_PARTICLE_EDITOR_INCLUDED +#define UNITY_STANDARD_PARTICLE_EDITOR_INCLUDED + +#if _REQUIRE_UV2 +#define _FLIPBOOK_BLENDING 1 +#endif + +#include "UnityCG.cginc" +#include "UnityShaderVariables.cginc" +#include "UnityStandardConfig.cginc" +#include "UnityStandardUtils.cginc" +#include "UnityStandardParticleInstancing.cginc" + +#ifdef _ALPHATEST_ON +half _Cutoff; +#endif +sampler2D _MainTex; +float4 _MainTex_ST; + +float _ObjectId; +float _PassValue; +float4 _SelectionID; +uniform float _SelectionAlphaCutoff; + +struct VertexInput +{ + float4 vertex : POSITION; + float3 normal : NORMAL; + fixed4 color : COLOR; + #if defined(_FLIPBOOK_BLENDING) && !defined(UNITY_PARTICLE_INSTANCING_ENABLED) + float4 texcoords : TEXCOORD0; + float texcoordBlend : TEXCOORD1; + #else + float2 texcoords : TEXCOORD0; + #endif + UNITY_VERTEX_INPUT_INSTANCE_ID +}; + +struct VertexOutput +{ + float2 texcoord : TEXCOORD0; + #ifdef _FLIPBOOK_BLENDING + float3 texcoord2AndBlend : TEXCOORD1; + #endif + fixed4 color : TEXCOORD2; +}; + +void vertEditorPass(VertexInput v, out VertexOutput o, out float4 opos : SV_POSITION) +{ + UNITY_SETUP_INSTANCE_ID(v); + + opos = UnityObjectToClipPos(v.vertex); + + #ifdef _FLIPBOOK_BLENDING + #ifdef UNITY_PARTICLE_INSTANCING_ENABLED + vertInstancingUVs(v.texcoords.xy, o.texcoord, o.texcoord2AndBlend); + #else + o.texcoord = v.texcoords.xy; + o.texcoord2AndBlend.xy = v.texcoords.zw; + o.texcoord2AndBlend.z = v.texcoordBlend; + #endif + #else + #ifdef UNITY_PARTICLE_INSTANCING_ENABLED + vertInstancingUVs(v.texcoords.xy, o.texcoord); + o.texcoord = TRANSFORM_TEX(o.texcoord, _MainTex); + #else + o.texcoord = TRANSFORM_TEX(v.texcoords.xy, _MainTex); + #endif + #endif + o.color = v.color; +} + +void fragSceneClip(VertexOutput i) +{ + half alpha = tex2D(_MainTex, i.texcoord).a; +#ifdef _FLIPBOOK_BLENDING + half alpha2 = tex2D(_MainTex, i.texcoord2AndBlend.xy); + alpha = lerp(alpha, alpha2, i.texcoord2AndBlend.z); +#endif + alpha *= i.color.a; + +#ifdef _ALPHATEST_ON + clip(alpha - _Cutoff); +#endif +} + +half4 fragSceneHighlightPass(VertexOutput i) : SV_Target +{ + fragSceneClip(i); + return float4(_ObjectId, _PassValue, 1, 1); +} + +half4 fragScenePickingPass(VertexOutput i) : SV_Target +{ + fragSceneClip(i); + return _SelectionID; +} + +#endif // UNITY_STANDARD_PARTICLE_EDITOR_INCLUDED diff --git a/CGIncludes/UnityStandardParticleShadow.cginc b/CGIncludes/UnityStandardParticleShadow.cginc index 6aab3ed..69f64cc 100644 --- a/CGIncludes/UnityStandardParticleShadow.cginc +++ b/CGIncludes/UnityStandardParticleShadow.cginc @@ -6,16 +6,16 @@ // NOTE: had to split shadow functions into separate file, // otherwise compiler gives trouble with LIGHTING_COORDS macro (in UnityStandardCore.cginc) +#if _REQUIRE_UV2 +#define _FLIPBOOK_BLENDING 1 +#endif + #include "UnityCG.cginc" #include "UnityShaderVariables.cginc" #include "UnityStandardConfig.cginc" #include "UnityStandardUtils.cginc" #include "UnityStandardParticleInstancing.cginc" -#if _REQUIRE_UV2 - #define _FLIPBOOK_BLENDING 1 -#endif - #if (defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_ON)) && defined(UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS) #define UNITY_STANDARD_USE_DITHER_MASK 1 #endif @@ -122,7 +122,12 @@ void vertParticleShadowCaster (VertexInput v, o.texcoord2AndBlend.z = v.texcoordBlend; #endif #else - o.texcoord = TRANSFORM_TEX(v.texcoords.xy, _MainTex); + #ifdef UNITY_PARTICLE_INSTANCING_ENABLED + vertInstancingUVs(v.texcoords.xy, o.texcoord); + o.texcoord = TRANSFORM_TEX(o.texcoord, _MainTex); + #else + o.texcoord = TRANSFORM_TEX(v.texcoords.xy, _MainTex); + #endif #endif o.color = v.color; #endif @@ -140,7 +145,7 @@ half4 fragParticleShadowCaster ( #ifdef UNITY_STANDARD_USE_SHADOW_UVS half alpha = tex2D(_MainTex, i.texcoord).a; #ifdef _FLIPBOOK_BLENDING - half alpha2 = tex2D(_MainTex, i.texcoord2AndBlend.xy); + half alpha2 = tex2D(_MainTex, i.texcoord2AndBlend.xy).a; alpha = lerp(alpha, alpha2, i.texcoord2AndBlend.z); #endif alpha *= i.color.a; diff --git a/DefaultResources/Internal-BlendShape.compute b/DefaultResources/Internal-BlendShape.compute new file mode 100644 index 0000000..f04caed --- /dev/null +++ b/DefaultResources/Internal-BlendShape.compute @@ -0,0 +1,43 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +#pragma kernel main +#pragma kernel main SKIN_NORM +#pragma kernel main SKIN_NORM SKIN_TANG + +#include "HLSLSupport.cginc" +#include "Internal-Skinning-Util.cginc" + +struct BlendShapeVertex +{ + int index; + float3 pos; + float3 norm; + float3 tang; +}; + +uint g_FirstVert; // First vertex from blend shape buffer to use +uint g_VertCount; // Sparse vertex count, not the full amount of vertices in mesh +float g_Weight; + +[numthreads(64, 1, 1)] +void main(uint3 threadID : SV_DispatchThreadID, SAMPLER_UNIFORM RWStructuredBuffer inOutMeshVertices, SAMPLER_UNIFORM StructuredBuffer inBlendShapeVertices) +{ + const uint t = threadID.x; + + if (t >= g_VertCount) + { + return; + } + + BlendShapeVertex blendShapeVert = inBlendShapeVertices[t + g_FirstVert]; + + const uint vertIndex = blendShapeVert.index; + + inOutMeshVertices[vertIndex].pos += blendShapeVert.pos * g_Weight; +#if SKIN_NORM + inOutMeshVertices[vertIndex].norm += blendShapeVert.norm * g_Weight; +#endif +#if SKIN_TANG + inOutMeshVertices[vertIndex].tang.xyz += blendShapeVert.tang * g_Weight; +#endif +} diff --git a/DefaultResources/Internal-Skinning-Util.cginc b/DefaultResources/Internal-Skinning-Util.cginc new file mode 100644 index 0000000..2f10947 --- /dev/null +++ b/DefaultResources/Internal-Skinning-Util.cginc @@ -0,0 +1,19 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + + +#if defined(SHADER_API_D3D11) || defined(SHADER_API_XBOXONE) || defined(SHADER_API_PS4) || defined(SHADER_API_GLCORE) || defined(SHADER_API_VULKAN) || defined(SHADER_API_METAL) || defined(SHADER_API_PSSL) || defined(SHADER_API_SWITCH) +#define STRUCTURED_BUFFER_SUPPORT 1 +#else +#define STRUCTURED_BUFFER_SUPPORT 0 +#endif + +struct MeshVertex +{ + float3 pos; +#if SKIN_NORM + float3 norm; +#endif +#if SKIN_TANG + float4 tang; +#endif +}; diff --git a/DefaultResources/internalskinning.compute b/DefaultResources/Internal-Skinning.compute similarity index 97% rename from DefaultResources/internalskinning.compute rename to DefaultResources/Internal-Skinning.compute index 59e7f72..c2d7c7a 100644 --- a/DefaultResources/internalskinning.compute +++ b/DefaultResources/Internal-Skinning.compute @@ -19,7 +19,7 @@ uint g_VertCount; -#if defined(SHADER_API_D3D11) || defined(SHADER_API_XBOXONE) || defined(SHADER_API_PS4) || defined(SHADER_API_GLCORE) || defined(SHADER_API_VULKAN) || defined(SHADER_API_PSSL) +#if defined(SHADER_API_D3D11) || defined(SHADER_API_XBOXONE) || defined(SHADER_API_PS4) || defined(SHADER_API_GLCORE) || defined(SHADER_API_GLES3) || defined(SHADER_API_VULKAN) || defined(SHADER_API_METAL) || defined(SHADER_API_PSSL) || defined(SHADER_API_SWITCH) #define STRUCTURED_BUFFER_SUPPORT 1 #else #define STRUCTURED_BUFFER_SUPPORT 0 @@ -191,7 +191,7 @@ void main(uint3 threadID : SV_DispatchThreadID, SAMPLER_UNIFORM StructuredBuffer #if SKIN_NORM #if SKIN_BONESFORVERT==1 - vNacc += mul( float4(vN,0), g_mBones[si.index0] ).xyz; + vNacc += mul( g_mBones[si.index0],float4(vN,0) ).xyz; #elif SKIN_BONESFORVERT==2 vNacc += si.weight0*mul( g_mBones[si.index0],float4(vN,0) ).xyz; vNacc += si.weight1*mul( g_mBones[si.index1],float4(vN,0) ).xyz; diff --git a/DefaultResourcesExtra/StandardRoughness.shader b/DefaultResourcesExtra/AutodeskInteractive.shader similarity index 99% rename from DefaultResourcesExtra/StandardRoughness.shader rename to DefaultResourcesExtra/AutodeskInteractive.shader index 5828059..7d5fee5 100644 --- a/DefaultResourcesExtra/StandardRoughness.shader +++ b/DefaultResourcesExtra/AutodeskInteractive.shader @@ -1,6 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) -Shader "Standard (Roughness setup)" +Shader "Autodesk Interactive" { Properties { @@ -327,5 +327,5 @@ Shader "Standard (Roughness setup)" FallBack "VertexLit" - CustomEditor "StandardRoughnessShaderGUI" + CustomEditor "AutodeskInteractiveShaderGUI" } diff --git a/DefaultResourcesExtra/GIDebug/ShowLightMask.shader b/DefaultResourcesExtra/GIDebug/ShowLightMask.shader index 76e4c6c..ae0560a 100644 --- a/DefaultResourcesExtra/GIDebug/ShowLightMask.shader +++ b/DefaultResourcesExtra/GIDebug/ShowLightMask.shader @@ -32,9 +32,6 @@ Shader "Hidden/GIDebug/ShowLightMask" { sampler2D _LightTexture; sampler2D _LightTextureB; - float4 _Decode_HDR; - float _ConvertToLinearSpace; - float _StaticUV1; float4 _Color; float4 _ChannelSelect; float4x4 _WorldToLight; @@ -50,14 +47,14 @@ Shader "Hidden/GIDebug/ShowLightMask" { return o; } - fixed UnitySpotCookie(float4 LightCoord) + fixed UnitySpotCookie(float4 lightCoord) { - return tex2D(_LightTexture, LightCoord.xy / LightCoord.w + 0.5).w; + return tex2D(_LightTextureB, lightCoord.xy / lightCoord.w + 0.5).w; } - fixed UnitySpotAttenuate(float3 LightCoord) + fixed UnityDefaultAttenuate(float3 lightCoord) { - return tex2D(_LightTextureB, dot(LightCoord, LightCoord).xx).r; + return tex2D(_LightTexture, dot(lightCoord, lightCoord).xx).r; } float4 frag_surf (v2f_surf IN) : COLOR @@ -78,12 +75,12 @@ Shader "Hidden/GIDebug/ShowLightMask" { else if (_LightType == 1) { // point - atten = tex2D(_LightTexture, dot(lightCoord.xyz, lightCoord.xyz).xx).r; + atten = UnityDefaultAttenuate(lightCoord.xyz); } else if (_LightType == 2) { // spot - atten = (lightCoord.z > 0) * UnitySpotCookie(lightCoord) * UnitySpotAttenuate(lightCoord.xyz); + atten = (lightCoord.z > 0) * UnitySpotCookie(lightCoord) * UnityDefaultAttenuate(lightCoord.xyz); } clip(atten - 0.001f); diff --git a/DefaultResourcesExtra/GIDebug/TextureUV.shader b/DefaultResourcesExtra/GIDebug/TextureUV.shader index 47b34ae..23e79a3 100644 --- a/DefaultResourcesExtra/GIDebug/TextureUV.shader +++ b/DefaultResourcesExtra/GIDebug/TextureUV.shader @@ -25,6 +25,7 @@ Shader "Hidden/GIDebug/TextureUV" { half4 _Decode_HDR; float _ConvertToLinearSpace; float _StaticUV1; + float _Exposure; v2f_surf vert_surf (appdata_full v) { @@ -52,7 +53,7 @@ Shader "Hidden/GIDebug/TextureUV" { if (_ConvertToLinearSpace) result = LinearToGammaSpace (result); - return float4 (result, 1); + return float4 (result * exp2(_Exposure), 1); } ENDCG } diff --git a/DefaultResourcesExtra/Illumin-VertexLit.shader b/DefaultResourcesExtra/Illumin-VertexLit.shader index 62b3bcc..f3d401a 100644 --- a/DefaultResourcesExtra/Illumin-VertexLit.shader +++ b/DefaultResourcesExtra/Illumin-VertexLit.shader @@ -52,6 +52,10 @@ SubShader { float4 pos : SV_POSITION; float2 uvMain : TEXCOORD0; float2 uvIllum : TEXCOORD1; + #ifdef EDITOR_VISUALIZATION + float2 vizUV : TEXCOORD2; + float4 lightCoord : TEXCOORD3; + #endif UNITY_VERTEX_OUTPUT_STEREO }; @@ -66,6 +70,17 @@ SubShader { o.pos = UnityMetaVertexPosition(v.vertex, v.texcoord1.xy, v.texcoord2.xy, unity_LightmapST, unity_DynamicLightmapST); o.uvMain = TRANSFORM_TEX(v.texcoord, _MainTex); o.uvIllum = TRANSFORM_TEX(v.texcoord, _Illum); + #ifdef EDITOR_VISUALIZATION + o.vizUV = 0; + o.lightCoord = 0; + if (unity_VisualizationMode == EDITORVIZ_TEXTURE) + o.vizUV = UnityMetaVizUV(unity_EditorViz_UVIndex, v.texcoord.xy, v.texcoord1.xy, v.texcoord2.xy, unity_EditorViz_Texture_ST); + else if (unity_VisualizationMode == EDITORVIZ_SHOWLIGHTMASK) + { + o.vizUV = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw; + o.lightCoord = mul(unity_EditorViz_WorldToLight, mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1))); + } + #endif return o; } @@ -83,6 +98,10 @@ SubShader { fixed4 c = tex * _Color; metaIN.Albedo = c.rgb; metaIN.Emission = c.rgb * tex2D(_Illum, i.uvIllum).a; + #if defined(EDITOR_VISUALIZATION) + metaIN.VizUV = i.vizUV; + metaIN.LightCoord = i.lightCoord; + #endif return UnityMetaFragment(metaIN); } diff --git a/DefaultResourcesExtra/Mobile/Mobile-BumpSpec-1DirectionalLight.shader b/DefaultResourcesExtra/Mobile/Mobile-BumpSpec-1DirectionalLight.shader index e6f6bef..5b00262 100644 --- a/DefaultResourcesExtra/Mobile/Mobile-BumpSpec-1DirectionalLight.shader +++ b/DefaultResourcesExtra/Mobile/Mobile-BumpSpec-1DirectionalLight.shader @@ -9,7 +9,7 @@ // - no Lightmap support // - supports ONLY 1 directional light. Other lights are completely ignored. -Shader "Mobile/Bumped Specular (1 Directional Light)" { +Shader "Mobile/Bumped Specular (1 Directional Realtime Light)" { Properties { [PowerSlider(5.0)] _Shininess ("Shininess", Range (0.03, 1)) = 0.078125 _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {} diff --git a/DefaultResourcesExtra/Nature/SpeedTree8.shader b/DefaultResourcesExtra/Nature/SpeedTree8.shader new file mode 100644 index 0000000..730a3e2 --- /dev/null +++ b/DefaultResourcesExtra/Nature/SpeedTree8.shader @@ -0,0 +1,90 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +Shader "Nature/SpeedTree8" +{ + Properties + { + _MainTex ("Base (RGB) Transparency (A)", 2D) = "white" {} + _Color ("Color", Color) = (1,1,1,1) + + [Toggle(EFFECT_HUE_VARIATION)] _HueVariationKwToggle("Hue Variation", Float) = 0 + _HueVariationColor ("Hue Variation Color", Color) = (1.0,0.5,0.0,0.1) + + [Toggle(EFFECT_BUMP)] _NormalMapKwToggle("Normal Mapping", Float) = 0 + _BumpMap ("Normalmap", 2D) = "bump" {} + + _ExtraTex ("Smoothness (R), Metallic (G), AO (B)", 2D) = "(0.5, 0.0, 1.0)" {} + _Glossiness ("Smoothness", Range(0.0, 1.0)) = 0.5 + _Metallic ("Metallic", Range(0.0, 1.0)) = 0.0 + + [Toggle(EFFECT_SUBSURFACE)] _SubsurfaceKwToggle("Subsurface", Float) = 0 + _SubsurfaceTex ("Subsurface (RGB)", 2D) = "white" {} + _SubsurfaceColor ("Subsurface Color", Color) = (1,1,1,1) + _SubsurfaceIndirect ("Subsurface Indirect", Range(0.0, 1.0)) = 0.25 + + [Toggle(EFFECT_BILLBOARD)] _BillboardKwToggle("Billboard", Float) = 0 + _BillboardShadowFade ("Billboard Shadow Fade", Range(0.0, 1.0)) = 0.5 + + [Enum(No,2,Yes,0)] _TwoSided ("Two Sided", Int) = 2 // enum matches cull mode + [KeywordEnum(None,Fastest,Fast,Better,Best,Palm)] _WindQuality ("Wind Quality", Range(0,5)) = 0 + } + + SubShader + { + Tags + { + "Queue"="AlphaTest" + "IgnoreProjector"="True" + "RenderType"="TransparentCutout" + "DisableBatching"="LODFading" + } + LOD 400 + Cull [_TwoSided] + + CGPROGRAM + #pragma surface SpeedTreeSurf SpeedTreeSubsurface vertex:SpeedTreeVert dithercrossfade addshadow + #pragma target 3.0 + #pragma multi_compile_vertex LOD_FADE_PERCENTAGE + #pragma instancing_options assumeuniformscaling maxcount:50 + + #pragma shader_feature _WINDQUALITY_NONE _WINDQUALITY_FASTEST _WINDQUALITY_FAST _WINDQUALITY_BETTER _WINDQUALITY_BEST _WINDQUALITY_PALM + #pragma shader_feature EFFECT_BILLBOARD + #pragma shader_feature EFFECT_HUE_VARIATION + #pragma shader_feature EFFECT_SUBSURFACE + #pragma shader_feature EFFECT_BUMP + #pragma shader_feature EFFECT_EXTRA_TEX + + #define ENABLE_WIND + #define EFFECT_BACKSIDE_NORMALS + #include "SpeedTree8Common.cginc" + + ENDCG + } + + // targeting SM2.0: Many effects are disabled for fewer instructions + SubShader + { + Tags + { + "Queue"="AlphaTest" + "IgnoreProjector"="True" + "RenderType"="TransparentCutout" + "DisableBatching"="LODFading" + } + LOD 400 + Cull [_TwoSided] + + CGPROGRAM + #pragma surface SpeedTreeSurf Standard vertex:SpeedTreeVert addshadow noinstancing + #pragma multi_compile_vertex LOD_FADE_PERCENTAGE + #pragma shader_feature EFFECT_BILLBOARD + #pragma shader_feature EFFECT_EXTRA_TEX + + #include "SpeedTree8Common.cginc" + + ENDCG + } + + FallBack "Transparent/Cutout/VertexLit" + CustomEditor "SpeedTree8ShaderGUI" +} diff --git a/DefaultResourcesExtra/Particle Add.shader b/DefaultResourcesExtra/Particle Add.shader index 1c63762..68c77ed 100644 --- a/DefaultResourcesExtra/Particle Add.shader +++ b/DefaultResourcesExtra/Particle Add.shader @@ -1,6 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) -Shader "Particles/Additive" { +Shader "Legacy Shaders/Particles/Additive" { Properties { _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5) _MainTex ("Particle Texture", 2D) = "white" {} @@ -77,7 +77,7 @@ Category { #endif fixed4 col = 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord); - col.a = saturate(col.a); // alpha should not have double-brightness applied to it, but we can't fix that legacy behaior without breaking everyone's effects, so instead clamp the output to get sensible HDR behavior (case 967476) + col.a = saturate(col.a); // alpha should not have double-brightness applied to it, but we can't fix that legacy behavior without breaking everyone's effects, so instead clamp the output to get sensible HDR behavior (case 967476) UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(0,0,0,0)); // fog towards black due to our blend mode return col; diff --git a/DefaultResourcesExtra/Particle AddMultiply.shader b/DefaultResourcesExtra/Particle AddMultiply.shader index 5f2a76f..c39e18a 100644 --- a/DefaultResourcesExtra/Particle AddMultiply.shader +++ b/DefaultResourcesExtra/Particle AddMultiply.shader @@ -1,6 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) -Shader "Particles/~Additive-Multiply" { +Shader "Legacy Shaders/Particles/~Additive-Multiply" { Properties { _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5) _MainTex ("Particle Texture", 2D) = "white" {} diff --git a/DefaultResourcesExtra/Particle AddSmooth.shader b/DefaultResourcesExtra/Particle AddSmooth.shader index f1828dd..6d9af36 100644 --- a/DefaultResourcesExtra/Particle AddSmooth.shader +++ b/DefaultResourcesExtra/Particle AddSmooth.shader @@ -1,6 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) -Shader "Particles/Additive (Soft)" { +Shader "Legacy Shaders/Particles/Additive (Soft)" { Properties { _MainTex ("Particle Texture", 2D) = "white" {} _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0 diff --git a/DefaultResourcesExtra/Particle Alpha Blend.shader b/DefaultResourcesExtra/Particle Alpha Blend.shader index 65591ac..b6e27a9 100644 --- a/DefaultResourcesExtra/Particle Alpha Blend.shader +++ b/DefaultResourcesExtra/Particle Alpha Blend.shader @@ -1,6 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) -Shader "Particles/Alpha Blended" { +Shader "Legacy Shaders/Particles/Alpha Blended" { Properties { _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5) _MainTex ("Particle Texture", 2D) = "white" {} @@ -77,7 +77,7 @@ Category { #endif fixed4 col = 2.0f * i.color * tex2D(_MainTex, i.texcoord); - col.a = saturate(col.a); // alpha should not have double-brightness applied to it, but we can't fix that legacy behaior without breaking everyone's effects, so instead clamp the output to get sensible HDR behavior (case 967476) + col.a = saturate(col.a); // alpha should not have double-brightness applied to it, but we can't fix that legacy behavior without breaking everyone's effects, so instead clamp the output to get sensible HDR behavior (case 967476) UNITY_APPLY_FOG(i.fogCoord, col); return col; diff --git a/DefaultResourcesExtra/Particle Anim Alpha Blend.shader b/DefaultResourcesExtra/Particle Anim Alpha Blend.shader index 78bea56..33a60da 100644 --- a/DefaultResourcesExtra/Particle Anim Alpha Blend.shader +++ b/DefaultResourcesExtra/Particle Anim Alpha Blend.shader @@ -1,6 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) -Shader "Particles/Anim Alpha Blended" { +Shader "Legacy Shaders/Particles/Anim Alpha Blended" { Properties { _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5) _MainTex ("Particle Texture", 2D) = "white" {} @@ -84,7 +84,7 @@ Category { fixed4 colA = tex2D(_MainTex, i.texcoord); fixed4 colB = tex2D(_MainTex, i.texcoord2); fixed4 col = 2.0f * i.color * lerp(colA, colB, i.blend); - col.a = saturate(col.a); // alpha should not have double-brightness applied to it, but we can't fix that legacy behaior without breaking everyone's effects, so instead clamp the output to get sensible HDR behavior (case 967476) + col.a = saturate(col.a); // alpha should not have double-brightness applied to it, but we can't fix that legacy behavior without breaking everyone's effects, so instead clamp the output to get sensible HDR behavior (case 967476) UNITY_APPLY_FOG(i.fogCoord, col); return col; diff --git a/DefaultResourcesExtra/Particle Blend.shader b/DefaultResourcesExtra/Particle Blend.shader index fa890d5..71f4530 100644 --- a/DefaultResourcesExtra/Particle Blend.shader +++ b/DefaultResourcesExtra/Particle Blend.shader @@ -1,6 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) -Shader "Particles/Blend" { +Shader "Legacy Shaders/Particles/Blend" { Properties { _MainTex ("Particle Texture", 2D) = "white" {} _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0 diff --git a/DefaultResourcesExtra/Particle Multiply.shader b/DefaultResourcesExtra/Particle Multiply.shader index 71d0e5a..ff635f6 100644 --- a/DefaultResourcesExtra/Particle Multiply.shader +++ b/DefaultResourcesExtra/Particle Multiply.shader @@ -1,6 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) -Shader "Particles/Multiply" { +Shader "Legacy Shaders/Particles/Multiply" { Properties { _MainTex ("Particle Texture", 2D) = "white" {} _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0 diff --git a/DefaultResourcesExtra/Particle MultiplyDouble.shader b/DefaultResourcesExtra/Particle MultiplyDouble.shader index b95ed62..12af0ca 100644 --- a/DefaultResourcesExtra/Particle MultiplyDouble.shader +++ b/DefaultResourcesExtra/Particle MultiplyDouble.shader @@ -1,6 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) -Shader "Particles/Multiply (Double)" { +Shader "Legacy Shaders/Particles/Multiply (Double)" { Properties { _MainTex ("Particle Texture", 2D) = "white" {} _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0 diff --git a/DefaultResourcesExtra/Particle Premultiply Blend.shader b/DefaultResourcesExtra/Particle Premultiply Blend.shader index c16bbb7..e48cc77 100644 --- a/DefaultResourcesExtra/Particle Premultiply Blend.shader +++ b/DefaultResourcesExtra/Particle Premultiply Blend.shader @@ -1,6 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) -Shader "Particles/Alpha Blended Premultiply" { +Shader "Legacy Shaders/Particles/Alpha Blended Premultiply" { Properties { _MainTex ("Particle Texture", 2D) = "white" {} _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0 diff --git a/DefaultResourcesExtra/Particle Standard Surface.shader b/DefaultResourcesExtra/Particle Standard Surface.shader index 89f3908..ceb52fd 100644 --- a/DefaultResourcesExtra/Particle Standard Surface.shader +++ b/DefaultResourcesExtra/Particle Standard Surface.shader @@ -71,7 +71,7 @@ Shader "Particles/Standard Surface" Cull Off CGPROGRAM - #pragma target 3.5 + #pragma target 3.0 #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON _ALPHAMODULATE_ON #pragma shader_feature _METALLICGLOSSMAP @@ -87,12 +87,62 @@ Shader "Particles/Standard Surface" ENDCG } + Pass + { + Name "SceneSelectionPass" + Tags { "LightMode" = "SceneSelectionPass" } + + BlendOp Add + Blend One Zero + ZWrite On + Cull Off + + CGPROGRAM + #pragma target 3.0 + + #pragma shader_feature _ _ALPHATEST_ON + #pragma shader_feature _REQUIRE_UV2 + #pragma multi_compile_instancing + #pragma instancing_options procedural:vertInstancingSetup + + #pragma vertex vertEditorPass + #pragma fragment fragSceneHighlightPass + + #include "UnityStandardParticleEditor.cginc" + ENDCG + } + + Pass + { + Name "ScenePickingPass" + Tags{ "LightMode" = "Picking" } + + BlendOp Add + Blend One Zero + ZWrite On + Cull Off + + CGPROGRAM + #pragma target 3.0 + + #pragma shader_feature _ _ALPHATEST_ON + #pragma shader_feature _REQUIRE_UV2 + #pragma multi_compile_instancing + #pragma instancing_options procedural:vertInstancingSetup + + #pragma vertex vertEditorPass + #pragma fragment fragScenePickingPass + + #include "UnityStandardParticleEditor.cginc" + ENDCG + } + CGPROGRAM #pragma surface surf Standard nolightmap nometa noforwardadd keepalpha vertex:vert #pragma multi_compile __ SOFTPARTICLES_ON #pragma multi_compile_instancing #pragma instancing_options procedural:vertInstancingSetup - #pragma target 3.5 + #pragma target 3.0 #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON _ALPHAMODULATE_ON #pragma shader_feature _METALLICGLOSSMAP diff --git a/DefaultResourcesExtra/Particle Standard Unlit.shader b/DefaultResourcesExtra/Particle Standard Unlit.shader index 8a643db..be832a9 100644 --- a/DefaultResourcesExtra/Particle Standard Unlit.shader +++ b/DefaultResourcesExtra/Particle Standard Unlit.shader @@ -88,6 +88,56 @@ Shader "Particles/Standard Unlit" ENDCG } + Pass + { + Name "SceneSelectionPass" + Tags { "LightMode" = "SceneSelectionPass" } + + BlendOp Add + Blend One Zero + ZWrite On + Cull Off + + CGPROGRAM + #pragma target 2.5 + + #pragma shader_feature _ _ALPHATEST_ON + #pragma shader_feature _REQUIRE_UV2 + #pragma multi_compile_instancing + #pragma instancing_options procedural:vertInstancingSetup + + #pragma vertex vertEditorPass + #pragma fragment fragSceneHighlightPass + + #include "UnityStandardParticleEditor.cginc" + ENDCG + } + + Pass + { + Name "ScenePickingPass" + Tags{ "LightMode" = "Picking" } + + BlendOp Add + Blend One Zero + ZWrite On + Cull Off + + CGPROGRAM + #pragma target 2.5 + + #pragma shader_feature _ _ALPHATEST_ON + #pragma shader_feature _REQUIRE_UV2 + #pragma multi_compile_instancing + #pragma instancing_options procedural:vertInstancingSetup + + #pragma vertex vertEditorPass + #pragma fragment fragScenePickingPass + + #include "UnityStandardParticleEditor.cginc" + ENDCG + } + Pass { Tags { "LightMode"="ForwardBase" } diff --git a/DefaultResourcesExtra/Particle VertexLit Blended.shader b/DefaultResourcesExtra/Particle VertexLit Blended.shader index 8fa0257..3166592 100644 --- a/DefaultResourcesExtra/Particle VertexLit Blended.shader +++ b/DefaultResourcesExtra/Particle VertexLit Blended.shader @@ -1,6 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) -Shader "Particles/VertexLit Blended" { +Shader "Legacy Shaders/Particles/VertexLit Blended" { Properties { _EmisColor ("Emissive Color", Color) = (.2,.2,.2,0) _MainTex ("Particle Texture", 2D) = "white" {} diff --git a/DefaultResourcesExtra/Skybox-Procedural.shader b/DefaultResourcesExtra/Skybox-Procedural.shader index fd23985..ec13fe7 100644 --- a/DefaultResourcesExtra/Skybox-Procedural.shader +++ b/DefaultResourcesExtra/Skybox-Procedural.shader @@ -166,13 +166,7 @@ SubShader { float scale(float inCos) { float x = 1.0 - inCos; - #if defined(SHADER_API_N3DS) - // The polynomial expansion here generates too many swizzle instructions for the 3DS vertex assembler - // Approximate by removing x^1 and x^2 - return 0.25 * exp(-0.00287 + x*x*x*(-6.80 + x*5.25)); - #else return 0.25 * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25)))); - #endif } v2f vert (appdata_t v) diff --git a/DefaultResourcesExtra/TerrainShaders/Details/WavingGrass.shader b/DefaultResourcesExtra/TerrainShaders/Details/WavingGrass.shader index eebc8ae..925fa9d 100644 --- a/DefaultResourcesExtra/TerrainShaders/Details/WavingGrass.shader +++ b/DefaultResourcesExtra/TerrainShaders/Details/WavingGrass.shader @@ -20,7 +20,7 @@ SubShader { ColorMask RGB CGPROGRAM -#pragma surface surf Lambert vertex:WavingGrassVert addshadow exclude_path:deferred +#pragma surface surf Lambert vertex:WavingGrassVert addshadow fullforwardshadows exclude_path:deferred #include "TerrainEngine.cginc" sampler2D _MainTex; diff --git a/DefaultResourcesExtra/TerrainShaders/Details/WavingGrassBillboard.shader b/DefaultResourcesExtra/TerrainShaders/Details/WavingGrassBillboard.shader index 899b99f..88a8e47 100644 --- a/DefaultResourcesExtra/TerrainShaders/Details/WavingGrassBillboard.shader +++ b/DefaultResourcesExtra/TerrainShaders/Details/WavingGrassBillboard.shader @@ -45,7 +45,7 @@ ENDCG ColorMask RGB CGPROGRAM -#pragma surface surf Lambert vertex:WavingGrassBillboardVert addshadow exclude_path:deferred +#pragma surface surf Lambert vertex:WavingGrassBillboardVert addshadow fullforwardshadows exclude_path:deferred sampler2D _MainTex; fixed _Cutoff; diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/AddPass.shader b/DefaultResourcesExtra/TerrainShaders/Splats/AddPass.shader index 4a35132..666d8e2 100644 --- a/DefaultResourcesExtra/TerrainShaders/Splats/AddPass.shader +++ b/DefaultResourcesExtra/TerrainShaders/Splats/AddPass.shader @@ -1,21 +1,11 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) Shader "Hidden/TerrainEngine/Splatmap/Diffuse-AddPass" { - Properties { - [HideInInspector] _Control ("Control (RGBA)", 2D) = "black" {} - [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {} - [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {} - [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {} - [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {} - [HideInInspector] _Normal3 ("Normal 3 (A)", 2D) = "bump" {} - [HideInInspector] _Normal2 ("Normal 2 (B)", 2D) = "bump" {} - [HideInInspector] _Normal1 ("Normal 1 (G)", 2D) = "bump" {} - [HideInInspector] _Normal0 ("Normal 0 (R)", 2D) = "bump" {} - } - CGINCLUDE - #pragma surface surf Lambert decal:add vertex:SplatmapVert finalcolor:SplatmapFinalColor finalprepass:SplatmapFinalPrepass finalgbuffer:SplatmapFinalGBuffer noinstancing + #pragma surface surf Lambert decal:add vertex:SplatmapVert finalcolor:SplatmapFinalColor finalprepass:SplatmapFinalPrepass finalgbuffer:SplatmapFinalGBuffer fullforwardshadows nometa + #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap forwardadd #pragma multi_compile_fog + #define TERRAIN_SPLAT_ADDPASS #include "TerrainSplatmapCommon.cginc" @@ -36,12 +26,12 @@ Shader "Hidden/TerrainEngine/Splatmap/Diffuse-AddPass" { "IgnoreProjector"="True" "RenderType" = "Opaque" } - // TODO: Seems like "#pragma target 3.0 _TERRAIN_NORMAL_MAP" can't fallback correctly on less capable devices? + // TODO: Seems like "#pragma target 3.0 _NORMALMAP" can't fallback correctly on less capable devices? // Use two sub-shaders to simulate different features for different targets and still fallback correctly. SubShader { // for sm3.0+ targets CGPROGRAM #pragma target 3.0 - #pragma multi_compile __ _TERRAIN_NORMAL_MAP + #pragma multi_compile __ _NORMALMAP ENDCG } SubShader { // for sm2.0 targets diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/DiffuseBase.shader b/DefaultResourcesExtra/TerrainShaders/Splats/DiffuseBase.shader new file mode 100644 index 0000000..1999000 --- /dev/null +++ b/DefaultResourcesExtra/TerrainShaders/Splats/DiffuseBase.shader @@ -0,0 +1,34 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +Shader "Hidden/TerrainEngine/Splatmap/Diffuse-Base" { +Properties { + _Color ("Main Color", Color) = (1,1,1,1) + _MainTex ("Base (RGB)", 2D) = "white" {} +} +SubShader { + Tags { "RenderType"="Opaque" } + LOD 200 + +CGPROGRAM +#pragma surface surf Lambert vertex:SplatmapVert addshadow fullforwardshadows +#pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap forwardadd + +#define TERRAIN_BASE_PASS +#include "TerrainSplatmapCommon.cginc" + +sampler2D _MainTex; +fixed4 _Color; + +void surf (Input IN, inout SurfaceOutput o) { + fixed4 c = tex2D(_MainTex, IN.tc.xy) * _Color; + o.Albedo = c.rgb; + o.Alpha = c.a; +} +ENDCG + +UsePass "Hidden/Nature/Terrain/Utilities/PICKING" +UsePass "Hidden/Nature/Terrain/Utilities/SELECTION" +} + +Fallback "Legacy Shaders/VertexLit" +} diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/DiffuseBaseGen.shader b/DefaultResourcesExtra/TerrainShaders/Splats/DiffuseBaseGen.shader new file mode 100644 index 0000000..0d153b0 --- /dev/null +++ b/DefaultResourcesExtra/TerrainShaders/Splats/DiffuseBaseGen.shader @@ -0,0 +1,88 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +Shader "Hidden/TerrainEngine/Splatmap/Diffuse-BaseGen" { + Properties + { + [HideInInspector] _Control("AlphaMap", 2D) = "" {} + + [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {} + [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {} + [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {} + [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {} + + [HideInInspector] _DstBlend("DstBlend", Float) = 0.0 + } + SubShader + { + Tags + { + "Name" = "_MainTex" + "Format" = "ARGB32" + "Size" = "1" + } + Pass + { + ZTest Always Cull Off ZWrite Off + Blend One [_DstBlend] + CGPROGRAM + + #pragma vertex vert + #pragma fragment frag + #pragma target 2.0 + + #include "UnityCG.cginc" + + sampler2D _Control; + sampler2D _Splat0; + sampler2D _Splat1; + sampler2D _Splat2; + sampler2D _Splat3; + + float4 _Control_TexelSize; + float4 _Splat0_ST; + float4 _Splat1_ST; + float4 _Splat2_ST; + float4 _Splat3_ST; + + struct appdata_t { + float4 vertex : POSITION; + float2 texcoord : TEXCOORD0; + }; + + struct v2f { + float4 vertex : SV_POSITION; + float2 texcoord0 : TEXCOORD0; + float2 texcoord1 : TEXCOORD1; + float2 texcoord2 : TEXCOORD2; + float2 texcoord3 : TEXCOORD3; + float2 texcoord4 : TEXCOORD4; + }; + + v2f vert(appdata_t v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + // adjust splatUVs so the edges of the terrain tile lie on pixel centers + float2 controlUV = (v.texcoord * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy; + o.texcoord0 = controlUV; + o.texcoord1 = TRANSFORM_TEX(v.texcoord, _Splat0); + o.texcoord2 = TRANSFORM_TEX(v.texcoord, _Splat1); + o.texcoord3 = TRANSFORM_TEX(v.texcoord, _Splat2); + o.texcoord4 = TRANSFORM_TEX(v.texcoord, _Splat3); + return o; + } + + float4 frag(v2f i) : SV_Target + { + float4 alpha = tex2D(_Control, i.texcoord0); + float4 splat0 = tex2D(_Splat0, i.texcoord1); + float4 splat1 = tex2D(_Splat1, i.texcoord2); + float4 splat2 = tex2D(_Splat2, i.texcoord3); + float4 splat3 = tex2D(_Splat3, i.texcoord4); + return alpha.x * splat0 + alpha.y * splat1 + alpha.z * splat2 + alpha.w * splat3; + } + ENDCG + } + } + Fallback Off +} diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/FirstPass.shader b/DefaultResourcesExtra/TerrainShaders/Splats/FirstPass.shader index fb9f94a..3b7986c 100644 --- a/DefaultResourcesExtra/TerrainShaders/Splats/FirstPass.shader +++ b/DefaultResourcesExtra/TerrainShaders/Splats/FirstPass.shader @@ -2,22 +2,14 @@ Shader "Nature/Terrain/Diffuse" { Properties { - [HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {} - [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {} - [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {} - [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {} - [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {} - [HideInInspector] _Normal3 ("Normal 3 (A)", 2D) = "bump" {} - [HideInInspector] _Normal2 ("Normal 2 (B)", 2D) = "bump" {} - [HideInInspector] _Normal1 ("Normal 1 (G)", 2D) = "bump" {} - [HideInInspector] _Normal0 ("Normal 0 (R)", 2D) = "bump" {} // used in fallback on old cards & base map [HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" {} [HideInInspector] _Color ("Main Color", Color) = (1,1,1,1) } CGINCLUDE - #pragma surface surf Lambert vertex:SplatmapVert finalcolor:SplatmapFinalColor finalprepass:SplatmapFinalPrepass finalgbuffer:SplatmapFinalGBuffer noinstancing + #pragma surface surf Lambert vertex:SplatmapVert finalcolor:SplatmapFinalColor finalprepass:SplatmapFinalPrepass finalgbuffer:SplatmapFinalGBuffer addshadow fullforwardshadows + #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap forwardadd #pragma multi_compile_fog #include "TerrainSplatmapCommon.cginc" @@ -37,13 +29,16 @@ Shader "Nature/Terrain/Diffuse" { "Queue" = "Geometry-99" "RenderType" = "Opaque" } - // TODO: Seems like "#pragma target 3.0 _TERRAIN_NORMAL_MAP" can't fallback correctly on less capable devices? + // TODO: Seems like "#pragma target 3.0 _NORMALMAP" can't fallback correctly on less capable devices? // Use two sub-shaders to simulate different features for different targets and still fallback correctly. SubShader { // for sm3.0+ targets CGPROGRAM #pragma target 3.0 - #pragma multi_compile __ _TERRAIN_NORMAL_MAP + #pragma multi_compile __ _NORMALMAP ENDCG + + UsePass "Hidden/Nature/Terrain/Utilities/PICKING" + UsePass "Hidden/Nature/Terrain/Utilities/SELECTION" } SubShader { // for sm2.0 targets CGPROGRAM @@ -51,12 +46,13 @@ Shader "Nature/Terrain/Diffuse" { } } - Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Diffuse-AddPass" - Dependency "BaseMapShader" = "Diffuse" - Dependency "Details0" = "Hidden/TerrainEngine/Details/Vertexlit" - Dependency "Details1" = "Hidden/TerrainEngine/Details/WavingDoublePass" - Dependency "Details2" = "Hidden/TerrainEngine/Details/BillboardWavingDoublePass" - Dependency "Tree0" = "Hidden/TerrainEngine/BillboardTree" + Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Diffuse-AddPass" + Dependency "BaseMapShader" = "Hidden/TerrainEngine/Splatmap/Diffuse-Base" + Dependency "BaseMapGenShader" = "Hidden/TerrainEngine/Splatmap/Diffuse-BaseGen" + Dependency "Details0" = "Hidden/TerrainEngine/Details/Vertexlit" + Dependency "Details1" = "Hidden/TerrainEngine/Details/WavingDoublePass" + Dependency "Details2" = "Hidden/TerrainEngine/Details/BillboardWavingDoublePass" + Dependency "Tree0" = "Hidden/TerrainEngine/BillboardTree" Fallback "Diffuse" } diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/Specular-AddPass.shader b/DefaultResourcesExtra/TerrainShaders/Splats/Specular-AddPass.shader index adff332..fb21ee5 100644 --- a/DefaultResourcesExtra/TerrainShaders/Splats/Specular-AddPass.shader +++ b/DefaultResourcesExtra/TerrainShaders/Splats/Specular-AddPass.shader @@ -4,17 +4,6 @@ Shader "Hidden/TerrainEngine/Splatmap/Specular-AddPass" { Properties { _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1) [PowerSlider(5.0)] _Shininess ("Shininess", Range (0.03, 1)) = 0.078125 - - // set by terrain engine - [HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {} - [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {} - [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {} - [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {} - [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {} - [HideInInspector] _Normal3 ("Normal 3 (A)", 2D) = "bump" {} - [HideInInspector] _Normal2 ("Normal 2 (B)", 2D) = "bump" {} - [HideInInspector] _Normal1 ("Normal 1 (G)", 2D) = "bump" {} - [HideInInspector] _Normal0 ("Normal 0 (R)", 2D) = "bump" {} } SubShader { @@ -25,9 +14,10 @@ Shader "Hidden/TerrainEngine/Splatmap/Specular-AddPass" { } CGPROGRAM - #pragma surface surf BlinnPhong decal:add vertex:SplatmapVert finalcolor:SplatmapFinalColor finalprepass:SplatmapFinalPrepass finalgbuffer:SplatmapFinalGBuffer noinstancing + #pragma surface surf BlinnPhong decal:add vertex:SplatmapVert finalcolor:SplatmapFinalColor finalprepass:SplatmapFinalPrepass finalgbuffer:SplatmapFinalGBuffer fullforwardshadows nometa + #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap forwardadd #pragma multi_compile_fog - #pragma multi_compile __ _TERRAIN_NORMAL_MAP + #pragma multi_compile __ _NORMALMAP #pragma target 3.0 // needs more than 8 texcoords #pragma exclude_renderers gles diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/Specular-Base.shader b/DefaultResourcesExtra/TerrainShaders/Splats/Specular-Base.shader index a933aef..27abb34 100644 --- a/DefaultResourcesExtra/TerrainShaders/Splats/Specular-Base.shader +++ b/DefaultResourcesExtra/TerrainShaders/Splats/Specular-Base.shader @@ -18,23 +18,27 @@ Shader "Hidden/TerrainEngine/Splatmap/Specular-Base" { LOD 200 CGPROGRAM - #pragma surface surf BlinnPhong + #pragma surface surf BlinnPhong vertex:SplatmapVert addshadow fullforwardshadows + #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap forwardadd + + #define TERRAIN_BASE_PASS + #define TERRAIN_SURFACE_OUTPUT SurfaceOutput + #include "TerrainSplatmapCommon.cginc" sampler2D _MainTex; half _Shininess; - struct Input { - float2 uv_MainTex; - }; - void surf (Input IN, inout SurfaceOutput o) { - fixed4 tex = tex2D(_MainTex, IN.uv_MainTex); + fixed4 tex = tex2D(_MainTex, IN.tc.xy); o.Albedo = tex.rgb; o.Gloss = tex.a; o.Alpha = 1.0f; o.Specular = _Shininess; } ENDCG + + UsePass "Hidden/Nature/Terrain/Utilities/PICKING" + UsePass "Hidden/Nature/Terrain/Utilities/SELECTION" } FallBack "Legacy Shaders/Specular" diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/Specular-FirstPass.shader b/DefaultResourcesExtra/TerrainShaders/Splats/Specular-FirstPass.shader index 462074c..c8f0256 100644 --- a/DefaultResourcesExtra/TerrainShaders/Splats/Specular-FirstPass.shader +++ b/DefaultResourcesExtra/TerrainShaders/Splats/Specular-FirstPass.shader @@ -5,16 +5,6 @@ Shader "Nature/Terrain/Specular" { _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1) [PowerSlider(5.0)] _Shininess ("Shininess", Range (0.03, 1)) = 0.078125 - // set by terrain engine - [HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {} - [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {} - [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {} - [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {} - [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {} - [HideInInspector] _Normal3 ("Normal 3 (A)", 2D) = "bump" {} - [HideInInspector] _Normal2 ("Normal 2 (B)", 2D) = "bump" {} - [HideInInspector] _Normal1 ("Normal 1 (G)", 2D) = "bump" {} - [HideInInspector] _Normal0 ("Normal 0 (R)", 2D) = "bump" {} // used in fallback on old cards & base map [HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" {} [HideInInspector] _Color ("Main Color", Color) = (1,1,1,1) @@ -27,9 +17,10 @@ Shader "Nature/Terrain/Specular" { } CGPROGRAM - #pragma surface surf BlinnPhong vertex:SplatmapVert finalcolor:SplatmapFinalColor finalprepass:SplatmapFinalPrepass finalgbuffer:SplatmapFinalGBuffer noinstancing + #pragma surface surf BlinnPhong vertex:SplatmapVert finalcolor:SplatmapFinalColor finalprepass:SplatmapFinalPrepass finalgbuffer:SplatmapFinalGBuffer addshadow fullforwardshadows + #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap forwardadd #pragma multi_compile_fog - #pragma multi_compile __ _TERRAIN_NORMAL_MAP + #pragma multi_compile __ _NORMALMAP #pragma target 3.0 // needs more than 8 texcoords #pragma exclude_renderers gles @@ -50,10 +41,14 @@ Shader "Nature/Terrain/Specular" { o.Specular = _Shininess; } ENDCG + + UsePass "Hidden/Nature/Terrain/Utilities/PICKING" + UsePass "Hidden/Nature/Terrain/Utilities/SELECTION" } - Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Specular-AddPass" - Dependency "BaseMapShader" = "Hidden/TerrainEngine/Splatmap/Specular-Base" + Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Specular-AddPass" + Dependency "BaseMapShader" = "Hidden/TerrainEngine/Splatmap/Specular-Base" + Dependency "BaseMapGenShader" = "Hidden/TerrainEngine/Splatmap/Diffuse-BaseGen" Fallback "Nature/Terrain/Diffuse" } diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/Standard-AddPass.shader b/DefaultResourcesExtra/TerrainShaders/Splats/Standard-AddPass.shader index 2610bc2..160c13d 100644 --- a/DefaultResourcesExtra/TerrainShaders/Splats/Standard-AddPass.shader +++ b/DefaultResourcesExtra/TerrainShaders/Splats/Standard-AddPass.shader @@ -1,27 +1,6 @@ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) Shader "Hidden/TerrainEngine/Splatmap/Standard-AddPass" { - Properties { - // set by terrain engine - [HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {} - [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {} - [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {} - [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {} - [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {} - [HideInInspector] _Normal3 ("Normal 3 (A)", 2D) = "bump" {} - [HideInInspector] _Normal2 ("Normal 2 (B)", 2D) = "bump" {} - [HideInInspector] _Normal1 ("Normal 1 (G)", 2D) = "bump" {} - [HideInInspector] _Normal0 ("Normal 0 (R)", 2D) = "bump" {} - [HideInInspector] [Gamma] _Metallic0 ("Metallic 0", Range(0.0, 1.0)) = 0.0 - [HideInInspector] [Gamma] _Metallic1 ("Metallic 1", Range(0.0, 1.0)) = 0.0 - [HideInInspector] [Gamma] _Metallic2 ("Metallic 2", Range(0.0, 1.0)) = 0.0 - [HideInInspector] [Gamma] _Metallic3 ("Metallic 3", Range(0.0, 1.0)) = 0.0 - [HideInInspector] _Smoothness0 ("Smoothness 0", Range(0.0, 1.0)) = 1.0 - [HideInInspector] _Smoothness1 ("Smoothness 1", Range(0.0, 1.0)) = 1.0 - [HideInInspector] _Smoothness2 ("Smoothness 2", Range(0.0, 1.0)) = 1.0 - [HideInInspector] _Smoothness3 ("Smoothness 3", Range(0.0, 1.0)) = 1.0 - } - SubShader { Tags { "Queue" = "Geometry-99" @@ -30,17 +9,19 @@ Shader "Hidden/TerrainEngine/Splatmap/Standard-AddPass" { } CGPROGRAM - #pragma surface surf Standard decal:add vertex:SplatmapVert finalcolor:SplatmapFinalColor finalgbuffer:SplatmapFinalGBuffer fullforwardshadows noinstancing + #pragma surface surf Standard decal:add vertex:SplatmapVert finalcolor:SplatmapFinalColor finalgbuffer:SplatmapFinalGBuffer fullforwardshadows nometa + #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap forwardadd #pragma multi_compile_fog #pragma target 3.0 // needs more than 8 texcoords - #pragma exclude_renderers gles psp2 + #pragma exclude_renderers gles #include "UnityPBSLighting.cginc" - #pragma multi_compile __ _TERRAIN_NORMAL_MAP + #pragma multi_compile __ _NORMALMAP #define TERRAIN_SPLAT_ADDPASS #define TERRAIN_STANDARD_SHADER + #define TERRAIN_INSTANCED_PERPIXEL_NORMAL #define TERRAIN_SURFACE_OUTPUT SurfaceOutputStandard #include "TerrainSplatmapCommon.cginc" diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/Standard-Base.shader b/DefaultResourcesExtra/TerrainShaders/Splats/Standard-Base.shader index dd1f8a3..e743895 100644 --- a/DefaultResourcesExtra/TerrainShaders/Splats/Standard-Base.shader +++ b/DefaultResourcesExtra/TerrainShaders/Splats/Standard-Base.shader @@ -17,28 +17,40 @@ Shader "Hidden/TerrainEngine/Splatmap/Standard-Base" { LOD 200 CGPROGRAM - #pragma surface surf Standard fullforwardshadows + #pragma surface surf Standard vertex:SplatmapVert addshadow fullforwardshadows + #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap forwardadd #pragma target 3.0 // needs more than 8 texcoords #pragma exclude_renderers gles + + #define TERRAIN_BASE_PASS + #define TERRAIN_INSTANCED_PERPIXEL_NORMAL + #include "TerrainSplatmapCommon.cginc" #include "UnityPBSLighting.cginc" sampler2D _MainTex; sampler2D _MetallicTex; - struct Input { - float2 uv_MainTex; - }; - void surf (Input IN, inout SurfaceOutputStandard o) { - half4 c = tex2D (_MainTex, IN.uv_MainTex); + half4 c = tex2D (_MainTex, IN.tc.xy); o.Albedo = c.rgb; o.Alpha = 1; o.Smoothness = c.a; - o.Metallic = tex2D (_MetallicTex, IN.uv_MainTex).r; + o.Metallic = tex2D (_MetallicTex, IN.tc.xy).r; + + #if defined(INSTANCING_ON) && defined(SHADER_TARGET_SURFACE_ANALYSIS) && defined(TERRAIN_INSTANCED_PERPIXEL_NORMAL) + o.Normal = float3(0, 0, 1); // make sure that surface shader compiler realizes we write to normal, as UNITY_INSTANCING_ENABLED is not defined for SHADER_TARGET_SURFACE_ANALYSIS. + #endif + + #if defined(UNITY_INSTANCING_ENABLED) && !defined(SHADER_API_D3D11_9X) && defined(TERRAIN_INSTANCED_PERPIXEL_NORMAL) + o.Normal = normalize(tex2D(_TerrainNormalmapTexture, IN.tc.zw).xyz * 2 - 1).xzy; + #endif } ENDCG + + UsePass "Hidden/Nature/Terrain/Utilities/PICKING" + UsePass "Hidden/Nature/Terrain/Utilities/SELECTION" } FallBack "Diffuse" diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/Standard-BaseGen.shader b/DefaultResourcesExtra/TerrainShaders/Splats/Standard-BaseGen.shader new file mode 100644 index 0000000..3b1ecf5 --- /dev/null +++ b/DefaultResourcesExtra/TerrainShaders/Splats/Standard-BaseGen.shader @@ -0,0 +1,163 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +Shader "Hidden/TerrainEngine/Splatmap/Standard-BaseGen" { + Properties + { + [HideInInspector] _Control("AlphaMap", 2D) = "" {} + + [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {} + [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {} + [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {} + [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {} + [HideInInspector] [Gamma] _Metallic0 ("Metallic 0", Range(0.0, 1.0)) = 0.0 + [HideInInspector] [Gamma] _Metallic1 ("Metallic 1", Range(0.0, 1.0)) = 0.0 + [HideInInspector] [Gamma] _Metallic2 ("Metallic 2", Range(0.0, 1.0)) = 0.0 + [HideInInspector] [Gamma] _Metallic3 ("Metallic 3", Range(0.0, 1.0)) = 0.0 + [HideInInspector] _Smoothness0 ("Smoothness 0", Range(0.0, 1.0)) = 1.0 + [HideInInspector] _Smoothness1 ("Smoothness 1", Range(0.0, 1.0)) = 1.0 + [HideInInspector] _Smoothness2 ("Smoothness 2", Range(0.0, 1.0)) = 1.0 + [HideInInspector] _Smoothness3 ("Smoothness 3", Range(0.0, 1.0)) = 1.0 + + [HideInInspector] _DstBlend("DstBlend", Float) = 0.0 + } + SubShader + { + CGINCLUDE + + #include "UnityCG.cginc" + sampler2D _Control; + + struct appdata_t { + float4 vertex : POSITION; + float2 texcoord : TEXCOORD0; + }; + + ENDCG + + Pass + { + Tags + { + "Name" = "_MainTex" + "Format" = "ARGB32" + "Size" = "1" + } + + ZTest Always Cull Off ZWrite Off + Blend One [_DstBlend] + CGPROGRAM + + #pragma vertex vert + #pragma fragment frag + + sampler2D _Splat0; + sampler2D _Splat1; + sampler2D _Splat2; + sampler2D _Splat3; + + float _Smoothness0; + float _Smoothness1; + float _Smoothness2; + float _Smoothness3; + + float4 _Control_TexelSize; + float4 _Splat0_ST; + float4 _Splat1_ST; + float4 _Splat2_ST; + float4 _Splat3_ST; + + struct v2f { + float4 vertex : SV_POSITION; + float2 texcoord0 : TEXCOORD0; + float2 texcoord1 : TEXCOORD1; + float2 texcoord2 : TEXCOORD2; + float2 texcoord3 : TEXCOORD3; + float2 texcoord4 : TEXCOORD4; + }; + + v2f vert(appdata_t v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + // adjust splatUVs so the edges of the terrain tile lie on pixel centers + float2 controlUV = (v.texcoord * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy; + o.texcoord0 = controlUV; + o.texcoord1 = TRANSFORM_TEX(v.texcoord, _Splat0); + o.texcoord2 = TRANSFORM_TEX(v.texcoord, _Splat1); + o.texcoord3 = TRANSFORM_TEX(v.texcoord, _Splat2); + o.texcoord4 = TRANSFORM_TEX(v.texcoord, _Splat3); + return o; + } + + float4 frag(v2f i) : SV_Target + { + float4 alpha = tex2D(_Control, i.texcoord0); + float4 splat0 = tex2D(_Splat0, i.texcoord1); + float4 splat1 = tex2D(_Splat1, i.texcoord2); + float4 splat2 = tex2D(_Splat2, i.texcoord3); + float4 splat3 = tex2D(_Splat3, i.texcoord4); + + splat0.a *= _Smoothness0; + splat1.a *= _Smoothness1; + splat2.a *= _Smoothness2; + splat3.a *= _Smoothness3; + + float4 albedoSmoothness = splat0 * alpha.x; + albedoSmoothness += splat1 * alpha.y; + albedoSmoothness += splat2 * alpha.z; + albedoSmoothness += splat3 * alpha.w; + return albedoSmoothness; + } + ENDCG + } + + Pass + { + Tags + { + "Name" = "_MetallicTex" + "Format" = "R8" + "Size" = "1/4" + "EmptyColor" = "FF000000" + } + + ZTest Always Cull Off ZWrite Off + Blend One [_DstBlend] + CGPROGRAM + + #pragma vertex vert + #pragma fragment frag + + float _Metallic0; + float _Metallic1; + float _Metallic2; + float _Metallic3; + + struct v2f { + float4 vertex : SV_POSITION; + float2 texcoord0 : TEXCOORD0; + }; + + v2f vert(appdata_t v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.texcoord0 = v.texcoord; + return o; + } + + float4 frag(v2f i) : SV_Target + { + float4 alpha = tex2D(_Control, i.texcoord0); + + float4 metallic = { _Metallic0 * alpha.x, 0, 0, 0 }; + metallic.r += _Metallic1 * alpha.y; + metallic.r += _Metallic2 * alpha.z; + metallic.r += _Metallic3 * alpha.w; + return metallic; + } + ENDCG + } + } + Fallback Off +} diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/Standard-FirstPass.shader b/DefaultResourcesExtra/TerrainShaders/Splats/Standard-FirstPass.shader index bb68949..05e9c25 100644 --- a/DefaultResourcesExtra/TerrainShaders/Splats/Standard-FirstPass.shader +++ b/DefaultResourcesExtra/TerrainShaders/Splats/Standard-FirstPass.shader @@ -2,25 +2,6 @@ Shader "Nature/Terrain/Standard" { Properties { - // set by terrain engine - [HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {} - [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {} - [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {} - [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {} - [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {} - [HideInInspector] _Normal3 ("Normal 3 (A)", 2D) = "bump" {} - [HideInInspector] _Normal2 ("Normal 2 (B)", 2D) = "bump" {} - [HideInInspector] _Normal1 ("Normal 1 (G)", 2D) = "bump" {} - [HideInInspector] _Normal0 ("Normal 0 (R)", 2D) = "bump" {} - [HideInInspector] [Gamma] _Metallic0 ("Metallic 0", Range(0.0, 1.0)) = 0.0 - [HideInInspector] [Gamma] _Metallic1 ("Metallic 1", Range(0.0, 1.0)) = 0.0 - [HideInInspector] [Gamma] _Metallic2 ("Metallic 2", Range(0.0, 1.0)) = 0.0 - [HideInInspector] [Gamma] _Metallic3 ("Metallic 3", Range(0.0, 1.0)) = 0.0 - [HideInInspector] _Smoothness0 ("Smoothness 0", Range(0.0, 1.0)) = 1.0 - [HideInInspector] _Smoothness1 ("Smoothness 1", Range(0.0, 1.0)) = 1.0 - [HideInInspector] _Smoothness2 ("Smoothness 2", Range(0.0, 1.0)) = 1.0 - [HideInInspector] _Smoothness3 ("Smoothness 3", Range(0.0, 1.0)) = 1.0 - // used in fallback on old cards & base map [HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" {} [HideInInspector] _Color ("Main Color", Color) = (1,1,1,1) @@ -33,16 +14,18 @@ Shader "Nature/Terrain/Standard" { } CGPROGRAM - #pragma surface surf Standard vertex:SplatmapVert finalcolor:SplatmapFinalColor finalgbuffer:SplatmapFinalGBuffer fullforwardshadows noinstancing - #pragma multi_compile_fog + #pragma surface surf Standard vertex:SplatmapVert finalcolor:SplatmapFinalColor finalgbuffer:SplatmapFinalGBuffer addshadow fullforwardshadows + #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap forwardadd + #pragma multi_compile_fog // needed because finalcolor oppresses fog code generation. #pragma target 3.0 // needs more than 8 texcoords - #pragma exclude_renderers gles psp2 + #pragma exclude_renderers gles #include "UnityPBSLighting.cginc" - #pragma multi_compile __ _TERRAIN_NORMAL_MAP + #pragma multi_compile __ _NORMALMAP #define TERRAIN_STANDARD_SHADER + #define TERRAIN_INSTANCED_PERPIXEL_NORMAL #define TERRAIN_SURFACE_OUTPUT SurfaceOutputStandard #include "TerrainSplatmapCommon.cginc" @@ -68,10 +51,14 @@ Shader "Nature/Terrain/Standard" { o.Metallic = dot(splat_control, half4(_Metallic0, _Metallic1, _Metallic2, _Metallic3)); } ENDCG + + UsePass "Hidden/Nature/Terrain/Utilities/PICKING" + UsePass "Hidden/Nature/Terrain/Utilities/SELECTION" } - Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Standard-AddPass" - Dependency "BaseMapShader" = "Hidden/TerrainEngine/Splatmap/Standard-Base" + Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Standard-AddPass" + Dependency "BaseMapShader" = "Hidden/TerrainEngine/Splatmap/Standard-Base" + Dependency "BaseMapGenShader" = "Hidden/TerrainEngine/Splatmap/Standard-BaseGen" Fallback "Nature/Terrain/Diffuse" } diff --git a/DefaultResourcesExtra/TerrainShaders/Splats/Terrain-Utilities.shader b/DefaultResourcesExtra/TerrainShaders/Splats/Terrain-Utilities.shader new file mode 100644 index 0000000..c211253 --- /dev/null +++ b/DefaultResourcesExtra/TerrainShaders/Splats/Terrain-Utilities.shader @@ -0,0 +1,72 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +Shader "Hidden/Nature/Terrain/Utilities" +{ + SubShader + { + Pass + { + Name "Picking" + Tags { "LightMode" = "Picking" } + + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + #pragma multi_compile_instancing + #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap + #include "UnityCG.cginc" + + #define TERRAIN_BASE_PASS + #include "TerrainSplatmapCommon.cginc" + + float4 vert(appdata_full v) : SV_POSITION + { + UNITY_SETUP_INSTANCE_ID(v); + Input i; + SplatmapVert(v, i); + return UnityObjectToClipPos(v.vertex); + } + + uniform float4 _SelectionID; + + fixed4 frag() : SV_Target + { + return _SelectionID; + } + ENDCG + } + + Pass + { + Name "Selection" + Tags { "LightMode" = "SceneSelectionPass" } + + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + #pragma multi_compile_instancing + #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap + #include "UnityCG.cginc" + + #define TERRAIN_BASE_PASS + #include "TerrainSplatmapCommon.cginc" + + float4 vert(appdata_full v) : SV_POSITION + { + UNITY_SETUP_INSTANCE_ID(v); + Input i; + SplatmapVert(v, i); + return UnityObjectToClipPos(v.vertex); + } + + int _ObjectId; + int _PassValue; + + float4 frag() : SV_Target + { + return float4(_ObjectId, _PassValue, 1, 1); + } + ENDCG + } + } +} diff --git a/DefaultResourcesExtra/TerrainShaders/Utils/BrushPreview.shader b/DefaultResourcesExtra/TerrainShaders/Utils/BrushPreview.shader new file mode 100644 index 0000000..b941b67 --- /dev/null +++ b/DefaultResourcesExtra/TerrainShaders/Utils/BrushPreview.shader @@ -0,0 +1,244 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +Shader "Hidden/TerrainEngine/BrushPreview" +{ + SubShader + { + ZTest Always Cull Back ZWrite Off + Blend SrcAlpha OneMinusSrcAlpha + + CGINCLUDE + // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices + #pragma exclude_renderers gles + + #include "UnityCG.cginc" + #include "TerrainPreview.cginc" + + sampler2D _BrushTex; + + ENDCG + + Pass // 0 + { + Name "TerrainPreviewProcedural" + + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + + struct v2f { + float4 clipPosition : SV_POSITION; + float3 positionWorld : TEXCOORD0; + float3 positionWorldOrig : TEXCOORD1; + float2 pcPixels : TEXCOORD2; + float2 brushUV : TEXCOORD3; + }; + + v2f vert(uint vid : SV_VertexID) + { + // build a quad mesh, with one vertex per paint context pixel (pcPixel) + float2 pcPixels = BuildProceduralQuadMeshVertex(vid); + + // compute heightmap UV and sample heightmap + float2 heightmapUV = PaintContextPixelsToHeightmapUV(pcPixels); + float heightmapSample = UnpackHeightmap(tex2Dlod(_Heightmap, float4(heightmapUV, 0, 0))); + + // compute brush UV + float2 brushUV = PaintContextPixelsToBrushUV(pcPixels); + + // compute object position (in terrain space) and world position + float3 positionObject = PaintContextPixelsToObjectPosition(pcPixels, heightmapSample); + float3 positionWorld = TerrainObjectToWorldPosition(positionObject); + + v2f o; + o.pcPixels = pcPixels; + o.positionWorld = positionWorld; + o.positionWorldOrig = positionWorld; + o.clipPosition = UnityWorldToClipPos(positionWorld); + o.brushUV = brushUV; + return o; + } + + + float4 frag(v2f i) : SV_Target + { + float brushSample = UnpackHeightmap(tex2D(_BrushTex, i.brushUV)); + + // out of bounds multiplier + float oob = all(saturate(i.brushUV) == i.brushUV) ? 1.0f : 0.0f; + + // brush outline stripe + float stripeWidth = 2.0f; // pixels + float stripeLocation = 0.2f; // at 20% alpha + float brushStripe = Stripe(brushSample, stripeLocation, stripeWidth); + + float4 color = float4(0.5f, 0.5f, 1.0f, 1.0f) * saturate(brushStripe + 0.5f * brushSample); + color.a = 0.6f * saturate(brushSample * 5.0f); + return color * oob; + } + ENDCG + } + + Pass // 1 + { + Name "TerrainPreviewProceduralDeltaNormals" + + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + + sampler2D _HeightmapOrig; + + struct v2f { + float4 clipPosition : SV_POSITION; + float3 positionWorld : TEXCOORD0; + float3 positionWorldOrig : TEXCOORD1; + float2 pcPixels : TEXCOORD2; + float2 brushUV : TEXCOORD3; + }; + + v2f vert(uint vid : SV_VertexID) + { + // build a quad mesh, with one vertex per paint context pixel (pcPixel) + float2 pcPixels = BuildProceduralQuadMeshVertex(vid); + + // compute heightmap UV and sample heightmap + float2 heightmapUV = PaintContextPixelsToHeightmapUV(pcPixels); + float heightmapSample = UnpackHeightmap(tex2Dlod(_Heightmap, float4(heightmapUV, 0, 0))); + float heightmapSampleOrig = UnpackHeightmap(tex2Dlod(_HeightmapOrig, float4(heightmapUV, 0, 0))); + + // compute brush UV + float2 brushUV = PaintContextPixelsToBrushUV(pcPixels); + + // compute object position (in terrain space) and world position + float3 positionObject = PaintContextPixelsToObjectPosition(pcPixels, heightmapSample); + float3 positionWorld = TerrainObjectToWorldPosition(positionObject); + + float3 positionObjectOrig = PaintContextPixelsToObjectPosition(pcPixels, heightmapSampleOrig); + float3 positionWorldOrig = TerrainObjectToWorldPosition(positionObjectOrig); + + v2f o; + o.pcPixels = pcPixels; + o.positionWorld = positionWorld; + o.positionWorldOrig = positionWorldOrig; + o.clipPosition = UnityWorldToClipPos(positionWorld); + o.brushUV = brushUV; + return o; + } + + float4 frag(v2f i) : SV_Target + { + float brushSample = UnpackHeightmap(tex2D(_BrushTex, i.brushUV)); + + // out of bounds multiplier + float oob = all(saturate(i.brushUV) == i.brushUV) ? 1.0f : 0.0f; + + float deltaHeight = abs(i.positionWorld.y - i.positionWorldOrig.y); + + // normal based coloring + float3 dx = ddx(i.positionWorld); + float3 dy = ddy(i.positionWorld); + float3 normal = normalize(cross(dy, dx)); + + float3 lightDir = UnityWorldSpaceLightDir(i.positionWorld.xyz); + + float4 color; + color.rgb = saturate(normal.xzy * float3(-0.5f, -0.5f, 0.5f) + 0.5f); + color.rgb = lerp(color.rgb, float3(1.0f, 1.0f, 1.0f), 0.3f); + color.rgb = color.rgb * (0.1f + 0.9f * dot(lightDir, normal)); + color.a = 0.9f * saturate(0.2f * deltaHeight); + + return color; + } + ENDCG + } + + Pass // 2 + { + Name "TerrainPreviewProceduralDeltaStripes" + + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + + sampler2D _HeightmapOrig; + + struct v2f { + float4 clipPosition : SV_POSITION; + float3 positionWorld : TEXCOORD0; + float3 positionWorldOrig : TEXCOORD1; + float2 pcPixels : TEXCOORD2; + float2 brushUV : TEXCOORD3; + }; + + v2f vert(uint vid : SV_VertexID) + { + // build a quad mesh, with one vertex per paint context pixel (pcPixel) + float2 pcPixels = BuildProceduralQuadMeshVertex(vid); + + // compute heightmap UV and sample heightmap + float2 heightmapUV = PaintContextPixelsToHeightmapUV(pcPixels); + float heightmapSample = UnpackHeightmap(tex2Dlod(_Heightmap, float4(heightmapUV, 0, 0))); + float heightmapSampleOrig = UnpackHeightmap(tex2Dlod(_HeightmapOrig, float4(heightmapUV, 0, 0))); + + // compute brush UV + float2 brushUV = PaintContextPixelsToBrushUV(pcPixels); + + // compute object position (in terrain space) and world position + float3 positionObject = PaintContextPixelsToObjectPosition(pcPixels, heightmapSample); + float3 positionWorld = TerrainObjectToWorldPosition(positionObject); + + float3 positionObjectOrig = PaintContextPixelsToObjectPosition(pcPixels, heightmapSampleOrig); + float3 positionWorldOrig = TerrainObjectToWorldPosition(positionObjectOrig); + + v2f o; + o.pcPixels = pcPixels; + o.positionWorld = positionWorld; + o.positionWorldOrig = positionWorldOrig; + o.clipPosition = UnityWorldToClipPos(positionWorld); + o.brushUV = brushUV; + return o; + } + + float MultiStripes(in float x, in float freq1, in float freq2) + { + float2 derivatives = float2(ddx(x), ddy(x)); + float derivLen = length(derivatives); + + float tweak = 0.5; + float sharpen = tweak / max(derivLen, 0.00001f); + + float triwave1 = abs(frac(x * freq1) - 0.5f) - 0.25f; + float triwave2 = abs(frac(x * freq2) - 0.5f) - 0.25f; + + float width = 0.95f; + + float result1 = saturate((triwave1 - width * 0.25f) * sharpen / freq1 + 0.75f); + float result2 = saturate((triwave2 - width * 0.25f) * sharpen / freq2 + 0.75f); + + return max(result1, result2); + } + + float4 frag(v2f i) : SV_Target + { + float brushSample = UnpackHeightmap(tex2D(_BrushTex, i.brushUV)); + + // out of bounds multiplier + float oob = all(saturate(i.brushUV) == i.brushUV) ? 1.0f : 0.0f; + + // brush outline stripe + float stripeWidth = 1.0f; // pixels + float stripeLocation = 0.2f; // at 20% alpha + float heightStripes = MultiStripes(i.positionWorld.y + 0.25f, 0.0625f, 1.0f); + float xStripes = MultiStripes(i.positionWorld.x, 0.03125f, 0.5f); + float zStripes = MultiStripes(i.positionWorld.z, 0.03125f, 0.5f); + + float deltaHeight = saturate(abs(i.positionWorld.y - i.positionWorldOrig.y)); + float4 color = lerp(float4(0.5f, 0.5f, 1.0f, 1.0f), float4(0.5f, 1.0f, 0.5f, 1.0f), deltaHeight); + return color * lerp(deltaHeight * 0.5f, (brushSample > 0.0f ? 1.0f : 0.0f), 0.5f * saturate(heightStripes + xStripes + zStripes)); + } + ENDCG + } + } + Fallback Off +} diff --git a/DefaultResourcesExtra/TerrainShaders/Utils/GenNormalmap.shader b/DefaultResourcesExtra/TerrainShaders/Utils/GenNormalmap.shader new file mode 100644 index 0000000..dea2203 --- /dev/null +++ b/DefaultResourcesExtra/TerrainShaders/Utils/GenNormalmap.shader @@ -0,0 +1,89 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +Shader "Hidden/TerrainEngine/GenerateNormalmap" { + Properties { _MainTex ("Texture", any) = "" {} } + SubShader { + Pass { + ZTest Always Cull Off ZWrite Off + + CGPROGRAM + #pragma vertex vert + #pragma fragment CalculateNormalSobel + #pragma target 3.0 + + #include "UnityCG.cginc" + + sampler2D _MainTex; + uniform float4 _MainTex_ST; + uniform float4 _TerrainNormalmapGenSize; // (1.0f / (float)m_Width, 1.0f / (float)m_Height, 1.0f / hmScale.x, 1.0f / hmScale.z); + uniform float4 _TerrainTilesScaleOffsets[9]; // ((65535.0f / kMaxHeight) * hmScale.y, terrainPosition.y, 0.0f, 0.0f) + + struct appdata_t { + float4 vertex : POSITION; + float2 texcoord : TEXCOORD0; + }; + + struct v2f { + float4 vertex : SV_POSITION; + float4 bt : TEXCOORD0; + float4 lr : TEXCOORD1; + float4 blbr : TEXCOORD2; + float4 tltr : TEXCOORD3; + }; + + v2f vert (appdata_t v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.bt = float4(v.texcoord.x, v.texcoord.y - _TerrainNormalmapGenSize.y, v.texcoord.x, v.texcoord.y + _TerrainNormalmapGenSize.y); + o.lr = float4(v.texcoord.x - _TerrainNormalmapGenSize.x, v.texcoord.y, v.texcoord.x + _TerrainNormalmapGenSize.x, v.texcoord.y); + o.blbr = float4(v.texcoord.x - _TerrainNormalmapGenSize.x, v.texcoord.y - _TerrainNormalmapGenSize.y, v.texcoord.x + _TerrainNormalmapGenSize.x, v.texcoord.y - _TerrainNormalmapGenSize.y); + o.tltr = float4(v.texcoord.x - _TerrainNormalmapGenSize.x, v.texcoord.y + _TerrainNormalmapGenSize.y, v.texcoord.x + _TerrainNormalmapGenSize.x, v.texcoord.y + _TerrainNormalmapGenSize.y); + return o; + } + + int ComputeTileIndex(float2 texcoord) + { + int2 idx = int2(floor(texcoord + 1.0f)); + return idx.y * 3 + idx.x; + } + + float4 CalculateNormalSobel (v2f i) : SV_Target + { + float2 scaleOffsetB = _TerrainTilesScaleOffsets[ComputeTileIndex(i.bt.xy)]; + float2 scaleOffsetT = _TerrainTilesScaleOffsets[ComputeTileIndex(i.bt.zw)]; + float2 scaleOffsetL = _TerrainTilesScaleOffsets[ComputeTileIndex(i.lr.xy)]; + float2 scaleOffsetR = _TerrainTilesScaleOffsets[ComputeTileIndex(i.lr.zw)]; + float2 scaleOffsetBL = _TerrainTilesScaleOffsets[ComputeTileIndex(i.blbr.xy)]; + float2 scaleOffsetBR = _TerrainTilesScaleOffsets[ComputeTileIndex(i.blbr.zw)]; + float2 scaleOffsetTL = _TerrainTilesScaleOffsets[ComputeTileIndex(i.tltr.xy)]; + float2 scaleOffsetTR = _TerrainTilesScaleOffsets[ComputeTileIndex(i.tltr.zw)]; + + float2 d; + + // Do X sobel filter + d.x = (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.blbr.xy, _MainTex))) * scaleOffsetBL.x + scaleOffsetBL.y) * -1.0F; + d.x += (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.lr.xy, _MainTex))) * scaleOffsetL.x + scaleOffsetL.y) * -2.0F; + d.x += (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.tltr.xy, _MainTex))) * scaleOffsetTL.x + scaleOffsetTL.y) * -1.0F; + d.x += (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.blbr.zw, _MainTex))) * scaleOffsetBR.x + scaleOffsetBR.y) * 1.0F; + d.x += (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.lr.zw, _MainTex))) * scaleOffsetR.x + scaleOffsetR.y) * 2.0F; + d.x += (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.tltr.zw, _MainTex))) * scaleOffsetTR.x + scaleOffsetTR.y) * 1.0F; + + // Do Y sobel filter + d.y = (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.blbr.xy, _MainTex))) * scaleOffsetBL.x + scaleOffsetBL.y) * -1.0F; + d.y += (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.bt.xy, _MainTex))) * scaleOffsetB.x + scaleOffsetB.y) * -2.0F; + d.y += (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.blbr.zw, _MainTex))) * scaleOffsetBR.x + scaleOffsetBR.y) * -1.0F; + d.y += (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.tltr.xy, _MainTex))) * scaleOffsetTL.x + scaleOffsetTL.y) * 1.0F; + d.y += (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.bt.zw, _MainTex))) * scaleOffsetT.x + scaleOffsetT.y) * 2.0F; + d.y += (UnpackHeightmap(tex2D(_MainTex, TRANSFORM_TEX(i.tltr.zw, _MainTex))) * scaleOffsetTR.x + scaleOffsetTR.y) * 1.0F; + d *= _TerrainNormalmapGenSize.zw; + + // Cross Product of components of gradient reduces to + float3 normal = float3(-d.x, 8.0f, -d.y); + return float4(0.5f + 0.5f * normalize(normal), 1.0f); + } + ENDCG + } + } + Fallback Off +} diff --git a/DefaultResourcesExtra/TerrainShaders/Utils/PaintHeight.shader b/DefaultResourcesExtra/TerrainShaders/Utils/PaintHeight.shader new file mode 100644 index 0000000..faa713f --- /dev/null +++ b/DefaultResourcesExtra/TerrainShaders/Utils/PaintHeight.shader @@ -0,0 +1,245 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +Shader "Hidden/TerrainEngine/PaintHeight" { + + Properties { _MainTex ("Texture", any) = "" {} } + + SubShader { + + ZTest Always Cull Off ZWrite Off + + CGINCLUDE + + #include "UnityCG.cginc" + #include "TerrainTool.cginc" + + sampler2D _MainTex; + float4 _MainTex_TexelSize; // 1/width, 1/height, width, height + + sampler2D _BrushTex; + + float4 _BrushParams; + #define BRUSH_STRENGTH (_BrushParams[0]) + #define BRUSH_TARGETHEIGHT (_BrushParams[1]) + + struct appdata_t { + float4 vertex : POSITION; + float2 pcUV : TEXCOORD0; + }; + + struct v2f { + float4 vertex : SV_POSITION; + float2 pcUV : TEXCOORD0; + }; + + v2f vert(appdata_t v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.pcUV = v.pcUV; + return o; + } + + float ApplyBrush(float height, float brushStrength) + { + float targetHeight = BRUSH_TARGETHEIGHT; + if (targetHeight > height) + { + height += brushStrength; + height = height < targetHeight ? height : targetHeight; + } + else + { + height -= brushStrength; + height = height > targetHeight ? height : targetHeight; + } + return height; + } + + ENDCG + + Pass // 0 raise/lower heights + { + Name "Raise/Lower Heights" + + CGPROGRAM + #pragma vertex vert + #pragma fragment RaiseHeight + + float4 RaiseHeight(v2f i) : SV_Target + { + float2 brushUV = PaintContextUVToBrushUV(i.pcUV); + float2 heightmapUV = PaintContextUVToHeightmapUV(i.pcUV); + + // out of bounds multiplier + float oob = all(saturate(brushUV) == brushUV) ? 1.0f : 0.0f; + + float height = UnpackHeightmap(tex2D(_MainTex, heightmapUV)); + float brushShape = oob * UnpackHeightmap(tex2D(_BrushTex, brushUV)); + + return PackHeightmap(clamp(height + BRUSH_STRENGTH * brushShape, 0, 0.5f)); + } + ENDCG + } + + Pass // 1 stamp heights + { + Name "Stamp Heights" + + CGPROGRAM + #pragma vertex vert + #pragma fragment StampHeight + + #define BRUSH_OPACITY (_BrushParams[0]) + #define BRUSH_STAMPHEIGHT (_BrushParams[2]) + #define BRUSH_MAXBLENDADD (_BrushParams[3]) + + float SmoothMax(float a, float b, float p) + { + // calculates a smooth maximum of a and b, using an intersection power p + // higher powers produce sharper intersections, approaching max() + return log2(exp2(a * p) + exp2(b * p) - 1.0f) / p; + } + + float4 StampHeight(v2f i) : SV_Target + { + float2 brushUV = PaintContextUVToBrushUV(i.pcUV); + float2 heightmapUV = PaintContextUVToHeightmapUV(i.pcUV); + + // out of bounds multiplier + float oob = all(saturate(brushUV) == brushUV) ? 1.0f : 0.0f; + + float height = UnpackHeightmap(tex2D(_MainTex, heightmapUV)); + float brushShape = oob * UnpackHeightmap(tex2D(_BrushTex, brushUV)); + float brushHeight = brushShape * BRUSH_STAMPHEIGHT; + + float targetHeight; + if (BRUSH_MAXBLENDADD > 0.0f) + { + float brushIntersection = saturate(1.0f - BRUSH_MAXBLENDADD); + float brushSmooth = exp2(brushIntersection * 8.0f); + targetHeight = SmoothMax(height, brushHeight, brushSmooth); + } + else + { + targetHeight = max(height, brushHeight); + } + targetHeight = clamp(targetHeight, 0.0f, 0.5f); // Keep in valid range (0..0.5f) + + height = lerp(height, targetHeight, BRUSH_OPACITY); + return PackHeightmap(height); + } + ENDCG + } + + Pass // 2 set height (flatten) + { + Name "Set Heights" + + CGPROGRAM + #pragma vertex vert + #pragma fragment SetHeight + + float4 SetHeight(v2f i) : SV_Target + { + float2 brushUV = PaintContextUVToBrushUV(i.pcUV); + float2 heightmapUV = PaintContextUVToHeightmapUV(i.pcUV); + + // out of bounds multiplier + float oob = all(saturate(brushUV) == brushUV) ? 1.0f : 0.0f; + + float height = UnpackHeightmap(tex2D(_MainTex, heightmapUV)); + float brushStrength = BRUSH_STRENGTH * oob * UnpackHeightmap(tex2D(_BrushTex, brushUV)); + + // smooth set + float targetHeight = BRUSH_TARGETHEIGHT; + + // have to do this check to ensure strength 0 == no change (code below makes a super tiny change even with strength 0) + if (brushStrength > 0.0f) + { + float deltaHeight = height - targetHeight; + + // see https://www.desmos.com/calculator/880ka3lfkl + float p = saturate(brushStrength); + float w = (1.0f - p) / (p + 0.000001f); + // float w = (1.0f - p*p) / (p + 0.000001f); // alternative TODO test and compare + float fx = clamp(w * deltaHeight, -1.0f, 1.0f); + float g = fx * (0.5f * fx * sign(fx) - 1.0f); + + deltaHeight = deltaHeight + g / w; + + height = targetHeight + deltaHeight; + } + + return PackHeightmap(height); + } + ENDCG + } + + Pass // 3 smooth terrain + { + Name "Smooth Heights" + + CGPROGRAM + #pragma vertex vert + #pragma fragment SmoothHeight + + float4 SmoothHeight(v2f i) : SV_Target + { + float2 brushUV = PaintContextUVToBrushUV(i.pcUV); + float2 heightmapUV = PaintContextUVToHeightmapUV(i.pcUV); + + // out of bounds multiplier + float oob = all(saturate(brushUV) == brushUV) ? 1.0f : 0.0f; + + float height = UnpackHeightmap(tex2D(_MainTex, heightmapUV)); + float brushStrength = BRUSH_STRENGTH * oob * UnpackHeightmap(tex2D(_BrushTex, brushUV)); + + float h = 0.0F; + float xoffset = _MainTex_TexelSize.x; + float yoffset = _MainTex_TexelSize.y; + + // 3*3 filter + h += height; + h += UnpackHeightmap(tex2D(_MainTex, heightmapUV + float2( xoffset, 0 ))); + h += UnpackHeightmap(tex2D(_MainTex, heightmapUV + float2(-xoffset, 0 ))); + h += UnpackHeightmap(tex2D(_MainTex, heightmapUV + float2( xoffset, yoffset))) * 0.75F; + h += UnpackHeightmap(tex2D(_MainTex, heightmapUV + float2(-xoffset, yoffset))) * 0.75F; + h += UnpackHeightmap(tex2D(_MainTex, heightmapUV + float2( xoffset, -yoffset))) * 0.75F; + h += UnpackHeightmap(tex2D(_MainTex, heightmapUV + float2(-xoffset, -yoffset))) * 0.75F; + h += UnpackHeightmap(tex2D(_MainTex, heightmapUV + float2( 0, yoffset))); + h += UnpackHeightmap(tex2D(_MainTex, heightmapUV + float2( 0, -yoffset))); + h /= 8.0F; + + return PackHeightmap(lerp(height, h, brushStrength)); + } + ENDCG + } + + Pass // 4 paint splat alphamap + { + Name "Paint Texture" + + CGPROGRAM + #pragma vertex vert + #pragma fragment PaintSplatAlphamap + + float4 PaintSplatAlphamap(v2f i) : SV_Target + { + float2 brushUV = PaintContextUVToBrushUV(i.pcUV); + + // out of bounds multiplier + float oob = all(saturate(brushUV) == brushUV) ? 1.0f : 0.0f; + + float brushStrength = BRUSH_STRENGTH * oob * UnpackHeightmap(tex2D(_BrushTex, brushUV)); + float alphaMap = tex2D(_MainTex, i.pcUV).r; + return ApplyBrush(alphaMap, brushStrength); + } + + ENDCG + } + + } + Fallback Off + +} diff --git a/DefaultResourcesExtra/TerrainShaders/Utils/TerrainLayerUtils.shader b/DefaultResourcesExtra/TerrainShaders/Utils/TerrainLayerUtils.shader new file mode 100644 index 0000000..5046aa9 --- /dev/null +++ b/DefaultResourcesExtra/TerrainShaders/Utils/TerrainLayerUtils.shader @@ -0,0 +1,122 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +Shader "Hidden/TerrainEngine/TerrainLayerUtils" { + + Properties { _MainTex ("Texture", any) = "" {} } + + SubShader { + + ZTest Always + Cull Off + ZWrite Off + + CGINCLUDE + + #include "UnityCG.cginc" + + float4 _LayerMask; + sampler2D _MainTex; + float4 _MainTex_TexelSize; // 1/width, 1/height, width, height + + ENDCG + + Pass // Select one channel and copy it into R channel + { + Name "Get Terrain Layer Channel" + + BlendOp Max + + CGPROGRAM + #pragma vertex vert + #pragma fragment GetLayer + + struct appdata_t { + float4 vertex : POSITION; + float2 texcoord : TEXCOORD0; + }; + struct v2f { + float4 vertex : SV_POSITION; + float2 texcoord : TEXCOORD0; + }; + v2f vert(appdata_t v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.texcoord = v.texcoord; + return o; + } + float4 GetLayer(v2f i) : SV_Target + { + float4 layerWeights = tex2D(_MainTex, i.texcoord); + return dot(layerWeights, _LayerMask); + } + ENDCG + } + + Pass // Copy the R channel of the input into a specific channel in the output, and renormalize the other channels + { + Name "Set Terrain Layer Channel" + + CGPROGRAM + #pragma vertex vert + #pragma fragment SetLayer + + sampler2D _AlphaMapTexture; // Terrain space texture -- current splatmap we are modifying + sampler2D _OldAlphaMapTexture; // PaintContext space texture -- contains target channel (PaintContext.source) + + sampler2D _OriginalTargetAlphaMap; // Terrain space texture, contains target channel + float4 _OriginalTargetAlphaMask; // mask for above, identifying target channel + + struct appdata_t { + float4 vertex : POSITION; + float2 uvPC : TEXCOORD0; + float2 uvTerrain : TEXCOORD1; + }; + + + struct v2f { + float4 vertex : SV_POSITION; + float2 uvPC : TEXCOORD0; + float2 uvTerrain : TEXCOORD1; + }; + + v2f vert(appdata_t v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.uvPC = v.uvPC; + o.uvTerrain = v.uvTerrain; + return o; + } + + float4 SetLayer(v2f i) : SV_Target + { + // alpha map we are modifying -- _LayerMask tells us which channel is the target (set to 1.0), non-targets are 0.0 + // Note: all four channels can be non-targets, as the target may be in a different alpha map texture + float4 origAlphaMap = tex2D(_AlphaMapTexture, i.uvTerrain); + + // old alpha of the target channel (according to the current terrain tile) + float origTarget = tex2D(_OldAlphaMapTexture, i.uvPC).r; + + // new alpha of the target channel (according to PaintContext destRenderTexture) + float newTarget = tex2D(_MainTex, i.uvPC).r; + + // not allowed to 'erase' a target channel (cannot reduce it's weight) + // this is a requirement to work around edge sync bugs + newTarget = max(newTarget, origTarget); + + float origAlphaOthers = 1 - origTarget; + if (origAlphaOthers > 0.001f) + { + float nonTargetRenormalizeScale = saturate((1.0f - newTarget) / origAlphaOthers); + float4 othersNormalized = origAlphaMap * saturate(1.0f - _LayerMask) * nonTargetRenormalizeScale; + return othersNormalized + newTarget * _LayerMask; + } + return _LayerMask; + } + ENDCG + } + + } + Fallback Off +} diff --git a/DefaultResourcesExtra/VideoDecode.shader b/DefaultResourcesExtra/VideoDecode.shader index 118c2b8..c4161ab 100644 --- a/DefaultResourcesExtra/VideoDecode.shader +++ b/DefaultResourcesExtra/VideoDecode.shader @@ -94,16 +94,17 @@ Shader "Hidden/VideoDecode" fixed4 fragmentNV12RGBOne(v2f i) : SV_Target { - float3 yCbCr = float3( tex2D(_MainTex, i.texcoord).a - 0.0625, - tex2D(_SecondTex, i.texcoord).r - 0.5, - tex2D(_SecondTex, i.texcoord).g - 0.5 ); - - fixed4 result = fixed4( dot(float3(1.1644f, 0.0f, 1.7927f), yCbCr), - dot(float3(1.1644f, -0.2133f, -0.5329f), yCbCr), - dot(float3(1.1644f, 2.1124f, 0.0f), yCbCr), - 1.0f ); - - return AdjustForColorSpace(result); + fixed y = tex2D(_MainTex, i.texcoord).a; + fixed2 uv = tex2D(_SecondTex, i.texcoord).rg; + fixed u = uv.x; + fixed v = uv.y; + fixed y1 = 1.15625 * y; + return AdjustForColorSpace(fixed4( + y1 + 1.59375 * v - 0.87254, + y1 - 0.390625 * u - 0.8125 * v + 0.53137, + y1 + 1.984375 * u - 1.06862, + 1.0f + )); } fixed4 fragmentNV12RGBA(v2f i) : SV_Target diff --git a/Editor/StandardParticlesShaderGUI.cs b/Editor/StandardParticlesShaderGUI.cs index 87b7c2d..e258bcd 100644 --- a/Editor/StandardParticlesShaderGUI.cs +++ b/Editor/StandardParticlesShaderGUI.cs @@ -52,40 +52,41 @@ private static class Styles public static GUIContent colorMode = EditorGUIUtility.TrTextContent("Color Mode", "Determines the blending mode between the particle color and the texture albedo."); public static GUIContent[] colorNames = Array.ConvertAll(Enum.GetNames(typeof(ColorMode)), item => new GUIContent(item)); - public static GUIContent flipbookMode = EditorGUIUtility.TrTextContent("Flip-Book Mode", "Determines the blending mode used for animated texture sheets."); - public static GUIContent[] flipbookNames = Array.ConvertAll(Enum.GetNames(typeof(FlipbookMode)), item => new GUIContent(item)); - + public static GUIContent flipbookBlending = EditorGUIUtility.TrTextContent("Flip-Book Frame Blending", "Enables blending between the frames of animated texture sheets."); public static GUIContent twoSidedEnabled = EditorGUIUtility.TrTextContent("Two Sided", "Render both front and back faces of the particle geometry."); - public static GUIContent distortionEnabled = EditorGUIUtility.TrTextContent("Enable Distortion", "Use a grab pass and normal map to simulate refraction."); + public static GUIContent distortionEnabled = EditorGUIUtility.TrTextContent("Distortion", "Use a grab pass and normal map to simulate refraction."); public static GUIContent distortionStrengthText = EditorGUIUtility.TrTextContent("Strength", "Distortion Strength."); public static GUIContent distortionBlendText = EditorGUIUtility.TrTextContent("Blend", "Weighting between albedo and grab pass."); - public static GUIContent softParticlesEnabled = EditorGUIUtility.TrTextContent("Enable Soft Particles", "Fade out particle geometry when it gets close to the surface of objects written into the depth buffer."); + public static GUIContent softParticlesEnabled = EditorGUIUtility.TrTextContent("Soft Particles", "Fade out particle geometry when it gets close to the surface of objects written into the depth buffer."); public static GUIContent softParticlesNearFadeDistanceText = EditorGUIUtility.TrTextContent("Near fade", "Soft Particles near fade distance."); public static GUIContent softParticlesFarFadeDistanceText = EditorGUIUtility.TrTextContent("Far fade", "Soft Particles far fade distance."); - public static GUIContent cameraFadingEnabled = EditorGUIUtility.TrTextContent("Enable Camera Fading", "Fade out particle geometry when it gets close to the camera."); + public static GUIContent cameraFadingEnabled = EditorGUIUtility.TrTextContent("Camera Fading", "Fade out particle geometry when it gets close to the camera."); public static GUIContent cameraNearFadeDistanceText = EditorGUIUtility.TrTextContent("Near fade", "Camera near fade distance."); public static GUIContent cameraFarFadeDistanceText = EditorGUIUtility.TrTextContent("Far fade", "Camera far fade distance."); public static GUIContent emissionEnabled = EditorGUIUtility.TrTextContent("Emission"); - public static string blendingOptionsText = "Blending Options"; - public static string mainOptionsText = "Main Options"; - public static string mapsOptionsText = "Maps"; - public static string requiredVertexStreamsText = "Required Vertex Streams"; - - public static string streamPositionText = "Position (POSITION.xyz)"; - public static string streamNormalText = "Normal (NORMAL.xyz)"; - public static string streamColorText = "Color (COLOR.xyzw)"; - public static string streamUVText = "UV (TEXCOORD0.xy)"; - public static string streamUV2Text = "UV2 (TEXCOORD0.zw)"; - public static string streamAnimBlendText = "AnimBlend (TEXCOORD1.x)"; - public static string streamAnimFrameText = "AnimFrame (INSTANCED0.x)"; - public static string streamTangentText = "Tangent (TANGENT.xyzw)"; + public static GUIContent blendingOptionsText = EditorGUIUtility.TrTextContent("Blending Options"); + public static GUIContent mainOptionsText = EditorGUIUtility.TrTextContent("Main Options"); + public static GUIContent mapsOptionsText = EditorGUIUtility.TrTextContent("Maps"); + public static GUIContent requiredVertexStreamsText = EditorGUIUtility.TrTextContent("Required Vertex Streams"); + + public static GUIContent streamPositionText = EditorGUIUtility.TrTextContent("Position (POSITION.xyz)"); + public static GUIContent streamNormalText = EditorGUIUtility.TrTextContent("Normal (NORMAL.xyz)"); + public static GUIContent streamColorText = EditorGUIUtility.TrTextContent("Color (COLOR.xyzw)"); + public static GUIContent streamColorInstancedText = EditorGUIUtility.TrTextContent("Color (INSTANCED0.xyzw)"); + public static GUIContent streamUVText = EditorGUIUtility.TrTextContent("UV (TEXCOORD0.xy)"); + public static GUIContent streamUV2Text = EditorGUIUtility.TrTextContent("UV2 (TEXCOORD0.zw)"); + public static GUIContent streamAnimBlendText = EditorGUIUtility.TrTextContent("AnimBlend (TEXCOORD1.x)"); + public static GUIContent streamAnimFrameText = EditorGUIUtility.TrTextContent("AnimFrame (INSTANCED1.x)"); + public static GUIContent streamTangentText = EditorGUIUtility.TrTextContent("Tangent (TANGENT.xyzw)"); public static GUIContent streamApplyToAllSystemsText = EditorGUIUtility.TrTextContent("Apply to Systems", "Apply the vertex stream layout to all Particle Systems using this material"); + + public static string undoApplyCustomVertexStreams = L10n.Tr("Apply custom vertex streams from material"); } MaterialProperty blendMode = null; @@ -182,7 +183,7 @@ public void ShaderPropertiesGUI(Material material) EditorGUILayout.Space(); GUILayout.Label(Styles.mainOptionsText, EditorStyles.boldLabel); - FlipbookModePopup(); + FlipbookBlendingPopup(); TwoSidedPopup(material); FadingPopup(material); DistortionPopup(material); @@ -294,17 +295,17 @@ void ColorModePopup() } } - void FlipbookModePopup() + void FlipbookBlendingPopup() { EditorGUI.showMixedValue = flipbookMode.hasMixedValue; - var mode = (FlipbookMode)flipbookMode.floatValue; + var enabled = (flipbookMode.floatValue == (float)FlipbookMode.Blended); EditorGUI.BeginChangeCheck(); - mode = (FlipbookMode)EditorGUILayout.Popup(Styles.flipbookMode, (int)mode, Styles.flipbookNames); + enabled = EditorGUILayout.Toggle(Styles.flipbookBlending, enabled); if (EditorGUI.EndChangeCheck()) { m_MaterialEditor.RegisterPropertyChangeUndo("Flip-Book Mode"); - flipbookMode.floatValue = (float)mode; + flipbookMode.floatValue = enabled ? (float)FlipbookMode.Blended : (float)FlipbookMode.Simple; } EditorGUI.showMixedValue = false; @@ -476,15 +477,21 @@ void DoVertexStreamsArea(Material material) // Display list of streams required to make this shader work bool useLighting = (material.GetFloat("_LightingEnabled") > 0.0f); bool useFlipbookBlending = (material.GetFloat("_FlipbookMode") > 0.0f); - bool useTangents = material.GetTexture("_BumpMap") && useLighting; + bool useTangents = useLighting && material.GetTexture("_BumpMap"); + bool useGPUInstancing = ShaderUtil.HasProceduralInstancing(material.shader); + if (useGPUInstancing && m_RenderersUsingThisMaterial.Count > 0) + { + if (!m_RenderersUsingThisMaterial[0].enableGPUInstancing) + useGPUInstancing = false; + } GUILayout.Label(Styles.streamPositionText, EditorStyles.label); if (useLighting) GUILayout.Label(Styles.streamNormalText, EditorStyles.label); - GUILayout.Label(Styles.streamColorText, EditorStyles.label); + GUILayout.Label(useGPUInstancing ? Styles.streamColorInstancedText : Styles.streamColorText, EditorStyles.label); GUILayout.Label(Styles.streamUVText, EditorStyles.label); if (useTangents) @@ -528,6 +535,8 @@ void DoVertexStreamsArea(Material material) // Set the streams on all systems using this material if (GUILayout.Button(Styles.streamApplyToAllSystemsText, EditorStyles.miniButton, GUILayout.ExpandWidth(false))) { + Undo.RecordObjects(m_RenderersUsingThisMaterial.Where(r => r != null).ToArray(), Styles.undoApplyCustomVertexStreams); + foreach (ParticleSystemRenderer renderer in m_RenderersUsingThisMaterial) { if (renderer != null) @@ -708,9 +717,9 @@ void SetMaterialKeywords(Material material) // Note: keywords must be based on Material value not on MaterialProperty due to multi-edit & material animation // (MaterialProperty value might come from renderer material property block) - bool useDistortion = (material.GetFloat("_DistortionEnabled") > 0.0f) && !hasZWrite; - SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap") && (useLighting || useDistortion)); - SetKeyword(material, "_METALLICGLOSSMAP", (material.GetTexture("_MetallicGlossMap") != null) && useLighting); + bool useDistortion = !hasZWrite && (material.GetFloat("_DistortionEnabled") > 0.0f); + SetKeyword(material, "_NORMALMAP", (useLighting || useDistortion) && material.GetTexture("_BumpMap")); + SetKeyword(material, "_METALLICGLOSSMAP", useLighting && material.GetTexture("_MetallicGlossMap")); material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.None; SetKeyword(material, "_EMISSION", material.GetFloat("_EmissionEnabled") > 0.0f); diff --git a/Editor/StandardRoughnessShaderGUI.cs b/Editor/StandardRoughnessShaderGUI.cs deleted file mode 100644 index 3eb1a8c..0000000 --- a/Editor/StandardRoughnessShaderGUI.cs +++ /dev/null @@ -1,330 +0,0 @@ -// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) - -using System; -using UnityEngine; - -namespace UnityEditor -{ - internal class StandardRoughnessShaderGUI : ShaderGUI - { - public enum BlendMode - { - Opaque, - Cutout, - Fade, // Old school alpha-blending mode, fresnel does not affect amount of transparency - Transparent // Physically plausible transparency mode, implemented as alpha pre-multiply - } - - private static class Styles - { - public static GUIContent uvSetLabel = EditorGUIUtility.TrTextContent("UV Set"); - - public static GUIContent albedoText = EditorGUIUtility.TrTextContent("Albedo", "Albedo (RGB) and Transparency (A)"); - public static GUIContent alphaCutoffText = EditorGUIUtility.TrTextContent("Alpha Cutoff", "Threshold for alpha cutoff"); - public static GUIContent metallicMapText = EditorGUIUtility.TrTextContent("Metallic", "Metallic (R) and Smoothness (A)"); - public static GUIContent roughnessText = EditorGUIUtility.TrTextContent("Roughness", "Roughness value"); - public static GUIContent highlightsText = EditorGUIUtility.TrTextContent("Specular Highlights", "Specular Highlights"); - public static GUIContent reflectionsText = EditorGUIUtility.TrTextContent("Reflections", "Glossy Reflections"); - public static GUIContent normalMapText = EditorGUIUtility.TrTextContent("Normal Map", "Normal Map"); - public static GUIContent heightMapText = EditorGUIUtility.TrTextContent("Height Map", "Height Map (G)"); - public static GUIContent occlusionText = EditorGUIUtility.TrTextContent("Occlusion", "Occlusion (G)"); - public static GUIContent emissionText = EditorGUIUtility.TrTextContent("Color", "Emission (RGB)"); - public static GUIContent detailMaskText = EditorGUIUtility.TrTextContent("Detail Mask", "Mask for Secondary Maps (A)"); - public static GUIContent detailAlbedoText = EditorGUIUtility.TrTextContent("Detail Albedo x2", "Albedo (RGB) multiplied by 2"); - public static GUIContent detailNormalMapText = EditorGUIUtility.TrTextContent("Normal Map", "Normal Map"); - - public static string primaryMapsText = "Main Maps"; - public static string secondaryMapsText = "Secondary Maps"; - public static string forwardText = "Forward Rendering Options"; - public static string renderingMode = "Rendering Mode"; - public static string advancedText = "Advanced Options"; - public static readonly string[] blendNames = Enum.GetNames(typeof(BlendMode)); - } - - MaterialProperty blendMode = null; - MaterialProperty albedoMap = null; - MaterialProperty albedoColor = null; - MaterialProperty alphaCutoff = null; - MaterialProperty metallicMap = null; - MaterialProperty metallic = null; - MaterialProperty roughness = null; - MaterialProperty roughnessMap = null; - MaterialProperty highlights = null; - MaterialProperty reflections = null; - MaterialProperty bumpScale = null; - MaterialProperty bumpMap = null; - MaterialProperty occlusionStrength = null; - MaterialProperty occlusionMap = null; - MaterialProperty heigtMapScale = null; - MaterialProperty heightMap = null; - MaterialProperty emissionColorForRendering = null; - MaterialProperty emissionMap = null; - MaterialProperty detailMask = null; - MaterialProperty detailAlbedoMap = null; - MaterialProperty detailNormalMapScale = null; - MaterialProperty detailNormalMap = null; - MaterialProperty uvSetSecondary = null; - - MaterialEditor m_MaterialEditor; - - bool m_FirstTimeApply = true; - - public void FindProperties(MaterialProperty[] props) - { - blendMode = FindProperty("_Mode", props); - albedoMap = FindProperty("_MainTex", props); - albedoColor = FindProperty("_Color", props); - alphaCutoff = FindProperty("_Cutoff", props); - metallicMap = FindProperty("_MetallicGlossMap", props, false); - metallic = FindProperty("_Metallic", props, false); - roughness = FindProperty("_Glossiness", props); - roughnessMap = FindProperty("_SpecGlossMap", props); - highlights = FindProperty("_SpecularHighlights", props, false); - reflections = FindProperty("_GlossyReflections", props, false); - bumpScale = FindProperty("_BumpScale", props); - bumpMap = FindProperty("_BumpMap", props); - heigtMapScale = FindProperty("_Parallax", props); - heightMap = FindProperty("_ParallaxMap", props); - occlusionStrength = FindProperty("_OcclusionStrength", props); - occlusionMap = FindProperty("_OcclusionMap", props); - emissionColorForRendering = FindProperty("_EmissionColor", props); - emissionMap = FindProperty("_EmissionMap", props); - detailMask = FindProperty("_DetailMask", props); - detailAlbedoMap = FindProperty("_DetailAlbedoMap", props); - detailNormalMapScale = FindProperty("_DetailNormalMapScale", props); - detailNormalMap = FindProperty("_DetailNormalMap", props); - uvSetSecondary = FindProperty("_UVSec", props); - } - - public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props) - { - FindProperties(props); // MaterialProperties can be animated so we do not cache them but fetch them every event to ensure animated values are updated correctly - m_MaterialEditor = materialEditor; - Material material = materialEditor.target as Material; - - // Make sure that needed setup (ie keywords/renderqueue) are set up if we're switching some existing - // material to a standard shader. - // Do this before any GUI code has been issued to prevent layout issues in subsequent GUILayout statements (case 780071) - if (m_FirstTimeApply) - { - MaterialChanged(material); - m_FirstTimeApply = false; - } - - ShaderPropertiesGUI(material); - } - - public void ShaderPropertiesGUI(Material material) - { - // Use default labelWidth - EditorGUIUtility.labelWidth = 0f; - - // Detect any changes to the material - EditorGUI.BeginChangeCheck(); - { - BlendModePopup(); - - // Primary properties - GUILayout.Label(Styles.primaryMapsText, EditorStyles.boldLabel); - DoAlbedoArea(material); - m_MaterialEditor.TexturePropertySingleLine(Styles.metallicMapText, metallicMap, metallicMap.textureValue != null ? null : metallic); - m_MaterialEditor.TexturePropertySingleLine(Styles.roughnessText, roughnessMap, roughnessMap.textureValue != null ? null : roughness); - m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, bumpMap, bumpMap.textureValue != null ? bumpScale : null); - m_MaterialEditor.TexturePropertySingleLine(Styles.heightMapText, heightMap, heightMap.textureValue != null ? heigtMapScale : null); - m_MaterialEditor.TexturePropertySingleLine(Styles.occlusionText, occlusionMap, occlusionMap.textureValue != null ? occlusionStrength : null); - m_MaterialEditor.TexturePropertySingleLine(Styles.detailMaskText, detailMask); - DoEmissionArea(material); - EditorGUI.BeginChangeCheck(); - m_MaterialEditor.TextureScaleOffsetProperty(albedoMap); - if (EditorGUI.EndChangeCheck()) - emissionMap.textureScaleAndOffset = albedoMap.textureScaleAndOffset; // Apply the main texture scale and offset to the emission texture as well, for Enlighten's sake - - EditorGUILayout.Space(); - - // Secondary properties - GUILayout.Label(Styles.secondaryMapsText, EditorStyles.boldLabel); - m_MaterialEditor.TexturePropertySingleLine(Styles.detailAlbedoText, detailAlbedoMap); - m_MaterialEditor.TexturePropertySingleLine(Styles.detailNormalMapText, detailNormalMap, detailNormalMapScale); - m_MaterialEditor.TextureScaleOffsetProperty(detailAlbedoMap); - m_MaterialEditor.ShaderProperty(uvSetSecondary, Styles.uvSetLabel.text); - - // Third properties - GUILayout.Label(Styles.forwardText, EditorStyles.boldLabel); - if (highlights != null) - m_MaterialEditor.ShaderProperty(highlights, Styles.highlightsText); - if (reflections != null) - m_MaterialEditor.ShaderProperty(reflections, Styles.reflectionsText); - } - if (EditorGUI.EndChangeCheck()) - { - foreach (var obj in blendMode.targets) - MaterialChanged((Material)obj); - } - - EditorGUILayout.Space(); - - // NB renderqueue editor is not shown on purpose: we want to override it based on blend mode - GUILayout.Label(Styles.advancedText, EditorStyles.boldLabel); - m_MaterialEditor.EnableInstancingField(); - m_MaterialEditor.DoubleSidedGIField(); - } - - public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader) - { - // _Emission property is lost after assigning Standard shader to the material - // thus transfer it before assigning the new shader - if (material.HasProperty("_Emission")) - { - material.SetColor("_EmissionColor", material.GetColor("_Emission")); - } - - base.AssignNewShaderToMaterial(material, oldShader, newShader); - - if (oldShader == null || !oldShader.name.Contains("Legacy Shaders/")) - { - SetupMaterialWithBlendMode(material, (BlendMode)material.GetFloat("_Mode")); - return; - } - - BlendMode blendMode = BlendMode.Opaque; - if (oldShader.name.Contains("/Transparent/Cutout/")) - { - blendMode = BlendMode.Cutout; - } - else if (oldShader.name.Contains("/Transparent/")) - { - // NOTE: legacy shaders did not provide physically based transparency - // therefore Fade mode - blendMode = BlendMode.Fade; - } - material.SetFloat("_Mode", (float)blendMode); - - MaterialChanged(material); - } - - void BlendModePopup() - { - EditorGUI.showMixedValue = blendMode.hasMixedValue; - var mode = (BlendMode)blendMode.floatValue; - - EditorGUI.BeginChangeCheck(); - mode = (BlendMode)EditorGUILayout.Popup(Styles.renderingMode, (int)mode, Styles.blendNames); - if (EditorGUI.EndChangeCheck()) - { - m_MaterialEditor.RegisterPropertyChangeUndo("Rendering Mode"); - blendMode.floatValue = (float)mode; - } - - EditorGUI.showMixedValue = false; - } - - void DoAlbedoArea(Material material) - { - m_MaterialEditor.TexturePropertySingleLine(Styles.albedoText, albedoMap, albedoColor); - if (((BlendMode)material.GetFloat("_Mode") == BlendMode.Cutout)) - { - m_MaterialEditor.ShaderProperty(alphaCutoff, Styles.alphaCutoffText.text, MaterialEditor.kMiniTextureFieldLabelIndentLevel + 1); - } - } - - void DoEmissionArea(Material material) - { - // Emission for GI? - if (m_MaterialEditor.EmissionEnabledProperty()) - { - bool hadEmissionTexture = emissionMap.textureValue != null; - - // Texture and HDR color controls - m_MaterialEditor.TexturePropertyWithHDRColor(Styles.emissionText, emissionMap, emissionColorForRendering, false); - - // If texture was assigned and color was black set color to white - float brightness = emissionColorForRendering.colorValue.maxColorComponent; - if (emissionMap.textureValue != null && !hadEmissionTexture && brightness <= 0f) - emissionColorForRendering.colorValue = Color.white; - - // change the GI flag and fix it up with emissive as black if necessary - m_MaterialEditor.LightmapEmissionFlagsProperty(MaterialEditor.kMiniTextureFieldLabelIndentLevel, true); - } - } - - public static void SetupMaterialWithBlendMode(Material material, BlendMode blendMode) - { - switch (blendMode) - { - case BlendMode.Opaque: - material.SetOverrideTag("RenderType", ""); - material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); - material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); - material.SetInt("_ZWrite", 1); - material.DisableKeyword("_ALPHATEST_ON"); - material.DisableKeyword("_ALPHABLEND_ON"); - material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); - material.renderQueue = -1; - break; - case BlendMode.Cutout: - material.SetOverrideTag("RenderType", "TransparentCutout"); - material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); - material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); - material.SetInt("_ZWrite", 1); - material.EnableKeyword("_ALPHATEST_ON"); - material.DisableKeyword("_ALPHABLEND_ON"); - material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); - material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest; - break; - case BlendMode.Fade: - material.SetOverrideTag("RenderType", "Transparent"); - material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); - material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); - material.SetInt("_ZWrite", 0); - material.DisableKeyword("_ALPHATEST_ON"); - material.EnableKeyword("_ALPHABLEND_ON"); - material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); - material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; - break; - case BlendMode.Transparent: - material.SetOverrideTag("RenderType", "Transparent"); - material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); - material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); - material.SetInt("_ZWrite", 0); - material.DisableKeyword("_ALPHATEST_ON"); - material.DisableKeyword("_ALPHABLEND_ON"); - material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); - material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; - break; - } - } - - static void SetMaterialKeywords(Material material) - { - // Note: keywords must be based on Material value not on MaterialProperty due to multi-edit & material animation - // (MaterialProperty value might come from renderer material property block) - SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap") || material.GetTexture("_DetailNormalMap")); - SetKeyword(material, "_SPECGLOSSMAP", material.GetTexture("_SpecGlossMap")); - SetKeyword(material, "_METALLICGLOSSMAP", material.GetTexture("_MetallicGlossMap")); - SetKeyword(material, "_PARALLAXMAP", material.GetTexture("_ParallaxMap")); - SetKeyword(material, "_DETAIL_MULX2", material.GetTexture("_DetailAlbedoMap") || material.GetTexture("_DetailNormalMap")); - - // A material's GI flag internally keeps track of whether emission is enabled at all, it's enabled but has no effect - // or is enabled and may be modified at runtime. This state depends on the values of the current flag and emissive color. - // The fixup routine makes sure that the material is in the correct state if/when changes are made to the mode or color. - MaterialEditor.FixupEmissiveFlag(material); - bool shouldEmissionBeEnabled = (material.globalIlluminationFlags & MaterialGlobalIlluminationFlags.EmissiveIsBlack) == 0; - SetKeyword(material, "_EMISSION", shouldEmissionBeEnabled); - } - - static void MaterialChanged(Material material) - { - SetupMaterialWithBlendMode(material, (BlendMode)material.GetFloat("_Mode")); - - SetMaterialKeywords(material); - } - - static void SetKeyword(Material m, string keyword, bool state) - { - if (state) - m.EnableKeyword(keyword); - else - m.DisableKeyword(keyword); - } - } -} // namespace UnityEditor diff --git a/Editor/StandardShaderGUI.cs b/Editor/StandardShaderGUI.cs index a4e5ade..7b70d3f 100644 --- a/Editor/StandardShaderGUI.cs +++ b/Editor/StandardShaderGUI.cs @@ -262,8 +262,8 @@ void DoNormalArea() m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, bumpMap, bumpMap.textureValue != null ? bumpScale : null); if (bumpScale.floatValue != 1 && UnityEditorInternal.InternalEditorUtility.IsMobilePlatform(EditorUserBuildSettings.activeBuildTarget)) if (m_MaterialEditor.HelpBoxWithButton( - EditorGUIUtility.TrTextContent("Bump scale is not supported on mobile platforms"), - EditorGUIUtility.TrTextContent("Fix Now"))) + EditorGUIUtility.TrTextContent("Bump scale is not supported on mobile platforms"), + EditorGUIUtility.TrTextContent("Fix Now"))) { bumpScale.floatValue = 1; } diff --git a/README.md b/README.md index 81bf7c5..9dd817d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Unity Built in Shaders -Current repository version: 2018.2.10f1 +Current repository version: 2018.3.13f1 An unofficial repo for Unity Built-in Shaders.