javafx - Using texture for triangle mesh without having to read/write an image file -



javafx - Using texture for triangle mesh without having to read/write an image file -

this followup on previous question (see coloring individual triangles in triangle mesh on javafx) believe topic on own.

is there way (with javafx) can away having write disk (or external device) image file utilize texture?

in other words: can utilize specific texture without having utilize image?

since color map alter on runtime don't want have write disk every time run it. also, might security issue (writing disk) using app.(with javafx)

as @jens-peter-haack suggest, snapshot can create image want, , apply image diffusion map. that, need create nodes, fill them colors require, grouping them in container , take snapshot.

there's straight-in approach, can build image pattern of colors using pixelwriter.

let's want 256 colors, method homecoming image of 256 pixels, each pixel has 1 of these colors. simplicity i've added 2 simple ways of building palette.

public static image colorpallete(int numcolors){ int width=(int)math.sqrt(numcolors); int height=numcolors/width; writableimage img = new writableimage(width, height); pixelwriter pw = img.getpixelwriter(); atomicinteger count = new atomicinteger(); intstream.range(0, height).boxed() .foreach(y->intstream.range(0, width).boxed() .foreach(x->pw.setcolor(x, y, getcolor(count.getandincrement(),numcolors)))); // save testing purposes seek { imageio.write(swingfxutils.fromfximage(img, null), "jpg", new file("palette.jpg")); } grab (ioexception ex) { } homecoming img; } private color getcolor(int icolor, int numcolors){ // nice palette of colors java.awt.color c = java.awt.color.gethsbcolor((float) icolor / (float) numcolors, 1.0f, 1.0f); homecoming color.rgb(c.getred(), c.getgreen(), c.getblue()); // raw palette //return color.rgb((icolor >> 16) & 0xff, (icolor >> 8) & 0xff, icolor & 0xff); }

once have image object, can set diffuse map:

icosahedronmesh mesh = new icosahedronmesh(); phongmaterial mat = new phongmaterial(); mat.setdiffusemap(colorpallete(256)); mesh.setmaterial(mat);

but still have provide proper mapping new texture.

for need map vertices of mesh pixel in image.

first, need way map colors texture coordinates on mesh. method homecoming pair of coordinates given color index:

public static float[] gettexturelocation(int ipoint, int numcolors){ int width=(int)math.sqrt(numcolors); int height=numcolors/width; int y = ipoint/width; int x = ipoint-width*y; homecoming new float[]{(((float)x)/((float)width)),(((float)y)/((float)height))}; }

finally, add together these textures m.gettextcoords() , faces m.getfaces(), shown here.

if assign color every vertex in our icosahedron pick color palette (scaling or downwards according number of colors , vertices), , set every face t0=p0, t1=p1, t2=p2:

intstream.range(0,numvertices).boxed() .foreach(i->m.gettexcoords() .addall(gettexturelocation(i*numcolors/numvertices,numcolors))); m.getfaces().addall( 1, 1, 11, 11, 7, 7, 1, 1, 7, 7, 6, 6, 1, 1, 6, 6, 10, 10, 1, 1, 10, 10, 3, 3, 1, 1, 3, 3, 11, 11, 4, 4, 8, 8, 0, 0, 5, 5, 4, 4, 0, 0, 9, 9, 5, 5, 0, 0, 2, 2, 9, 9, 0, 0, 8, 8, 2, 2, 0, 0, 11, 11, 9, 9, 7, 7, 7, 7, 2, 2, 6, 6, 6, 6, 8, 8, 10, 10, 10, 10, 4, 4, 3, 3, 3, 3, 5, 5, 11, 11, 4, 4, 10, 10, 8, 8, 5, 5, 3, 3, 4, 4, 9, 9, 11, 11, 5, 5, 2, 2, 7, 7, 9, 9, 8, 8, 6, 6, 2, 2 );

this give this:

edit

playing around textures coordinates, instead of mapping node color, can add together function , create contour plot, this:

javafx textures texture-mapping

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -