com.badlogic.gdx.graphics.g3d.Renderable Java Examples

The following examples show how to use com.badlogic.gdx.graphics.g3d.Renderable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GameRenderer.java    From GdxDemo3D with Apache License 2.0 6 votes vote down vote up
public GameRenderer(Viewport viewport, Camera camera, GameEngine engine) {
	this.viewport = viewport;
	this.camera = camera;
	this.engine = engine;

	shapeRenderer = new MyShapeRenderer();
	shapeRenderer.setAutoShapeType(true);

	spriteBatch = new SpriteBatch();
	font = new BitmapFont();
	font.setColor(Color.WHITE);
	font.setUseIntegerPositions(false);
	font.getData().setScale(0.01f);
	shadowBatch = new ModelBatch(new DepthShaderProvider());

	ShaderProgram.pedantic = false;
	final String vertUber = Gdx.files.internal("shaders/uber.vert").readString();
	final String fragUber = Gdx.files.internal("shaders/uber.frag").readString();
	modelBatch = new ModelBatch(new DefaultShaderProvider(vertUber, fragUber) {
		@Override
		protected Shader createShader(final Renderable renderable) {
			return new UberShader(renderable, config);
		}
	});
}
 
Example #2
Source File: PBRShader.java    From gdx-gltf with Apache License 2.0 6 votes vote down vote up
@Override
public boolean canRender(Renderable renderable) {
	// TODO properly determine if current shader can render this renderable.
	
	// compare texture coordinates mapping
	long textureCoordinateMapMask = getTextureCoordinateMapMask(renderable.material);
	if(textureCoordinateMapMask != this.textureCoordinateMapMask){
		return false;
	}
	
	// compare morph targets
	if(this.morphTargetsMask != computeMorphTargetsMask(renderable)) return false;
	
	// compare vertex colors count
	if(this.vertexColorLayers != computeVertexColorLayers(renderable)) return false;
	
	return super.canRender(renderable);
}
 
Example #3
Source File: SkyboxShader.java    From Mundus with Apache License 2.0 6 votes vote down vote up
@Override
public void render(Renderable renderable) {

    // texture uniform
    CubemapAttribute cubemapAttribute = ((CubemapAttribute) (renderable.material
            .get(CubemapAttribute.EnvironmentMap)));
    if (cubemapAttribute != null) {
        set(UNIFORM_TEXTURE, cubemapAttribute.textureDescription);
    }

    // Fog
    Fog fog = ((MundusEnvironment) renderable.environment).getFog();
    if (fog == null) {
        set(UNIFORM_FOG, 0);
    } else {
        set(UNIFORM_FOG, 1);
        set(UNIFORM_FOG_COLOR, fog.color);
    }

    renderable.meshPart.render(program);
}
 
Example #4
Source File: TerrainShader.java    From Mundus with Apache License 2.0 6 votes vote down vote up
@Override
public void render(Renderable renderable) {
    final MundusEnvironment env = (MundusEnvironment) renderable.environment;

    setLights(env);
    setTerrainSplatTextures(renderable);
    set(UNIFORM_TRANS_MATRIX, renderable.worldTransform);

    // Fog
    final Fog fog = env.getFog();
    if (fog == null) {
        set(UNIFORM_FOG_DENSITY, 0f);
        set(UNIFORM_FOG_GRADIENT, 0f);
    } else {
        set(UNIFORM_FOG_DENSITY, fog.density);
        set(UNIFORM_FOG_GRADIENT, fog.gradient);
        set(UNIFORM_FOG_COLOR, fog.color);
    }

    // bind attributes, bind mesh & render; then unbinds everything
    renderable.meshPart.render(program);
}
 
Example #5
Source File: PBRShader.java    From gdx-gltf with Apache License 2.0 6 votes vote down vote up
@Override
protected void bindLights(Renderable renderable, Attributes attributes) {
	
	// XXX update color (to apply intensity) before default binding
	DirectionalLightsAttribute dla = attributes.get(DirectionalLightsAttribute.class, DirectionalLightsAttribute.Type);
	if(dla != null){
		for(DirectionalLight light : dla.lights){
			if(light instanceof DirectionalLightEx){
				((DirectionalLightEx) light).updateColor();
			}
		}
	}
	
	super.bindLights(renderable, attributes);
		
	// XXX
	ColorAttribute ambiantLight = attributes.get(ColorAttribute.class, ColorAttribute.AmbientLight);
	if(ambiantLight != null){
		program.setUniformf(u_ambientLight, ambiantLight.color.r, ambiantLight.color.g, ambiantLight.color.b);
	}
}
 
Example #6
Source File: UberShader.java    From GdxDemo3D with Apache License 2.0 6 votes vote down vote up
public UberShader(Renderable renderable, Config config) {
	super(renderable, config);

	String prefix = DefaultShader.createPrefix(renderable, config);

	if (renderable.material.has(VertexColorTextureBlend.Red)) {
		prefix += "#define redVertexColorBlendFlag\n";
	}
	if (renderable.material.has(VertexColorTextureBlend.Green)) {
		prefix += "#define greenVertexColorBlendFlag\n";
	}
	if (renderable.material.has(VertexColorTextureBlend.Blue)) {
		prefix += "#define blueVertexColorBlendFlag\n";
	}

	program = new ShaderProgram(prefix + config.vertexShader, prefix + config.fragmentShader);
	if (!program.isCompiled()) {
		Gdx.app.debug(TAG, program.getLog());
	}
}
 
Example #7
Source File: BreakingRenderer.java    From Cubes with MIT License 6 votes vote down vote up
public static Renderable draw() {
  Player player = Cubes.getClient().player;
  ItemTool.MiningTarget currentlyMining = player.getCurrentlyMining();
  if (currentlyMining == null) return null;
  BlockReference position = currentlyMining.target;
  float percent = currentlyMining.time / currentlyMining.totalTime;
  percent -= 1f / (1f + num);
  if (percent <= 0f) return null;
  int n = (int) Math.floor(percent * num);

  float f = 1f / 128f;
  renderable.meshPart.offset = n * (6 * 6);
  renderable.worldTransform.idt();
  renderable.worldTransform.translate(position.blockX - f, position.blockY - f, position.blockZ - f);
  renderable.worldTransform.scl(1f + f + f);

  return renderable;
}
 
Example #8
Source File: CubesShaderProvider.java    From Cubes with MIT License 6 votes vote down vote up
@Override
public void render(Renderable renderable) {
  if (renderable instanceof CubesRenderable && ((CubesRenderable) renderable).lightOverride != -1) {
    program.setUniformf(u_lightoverride, ((CubesRenderable) renderable).lightOverride);
    lightoverride = true;
  } else if (lightoverride) {
    program.setUniformf(u_lightoverride, -1f);
    lightoverride = false;
  }
  try {
    super.render(renderable);
  } catch (Exception e) {
    if (renderable instanceof CubesRenderable && ((CubesRenderable) renderable).name != null && !((CubesRenderable) renderable).name.isEmpty()) {
      throw new CubesException("Error during rendering '" + ((CubesRenderable) renderable).name + "' renderable", e);
    } else {
      throw new CubesException("Error during rendering renderable", e);
    }
  }
}
 
Example #9
Source File: PBRShaderProvider.java    From gdx-gltf with Apache License 2.0 6 votes vote down vote up
public static String morphTargetsPrefix(Renderable renderable){
	String prefix = "";
	// TODO optimize double loop
	for(VertexAttribute att : renderable.meshPart.mesh.getVertexAttributes()){
		for(int i=0 ; i<PBRCommon.MAX_MORPH_TARGETS ; i++){
			if(att.usage == PBRVertexAttributes.Usage.PositionTarget && att.unit == i){
				prefix += "#define " + "position" + i + "Flag\n";
			}else if(att.usage == PBRVertexAttributes.Usage.NormalTarget && att.unit == i){
				prefix += "#define " + "normal" + i + "Flag\n";
			}else if(att.usage == PBRVertexAttributes.Usage.TangentTarget && att.unit == i){
				prefix += "#define " + "tangent" + i + "Flag\n";
			}
		}
	}
	return prefix;
}
 
