Author Topic: Object3D color not being read in shader  (Read 7518 times)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D color not being read in shader
« Reply #15 on: November 20, 2015, 08:42:18 pm »
About the material loader. It's really simple stuff (mtl is a String that contains the mtl-file's content as text):

Code: [Select]
                Map<String, Object[]> mats = new HashMap<String, Object[]>();
StringTokenizer st = new StringTokenizer(mtl, "\n");
String matName = null;
RGBColor col = null;
String texture = null;
String texture2 = null;
float[] cols = new float[3];
boolean next = true;
String line = null;
Float trans = null;

TextureManager texMan = TextureManager.getInstance();

int mode = 0;
while (st.hasMoreTokens()) {
if (next) {
line = st.nextToken().trim();
}
switch (mode) {
case (0): {
if (line.startsWith("newmtl ")) {
matName = line.substring(7).trim();
mode = 1;
next = true;
col = null;
texture = null;
trans = null;
Logger.log("Processing new material " + matName + "!", Logger.MESSAGE);
}
break;
}
case (1): {
String lLine = line.toLowerCase();
if (lLine.startsWith("kd ")) {
String sub = lLine.substring(3).trim();
StringTokenizer st2 = new StringTokenizer(sub, " ");
int ind = 0;
while (st2.hasMoreTokens() && ind < 3) {
String c = st2.nextToken();
try {
cols[ind] = Float.valueOf(c).floatValue();
} catch (Exception e) {
cols[ind] = 1;
Logger.log("Error in MTL-file near: " + line, Logger.ERROR);
}
ind++;
}
col = new RGBColor((int) (cols[0] * 255f), (int) (cols[1] * 255f), (int) (cols[2] * 255f));
} else {
if ((lLine.startsWith("map_kd") || lLine.startsWith("map_ka")) && line.length() > 7) {
// Textur
if (lLine.startsWith("map_kd")) {
texture = removeBogusData(line.substring(7).trim());
if (!texMan.containsTexture(texture)) {
texMan.addTexture(texture);
Logger.log("Texture named " + texture + " added to TextureManager!", Logger.MESSAGE);
}
} else {
texture2 = removeBogusData(line.substring(7).trim());
if (!texMan.containsTexture(texture2)) {
texMan.addTexture(texture2);
Logger.log("Texture named " + texture2 + " added to TextureManager!", Logger.MESSAGE);
}
}
} else {
if (lLine.startsWith("d ")) {
float tt = -1;
try {
tt = Float.parseFloat(line.substring(2).trim());
} catch (Exception e) {
}
if (tt != -1 && tt != 1) {
trans = Float.valueOf(tt);
}
} else {
if (lLine.startsWith("newmtl")) {
mode = 0;
next = false;
}
}
}
}
break;
}
}
if (!next || !st.hasMoreTokens()) {
mats.put(matName, new Object[] { col, texture, trans });
}
}

Offline zoalord12

  • byte
  • *
  • Posts: 29
    • View Profile
Re: Object3D color not being read in shader
« Reply #16 on: November 23, 2015, 03:52:54 pm »
Thanks EgonOlsen, with your help i was able to get the required results for my project :)

Offline zoalord12

  • byte
  • *
  • Posts: 29
    • View Profile
Re: Object3D color not being read in shader
« Reply #17 on: November 24, 2015, 10:48:06 pm »
More shower thoughts ...

I'm thinking about replacing the .mtl file flat colors with an unwrapped texture, 1 for each model.
So lets say im using a single texture for a model,,, its unwrapped UV on a flat texture with different colors/patterns whatever,

Now, is it possible to tag the vertices of the face, hand etc. in some way to help me figure out later what vertices belong to what texture ?

like i want to write this in my code

model.getSUBPART("FACE").setTexture( myNewFaceTexture);





Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D color not being read in shader
« Reply #18 on: November 24, 2015, 10:53:19 pm »
You can query the PolygonManager about polygon textures. However, and I'm not sure if I got this correctly, you can't assign individual textures to polygons than didn't have individual textures before. So you can't create an Object3D with 20 polygons that uses one texture and assign the first 10 polygon texture x and the second 10 polygon texture y, if that's what you have in mind...

Offline zoalord12

  • byte
  • *
  • Posts: 29
    • View Profile
Re: Object3D color not being read in shader
« Reply #19 on: November 24, 2015, 11:51:28 pm »
yeah no i got that part. all polygons will be textured initially.
once they are textured with the default color in the texture, now i want to change the textures.


Hmmm correct me here please,

 i think i need to save each material as a separate jpg, as in i cant get by using something like this

https://drive.google.com/file/d/0BzkvMWM-w80JT3JkWDNSZGVMVDQ/view?usp=sharing
since i assume the texture space is all shared hence the texturemanager will create only 1 texture.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D color not being read in shader
« Reply #20 on: November 25, 2015, 12:39:33 pm »
In that case, it's one texture. Depending on your needs, you might get away with a custom shader to modify the coloring of some parts. I've described the method that I'm using in my RPG here briefly: http://www.jpct.net/forum2/index.php/topic,2471.msg26829.html#msg26829

Offline zoalord12

  • byte
  • *
  • Posts: 29
    • View Profile
Re: Object3D color not being read in shader
« Reply #21 on: November 26, 2015, 12:00:46 am »
hmmm yeah i do get these color maps with some models. look like a good first solution, but i think its hacky

I mean there is a limitation on the number of materials you can represent this way right ? but thanks, i think doing my shader is the first step
so will get on that

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D color not being read in shader
« Reply #22 on: November 26, 2015, 06:57:38 am »
I don't see what's hacky with that. It just blends pixels based on some blending map. It doesn't rely on exact color values and it even allows for smooth transitions. You can create the color map on your own in any image editor that supports layers. It's just a variation of the usual texture splatting approach.

Offline zoalord12

  • byte
  • *
  • Posts: 29
    • View Profile
Re: Object3D color not being read in shader
« Reply #23 on: December 02, 2015, 03:35:42 pm »
oh i meant hacky as in its not that diverse
you can only represent a few number of materials this way, what if you want to differentiate , say 20 materials on the character
then the masking wont work, if i understand it correctly

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D color not being read in shader
« Reply #24 on: December 02, 2015, 04:06:13 pm »
oh i meant hacky as in its not that diverse
you can only represent a few number of materials this way, what if you want to differentiate , say 20 materials on the character
then the masking wont work, if i understand it correctly
...that's right...unless you revert to being hacky again and do some magic with color ranges instead of the actual colors... ;)