Search code examples
iosscenekit

SCNTechnique renders strangely when SCNView is small


I have a My3DScene, which adds SCNTechnique to the camera. The technique simply adds a border to a 3d model.

Then the following code works fine:

    // Here: Notice the 400x300 size
    let scnView = SCNView(frame: CGRectMake(0, 0, 400, 300))
    view.addSubview(scnView)
    scnView.present(My3DScene(), with: .fade(withDuration: 0.1), incomingPointOfView: nil)

However, if I change the view size to be smaller, then it renders strangely:

    // Here: Note the 40x40 size here
    let v2 = SCNView(frame: CGRectMake(0, 400, 40, 40))
    view.addSubview(v2)
    v2.present(My3DScene(), with: .fade(withDuration: 0.1), incomingPointOfView: nil)

In the screenshot below, the top is a SCNView that renders correctly. The bottom is a smaller SCNView that renders strangely.

enter image description here

I have attached my project:

https://drive.google.com/file/d/1BITsN7gucSU9fsbqjUBPxPiU6h7JN1uB/view?usp=sharing

The following is another screenshot with 4 SCNViews, from top to bottom, their sizes are:

400x300 (renders correctly) 40x40 100x100 150x150

enter image description here


Solution

  • The issue occurs because of a fixed MASK size definition in your glowing_border_technique.plist

    Since your MASK texture has a fixed size of 512x512, when applied to a smaller SCNView, the resolution mismatch might cause sampling errors or misalignment. The effect might look fine on larger views but appear distorted or incomplete on smaller ones.

    Change this:

    <key>size</key>
    <string>512x512</string>
    

    To:

    <key>size</key>
    <string>auto</string>
    

    glowing_border_technique.plist

    This is the out coming result and should fix your issue: Fixed SCNTechnique

    OPTIONAL: In addition you might want to check (or make somehow dynamic) the constant float bufferSize = 512.0; in your glowing_border_technique.metal file.