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

Node ES6 / React example #161

Closed
KarimJedda opened this issue Jan 2, 2021 · 8 comments
Closed

Node ES6 / React example #161

KarimJedda opened this issue Jan 2, 2021 · 8 comments
Assignees
Labels
📑 code improvement 🙏 suggestion Suggestion to consider ❓ question Further information is requested
Milestone

Comments

@KarimJedda
Copy link

Hi jscastro76 and happy new year,

Is there a way to add an example using node ES6 to the examples folder?

I am currently using Svelte to build the app I am working on and somehow I can't get the same example from here https://github.com/jscastro76/threebox/blob/master/examples/06-object3d.html to work. I copied all the code into my component like this:

import { Threebox } from 'threebox-plugin';
[...]
    mapRef = new mapboxgl.Map({
        container: 'map',
        style: '../data/osm-liberty.json',
        attributionControl: true,
        hash: true,
        center: [11.4017, 48.7771, ],
        zoom: 14,
        pitch: 60, // pitch in degrees
        bearing: -60, // bearing in degrees
    });
[..]
    window.tb = new Threebox(
        mapRef,
			mapRef.getCanvas().getContext('webgl'),
			{
				defaultLights: true,
			}
        );
    tb = window.tb

mapRef.on('load', function() {
 [...]

     mapRef.addLayer({
            id: 'custom_layer',
            type: 'custom',
            renderingMode: '3d',
            onAdd: function (map, mbxContext) {

                // import soldier from an external glb file, scaling up its size 20x
                // IMPORTANT: .glb is not a standard MIME TYPE, you'll have to add it to your web server config,
                // otherwise you'll receive a 404 error
                var options = {
                    obj: 'models/soldier.glb',
                    type: 'gltf',
                    scale: 1000,
                    units: 'meters',
                    rotation: { x: 90, y: 0, z: 0 }, //default rotation
                    anchor: 'center'
                }

                tb.loadObj(options, function (model) {
                    let soldier = model.setCoords(origin);
                    tb.add(soldier);
                })

            },

            render: function (gl, matrix) {
                tb.update();
            }
        });
}

And when running it I get:

Uncaught (in promise) TypeError: can't access property "loaded", this is undefined

So I must be doing something wrong I just don't know what it is. Of course the 'models/soldier.glb' file is in the right folder and reachable.

Would be great, I am blocked with this since last night :'D

@jscastro76
Copy link
Owner

jscastro76 commented Jan 2, 2021

Only with this I have no idea what is going wrong, if I’m not wrong I have not used any property called loaded. It seems loaded is a Mapbox property.

Do you have a GitHub repo to check it out? Are you able to debug it in runtime to know where it breaks? Is your style osm-liberty.json loading properly? Does the map load without the custom layer?

@jscastro76 jscastro76 added 🙏 suggestion Suggestion to consider ❓ question Further information is requested labels Jan 2, 2021
@KarimJedda
Copy link
Author

It was referring to this piece of code here:

https://github.com/jscastro76/threebox/blob/master/src/objects/loadObj.js#L22

When replacing "this" with "self" it solved that error but another issue popped up.

But I managed to solve the problem. Instead of
import { Threebox } from 'threebox-plugin';
I had to do
import '../node_modules/threebox-plugin/dist/threebox' and it worked.

I do not understand why since they should be exactly the same. But again, I am by no measure an expert at Svelte or npm so maybe I am doing it wrong.
In any case, here is a repository with the working example in Svelte https://github.com/KarimJedda/threebox-mapboxgl-svelte-example, is how I use it correct?

Thanks a lot!

@KarimJedda
Copy link
Author

And to answer your questions:

Is your style osm-liberty.json loading properly? Yes, it is actually the same I am using with the vanilla-js implementation.
Does the map load without the custom layer? Yes the map loads without the custom layer.

@jscastro76
Copy link
Owner

It was referring to this piece of code here:

https://github.com/jscastro76/threebox/blob/master/src/objects/loadObj.js#L22

When replacing "this" with "self" it solved that error but another issue popped up.

That's old code from the original version, not used anymore, indeed the const modelComplete is not anymore referenced anywhere. It would be better to remove it so you'll be able to reference the module with your original syntax.
Thanks for the clue.

@jscastro76
Copy link
Owner

TODO: remove this.loaded and const modelComplete code
https://github.com/jscastro76/threebox/blob/master/src/objects/loadObj.js#L22

@jscastro76 jscastro76 reopened this Jan 3, 2021
@KarimJedda
Copy link
Author

You are welcome, I am glad to be of help.

@jscastro76 jscastro76 self-assigned this Jan 5, 2021
@jscastro76 jscastro76 added this to the v.2.1.6. milestone Jan 5, 2021
jscastro76 added a commit that referenced this issue Jan 5, 2021
Minor version by [@jscastro76](https://github.com/jscastro76), some enhancements and bugs.

#### ✨ Enhancements

- #111 Show the dimensions of a model
- #151 Remove auxiliary test method on `CameraSync` used to debug #145
- #156 Create an Orthographic view mode
- #159 Create an example for FOV and Orthographic.
  - Added to [09-raycaster.html](https://github.com/jscastro76/threebox/blob/master/examples/09-raycaster.html) as it impacts in raycast and shows fill-extrusions and 3D models together
- #161 Remove obsolete code that is avoiding to be used from React

#### 🪲 Bug fixes
- #152 `obj.raycasted` is ignored when an object is hidden and again visible.
- #157 Bug draggging after removing an object.
- #160 Bug using `utils.equal`

#### 📝 Documentation
- #158 MERCATOR_A constant unused, added an code comment to explain why it was deprecated (nor removed)
- Updated [documentation](/docs/Threebox.md) (`tb.orthographic`, `tb.fov`)
- Updated [README.md](/).
- Updated [Examples](/examples) documentation.
@jscastro76
Copy link
Owner

@KarimJedda, changed and available to download so you don't have to modify your code. Please check if it works

@KarimJedda
Copy link
Author

I will check later tonight and if it works I'll send you a PR for a Svelte example (should be the same for React/Vue.js) to add in the examples folder :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📑 code improvement 🙏 suggestion Suggestion to consider ❓ question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants