浏览代码

Merge pull request #1066 from Unity-Technologies/LWParticles

Added a shader for lit particles with SimpleLighting...
/main
GitHub 7 年前
当前提交
8ae2aaf6
共有 8 个文件被更改,包括 272 次插入106 次删除
  1. 38
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Core.hlsl
  2. 23
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/LightweightPassLit.hlsl
  3. 104
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Particles.hlsl
  4. 6
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Shadows.hlsl
  5. 18
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardParticles.shader
  6. 50
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardParticlesUnlit.shader
  7. 130
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardParticlesSimpleLighting.shader
  8. 9
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardParticlesSimpleLighting.shader.meta

38
ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Core.hlsl


#define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) (coord)
#endif
void AlphaDiscard(half alpha, half cutoff)
void AlphaDiscard(half alpha, half cutoff, half offset = 0.0h)
clip(alpha - cutoff);
clip(alpha - cutoff + offset);
#endif
}

binormalWS = cross(normalWS, tangentWS) * sign;
}
half3 TangentToWorldNormal(half3 normalTangent, half3 tangent, half3 binormal, half3 normal)
half3 FragmentNormalWS(half3 normal)
half3x3 tangentToWorld = half3x3(tangent, binormal, normal);
return mul(normalTangent, tangentToWorld);
// World normal is already normalized in vertex. Small acceptable error to save ALU.
return normal;
return normalize(mul(normalTangent, tangentToWorld));
return normalize(normal);
}
half3 FragmentViewDirWS(half3 viewDir)
{
#if !SHADER_HINT_NICE_QUALITY
// View direction is already normalized in vertex. Small acceptable error to save ALU.
return viewDir;
#else
return SafeNormalize(viewDir);
#endif
}
half3 VertexViewDirWS(half3 viewDir)
{
#if !SHADER_HINT_NICE_QUALITY
// Normalize in vertex and avoid renormalizing it in frag to save ALU.
return SafeNormalize(viewDir);
#else
return viewDir;
#endif
}
half3 TangentToWorldNormal(half3 normalTangent, half3 tangent, half3 binormal, half3 normal)
{
half3x3 tangentToWorld = half3x3(tangent, binormal, normal);
return FragmentNormalWS(mul(normalTangent, tangentToWorld));
}
// TODO: A similar function should be already available in SRP lib on master. Use that instead

23
ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/LightweightPassLit.hlsl


inputData.normalWS = TangentToWorldNormal(normalTS, IN.tangent.xyz, IN.binormal.xyz, IN.normal.xyz);
#else
half3 viewDir = IN.viewDir;
#if !SHADER_HINT_NICE_QUALITY
// World normal is already normalized in vertex. Small acceptable error to save ALU.
inputData.normalWS = IN.normal;
#else
inputData.normalWS = normalize(IN.normal);
#endif
#endif
#if SHADER_HINT_NICE_QUALITY
inputData.viewDirectionWS = SafeNormalize(viewDir);
#else
// View direction is already normalized in vertex. Small acceptable error to save ALU.
inputData.viewDirectionWS = viewDir;
inputData.normalWS = FragmentNormalWS(IN.normal);
inputData.viewDirectionWS = FragmentViewDirWS(viewDir);
inputData.fogCoord = IN.fogFactorAndVertexLight.x;
inputData.vertexLighting = IN.fogFactorAndVertexLight.yzw;
inputData.bakedGI = SAMPLE_GI(IN.lightmapUV, IN.vertexSH, inputData.normalWS);

o.posWSShininess.w = _Shininess * 128.0;
o.clipPos = TransformWorldToHClip(o.posWSShininess.xyz);
half3 viewDir = GetCameraPositionWS() - o.posWSShininess.xyz;
#if !SHADER_HINT_NICE_QUALITY
// Normalize in vertex and avoid renormalizing it in frag to save ALU.
viewDir = SafeNormalize(viewDir);
#endif
half3 viewDir = VertexViewDirWS(GetCameraPositionWS() - o.posWSShininess.xyz);
#ifdef _NORMALMAP
o.normal.w = viewDir.x;

104
ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Particles.hlsl


float4 projectedPosition : TEXCOORD5;
#endif
float4 posWS : TEXCOORD6; // xyz: position; w = fogFactor;
half4 viewDirShininess : TEXCOORD7; // xyz: viewDir; w = shininess
float4 clipPos : SV_POSITION;
};

return color;
}
void InitializeSurfaceData(VertexOutputLit IN, out SurfaceData surfaceData)
half3 NormalTS(VertexOutputLit IN)
{
#if defined(_NORMALMAP)
#if BUMP_SCALE_NOT_SUPPORTED
return UnpackNormal(readTexture(_BumpMap, sampler_BumpMap, IN));
#else
return UnpackNormalScale(readTexture(_BumpMap, sampler_BumpMap, IN), _BumpScale);
#endif
#else
return half3(0.0h, 0.0h, 1.0h);
#endif
}
half3 Emission(VertexOutputLit IN)
{
#if defined(_EMISSION)
return readTexture(_EmissionMap, sampler_EmissionMap, IN).rgb * _EmissionColor.rgb;
#else
return half3(0.0h, 0.0h, 0.0h);
#endif
}
half4 Albedo(VertexOutputLit IN)
{
half4 albedo = readTexture(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), IN) * IN.color;

fragCameraFading(IN);
#if defined(_METALLICGLOSSMAP)
half2 metallicGloss = readTexture(_MetallicGlossMap, sampler_MetallicGlossMap, IN).ra * half2(1.0, _Glossiness);
return albedo;
}
half4 SpecularGloss(VertexOutputLit IN, half alpha)
{
half4 specularGloss = half4(0.0h, 0.0h, 0.0h, 1.0h);
#ifdef _SPECGLOSSMAP
specularGloss = readTexture(_SpecGlossMap, sampler_SpecGlossMap, IN);
#elif defined(_SPECULAR_COLOR)
specularGloss = _SpecColor;
#endif
#ifdef _GLOSSINESS_FROM_BASE_ALPHA
specularGloss.a = alpha;
#endif
return specularGloss;
}
half AlphaBlendAndTest(half alpha)
{
#if defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_ON) || defined(_ALPHAOVERLAY_ON)
half result = alpha;
half2 metallicGloss = half2(_Metallic, _Glossiness);
half result = 1.0h;
AlphaDiscard(result, _Cutoff, 0.0001h);
#if defined(_NORMALMAP)
half3 normalTS = normalize(UnpackNormalScale(readTexture(_BumpMap, sampler_BumpMap, IN), _BumpScale));
return result;
}
half3 AlphaModulate(half3 albedo, half alpha)
{
#if defined(_ALPHAMODULATE_ON)
return lerp(half3(1.0h, 1.0h, 1.0h), albedo, alpha);
half3 normalTS = float3(0, 0, 1);
return albedo;
}
#if defined(_EMISSION)
half3 emission = readTexture(_EmissionMap, sampler_EmissionMap, IN).rgb;
void InitializeSurfaceData(VertexOutputLit IN, out SurfaceData surfaceData)
{
half4 albedo = Albedo(IN);
#if defined(_METALLICGLOSSMAP)
half2 metallicGloss = readTexture(_MetallicGlossMap, sampler_MetallicGlossMap, IN).ra * half2(1.0, _Glossiness);
half3 emission = 0;
half2 metallicGloss = half2(_Metallic, _Glossiness);
half3 normalTS = NormalTS(IN);
half3 emission = Emission(IN);
surfaceData.specular = half3(0, 0, 0);
surfaceData.specular = half3(0.0h, 0.0h, 0.0h);
surfaceData.emission = emission * _EmissionColor.rgb;
surfaceData.emission = emission;
#if defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_ON) || defined(_ALPHAOVERLAY_ON)
surfaceData.alpha = albedo.a;
#else
surfaceData.alpha = 1;
#endif
#if defined(_ALPHAMODULATE_ON)
surfaceData.albedo = lerp(half3(1.0, 1.0, 1.0), surfaceData.albedo, surfaceData.alpha);
#endif
#if defined(_ALPHATEST_ON)
clip(surfaceData.alpha - _Cutoff + 0.0001);
#endif
surfaceData.alpha = AlphaBlendAndTest(albedo.a);
surfaceData.albedo = AlphaModulate(surfaceData.albedo, surfaceData.alpha);
}
void InitializeInputData(VertexOutputLit IN, half3 normalTS, out InputData input)

