Skip to content

Commit

Permalink
*渐变纹理
Browse files Browse the repository at this point in the history
  • Loading branch information
zengzhiwei committed Jul 22, 2019
1 parent a0bbf87 commit 47a7ebf
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 39 deletions.
165 changes: 165 additions & 0 deletions Assets/Shader/_Aver3/07BaseTexture/20190713.unity
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,171 @@ NavMeshSettings:
tileSize: 256
accuratePlacement: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &39418770
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 39418774}
- component: {fileID: 39418773}
- component: {fileID: 39418772}
- component: {fileID: 39418771}
m_Layer: 0
m_Name: Cube
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!23 &39418771
MeshRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 39418770}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_Materials:
- {fileID: 2100000, guid: d189417a95392d2429e254ee4a83e9de, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 1
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!65 &39418772
BoxCollider:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 39418770}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!33 &39418773
MeshFilter:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 39418770}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &39418774
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 39418770}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 1.266, y: 0.994, z: -7.165}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &291369251
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 291369255}
- component: {fileID: 291369254}
- component: {fileID: 291369253}
- component: {fileID: 291369252}
m_Layer: 0
m_Name: Capsule (2)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!23 &291369252
MeshRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 291369251}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_Materials:
- {fileID: 2100000, guid: 06efff740f3635d43b1a538d7f399e57, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 1
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!136 &291369253
CapsuleCollider:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 291369251}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
m_Radius: 0.5
m_Height: 2
m_Direction: 1
m_Center: {x: 0, y: 0, z: 0}
--- !u!33 &291369254
MeshFilter:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 291369251}
m_Mesh: {fileID: 10208, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &291369255
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 291369251}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -2.297, y: 0.99, z: -7.54}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &603793413
GameObject:
m_ObjectHideFlags: 0
Expand Down
6 changes: 5 additions & 1 deletion Assets/Shader/_Aver3/07BaseTexture/Ramp Texture.mat
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Material:
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: Ramp Texture
m_Shader: {fileID: 4800000, guid: ff465a8cbf40bc14293b81abf82618e9, type: 3}
m_Shader: {fileID: 4800000, guid: dd2cf51979363db48a01af204a310cb6, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
Expand Down Expand Up @@ -54,6 +54,10 @@ Material:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _RampTex:
m_Texture: {fileID: 2800000, guid: ff0f2af5e9688e84f9d3695776fa9949, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
Expand Down
56 changes: 18 additions & 38 deletions Assets/Shader/_Aver3/07BaseTexture/Ramp Texture.shader
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Shader "_Aver3/Ramp Texture"
//2019.07.22 渐变纹理
Shader "_Aver3/Ramp Texture"
{
Properties
{
Expand All @@ -25,10 +26,9 @@
struct v2f
{
float4 vertex : SV_POSITION;
float4 uv : TEXCOORD0;
float4 TangentToWorld0 : TEXCOORD1;
float4 TangentToWorld1 : TEXCOORD2;
float4 TangentToWorld2 : TEXCOORD3;
float2 uv : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float3 worldPos : TEXCOORD2;
};

sampler2D _RampTex;
Expand All @@ -41,47 +41,27 @@
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);

float3 worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
float3 worldBinormal = cross(worldNormal,worldTangent) * v.tangent.w;

o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.uv = TRANSFORM_TEX(v.texcoord, _RampTex);
return o;
}

// 得到切线空间法线方向,
fixed4 frag (v2f i) : SV_Target
{
float3 worldPos = float3(i.TangentToWorld0.w,i.TangentToWorld1.w,i.TangentToWorld2.w);
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));

//fixed3 tangentLightDir = normalize(i.lightDir);
//fixed3 tangentViewDir = normalize(i.viewDir);
//
//// 获取法线贴图的像素
fixed4 packedNormal = tex2D(_BumpMap, i.uv.zw);
//fixed3 tangentNormal;
//// 如果纹理没有标记成法线贴图
////tangentNormal.xy = (packedNormal.xy * 2 - 1) * _BumpScale;
////tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
//
//// 如果标记成了法线贴图,可以用内置函数
fixed3 normal = UnpackNormal(packedNormal);
normal.xy *= _BumpScale;
normal.z = sqrt(1.0 - saturate(dot(normal.xy, normal.xy)));
// 把法线从切线空间转到世界空间
normal = normalize(fixed3(dot(i.TangentToWorld0.xyz,normal),dot(i.TangentToWorld1.xyz,normal),dot(i.TangentToWorld2.xyz,normal)));

//使用tex2D做纹理采样,将采样结果和颜色属性_Color相乘作为反射率
fixed3 albedo = tex2D(_MainTex,i.uv).rgb * _Color.rgb;

fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
// 顶点和法线的normalize会有一丝区别
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));

fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(normal,lightDir));
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed halfLambert = 0.5 * dot(worldNormal,worldLightDir) + 0.5;
fixed3 albedo = tex2D(_RampTex,fixed2(halfLambert,halfLambert)).rgb * _Color.rgb;
fixed3 diffuse = _LightColor0.rgb * albedo;

fixed3 halfDir = normalize(lightDir + viewDir);
fixed3 specular = _LightColor0.rgb * _Specular * pow(saturate(dot(halfDir,normal)), _Gloss);
fixed3 halfDir = normalize(worldLightDir + worldViewDir);
fixed3 specular = _LightColor0.rgb * _Specular * pow(saturate(dot(halfDir,worldNormal)), _Gloss);

return fixed4(ambient + diffuse + specular,1.0);
}
Expand Down
Binary file not shown.
68 changes: 68 additions & 0 deletions Assets/Shader/_Aver3/07BaseTexture/Ramp_Texture0.psd.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Loading

0 comments on commit 47a7ebf

Please sign in to comment.