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

Custom THREE.ObjectLoader for Extended Objects #30

Closed
HexaField opened this issue Jun 27, 2020 · 6 comments
Closed

Custom THREE.ObjectLoader for Extended Objects #30

HexaField opened this issue Jun 27, 2020 · 6 comments
Labels
enhancement New feature or request

Comments

@HexaField
Copy link
Contributor

HexaField commented Jun 27, 2020

It would be great to extend the Object3D .toJSON() and JSON parser for threejs (THREE.ObjectLoader) to work with the extended objects. If there is a way to save and load to json already I apologise, I did spend some time looking. Thanks.

@yandeu yandeu added the enhancement New feature or request label Jun 27, 2020
@yandeu
Copy link
Member

yandeu commented Jun 27, 2020

I will implement the ObjectLoader.

But you should already be able to make it work manually. The ExtendedObject3D already extends all Object3D functionalities.

Maybe you want to share your code?

Edit:
Oh, wait. The ObjectLoader is actually for JSON Object Scenes. I did not know that. I will see what I can do.

@yandeu
Copy link
Member

yandeu commented Jun 27, 2020

I have implemented the ObjectLoader (e32baa8).

This is how you will be able to do it:

async preload() {
  this.load.preload('plane', '/assets/plane.json')
}

async create() {
  this.load.object('plane').then((obj: any) => {
    // in most cases ExtendedObject3D is not needed
    // to be able to add physics, just add a name
    obj.name = `object-${obj.id}`
    this.scene.add(obj)
  })
}

// or without preloading
this.load.object('/assets/plane.json').then((obj: any) => { ... }

// async/await is of course also supported
const obj = await this.load.object('/assets/plane.json')

If you do already have a THREE.ObjectLoader in your project and want to add physics to it, just set the name as in the example above.

Hope this helps :)

@HexaField
Copy link
Contributor Author

This won't save and load the information specific to Extended objects though (physics body velocity for example), that's more what I'm looking for.

@yandeu
Copy link
Member

yandeu commented Jun 27, 2020

Well it will not be 100% an ExtendedObject3D, but most of the code of the ExtendedObject3D is only related to animations anyways. When you use this.physics.add.existing(obj), the object will receive a physics body. Of course if you use TypeScript, you won't get the fancy IntelliSense. Fortunately, you can add the ExtendedObject3D type to any object you want.

Try this:
(everything, except animations, should work)

// add ExtendedObject3D as the type
this.load.object('plane').then((obj: ExtendedObject3D) => {

  // add a name (this is required for physics)
  obj.name = `object-${obj.id}`

  this.scene.add(obj)

  // add a physics body
  this.physics.add.existing(obj, { shape: 'convex' })

  // use the physics body with IntelliSense
  obj.body.applyForceY(5)
})

@yandeu
Copy link
Member

yandeu commented Jun 28, 2020

Actually, if you look at the examples, I always use the following approach:

// preload the object
this.load.preload('plane', '/assets/plane.json')

// parse the object
this.load.object('plane').then(obj => {

  // create the extendedObject3D
  const plane = new ExtendedObject3D()

  // add the object as a child
  plane.add(obj)

  // add plane to the scene
  this.scene.add(plane)

  // add a physics body
  this.physics.add.existing(plane, { shape: 'convex' })

  // use the physics body with IntelliSense
  plane.body.applyForceY(5)
})

@HexaField
Copy link
Contributor Author

I've solved this by storing the physics body data in userData for each object. It would be nice to have a native loader for saving/loading aspects of a physics body such as shape, velocity, collision flags etc but I can manage doing this manually for now. Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants