Three.js - MatCap 的简单尝试

关于MatCap

MatCap(Material Capture 材质捕获)是对材质的近似,对于没有UV贴图的模型非常有效。

MatCap(它只是一个规则的位图)定义了每个法线方向的曲面颜色,这极大地有助于显示模型中的细节。

原始的MatCap在左侧,生成的着色表面在右侧。
原始的MatCap在左侧,生成的着色表面在右侧

Shader

  • Vertex Shader

顶点着色器用来将模型顶点的法向量转化为视点坐标系下的法向量,并获得该法线中的xy分量

varying vec2 Point;

void main()
{
    vec3 vNormal = ( mat3( modelViewMatrix ) * normal );
    vNormal = normalize(vNormal);
	
	// vNormal 的 x, y分量
    Point.x = vNormal.x * 0.5 + 0.5;
    Point.y = vNormal.y * 0.5 + 0.5;
    
    gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );

}
  • Fragment Shader

获取对应坐标下的MatCap的颜色值。

uniform sampler2D Matcap;      // Matcap纹理

varying vec2 Point;

void main(void){

    // texture2D()获取颜色值
    vec4 color = texture2D(Matcap, Point);

	// 改片元的颜色值为对应坐标下的MatCap的颜色值
    gl_FragColor = color;
}

效果图

在这里插入图片描述

函数封装如下

function createMatCapMaterial(texture) {
    let vertexShader = `
        varying vec2 Point;

        void main()
        {
            vUv = uv;
            vec3 vNormal = ( mat3( modelViewMatrix ) * normal );
            vNormal = normalize(vNormal);

            Point.x = vNormal.x * 0.5 + 0.5;
            Point.y = vNormal.y * 0.5 + 0.5;
            
            gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );

        }
    `;

    let fragmentShader = `
        uniform sampler2D Matcap;      // Matcap纹理

        varying vec2 Point;

        void main(void){

            // texture2D()获取颜色值
            vec4 color = texture2D(Matcap, Point);

            gl_FragColor = color;
        }
    `;

    let Material = new THREE.ShaderMaterial({
        uniforms: {
            Matcap: { value: texture },
        },
        vertexShader,
        fragmentShader
    });

    return Material;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值