#if _NORMALMAP
input.normalWS = TangentToWorldNormal(normalTS, IN.tangent, IN.binormal, IN.normal);
#else
input.normalWS = normalize(IN.normal);
input.normalWS = FragmentNormalWS(IN.normal);
input.viewDirectionWS = SafeNormalize(GetCameraPositionWS() - input.positionWS);
input.viewDirectionWS = FragmentViewDirWS(IN.viewDirShininess.xyz);
input.vertexLighting = half3(0, 0, 0);
input.bakedGI = half3(0, 0, 0);
input.vertexLighting = half3(0.0h, 0.0h, 0.0h);
input.bakedGI = half3(0.0h, 0.0h, 0.0h);
}

6
ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Shadows.hlsl


float4 ComputeScreenSpaceShadowCoords(float3 positionWS)
{
#ifdef _SHADOWS_CASCADE
#ifdef _SHADOWS_CASCADE
half cascadeIndex = ComputeCascadeIndex(positionWS);
return mul(_WorldToShadow[cascadeIndex], float4(positionWS, 1.0));
#else

half RealtimeShadowAttenuation(float4 shadowCoord)
{
#if NO_SHADOWS
return 1.0h;
#else
#endif
}
#endif

18
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardParticles.shader


// Only directional light is supported for lit particles
// No shadow
// No distortion
Shader "LightweightPipeline/Particles/Standard"
Shader "LightweightPipeline/Particles/Standard (Physically Based)"
{
Properties
{

#pragma vertex ParticlesLitVertex
#pragma fragment ParticlesLitFragment
#pragma multi_compile __ SOFTPARTICLES_ON
#pragma target 3.5
#pragma target 2.0
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON _ALPHAMODULATE_ON
#pragma shader_feature _METALLICGLOSSMAP

#pragma shader_feature _REQUIRE_UV2
#define NO_SHADOWS 1
#include "LWRP/ShaderLibrary/Particles.hlsl"
#include "LWRP/ShaderLibrary/Lighting.hlsl"

VertexOutputLit o;
#if _NORMALMAP
OutputTangentToWorld(v.tangent, v.normal, o.tangent, o.binormal, o.normal);
#else
o.normal = normalize(TransformObjectToWorldNormal(v.normal));
#endif
o.color = v.color;
OUTPUT_NORMAL(v, o);
o.color = v.color * _Color;
o.viewDirShininess.xyz = VertexViewDirWS(GetCameraPositionWS() - o.posWS.xyz);
vertTexcoord(v, o);
vertFading(o, o.posWS, o.clipPos);
return o;

}
}
Fallback "LightweightPipeline/Particles/Standard Unlit"
Fallback "LightweightPipeline/Particles/Standard (Simple Lighting)"
CustomEditor "LightweightStandardParticlesShaderGUI"
}

50
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardParticlesUnlit.shader


_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
_BumpScale("Scale", Float) = 1.0
_BumpMap("Normal Map", 2D) = "bump" {}
_EmissionColor("Color", Color) = (0,0,0)
_EmissionMap("Emission", 2D) = "white" {}

#pragma prefer_hlslcc gles
#pragma multi_compile __ SOFTPARTICLES_ON
#pragma multi_compile_fog
#pragma target 2.5
#pragma target 2.0
#pragma shader_feature _NORMALMAP
#pragma shader_feature _EMISSION
#pragma shader_feature _FADING_ON
#pragma shader_feature _REQUIRE_UV2