Example #10
Source File: PBRShader.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
public long computeMorphTargetsMask(Renderable renderable){
	int morphTargetsFlag = 0;
	VertexAttributes vertexAttributes = renderable.meshPart.mesh.getVertexAttributes();
	final int n = vertexAttributes.size();
	for (int i = 0; i < n; i++) {
		final VertexAttribute attr = vertexAttributes.get(i);
		if (attr.usage == PBRVertexAttributes.Usage.PositionTarget) morphTargetsFlag |= (1 << attr.unit);
		if (attr.usage == PBRVertexAttributes.Usage.NormalTarget) morphTargetsFlag |= (1 << (attr.unit + 8));
		if (attr.usage == PBRVertexAttributes.Usage.TangentTarget) morphTargetsFlag |= (1 << (attr.unit + 16));
	}
	return morphTargetsFlag;
}
 
Example #11
Source File: PBRShader.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
private int computeVertexColorLayers(Renderable renderable) {
	int num = 0;
	VertexAttributes vertexAttributes = renderable.meshPart.mesh.getVertexAttributes();
	final int n = vertexAttributes.size();
	for (int i = 0; i < n; i++) {
		final VertexAttribute attr = vertexAttributes.get(i);
		if (attr.usage == VertexAttributes.Usage.ColorUnpacked) num++;
	}
	return num;
}
 
Example #12
Source File: PBRShader.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
@Override
public void set (BaseShader shader, int inputID, Renderable renderable, Attributes combinedAttributes) {
	ColorAttribute attribute = combinedAttributes.get(ColorAttribute.class, PBRColorAttribute.BaseColorFactor);
	Color color = attribute == null ? Color.WHITE : attribute.color;
	shader.set(inputID, color);
}
 
Example #13
Source File: PBRDepthShaderProvider.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
protected String morphTargetsPrefix(Renderable renderable){
	String prefix = "";
	for(VertexAttribute att : renderable.meshPart.mesh.getVertexAttributes()){
		for(int i=0 ; i<PBRCommon.MAX_MORPH_TARGETS ; i++){
			if(att.usage == PBRVertexAttributes.Usage.PositionTarget && att.unit == i){
				prefix += "#define " + "position" + i + "Flag\n";
			}
		}
	}
	return prefix;
}
 
Example #14
Source File: PBRShader.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
@Override
public void set (BaseShader shader, int inputID, Renderable renderable, Attributes combinedAttributes) {
	PBRFloatAttribute metallicAttribute = combinedAttributes.get(PBRFloatAttribute.class, PBRFloatAttribute.Metallic);
	PBRFloatAttribute roughnessAttribute = combinedAttributes.get(PBRFloatAttribute.class, PBRFloatAttribute.Roughness);
	float metallic = metallicAttribute == null ? 1f : metallicAttribute.value;
	float roughness = roughnessAttribute == null ? 1f : roughnessAttribute.value;
	shader.set(inputID, v2.set(metallic, roughness));
}
 
Example #15
Source File: PBRCommon.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
public static void checkVertexAttributes(Renderable renderable){
	final int numVertexAttributes = renderable.meshPart.mesh.getVertexAttributes().size();
	final int maxVertexAttribs = getCapability(GL20.GL_MAX_VERTEX_ATTRIBS);
	if(numVertexAttributes > maxVertexAttribs){
		throw new GdxRuntimeException("too many vertex attributes : " + numVertexAttributes + " > " + maxVertexAttribs);
	}
}
 
Example #16
Source File: PBRShader.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
@Override
public void set (BaseShader shader, int inputID, Renderable renderable, Attributes combinedAttributes) {
	PBRTextureAttribute attribute = combinedAttributes.get(PBRTextureAttribute.class, PBRTextureAttribute.BRDFLUTTexture);
	if(attribute != null){
		final int unit = shader.context.textureBinder.bind(attribute.textureDescription);
		shader.set(inputID, unit);
	}
}
 
Example #17
Source File: SceneRenderableSorter.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(Renderable o1, Renderable o2) 
{
	// original (blending and hints)
	final boolean b1 = o1.material.has(BlendingAttribute.Type) && ((BlendingAttribute)o1.material.get(BlendingAttribute.Type)).blended;
	final boolean b2 = o2.material.has(BlendingAttribute.Type) && ((BlendingAttribute)o2.material.get(BlendingAttribute.Type)).blended;

	final Hints h1 = o1.userData instanceof Hints ? (Hints)o1.userData : null;
	final Hints h2 = o2.userData instanceof Hints ? (Hints)o2.userData : null;
	
	if(h1 != h2){
		if(h1 == Hints.OPAQUE_LAST){
			return b2 ? -1 : 1;
		}
		if(h2 == Hints.OPAQUE_LAST){
			return b1 ? 1 : -1;
		}
	}
	
	// simple switch limitation by identifying same context.
	
	int shaderCompare = compareIdentity(o1.shader, o2.shader);
	if(shaderCompare != 0) return shaderCompare;
	
	int envCompare = compareIdentityNullable(o1.environment, o2.environment);
	if(envCompare != 0) return envCompare;
	
	int materialCompare = compareIdentity(o1.material, o2.material);
	if(materialCompare != 0) return materialCompare;
	
	int meshCompare = compareIdentity(o1.meshPart.mesh, o2.meshPart.mesh);
	if(meshCompare != 0) return meshCompare;
	
	// classic with distance
	getTranslation(o1.worldTransform, o1.meshPart.center, tmpV1);
	getTranslation(o2.worldTransform, o2.meshPart.center, tmpV2);
	final float dst = (int)(camera.position.dst2(tmpV1)) - (int)(camera.position.dst2(tmpV2));
	final int result = dst < 0 ? -1 : (dst > 0 ? 1 : 0);
	return b1 ? -result : result;
}
 
Example #18
Source File: SceneSkybox.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
public SceneSkybox(Cubemap cubemap){
	super();
	
	// create shader provider
	Config shaderConfig = new Config();
	String basePathName = "net/mgsx/gltf/shaders/skybox";
	shaderConfig.vertexShader = Gdx.files.classpath(basePathName + ".vs.glsl").readString();
	shaderConfig.fragmentShader = Gdx.files.classpath(basePathName + ".fs.glsl").readString();
	shaderProvider =  new DefaultShaderProvider(shaderConfig);
	
	// create box
	float boxScale = (float)(1.0 / Math.sqrt(2.0));
	boxModel = new ModelBuilder().createBox(boxScale, boxScale, boxScale, new Material(), VertexAttributes.Usage.Position);
	box = boxModel.nodes.first().parts.first().setRenderable(new Renderable());
	
	// assign environment
	Environment env = new Environment();
	env.set(new CubemapAttribute(CubemapAttribute.EnvironmentMap, cubemap));
	env.set(new ColorAttribute(ColorAttribute.AmbientLight, Color.WHITE));
	box.environment = env;
	
	// set hint to render last but before transparent ones
	box.userData = SceneRenderableSorter.Hints.OPAQUE_LAST;
	
	// set material options : preserve background depth
	box.material = new Material(ColorAttribute.createDiffuse(Color.WHITE));
	box.material.set(new DepthTestAttribute(false));
	
	
	// assign shader
	box.shader = shaderProvider.getShader(box);
}
 
Example #19
Source File: PBRDepthShaderProvider.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
@Override
protected Shader createShader(Renderable renderable) {
	
	// TODO only count used attributes, depth shader only require a few of them.
	PBRCommon.checkVertexAttributes(renderable);
	
	return new PBRDepthShader(renderable, config, DepthShader.createPrefix(renderable, config) + morphTargetsPrefix(renderable));
}
 
Example #20
Source File: NodePartPlus.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
public Renderable setRenderable (final Renderable out) {
	out.material = material;
	out.meshPart.set(meshPart);
	out.bones = bones;
	out.userData = morphTargets;
	return out;
}
 
