Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of SVGRenderer #11359

Merged
merged 9 commits into from
May 20, 2017
Merged

Improve performance of SVGRenderer #11359

merged 9 commits into from
May 20, 2017

Conversation

linev
Copy link
Contributor

@linev linev commented May 18, 2017

Addresses first part of #11356

Main idea - always use svg:path for rect, line or triangle rendering.
If consequent elements have similar style, just concatenate them.
Plus several more optimizations:

  • use less number of spaces
  • use rgba() instead of separate fill-opacity or stroke-opacity parameters
  • optionally precision of SVG coordinates can be configured

Depending from geometry, can speed up SVG generation by factor 20 and reduce SVG size by factor 4

linev added 8 commits May 18, 2017 10:30
Idea to use svg:path for all SVG primitives.
Later one can concatenate svg:path if they have identical styles
Now rendering of all elements done with svg:path
If several svg:path with same style should be created, just concatenate
there 'd' attributes and make single svg:path node. Will highly reduce
size of SVG if many elements with similar color should be rendered.
When SVG stored into the file, reduces file size by ~5%
Avoid explicit usage of fill-opacity or stroke-opacity
One could provide similar function in THREE.Color object itself
While SVG is text format, 10-digits for each coordinate is overkill. In
many cases on can use precion=1 or even precision=0. For instance,
instead of 
path
d="M95.67875432291896,61.48774395485388L99.22967055503229,-179.38931463976644"

one gets (with precision 1)

path d="M95.7,61.5L99.2,-179.4"

This significantly (factor 2) reduces size of SVG when stored into the
file.

function convert ( c ) {

return _precision < 0 ? c : c.toFixed(_precision);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about setting _precision to null by default and here check for _precisions !== null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0daa689 commit

@@ -220,6 +241,8 @@ THREE.SVGRenderer = function () {

}

addPath ( "!dummy!", ""); // just to flush last svg:path
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about doing a clearPath() function instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I call function flushPath() - it is used also in addPath()

Such method can be used directly at the end of rendering loop

if ( opacity === undefined || opacity === 1 ) return 'rgb(' + arg + ')';

return 'rgba(' + arg + ',' + opacity + ')';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, similar approach can be used THREE.Color.getStyle().
One could add opacity parameter to the function and return rgba() string if opacity defined and not equal to 1

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you done benchmarks on this? Maybe the browser is already doing the same check internally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reduces size of SVG string - when written into the file.
I do not think that it has any significant impact on performance

@mrdoob mrdoob merged commit e7621bf into mrdoob:dev May 20, 2017
@mrdoob
Copy link
Owner

mrdoob commented May 20, 2017

Sweet! Thanks!

@linev linev deleted the fastsvg branch May 22, 2017 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants