【问题标题】:webgl / three js shader errorwebgl/三个js着色器错误
【发布时间】:2016-11-17 19:12:44
【问题描述】:

我是来问你三个 js 中遇到的问题。 我尝试制作视差映射。

顶点着色器:

varying vec3 v_pos;
  varying vec3 v_nrm;
  varying vec2 v_txc;

  void main(){
    v_pos = position;
    v_nrm = normal;
    v_txc = uv;

    gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
  }

片段着色器:

  uniform vec3 camPos;
  uniform sampler2D heightMap;
  uniform sampler2D textureDiffuse;
  uniform float bumpness;

  varying vec3 v_pos;
  varying vec3 v_nrm;
  varying vec2 v_txc;

  float ApplyChainRule( float dhdu, float dhdv, float dud_, float dvd_ )
  {
      return dhdu * dud_ + dhdv * dvd_;
  }

  vec3 SurfaceGradient( vec3 n, vec3 dpdx, vec3 dpdy, float dhdx, float dhdy )
  {
      vec3 r1 = cross( dpdy, n );
      vec3 r2 = cross( n, dpdx );
      float det = dot( dpdx, r1 );

      return ( r1 * dhdx + r2 * dhdy ) / det;
  }
   
  void main()
  {
      vec3 wsViewDir = normalize( camPos - v_pos );
      vec3 wsNormal = normalize( v_nrm );
       
      vec3 dpdx = dFdx( v_pos );
      vec3 dpdy = dFdy( v_pos );
   
      vec2 uv = v_txc;
      vec2 duvdx = dFdx( uv );
      vec2 duvdy = dFdy( uv );
   
      // here will be the POM code
       
      vec2 dhduv = texture2D( heightMap, uv ).rg;
      dhduv = ( dhduv * 2.0 - 1.0 ) * bumpness;
       
      float dhdx = ApplyChainRule( dhduv.x, dhduv.y, duvdx.x, duvdx.y );
      float dhdy = ApplyChainRule( dhduv.x, dhduv.y, duvdy.x, duvdy.y );
       
      wsNormal = normalize( wsNormal - SurfaceGradient( wsNormal, dpdx, dpdy, dhdx, dhdy ) );

      gl_FragColor = vec4(texture2D( textureDiffuse, uv )+(wsNormal * 0.5 +0.5),1.0);
   
      // here will be the lighting and shading code
  }

我有这个错误:

THREE.WebGLShader: gl.getShaderInfoLog() 片段错误: 0:? : '' : 语法错误

我找不到我的错误,你能帮我吗?

谢谢 ;)

【问题讨论】:

  • 发布您的实际代码,而不仅仅是着色器。因为着色器在其他地方工作,这表明错误在于您如何向 three.js 提供着色器

标签: javascript three.js webgl shader


【解决方案1】:

你的意思是:

gl_FragColor = texture2D(textureDiffuse, uv) + vec4(wsNormal * 0.5 + 0.5, 1.0);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
  • 2022-01-18
  • 2017-03-28
  • 1970-01-01
  • 2017-08-07
  • 2017-07-03
相关资源
最近更新 更多