4

I am trying to create a 3D model in React-Native using Three.js, for that I have to use an image for texture but TextureLoader() function using 'document' and 'canvas' object creation logic, which can't be used in react-native.

so, how can I use an image as texture in react-native using three.js ?

7
  • I have exactly the same question! Any solutions? Commented Jul 3, 2018 at 12:52
  • @robertjuh, no luck... still searching for a solution :( Commented Jul 9, 2018 at 9:35
  • I'm trying to find a way to override document.createElementNS with a custom function so that it will create an Image another way. As far as i know the Textureloader uses the ImageLoader in which the document.createElementNS function is used. I will create an answer if i manage to make this work Commented Jul 9, 2018 at 9:51
  • @robertjuh, that's great... waiting for the answer :) Commented Jul 17, 2018 at 13:26
  • Yea well i managed to override the document.createElementNS function but nothing good came out of it really. The IMG file react native creates appears to be different than the HTML IMG object. Going to speak to another developer about this soon. Commented Jul 18, 2018 at 9:03

2 Answers 2

1

Short summary: the Textureloader didnt work because it required an imageloader which required the inaccesible DOM.

Apparantly there's a THREE.Texture function which i used like this:

let texture = new THREE.Texture(
     url //URL = a base 64 JPEG string in this case     
     );

var img = new Image(128, 128);
img.src = url;
texture.normal = img;

As you can see i used an Image constructor, this is from React Native and is imported like this:

import { Image } from 'react-native';

In the react native documentation it will explain how it can be used, it supports base64 encoded JPEG.

and finally map the texture to the material:

material.map = texture;

Note: i havent tried to show the end result in my webglview yet, but in the element inspector it seems to show proper THREEjs objects.

Sign up to request clarification or add additional context in comments.

Comments

0

(Note: this is not the answer)

EDIT: made the code a bit shorter and use DOMParser, still doesnt work because: "image.addEventListener is not a function" Which implies the image object which is created in this method doesnt seem to be able to be used in THREEjs,..

var DOMParser = require('react-native-html-parser').DOMParser;
var myDomParser = new DOMParser();
window.document = {};

window.document.createElementNS = (x,y) => {
    if (y === "img") {
        let doc = myDomParser.parseFromString("<html><head></head><body><img class='image' src=''></img></body></html>");
        return doc.getElementsByAttribute("class", "image")[0];
    }
}

Edit: be careful with this, because it also overrides the other createElemensNS variants in which they create canvas or other things.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.