binki / babylonjs-blender-loader

Webpack loader for babylonjs-blender
MIT License
3 stars 1 forks source link

Information about using 'data:' #6

Open ghost opened 6 years ago

ghost commented 6 years ago

As I understand it you're having trouble with using data: and it not reading the JSON data correctly.

I'm writing my own loader because I couldn't get yours to work, and as part of that I ran into problems with using data: as well.

The following code resulted in a working data: object:

var testUnit = fs.readFileSync(path.join(__dirname, './test_unit.babylon'), 'utf-8');
callback(null, "export default {model:'data:" + JSON.stringify(JSON.parse(testUnit)) + "'}");

// Or simplified a tiny bit for just the object
var testUnit = 'data:' + JSON.stringify(JSON.parse(fs.readFileSync(path.join(__dirname, './test_unit.babylon'), 'utf-8')));

So basically I'm first using readFileSync to get the contents of the .babylon file, then I parse it as JSON, then I stringify it. The problem seemed to be that if I just stringified it directly it added unnecessary double quotes before and after, so when it should've been data:{"hello":"world"} it instead became data:"{"hello":"world"}", which gave me understandable unterminated string constant errors.

Looking at the SceneLoader source code it actually just looks at the first 5 characters to determine that it says data: and then strips them from the string, then later on I guess it parses the string after data: and if there are double quotes before and after the parsing fails.

If this wasn't your problem just ignore this, but I figured it might solve your issue.

binki commented 6 years ago

babylonjs’s loader does not read data: correctly. As you said, it just checks the first 5 characters for matching data: and then treats the remainder as verbatim JSON. I.e., it does not support ;base64 or MIME types and does not decodeURIComponent(). It therefore violates the data: standard and, in fact, it is impossible to feed it standard data:.

Because of all this, there is no need for you to use JSON.stringify(JSON.parse()). That should basically be an identity function ;-). Instead just do data:${testUnit}.

The purpose of my loader is to enable you to commit and use .blend files directly. One shouldn’t have to manually open Blender and export a .babylon file. You should commit the source file which the human actually edits—the .blend—and leave the rest to the build system. My loader returns an object wrapper offering a few mechanisms for actually loading the imported .blend into a scene. If you need any help using it, please make a minimal demo repository which fails with this loader and I can poke at it and make it work with this loader. Unfortunately, this loader (unlike some loaders which manage all prerequisites magically in a cross platform way somehow via npm install) requires you to have blender in PATH and to either install the necessary export plugin manually or to use the provided babylonjs-blender-install command to install a known working BabylonJS working export plugin automatically. If you encounter issues at this step, please let me know!

Additionally, I do not think I have tested this loader with webpack-1 yet. Maybe I am interacting with webpack incorrectly.

Sorry for the delay in response. Please reply and I will help you if I can.