平移时着色器材质发光,放大三个.js

时间:2016-01-09 21:21:25

标签: debugging three.js shader vertex-shader

我使用以下代码创建了这样的虚拟地球:

function earthView(){
if (!scene){
    main(); // create three js basic code(camera, renderer etc.)
}

// create the geometry sphere stars
var geometry  = new THREE.SphereBufferGeometry(6371000000, 36, 36)
// create the material, using a texture of startfield
var material  = new THREE.MeshBasicMaterial()
material.map   = THREE.ImageUtils.loadTexture('images/earthView/ESO_-_Milky_Way.jpg')
material.side  = THREE.BackSide
// create the mesh based on geometry and material
var mesh  = new THREE.Mesh(geometry, material)
mesh.position.set(0,0,-6371000)

scene.add(mesh)
/*
var geometry = new THREE.SphereGeometry(5000,10,10);
var material = new THREE.MeshBasicMaterial({color:"0xff0000"});
var mesh_test = new THREE.Mesh(geometry,material);
scene.add(mesh_test);*/


//earth
var geometry   = new THREE.SphereBufferGeometry(6371000, 36, 36)
var material  = new THREE.MeshPhongMaterial()
var earthMesh = new THREE.Mesh(geometry, material)
earthMesh.position.set(0,0,-6371000)
earthMesh.rotation.set(Math.PI/2,Math.PI/2,0)
earthMesh.rotation.y -=22.87*Math.PI/180//rightturn ^
earthMesh.rotation.x +=49.02*Math.PI/180//rightturn ^
helper = new THREE.EdgesHelper( earthMesh );
helper.material.color.set( 0xffffff );


material.map    = THREE.ImageUtils.loadTexture('images/earthView/earthmap1k.jpg')
material.bumpMap    = THREE.ImageUtils.loadTexture('images/earthView/earthbump1k.jpg')
material.bumpScale = 100


material.specularMap = THREE.ImageUtils.loadTexture('images/earthView/earthspec1k.jpg')

scene.add(earthMesh);
scene.add( helper );

//atmosphere
var geometry   = new THREE.SphereBufferGeometry(9371000, 36, 36)
var material  = new createAtmosphereMaterial()
material.uniforms.glowColor.value.set(0x00b3ff)
material.uniforms.coeficient.value  = 0.02
material.uniforms.power.value       = 2.5
material.side = THREE.DoubleSide
var earthAtmo = new THREE.Mesh(geometry, material)
earthAtmo.position.set(0,0,-6371000)

scene.add(earthAtmo);

/**
 * from http://stemkoski.blogspot.fr/2013/07/shaders-in-threejs-glow-and-  halo.html
* @return {[type]} [description]
*/ 

function createAtmosphereMaterial(){
  var vertexShader  = [
    'varying vec3 vNormal;',
    'void main(){',
    '   // compute intensity',
    '   vNormal     = normalize( normalMatrix * normal );',
    '   // set gl_Position',
    '   gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
    '}',
].join('\n')
var fragmentShader  = [
    'uniform float coeficient;',
    'uniform float power;',
    'uniform vec3  glowColor;',

    'varying vec3  vNormal;',

    'void main(){',
    '   float intensity = pow( coeficient - dot(vNormal, vec3(0.0, 0.0, 1.0)), power );',
    '   gl_FragColor    = vec4( glowColor * intensity, 1.0 );',
    '}',
].join('\n')

// create custom material from the shader code above
//   that is within specially labeled script tags
var material    = new THREE.ShaderMaterial({
    uniforms: { 
        coeficient  : {
            type    : "f", 
            value   : 1.0
        },
        power       : {
            type    : "f",
            value   : 2
        },
        glowColor   : {
            type    : "c",
            value   : new THREE.Color('blue')
        },
    },
    vertexShader    : vertexShader,
    fragmentShader  : fragmentShader,
    side        : THREE.BackSide,
    blending    : THREE.AdditiveBlending,
    transparent : true,
    depthWrite  : false,
});

return material
}


}

previus question中,我遇到了渲染器的问题,因为我以实际比例创建了虚拟地球(1px = 1m)。我通过在定义渲染器时设置logarithmicDepthBuffer: true来克服此错误。

现在问题是大气(发光着色器材质)在平移或放大webgl时有一个错误 - 容器已经说明了,here是解决这个问题的一个例子。

问题是: 如何更改我的代码来克服这个错误?(我想在渲染功能中有一些东西需要添加,但我可以&#39 ;让它工作)。

提示1:此错误仅在设置logarithmicDepthBuffer: true时发生。否则,由于我正在使用的大型物体,我得到了错误的渲染。

Image1:渲染选项logarithmicDepthBuffer: false,没有错误,只有错误渲染。

image using <code>logarithmicDepthBuffer: false</code>

Image2:渲染选项logarithmicDepthBuffer: true,没有错误,如果没有缩放或平移。

image using <code>logarithmicDepthBuffer: true</code>,no bug without pan-zoom

Image3:渲染选项logarithmicDepthBuffer: true,当应用着色器材质接缝的区域放大变小时。

enter image description here

Image4:渲染选项logarithmicDepthBuffer: true,当平移所应用着色器材质接缝的区域时,不要跟随或理解平移。

enter image description here

提示2:着色器材质可渲染的区域在缩小时变大,在放大时变小。

更新:现在看看,当我添加星球时会出现问题。如果我不添加星球,那么一切都正常..任何想法为什么会发生这种情况?

0 个答案:

没有答案