Threepipe Tweakpane Editor Plugin
    Preparing search index...

    Class SelectionOutlinePlugin

    Selection Outline Plugin

    Draws the outline of the selected and hovered objects in the render pipeline, as in Figma or Blender.

    1. A SelectionMaskPass renders the meshes of the selected and hovered objects into a half-float mask, with its own depth buffer: R = selected item, G = part inside that item, A = linear depth of the selection, B = the hover (the inverse of its own linear depth, 0 = none). The hover is a second draw, so a hovered object in front of the selection does not hide it, and the reverse.
    2. An extension of the screen pass reads the mask after tonemapping and draws an outer outline around each selected object and the hovered object. Options: anti-aliased edges (antialias), thin lines between the parts of an item (parts), a dashed outline where the selection is behind other objects (xray), a fill tint (fill), and a dim of all other objects (dimOthers).

    The objects come from the PickingPlugin when followPicking is on (the default; for the hover, turn on PickingPlugin.hoverEnabled), else set selected and hovered. A change of the selection or the hover renders one more frame, it does not restart the progressive render. Fat lines (Line2, LineSegments2) are in the mask with their own width.

    Requires the GBufferPlugin (the depth for the x-ray outline). Not saved with the viewer config.

    Hierarchy

    • AScreenPassExtensionPlugin
      • SelectionOutlinePlugin
    Index
    __setDirty?: () => void
    _dirty: boolean = false
    _shaderPatch: string = ...
    _viewer?: ThreeViewer
    _viewerListeners: PartialRecord<IViewerEventTypes, (e: IViewerEvent) => void> = {}
    antialias: boolean = false

    Anti-aliased outline edges (the define SEL_OUTLINE_AA of the screen pass). Off by default.

    • The outer edge fades over about 1 px at the distance width from the mask edge: the distance is found by a bisection along the edge normal (and 15 deg to each side of it in the outer 1 px), not only at the radii of the search rings, so the width is the same at each angle.
    • The outline goes half a pixel into the edge of the object (its anti-aliased edge). The alpha there comes from the part of the 3x3 pixels around it in the mask, so the pixel steps of the mask do not show.

    Mask reads per pixel: 49 when off. When on, up to 63 in the outline and up to 79 in its outer 1 px.

    color: Color = ...

    The outline color. Call redraw after a change in place (color.set(...)).

    constructor: typeof AScreenPassExtensionPlugin & typeof AViewerPluginSync & typeof AViewerPlugin
    dependencies: typeof GBufferPlugin[] = ...
    dimOthers: boolean = false

    Dim all the objects that are not selected

    enabled: boolean = true
    extraDefines: { SEL_OUTLINE_AA: () => "1" | "0" } = ...
    extraUniforms: {
        selActive: { value: number };
        selColor: { value: Color };
        selDim: { value: number };
        selFill: { value: number };
        selHasSel: { value: number };
        selHoverColor: { value: Color };
        selHoverWidth: { value: number };
        selMaskSize: { value: Vector2 };
        selNearFar: { value: Vector2 };
        selParts: { value: number };
        selWidth: { value: number };
        selXray: { value: number };
        tSelectionMask: { value: Texture<TextureEventMap> | null };
    } = ...
    fill: boolean = false

    A light tint of the color inside the selection

    followPicking: boolean = true

    Use the selection and the hover of the PickingPlugin

    hoverColor: Color = ...

    The hover outline color, by default a little lighter than color. Call redraw after a change in place.

    isViewerPluginSync: true = ...
    outlineHover: boolean = true

    Outline the hovered objects

    outlineSelected: boolean = true

    Outline the selected objects

    parts: boolean = true

    Thin lines between the parts of a selected object (see partsOf)

    partsOf: (object: IObject3D) => Object3D<Object3DEventMap>[] = ...

    The parts of a selected object, for the lines between parts. Default: the children, below any chain of groups with one child (the objects that a click inside the selection selects next).

    priority: number = -110

    After tonemapping and the other extensions, so the colors are exact

    serializeWithViewer: boolean = false
    uiConfig?: UiObjectConfig<any, string, any> = undefined
    width: number = 2

    Outline width in CSS pixels. The hover outline is 3/4 of it, at least 1 px.

    xray: boolean = true

    A dashed outline where the selection is behind other objects

    OldPluginType?: string
    PluginType: "SelectionOutlinePlugin" = 'SelectionOutlinePlugin'
    • Parameters

      • e: IViewerEvent

      Returns IViewerEvent

    • Parameters

      • Optionalprefix: string

      Returns string

    • Parameters

      • key: any
      • setDirty: boolean = true

      Returns void

    • Parameters

      • key: any
      • setDirty: boolean = true

      Returns void

    • Returns ISerializedConfig | Record<string, never>

    • Parameters

      • state: any

      Returns Promise<void>

    • Function to check if this material extension is compatible with the given material. If not compatible, the material extension will not be added to the material. This is only checked when the extension is registered.

      The extension is assumed to be compatible if this function is not defined

      Parameters

      • _: IMaterial

      Returns boolean

    • Returns
          | ""
          | "\nuniform sampler2D tSelectionMask;\nuniform vec2 selMaskSize;\nuniform vec3 selColor;\nuniform vec3 selHoverColor;\nuniform float selWidth;\nuniform float selHoverWidth;\nuniform float selFill;\nuniform float selXray;\nuniform float selParts;\nuniform float selDim;\nuniform float selActive;\nuniform float selHasSel;\nuniform vec2 selNearFar;\n\n// the alpha of the outline at this pixel, set by selectionOutline\nfloat selCoverage = 0.0;\n\n// the distance of no hit in the outline search: larger than any tap radius (the width times the render scale)\nconst float selNoHit = 1.0e4;\n\n// paints a color with alpha a over dst (straight alpha), so the outline shows on a transparent background too\nvec4 selOver(vec4 dst, vec3 c, float a) {\n float outA = a + dst.a * (1.0 - a);\n if (outA <= 0.0) return vec4(dst.rgb, 0.0);\n selCoverage = max(selCoverage, a);\n return vec4((c * a + dst.rgb * dst.a * (1.0 - a)) / outA, outA);\n}\n\n// true when the masked surface at uv is hidden by another object in the scene. Both depths are\n// (viewZ - near) / (far - near). The tolerance is 0.5% of the view distance (the depth step of a sloped surface across\n// a pixel, and the precision of the two buffers), not of far - near, so a wide clipping range does not hide occlusion.\n// the linear depth of the hover from the B channel of the mask (B > 0.5 where hovered)\nfloat selHoverDepth(float b) {\n return 1.0 / b - 1e-4;\n}\n\nfloat selHidden(vec2 uv, float maskDepth) {\n float range = selNearFar.y - selNearFar.x;\n float viewZ = selNearFar.x + maskDepth * range;\n return getDepth(uv) < maskDepth - 0.005 * abs(viewZ) / range ? 1.0 : 0.0;\n}\n\n#if SEL_OUTLINE_AA\n// the bisection steps along the edge normal: the distance to the mask edge to 1/16 of the ring spacing\nconst int selAaSteps = 4;\n\n// true when the mask sample s is a hit of the search at a pixel with the item cSel and the hover cHov: another item drawn\n// over this one (the selection), or the hover where this pixel is not hovered\nbool selIsHit(vec4 s, float cSel, float cHov, bool hover) {\n if (hover) return cHov < 0.5 && s.b > 0.5;\n float sSel = floor(s.r + 0.5);\n return sSel > 0.5 && sSel != cSel && (cSel < 0.5 || sSel > cSel);\n}\n\n// the part of a pixel in the outline band from min(0.5, width) inside the edge to width outside it, for the distance s of\n// the pixel center to the edge (outward)\nfloat selBand(float s, float width) {\n return clamp(min(s + 0.5, width) - max(s - 0.5, -min(0.5, width)), 0.0, 1.0);\n}\n\n// the distance in pixels to the edge of the hits along dir, by bisection between lo (no hit) and hi (a hit). n gets the uv\n// and the mask depth of the nearest hit\nfloat selBisect(vec2 dir, float lo, float hi, float cSel, float cHov, bool hover, vec2 px, inout vec3 n) {\n for (int k = 0; k < selAaSteps; k++) {\n float m = 0.5 * (lo + hi);\n vec2 uv = vUv + dir * m * px;\n vec4 s = texture2D(tSelectionMask, uv);\n if (selIsHit(s, cSel, cHov, hover)) {\n hi = m;\n n = vec3(uv, hover ? selHoverDepth(s.b) : s.a);\n } else {\n lo = m;\n }\n }\n return 0.5 * (lo + hi);\n}\n\n// the distance in pixels to the edge of the hits, between the radius lo (no hit) and the radius hi of the nearest hits.\n// Along the sum of the directions of those hits (about the edge normal), else the direction of the first one. That\n// direction can be 15 deg off the normal (the taps are 30 deg apart), so in the outer 1 px of the outline (from width -\n// 0.5) the distance is also found 15 deg to each side, and the smallest is taken. n gets the uv and the mask depth of\n// the nearest hit\nfloat selEdgeDistance(vec2 sum, vec2 first, float lo, float hi, float width, float cSel, float cHov, bool hover, vec2 px, inout vec3 n) {\n vec2 dir = first;\n if (dot(sum, sum) > 1e-4) {\n vec2 d = normalize(sum);\n vec2 uv = vUv + d * hi * px;\n vec4 s = texture2D(tSelectionMask, uv);\n if (selIsHit(s, cSel, cHov, hover)) {\n dir = d;\n n = vec3(uv, hover ? selHoverDepth(s.b) : s.a);\n }\n }\n float t = selBisect(dir, lo, hi, cSel, cHov, hover, px, n);\n if (t > width - 0.5) {\n // cos and sin of 15 deg\n const vec2 cs = vec2(0.9659258, 0.2588190);\n vec3 n1 = n, n2 = n;\n float t1 = selBisect(vec2(dir.x * cs.x - dir.y * cs.y, dir.x * cs.y + dir.y * cs.x), lo, hi, cSel, cHov, hover, px, n1);\n float t2 = selBisect(vec2(dir.x * cs.x + dir.y * cs.y, dir.y * cs.x - dir.x * cs.y), lo, hi, cSel, cHov, hover, px, n2);\n if (t1 < t) {\n t = t1;\n n = n1;\n }\n if (t2 < t) {\n t = t2;\n n = n2;\n }\n }\n return t;\n}\n#endif\n\nvec4 selectionOutline(in vec4 color) {\n if (selActive < 0.5) return color;\n vec2 px = 1.0 / selMaskSize;\n vec4 c = texture2D(tSelectionMask, vUv);\n float cSel = floor(c.r + 0.5);\n float cPart = floor(c.g + 0.5);\n float cHov = step(0.5, c.b);\n vec4 col = color;\n float dash = mod(floor((gl_FragCoord.x + gl_FragCoord.y) / 3.0), 2.0);\n\n // dim everything outside the selection\n if (selDim > 0.0 && selHasSel > 0.5 && cSel < 0.5) {\n float l = dot(col.rgb, vec3(0.2126, 0.7152, 0.0722));\n col.rgb = mix(col.rgb, vec3(l) * 0.6 + 0.25, selDim);\n }\n\n // fill tint inside the selection (fainter where hidden)\n if (cSel > 0.5 && selFill > 0.0) {\n float hid = selHidden(vUv, c.a);\n col.rgb = mix(col.rgb, selColor, selFill * (hid > 0.5 ? 0.5 * selXray : 1.0));\n }\n\n // search around the pixel for the nearest different id. Rings of 12 taps.\n float maxR = max(selWidth, selHoverWidth) + 0.5;\n#if SEL_OUTLINE_AA\n // the last ring past the outer edge by 1 / cos(15 deg): an edge inside the outline is hit there also between two taps\n // (the taps of a ring are 30 degrees apart)\n maxR *= 1.0353;\n // for each outline: the ring index of the nearest hits, the sum of their directions, the direction of the first one,\n // and the item of the first hit\n float kSel = 0.0, kHov = 0.0, idSel = 0.0;\n vec2 sumSel = vec2(0.0), sumHov = vec2(0.0), firstSel = vec2(0.0), firstHov = vec2(0.0);\n // a pixel next to this one (the first ring) is outside the selected item / the hover: the inner edge of the outline\n float edgeSel = 0.0, edgeHov = 0.0;\n // the item and the hover of the 4 pixels next to this one (right, up, left, down: the first ring at 0, 90, 180, 270 deg)\n vec4 nbSel = vec4(0.0), nbHov = vec4(0.0);\n#endif\n float dSel = selNoHit, dHov = selNoHit, dPart = selNoHit;\n // the uv and the mask depth of the nearest sample: the x-ray test (a depth read) is done once after the search,\n // not inside the unrolled loop\n vec3 nSel = vec3(0.0), nHov = vec3(0.0);\n for (int ri = 0; ri < 4; ri++) {\n float r = 0.75 + float(ri) * (maxR - 0.75) / 3.0;\n for (int i = 0; i < 12; i++) {\n float a = (float(i) + 0.5 * float(ri)) * 0.5235988;\n vec2 o = vec2(cos(a), sin(a)) * r;\n vec2 uv = vUv + o * px;\n vec4 s = texture2D(tSelectionMask, uv);\n float sSel = floor(s.r + 0.5);\n float sPart = floor(s.g + 0.5);\n float sHov = step(0.5, s.b);\n#if SEL_OUTLINE_AA\n vec2 dir = o / r;\n if (sSel > 0.5 && sSel != cSel && (cSel < 0.5 || sSel > cSel)) {\n if (r < dSel) {\n kSel = float(ri);\n idSel = sSel;\n sumSel = vec2(0.0);\n firstSel = dir;\n }\n if (r <= dSel) sumSel += dir;\n }\n if (sHov > 0.5 && cHov < 0.5) {\n if (r < dHov) {\n kHov = float(ri);\n sumHov = vec2(0.0);\n firstHov = dir;\n }\n if (r <= dHov) sumHov += dir;\n }\n if (ri == 0) {\n if (cSel > 0.5 && sSel != cSel && (sSel < 0.5 || cSel > sSel)) edgeSel = 1.0;\n if (cHov > 0.5 && sHov < 0.5) edgeHov = 1.0;\n if (i == 0) { nbSel.x = sSel; nbHov.x = sHov; }\n if (i == 3) { nbSel.y = sSel; nbHov.y = sHov; }\n if (i == 6) { nbSel.z = sSel; nbHov.z = sHov; }\n if (i == 9) { nbSel.w = sSel; nbHov.w = sHov; }\n }\n#endif\n // outer outline of each selected item (one-sided where two items touch)\n if (sSel > 0.5 && sSel != cSel && (cSel < 0.5 || sSel > cSel) && r < dSel) {\n dSel = r;\n nSel = vec3(uv, s.a);\n }\n if (sHov > 0.5 && cHov < 0.5 && r < dHov) {\n dHov = r;\n nHov = vec3(uv, selHoverDepth(s.b));\n }\n // segmentation lines between parts of the same item\n if (ri == 0 && sSel == cSel && cSel > 0.5 && sPart != cPart && sPart > cPart) dPart = min(dPart, r);\n }\n }\n\n#if SEL_OUTLINE_AA\n // the distance to the mask edge, not the ring radius: between the ring before the hits (no hit) and the ring of the hits\n float ringStep = (maxR - 0.75) / 3.0;\n if (dSel < selNoHit) dSel = selEdgeDistance(sumSel, firstSel, kSel > 0.5 ? dSel - ringStep : 0.0, dSel, selWidth, cSel, cHov, false, px, nSel);\n if (dHov < selNoHit) dHov = selEdgeDistance(sumHov, firstHov, kHov > 0.5 ? dHov - ringStep : 0.0, dHov, selHoverWidth, cSel, cHov, true, px, nHov);\n // the inner edge, at a pixel next to the edge of an item (its own, else the item of the nearest hit) or of the hover\n float eSel = edgeSel > 0.5 ? cSel : kSel < 0.5 && dSel < selNoHit ? idSel : 0.0;\n float inSel = 1.0, inHov = 1.0;\n if (eSel > 0.5 || edgeHov > 0.5 || kHov < 0.5 && dHov < selNoHit) {\n vec4 d0 = texture2D(tSelectionMask, vUv + vec2(1.0, 1.0) * px);\n vec4 d1 = texture2D(tSelectionMask, vUv + vec2(-1.0, 1.0) * px);\n vec4 d2 = texture2D(tSelectionMask, vUv + vec2(-1.0, -1.0) * px);\n vec4 d3 = texture2D(tSelectionMask, vUv + vec2(1.0, -1.0) * px);\n vec4 dSels = floor(vec4(d0.r, d1.r, d2.r, d3.r) + 0.5);\n vec4 dHovs = step(0.5, vec4(d0.b, d1.b, d2.b, d3.b));\n // the part of the 3x3 pixels in the item / the hover. At a straight edge it is (1.5 - s) / 3, with s the distance\n // of the pixel center to the edge (outward, along the nearer axis); along a stair of the mask it changes from one\n // pixel to the next, as the distance to the real edge does\n vec4 one = vec4(1.0);\n float fSel = (step(abs(cSel - eSel), 0.5) + dot(one, step(abs(nbSel - eSel), vec4(0.5))) + dot(one, step(abs(dSels - eSel), vec4(0.5)))) / 9.0;\n float fHov = (cHov + dot(one, nbHov) + dot(one, dHovs)) / 9.0;\n // the outline from half a pixel inside the edge (less for a thinner outline) to the width: the alpha of the pixel is\n // the part of it in that band\n if (eSel > 0.5) inSel = selBand(1.5 - 3.0 * fSel, selWidth);\n inHov = selBand(1.5 - 3.0 * fHov, selHoverWidth);\n }\n#endif\n if (selParts > 0.0 && dPart < selNoHit) {\n float hid = selHidden(vUv, c.a);\n float a = selParts * (hid > 0.5 ? 0.5 * selXray * dash : 1.0);\n col = selOver(col, selColor, a);\n }\n if (dHov < selNoHit) {\n float a = clamp(selHoverWidth + 0.5 - dHov, 0.0, 1.0);\n#if SEL_OUTLINE_AA\n a = min(a, inHov);\n#endif\n if (selHidden(nHov.xy, nHov.z) > 0.5) a *= selXray * dash * 0.8;\n col = selOver(col, selHoverColor, a);\n }\n#if SEL_OUTLINE_AA\n // a hovered pixel has no hover hits: the inner edge alone\n else if (edgeHov > 0.5) {\n float a = inHov;\n if (selHidden(vUv, selHoverDepth(c.b)) > 0.5) a *= selXray * dash * 0.8;\n col = selOver(col, selHoverColor, a);\n }\n // the inner edge of the own item of the pixel\n float inOwn = 0.0;\n if (edgeSel > 0.5) {\n inOwn = inSel;\n if (selHidden(vUv, c.a) > 0.5) inOwn *= selXray * dash;\n }\n#endif\n if (dSel < selNoHit) {\n float a = clamp(selWidth + 0.5 - dSel, 0.0, 1.0);\n#if SEL_OUTLINE_AA\n // the edge of the item of the nearest hit: next to it, and not the own item of this pixel\n if (edgeSel < 0.5) a = min(a, inSel);\n#endif\n if (selHidden(nSel.xy, nSel.z) > 0.5) a *= selXray * dash;\n#if SEL_OUTLINE_AA\n a = max(a, inOwn);\n#endif\n col = selOver(col, selColor, a);\n }\n#if SEL_OUTLINE_AA\n else if (inOwn > 0.0) col = selOver(col, selColor, inOwn);\n#endif\n return col;\n}\n"

    • Renders one more frame, without a restart of the progressive render: the outline is drawn over the final image. The mask is drawn again in that frame. The plugin calls it when the selection, the hover or a setting changes. Call it after a change of the selected objects that renders without a reset of the render (viewer.setDirty() and the setDirty of an object reset it), else the mask of the last change stays until the next reset.

      Returns void

    • Parameters

      • Optionalmeta: SerializationMetaType

      Returns ISerializedConfig