Programmatically generate simple UV Mapping for models
<p>Coming from <a href="https://stackoverflow.com/questions/34932860/how-to-apply-a-texture-to-a-custom-geometry-in-three-js#34937000%20%22%22">this</a> question I'm trying to generate UV Mappings programmatically with Three.js for some models, I need this because my models are being generated programmatically too and I need to apply a simple texture to them. I have read <a href="https://stackoverflow.com/questions/20774648/three-js-generate-uv-coordinate">here</a> and successfully generated UV mapping for some simple 3D text but when applying the same mapping to more complex models it just doesn't work.</p>

<p>The texture I'm trying to apply is something like this:</p>

<p><a href="https://i.stack.imgur.com/ZMO90.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZMO90.png" alt="enter image description here"></a></p>

<p>The black background it's just transparent in the PNG image. I need to apply this to my models, it's just a glitter effect so I don't care about the exact position in the model, is any way to create a simple UV Map programatically for this cases?</p>

<p>I'm using this code from the linked question which works great for planar models but doesn't work for non-planar models:</p>

<pre><code>assignUVs = function( geometry ){

    geometry.computeBoundingBox();

    var max     = geometry.boundingBox.max;
    var min     = geometry.boundingBox.min;

    var offset  = new THREE.Vector2(0 - min.x, 0 - min.y);
    var range   = new THREE.Vector2(max.x - min.x, max.y - min.y);

    geometry.faceVertexUvs[0] = [];
    var faces = geometry.faces;

    for (i = 0; i &lt; geometry.faces.length ; i++) {

      var v1 = geometry.vertices[faces[i].a];
      var v2 = geometry.vertices[faces[i].b];
      var v3 = geometry.vertices[faces[i].c];

      geometry.faceVertexUvs[0].push([
        new THREE.Vector2( ( v1.x + offset.x ) / range.x , ( v1.y + offset.y ) / range.y ),
        new THREE.Vector2( ( v2.x + offset.x ) / range.x , ( v2.y + offset.y ) / range.y ),
        new THREE.Vector2( ( v3.x + offset.x ) / range.x , ( v3.y + offset.y ) / range.y )
      ]);

    }

    geometry.uvsNeedUpdate = true;
}
</code></pre>