如何将纹理图像映射到着色器几何体?

时间:2019-01-01 23:22:51

标签: javascript three.js glsl

我有2个场景,在其中一个场景中,我已将纹理图像映射到平面几何图形,我只是对其进行了渲染,在另一个场景中,我有一个带有着色器材质的立方体,现在我想要用hte显示的图像纹理第一个要映射到立方体曲面的场景,但是我不知道该怎么办,有人可以帮忙吗?

实际上,我没有足够的文档来做我想做的事情,而且我对Three.js还是陌生的,所以我不知道我应该在HTML文件的顶点着色器和片段着色器中做什么,只是做了我刚才提到的内容。

这是我在第一个场景中的纹理图像和平面几何图形,在另一个场景中是立方体,还有片段和顶点着色器:

this.vertShader = document.getElementById('vertexShader').innerHTML;
this.fragShader = document.getElementById('fragmentShader').innerHTML;
var geometry = new THREE.BoxGeometry( 0.5, 0.5 );

var material = new THREE.MeshLambertMaterial( { color: "blue", wireframe: 
true} );
this.mesh = new THREE.Mesh( geometry, material );
this.scene.add( this.mesh );

var texture = new THREE.TextureLoader().load ('js/textures/earth.jpg');
var texMaterial = new THREE.MeshBasicMaterial( { map: texture } );
var texGeometry = new THREE.PlaneGeometry(1, 1);
this.texmesh = new THREE.Mesh(texGeometry, texMaterial);
this.texmesh.position.set(0,0,0);
this.texScene.add(this.texmesh);

顶点着色器:     变化的vec2 vUv;

void main() {
    vUv = uv;

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

片段着色器:      均匀的sampler2D纹理;

varying vec2 vUv;

void main() {
    gl_FragColor = texture2D(texture, vUv);
}

我希望立方体被纹理图像覆盖。

1 个答案:

答案 0 :(得分:0)

在片段着色器中,必须声明一个sampler2D类型的uniform变量:

Vertex Shader

varying vec2 vUv;

void main() {
    vUv = uv;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}

Fragment Shader

precision highp float;

uniform sampler2D u_texture; // <---------------------------------- texture sampler uniform

varying vec2 vUv;

void main(){
    gl_FragColor = texture2D(u_texture, vUv);
}

使用着色器可以创建THREE.ShaderMaterial

首先加载纹理:

var texture = new THREE.TextureLoader().load ('js/textures/earth.jpg');

然后指定一组Uniform(在这种情况下,仅纹理统一):

var uniforms = {
    u_texture: {type: 't', value: texture}
};

最后创建材料:

var material = new THREE.ShaderMaterial({  
      uniforms: uniforms,
      vertexShader: document.getElementById('vertex-shader').textContent,
      fragmentShader: document.getElementById('fragment-shader').textContent
});

该材料可以与任何其他材料相同的方式使用,请参见示例:

(function onLoad() {
  var loader, camera, scene, renderer, orbitControls;
  
  init();
  animate();

  function createModel() {

    var texture = new THREE.TextureLoader().load( 'https://raw.githubusercontent.com/Rabbid76/graphics-snippets/master/resource/texture/Gominolas.png' );

    var uniforms = {
        u_texture: {type: 't', value: texture}
    };
        
    var material = new THREE.ShaderMaterial({  
          uniforms: uniforms,
          vertexShader: document.getElementById('vertex-shader').textContent,
          fragmentShader: document.getElementById('fragment-shader').textContent
    });

    var geometry = new THREE.BoxGeometry( 1, 1, 1 );
    var mesh = new THREE.Mesh(geometry, material);

    scene.add(mesh);
  }

  function init() {
    renderer = new THREE.WebGLRenderer({
      antialias: true,
      alpha: true
    });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMap.enabled = true;
    document.body.appendChild(renderer.domElement);

    camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 100);
    camera.position.set(0, 1, -2);

    loader = new THREE.TextureLoader();
    loader.setCrossOrigin("");

    scene = new THREE.Scene();
    scene.background = new THREE.Color(0xffffff);
    scene.add(camera);
    window.onresize = resize;
    
    orbitControls = new THREE.OrbitControls(camera);
    
    addGridHelper();
    createModel();
  }

  function addGridHelper() {
    
    var helper = new THREE.GridHelper(100, 100);
    helper.material.opacity = 0.25;
    helper.material.transparent = true;
    scene.add(helper);

    var axis = new THREE.AxesHelper(1000);
    scene.add(axis);
  }

  function resize() {
    
    var aspect = window.innerWidth / window.innerHeight;
    renderer.setSize(window.innerWidth, window.innerHeight);
    camera.aspect = aspect;
    camera.updateProjectionMatrix();
  }

  function animate() {
    requestAnimationFrame(animate);
    orbitControls.update();
    render();
  }

  function render() {
    renderer.render(scene, camera);
  }
})();
<script type='x-shader/x-vertex' id='vertex-shader'>
varying vec2 vUv;
void main() {
    vUv = uv;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>

<script type='x-shader/x-fragment' id='fragment-shader'>
precision highp float;
uniform sampler2D u_texture;
varying vec2 vUv;
void main(){
    gl_FragColor = texture2D(u_texture, vUv);
}
</script>

<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>