Unity3D Shader实现动态星空

时间:2021-05-20

本文实例为大家分享了Unity3D Shader实现动态星空的具体代码,供大家参考,具体内容如下

Shader "Unlit/Test"{Properties{[Gamma][Header(CubeMap)]_MainColor("MainColor",COLOR)=(0.5,0.5,0.5,1)_Spec("Spec",Range(1,8))=1[NoScaleOffset]_Tex("CubeMap",CUBE)="black"{}[Header(Rotation)][Toggle(_ENABLEROTATION_ON)]_EnableRotation("Enable Rotation",Float)=0[IntRange]_Rotation("Rotation",Range(0,360))=0_RotationSpeed("RotationSpeed",float)=1[Header(Fog)][Toggle(_ENABLEFOG_ON)]_EnableFog("Enable Fog",float)=0_FogHeight("FogHeight",Range(0,1))=1_FogSmooth("FogSmooth",Range(0.01,1))=0.01_FogHill("FogHill",Range(0,1))=0.5 }SubShader{Tags { "RenderType"="Background" "Queue"="Background" "IgnoreProjector"="True" "ForceNoShadowCasting"="True"}LOD 100Cull OffZWrite OffCGPROGRAM#include "UnityShaderVariables.cginc"#include "UnityCG.cginc"#pragma target 3.0#pragma shader_feature _ENABLEROTATION_ON#pragma shader_feature _ENABLEFOG_ON#pragma surface surf Lint keepalpha noshadow noambient novertexlights nolightmap nodynlightmap nodirlightmap nofog nometa noforwardadd vertex:vertexRo struct Input{float3 worldPos;float3 vertextofrag;}; uniform float4 _MainColor;uniform float _Spec;uniform samplerCUBE _Tex;uniform half _Rotation;uniform half _RotationSpeed;uniform half _FogHeight;uniform half _FogSmooth;uniform half _FogHill;uniform half4 _Tex_HDR; half3 decode_HDR(half4 data){return DecodeHDR(data,_Tex_HDR);}void vertexRo(inout appdata_full v,out Input o){UNITY_INITIALIZE_OUTPUT(Input,o);float3 _worldPos=mul(unity_ObjectToWorld,v.vertex);float _lerpResult=lerp(1.0,(unity_OrthoParams.y/unity_OrthoParams.x),unity_OrthoParams.w);float3 _append=float3(_worldPos.x,_worldPos.y*_lerpResult,_worldPos.z);float3 _nor_append=normalize(_append);float _timeMove=_Time.y;float3 _timeMove1=float3(cos(radians(_Rotation+_timeMove*_RotationSpeed)),0,sin(radians(_Rotation+_timeMove*_RotationSpeed))*-1);float3 _timeMove2=float3(0,_lerpResult,0);float3 _timeMove3=float3(sin(radians(_Rotation+_timeMove*_RotationSpeed)),0,cos(radians(_Rotation+_timeMove*_RotationSpeed)));float3 _nor_worldPos=normalize(_worldPos);#ifdef _ENABLEROTATION_ON o.vertextofrag=mul(float3x3(_timeMove1,_timeMove2,_timeMove3),_nor_worldPos);#elseo.vertextofrag=_nor_append;#endif }fixed4 LightingLint(SurfaceOutput s,float3 lightDir,float atten){return fixed4(0,0,0,s.Alpha);}void surf(Input i,inout SurfaceOutput o){half4 CUBEdata=texCUBE(_Tex,i.vertextofrag);half3 CUBEdataHDR=decode_HDR(CUBEdata);float4 CUBEColor=(float4(CUBEdataHDR,0))*_MainColor*_Spec*unity_ColorSpaceDouble;float3 _nor_worldPos=normalize(i.worldPos);float _lerpFog=lerp(saturate(pow(_nor_worldPos.y/_FogHeight,1-_FogSmooth)),0,_FogHill);float4 FinalColor=lerp(unity_FogColor,CUBEColor,_lerpFog);#ifdef _ENABLEFOG_ONo.Emission=FinalColor.rgb;#elseo.Emission=CUBEColor.rgb;#endifo.Alpha=1; }ENDCG} }

Spec:亮度

Rotation:开始旋转角度,可以调节

RotationSpeed:旋转速度

Fog开头:雾化效果,可以看算法实现

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章