在可以在代码中使用的着色器中定义变量

时间:2013-01-24 15:38:16

标签: unity3d

我在自定义着色器中定义了一些变量:

 _Emiss("emission", Float) = .5
 _Albed("albedo", Float) = .9

直到这里一切都很好,但是当我尝试在代码中使用变量时:

void surf (Input IN, inout SurfaceOutput o) {
          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Albed;
          o.Emission = texCUBE (_Cube, IN.worldRefl).rgb * _Emiss;
      }

我收到一些错误,告诉我_Albed和_Emiss没有被声明。

我做错了什么?

这是完整的着色器:

Shader "Custom/cubemapReflShader" {


        Properties {
      _MainTex ("Texture", 2D) = "red" {}
      _Cube ("Cubemap", CUBE) = "" {}
      _Emiss("emission", Float) = .5
      _Albed("albedo", Float) = .9
      // _RefrColor ("Refraction color", Color)  = (.34, .85, .92, 1) // color
    }



    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
      #pragma surface surf Lambert
      struct Input {
          float2 uv_MainTex;
          float3 worldRefl;
      };
      sampler2D _MainTex;
      samplerCUBE _Cube;
      void surf (Input IN, inout SurfaceOutput o) {
          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * .9;
          o.Emission = texCUBE (_Cube, IN.worldRefl).rgb * .5;
      }
      ENDCG
    } 
    Fallback "Diffuse"
}

1 个答案:

答案 0 :(得分:1)

你有declare them to the shader在属性之外吗?

Properties 
{
    _Emiss("emission", Float) = .5
    _Albed("albedo", Float) = .9
}

float _Emiss;
float _Albed;

void surf (Input IN, inout SurfaceOutput o) {
    o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Albed;
    o.Emission = texCUBE (_Cube, IN.worldRefl).rgb * _Emiss;
}

<强>更新

修改OP的完整脚本:

Shader "Custom/cubemapReflShader" {
    Properties {
  _MainTex ("Texture", 2D) = "red" {}
  _Cube ("Cubemap", CUBE) = "" {}
  _Emiss("emission", Float) = .5
  _Albed("albedo", Float) = .9
}
SubShader {
  Tags { "RenderType" = "Opaque" }
  CGPROGRAM
  #pragma surface surf Lambert
  struct Input {
      float2 uv_MainTex;
      float3 worldRefl;
  };
  sampler2D _MainTex;
  samplerCUBE _Cube;

  // PUT THESE HERE <----------
  float _Albed;
  float _Emiss;

  void surf (Input IN, inout SurfaceOutput o) {
      o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Albed;
      o.Emission = texCUBE (_Cube, IN.worldRefl).rgb * _Emiss;
  }
  ENDCG
} 
Fallback "Diffuse"
}