Example #21
Source File: ModelInstanceHack.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
@Override
public Renderable getRenderable (final Renderable out, final Node node, final NodePart nodePart) {
	super.getRenderable(out, node, nodePart);
	if(nodePart instanceof NodePartPlus){
		out.userData = ((NodePartPlus) nodePart).morphTargets;
	}
	return out;
}
 
Example #22
Source File: TerrainShader.java    From Mundus with Apache License 2.0 5 votes vote down vote up
protected void setTerrainSplatTextures(Renderable renderable) {
    final TerrainTextureAttribute splatAttrib = (TerrainTextureAttribute) renderable.material
            .get(TerrainTextureAttribute.ATTRIBUTE_SPLAT0);
    final TerrainTexture terrainTexture = splatAttrib.terrainTexture;

    // base texture
    SplatTexture st = terrainTexture.getTexture(SplatTexture.Channel.BASE);
    if (st != null) {
        set(UNIFORM_TEXTURE_BASE, st.texture.getTexture());
        set(UNIFORM_TEXTURE_HAS_DIFFUSE, 1);
    } else {
        set(UNIFORM_TEXTURE_HAS_DIFFUSE, 0);
    }

    // splat textures
    if (terrainTexture.getSplatmap() != null) {
        set(UNIFORM_TEXTURE_HAS_SPLATMAP, 1);
        set(UNIFORM_TEXTURE_SPLAT, terrainTexture.getSplatmap().getTexture());
        st = terrainTexture.getTexture(SplatTexture.Channel.R);
        if (st != null) set(UNIFORM_TEXTURE_R, st.texture.getTexture());
        st = terrainTexture.getTexture(SplatTexture.Channel.G);
        if (st != null) set(UNIFORM_TEXTURE_G, st.texture.getTexture());
        st = terrainTexture.getTexture(SplatTexture.Channel.B);
        if (st != null) set(UNIFORM_TEXTURE_B, st.texture.getTexture());
        st = terrainTexture.getTexture(SplatTexture.Channel.A);
        if (st != null) set(UNIFORM_TEXTURE_A, st.texture.getTexture());
    } else {
        set(UNIFORM_TEXTURE_HAS_SPLATMAP, 0);
    }

    // set terrain world size
    terrainSize.x = terrainTexture.getTerrain().terrainWidth;
    terrainSize.y = terrainTexture.getTerrain().terrainDepth;
    set(UNIFORM_TERRAIN_SIZE, terrainSize);
}
 
Example #23
Source File: ModelShader.java    From Mundus with Apache License 2.0 5 votes vote down vote up
@Override
public void render(Renderable renderable) {
    final MundusEnvironment env = (MundusEnvironment) renderable.environment;

    setLights(env);
    set(UNIFORM_TRANS_MATRIX, renderable.worldTransform);

    // texture uniform
    TextureAttribute diffuseTexture = ((TextureAttribute) (renderable.material.get(TextureAttribute.Diffuse)));
    ColorAttribute diffuseColor = ((ColorAttribute) (renderable.material.get(ColorAttribute.Diffuse)));

    if (diffuseTexture != null) {
        set(UNIFORM_MATERIAL_DIFFUSE_TEXTURE, diffuseTexture.textureDescription.texture);
        set(UNIFORM_MATERIAL_DIFFUSE_USE_TEXTURE, 1);
    } else {
        set(UNIFORM_MATERIAL_DIFFUSE_COLOR, diffuseColor.color);
        set(UNIFORM_MATERIAL_DIFFUSE_USE_TEXTURE, 0);
    }

    // shininess
    float shininess = ((FloatAttribute)renderable.material.get(FloatAttribute.Shininess)).value;
    set(UNIFORM_MATERIAL_SHININESS, shininess);

    // Fog
    final Fog fog = env.getFog();
    if (fog == null) {
        set(UNIFORM_FOG_DENSITY, 0f);
        set(UNIFORM_FOG_GRADIENT, 0f);
    } else {
        set(UNIFORM_FOG_DENSITY, fog.density);
        set(UNIFORM_FOG_GRADIENT, fog.gradient);
        set(UNIFORM_FOG_COLOR, fog.color);
    }

    // bind attributes, bind mesh & render; then unbinds everything
    renderable.meshPart.render(program);
}
 
