转成unity可用的 shader
时间: 2024-12-18 15:15:48 浏览: 6
将给定的Cocos Creator Shader转换为Unity可用的Shader需要进行一些调整和重构。以下是基于提供的Cocos Creator Shader代码生成的一个Unity Shader示例:
### Unity Shader 示例
```csharp
Shader "Custom/CocosCreatorShader"
{
Properties
{
_MainTex ("Albedo Map", 2D) = "white" {}
_Albedo ("Albedo Color", Color) = (1,1,1,1)
_TilingOffset ("Tiling Offset", Vector) = (1,1,0,0)
_AlphaThreshold ("Alpha Threshold", Range(0,1)) = 0.5
_Occlusion ("Occlusion", Range(0,1)) = 1.0
_Roughness ("Roughness", Range(0,1)) = 0.8
_Metallic ("Metallic", Range(0,1)) = 0.6
_SpecularIntensity ("Specular Intensity", Range(0,1)) = 0.5
_NormalStrength ("Normal Strength", Range(0,1)) = 1.0
_Emissive ("Emissive Color", Color) = (0,0,0,1)
_EmissiveScale ("Emissive Scale", Vector) = (1,1,1,1)
_NormalMap ("Normal Map", 2D) = "bump" {}
_PBRMap ("PBR Map", 2D) = "white" {}
_MetallicRoughnessMap ("Metallic Roughness Map", 2D) = "white" {}
_OcclusionMap ("Occlusion Map", 2D) = "white" {}
_EmissiveMap ("Emissive Map", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
Pass
{
Name "FORWARD"
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 uv : TEXCOORD0;
float2 uv1 : TEXCOORD1;
fixed4 color : COLOR;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float3 worldTangent : TEXCOORD2;
float3 worldBitangent : TEXCOORD3;
float2 uv : TEXCOORD4;
float2 uv1 : TEXCOORD5;
fixed4 color : COLOR;
SHADOW_COORDS(6)
};
sampler2D _MainTex;
sampler2D _NormalMap;
sampler2D _PBRMap;
sampler2D _MetallicRoughnessMap;
sampler2D _OcclusionMap;
sampler2D _EmissiveMap;
float4 _MainTex_ST;
float4 _TilingOffset;
float4 _Albedo;
float _AlphaThreshold;
float _Occlusion;
float _Roughness;
float _Metallic;
float _SpecularIntensity;
float _NormalStrength;
float4 _Emissive;
float4 _EmissiveScale;
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
o.worldBitangent = cross(o.worldNormal, o.worldTangent) * v.tangent.w;
o.uv = TRANSFORM_TEX(v.uv, _MainTex) * _TilingOffset.xy + _TilingOffset.zw;
o.uv1 = TRANSFORM_TEX(v.uv1, _MainTex) * _TilingOffset.xy + _TilingOffset.zw;
o.color = v.color;
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 baseColor = _Albedo;
baseColor *= tex2D(_MainTex, i.uv);
if (baseColor.a < _AlphaThreshold)
discard;
float3 normal = i.worldNormal;
if (_NormalStrength > 0)
{
float3 normalMap = UnpackNormal(tex2D(_NormalMap, i.uv));
normal = normalize(i.worldTangent * normalMap.x * _NormalStrength +
i.worldBitangent * normalMap.y * _NormalStrength +
i.worldNormal * normalMap.z);
}
float4 pbr = float4(_Occlusion, _Roughness, _Metallic, _SpecularIntensity);
if (tex2D(_PBRMap, i.uv).r > 0)
{
pbr.x *= tex2D(_PBRMap, i.uv).r;
pbr.y *= tex2D(_PBRMap, i.uv).g;
pbr.z *= tex2D(_PBRMap, i.uv).b;
pbr.w *= tex2D(_PBRMap, i.uv).a;
}
if (tex2D(_MetallicRoughnessMap, i.uv).r > 0)
{
pbr.z *= tex2D(_MetallicRoughnessMap, i.uv).b;
pbr.y *= tex2D(_MetallicRoughnessMap, i.uv).g;
}
if (tex2D(_OcclusionMap, i.uv).r > 0)
{
pbr.x *= tex2D(_OcclusionMap, i.uv).r;
}
float3 emissive = _Emissive.rgb * _EmissiveScale.xyz;
if (tex2D(_EmissiveMap, i.uv).rgb > 0)
{
emissive *= tex2D(_EmissiveMap, i.uv).rgb;
}
UNITY_LIGHT_ATTENUATION(attenuation, i, i.worldPos)
fixed4 finalColor = baseColor * attenuation;
finalColor.rgb += emissive;
return finalColor;
}
ENDCG
}
Pass
{
Name "SHADOWCASTER"
Tags { "LightMode"="ShadowCaster" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float2 uv1 : TEXCOORD1;
};
struct v2f
{
V2F_SHADOW_CASTER;
float2 uv : TEXCOORD1;
float2 uv1 : TEXCOORD2;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _TilingOffset;
float _AlphaThreshold;
v2f vert (appdata v)
{
v2f o;
TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
o.uv = TRANSFORM_TEX(v.uv, _MainTex) * _TilingOffset.xy + _TilingOffset.zw;
o.uv1 = TRANSFORM_TEX(v.uv1, _MainTex) * _TilingOffset.xy + _TilingOffset.zw;
return o;
}
float4 frag (v2f i) : SV_Target
{
fixed4 baseColor = tex2D(_MainTex, i.uv);
if (baseColor.a < _AlphaThreshold)
discard;
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
}
FallBack "Diffuse"
}
```
### 解释
1. **Properties**:
- 定义了与Cocos Creator Shader中对应的属性,如 `_MainTex`, `_Albedo`, `_TilingOffset` 等。
2. **SubShader**:
- 包含两个Pass:一个用于前向渲染(`FORWARD`),另一个用于阴影投射(`SHADOWCASTER`)。
3. **Vertex Shader (`vert`)**:
- 处理顶点变换,计算世界坐标、法线、切线和副切线,并传递纹理坐标和颜色。
4. **Fragment Shader (`frag`)**:
- 计算基础颜色、法线、PBR参数、自发光等,并应用阴影衰减。
- 使用 `discard` 关键字处理透明度测试。
5. **ShadowCaster Pass**:
- 用于生成阴影贴图,处理透明度测试并丢弃不满足条件的片段。
这个Unity Shader应该能够实现类似Cocos Creator Shader的效果。你可以根据具体需求进一步调整和优化。
阅读全文