Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

OBJ files uploader #66

@JWDobken

Description

@JWDobken

I want to build a file uploader (obj, mtl, etc.) but access files directly in the browser using the FileReader API instead of sending it to a server and retrieve it from there again with the <Asset src='..'> component.

Regarding functionally this is probably very similar to this project: https://3dviewer.net/#

Now... this seems to be a good starting point. I have a very simple MWE that writes the content of an uploaded obj file to a data variable objtext but I have no idea how to load this into the Scene.

<template>
  <div>
  <file-reader @load="objtext = $event"></file-reader>
    <Scene v-model="productScene">
      <Property name="clearColor"
                color="#fff" />
      <Camera v-model="camera"
              :radius="2500"
              type="arcRotate" />
    </Scene>
  </div>
</template>


<script>
import FileReader from './FileReader.vue';

export default {
  name: 'Upload',
  components: {
    FileReader
  },
  data () {
    return {
      productScene: null,
      camera: null,
      objtext: ''
    }
  },
  watch: {
    objtext () {
      const scene = this.productScene
      // Add objtext to scene
      this.BABYLON.SceneLoader.Append('', 'data:' + this.objtext, scene, function () {
        scene.createDefaultCamera(true, true, true)
      })
  }
}
</script>

My filereader looks like:

<template>
  <div>
    <label class="text-reader">
      <input type="file"
             @change="loadTextFromFile">
    </label>
  </div>
</template>

<script>
export default {
  methods: {
    loadTextFromFile (ev) {
      const file = ev.target.files[0] // now it only holds one file
      const reader = new FileReader()

      reader.onload = e => this.$emit('load', e.target.result)
      reader.readAsText(file)
    }
  }
}
</script>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions