欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity3D Shader实现动态星空

程序员文章站 2022-11-19 18:56:57
本文实例为大家分享了unity3d shader实现动态星空的具体代码,供大家参考,具体内容如下shader "unlit/test"{properties{[gamma][header(cubema...

本文实例为大家分享了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 100
cull off
zwrite off
cgprogram
#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);
#else
o.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_on
o.emission=finalcolor.rgb;
#else
o.emission=cubecolor.rgb;
#endif
o.alpha=1;
 
 
}
endcg
}
 
}

spec:亮度

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

rotationspeed:旋转速度

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

Unity3D Shader实现动态星空

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