使用threejs和着色器显示球体发光的问题

时间:2015-05-29 00:37:43

标签: three.js shader

我正在进行三维可视化。我需要展示一个充满氛围的世界,我基于一个与threejs r40相关的例子,但我的是r71。

当我尝试添加气氛(表示世界的球体的发光类型)时,我收到以下错误:

three.min.js:445 Uncaught TypeError: c.addEventListener is not a function

我的代码如下:

scene.add(createAtmosphere());

function createAtmosphere() {

    shader = Shaders['atmosphere'];
    uniforms = THREE.UniformsUtils.clone(shader.uniforms);
    mesh = new THREE.Mesh(new THREE.Sphere(200, 40, 30), 
        new THREE.ShaderMaterial({

          uniforms: uniforms,
          vertexShader: shader.vertexShader,
          fragmentShader: shader.fragmentShader

        })
      );

    mesh.scale.x = mesh.scale.y = mesh.scale.z = 1.1;
    mesh.flipSided = true;
    mesh.matrixAutoUpdate = false;
    mesh.updateMatrix();
    return mesh;
  }

这是着色器

var Shaders = {
    'atmosphere' : {
      uniforms: {},
      vertexShader: [
        'varying vec3 vNormal;',
        'void main() {',
          'vNormal = normalize( normalMatrix * normal );',
          'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
        '}'
      ].join('\n'),
      fragmentShader: [
        'varying vec3 vNormal;',
        'void main() {',
          'float intensity = pow( 0.8 - dot( vNormal, vec3( 0, 0, 1.0 ) ), 12.0 );',
          'gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 ) * intensity;',
        '}'
      ].join('\n')
    }
  };

我解决了另一个着色器问题,但我找不到这个问题(我不熟悉着色器,它让我发疯)。

提前致谢

1 个答案:

答案 0 :(得分:1)

您必须创建发送SphereGeometry的网格,而不是Sphere

http://threejs.org/docs/#Reference/Objects/Mesh http://threejs.org/docs/#Reference/Extras.Geometries/SphereGeometry

scene.add(createAtmosphere());

function createAtmosphere() {

    shader = Shaders['atmosphere'];
    uniforms = THREE.UniformsUtils.clone(shader.uniforms);
    mesh = new THREE.Mesh(new THREE.SphereGeometry(200, 40, 30), 
        new THREE.ShaderMaterial({

          uniforms: uniforms,
          vertexShader: shader.vertexShader,
          fragmentShader: shader.fragmentShader

        })
      );

    mesh.scale.x = mesh.scale.y = mesh.scale.z = 1.1;
    mesh.flipSided = true;
    mesh.matrixAutoUpdate = false;
    mesh.updateMatrix();
    return mesh;
  }