GLSL变量没有存储?

时间:2016-08-19 01:49:34

标签: variables unity3d glsl shader

我正在通过Unity学习GLSL,最近我遇到了一个涉及存储变量的问题。

Shader" Shader" {

Properties{
    _Hole_Position("hole_Position", Vector) = (0., 0., 0., 1.0)
    _Hole_EventHorizonDistance("hole_EventHorizonDistance", Float) = 1.0
    _DebugValue("debugValue", Float) = 0.0
}
SubShader{
    Pass{
    GLSLPROGRAM
    uniform mat4 _Object2World;
//Variables
varying float debugValue;

varying vec4 pos;
varying vec4 hole_Position;
varying float hole_EventHorizonDistance = 1;
#ifdef VERTEX
void main()
{
    pos = _Object2World * gl_Vertex;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#endif
#ifdef FRAGMENT
void main()
{
    float dist = distance(vec4(pos.x, 0.0,pos.z, 1.0), vec4(hole_Position.x, 0.0, hole_Position.z, 1.0));
    debugValue = dist;
    if (dist < hole_EventHorizonDistance)
    {
        gl_FragColor = vec4(0.3, 0.3, 0.3, 1.0);
    }
    else
    {
        gl_FragColor = vec4(0.4, 0.6, 1.0, 1.0);
    }
    //gl_FragColor = vec4(hole_EventHorizonDistance, 0, 0, 1.0);
}
#endif
ENDGLSL
}
}
}

现在,Hole_Position和EventHorizo​​nDistance从外部C#-script改为:

g.GetComponent<Renderer>().sharedMaterial.SetVector("_Hole_Position", new Vector4(transform.position.x, transform.position.y, transform.position.z, 1));
g.GetComponent<Renderer>().sharedMaterial.SetFloat("_Hole_EventHorizonDistance", 2);

这不起作用,因为我也打算这样做(如果它的位置在距离Hole_Position 2个单位内,则更改碎片颜色。但是调试时:

gl_FragColor = vec4(hole_EventHorizonDistance, 0, 0, 1.0);

似乎表明EventHorizo​​n始终为0(测试的网格仍为完全黑色),但是通过从外部获取和打印变量进行调试(通过

 print(g.GetComponent<Renderer>().sharedMaterial.GetFloat("_Hole_EventHorizonDistance"));

)告诉我EventHorizo​​nDistance = 2.我无法理解为什么会这样,为什么会这样呢?

0 个答案:

没有答案