sabresaurus / SabreCSG

Level design tools for Unity
MIT License
1.37k stars 137 forks source link

See-through pixels in triangle seams and lighting artifacts #19

Open Henry00IS opened 7 years ago

Henry00IS commented 7 years ago

I have been encountering this issue every time I create a dark environment. The triangulation seams are all slightly separated (I guess?) allowing very thin dynamic light to shine through as well as the skybox to be seen through thousands of flashing bright pixels.

seethroughpixels

I made a script that causes the player to "breathe" by slightly moving the camera up and down over time, it makes this extremely noticeable. I am actually looking into this problem myself I just figured I should start a discussion here as it may require the author's CSG knowledge to figure this out. ;)

Henry00IS commented 7 years ago

Using the "Export All To OBJ" feature I was able to reproduce the problem in Blender.

seethroughpixelsblender It's here that I made a shocking discovery. The vertex coordinates are spot on and there are no obvious discrepancies.

I did some more research to check whether this is a common problem and found this: https://stackoverflow.com/questions/18523231/opengl-shimmering-pixels-artifact

It turns out we are dealing with T-Junction elimination.

"T-Junctions occur whenever you have a vertex that lies on, but is not part of another edge. Errors occur when there's not enough sub-pixel precision to properly identify the vertice's relationship to the other edge. They're really common on older hardware (i.e. PS1), less common these days."

I made two quads in Blender and placed them close together and they result in the same artifacts. tjunction

Which turns out to be a common BSP/CSG issue that is solvable.

https://www.gamedev.net/forums/topic/230012-eliminating-discontinuities-t-junctions-in-bsp/ https://github.com/evanw/csg.js/issues/13

However this may just be too complex for me to implement alone...

Henry00IS commented 7 years ago

I found an algorithm to fix T-Junctions here: https://github.com/jscad/csg.js/blob/85a8054c8dabaf17866b5de6dab9d5ee65fca4b7/src/CSG.js#L920

RobbyZ commented 6 years ago

I'm also experiencing this issue which is manifesting in two ways on my project:

First - I use Unity's standard edge detection script as a camera effect, and this error causes false edges to flicker:

edgedetectionerror

Second - During bake, light leaks through the edges into areas that should be dark:

lightleak1

lightleak2