sto cercando di definire un materiale a maglie ho caricato in dal OBJLoader attraverso la seguente funzione wrapper:Tre JS materiale cartografico provoca WebGL Attenzione
function applyTexture(src){
var texture = new THREE.Texture();
var loader = new THREE.ImageLoader();
loader.addEventListener('load', function (event) {
texture.image = event.content;
texture.needsUpdate = true;
// find the meshes from the loaded OBJ and apply the texture to it.
object.traverse(function (child) {
if (child instanceof THREE.Mesh) {
if(child.name.indexOf("Lens") < 0){
child.dynamic = true;
child.material = new THREE.MeshLambertMaterial({ color: 0xdddddd, shading: THREE.FlatShading, map : texture });
// also tried:
//child.material = new THREE.MeshPhongMaterial({ color: 0x000000, specular: 0x666666, emissive: 0x000000, ambient: 0x000000, shininess: 10, shading: THREE.SmoothShading, map : texture});
// and:
//child.material = new THREE.MeshBasicMaterial({map : texture});
child.material.map = texture; // won't throw the WebGL Warning, but won't show the texture either;
} else {
// works just fine.
child.material = new THREE.MeshPhongMaterial({ color: 0x000000, specular: 0x666666, emissive: 0x000011, ambient: 0x000000, shininess: 10, shading: THREE.SmoothShading, opacity: 0.6, transparent: true });
}
}
});
});
loader.load(src);
}
Quando il tessuto è stato caricato ed è il momento di applicare il materiale alla mesh, inizio a ricevere il seguente avviso sulla console:
.WebGLRenderingContext: GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 0
e la trama scompare.
cosa sto facendo di sbagliato qui?
UPDATE
Come @WestLangley puntato sui commenti: Non tentare mai di applicare texture/materiali dopo che le cose sono state rese. Creare i materiali prima del rendering l'oggetto alla scena e poi modificarle utilizzando:
obj.material.map = texture
Funziona se si aggiunge la trama al materiale dall'inizio, quindi il primo rendering della mesh include la trama? – WestLangley
@WestLangley Una texture viene applicata quando viene caricata la mesh e prima che venga aggiunta alla scena e che ciò non sia possibile. Quando ho provato a utilizzare "child.material.map = texture" non ho ricevuto l'avviso WebGL, ma la texture non è stata applicata. Per qualsiasi motivo, "child.material = new THREE.MeshLambertMaterial ({color: 0xdddddd, shading: THREE.FlatShading, map: texture});" applica la texture quando si modifica il renderer da WebGL a Canvas. Codice completo qui: https://gist.github.com/jrmoretti/81047c9d6821e4bbaac5 – JayMoretti
In WebGLRenderer, non è possibile aggiungere una texture dopo il fatto. Puoi cambiare solo la trama, però. Una soluzione è iniziare con un materiale con una semplice trama bianca. – WestLangley