У меня довольно таки странна просьба о помощи - мне нужно оптимизировать шейдер для мобильного устройства
Шейдер создан в ShaderForge. Вот здесь есть пара слов о оптимизации для мобильных, но это скорее общая инфа:
http://acegikmo.com/shaderforge/wiki/in ... for_Mobile
Сам я не программер, так что для меня это просто не реально. Ткните плиз мне пальцем в места, на которые следует уделить внимание. Взамен я делюсь шейдером с сообществом в целом (а вдруг кому пригодится? к тому же он работает и на мобайле, если накинуть на камеру скрипт для чтения глубины с камеры).
Или даже финансово отблагодарить, если кто-то готов за это взяться и работа того стоит.
Вот листинг шейдера:
Синтаксис:
Используется csharp
// Shader created with Shader Forge v1.13
// Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:1.13;sub:START;pass:START;ps:flbk:,lico:1,lgpr:1,nrmq:1,nrsp:0,limd:1,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,rprd:False,enco:False,rmgx:True,rpth:0,hqsc:True,hqlp:False,tesm:0,bsrc:3,bdst:7,culm:0,dpts:2,wrdp:False,dith:0,ufog:True,aust:True,igpj:True,qofs:0,qpre:3,rntp:2,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,ofsf:0,ofsu:0,f2p0:False;n:type:ShaderForge.SFN_Final,id:2493,x:33749,y:31849,varname:node_2493,prsc:2|diff-6743-RGB,normal-9963-OUT,emission-742-OUT,alpha-2680-OUT,refract-2816-OUT;n:type:ShaderForge.SFN_DepthBlend,id:3129,x:32872,y:32140,varname:node_3129,prsc:2|DIST-1373-OUT;n:type:ShaderForge.SFN_Slider,id:1373,x:32512,y:32146,ptovrint:False,ptlb:Opacity_Intencity,ptin:_Opacity_Intencity,varname:node_1373,prsc:2,min:0,cur:2,max:200;n:type:ShaderForge.SFN_Color,id:6743,x:32669,y:31730,ptovrint:False,ptlb:Color_Dark,ptin:_Color_Dark,varname:node_6743,prsc:2,glob:False,c1:0.1058824,c2:0.1294118,c3:0.1137255,c4:1;n:type:ShaderForge.SFN_Tex2d,id:310,x:32669,y:32318,ptovrint:False,ptlb:NormalMap,ptin:_NormalMap,varname:node_310,prsc:2,ntxv:3,isnm:True;n:type:ShaderForge.SFN_Color,id:355,x:32669,y:32506,ptovrint:False,ptlb:CleanNormal,ptin:_CleanNormal,varname:node_355,prsc:2,glob:False,c1:0,c2:0,c3:1,c4:0;n:type:ShaderForge.SFN_Lerp,id:2215,x:33113,y:32373,varname:node_2215,prsc:2|A-310-RGB,B-355-RGB,T-7367-OUT;n:type:ShaderForge.SFN_Slider,id:1890,x:32512,y:32682,ptovrint:False,ptlb:NormalMap_Intencity,ptin:_NormalMap_Intencity,varname:node_1890,prsc:2,min:0,cur:0,max:1;n:type:ShaderForge.SFN_OneMinus,id:7367,x:32893,y:32616,varname:node_7367,prsc:2|IN-1890-OUT;n:type:ShaderForge.SFN_Cubemap,id:6056,x:33355,y:31868,ptovrint:False,ptlb:Cubemap,ptin:_Cubemap,varname:node_6056,prsc:2|MIP-323-OUT;n:type:ShaderForge.SFN_Fresnel,id:3926,x:33355,y:32012,varname:node_3926,prsc:2|EXP-5765-OUT;n:type:ShaderForge.SFN_Multiply,id:742,x:33577,y:31948,varname:node_742,prsc:2|A-6056-RGB,B-3926-OUT;n:type:ShaderForge.SFN_Slider,id:5765,x:32512,y:32031,ptovrint:False,ptlb:Fresnel,ptin:_Fresnel,varname:node_5765,prsc:2,min:0,cur:3.373857,max:10;n:type:ShaderForge.SFN_Slider,id:323,x:32512,y:31905,ptovrint:False,ptlb:Reflection_Blur,ptin:_Reflection_Blur,varname:node_323,prsc:2,min:0,cur:0,max:10;n:type:ShaderForge.SFN_Normalize,id:9963,x:33372,y:32291,varname:node_9963,prsc:2|IN-2215-OUT;n:type:ShaderForge.SFN_ComponentMask,id:8039,x:33312,y:32516,varname:node_8039,prsc:2,cc1:0,cc2:1,cc3:-1,cc4:-1|IN-2215-OUT;n:type:ShaderForge.SFN_Multiply,id:2816,x:33552,y:32486,varname:node_2816,prsc:2|A-8039-OUT,B-3449-OUT;n:type:ShaderForge.SFN_ValueProperty,id:3449,x:33312,y:32695,ptovrint:False,ptlb:Reflection_Fresnel,ptin:_Reflection_Fresnel,varname:node_3449,prsc:2,glob:False,v1:0.35;n:type:ShaderForge.SFN_Blend,id:2680,x:33272,y:32111,varname:node_2680,prsc:2,blmd:6,clmp:False|SRC-1092-OUT,DST-1092-OUT;n:type:ShaderForge.SFN_RemapRange,id:1092,x:33051,y:32140,varname:node_1092,prsc:2,frmn:0,frmx:1,tomn:0.1,tomx:1|IN-3129-OUT;proporder:6743-1373-355-310-1890-6056-5765-323-3449;pass:END;sub:END;*/
Shader "Shader Forge/Depth_Blend" {
Properties {
_Color_Dark ("Color_Dark", Color) = (0.1058824,0.1294118,0.1137255,1)
_Opacity_Intencity ("Opacity_Intencity", Range(0, 200)) = 2
_CleanNormal ("CleanNormal", Color) = (0,0,1,0)
_NormalMap ("NormalMap", 2D) = "bump" {}
_NormalMap_Intencity ("NormalMap_Intencity", Range(0, 1)) = 0
_Cubemap ("Cubemap", Cube) = "_Skybox" {}
_Fresnel ("Fresnel", Range(0, 10)) = 3.373857
_Reflection_Blur ("Reflection_Blur", Range(0, 10)) = 0
_Reflection_Fresnel ("Reflection_Fresnel", Float ) = 0.35
[HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {
"IgnoreProjector"="True"
"Queue"="Transparent"
"RenderType"="Transparent"
}
GrabPass{ }
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase
#pragma multi_compile_fog
#pragma exclude_renderers d3d9 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2
#pragma target 3.0
#pragma glsl
uniform float4 _LightColor0;
uniform sampler2D _GrabTexture;
uniform sampler2D _CameraDepthTexture;
uniform float _Opacity_Intencity;
uniform float4 _Color_Dark;
uniform sampler2D _NormalMap; uniform float4 _NormalMap_ST;
uniform float4 _CleanNormal;
uniform float _NormalMap_Intencity;
uniform samplerCUBE _Cubemap;
uniform float _Fresnel;
uniform float _Reflection_Blur;
uniform float _Reflection_Fresnel;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
float3 tangentDir : TEXCOORD3;
float3 bitangentDir : TEXCOORD4;
float4 screenPos : TEXCOORD5;
float4 projPos : TEXCOORD6;
UNITY_FOG_COORDS(7)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.normalDir = UnityObjectToWorldNormal(v.normal);
o.tangentDir = normalize( mul( _Object2World, float4( v.tangent.xyz, 0.0 ) ).xyz );
o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
o.posWorld = mul(_Object2World, v.vertex);
float3 lightColor = _LightColor0.rgb;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
UNITY_TRANSFER_FOG(o,o.pos);
o.projPos = ComputeScreenPos (o.pos);
COMPUTE_EYEDEPTH(o.projPos.z);
o.screenPos = o.pos;
return o;
}
float4 frag(VertexOutput i) : COLOR {
#if UNITY_UV_STARTS_AT_TOP
float grabSign = -_ProjectionParams.x;
#else
float grabSign = _ProjectionParams.x;
#endif
i.normalDir = normalize(i.normalDir);
i.screenPos = float4( i.screenPos.xy / i.screenPos.w, 0, 0 );
i.screenPos.y *= _ProjectionParams.x;
float sceneZ = max(0,LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)))) - _ProjectionParams.g);
float partZ = max(0,i.projPos.z - _ProjectionParams.g);
float3 _NormalMap_var = UnpackNormal(tex2D(_NormalMap,TRANSFORM_TEX(i.uv0, _NormalMap)));
float3 node_2215 = lerp(_NormalMap_var.rgb,_CleanNormal.rgb,(1.0 - _NormalMap_Intencity));
float2 sceneUVs = float2(1,grabSign)*i.screenPos.xy*0.5+0.5 + (node_2215.rg*_Reflection_Fresnel);
float4 sceneColor = tex2D(_GrabTexture, sceneUVs);
float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
/////// Vectors:
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 normalLocal = normalize(node_2215);
float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
float3 viewReflectDirection = reflect( -viewDirection, normalDirection );
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 lightColor = _LightColor0.rgb;
////// Lighting:
float attenuation = 1;
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = max(0.0,dot( normalDirection, lightDirection ));
float3 directDiffuse = max( 0.0, NdotL) * attenColor;
float3 indirectDiffuse = float3(0,0,0);
indirectDiffuse += UNITY_LIGHTMODEL_AMBIENT.rgb; // Ambient Light
float3 diffuseColor = _Color_Dark.rgb;
float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
////// Emissive:
float3 emissive = (texCUBElod(_Cubemap,float4(viewReflectDirection,_Reflection_Blur)).rgb*pow(1.0-max(0,dot(normalDirection, viewDirection)),_Fresnel));
/// Final Color:
float3 finalColor = diffuse + emissive;
float node_1092 = (saturate((sceneZ-partZ)/_Opacity_Intencity)*0.9+0.1);
fixed4 finalRGBA = fixed4(lerp(sceneColor.rgb, finalColor,(1.0-(1.0-node_1092)*(1.0-node_1092))),1);
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}
Pass {
Name "FORWARD_DELTA"
Tags {
"LightMode"="ForwardAdd"
}
Blend One One
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDADD
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#pragma multi_compile_fwdadd
#pragma multi_compile_fog
#pragma exclude_renderers d3d9 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2
#pragma target 3.0
#pragma glsl
uniform float4 _LightColor0;
uniform sampler2D _GrabTexture;
uniform sampler2D _CameraDepthTexture;
uniform float _Opacity_Intencity;
uniform float4 _Color_Dark;
uniform sampler2D _NormalMap; uniform float4 _NormalMap_ST;
uniform float4 _CleanNormal;
uniform float _NormalMap_Intencity;
uniform samplerCUBE _Cubemap;
uniform float _Fresnel;
uniform float _Reflection_Blur;
uniform float _Reflection_Fresnel;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
float3 tangentDir : TEXCOORD3;
float3 bitangentDir : TEXCOORD4;
float4 screenPos : TEXCOORD5;
float4 projPos : TEXCOORD6;
LIGHTING_COORDS(7,8)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.normalDir = UnityObjectToWorldNormal(v.normal);
o.tangentDir = normalize( mul( _Object2World, float4( v.tangent.xyz, 0.0 ) ).xyz );
o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
o.posWorld = mul(_Object2World, v.vertex);
float3 lightColor = _LightColor0.rgb;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.projPos = ComputeScreenPos (o.pos);
COMPUTE_EYEDEPTH(o.projPos.z);
o.screenPos = o.pos;
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
float4 frag(VertexOutput i) : COLOR {
#if UNITY_UV_STARTS_AT_TOP
float grabSign = -_ProjectionParams.x;
#else
float grabSign = _ProjectionParams.x;
#endif
i.normalDir = normalize(i.normalDir);
i.screenPos = float4( i.screenPos.xy / i.screenPos.w, 0, 0 );
i.screenPos.y *= _ProjectionParams.x;
float sceneZ = max(0,LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)))) - _ProjectionParams.g);
float partZ = max(0,i.projPos.z - _ProjectionParams.g);
float3 _NormalMap_var = UnpackNormal(tex2D(_NormalMap,TRANSFORM_TEX(i.uv0, _NormalMap)));
float3 node_2215 = lerp(_NormalMap_var.rgb,_CleanNormal.rgb,(1.0 - _NormalMap_Intencity));
float2 sceneUVs = float2(1,grabSign)*i.screenPos.xy*0.5+0.5 + (node_2215.rg*_Reflection_Fresnel);
float4 sceneColor = tex2D(_GrabTexture, sceneUVs);
float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
/////// Vectors:
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 normalLocal = normalize(node_2215);
float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
float3 viewReflectDirection = reflect( -viewDirection, normalDirection );
float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));
float3 lightColor = _LightColor0.rgb;
////// Lighting:
float attenuation = LIGHT_ATTENUATION(i);
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = max(0.0,dot( normalDirection, lightDirection ));
float3 directDiffuse = max( 0.0, NdotL) * attenColor;
float3 diffuseColor = _Color_Dark.rgb;
float3 diffuse = directDiffuse * diffuseColor;
/// Final Color:
float3 finalColor = diffuse;
float node_1092 = (saturate((sceneZ-partZ)/_Opacity_Intencity)*0.9+0.1);
return fixed4(finalColor * (1.0-(1.0-node_1092)*(1.0-node_1092)),0);
}
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "ShaderForgeMaterialInspector"
}
// Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:1.13;sub:START;pass:START;ps:flbk:,lico:1,lgpr:1,nrmq:1,nrsp:0,limd:1,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,rprd:False,enco:False,rmgx:True,rpth:0,hqsc:True,hqlp:False,tesm:0,bsrc:3,bdst:7,culm:0,dpts:2,wrdp:False,dith:0,ufog:True,aust:True,igpj:True,qofs:0,qpre:3,rntp:2,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,ofsf:0,ofsu:0,f2p0:False;n:type:ShaderForge.SFN_Final,id:2493,x:33749,y:31849,varname:node_2493,prsc:2|diff-6743-RGB,normal-9963-OUT,emission-742-OUT,alpha-2680-OUT,refract-2816-OUT;n:type:ShaderForge.SFN_DepthBlend,id:3129,x:32872,y:32140,varname:node_3129,prsc:2|DIST-1373-OUT;n:type:ShaderForge.SFN_Slider,id:1373,x:32512,y:32146,ptovrint:False,ptlb:Opacity_Intencity,ptin:_Opacity_Intencity,varname:node_1373,prsc:2,min:0,cur:2,max:200;n:type:ShaderForge.SFN_Color,id:6743,x:32669,y:31730,ptovrint:False,ptlb:Color_Dark,ptin:_Color_Dark,varname:node_6743,prsc:2,glob:False,c1:0.1058824,c2:0.1294118,c3:0.1137255,c4:1;n:type:ShaderForge.SFN_Tex2d,id:310,x:32669,y:32318,ptovrint:False,ptlb:NormalMap,ptin:_NormalMap,varname:node_310,prsc:2,ntxv:3,isnm:True;n:type:ShaderForge.SFN_Color,id:355,x:32669,y:32506,ptovrint:False,ptlb:CleanNormal,ptin:_CleanNormal,varname:node_355,prsc:2,glob:False,c1:0,c2:0,c3:1,c4:0;n:type:ShaderForge.SFN_Lerp,id:2215,x:33113,y:32373,varname:node_2215,prsc:2|A-310-RGB,B-355-RGB,T-7367-OUT;n:type:ShaderForge.SFN_Slider,id:1890,x:32512,y:32682,ptovrint:False,ptlb:NormalMap_Intencity,ptin:_NormalMap_Intencity,varname:node_1890,prsc:2,min:0,cur:0,max:1;n:type:ShaderForge.SFN_OneMinus,id:7367,x:32893,y:32616,varname:node_7367,prsc:2|IN-1890-OUT;n:type:ShaderForge.SFN_Cubemap,id:6056,x:33355,y:31868,ptovrint:False,ptlb:Cubemap,ptin:_Cubemap,varname:node_6056,prsc:2|MIP-323-OUT;n:type:ShaderForge.SFN_Fresnel,id:3926,x:33355,y:32012,varname:node_3926,prsc:2|EXP-5765-OUT;n:type:ShaderForge.SFN_Multiply,id:742,x:33577,y:31948,varname:node_742,prsc:2|A-6056-RGB,B-3926-OUT;n:type:ShaderForge.SFN_Slider,id:5765,x:32512,y:32031,ptovrint:False,ptlb:Fresnel,ptin:_Fresnel,varname:node_5765,prsc:2,min:0,cur:3.373857,max:10;n:type:ShaderForge.SFN_Slider,id:323,x:32512,y:31905,ptovrint:False,ptlb:Reflection_Blur,ptin:_Reflection_Blur,varname:node_323,prsc:2,min:0,cur:0,max:10;n:type:ShaderForge.SFN_Normalize,id:9963,x:33372,y:32291,varname:node_9963,prsc:2|IN-2215-OUT;n:type:ShaderForge.SFN_ComponentMask,id:8039,x:33312,y:32516,varname:node_8039,prsc:2,cc1:0,cc2:1,cc3:-1,cc4:-1|IN-2215-OUT;n:type:ShaderForge.SFN_Multiply,id:2816,x:33552,y:32486,varname:node_2816,prsc:2|A-8039-OUT,B-3449-OUT;n:type:ShaderForge.SFN_ValueProperty,id:3449,x:33312,y:32695,ptovrint:False,ptlb:Reflection_Fresnel,ptin:_Reflection_Fresnel,varname:node_3449,prsc:2,glob:False,v1:0.35;n:type:ShaderForge.SFN_Blend,id:2680,x:33272,y:32111,varname:node_2680,prsc:2,blmd:6,clmp:False|SRC-1092-OUT,DST-1092-OUT;n:type:ShaderForge.SFN_RemapRange,id:1092,x:33051,y:32140,varname:node_1092,prsc:2,frmn:0,frmx:1,tomn:0.1,tomx:1|IN-3129-OUT;proporder:6743-1373-355-310-1890-6056-5765-323-3449;pass:END;sub:END;*/
Shader "Shader Forge/Depth_Blend" {
Properties {
_Color_Dark ("Color_Dark", Color) = (0.1058824,0.1294118,0.1137255,1)
_Opacity_Intencity ("Opacity_Intencity", Range(0, 200)) = 2
_CleanNormal ("CleanNormal", Color) = (0,0,1,0)
_NormalMap ("NormalMap", 2D) = "bump" {}
_NormalMap_Intencity ("NormalMap_Intencity", Range(0, 1)) = 0
_Cubemap ("Cubemap", Cube) = "_Skybox" {}
_Fresnel ("Fresnel", Range(0, 10)) = 3.373857
_Reflection_Blur ("Reflection_Blur", Range(0, 10)) = 0
_Reflection_Fresnel ("Reflection_Fresnel", Float ) = 0.35
[HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {
"IgnoreProjector"="True"
"Queue"="Transparent"
"RenderType"="Transparent"
}
GrabPass{ }
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase
#pragma multi_compile_fog
#pragma exclude_renderers d3d9 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2
#pragma target 3.0
#pragma glsl
uniform float4 _LightColor0;
uniform sampler2D _GrabTexture;
uniform sampler2D _CameraDepthTexture;
uniform float _Opacity_Intencity;
uniform float4 _Color_Dark;
uniform sampler2D _NormalMap; uniform float4 _NormalMap_ST;
uniform float4 _CleanNormal;
uniform float _NormalMap_Intencity;
uniform samplerCUBE _Cubemap;
uniform float _Fresnel;
uniform float _Reflection_Blur;
uniform float _Reflection_Fresnel;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
float3 tangentDir : TEXCOORD3;
float3 bitangentDir : TEXCOORD4;
float4 screenPos : TEXCOORD5;
float4 projPos : TEXCOORD6;
UNITY_FOG_COORDS(7)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.normalDir = UnityObjectToWorldNormal(v.normal);
o.tangentDir = normalize( mul( _Object2World, float4( v.tangent.xyz, 0.0 ) ).xyz );
o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
o.posWorld = mul(_Object2World, v.vertex);
float3 lightColor = _LightColor0.rgb;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
UNITY_TRANSFER_FOG(o,o.pos);
o.projPos = ComputeScreenPos (o.pos);
COMPUTE_EYEDEPTH(o.projPos.z);
o.screenPos = o.pos;
return o;
}
float4 frag(VertexOutput i) : COLOR {
#if UNITY_UV_STARTS_AT_TOP
float grabSign = -_ProjectionParams.x;
#else
float grabSign = _ProjectionParams.x;
#endif
i.normalDir = normalize(i.normalDir);
i.screenPos = float4( i.screenPos.xy / i.screenPos.w, 0, 0 );
i.screenPos.y *= _ProjectionParams.x;
float sceneZ = max(0,LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)))) - _ProjectionParams.g);
float partZ = max(0,i.projPos.z - _ProjectionParams.g);
float3 _NormalMap_var = UnpackNormal(tex2D(_NormalMap,TRANSFORM_TEX(i.uv0, _NormalMap)));
float3 node_2215 = lerp(_NormalMap_var.rgb,_CleanNormal.rgb,(1.0 - _NormalMap_Intencity));
float2 sceneUVs = float2(1,grabSign)*i.screenPos.xy*0.5+0.5 + (node_2215.rg*_Reflection_Fresnel);
float4 sceneColor = tex2D(_GrabTexture, sceneUVs);
float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
/////// Vectors:
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 normalLocal = normalize(node_2215);
float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
float3 viewReflectDirection = reflect( -viewDirection, normalDirection );
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 lightColor = _LightColor0.rgb;
////// Lighting:
float attenuation = 1;
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = max(0.0,dot( normalDirection, lightDirection ));
float3 directDiffuse = max( 0.0, NdotL) * attenColor;
float3 indirectDiffuse = float3(0,0,0);
indirectDiffuse += UNITY_LIGHTMODEL_AMBIENT.rgb; // Ambient Light
float3 diffuseColor = _Color_Dark.rgb;
float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
////// Emissive:
float3 emissive = (texCUBElod(_Cubemap,float4(viewReflectDirection,_Reflection_Blur)).rgb*pow(1.0-max(0,dot(normalDirection, viewDirection)),_Fresnel));
/// Final Color:
float3 finalColor = diffuse + emissive;
float node_1092 = (saturate((sceneZ-partZ)/_Opacity_Intencity)*0.9+0.1);
fixed4 finalRGBA = fixed4(lerp(sceneColor.rgb, finalColor,(1.0-(1.0-node_1092)*(1.0-node_1092))),1);
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}
Pass {
Name "FORWARD_DELTA"
Tags {
"LightMode"="ForwardAdd"
}
Blend One One
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDADD
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#pragma multi_compile_fwdadd
#pragma multi_compile_fog
#pragma exclude_renderers d3d9 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2
#pragma target 3.0
#pragma glsl
uniform float4 _LightColor0;
uniform sampler2D _GrabTexture;
uniform sampler2D _CameraDepthTexture;
uniform float _Opacity_Intencity;
uniform float4 _Color_Dark;
uniform sampler2D _NormalMap; uniform float4 _NormalMap_ST;
uniform float4 _CleanNormal;
uniform float _NormalMap_Intencity;
uniform samplerCUBE _Cubemap;
uniform float _Fresnel;
uniform float _Reflection_Blur;
uniform float _Reflection_Fresnel;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
float3 tangentDir : TEXCOORD3;
float3 bitangentDir : TEXCOORD4;
float4 screenPos : TEXCOORD5;
float4 projPos : TEXCOORD6;
LIGHTING_COORDS(7,8)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.normalDir = UnityObjectToWorldNormal(v.normal);
o.tangentDir = normalize( mul( _Object2World, float4( v.tangent.xyz, 0.0 ) ).xyz );
o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
o.posWorld = mul(_Object2World, v.vertex);
float3 lightColor = _LightColor0.rgb;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.projPos = ComputeScreenPos (o.pos);
COMPUTE_EYEDEPTH(o.projPos.z);
o.screenPos = o.pos;
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
float4 frag(VertexOutput i) : COLOR {
#if UNITY_UV_STARTS_AT_TOP
float grabSign = -_ProjectionParams.x;
#else
float grabSign = _ProjectionParams.x;
#endif
i.normalDir = normalize(i.normalDir);
i.screenPos = float4( i.screenPos.xy / i.screenPos.w, 0, 0 );
i.screenPos.y *= _ProjectionParams.x;
float sceneZ = max(0,LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)))) - _ProjectionParams.g);
float partZ = max(0,i.projPos.z - _ProjectionParams.g);
float3 _NormalMap_var = UnpackNormal(tex2D(_NormalMap,TRANSFORM_TEX(i.uv0, _NormalMap)));
float3 node_2215 = lerp(_NormalMap_var.rgb,_CleanNormal.rgb,(1.0 - _NormalMap_Intencity));
float2 sceneUVs = float2(1,grabSign)*i.screenPos.xy*0.5+0.5 + (node_2215.rg*_Reflection_Fresnel);
float4 sceneColor = tex2D(_GrabTexture, sceneUVs);
float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
/////// Vectors:
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 normalLocal = normalize(node_2215);
float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
float3 viewReflectDirection = reflect( -viewDirection, normalDirection );
float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));
float3 lightColor = _LightColor0.rgb;
////// Lighting:
float attenuation = LIGHT_ATTENUATION(i);
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = max(0.0,dot( normalDirection, lightDirection ));
float3 directDiffuse = max( 0.0, NdotL) * attenColor;
float3 diffuseColor = _Color_Dark.rgb;
float3 diffuse = directDiffuse * diffuseColor;
/// Final Color:
float3 finalColor = diffuse;
float node_1092 = (saturate((sceneZ-partZ)/_Opacity_Intencity)*0.9+0.1);
return fixed4(finalColor * (1.0-(1.0-node_1092)*(1.0-node_1092)),0);
}
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "ShaderForgeMaterialInspector"
}
Чтобы скорее понять, что там происходит, привожу скрин из шейдерфоржа:
Заранее благодарю всех, откликнувшихся!