o.posWS.xyz = TransformObjectToWorld(v.vertex.xyz);
o.posWS.w = ComputeFogFactor(o.clipPos.z);
o.clipPos = TransformWorldToHClip(o.posWS.xyz);
o.color = v.color;
o.color = v.color * _Color;
vertColor(o.color);
vertTexcoord(v, o);

half4 fragParticleUnlit(VertexOutputLit IN) : SV_Target
{
half4 albedo = readTexture(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), IN);
albedo *= _Color;
fragColorMode(IN);
fragSoftParticles(IN);
fragCameraFading(IN);
#if defined(_NORMALMAP)
float3 normal = normalize(UnpackNormalScale(readTexture(TEXTURE2D_PARAM(_BumpMap, sampler_BumpMap), IN), _BumpScale));
#else
float3 normal = float3(0,0,1);
#endif
#if defined(_EMISSION)
half3 emission = readTexture(TEXTURE2D_PARAM(_EmissionMap, sampler_EmissionMap), IN).rgb;
#else
half3 emission = 0;
#endif
half4 albedo = Albedo(IN);
half alpha = AlphaBlendAndTest(albedo.a);
half3 emission = Emission(IN);
half3 diffuse = AlphaModulate(albedo.rgb, alpha);
half4 result = albedo;
#if defined(_ALPHAMODULATE_ON)
result.rgb = lerp(half3(1.0, 1.0, 1.0), albedo.rgb, albedo.a);
#endif
result.rgb += emission * _EmissionColor.rgb;
#if !defined(_ALPHABLEND_ON) && !defined(_ALPHAPREMULTIPLY_ON) && !defined(_ALPHAOVERLAY_ON)
result.a = 1;
#endif
#if defined(_ALPHATEST_ON)
clip(albedo.a - _Cutoff + 0.0001);
#endif
half3 result = diffuse + emission;
ApplyFogColor(result.rgb, half3(0, 0, 0), fogFactor);
return result;
ApplyFogColor(result, half3(0, 0, 0), fogFactor);
return half4(result, alpha);
}
ENDHLSL
}

130
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardParticlesSimpleLighting.shader


// ------------------------------------------
// Only directional light is supported for lit particles
// No shadow
// No distortion
Shader "LightweightPipeline/Particles/Standard (Simple Lighting)"
{
Properties
{
_MainTex("Albedo", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
_Shininess("Shininess", Range(0.01, 1.0)) = 1.0
_GlossMapScale("Smoothness Factor", Range(0.0, 1.0)) = 1.0
_Glossiness("Glossiness", Range(0.0, 1.0)) = 0.5
[Enum(Specular Alpha,0,Albedo Alpha,1)] _SmoothnessTextureChannel("Smoothness texture channel", Float) = 0
[HideInInspector] _SpecSource("Specular Color Source", Float) = 0.0
_SpecColor("Specular", Color) = (1.0, 1.0, 1.0)
_SpecGlossMap("Specular", 2D) = "white" {}
[HideInInspector] _GlossinessSource("Glossiness Source", Float) = 0.0
[ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
[ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0
[HideInInspector] _BumpScale("Scale", Float) = 1.0
[NoScaleOffset] _BumpMap("Normal Map", 2D) = "bump" {}
_EmissionColor("Color", Color) = (0,0,0)
_EmissionMap("Emission", 2D) = "white" {}
_SoftParticlesNearFadeDistance("Soft Particles Near Fade", Float) = 0.0
_SoftParticlesFarFadeDistance("Soft Particles Far Fade", Float) = 1.0
_CameraNearFadeDistance("Camera Near Fade", Float) = 1.0
_CameraFarFadeDistance("Camera Far Fade", Float) = 2.0
// Hidden properties
[HideInInspector] _Mode("__mode", Float) = 0.0
[HideInInspector] _FlipbookMode("__flipbookmode", Float) = 0.0
[HideInInspector] _LightingEnabled("__lightingenabled", Float) = 1.0
[HideInInspector] _EmissionEnabled("__emissionenabled", Float) = 0.0
[HideInInspector] _BlendOp("__blendop", Float) = 0.0
[HideInInspector] _SrcBlend("__src", Float) = 1.0
[HideInInspector] _DstBlend("__dst", Float) = 0.0
[HideInInspector] _ZWrite("__zw", Float) = 1.0
[HideInInspector] _Cull("__cull", Float) = 2.0
[HideInInspector] _SoftParticlesEnabled("__softparticlesenabled", Float) = 0.0
[HideInInspector] _CameraFadingEnabled("__camerafadingenabled", Float) = 0.0
[HideInInspector] _SoftParticleFadeParams("__softparticlefadeparams", Vector) = (0,0,0,0)
[HideInInspector] _CameraFadeParams("__camerafadeparams", Vector) = (0,0,0,0)
}
SubShader
{
Tags{"RenderType" = "Opaque" "IgnoreProjector" = "True" "PreviewType" = "Plane" "PerformanceChecks" = "False" "RenderPipeline" = "LightweightPipeline"}
BlendOp[_BlendOp]
Blend[_SrcBlend][_DstBlend]
ZWrite[_ZWrite]
Cull[_Cull]
Pass
{
Tags {"LightMode" = "LightweightForward"}
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma vertex ParticlesLitVertex
#pragma fragment ParticlesLitFragment
#pragma multi_compile __ SOFTPARTICLES_ON
#pragma target 2.0
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON _ALPHAMODULATE_ON
#pragma shader_feature _ _SPECGLOSSMAP _SPECULAR_COLOR
#pragma shader_feature _ _GLOSSINESS_FROM_BASE_ALPHA
#pragma shader_feature _NORMALMAP
#pragma shader_feature _EMISSION
#pragma shader_feature _FADING_ON
#pragma shader_feature _REQUIRE_UV2
#define BUMP_SCALE_NOT_SUPPORTED 1
#define NO_SHADOWS 1
#include "LWRP/ShaderLibrary/Particles.hlsl"
#include "LWRP/ShaderLibrary/Lighting.hlsl"
VertexOutputLit ParticlesLitVertex(appdata_particles v)
{
VertexOutputLit o;
OUTPUT_NORMAL(v, o);
o.color = v.color * _Color;
o.posWS.xyz = TransformObjectToWorld(v.vertex.xyz).xyz;
o.posWS.w = ComputeFogFactor(o.clipPos.z);
o.clipPos = TransformWorldToHClip(o.posWS.xyz);
o.viewDirShininess.xyz = VertexViewDirWS(GetCameraPositionWS() - o.posWS.xyz);
o.viewDirShininess.w = _Shininess * 128.0h;
vertTexcoord(v, o);
vertFading(o, o.posWS, o.clipPos);
return o;
}
half4 ParticlesLitFragment(VertexOutputLit IN) : SV_Target
{
half4 albedo = Albedo(IN);
half alpha = AlphaBlendAndTest(albedo.a);
half3 diffuse = AlphaModulate(albedo.rgb, alpha);
half3 normalTS = NormalTS(IN);
half3 emission = Emission(IN);
half4 specularGloss = SpecularGloss(IN, albedo.a);
half shininess = IN.viewDirShininess.w;
InputData inputData;
InitializeInputData(IN, normalTS, inputData);
half4 color = LightweightFragmentBlinnPhong(inputData, diffuse, specularGloss, shininess, emission, alpha);
ApplyFog(color.rgb, inputData.fogCoord);
return color;
}
ENDHLSL
}
}
Fallback "LightweightPipeline/Particles/Standard Unlit"
CustomEditor "LightweightStandardParticlesShaderGUI"
}

9
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardParticlesSimpleLighting.shader.meta


fileFormatVersion: 2
guid: 8516d7a69675844a7a0b7095af7c46af
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存