0

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

 _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"
}
4

1 回答 1

1

您是否将它们声明给属性之外的着色器?

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"
}
于 2013-01-24T19:11:21.603 回答