Babylon.js: Cannot assign to read only property 'RGBA16F' of object in strict mode
Edit: This is caused by strict mode, see https://github.com/BabylonJS/Babylon.js/issues/2988#issuecomment-340539936
I have the exact same error as https://github.com/BabylonJS/Babylon.js/issues/2642 but the issue is closed and I don’t know why and how it has been resolved by OP
Context:
- Developing on
localhost
- With MacOSX and ArchLinux
- With Chrome and Firefox
- Using
babylonjs
as a CommonJS module with rollup (and babel) - With
babylon.3.0-beta12.max.js
andbabylon.3.0-beta12.js
(with and without npm)
Error:
babylon.3.0-beta12.max.js:7623 Uncaught TypeError: Cannot assign to read only property 'RGBA16F' of object '#<WebGL2RenderingContext>' at new Engine (babylon.3.0-beta12.max.js:7623)
It looks like the following WebGL constants RGBA16F
, RGBA32F
and DEPTH24_STENCIL8
are read only and cannot be set during engine creation.
Strangely HALF_FLOAT_OES
can be set without any problem…
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 16 (16 by maintainers)
Commits related to this issue
- Fixed #2988 Associated with #2982 — committed to BabylonJS/Babylon.js by deltakosh 7 years ago
@deltakosh I still have the issue with the latest release
I’ve seen that you’ve reverted the
if (...)
thing here in this commit 🤔 Why did you revert this? Is it related to strict check null #2982?I figured out exactly why this is happening using a bundler like rollup or webpack, the thing is that the bundler is enabling strict mode by default by adding
'use strict';
which is good for ES6In rollup documentation https://rollupjs.org/#strict
So I still have the problem with those 3 lines:
But also in
new Mesh(...)
at this line, theTools.DeepCopy
function is throwing at this line:The error details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Getter_only
Seems like
Mesh.source
is a getter (fromthis._source
) and then cannot be set ⚠️My workaround is to disable strict mode in my bundler, but I think there is several benefits to use strict mode directly in BabylonJS in development mode. In production mode we should be able to use it or not because there are many resources about performance improvements, see this and this and this.
@deltakosh do you want me to create 2 separate issues? Or to create another issue called Enable strict mode? Do you also think enabling strict mode is a good thing?
@vousk CC
I’m in a middle of an update so I’ll add it no worry 😃 (directly though)
@deltakosh I think you forgot
DEPTH24_STENCIL8
(I made a copy/paste mistake in my issue)There is another problem, in the case where the constants doesn’t equal what we are expecting (e.g. the browser only supports WEBGL 1) then we will still get the error in strict mode.
May I suggest something more explicit like:
It’s verbose but it’s explicit 😉 You can refactor the
trySetReadOnlyProperty
function to make it generic to any target object if needed.If you want me to fill a PR, I’ll do it.