Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three.js insert image

I am trying to attach a simple image on a PlaneGeometry Mesh but it doesn't seem to work.

window.onload = function(){


        var renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

        // camera
        var camera = new THREE.PerspectiveCamera(95, window.innerWidth / window.innerHeight, 1, 1000);
        camera.position.y = -250;
        camera.position.z = 400;
        camera.rotation.x = 45 * (Math.PI / 180);

        // scene
        var scene = new THREE.Scene();

        var img = new THREE.MeshLambertMaterial({
            map:THREE.ImageUtils.loadTexture('img/front.jpg')
        });
        // plane
        var plane = new THREE.Mesh(new THREE.PlaneGeometry(200, 200),img);
        plane.overdraw = true;
        scene.add(plane);
         // add subtle ambient lighting
        var ambientLight = new THREE.AmbientLight(0x555555);
        scene.add(ambientLight);

        // add directional light source
        var directionalLight = new THREE.DirectionalLight(0xffffff);
        directionalLight.position.set(1, 1, 1).normalize();
        scene.add(directionalLight);

        // create wrapper object that contains three.js objects
        var three = {
            renderer: renderer,
            camera: camera,
            scene: scene,
            plane: plane
        };
        renderer.render(scene,camera);
    };

This is my full javascript file with a canvas of 580x300

I only see a black square whenever I run it. any ideas? Thanks!

here is my reference for those who need it:

http://www.html5canvastutorials.com/webgl/html5-canvas-webgl-texture-with-three-js/

like image 867
AJ Naidas Avatar asked Mar 15 '12 10:03

AJ Naidas


1 Answers

After downloading latest tag from github and playing with examples it became clear that the CORS is the one to blame. If I open the Chrome Developper Tools and look at the console there are many error messages:

Cross-origin image load denied by Cross-Origin Resource Sharing policy.

If you are using Chrome, start it with flag -allow-file-access-from-files

Tested with Chrome 17.0.963.79.

window.onload = function(){

    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    // camera 
    var camera = new THREE.PerspectiveCamera(95, window.innerWidth / window.innerHeight, 1, 1000); 
    camera.position.y = -250; 
    camera.position.z = 400; 
    camera.rotation.x = 45 * (Math.PI / 180); 

    // scene 
    var scene = new THREE.Scene();
    scene.add(camera); //ADDED

    var img = new THREE.MeshBasicMaterial({ //CHANGED to MeshBasicMaterial
        map:THREE.ImageUtils.loadTexture('img/front.jpg')
    });
    img.map.needsUpdate = true; //ADDED

    // plane
    var plane = new THREE.Mesh(new THREE.PlaneGeometry(200, 200),img);
    plane.overdraw = true;
    scene.add(plane);

     // add subtle ambient lighting
    var ambientLight = new THREE.AmbientLight(0x555555);
    scene.add(ambientLight);

    // add directional light source
    var directionalLight = new THREE.DirectionalLight(0xffffff);
    directionalLight.position.set(1, 1, 1).normalize();
    scene.add(directionalLight);

    // create wrapper object that contains three.js objects
    var three = {
        renderer: renderer,
        camera: camera,
        scene: scene,
        plane: plane
    };
    renderer.render(scene,camera);
};
like image 75
Martin Avatar answered Oct 15 '22 16:10

Martin