Hi, I am trying to implement something like this: https://playground.babylonjs.com/#7F6S08#2
The above code renders 3D model after converting into binary stream.
I am trying to do the same in react babylon using and . However, I am receiving the following error: webRequest.ts:119 Not allowed to load local resource: blob:null/a7cf7204-89cd-4c37-8933-487cac9deb4e
Below is my code:
renderModel = (rootURL) => {
console.log(“renderModel”);
this.setState({
…this.state,
url: rootURL,
id: “after”
})
}
runXHR = (loadModel) => {
var url = "/modelURL"
var binaryString = "";
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "blob";
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (((xhr.status === 200) || (xhr.status == 0)) && (xhr.response)) {
var reader = new FileReader();
reader.onloadend = function () {
binaryString = reader.result;
binaryString = binaryString.replace("application/octet-stream;", "");
loadModel(binaryString);
}
reader.readAsDataURL(xhr.response);
}
}
}
xhr.send(null);
}
loadModel = (base64_model_content) => {
var raw_content = this._base64ToArrayBuffer(base64_model_content.replace("data:base64,", ""));
var blob = new Blob([raw_content]);
var url = URL.createObjectURL(blob);
url=url.replace("http://localhost:3000","null");
console.log(url);
this.renderModel(url);
}
_base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
render() {
return (
<div className="mainScene">
<button onClick={() => this.runXHR(this.loadModel)}>Click</button>
<Engine antialias adaptToDeviceRatio canvasId="sample-canvas">
<Scene canvasId="scene_1">
<ArcRotateCamera name="arc" target={new Vector3(0, 1, 0)} minZ={0.001}
alpha={-Math.PI / 2} beta={(0.5 + (Math.PI / 4))} radius={5} lowerRadiusLimit={5} upperRadiusLimit={10} />
<HemisphericLight name='hemi' direction={new Vector3(0, -1, 0)} intensity={0.8} />
<Model
rotation={new Vector3(0, -15, 0)} key={this.state.id} position={new Vector3(0, 0, 0)}
rootUrl={this.state.url} sceneFilename=""
scaling={new Vector3(1, 1, 1)} />
<EnvironmentHelper options={{ enableGroundShadow: true, groundYBias: 1 }} mainColor={Color3.FromHexString("#74b9ff")} />
</Scene>
</Engine>
</div>
)
}
}