Dreik Posted August 6, 2018 Share Posted August 6, 2018 Recently I enountered an issue with texture has alpha performance. Public attribute 'hasAlpha' is 100 times slower than '_hasAlpha'. Does anyone know why is that and if there is a way to easily fix it? I can't release code to production with private attribute setter. I made two playground to show the problem. http://www.babylonjs-playground.com/#9FMJCS - public hasAlpha 80411 ms http://www.babylonjs-playground.com/#R8QY81 - private _hasAlpha 787 ms Quote Link to comment Share on other sites More sharing options...
sable Posted August 6, 2018 Share Posted August 6, 2018 https://github.com/BabylonJS/Babylon.js/blob/4b63e18d3dbd31d400fff1cba68de2981f8ba2ee/src/Materials/Textures/babylon.baseTexture.ts#L16 https://github.com/BabylonJS/Babylon.js/blob/4b63e18d3dbd31d400fff1cba68de2981f8ba2ee/src/babylon.scene.ts#L6001 Setting has alpha seems to mark all materials in the scene as dirty. So in this case it's calling markAllMaterialsAsDirty a thousand times, each time with a larger materials array to loop over. You could get a compromise of behaviour by just setting the private _hasAlpha and then calling markAllMaterialsAsDirty after the loop. http://www.babylonjs-playground.com/#R8QY81#1 Dreik 1 Quote Link to comment Share on other sites More sharing options...
Dreik Posted August 6, 2018 Author Share Posted August 6, 2018 I know, it would be great if I could do it but I can't use _hasAlpha because the build won't pass. It's an error because it's private. Quote Link to comment Share on other sites More sharing options...
sable Posted August 6, 2018 Share Posted August 6, 2018 If you're meaning it won't build (i.e. you're using typescript) you can cast to any to get around that (the only downside being if the variable name or behaviour changes in a future version of babylonjs). Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted August 6, 2018 Share Posted August 6, 2018 Hi, If you don't need the shader recompile, you can also add another prototype that doesn't call markAllMaterialsAsDirty http://www.babylonjs-playground.com/#9FMJCS#1 tests; 3085, 2970, 3156 And even if you do need the recompile, you should be able to just call it after the last one, to only call markAllMaterialsAsDirty once. http://www.babylonjs-playground.com/#9FMJCS#2 tests; 3432, 3462, 3486 Dreik 1 Quote Link to comment Share on other sites More sharing options...
Dreik Posted August 6, 2018 Author Share Posted August 6, 2018 Wow thank you for the help. @aWeirdo I didn't even know that's possible like this. Quote Link to comment Share on other sites More sharing options...
Dreik Posted August 6, 2018 Author Share Posted August 6, 2018 @aWeirdo For some reason typeScript can't see the overwirten property. So I guess I'm off to casting to any. Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted August 6, 2018 Share Posted August 6, 2018 Ahh typeScript.. i think.. that would require you adding it to the baseTexture source.. I don't know if you can add properties from outside the object in typeScript. public set hasAlphaOverwrite(value: boolean) { if (this._hasAlpha === value) { return; } this._hasAlpha = value; } public get hasAlphaOverwrite(): boolean { return this._hasAlpha; } If casting to any also does the trick, then that would probably be better. Have a nice day Quote Link to comment Share on other sites More sharing options...
Guest Posted August 6, 2018 Share Posted August 6, 2018 Or...I can add a boolean that freeze the dirty system for you Stay tuned! Quote Link to comment Share on other sites More sharing options...
Nabroski Posted August 6, 2018 Share Posted August 6, 2018 or use a instance or copy (clone) https://doc.babylonjs.com/how_to/how_to_use_instances https://doc.babylonjs.com/extensions/objectcloner it works at source level way like this: http://www.babylonjs-playground.com/#9FMJCS#3 Quote Link to comment Share on other sites More sharing options...
Guest Posted August 6, 2018 Share Posted August 6, 2018 here we are: scene.blockMaterialDirtyMechanism = true; http://www.babylonjs-playground.com/#9FMJCS#4 Dreik 1 Quote Link to comment Share on other sites More sharing options...
Dreik Posted August 7, 2018 Author Share Posted August 7, 2018 You are probably the most hard working person I have ever met @Deltakosh Quote Link to comment Share on other sites More sharing options...
Guest Posted August 7, 2018 Share Posted August 7, 2018 I want to make sure that our community has all the required tools Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.