# Manually created octrees no longer working in BJS 4.0

**URL:** <https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412>\
**Category:** Questions\
**Created:** [September 2, 2019, 5:06am UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412 "2019-09-02T05:06:59Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![fenomas](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/fenomas/32/2309_2.png) [@fenomas](https://forum.babylonjs.com/u/fenomas)\
**Post date:** [September 2, 2019, 5:06am UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/1 "2019-09-02T05:06:59Z")

</div>

I have a scene where I manually create and manage the selection octree, but since moving to BJS 4.0 the octree seems to be ignored - nothing gets culled, regardless of how I construct the octree or octreeBlocks.

My code basically looks like this:

```auto
// at init
scene._selectionOctree = new Octree(() => {})
scene._selectionOctree.blocks = []

// ... later when creating a chunk of world
var min = new Vector3( ... )
var max = new Vector3( ... )
var block = new OctreeBlock(min, max, undefined, undefined, undefined, () => {})
scene._selectionOctree.blocks.push(block)

```

From source diving it looks like maybe I need to add some kind of component to `scene` to tell BJS that octrees are being used? But I can’t find any sample code doing this, or docs on what changed.

How can this be done in 4.0?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 2, 2019, 6:59am UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/2 "2019-09-02T06:59:54Z")

</div>

What package are you using, the es6 version with @babylonjs/core ? Or the umd/cdn version ?

---

<div class="post-metadata">

**Author:** ![fenomas](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/fenomas/32/2309_2.png) [@fenomas](https://forum.babylonjs.com/u/fenomas)\
**Post date:** [September 2, 2019, 7:18am UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/3 "2019-09-02T07:18:30Z")

</div>

I’m using es6. I’m guessing the “\_components” array on scene has something to do with that?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 2, 2019, 8:43am UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/4 "2019-09-02T08:43:06Z")

</div>

According to the es6 doc, you need to import “Culling/Octrees/octreeSceneComponent”; for side effects to use octree with es6.

---

<div class="post-metadata">

**Author:** ![fenomas](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/fenomas/32/2309_2.png) [@fenomas](https://forum.babylonjs.com/u/fenomas)\
**Post date:** [September 2, 2019, 9:09am UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/5 "2019-09-02T09:09:34Z")

</div>

Is anything else needed? Adding just the import has no effect I can see, the selection octree still doesn’t affect the scene.

I’d repro this in the playground but since it seems to depend on es6 imports I’m not sure how that would work.

The imports I have currently look like:

```auto
import { Scene } from '@babylonjs/core/scene'
import { Octree } from '@babylonjs/core/Culling/Octrees/octree'
import { OctreeBlock } from '@babylonjs/core/Culling/Octrees/octreeBlock'
import { Engine } from '@babylonjs/core/Engines/engine'
import { Vector3, Color3 } from '@babylonjs/core/Maths/math'
import { Mesh } from '@babylonjs/core/Meshes/mesh'
import '@babylonjs/core/Culling/Octrees/octreeSceneComponent'

```

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 2, 2019, 9:27am UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/6 "2019-09-02T09:27:53Z")

</div>

So you have a simple github project for the repro ???

---

<div class="post-metadata">

**Author:** ![fenomas](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/fenomas/32/2309_2.png) [@fenomas](https://forum.babylonjs.com/u/fenomas)\
**Post date:** [September 2, 2019, 10:46am UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/7 "2019-09-02T10:46:21Z")

</div>

A repo wouldn’t help here. ES6 Babylon apparently requires some new extra step to use a selection octree; an empty repo wouldn’t help figure out what it is.

Guess I’m on my own, I’ll go sourcediving…

---

<div class="post-metadata">

**Author:** ![fenomas](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/fenomas/32/2309_2.png) [@fenomas](https://forum.babylonjs.com/u/fenomas)\
**Post date:** [September 2, 2019, 10:58am UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/8 "2019-09-02T10:58:43Z")

</div>

Hmm, it looks like the extra step is:

```auto
scene._addComponent(new OctreeSceneComponent(scene))

```

I guess `_addComponent` is some new (undocumented) mechanism to work with ES6 imports?

---

<div class="post-metadata">

**Author:** ![MarianG](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mariang/32/138_2.png) [@MarianG](https://forum.babylonjs.com/u/MarianG)\
**Post date:** [September 2, 2019, 11:50am UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/9 "2019-09-02T11:50:49Z")

</div>

Hi @fenomas. Unfortuantelly I cann’t help you to solve the issue, but I can give you a sugestion where to isolate and reproduce.

> **[CodeSandbox: Online Code Editor Tailored for Web Application Development](https://codesandbox.io/)**
>
> CodeSandbox is an online code editor with a focus on creating and sharing web application projects

  
I did few times and it is very usefull - [BabylonJS ES6 - CodeSandbox](https://codesandbox.io/s/babylonjs-es6-uxls4)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 2, 2019, 1:41pm UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/10 "2019-09-02T13:41:41Z")

</div>

Oh I see, you do not rely at all on the createOrUpdateMecanism in your case. So the automatic reference through createOrUpdateSubmeshesOctree or createOrUpdateSelectionOctree is not called.

Actually the code called in both those functions is:

```auto
    let component = scene._getComponent(SceneComponentConstants.NAME_OCTREE);
    if (!component) {
        component = new OctreeSceneComponent(scene);
        scene._addComponent(component);
    } 

```

Do you want me to wrap it in a public function for you ?

---

<div class="post-metadata">

**Author:** ![fenomas](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/fenomas/32/2309_2.png) [@fenomas](https://forum.babylonjs.com/u/fenomas)\
**Post date:** [September 2, 2019, 2:04pm UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/11 "2019-09-02T14:04:47Z")

</div>

No idea - I just want to use a selection octree where I manually create and position the blocks. Is that currently possible to do with public methods, or is it not intended to be?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 2, 2019, 2:10pm UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/12 "2019-09-02T14:10:40Z")

</div>

You can but as you did, you need to reference to OctreeSceneComponent in the scene.

If you use the built in system, this step is done for you.I was wondering if you d like that I introduce a new function like:

OctreeSceneComponent.RegisterForScene(scene) so you do not have to worry about changes on the \_ functions ???

---

<div class="post-metadata">

**Author:** ![fenomas](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/fenomas/32/2309_2.png) [@fenomas](https://forum.babylonjs.com/u/fenomas)\
**Post date:** [September 2, 2019, 3:11pm UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/13 "2019-09-02T15:11:27Z")

</div>

I understand what you were suggesting, but it looks to me like 4.0 has introduced some kind of “component” mechanic, that seems to touch lots of different features. Do other such features already have some way of getting initialized?

`FooComponent.RegisterForScene` would be fine here, but it would be best if there’s a consistent way of dealing with all “componentized” features.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 2, 2019, 3:16pm UTC](https://forum.babylonjs.com/t/manually-created-octrees-no-longer-working-in-bjs-4-0/5412/14 "2019-09-02T15:16:41Z")

</div>

They are all automatically initialized when you use a feature from its embedded method. Unfortunately using Octree manually is a bit edge case as it requires the component but can not initializes automatically as you are doing all the rest manually. It is probably only an issue with the octree system.
