2012-06-26 180 views
2

這些着色器在我的遊戲引擎工作的偉大,但是當我試圖用他們的WebGL吐了一大堆我的錯誤使用它們,WebGL的着色器錯誤

ERROR: 0:21: 'for' : Invalid init declaration 
ERROR: 0:2: '' : Version number not supported by ESSL 
ERROR: 0:7: 'ftransform' : no matching overloaded function found 
ERROR: 0:7: 'assign' : cannot convert from 'const mediump float' to 'Position highp 4-component vector of float' 
ERROR: 0:9: 'gl_MultiTexCoord0' : undeclared identifier 
ERROR: 0:9: 'assign' : cannot convert from 'float' to 'varying highp 4-component vector of float' 

因此,誰能幫助?

<script id="shader-fs" type="x-shader/x-fragment"> 
    precision mediump float; 

    const float BLOOM_AMOUNT = 10.0; 
// Increasing range can lower your FPS. 
const int BLOOM_RANGE = 3; 


uniform sampler2D composite; 
varying vec4 texcoord; 
varying vec4 texel; 


     vec4 addBloom(vec4 c, vec2 t) { 
       int j; 
       int i; 
       vec4 bloom = vec4(0.0); 
       vec2 loc = vec2(0.0); 
       float count = 0.0; 

       for(i= -BLOOM_RANGE ; i < BLOOM_RANGE; i++) { 
         for (j = -BLOOM_RANGE; j < BLOOM_RANGE; j++) { 
           loc = t + vec2(j, i)*0.004; 

           // Only add to bloom texture if loc is on-screen. 
           if(loc.x > 0.0 && loc.x < 1.0 && loc.y > 0.0 && loc.y < 1.0) { 
             bloom += texture2D(composite, loc) * BLOOM_AMOUNT; 
             count += 1.0; 
           } 
         } 
       } 
       bloom /= vec4(count); 

       if (c.r < 0.3) 
       { 
         return bloom*bloom*0.012; 
       } 
       else 
       { 
         if (c.r < 0.5) 
         { 
           return bloom*bloom*0.009; 
         } 
         else 
         { 
           return bloom*bloom*0.0075; 
         } 
       } 
     } 


void main() { 


     vec4 color = texture2D(composite, texcoord.st); 

     color += addBloom(color, texcoord.st); 

     gl_FragColor = color; 
} 
</script> 

<script id="shader-vs" type="x-shader/x-vertex"> 
    #version 120 

    varying vec4 texcoord; 

    void main() { 
     gl_Position = ftransform(); 

     texcoord = gl_MultiTexCoord0; 
    } 
</script> 

回答

7

WebGL的是基於OpenGL ES,這是不一樣的OpenGL(它幾乎是一個子集)。 OpenGL ES的GLSL版本中有許多功能缺失或不同。

這裏有問題,我可以從你的錯誤消息確定:

  • 在for循環中,變量必須在環頭本身被宣佈,而不是外面:

    for(int i = -BLOOM_RANGE ; i < BLOOM_RANGE; i++) { 
    
  • OpenGL ES中完全沒有固定功能模式;你必須編寫所有你自己的轉換和光照(或者,當然,使用提供它的框架)。這是缺少ftransform()gl_MultiTexCoord0的原因。 (我相信轉換錯誤是由此級聯的。)

  • GLSL ES使用不同的版本號,這就是它反對你的版本聲明的原因。

0

WebGL的是基於OpenGL ES 2,其不使用任何內建的東西(例如,ftransformgl_MultiTexCoord0)。否則,代碼在語法上是很好的 - 編譯好的OpenGL ES 1的目的(但不去WebGL)。