javascript - Three.js Update Texture image -
i'm using three.js create minecraft texture editor, similar this. i'm trying basic click-and-paint functionality down, can't seem figure out. have textures each face of each cube , apply them making shader materials following functions.
this.createbodyshadertexture = function(part, update) { sides = ['left', 'right', 'top', 'bottom', 'front', 'back']; images = []; (i = 0; < sides.length; i++) { images[i] = 'img/'+part+'/'+sides[i]+'.png'; } texcube = new three.imageutils.loadtexturecube(images); texcube.magfilter = three.nearestfilter; texcube.minfilter = three.linearmipmaplinearfilter; if (update) { texcube.needsupdate = true; console.log(texcube); } return texcube; } this.createbodyshadermaterial = function(part, update) { shader = three.shaderlib['cube']; shader.uniforms['tcube'].value = this.createbodyshadertexture(part, update); shader.fragmentshader = document.getelementbyid("fshader").innerhtml; shader.vertexshader = document.getelementbyid("vshader").innerhtml; material = new three.shadermaterial({fragmentshader: shader.fragmentshader, vertexshader: shader.vertexshader, uniforms: shader.uniforms}); return material; } skinapp.prototype.onclick = function(event) { event.preventdefault(); this.change(); //makes texture file simple red square testing this.avatar.remove(this.head); this.head = new three.mesh(new three.cubegeometry(8, 8, 8), this.createbodyshadermaterial('head', false)); this.head.position.y = 10; this.avatar.add(this.head); this.head.material.needsupdate = true; this.head.dynamic = true; }
then, when user clicks on mesh, texture file update using canvas. update occurs, change isn't showing in browser unless page refreshed. i've found plenty of examples of how change texture image new file, not on how show changes in same texture file during runtime, or if it's possible. possible, , if not alternatives there?
when update texture, whether based on canvas, video or loaded externally, need set following property on texture true:
if object created this:
var canvas = document.createelement("canvas"); var canvasmap = new three.texture(canvas) var mat = new three.meshphongmaterial(); mat.map = canvasmap; var mesh = new three.mesh(geom,mat);
after texture has been changed, set following true:
cube.material.map.needsupdate = true;
and next time render scene it'll show new texture.
Comments
Post a Comment