2

Im loading a glTF shirt object. I want to wrap text around the sleeve and for that i have created a mesh on top of the sleeve using blender. Now i have created a canvas texture on that mesh and tried to render text but it does not seem to be working. It gives the following error.

enter image description here

Here's my code.

loadModel(model, callback) {
        this.loader = new GLTFLoader();
        this.loader.setCrossOrigin('anonymous');
        this.loader.load(model, (gltf) => {
            if (typeof callback === 'function') {
                callback(gltf.scene);
            }

            this.scene.add(gltf.scene);
        });
    }

loadModel() {
            this.isLoaded = false;

            this.scene.loadModel('model10/V Shirt text.gltf', (model) => {
                model.name = 'shirt';
                
                // Iterator through the model's children
                model.traverse((child) => {
                    if (child instanceof THREE.Mesh) {
                        // reset original material
                        child.material.map = null;
                        if (child.name = 'Text') {
                            let canvas = document.createElement('canvas');
                            canvas.height = 100;
                            canvas.width = 100;
                            let context = canvas.getContext('2d');
                            context.fillStyle = 'black'
                            context.font = '100pt Helvetica'
                            context.fillText('Hello', 10, 90);
                            const texture = new THREE.CanvasTexture(context);
                            const material = new THREE.MeshBasicMaterial({
                                map: texture
                            })
                            child.material.map = texture;
                            child.material.map.needsUpdate = true;
                        }
                        // create wireframes
                        // this.createWireframe({ mesh: child });

                        // console.log(child)
                        // push to local array
                        this.objects.push(child);
                    }
                });

1 Answer 1

1

const texture = new THREE.CanvasTexture(context);

Canvas textures are created like so:

const texture = new THREE.CanvasTexture(canvas);

So you don't pass the rendering context but the canvas itself.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.