Complex shape character outline

后端 未结 5 2037
太阳男子
太阳男子 2020-12-05 01:07

Say I have this character and I want allow user to select it, so when it s selected I want to show an outline around it.

5条回答
  •  既然无缘
    2020-12-05 01:44

    An generic solution that applies to geometries of any complexity might be to apply a fragment shader via the ShaderMaterial class in three.js. Not sure what your experience level is at, but if you need it an introduction to shaders can be found here.

    A good example where shaders are used to highlight geometries can be found here. In their vertex shader, they calculate the normal for a vertex and a parameter used to express intensity of a glow effect:

    uniform vec3 viewVector;
    uniform float c;
    uniform float p;
    varying float intensity;
    void main() 
    {
        vec3 vNormal = normalize( normalMatrix * normal );
        vec3 vNormel = normalize( normalMatrix * viewVector );
        intensity = pow( c - dot(vNormal, vNormel), p );
    
        gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
    }
    

    These parameters are passed to the fragment shader where they are used to modify the color values of pixels surrounding the geometry:

    uniform vec3 glowColor;
    varying float intensity;
    void main() 
    {
        vec3 glow = glowColor * intensity;
        gl_FragColor = vec4( glow, 1.0 );
    }
    

提交回复
热议问题