Skip to content

Instantly share code, notes, and snippets.

@heckj
Created April 20, 2019 22:28
Show Gist options
  • Save heckj/488c753e017cc53fa792e873b296640b to your computer and use it in GitHub Desktop.
Save heckj/488c753e017cc53fa792e873b296640b to your computer and use it in GitHub Desktop.
Wireframe Shader For An SCNBox Geometry
/*
Copyright © 2018 Apple Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, T
ORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//------------------------
//MARK: - Transparent Body
//------------------------
float3 modelPosition = scn_node.modelTransform[3].xyz;
float3 viewPosition = scn_frame.inverseViewTransform[3].xyz;
float distance = length(modelPosition - viewPosition);
float3 bBoxMin = scn_node.boundingBox[0];
float3 bBoxMax = scn_node.boundingBox[1];
float3 size = bBoxMax - bBoxMin;
float bBoxDiag = length(size);
//-------------------------------
//MARK: - Render Only A Wireframe
//-------------------------------
float lineThickness = 0.02;
float u = _surface.diffuseTexcoord.x;
float v = _surface.diffuseTexcoord.y;
//-----------------------------------------
//MARK: - Compute Scaling Of Line Thickness
//-----------------------------------------
float2 scale;
if (abs((scn_node.inverseModelViewTransform * float4(_surface.normal, 0.0)).x) > 0.5) {
scale = size.zy;
} else if (abs((scn_node.inverseModelViewTransform * float4(_surface.normal, 0.0)).y) > 0.5) {
scale = size.xz;
} else {
scale = size.xy;
}
//--------------------------------------------------
//MARK: - Compute Threshold For Discarding Rendering
//--------------------------------------------------
float2 thresh = float2(lineThickness) / scale;
if (u > thresh[0] && u < (1.0 - thresh[0]) && v > thresh[1] && v < (1.0 - thresh[1])) {
discard_fragment();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment