Unity shader(1)舞台灯

Shader "CG shader"{
   Properties {
      _Point ("a point in world space", Vector) = (0., 0., 0., 1.0)
      _DistanceNear ("threshold distance", Float) = 5.0
      _ColorNear ("color near to point", Color) = (0.0, 1.0, 0.0, 1.0)
      _ColorFar ("color far from point", Color) = (0.3, 0.3, 0.3, 1.0)
   }
    SubShader{
        Pass{
          CGPROGRAM
          #pragma vertex vert
          #pragma fragment frag
          
          #include "UnityCG.cginc"
          
          uniform float4 _Point;
          uniform float _DistanceNear;
          uniform float4 _ColorNear;
          uniform float4 _ColorFar;
          
          struct vertexInput{
             float4 vertex:POSITION;
          };
          struct vertexOutput{
             float4 pos :SV_POSITION;
             float4 position_in_world_space:TEXCOORD0;
          };
          vertexOutput vert(vertexInput input)
          {
              vertexOutput output;
              output.pos=mul(UNITY_MATRIX_MVP,input.vertex);
              output.position_in_world_space=mul(_Object2World,input.vertex);//_Object2WorldUnityCG.cginc有定义 物体坐标转到世界坐标矩阵
              return output;
          }
          float4 frag(vertexOutput input):COLOR
          {
              float dist=distance(input.position_in_world_space,_Point);
              if(dist<_DistanceNear)
              {
                return _ColorNear;
              }
              else
              {
                return _ColorFar;
              }
          }
          ENDCG
       }
     }  
} 

以上是所需shader文件

所需控制脚本: 

   void Start () {
        InvokeRepeating("setPositon",1,0.5f);
    }

void setPositon()
    {
        float x=Random.Range(-40,40);
            float z=Random.Range(56,138);
        Vector3 vec3=new Vector3(x,-3,z);
        renderer.sharedMaterial.SetVector("_Point",vec3);
    }

Main Camera:pos:-33 60 -3  rotation :22 0 0

panle(赋shader 托脚本的):pos 0 0 100   scale:10 1 10

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值