Example #24
Source File: EditorTerrainShader.java    From Mundus with Apache License 2.0 5 votes vote down vote up
@Override
public void render(Renderable renderable) {
    final MundusEnvironment env = (MundusEnvironment) renderable.environment;

    setLights(env);
    setTerrainSplatTextures(renderable);
    set(UNIFORM_TRANS_MATRIX, renderable.worldTransform);

    // Fog
    final Fog fog = env.getFog();
    if (fog == null) {
        set(UNIFORM_FOG_DENSITY, 0f);
        set(UNIFORM_FOG_GRADIENT, 0f);
    } else {
        set(UNIFORM_FOG_DENSITY, fog.density);
        set(UNIFORM_FOG_GRADIENT, fog.gradient);
        set(UNIFORM_FOG_COLOR, fog.color);
    }

    // mouse picking
    if(pickerActive) {
        set(UNIFORM_MOUSE_ACTIVE, 1);
        set(UNIFORM_PICKER_POS, pickerPosition);
        set(UNIFORM_PICKER_RADIUS, pickerRadius);
    } else {
        set(UNIFORM_MOUSE_ACTIVE, 0);
    }

    // bind attributes, bind mesh & render; then unbinds everything
    renderable.meshPart.render(program);
}
 
Example #25
Source File: PickerShader.java    From Mundus with Apache License 2.0 5 votes vote down vote up
@Override
public void render(Renderable renderable) {
    set(UNIFORM_TRANS_MATRIX, renderable.worldTransform);

    PickerIDAttribute goID = (PickerIDAttribute) renderable.material.get(PickerIDAttribute.Type);
    if (goID != null) {
        set(UNIFORM_COLOR, vec3.set(goID.r, goID.g, goID.b));
    }

    renderable.meshPart.render(program);
}
 
Example #26
Source File: RainRenderer.java    From Cubes with MIT License 5 votes vote down vote up
@Override
public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool) {
  Renderable renderable = pool.obtain();
  ((CubesRenderable) renderable).name = "RainMesh";
  renderable.material = Assets.blockItemSheet.getMaterial(); // not actually used
  renderable.meshPart.set(meshPart);
  if (SHADER == null) {
    SHADER = new RainShader(renderable);
    SHADER.init();
  }
  renderable.shader = SHADER;
  renderables.add(renderable);
}
 
Example #27
Source File: AreaRenderer.java    From Cubes with MIT License 5 votes vote down vote up
@Override
public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool) {
  if (area == null || meshs.size() == 0) return;
  renderedThisFrame++;
  for (AreaMesh mesh : meshs) {
    renderedMeshesThisFrame++;
    renderables.add(mesh.renderable);
  }
}
 
Example #28
Source File: CubesModelBatch.java    From Cubes with MIT License 5 votes vote down vote up
@Override
public void sort(final Camera camera, final Array<Renderable> renderables) {
  this.camera = camera;
  Performance.start(PerformanceTags.CLIENT_RENDER_WORLD_FLUSH_SORT);
  renderables.sort(this);
  Performance.stop(PerformanceTags.CLIENT_RENDER_WORLD_FLUSH_SORT);
}
 
Example #29
Source File: CubesModelBatch.java    From Cubes with MIT License 5 votes vote down vote up
@Override
protected void reset(Renderable object) {
  if (!(object instanceof CubesRenderable)) {
    Log.error("Resetting non-Cubes renderable! Flushing pool");
    flush();
  }
}
 
Example #30
Source File: Explosion.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void getRenderables (Array<Renderable> renderables, Pool<Renderable> pool) {
	super.getRenderables(renderables, pool);
	Renderable r = renderables.get(renderables.size - 1);
	r.meshPart.offset = 6 * (int)(15 * aliveTime / EXPLOSION_LIVE_TIME);
	r.meshPart.size = 6;
}