u3d27-shader-镜面反射说实话镜面反射很简单,难就难在实现镜面反射的代码困难,但是现在我们的代码都是集成好的所以直接使用就可以了!我们创建一个组件作为我们要变成镜面的工具(一般我们选择plane),之后创建一个材质,在其属性界面选择我们shader创建的方法把我们创建的材质赋给我们要做成镜面的组件(plane),把我们的代码与我们的组件绑定!Ok我们的镜面就做好了!共享一段JS代码。。。。可以让我们创建的组件自行旋转欧!functionUpdate(){transform.Rotate(0,Time.deltaTime*10,0);}把代码共享如下:Shader文件:ShaderFX/MirrorReflection{Properties{_MainTex(Base(RGB),2D)=white{}_ReflectionTex(Reflection,2D)=white{TexGenObjectLinear}}//twotexturecards:fullthingSubshader{Pass{SetTexture[_MainTex]{combinetexture}SetTexture[_ReflectionTex]{matrix[_ProjMatrix]combinetexture*previous}}}//fallback:justmaintextureSubshader{Pass{SetTexture[_MainTex]{combinetexture}}}}Cs文件:usingUnityEngine;usingSystem.Collections;//实际上ThisisinfactjusttheWaterscriptfromProStandardAssets,//justwithrefractionstuffremoved.[ExecuteInEditMode]//Makemirrorlive-updateevenwhennotinplaymodepublicclassMirrorReflection:MonoBehaviour{publicboolm_DisablePixelLights=true;publicintm_TextureSize=256;publicfloatm_ClipPlaneOffset=0.07f;publicLayerMaskm_ReflectLayers=-1;privateHashtablem_ReflectionCameras=newHashtable();//Camera-CameratableprivateRenderTexturem_ReflectionTexture=null;privateintm_OldReflectionTextureSize=0;privatestaticbools_InsideRendering=false;//Thisiscalledwhenit'sknownthattheobjectwillberenderedbysome//camera.Werenderreflectionsanddootherupdateshere.//Becausethescriptexecutesineditmode,reflectionsforthesceneview//camerawilljustwork!publicvoidOnWillRenderObject(){if(!enabled||!renderer||!renderer.sharedMaterial||!renderer.enabled)return;Cameracam=Camera.current;if(!cam)return;//Safeguardfromrecursivereflections.if(s_InsideRendering)return;s_InsideRendering=true;CamerareflectionCamera;CreateMirrorObjects(cam,outreflectionCamera);//findoutthereflectionplane:positionandnormalinworldspaceVector3pos=transform.position;Vector3normal=transform.up;//OptionallydisablepixellightsforreflectionintoldPixelLightCount=QualitySettings.pixelLightCount;if(m_DisablePixelLights)QualitySettings.pixelLightCount=0;UpdateCameraModes(cam,reflectionCamera);//Renderreflection//Reflectcameraaroundreflectionplanefloatd=-Vector3.Dot(normal,pos)-m_ClipPlaneOffset;Vector4reflectionPlane=newVector4(normal.x,normal.y,normal.z,d);Matrix4x4reflection=Matrix4x4.zero;CalculateReflectionMatrix(refreflection,reflectionPlane);Vector3oldpos=cam.transform.position;Vector3newpos=reflection.MultiplyPoint(oldpos);reflectionCamera.worldToCameraMatrix=cam.worldToCameraMatrix*reflection;//Setupobliqueprojectionmatrixsothatnearplaneisourreflection//plane.Thiswayweclipeverythingbelow/aboveitforfree.Vector4clipPlane=CameraSpacePlane(reflectionCamera,pos,normal,1.0f);Matrix4x4projection=cam.projectionMatrix;CalculateObliqueMatrix(refprojection,clipPlane);reflectionCamera.projectionMatrix=projection;reflectionCamera.cullingMask=~(14)&m_ReflectLayers.value;//neverrenderwaterlayerreflectionCamera.targetTexture=m_ReflectionTexture;GL.SetRevertBackfacing(true);reflectionCamera.transform.position=newpos;Vector3euler=cam.transform.eulerAngles;reflectionCamera.transform.eulerAngles=newVector3(0,euler.y,euler.z);reflectionCamera.Render();reflectionCamera.transform.position=oldpos;GL.SetRevertBackfacing(false);Material[]materials=renderer.sharedMaterials;foreach(Materialmatinmaterials){if(mat.HasProperty(_ReflectionTex))mat.SetTexture(_ReflectionTex,m_ReflectionTexture);}//SetmatrixontheshaderthattransformsUVsfromobjectspaceintoscreen//space.Wewanttojustprojectreflectiontextureonscreen.Matrix4x4scaleOffset=Matrix4x4.TRS(newVector3(0.5f,0.5f,0.5f),Quaternion.identity,newVector3(0.5f,0.5f,0.5f));Vector3scale=transform.lossyScale;Matrix4x4mtx=transform.localToWorldMatrix*Matrix4x4.Scale(newVector3(1.0f/scale.x,1.0f/scale.y,1.0f/scale.z));mtx=scaleOffset*cam.projectionMatrix*cam.worldToCameraMatrix*mtx;foreach(Materialmatinmaterials){mat.SetMatrix(_ProjMatrix,mtx);}//Restorepixellightcountif(m_DisablePixelLights)QualitySettings.pixelLightCount=oldPixelLightCount;s_InsideRendering=false;}//CleanupalltheobjectswepossiblyhavecreatedvoidOnDisable(){if(m_ReflectionTexture){DestroyImmediate(m_ReflectionTexture);m_ReflectionTexture=null;}foreach(DictionaryEntrykvpinm_ReflectionCameras)DestroyImmediate(((Camera)kvp.Value).gameObject);m_ReflectionCameras.Clear();}privatevoidUpdateCameraModes(Camerasrc,Cameradest){if(dest==null)return;//setcameratoclearthesamewayascurrentcameradest.clearFlags=src.clearFlags;dest.backgroundColor=src.backgroundColor;if(src.clearFlags==CameraClearFlags.Skybox){Skyboxsky=src.GetComponent(typeof(Skybox))asSkybox;Skyboxmysky=dest.GetComponent(typeof(Skybox))asSkybox;if(!sky||!sky.material){mysky.enabled=false;}else{mysky.enabled=true;mysky.material=sky.material;}}//updateothervaluestomatchcurrentcamera.//evenifwearesupplyingcustomcamera&projectionmatrices,//someofvaluesareusedelsewhere(e.g.skyboxusesfarplane)dest.farClipPlane=src.farClipPlane;dest.nearClipPlane=src.nearClipPlane;dest.orthographic=src.orthographic;dest.fieldOfView=src.fieldOfView;dest.aspect=src.aspect;dest.orthographicSize=src.orthog