Scene as an optional parameter


I’m using Raanan great kickstart repo to convert my project to Typescript.
I’m trying to add:

 new StandardMaterial(`myMat`);

It generates the following error:

TS2554: Expected 2 arguments, but got 1.  standardMaterial.d.ts(461, 31): An argument for 'scene' was not provided.

I can see that the signature of StandardMaterial is:

    constructor(name: string, scene: Scene);

I think this is wrong.
IMO scene should be optional:

  1. it is already the current behaviour
    I have been creating some objects without attaching them directly at the scene and it works fine

  2. leaving scene optional, and leaving the behaviour of adding to the scene at a later point, is very advantageous in terms of modulating the code and trying to be a minimal functional
    I have a function for creating the material, one for the mesh, etc… and I can easily reuse them across components

  3. there is no harm for people that want to attach an object to the scene directly

What do you guys think?

scene being optional is just syntactic sugar

If not added explicitly, is being added by:
this._scene = scene || EngineStore.LastCreatedScene;

If a scene hasn’t been defined previously, the code will fail later at:

if (this._scene.useRightHandedSystem) {

There is a mistake here: the material is still added to a scene even if you don’t pass it to the constructor / pass null! It is added to the last created scene:

        this._scene = scene || EngineStore.LastCreatedScene;

This is how _scene is set in the Material constructor.

If you don’t pass a scene and EngineStore.LastCreatedScene is null it will crash, as soon after this line there is:

if (this._scene.useRightHandedSystem) {

Ok so that’s interesting
So a scene must be created before creating other objects, so that it will be available globally…

However, IMO this line justify making scene optional in the constructor even more:

        this._scene = scene || EngineStore.LastCreatedScene;

BTW what will happen if I create a scene temporally, and then add the object to another scene?

Most probably you will have some problems. I don’t think you can create assets in a scene and use them in another one. Experts will have to tell!

Yup materials attachment to a scene is critical wrt their state mechanism or lights or any other info they would use from the scene.

The fact that is not mandatory is only syntaxic sugar here but might be error prone in a multi scene use case.

Ok so then I’ll withdraw everything I said :laughing: :laughing:
Thanks for the explanations

1 Like