Occlusion transparency If there is no gradient implementation method:
1, transparent objects exist in the list
2, each period of time (can each frame, but the smoothness will be reduced) the camera sends a ray to the player, out Hitinfo
3, List and Hitinfo, will be in the list but not in the Hitinfo object into an opaque, list.remove; will be in the hitinfo but not in the list transit becomes transparent, list.add
If there is a gradient, the implementation is more troublesome, the object has two states: into the transparent process, into the opaque process, add two list (intransparent,outtransparent) corresponding to the two states, then maintain two lists can:
1, not obscured, occlusion, view outtransparent, if there is an object, move from outtransparent to Intransparent,outtransparent remove ; If you do not add a new join intransparent
2, occlusion, not obscured, see intransparent, if there is this object, from Intransparent to outtransparent,intransparent remove ; If you do not add a new join Outtransparent
Camera CameraFollow.cs:
1 Public classCamerafollow:monobehaviour2 {3 4 Public StaticCamerafollow instance;5 PublicTransform Player;6 7 PublicList<cameramaskinfo>Mask;8 PublicList<cameramaskinfo>intransparentlist;9 PublicList<cameramaskinfo>outtransparentlist;Ten One raycasthit[] Hitinfo; A - voidAwake () - { theInstance = This; - } - voidStart () - { + Startcoroutine (Iemask ()); - } + Public voidClearmask () A { at mask.clear (); - intransparentlist.clear (); - outtransparentlist.clear (); - } - - IEnumerator iemask () in { - while(true) to { +Hitinfo = Physics.capsulecastall (followtarget.position + vector3.up *0.2f, Followtarget.position-vector3.up *0.1f,0.2f, (Transform.position-followtarget.position). Normalized, 100f, layermask); - BOOLFlag; the for(inti = Mask.count-1; I >=0; i--) * { $Flag =false;Panax Notoginseng for(intj =0; J < Hitinfo.length; J + +) - { the if(Mask[i]. Getobj () = =hitinfo[j].collider.gameobject) + { AFlag =true; the Break; + } - } $ if(!flag) $ { - Mask[i]. Putouttransparent (); - mask.removeat (i); the } - }Wuyi the for(inti =0; i < hitinfo.length; i++) - { WuFlag =false; - for(intj =0; J < Mask.count; J + +) About { $ if(Hitinfo[i].collider.gameobject = =Mask[j]. Getobj ()) - { -Flag =true; - Break; A } + } the if(!flag) - { $Cameramaskinfo Maskinfo =NewCameramaskinfo (hitinfo[i].collider.gameobject); the maskinfo.putintransparent (); the Mask.add (maskinfo); the } the } - } in yield return NewWaitforseconds (0.2f); the } the About voidUpdate () the { the for(inti = Intransparentlist.count-1; I >=0; i--) the { + Intransparentlist[i]. Sub (); - } the Bayi for(inti = Outtransparentlist.count-1; I >=0; i--) the { the Outtransparentlist[i]. Add (); - } - } the}
View Code
Occlusion Object CameraMaskInfo.cs:
usingUnityengine;usingSystem.Collections; Public classCameramaskinfo {PrivateGameobject obj; Privateshader[] Prematerialshader; Public BOOLIsdestroy; #regionGet Publicgameobject getobj () {returnobj; } Publicshader[] Getprematerialshader () {returnPrematerialshader; } #endregion Publiccameramaskinfo (Gameobject obj) { This. obj =obj; } Public voidputintransparent () {intID =-1; for(inti =0; i < CameraFollow.instance.OutTransparentList.Count; i++) { if(Camerafollow.instance.outtransparentlist[i]. Getobj () = =obj) {ID=i; //if there is in out, then the shader of the object has been changed and can only be obtained from the previousPrematerialshader =Camerafollow.instance.outtransparentlist[i]. Getprematerialshader (); CAMERAFOLLOW.INSTANCE.INTRANSPARENTLIST.ADD (Camerafollow.instance.outtransparentlist[i]); CameraFollow.instance.OutTransparentList.RemoveAt (i); Break; } } if(id==-1) {Prematerialshader=NewShader[obj.renderer.materials.length]; for(inti =0; i < obj.renderer.materials.Length; i++) {Prematerialshader[i]=Obj.renderer.materials[i].shader; Obj.renderer.materials[i].shader= Shader.find ("Transparent/diffuse"); } CameraFollow.instance.InTransparentList.Add ( This); } } Public voidputouttransparent () {intID =-1; for(inti =0; i < CameraFollow.instance.InTransparentList.Count; i++) { if(Camerafollow.instance.intransparentlist[i]. Getobj () = =obj) {ID=i; CAMERAFOLLOW.INSTANCE.OUTTRANSPARENTLIST.ADD (Camerafollow.instance.intransparentlist[i]); CameraFollow.instance.InTransparentList.RemoveAt (i); Break; } } if(id = =-1) {CAMERAFOLLOW.INSTANCE.OUTTRANSPARENTLIST.ADD ( This); } } Public voidAdd () { for(inti =0; i < obj.renderer.materials.Length; i++) {Obj.renderer.materials[i].color=NewColor (OBJ.RENDERER.MATERIALS[I].COLOR.R, OBJ.RENDERER.MATERIALS[I].COLOR.G, Obj.renderer.materials[i].color.b, OBJ.RENDERER.MATERIALS[I].COLOR.A + time.deltatime *2f); } if(obj.renderer.materials[0].COLOR.A >=0.95f) { for(inti =0; i < obj.renderer.materials.Length; i++) {Obj.renderer.materials[i].color=NewColor (OBJ.RENDERER.MATERIALS[I].COLOR.R, OBJ.RENDERER.MATERIALS[I].COLOR.G, Obj.renderer.materials[i]. Color.b, 1f); } setpreshader (); CameraFollow.instance.OutTransparentList.Remove ( This); } } Public voidSub () { for(inti =0; i < obj.renderer.materials.Length; i++) {Obj.renderer.materials[i].color=NewColor (OBJ.RENDERER.MATERIALS[I].COLOR.R, OBJ.RENDERER.MATERIALS[I].COLOR.G, Obj.renderer.materials[i].color.b, Obj.renderer.materials[i].color.a-time.deltatime *2f); } if(obj.renderer.materials[0].COLOR.A <=0.3f) {CameraFollow.instance.InTransparentList.Remove ( This); } } Private voidSetpreshader () { for(inti =0; i < obj.renderer.materials.Length; i++) {Obj.renderer.materials[i].shader=Prematerialshader[i]; } }}
View Code
Effect:
Unity Occlusion Transparent Gradient