📝

GLSL|matcap

2024.03.17

SHARE

TABLE OF CONTENTS

    📝

    GLSL|matcap

    2024.03.17

    GLSLのmatcapについてメモしています。

    vec2 matcap(vec3 eye, vec3 normal) {
      vec3 reflected = reflect(eye, normal);
      float m = 2.8284271247461903 * sqrt( reflected.z+1.0 );
      return reflected.xy / m + 0.5;
    }
    
    // vs
    varying vec3 vNormal;
    varying vec3 vPosition;
    void main(){
      vPosition = (modelMatrix * vec4(position, 1.0)).xyz;
    }
    
    // fs
    varying vec3 vNormal;
    varying vec3 vPosition;
    uniform sampler2D uMatcap;
    uniform vec3 uCameraPosition;
    
    void main() {
      vec3 eyeDirection = normalize(vPosition - uCameraPosition);
      vec2 matcapUv = matcap(eyeDirection, vNormal);
      vec4 matcapTexture = texture2D(uMatcap, matcapUv);
      gl_FragColor = matcapTexture;
    }
    

    ©2025 SHOYA KAJITA.