要点
1) 漫反射和高光发射都是光照到物体表面,然后光反射到人眼
2) 环境反射是周边环境的画面照到物体表面, 然后环境的画面反射到人眼
贴图漫反射(MainTex) + 高光反射 + 环境反射
Shader "My/Tex2/ReflectCubeMap" { Properties { _MainTex("Main Tex", 2D) = "white" {} _Color("Diffuse Color", Color) = (1, 1, 1, 1) _Specular("Specular", Color) = (1, 1, 1, 1) //高光反射颜色 _Gloss("Gloss", Range(8.0, 256)) = 20 //高光区域大小 _ReflectColor("Reflection Color", Color) = (1, 1, 1, 1) //反射颜色 _ReflectAmount("Reflect Amount", Range(0, 1)) = 1 //反射程度 _Cubemap("Reflection Cubemap", Cube) = "_Skybox" {} //反射环境时从这个贴图采样环境 } SubShader { Tags { "RenderType" = "Opaque" "Queue" = "Geometry"} Pass { Tags { "LightMode" = "ForwardBase" } CGPROGRAM #pragma multi_compile_fwdbase #pragma vertex vert #pragma fragment frag #include "Lighting.cginc" #include "AutoLight.cginc" sampler2D _MainTex; float4 _MainTex_ST; fixed4 _Color; fixed4 _Specular; float _Gloss; fixed4 _ReflectColor; fixed _ReflectAmount; samplerCUBE _Cubemap; struct a2v { float4 vertex : POSITION; float3 normal : NORMAL; //顶点法线 float2 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; float3 worldNormal : TEXCOORD1; float3 worldPos : TEXCOORD2; float3 worldViewDir : TEXCOORD3; float3 worldRefl : TEXCOORD4; SHADOW_COORDS(5) }; v2f vert(a2v v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.worldNormal = UnityObjectToWorldNormal(v.normal); o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos); o.worldRefl = reflect(-o.worldViewDir, o.worldNormal); //入射光线方向, 通过入射光线方向获得从环境的哪里发射过来的 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); TRANSFER_SHADOW(o); return o; } fixed4 frag(v2f i) : SV_Target { fixed3 worldNormal = normalize(i.worldNormal); fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos)); //光方向 fixed3 worldViewDir = normalize(i.worldViewDir); //视线方向(顶点到相机) fixed4 texColor = tex2D(_MainTex, i.uv); fixed3 albedo = texColor.rgb * _Color.rgb; //取贴图颜色作为漫反射颜色 fixed lambert = max(0, dot(worldNormal, worldLightDir)); //表面法线和光线方向夹角的cos值成正比 fixed3 diffuse = _LightColor0.rgb * albedo * lambert; //漫反射计算公式 fixed3 halfDir = normalize(worldLightDir + worldViewDir); // blinn模型引入的h向量 fixed3 specularColor = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(worldNormal, halfDir)), _Gloss); // blinn模型高光反射计算公式(更亮, 高光区域更大) fixed3 reflection = texCUBE(_Cubemap, i.worldRefl).rgb * _ReflectColor.rgb; //根据入射光线与环境的交点, 采样颜色 UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos); //光照衰减+接收阴影宏 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo; //环境光 fixed3 color = ambient + (lerp(diffuse, reflection, _ReflectAmount) + specularColor) * atten; return fixed4(color, 1.0); } ENDCG } } FallBack "Reflective/VertexLit" }
设置天空盒
1) 创建一个材质Skybox.mat,将shader设置为Unity内置的Skybox/6 Slided,然后6个方向的图片
2) Window -> Rendering -> Light Settings, 将材质设置上去
Cubemap资源的生成
1) 右键 -> Create -> Legacy -> Cubemap
2) 使用下面的工具脚本生成
using UnityEngine; using UnityEditor;public class RenderCubemapWizard : ScriptableWizard { public Transform renderFromPosition; public Cubemap cubemap; void OnWizardUpdate () { helpString = "Select transform to render from and cubemap to render into"; isValid = (renderFromPosition != null) && (cubemap != null); } void OnWizardCreate () { // create temporary camera for rendering GameObject go = new GameObject( "CubemapCamera"); go.AddComponent<Camera>(); // place it on the object go.transform.position = renderFromPosition.position; // render into cubemap go.GetComponent<Camera>().RenderToCubemap(cubemap); // destroy temporary camera DestroyImmediate( go ); } [MenuItem("GameObject/Render into Cubemap")] static void RenderCubemap () { ScriptableWizard.DisplayWizard<RenderCubemapWizard>( "Render cubemap", "Render!"); } }
3) 点击Render,就是在Sphere所在的位置,分别对左,右,前,后,上,下拍6张照片,正好是对应立方体映射纹理(Cubemap)的6个面
标签:CH10,MainTex,Color,Cubemap,Shader,fixed3,rgb,worldNormal,精要 From: https://www.cnblogs.com/sailJs/p/17085543.html