11

Note: I want to avoid modifying the model in the javascript code and do all the model design inside Blender.

Note #2: While this question is long, it is actually a basic problem (title says it all). The below is "walk-through" to the problem.

I am trying export Blender models to threejs.org as DAE model but have problem with models with texture (I have tried JSON and OBJ+MTL format too):

To make things simpler, here are the steps I perform (and fail) to add texture to simple "Startup file" which contains a cube, camera, and light:

  1. Select the cube
  2. In the Materials panel, make sure the default Material for the cube is selected.
  3. In the Texture panel, make sure "Tex" (the default texture for the material) is selected.
  4. For this texture, set Type to "Image or Movie"
  5. In the Image section of panel, open a grass1.jpg (a 512x512 image) as the texture.
  6. In the Mapping section, change the Coordinates to UV.
  7. Export the model as Collada model, checking "Include UV Textures", "Include Material Textures", and "Copy" checkboxes.

You can download blend, dae, and texture file mentioned in these steps.

Then I use the following code to load the DAE model, but I get this error and the cube is not shown:

256 [.WebGLRenderingContext]GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 2 WebGL: too many errors, no more errors will be reported to the console for this context.

enter image description here

<script src="js/three.min.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/ColladaLoader.js"></script>
<script>
    var scene, camera, renderer;
    init();
    animate();

    function init() {
        scene = new THREE.Scene();
        var WIDTH = window.innerWidth,
            HEIGHT = window.innerHeight;

        renderer = new THREE.WebGLRenderer({antialias:true});
        renderer.setSize(WIDTH, HEIGHT);
        document.body.appendChild(renderer.domElement);

        camera = new THREE.PerspectiveCamera(45, WIDTH / HEIGHT, 0.1, 10000);
        camera.position.set(10,10,10);
        scene.add(camera);

        window.addEventListener('resize', function() {
        var WIDTH = window.innerWidth,
            HEIGHT = window.innerHeight;
            renderer.setSize(WIDTH, HEIGHT);
            camera.aspect = WIDTH / HEIGHT;
            camera.updateProjectionMatrix();
        });  

        var loader = new THREE.ColladaLoader();

        loader.load( 'models/untitled.dae', function(geometry) {
            dae = geometry.scene;
            scene.add(dae);
            var gridXZ = new THREE.GridHelper(100, 10);
            gridXZ.setColors( new THREE.Color(0x8f8f8f), new THREE.Color(0x8f8f8f) );
            gridXZ.position.set(0,0,0);
            scene.add(gridXZ);
        });

        controls = new THREE.OrbitControls(camera, renderer.domElement);
    }

    function animate() {
      requestAnimationFrame(animate);
      renderer.render(scene, camera);
      controls.update();
    }
</script>

And here is the screenshot of Blender after mentioned 7 steps:

enter image description here

Update: Exporting as js file using JSON exporter for Blender doesn't work either and resulted the very same error.

Update 2: Same error after exporting to OBJ+MTL and loading them with OBJMTLLoader.

Isaac
  • 2,332
  • 6
  • 33
  • 59
  • 1
    r.68 was released recently, you should switch to that really (not saying it __will__ fix your issues, but no point lagging behind when dealing with a possible bug in the code somewhere). – Leeft Jul 21 '14 at 14:18
  • @Leeft: Absolutely! I am currently on r67. Will give it a try. – Isaac Jul 21 '14 at 14:23
  • Can you provide a link to a live example? – WestLangley Jul 21 '14 at 15:54
  • @WestLangley Sure: http://ztco.ir/emulate/index.html – Isaac Jul 21 '14 at 16:05
  • 2
    Your mesh geometry does not have UVs, but your mesh material has a texture. – WestLangley Jul 21 '14 at 16:48
  • @WestLangley: I've exported this model from Blender, checking "Include UVs". So, what do I have to do to solve this issue? – Isaac Jul 21 '14 at 18:28
  • Make sure to UV unwrap your model in blender. Go to edit mode (tab), select all faces, press "u", and click "unwrap". Then try to re-export. – beiller Jul 21 '14 at 18:53
  • @beiller: It DID help. (I choose "Unwrap", there is also a "Smart UV Project"). Why? Can you provide more explanation on what was missing and What "Unwraping" is doing? Is it something that must be done with more care for more complex object? Possibly as an answer so I can accept it. – Isaac Jul 21 '14 at 19:05
  • Placed elaboration as answer :) – beiller Jul 21 '14 at 19:08

1 Answers1

7

The problem is you have not set up UV coordinates for your model. By default, each face applies the whole texture, but in blender the UVs are blank when exporting.

You want to specifically set up your UV coordinates. These are coordinates that show how to apply a texture to each face.

Make sure to UV unwrap your model in blender. Go to edit mode (tab), select all faces, press "u", and click "unwrap". Then try to re-export.

Unwrap is just 1 method, there are many. Experiment with different methods in blender to get the results you want (possibly the "reset" option).

beiller
  • 3,105
  • 1
  • 11
  • 19
  • Thank you! I had posted this question to [Blender.SE](http://blender.stackexchange.com) too but the guys over there couldn't help me. This [Wikipedia](http://en.wikipedia.org/wiki/UV_mapping) and [Blender](http://wiki.blender.org/index.php/Doc:2.6/Manual/Textures/Mapping/UV/Unwrapping) article also helped me to have a yet better understanding of this issue. – Isaac Jul 21 '14 at 20:41
  • Hello, sorry that so late, I am having a similar problem, but UV maping did not solve the problem. May it be because I am using cycles render engine instead of blender render? – Vlas Bashynskyi Jul 06 '15 at 14:57
  • 1
    Are you getting the same error as original poster? You should probably make a new question. If you are getting "out of bounds" type errors (attribute type 2 is UV), make sure your exported model has UV coordinates. – beiller Jul 06 '15 at 15:04