overhaul material loading
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit

This commit is contained in:
austin 2025-05-19 17:04:36 -04:00
parent 2599343427
commit ef0adbe3e6
24 changed files with 256 additions and 239 deletions

View File

@ -1905,6 +1905,7 @@ Ambient light sent in dedicated variable to shader
Major lighting shader organization rework Major lighting shader organization rework
Material albedo work Material albedo work
Editor entities don't use charges on placing blocks Editor entities don't use charges on placing blocks
Overhaul material loading - uses queued textures now

View File

@ -16,7 +16,6 @@ import electrosphere.client.terrain.cache.ChunkData;
import electrosphere.data.entity.foliage.FoliageType; import electrosphere.data.entity.foliage.FoliageType;
import electrosphere.engine.Globals; import electrosphere.engine.Globals;
import electrosphere.engine.assetmanager.queue.QueuedTexture; import electrosphere.engine.assetmanager.queue.QueuedTexture;
import electrosphere.engine.assetmanager.queue.QueuedTexture.QueuedTextureType;
import electrosphere.engine.threads.ThreadCounts; import electrosphere.engine.threads.ThreadCounts;
import electrosphere.entity.ClientEntityUtils; import electrosphere.entity.ClientEntityUtils;
import electrosphere.entity.Entity; import electrosphere.entity.Entity;
@ -249,7 +248,7 @@ public class FoliageModel {
int textureWidth = (1 + (drawCount / MAX_TEXTURE_HEIGHT)) * (SINGLE_FOLIAGE_DATA_SIZE_BYTES / 4); int textureWidth = (1 + (drawCount / MAX_TEXTURE_HEIGHT)) * (SINGLE_FOLIAGE_DATA_SIZE_BYTES / 4);
//construct data texture //construct data texture
QueuedTexture queuedAsset = new QueuedTexture(QueuedTextureType.DATA_BUFF,buffer,textureWidth,textureHeight); QueuedTexture queuedAsset = QueuedTexture.createFromBuffer(buffer,textureWidth,textureHeight);
Globals.assetManager.queuedAsset(queuedAsset); Globals.assetManager.queuedAsset(queuedAsset);
TextureInstancedActor actor = TextureInstancedActor.attachTextureInstancedActor(rVal, foliageType.getGraphicsTemplate().getModel().getPath(), vertexPath, fragmentPath, queuedAsset, drawCount, textureWidth / NUM_PER_INSTANCE_VARS); TextureInstancedActor actor = TextureInstancedActor.attachTextureInstancedActor(rVal, foliageType.getGraphicsTemplate().getModel().getPath(), vertexPath, fragmentPath, queuedAsset, drawCount, textureWidth / NUM_PER_INSTANCE_VARS);

View File

@ -31,52 +31,57 @@ public class QueuedTexture implements QueuedAsset<Texture> {
/** /**
* The type of queued texture * The type of queued texture
*/ */
QueuedTextureType type; private QueuedTextureType type;
/** /**
* True if loaded * True if loaded
*/ */
boolean hasLoaded = false; private boolean hasLoaded = false;
/** /**
* The resuling texture object * The resuling texture object
*/ */
Texture texture = null; private Texture texture = null;
/** /**
* The data to be loaded * The data to be loaded
*/ */
BufferedImage data; private BufferedImage data;
/** /**
* The byte buffer * The byte buffer
*/ */
ByteBuffer buffer; private ByteBuffer buffer;
/** /**
* Width of the image * Width of the image
*/ */
int width = -1; private int width = -1;
/** /**
* Height of the image * Height of the image
*/ */
int height = -1; private int height = -1;
/** /**
* The path the asset manager promises this texture will be stored at * The path the asset manager promises this texture will be stored at
*/ */
String promisedPath; private String promisedPath;
/** /**
* true if the path to register this asset was supplied while it was being queued, false if the promised path should be generated when it is placed in queue * true if the path to register this asset was supplied while it was being queued, false if the promised path should be generated when it is placed in queue
*/ */
boolean suppliedPath = false; private boolean suppliedPath = false;
/** /**
* The runnable to invoke to actually load the model * The runnable to invoke to actually load the model
*/ */
Consumer<Texture> loadFunc; private Consumer<Texture> loadFunc;
/**
* Constructor
*/
private QueuedTexture(){ }
/** /**
* Creates the queued texture object * Creates the queued texture object
@ -84,13 +89,29 @@ public class QueuedTexture implements QueuedAsset<Texture> {
* @param width The width of the buffer * @param width The width of the buffer
* @param height The height of the buffer * @param height The height of the buffer
*/ */
public QueuedTexture(QueuedTextureType type, ByteBuffer buffer, int width, int height){ private QueuedTexture(QueuedTextureType type, ByteBuffer buffer, int width, int height){
this.type = type; this.type = type;
this.buffer = buffer; this.buffer = buffer;
this.width = width; this.width = width;
this.height = height; this.height = height;
} }
/**
* Creates a queued texture from a buffer
* @param buffer The buffer
* @param width The width of the buffer
* @param height The height of the buffer
* @return The queued texture
*/
public static QueuedTexture createFromBuffer(ByteBuffer buffer, int width, int height){
QueuedTexture rVal = new QueuedTexture();
rVal.type = QueuedTextureType.DATA_BUFF;
rVal.buffer = buffer;
rVal.width = width;
rVal.height = height;
return rVal;
}
/** /**
* Creates the queued texture object * Creates the queued texture object
* @param image the image to load to gpu * @param image the image to load to gpu
@ -175,6 +196,20 @@ public class QueuedTexture implements QueuedAsset<Texture> {
return rVal; return rVal;
} }
/**
* Creates the queued texture object
* @param path The path to the texture file
* @param loadFunc The function to invoke when the texture loads
*/
public static QueuedTexture createFromPath(String path, Consumer<Texture> loadFunc){
QueuedTexture rVal = new QueuedTexture();
rVal.type = QueuedTextureType.IMG_BUFF;
rVal.promisedPath = path;
rVal.loadFunc = loadFunc;
rVal.suppliedPath = true;
return rVal;
}
@Override @Override
public void load() { public void load() {
switch(this.type){ switch(this.type){
@ -183,7 +218,11 @@ public class QueuedTexture implements QueuedAsset<Texture> {
this.buffer = null; this.buffer = null;
} break; } break;
case IMG_BUFF: { case IMG_BUFF: {
texture = new Texture(Globals.renderingEngine.getOpenGLState(), data, buffer); if(data != null){
texture = new Texture(Globals.renderingEngine.getOpenGLState(), data, buffer);
} else if(promisedPath != null){
texture = new Texture(Globals.renderingEngine.getOpenGLState(), promisedPath);
}
} break; } break;
} }
if(this.loadFunc != null){ if(this.loadFunc != null){

View File

@ -220,7 +220,7 @@ public class RenderUtils {
particleMesh.setMaterial(new Material(AssetDataStrings.TEXTURE_PARTICLE)); particleMesh.setMaterial(Material.createExisting(AssetDataStrings.TEXTURE_PARTICLE));
@ -391,8 +391,7 @@ public class RenderUtils {
//setup extra structures //setup extra structures
Material mat = new Material(); Material mat = Material.createExisting(AssetDataStrings.TEXTURE_TEAL_TRANSPARENT);
mat.setDiffuse(AssetDataStrings.TEXTURE_TEAL_TRANSPARENT);
sphereMesh.setMaterial(mat); sphereMesh.setMaterial(mat);
sphereMesh.setShader(VisualShader.smartAssembleShader()); sphereMesh.setShader(VisualShader.smartAssembleShader());
openGLState.glBindVertexArray(0); openGLState.glBindVertexArray(0);
@ -447,8 +446,7 @@ public class RenderUtils {
} }
//setup extra structures //setup extra structures
Material mat = new Material(); Material mat = Material.createExisting(AssetDataStrings.TEXTURE_TEAL_TRANSPARENT);
mat.setDiffuse(AssetDataStrings.TEXTURE_TEAL_TRANSPARENT);
sphereMesh.setMaterial(mat); sphereMesh.setMaterial(mat);
sphereMesh.setShader(VisualShader.smartAssembleShader()); sphereMesh.setShader(VisualShader.smartAssembleShader());
openGLState.glBindVertexArray(0); openGLState.glBindVertexArray(0);
@ -535,8 +533,7 @@ public class RenderUtils {
sphereMesh.bufferTextureCoords(texCoords, 2); sphereMesh.bufferTextureCoords(texCoords, 2);
//setup extra structures //setup extra structures
Material mat = new Material(); Material mat = Material.createExisting(AssetDataStrings.TEXTURE_TEAL_TRANSPARENT);
mat.setDiffuse(AssetDataStrings.TEXTURE_TEAL_TRANSPARENT);
sphereMesh.setMaterial(mat); sphereMesh.setMaterial(mat);
sphereMesh.setShader(VisualShader.smartAssembleShader()); sphereMesh.setShader(VisualShader.smartAssembleShader());
openGLState.glBindVertexArray(0); openGLState.glBindVertexArray(0);
@ -623,8 +620,7 @@ public class RenderUtils {
cubeMesh.bufferTextureCoords(texCoords, 2); cubeMesh.bufferTextureCoords(texCoords, 2);
//setup extra structures //setup extra structures
Material mat = new Material(); Material mat = Material.createExisting(AssetDataStrings.TEXTURE_BLOCK_ATLAS);
mat.setDiffuse(AssetDataStrings.TEXTURE_BLOCK_ATLAS);
cubeMesh.setMaterial(mat); cubeMesh.setMaterial(mat);
cubeMesh.setShader(VisualShader.loadSpecificShader(AssetDataStrings.SHADER_BLOCK_SINGLE_VERT, AssetDataStrings.SHADER_BLOCK_SINGLE_FRAG)); cubeMesh.setShader(VisualShader.loadSpecificShader(AssetDataStrings.SHADER_BLOCK_SINGLE_VERT, AssetDataStrings.SHADER_BLOCK_SINGLE_FRAG));
openGLState.glBindVertexArray(0); openGLState.glBindVertexArray(0);
@ -714,10 +710,7 @@ public class RenderUtils {
openGLState.glBindVertexArray(0); openGLState.glBindVertexArray(0);
m.setParent(rVal); m.setParent(rVal);
Material uiMat = new Material(); Material uiMat = Material.create("/Textures/Fonts/myfont1-harsher.png");
Globals.assetManager.addTexturePathtoQueue("/Textures/Fonts/myfont1-harsher.png");
uiMat.setDiffuse("/Textures/Fonts/myfont1-harsher.png");
uiMat.setSpecular("/Textures/Fonts/myfont1-harsher.png");
m.setMaterial(uiMat); m.setMaterial(uiMat);
rVal.getMaterials().add(uiMat); rVal.getMaterials().add(uiMat);
@ -1067,10 +1060,7 @@ public class RenderUtils {
openGLState.glBindVertexArray(0); openGLState.glBindVertexArray(0);
m.setParent(rVal); m.setParent(rVal);
Material groundMat = new Material(); Material groundMat = Material.create("/Textures/Ground/Dirt1.png");
Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png");
groundMat.setDiffuse("/Textures/Ground/Dirt1.png");
groundMat.setSpecular("/Textures/Ground/Dirt1.png");
m.setMaterial(groundMat); m.setMaterial(groundMat);
rVal.getMeshes().add(m); rVal.getMeshes().add(m);
@ -1624,10 +1614,7 @@ public class RenderUtils {
openGLState.glBindVertexArray(0); openGLState.glBindVertexArray(0);
m.setParent(rVal); m.setParent(rVal);
Material groundMat = new Material(); Material groundMat = Material.create("/Textures/Ground/Dirt1.png");
Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png");
groundMat.setDiffuse("/Textures/Ground/Dirt1.png");
groundMat.setSpecular("/Textures/Ground/Dirt1.png");
m.setMaterial(groundMat); m.setMaterial(groundMat);
rVal.getMeshes().add(m); rVal.getMeshes().add(m);

View File

@ -190,7 +190,7 @@ public class RenderingEngine {
/** /**
* The default material * The default material
*/ */
private Material materialDefault = new Material(AssetDataStrings.TEXTURE_DEFAULT); private Material materialDefault = Material.createExisting(AssetDataStrings.TEXTURE_DEFAULT);
/** /**
* the current state of the rendering pipeline * the current state of the rendering pipeline

View File

@ -69,7 +69,7 @@ public class TextureInstancedActor {
protected TextureInstancedActor(String modelPath, String vertexShaderPath, String fragmentShaderPath, Texture dataTexture, int drawCount){ protected TextureInstancedActor(String modelPath, String vertexShaderPath, String fragmentShaderPath, Texture dataTexture, int drawCount){
this.modelPath = modelPath; this.modelPath = modelPath;
this.material = new Material(); this.material = new Material();
this.material.setTexturePointer(dataTexture.getTexturePointer()); this.material.setDiffuse(dataTexture);
this.drawCount = drawCount; this.drawCount = drawCount;
this.vertexShaderPath = vertexShaderPath; this.vertexShaderPath = vertexShaderPath;
this.fragmentShaderPath = fragmentShaderPath; this.fragmentShaderPath = fragmentShaderPath;
@ -126,7 +126,7 @@ public class TextureInstancedActor {
Model model = Globals.assetManager.fetchModel(modelPath); Model model = Globals.assetManager.fetchModel(modelPath);
VisualShader shader = Globals.assetManager.fetchShader(vertexShaderPath, fragmentShaderPath); VisualShader shader = Globals.assetManager.fetchShader(vertexShaderPath, fragmentShaderPath);
if(queuedTexture != null && !setQueuedTexturePointer && queuedTexture.getTexture() != null){ if(queuedTexture != null && !setQueuedTexturePointer && queuedTexture.getTexture() != null){
this.material.setTexturePointer(queuedTexture.getTexture().getTexturePointer()); this.material.setDiffuse(queuedTexture.getTexture());
setQueuedTexturePointer = true; setQueuedTexturePointer = true;
} }
if( if(

View File

@ -101,7 +101,7 @@ public class ModelLoader {
} }
} }
} else { } else {
LoggerInterface.loggerRenderer.WARNING("Trying to get textures for model that doesn't have local or global texture map entries! " + path); // LoggerInterface.loggerRenderer.WARNING("Trying to get textures for model that doesn't have local or global texture map entries! " + path);
} }
} }
@ -116,24 +116,16 @@ public class ModelLoader {
//set diffuse //set diffuse
String diffusePath = meshTextureData.getDiffuse(); String diffusePath = meshTextureData.getDiffuse();
LoggerInterface.loggerRenderer.DEBUG(mesh.getMeshName() + "->" + diffusePath); LoggerInterface.loggerRenderer.DEBUG(mesh.getMeshName() + "->" + diffusePath);
if(diffusePath != null){ if(diffusePath == null){
LoggerInterface.loggerRenderer.DEBUG(diffusePath); diffusePath = AssetDataStrings.TEXTURE_DEFAULT;
Globals.assetManager.addTexturePathtoQueue(diffusePath);
finalMat.setDiffuse(diffusePath);
LoggerInterface.loggerRenderer.DEBUG(diffusePath);
} else {
finalMat.setDiffuse(AssetDataStrings.TEXTURE_DEFAULT);
} }
//set specular //set specular
String specularPath = meshTextureData.getSpecular(); String specularPath = meshTextureData.getSpecular();
if(specularPath != null){ if(specularPath == null){
Globals.assetManager.addTexturePathtoQueue(specularPath); specularPath = AssetDataStrings.TEXTURE_DEFAULT;
finalMat.setSpecular(specularPath);
LoggerInterface.loggerRenderer.DEBUG(specularPath);
} else {
finalMat.setSpecular(AssetDataStrings.TEXTURE_DEFAULT);
} }
finalMat = Material.create(diffusePath, specularPath);
//once we've either added default textures or actual textures, //once we've either added default textures or actual textures,
//set the current mesh's material to this new one //set the current mesh's material to this new one
mesh.setMaterial(finalMat); mesh.setMaterial(finalMat);

View File

@ -580,9 +580,7 @@ public class BlockMeshgen {
Mesh m = BlockMeshgen.generateBlockMesh(Globals.renderingEngine.getOpenGLState(), meshData); Mesh m = BlockMeshgen.generateBlockMesh(Globals.renderingEngine.getOpenGLState(), meshData);
//construct the material for the chunk //construct the material for the chunk
Material groundMat = new Material(); Material groundMat = Material.create(Globals.blockTextureAtlas.getSpecular(), Globals.blockTextureAtlas.getNormal());
groundMat.setTexturePointer(Globals.blockTextureAtlas.getSpecular().getTexturePointer());
groundMat.setNormalTexturePointer(Globals.blockTextureAtlas.getNormal().getTexturePointer());
m.setMaterial(groundMat); m.setMaterial(groundMat);
//shader logic //shader logic

View File

@ -791,10 +791,7 @@ public class FluidChunkModelGeneration {
Mesh m = generateFluidMesh(Globals.renderingEngine.getOpenGLState(),data); Mesh m = generateFluidMesh(Globals.renderingEngine.getOpenGLState(),data);
Material groundMat = new Material(); Material groundMat = Material.create("/Textures/Ground/Dirt1.png");
groundMat.setDiffuse("/Textures/Ground/Dirt1.png");
groundMat.setSpecular("/Textures/Ground/Dirt1.png");
Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png");
m.setMaterial(groundMat); m.setMaterial(groundMat);
m.setShader(FluidChunkModelGeneration.fluidChunkShaderProgram); m.setShader(FluidChunkModelGeneration.fluidChunkShaderProgram);

View File

@ -886,9 +886,7 @@ public class TerrainChunkModelGeneration {
Mesh m = TerrainChunkModelGeneration.generateTerrainMesh(Globals.renderingEngine.getOpenGLState(), data); Mesh m = TerrainChunkModelGeneration.generateTerrainMesh(Globals.renderingEngine.getOpenGLState(), data);
//construct the material for the chunk //construct the material for the chunk
Material groundMat = new Material(); Material groundMat = Material.create(atlas.getSpecular(), atlas.getNormal());
groundMat.setTexturePointer(atlas.getSpecular().getTexturePointer());
groundMat.setNormalTexturePointer(atlas.getNormal().getTexturePointer());
m.setMaterial(groundMat); m.setMaterial(groundMat);
//shader logic //shader logic

View File

@ -2296,9 +2296,7 @@ public class TransvoxelModelGeneration {
Mesh m = TransvoxelModelGeneration.generateTerrainMesh(Globals.renderingEngine.getOpenGLState(), data); Mesh m = TransvoxelModelGeneration.generateTerrainMesh(Globals.renderingEngine.getOpenGLState(), data);
//construct the material for the chunk //construct the material for the chunk
Material groundMat = new Material(); Material groundMat = Material.create(atlas.getSpecular(), atlas.getNormal());
groundMat.setTexturePointer(atlas.getSpecular().getTexturePointer());
groundMat.setNormalTexturePointer(atlas.getNormal().getTexturePointer());
m.setMaterial(groundMat); m.setMaterial(groundMat);
//shader logic //shader logic

View File

@ -1,6 +1,7 @@
package electrosphere.renderer.model; package electrosphere.renderer.model;
import electrosphere.engine.Globals; import electrosphere.engine.Globals;
import electrosphere.engine.assetmanager.queue.QueuedTexture;
import electrosphere.logger.LoggerInterface; import electrosphere.logger.LoggerInterface;
import electrosphere.renderer.OpenGLState; import electrosphere.renderer.OpenGLState;
import electrosphere.renderer.texture.Texture; import electrosphere.renderer.texture.Texture;
@ -40,35 +41,19 @@ public class Material {
/** /**
* The path of the diffuse texture when using texture lookups * The diffuse texture
*/ */
private String diffuse; private Texture diffuse;
/** /**
* The path of the specular texture when using texture lookups * The specular texture
*/ */
private String specular; private Texture specular;
/** /**
* Tracks whether this material has transparency or not * used to continuously poll for an existing texture (that maybe is still being loaded in) to assign to this material
*/ */
public boolean hasTransparency = false; private String existingPath = null;
/**
* Sets whether this material should get its texture pointers from the assetManager by looking up diffuse and specular paths
* or whether it should have a manually set texturePointer and not look up while binding
*/
private boolean usesFetch = true;
/**
* texture pointer for the specular
*/
private int texturePointer;
/**
* texture pointer for the normal
*/
private int normalPointer;
/** /**
* The shininess value * The shininess value
@ -92,11 +77,84 @@ public class Material {
} }
/** /**
* Creates a material with a diffuse texture * Creates a material with a supplied diffuse path
* @param diffuse The path to the diffuse texture * @param diffuse The diffuse path
* @return The material
*/ */
public Material(String diffuse){ public static Material create(String diffuse){
this.diffuse = diffuse; if(diffuse == null){
throw new Error("Diffuse undefined");
}
Material rVal = new Material();
Globals.assetManager.queuedAsset(QueuedTexture.createFromPath(diffuse, (Texture tex) -> {
if(tex == null){
throw new Error("Failed to load " + diffuse);
}
rVal.diffuse = tex;
}));
return rVal;
}
/**
* Creates a material with a supplied diffuse path
* @param diffuse The diffuse path
* @return The material
*/
public static Material createExisting(String diffuse){
if(diffuse == null){
throw new Error("Diffuse undefined");
}
Material rVal = new Material();
rVal.existingPath = diffuse;
return rVal;
}
/**
* Creates a material with a supplied diffuse and specular path
* @param diffuse The diffuse path
* @param specular The specular path
* @return The material
*/
public static Material create(String diffuse, String specular){
if(diffuse == null){
throw new Error("Diffuse undefined");
}
if(specular == null){
throw new Error("Diffuse undefined");
}
Material rVal = new Material();
Globals.assetManager.queuedAsset(QueuedTexture.createFromPath(diffuse, (Texture tex) -> {
if(tex == null){
throw new Error("Failed to load " + diffuse);
}
rVal.diffuse = tex;
}));
Globals.assetManager.queuedAsset(QueuedTexture.createFromPath(specular, (Texture tex) -> {
if(tex == null){
throw new Error("Failed to load " + specular);
}
rVal.specular = tex;
}));
return rVal;
}
/**
* Creates a material with a supplied diffuse and specular path
* @param diffuse The diffuse path
* @param specular The specular path
* @return The material
*/
public static Material create(Texture diffuse, Texture specular){
if(diffuse == null){
throw new Error("Diffuse undefined");
}
if(specular == null){
throw new Error("Diffuse undefined");
}
Material rVal = new Material();
rVal.diffuse = diffuse;
rVal.specular = specular;
return rVal;
} }
/** /**
@ -126,12 +184,47 @@ public class Material {
texPaths[i] = tex.mFilename().dataString(); texPaths[i] = tex.mFilename().dataString();
} }
//discover diffuse //discover diffuse
boolean discovered = Material.tryDiscoverDiffuse(path,scene,input,texPaths,aiPathString,Assimp.aiTextureType_DIFFUSE,rVal);
if(!discovered){
discovered = Material.tryDiscoverDiffuse(path,scene,input,texPaths,aiPathString,Assimp.aiTextureType_BASE_COLOR,rVal);
}
if(!discovered){
discovered = Material.tryDiscoverDiffuse(path,scene,input,texPaths,aiPathString,Assimp.aiTextureType_EMISSIVE,rVal);
}
if(!discovered){
discovered = Material.tryDiscoverDiffuse(path,scene,input,texPaths,aiPathString,Assimp.aiTextureType_AMBIENT,rVal);
}
if(!discovered){
discovered = Material.tryDiscoverDiffuse(path,scene,input,texPaths,aiPathString,Assimp.aiTextureType_EMISSION_COLOR,rVal);
}
if(!discovered){
LoggerInterface.loggerRenderer.DEBUG("Failed to find diffuse path for mesh in " + path);
}
//free mem
aiPathString.free();
return rVal;
}
/**
* Tries to discover a material at a given texture type
* @param path The path to the model
* @param scene The scene of the model
* @param input The input material
* @param texPaths The texture paths
* @param aiPathString The ai string buffer
* @param mat The material object
* @return true if a diffuse was found, false otherwise
*/
private static boolean tryDiscoverDiffuse(String path, AIScene scene, AIMaterial input, String[] texPaths, AIString aiPathString, int texType, Material mat){
boolean foundDiffuse = false; boolean foundDiffuse = false;
int textureCount = Assimp.aiGetMaterialTextureCount(input, Assimp.aiTextureType_DIFFUSE); int textureCount = Assimp.aiGetMaterialTextureCount(input, texType);
if(textureCount > 0){ if(textureCount > 0){
//for the time being, only load the first diffuse //for the time being, only load the first diffuse
int textureIndex = 0; int textureIndex = 0;
int retCode = Assimp.aiGetMaterialTexture(input, Assimp.aiTextureType_DIFFUSE, textureIndex, aiPathString, (IntBuffer)null, null, null, null, null, null); int retCode = Assimp.aiGetMaterialTexture(input, texType, textureIndex, aiPathString, (IntBuffer)null, null, null, null, null, null);
if(retCode != Assimp.aiReturn_SUCCESS){ if(retCode != Assimp.aiReturn_SUCCESS){
throw new Error("Failed to read diffuse! " + textureCount + " " + Assimp.aiGetErrorString()); throw new Error("Failed to read diffuse! " + textureCount + " " + Assimp.aiGetErrorString());
} }
@ -148,64 +241,28 @@ public class Material {
} }
String resolved = Material.resolveTexturePath(path, texPaths[indexInLoadedTexturePaths]); String resolved = Material.resolveTexturePath(path, texPaths[indexInLoadedTexturePaths]);
if(resolved != null && resolved.length() > 0){ if(resolved != null && resolved.length() > 0){
rVal.usesFetch = true; Globals.assetManager.queuedAsset(QueuedTexture.createFromPath(resolved, (Texture tex) -> {
rVal.diffuse = resolved; if(tex == null){
Globals.assetManager.addTexturePathtoQueue(rVal.diffuse); throw new Error("Failed to load " + resolved);
}
mat.diffuse = tex;
}));
foundDiffuse = true; foundDiffuse = true;
} }
} else { } else {
String resolved = Material.resolveTexturePath(path, texturePath); String resolved = Material.resolveTexturePath(path, texturePath);
if(resolved != null && resolved.length() > 0){ if(resolved != null && resolved.length() > 0){
rVal.usesFetch = true; Globals.assetManager.queuedAsset(QueuedTexture.createFromPath(resolved, (Texture tex) -> {
rVal.diffuse = resolved; if(tex == null){
Globals.assetManager.addTexturePathtoQueue(rVal.diffuse); throw new Error("Failed to load " + resolved);
}
mat.diffuse = tex;
}));
foundDiffuse = true; foundDiffuse = true;
} }
} }
} }
if(!foundDiffuse){ return foundDiffuse;
textureCount = Assimp.aiGetMaterialTextureCount(input, Assimp.aiTextureType_BASE_COLOR);
if(textureCount > 0){
//for the time being, only load the first diffuse
int textureIndex = 0;
int retCode = Assimp.aiGetMaterialTexture(input, Assimp.aiTextureType_BASE_COLOR, textureIndex, aiPathString, (IntBuffer)null, null, null, null, null, null);
if(retCode != Assimp.aiReturn_SUCCESS){
throw new Error("Failed to read diffuse! " + textureCount + " " + Assimp.aiGetErrorString());
}
String texturePath = aiPathString.dataString();
if(texturePath == null || texturePath.length() <= 0){
throw new Error("Texture path is empty " + texturePath);
}
if(texturePath.length() == 2 && texturePath.startsWith("*")){
//older versions of Assimp require you to read the INDEX of the texture from the material, then look up that texture in the scene itself
//format looks like "*<index>" ie "*0"
int indexInLoadedTexturePaths = Integer.parseInt(texturePath.substring(1));
if(indexInLoadedTexturePaths >= texPaths.length){
throw new Error("Index discovered is outside the array's length " + indexInLoadedTexturePaths + " " + texPaths.length);
}
String resolved = Material.resolveTexturePath(path, texPaths[indexInLoadedTexturePaths]);
if(resolved != null && resolved.length() > 0){
rVal.usesFetch = true;
rVal.diffuse = resolved;
Globals.assetManager.addTexturePathtoQueue(rVal.diffuse);
foundDiffuse = true;
}
} else {
String resolved = Material.resolveTexturePath(path, texturePath);
if(resolved != null && resolved.length() > 0){
rVal.usesFetch = true;
rVal.diffuse = resolved;
Globals.assetManager.addTexturePathtoQueue(rVal.diffuse);
foundDiffuse = true;
}
}
}
}
//free mem
aiPathString.free();
return rVal;
} }
/** /**
@ -259,7 +316,7 @@ public class Material {
* Gets the path for the diffuse of the material * Gets the path for the diffuse of the material
* @return The path for the diffuse texture * @return The path for the diffuse texture
*/ */
public String getDiffuse(){ public Texture getDiffuse(){
return diffuse; return diffuse;
} }
@ -267,7 +324,7 @@ public class Material {
* Gets the path for the specular of the material * Gets the path for the specular of the material
* @return The path for the specular texture * @return The path for the specular texture
*/ */
public String getSpecular(){ public Texture getSpecular(){
return specular; return specular;
} }
@ -275,7 +332,7 @@ public class Material {
* Sets the diffuse texture * Sets the diffuse texture
* @param t The texture path * @param t The texture path
*/ */
public void setDiffuse(String t){ public void setDiffuse(Texture t){
diffuse = t; diffuse = t;
} }
@ -283,7 +340,7 @@ public class Material {
* Sets the specular texture path * Sets the specular texture path
* @param t The specular texture path * @param t The specular texture path
*/ */
public void setSpecular(String t){ public void setSpecular(Texture t){
specular = t; specular = t;
} }
@ -302,24 +359,6 @@ public class Material {
public Vector3f getAlbedo(){ public Vector3f getAlbedo(){
return this.albedo; return this.albedo;
} }
/**
* Sets the texture pointer
* @param pointer The texture pointer
*/
public void setTexturePointer(int pointer){
texturePointer = pointer;
usesFetch = false;
}
/**
* Sets the in-material pointer for the normal
* @param pointer the normal texture
*/
public void setNormalTexturePointer(int pointer){
normalPointer = pointer;
usesFetch = false;
}
/** /**
* Reads a float property from a material * Reads a float property from a material
@ -347,28 +386,24 @@ public class Material {
* Applies the material * Applies the material
*/ */
public void applyMaterial(OpenGLState openGLState){ public void applyMaterial(OpenGLState openGLState){
if(this.existingPath != null){
Texture tex = Globals.assetManager.fetchTexture(existingPath);
if(tex != null){
this.diffuse = tex;
this.existingPath = null;
}
}
//Controls whether the texturePointer should be resolved by looking up the diffuse in asset manager or using the texture pointer already set in this material //Controls whether the texturePointer should be resolved by looking up the diffuse in asset manager or using the texture pointer already set in this material
if(usesFetch){ if(diffuse != null){
if(diffuse != null){ diffuse.bind(openGLState,0);
Texture diffuseTexture = Globals.assetManager.fetchTexture(diffuse); Globals.renderingEngine.checkError();
if(diffuseTexture != null){ openGLState.getActiveShader().setUniform(openGLState, "material.diffuse", 0);
diffuseTexture.bind(openGLState,0); Globals.renderingEngine.checkError();
Globals.renderingEngine.checkError(); }
openGLState.getActiveShader().setUniform(openGLState, "material.diffuse", 0); if(specular != null){
Globals.renderingEngine.checkError(); specular.bind(openGLState,1);
} Globals.renderingEngine.checkError();
} openGLState.getActiveShader().setUniform(openGLState, "material.specular", 1);
if(specular != null){
Texture specularTexture = Globals.assetManager.fetchTexture(specular);
if(specularTexture != null){
specularTexture.bind(openGLState,1);
Globals.renderingEngine.checkError();
openGLState.getActiveShader().setUniform(openGLState, "material.specular", 1);
Globals.renderingEngine.checkError();
}
}
} else {
openGLState.glBindTextureUnit(GL45.GL_TEXTURE0, texturePointer, GL45.GL_TEXTURE_2D);
Globals.renderingEngine.checkError(); Globals.renderingEngine.checkError();
} }
//send physical properties //send physical properties
@ -379,31 +414,16 @@ public class Material {
openGLState.getActiveShader().setUniform(openGLState, "material.albedo", this.albedo); openGLState.getActiveShader().setUniform(openGLState, "material.albedo", this.albedo);
Globals.renderingEngine.checkError(); Globals.renderingEngine.checkError();
} }
/**
* Checks if this material has transparency
* @return true if there is transparency, false otherwise
*/
public boolean isTransparent(){
boolean rVal = false;
Texture diffuseTexture = Globals.assetManager.fetchTexture(diffuse);
if(diffuseTexture != null && diffuseTexture.isTransparent()){
rVal = true;
}
Texture specularTexture = Globals.assetManager.fetchTexture(specular);
if(specularTexture != null && specularTexture.isTransparent()){
rVal = true;
}
return rVal;
}
/** /**
* Frees the material * Frees the material
*/ */
public void free(){ public void free(){
GL45.glDeleteTextures(new int[]{ if(this.diffuse != null){
this.texturePointer, GL45.glDeleteTextures(this.diffuse.getTexturePointer());
this.normalPointer, }
}); if(this.specular != null){
GL45.glDeleteTextures(this.specular.getTexturePointer());
}
} }
} }

View File

@ -180,11 +180,7 @@ public class Model {
//conditionally add material //conditionally add material
int materialIndex = aiMesh.mMaterialIndex(); int materialIndex = aiMesh.mMaterialIndex();
if(materialIndex < rVal.materials.size()){ if(materialIndex < rVal.materials.size()){
Material mat = rVal.materials.get(materialIndex); currentMesh.setMaterial(rVal.materials.get(materialIndex));
//only assign if the diffuse is actually set (ie we've actually loaded it properly)
if(mat.getDiffuse() != null){
currentMesh.setMaterial(rVal.materials.get(materialIndex));
}
} }
} }

View File

@ -69,7 +69,7 @@ public class DebugBonesPipeline implements RenderPipeline {
// //
Actor targetActor = EntityUtils.getActor(targetEntity); Actor targetActor = EntityUtils.getActor(targetEntity);
Model boneModel = Globals.assetManager.fetchModel(AssetDataStrings.UNITCYLINDER); Model boneModel = Globals.assetManager.fetchModel(AssetDataStrings.UNITCYLINDER);
boneModel.getMaterials().get(0).setDiffuse(AssetDataStrings.TEXTURE_DEFAULT); boneModel.getMaterials().get(0).setDiffuse(Globals.assetManager.fetchTexture(AssetDataStrings.TEXTURE_DEFAULT));
for(Bone bone : targetActor.getBoneValues()){ for(Bone bone : targetActor.getBoneValues()){
Vector3d bonePos = MathBones.getBoneWorldPosition(targetEntity, bone.boneID); Vector3d bonePos = MathBones.getBoneWorldPosition(targetEntity, bone.boneID);
Quaterniond boneRot = MathBones.getBoneWorldRotation(targetEntity, bone.boneID); Quaterniond boneRot = MathBones.getBoneWorldRotation(targetEntity, bone.boneID);

View File

@ -19,7 +19,7 @@ import electrosphere.renderer.ui.elementtypes.Element;
public class UIUtils { public class UIUtils {
//the material that links the texture to draw //the material that links the texture to draw
static Material customMat = new Material("Textures/ui/uiOutline1.png"); static Material customMat = Material.createExisting("Textures/ui/uiOutline1.png");
/** /**
* Renders the outline of the provided element and all child elements of the rootEl * Renders the outline of the provided element and all child elements of the rootEl

View File

@ -107,7 +107,7 @@ public class BufferedStandardDrawableContainerElement extends StandardDrawableC
planeModel.pushUniformToMesh("plane", "mDimension", boxDimensions); planeModel.pushUniformToMesh("plane", "mDimension", boxDimensions);
planeModel.pushUniformToMesh("plane", "tPosition", texPosition); planeModel.pushUniformToMesh("plane", "tPosition", texPosition);
planeModel.pushUniformToMesh("plane", "tDimension", texScale); planeModel.pushUniformToMesh("plane", "tDimension", texScale);
elementMat.setTexturePointer(windowFrame.getTexturePointer()); elementMat.setDiffuse(windowFrame);
planeModel.getMeshes().get(0).setMaterial(elementMat); planeModel.getMeshes().get(0).setMaterial(elementMat);
planeModel.drawUI(); planeModel.drawUI();
} }
@ -117,7 +117,7 @@ public class BufferedStandardDrawableContainerElement extends StandardDrawableC
planeModel.pushUniformToMesh("plane", "mDimension", boxDimensions); planeModel.pushUniformToMesh("plane", "mDimension", boxDimensions);
planeModel.pushUniformToMesh("plane", "tPosition", texPosition); planeModel.pushUniformToMesh("plane", "tPosition", texPosition);
planeModel.pushUniformToMesh("plane", "tDimension", texScale); planeModel.pushUniformToMesh("plane", "tDimension", texScale);
elementMat.setTexturePointer(elementBuffer.getTexture().getTexturePointer()); elementMat.setDiffuse(elementBuffer.getTexture());
planeModel.getMeshes().get(0).setMaterial(elementMat); planeModel.getMeshes().get(0).setMaterial(elementMat);
planeModel.drawUI(); planeModel.drawUI();
} else { } else {
@ -148,7 +148,7 @@ public class BufferedStandardDrawableContainerElement extends StandardDrawableC
} catch(Exception e){ } catch(Exception e){
LoggerInterface.loggerRenderer.ERROR(e); LoggerInterface.loggerRenderer.ERROR(e);
} }
elementMat.setTexturePointer(elementBuffer.getTexture().getTexturePointer()); elementMat.setDiffuse(elementBuffer.getTexture());
} }

View File

@ -76,10 +76,10 @@ public class ImagePanel extends StandardElement implements DrawableElement, Drag
texture = Globals.assetManager.fetchTexture(this.texturePath); texture = Globals.assetManager.fetchTexture(this.texturePath);
} }
if(texture != null){ if(texture != null){
customMat.setTexturePointer(texture.getTexturePointer()); customMat.setDiffuse(texture);
hasLoadedTexture = true; hasLoadedTexture = true;
} else if(Globals.assetManager.fetchTexture(AssetDataStrings.TEXTURE_BLACK) != null) { } else if(Globals.assetManager.fetchTexture(AssetDataStrings.TEXTURE_BLACK) != null) {
customMat.setTexturePointer(Globals.assetManager.fetchTexture(AssetDataStrings.TEXTURE_BLACK).getTexturePointer()); customMat.setDiffuse(Globals.assetManager.fetchTexture(AssetDataStrings.TEXTURE_BLACK));
} }
} }
@ -106,7 +106,7 @@ public class ImagePanel extends StandardElement implements DrawableElement, Drag
* @param texture The texture to use * @param texture The texture to use
*/ */
public void setTexture(Texture texture){ public void setTexture(Texture texture){
customMat.setTexturePointer(texture.getTexturePointer()); customMat.setDiffuse(texture);
} }
/** /**
@ -137,7 +137,7 @@ public class ImagePanel extends StandardElement implements DrawableElement, Drag
Model planeModel = Globals.assetManager.fetchModel(imagePanelModelPath); Model planeModel = Globals.assetManager.fetchModel(imagePanelModelPath);
if(texture != null){ if(texture != null){
customMat.setTexturePointer(texture.getTexturePointer()); customMat.setDiffuse(texture);
} else if(this.texturePath != null){ } else if(this.texturePath != null){
texture = Globals.assetManager.fetchTexture(this.texturePath); texture = Globals.assetManager.fetchTexture(this.texturePath);
} }

View File

@ -186,7 +186,7 @@ public class ScrollableContainer extends BufferedStandardDrawableContainerElemen
planeModel.pushUniformToMesh("plane", "tPosition", texPosition); planeModel.pushUniformToMesh("plane", "tPosition", texPosition);
planeModel.pushUniformToMesh("plane", "tDimension", texScale); planeModel.pushUniformToMesh("plane", "tDimension", texScale);
planeModel.pushUniformToMesh(planeModel.getMeshes().get(0).getMeshName(), "color", color); planeModel.pushUniformToMesh(planeModel.getMeshes().get(0).getMeshName(), "color", color);
elementMat.setTexturePointer(elementBuffer.getTexture().getTexturePointer()); elementMat.setDiffuse(elementBuffer.getTexture());
planeModel.getMeshes().get(0).setMaterial(elementMat); planeModel.getMeshes().get(0).setMaterial(elementMat);
planeModel.drawUI(); planeModel.drawUI();
} else { } else {

View File

@ -87,9 +87,7 @@ public class Slider extends StandardDrawableElement implements ClickableElement,
private Slider(){ private Slider(){
super(); super();
if(mat == null){ if(mat == null){
mat = new Material(); mat = Material.create("Textures/ui/square.png");
mat.setDiffuse("Textures/ui/square.png");
mat.setSpecular("Textures/ui/square.png");
} }
setWidth(DEFAULT_WIDTH); setWidth(DEFAULT_WIDTH);
setHeight(DEFAULT_HEIGHT); setHeight(DEFAULT_HEIGHT);
@ -99,9 +97,7 @@ public class Slider extends StandardDrawableElement implements ClickableElement,
public Slider(int positionX, int positionY, int width, int height, Vector4f colorBackground, Vector4f colorForeground){ public Slider(int positionX, int positionY, int width, int height, Vector4f colorBackground, Vector4f colorForeground){
super(); super();
if(mat == null){ if(mat == null){
mat = new Material(); mat = Material.create("Textures/ui/square.png");
mat.setDiffuse("Textures/ui/square.png");
mat.setSpecular("Textures/ui/square.png");
} }
setPositionX(positionX); setPositionX(positionX);
setPositionY(positionY); setPositionY(positionY);

View File

@ -96,14 +96,10 @@ public class ToggleInput extends StandardDrawableElement implements ClickableEle
private ToggleInput(){ private ToggleInput(){
//material work //material work
if(circleMat == null){ if(circleMat == null){
circleMat = new Material(); circleMat = Material.create("Textures/ui/circle.png");
circleMat.setDiffuse("Textures/ui/circle.png");
circleMat.setSpecular("Textures/ui/circle.png");
} }
if(barMat == null){ if(barMat == null){
barMat = new Material(); barMat = Material.create("Textures/ui/square.png");
barMat.setDiffuse("Textures/ui/square.png");
barMat.setSpecular("Textures/ui/square.png");
} }
this.setWidth(TOGGLE_PIXEL_WIDTH_DEFAULT); this.setWidth(TOGGLE_PIXEL_WIDTH_DEFAULT);

View File

@ -128,7 +128,7 @@ public class Tooltip extends StandardDrawableContainerElement {
planeModel.pushUniformToMesh("plane", "tPosition", texPosition); planeModel.pushUniformToMesh("plane", "tPosition", texPosition);
planeModel.pushUniformToMesh("plane", "tDimension", texScale); planeModel.pushUniformToMesh("plane", "tDimension", texScale);
planeModel.pushUniformToMesh(planeModel.getMeshes().get(0).getMeshName(), "color", backgroundColor); planeModel.pushUniformToMesh(planeModel.getMeshes().get(0).getMeshName(), "color", backgroundColor);
customMat.setTexturePointer(windowFrame.getTexturePointer()); customMat.setDiffuse(windowFrame);
planeModel.getMeshes().get(0).setMaterial(customMat); planeModel.getMeshes().get(0).setMaterial(customMat);
planeModel.drawUI(); planeModel.drawUI();
} }

View File

@ -263,7 +263,7 @@ public class Window implements DrawableElement, ContainerElement, NavigableEleme
planeModel.pushUniformToMesh("plane", "tPosition", texPosition); planeModel.pushUniformToMesh("plane", "tPosition", texPosition);
planeModel.pushUniformToMesh("plane", "tDimension", texScale); planeModel.pushUniformToMesh("plane", "tDimension", texScale);
planeModel.pushUniformToMesh(planeModel.getMeshes().get(0).getMeshName(), "color", new Vector4f(1.0f)); planeModel.pushUniformToMesh(planeModel.getMeshes().get(0).getMeshName(), "color", new Vector4f(1.0f));
customMat.setTexturePointer(widgetBuffer.getTexture().getTexturePointer()); customMat.setDiffuse(widgetBuffer.getTexture());
planeModel.getMeshes().get(0).setMaterial(customMat); planeModel.getMeshes().get(0).setMaterial(customMat);
planeModel.drawUI(); planeModel.drawUI();
} }
@ -315,7 +315,7 @@ public class Window implements DrawableElement, ContainerElement, NavigableEleme
} catch(Exception e){ } catch(Exception e){
LoggerInterface.loggerRenderer.ERROR(e); LoggerInterface.loggerRenderer.ERROR(e);
} }
customMat.setTexturePointer(widgetBuffer.getTexture().getTexturePointer()); customMat.setDiffuse(widgetBuffer.getTexture());
} }
@Override @Override

View File

@ -186,7 +186,7 @@ public class FontUtils {
//create material with new font image //create material with new font image
Material uiMat = new Material(); Material uiMat = new Material();
Globals.assetManager.queuedAsset(QueuedTexture.createFromImage(image, (Texture tex) -> { Globals.assetManager.queuedAsset(QueuedTexture.createFromImage(image, (Texture tex) -> {
uiMat.setTexturePointer(tex.getTexturePointer()); uiMat.setDiffuse(tex);
})); }));
@ -271,7 +271,7 @@ public class FontUtils {
//create the bitmap image off of the raw texture data //create the bitmap image off of the raw texture data
Texture texture = Texture.createBitmap(openGLState, bakedTextureData, TTF_BITMAP_WIDTH, TTF_BITMAP_HEIGHT); Texture texture = Texture.createBitmap(openGLState, bakedTextureData, TTF_BITMAP_WIDTH, TTF_BITMAP_HEIGHT);
uiMat.setTexturePointer(texture.getTexturePointer()); uiMat.setDiffuse(texture);
//parse the glyphs //parse the glyphs
try(MemoryStack stack = MemoryStack.stackPush()){ try(MemoryStack stack = MemoryStack.stackPush()){

View File

@ -69,7 +69,7 @@ public class UIFrameUtils {
//render background of window //render background of window
if(planeModel != null && windowFrame != null){ if(planeModel != null && windowFrame != null){
//set materials + uniforms //set materials + uniforms
customMat.setTexturePointer(windowFrame.getTexturePointer()); customMat.setDiffuse(windowFrame);
planeModel.getMeshes().get(0).setMaterial(customMat); planeModel.getMeshes().get(0).setMaterial(customMat);
planeModel.pushUniformToMesh(planeModel.getMeshes().get(0).getMeshName(), "color", color); planeModel.pushUniformToMesh(planeModel.getMeshes().get(0).getMeshName(), "color", color);