xeokit / xeokit-sdk

Open source JavaScript SDK for viewing high-detail, full-precision 3D BIM and AEC models in the Web browser.
https://xeokit.io
Other
736 stars 291 forks source link

[OPTIMIZATION SUGGESTION]: Do heavy calculations on section planes in Vertex Shader instead of Fragment Shader #268

Open tmarti opened 4 years ago

tmarti commented 4 years ago

The current way of doing section plane clips

This idea is related to a possible optimization about section planes' clipping in GLSL shaders.

Right now, on all xyzDrawShaderSource.js files, this scheme is used on every shader source (take for example https://github.com/xeokit/xeokit-sdk/blob/master/src/viewer/scene/PerformanceModel/lib/batching/draw/batchingDrawShaderSource.js):

Step 1

The Vertex Shader (VS) creates 2 varying variables that will be used in the Fragment Shader (FS):

https://github.com/xeokit/xeokit-sdk/blob/7d2d9b37cb9a2096dc09d469e15c42176b0340a3/src/viewer/scene/PerformanceModel/lib/batching/draw/batchingDrawShaderSource.js#L68-L71

Step 2

The section planes uniform variables are loaded from the DrawRenderer into the FS:

https://github.com/xeokit/xeokit-sdk/blob/7d2d9b37cb9a2096dc09d469e15c42176b0340a3/src/viewer/scene/PerformanceModel/lib/batching/draw/batchingDrawShaderSource.js#L174-L182

Step 3

With the varying variables provided by the VS and the section planes uniform variables, the FS calculates, for each fragment, if the fragment world position is effectively clipped by the section plane and if that's the case, discard the fragment:

https://github.com/xeokit/xeokit-sdk/blob/7d2d9b37cb9a2096dc09d469e15c42176b0340a3/src/viewer/scene/PerformanceModel/lib/batching/draw/batchingDrawShaderSource.js#L185-L196

What this way of doing involves

This involves, for each fragment:

The new idea for doing section plane clips

Currently...

... and the new idea is that...

The result

The result with this idea is that the distances from section planes would be calculated in the VS, and interpolated on the FS.

This way we only calculate one distance (per section plane) per each vertex, instead of one distance (per section plane) per fragment.

@xeolabs, what do you think about it? 😀

xeolabs commented 4 years ago

@tmarti great idea - will definitely implement this soon.

tmarti commented 4 years ago

Super! I think the issue description provides enough information for a complete implementation 😃.

Right now just started vacation, but ping me if there is any problem with the implementation 👍