com.badlogic.gdx.graphics.g3d.environment.DirectionalLight Java Examples

The following examples show how to use com.badlogic.gdx.graphics.g3d.environment.DirectionalLight. 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: Renderer.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
public Renderer () {
	try {
		lights = new Environment();
		lights.add(new DirectionalLight().set(Color.WHITE, new Vector3(-1, -0.5f, 0).nor()));

		spriteBatch = new SpriteBatch();
		modelBatch = new ModelBatch();

		backgroundTexture = new Texture(Gdx.files.internal("data/planet.jpg"), Format.RGB565, true);
		backgroundTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);

		font = new BitmapFont(Gdx.files.internal("data/font10.fnt"), Gdx.files.internal("data/font10.png"), false);

		camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
	} catch (Exception ex) {
		ex.printStackTrace();
	}
}
 
Example #2
Source File: Stage3d.java    From Scene3d with Apache License 2.0 6 votes vote down vote up
public Stage3d (float width, float height, boolean keepAspectRatio) {
	this.width = width;
	this.height = height;

	root = new Group3d();
	root.setStage3d(this);

	modelBatch = new ModelBatch();

	camera =  new Camera3d();
	environment = new Environment();
	environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.9f, 0.9f, 0.9f, 1f));
	environment.add(new DirectionalLight().set(0.8f, 0f, 0f, -1f, -0.8f, -0.2f));

	setViewport(width, height, keepAspectRatio);
}
 
Example #3
Source File: SimpleRoom.java    From gdx-vr with Apache License 2.0 6 votes vote down vote up
@Override
public void create() {
	assets = new AssetManager();
	String model = "Bambo_House.g3db";
	assets.load(model, Model.class);
	assets.finishLoading();
	modelInstance = new ModelInstance(assets.get(model, Model.class), new Matrix4().setToScaling(0.6f, 0.6f, 0.6f));

	DefaultShader.Config config = new Config();
	config.defaultCullFace = GL20.GL_NONE;
	ShaderProvider shaderProvider = new DefaultShaderProvider(config);
	modelBatch = new ModelBatch(shaderProvider);

	ModelBuilder builder = new ModelBuilder();
	float groundSize = 1000f;
	ground = new ModelInstance(builder.createRect(-groundSize, 0, groundSize, groundSize, 0, groundSize, groundSize, 0, -groundSize, -groundSize, 0, -groundSize, 0,
			1, 0, new Material(), Usage.Position | Usage.Normal), new Matrix4().setToTranslation(0, -0.01f, 0));
	environment = new Environment();
	environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
	environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

	VirtualReality.renderer.listeners.add(this);
	// VirtualReality.head.setCyclops(true);
}
 
Example #4
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 #5
Source File: IBLBuilder.java    From gdx-gltf with Apache License 2.0 6 votes vote down vote up
public static IBLBuilder createOutdoor(DirectionalLight sun) {
	IBLBuilder ibl = new IBLBuilder();
	
	ibl.nearGroundColor.set(.5f, .45f, .4f, 1);
	ibl.farGroundColor.set(.3f, .25f, .2f, 1);
	ibl.nearSkyColor.set(.7f, .8f, 1f, 1);
	ibl.farSkyColor.set(.9f, .95f, 1f, 1);
	
	Light light = new Light();
	light.direction.set(sun.direction).nor();
	light.color.set(sun.color);
	light.exponent = 30f;
	ibl.lights.add(light);
	
	return ibl;
}
 
Example #6
Source File: IBLBuilder.java    From gdx-gltf with Apache License 2.0 6 votes vote down vote up
public static IBLBuilder createIndoor(DirectionalLight sun) {
	IBLBuilder ibl = new IBLBuilder();
	
	Color tint = new Color(1f, .9f, .8f, 1).mul(.3f);
	
	ibl.nearGroundColor.set(tint).mul(.7f);
	ibl.farGroundColor.set(tint);
	ibl.farSkyColor.set(tint);
	ibl.nearSkyColor.set(tint).mul(2f);
	
	Light light = new Light();
	light.direction.set(sun.direction).nor();
	light.color.set(1f, .5f, 0f, 1f).mul(.3f);
	light.exponent = 3f;
	ibl.lights.add(light);
	
	return ibl;
}
 
Example #7
Source File: Scene.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
private void syncLights(){
	for(Entry<Node, BaseLight> e : lights){
		Node node = e.key;
		BaseLight light = e.value;
		transform.set(node.globalTransform).mul(modelInstance.transform);
		if(light instanceof DirectionalLight){
			((DirectionalLight)light).direction.set(0,0,-1).rot(transform);
		}else if(light instanceof PointLight){
			((PointLight)light).position.setZero().mul(transform);
		}else if(light instanceof SpotLight){
			((SpotLight)light).position.setZero().mul(transform);
			((SpotLight)light).direction.set(0,0,-1).rot(transform);
		}
	}
}
 
Example #8
Source File: FluidSimulatorGeneric.java    From fluid-simulator-v2 with Apache License 2.0 5 votes vote down vote up
public FluidSimulatorGeneric(FluidSimulatorStarter fluidSimulatorStarter) {
		this.game = fluidSimulatorStarter;
		// LibGDX single batches cannot have a size more than 5460
		batch = new SpriteBatch(IS_DESKTOP ? 5460 : ANDROID_SIZE);
		font = new BitmapFont();
		camera = new OrthographicCamera(WORLD_WIDTH, WORLD_HEIGHT);
		camera.position.set(0, (WORLD_HEIGHT / 2) - 1, 0);
		immediateRenderer = new ImmediateModeRenderer20(SIZE*6, false, true, 0);
		irt = new Renderer20(SIZE*6, false, true, 1);
		irt2 = new ImmediateModeRenderer20(SIZE*11, false, true, 1);
		shapeRenderer = new ShapeRenderer(SIZE);
		renderer = new Box2DDebugRenderer(true, true, false, true, false, false);
		
		//3D
		camera3D = new PerspectiveCamera(67, WORLD_WIDTH, WORLD_HEIGHT);
		camera3D.position.set(0, 130f, 250f);
		camera3D.lookAt(0,150f,0);
		camera3D.near = 0.1f;
		camera3D.far = 500f;
		camera3D.update();
		ModelBuilder modelBuilder = new ModelBuilder();
//        model = modelBuilder.createSphere(5f, 5f, 5f, 4, 4, GL10.GL_TRIANGLES,
//                new Material(ColorAttribute.createDiffuse(Color.GREEN)),
//                Usage.Position | Usage.Normal);
        model = modelBuilder.createBox(5f, 5f, 5f,
            new Material(ColorAttribute.createDiffuse(Color.GREEN)),
            Usage.Position | Usage.Normal);
        instance = new ModelInstance(model);
        modelBatch = new ModelBatch();
        environment = new Environment();
        environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
        environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, 0, -0.8f, -0.2f));
        camController = new Camera3DController(camera3D);
        camController.setFluidSimulator(this);
		
		world = new World(new Vector2(0, -9.8f), false);
		world.setContactListener(this);
	}
 
Example #9
Source File: WorldGenerator.java    From Skyland with MIT License 5 votes vote down vote up
public static Environment generateBaseEnvironment(Vector3 sun) {
    Environment environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
    environment.set(new ColorAttribute(ColorAttribute.Fog, .3f, .55f, 1, 1));
    environment.add(new DirectionalLight().set(.3f, .3f, .3f, -.2f, 0.6f, .8f));
    environment.add(new DirectionalLight().set(1f, 1f, 1f, .2f, -0.6f, -.8f));
    environment.add(new PointLight().set(1, 1, 1, sun, 200));
    return environment;
}
 
Example #10
Source File: DirectionalLightEx.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
@Override
public DirectionalLight set (final DirectionalLight copyFrom) {
	if(copyFrom instanceof DirectionalLightEx){
		return set(((DirectionalLightEx) copyFrom).baseColor, copyFrom.direction, ((DirectionalLightEx)copyFrom).intensity);
	}else{
		return set(copyFrom.color, copyFrom.direction, 1f);
	}
}
 
Example #11
Source File: SceneManager.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
public DirectionalLight getFirstDirectionalLight(){
	DirectionalLightsAttribute dla = environment.get(DirectionalLightsAttribute.class, DirectionalLightsAttribute.Type);
	if(dla != null){
		for(DirectionalLight dl : dla.lights){
			if(dl instanceof DirectionalLight){
				return (DirectionalLight)dl;
			}
		}
	}
	return null;
}
 
Example #12
Source File: SceneManager.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
/**
 * Render shadows only to interal frame buffers.
 * (useful when you're using your own frame buffer to render scenes)
 */
@SuppressWarnings("deprecation")
public void renderShadows(){
	DirectionalLight light = getFirstDirectionalLight();
	if(light instanceof DirectionalShadowLight){
		DirectionalShadowLight shadowLight = (DirectionalShadowLight)light;
		shadowLight.begin();
		renderDepth(shadowLight.getCamera());
		shadowLight.end();
		
		environment.shadowMap = shadowLight;
	}else{
		environment.shadowMap = null;
	}
}
 
Example #13
Source File: Scene.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
public int getDirectionalLightCount() {
	int count = 0;
	for(Entry<Node, BaseLight> entry : lights){
		if(entry.value instanceof DirectionalLight){
			count++;
		}
	}
	return count;
}
 
Example #14
Source File: GLTFDemo.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
private void load(Scene scene)
{
	clearScene();
	
	this.scene = scene;
	
	scene.modelInstance.calculateBoundingBox(sceneBox);
	
	ui.setMaterials(scene.modelInstance.materials);
	ui.setAnimations(scene.modelInstance.animations);
	ui.setNodes(NodeUtil.getAllNodes(new Array<Node>(), scene.modelInstance));
	ui.setCameras(scene.cameras);
	ui.setLights(scene.lights);
	
	if(scene.getDirectionalLightCount() == 0){
		resetDefaultLight();
		sceneManager.environment.add(defaultLight);
	}
	
	sceneManager.addScene(scene, true);
	
	setShadow(ui.lightShadow.isOn());
	
	DirectionalLight light = sceneManager.getFirstDirectionalLight();
	if(light instanceof DirectionalShadowLight){
		((DirectionalShadowLight)light).setBounds(sceneBox);
	}
	
	invalidateShaders();
}
 
Example #15
Source File: Scene.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
protected BaseLight createLight(BaseLight from) {
	if(from instanceof DirectionalLight){
		return new DirectionalLightEx().set((DirectionalLight)from);
	}
	if(from instanceof PointLight){
		return new PointLightEx().set((PointLight)from);
	}
	if(from instanceof SpotLight){
		return new SpotLightEx().set((SpotLight)from);
	}
	throw new GdxRuntimeException("unknown light type " + from.getClass().getName());
}
 
Example #16
Source File: IBLBuilder.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
public static IBLBuilder createCustom(DirectionalLight sun) {
	IBLBuilder ibl = new IBLBuilder();
	
	Light light = new Light();
	light.direction.set(sun.direction).nor();
	light.color.set(sun.color);
	light.exponent = 100f;
	ibl.lights.add(light);
	
	return ibl;
}
 
Example #17
Source File: LightUtils.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
public static LightsInfo getLightsInfo(LightsInfo info, Iterable<BaseLight> lights){
	info.reset();
	for(BaseLight light : lights){
		if(light instanceof DirectionalLight){
			info.dirLights++;
		}else if(light instanceof PointLight){
			info.pointLights++;
		}else if(light instanceof SpotLight){
			info.spotLights++;
		}else{
			info.miscLights++;
		}
	}
	return info;
}
 
Example #18
Source File: IBLStudio.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
@Override
public IBLBuilder createBuilder(DirectionalLight sun) {
	IBLBuilder ibl = IBLBuilder.createCustom(sun);
	ibl.lights.first().exponent = 100f;
	ibl.lights.first().color.set(Color.WHITE);
	return ibl;
}
 
Example #19
Source File: DirectionalLightEx.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
@Override
public boolean equals(DirectionalLight other) {
	return (other instanceof DirectionalLightEx) ? equals((DirectionalLightEx)other) : false;
}
 
Example #20
Source File: GLTFLightExporter.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
public static GLTFLight map(GLTFLight glLight, BaseLight light) {
	float intensityScale;
	if(light instanceof DirectionalLight){
		glLight.type = GLTFLight.TYPE_DIRECTIONAL;
		if(light instanceof DirectionalLightEx){
			glLight.intensity = ((DirectionalLightEx) light).intensity;
		}else{
			glLight.intensity = 1;
		}
		intensityScale = 1;
	}
	else if(light instanceof PointLight){
		glLight.type = GLTFLight.TYPE_POINT;
		if(light instanceof PointLightEx){
			glLight.intensity = ((PointLightEx) light).intensity;
			glLight.range = ((PointLightEx) light).range;
		}else{
			glLight.intensity = 1;
		}
		intensityScale = 10;
	}
	else if(light instanceof SpotLight){
		glLight.type = GLTFLight.TYPE_SPOT;
		glLight.spot = new GLTFSpotLight();
		if(light instanceof SpotLightEx){
			glLight.intensity = ((SpotLightEx) light).intensity;
			glLight.range = ((SpotLightEx) light).range;
		}else{
			glLight.intensity = 1;
		}
		intensityScale = 10;
		// https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_lights_punctual/README.md#inner-and-outer-cone-angles
		// inverse formula
		float cosDeltaAngle = 1f / ((SpotLight)light).exponent;
		float cosOuterAngle = -((SpotLight)light).cutoffAngle / ((SpotLight)light).exponent;
		glLight.spot.outerConeAngle = (float)Math.acos(cosOuterAngle);
		glLight.spot.innerConeAngle = (float)Math.acos(cosOuterAngle + cosDeltaAngle);
	}
	else{
		throw new GdxRuntimeException("unsupported light type " + light.getClass());
	}
	
	// rescale color based on intensity
	glLight.color = GLTFExportTypes.rgb(light.color.cpy().mul(1f / glLight.intensity));
	glLight.intensity *= intensityScale;
	
	return glLight;
}
 
Example #21
Source File: IBLStudio.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
@Override
public IBLBuilder createBuilder(DirectionalLight sun) {
	return IBLBuilder.createIndoor(sun);
}
 
Example #22
Source File: IBLStudio.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
@Override
public IBLBuilder createBuilder(DirectionalLight sun) {
	return IBLBuilder.createOutdoor(sun);
}
 
Example #23
Source File: IBLStudio.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
@Override
public IBLBuilder createBuilder(DirectionalLight sun) {
	return null;
}
 
Example #24
Source File: View.java    From gdx-proto with Apache License 2.0 4 votes vote down vote up
public View() {
	ModelManager modelManager = new ModelManager();
	modelManager.init();
	storeSize();
	inst = this;
	gl = Gdx.graphics.getGL20();
	float fov = 67f;
	camera = new PerspectiveCamera(fov, width(), height());
	// camera.far affects frustrum culling, so a shorter distance can boost performance
	camera.far = 60f;
	camera.near = 0.01f;
	resetCamera();

	initShaders();
	modelBatch = new ModelBatch(shaderProvider);

	environ = new Environment();
	basicEnviron = new Environment();
	camLight = new PointLight();
	float intensity = 100f;
	camLight.set(new Color(0.2f, 0.2f, 0.2f, 1f), 0f, 0f, 0f, intensity);
	ColorAttribute ambientLight = ColorAttribute.createAmbient(new Color(0.1f, 0.1f, 0.1f, 1f));
	environ.set(ambientLight);
	ColorAttribute fog = new ColorAttribute(ColorAttribute.Fog);
	fog.color.set(fogColor);
	environ.set(fog);
	environ.add(camLight);
	dirLight = new DirectionalLight();
	dirLight.set(new Color(0.3f, 0.3f, 0.35f, 1f), -0.25f, -0.75f, 0.25f);
	environ.add(dirLight);

	basicEnviron.set(ColorAttribute.createAmbient(0.3f, 0.3f, 0.3f, 1f));

	if (Toggleable.profileGL()) {
		Profiler.enable();
	}

	hud = new HUD();
	
	Sky.createSkyBox(
		Assets.manager.get("textures/skybox/xpos.png", Texture.class),
		Assets.manager.get("textures/skybox/xneg.png", Texture.class),
		Assets.manager.get("textures/skybox/ypos.png", Texture.class),
		Assets.manager.get("textures/skybox/yneg.png", Texture.class),
		Assets.manager.get("textures/skybox/zpos.png", Texture.class),
		Assets.manager.get("textures/skybox/zneg.png", Texture.class)
	);
}
 
Example #25
Source File: GLTFDemo.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
@Override
public void render() {
	float delta = Gdx.graphics.getDeltaTime();
	stage.act();
	
	// recreate shaders if needed
	validateShaders();

	sceneManager.update(delta);
	
	if(cameraControl != null){
		cameraControl.update();
	}
	
	Gdx.gl.glClearColor(ui.fogColor.value.r, ui.fogColor.value.g, ui.fogColor.value.b, 0f);
	Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
	
	sceneManager.setAmbientLight(ui.ambiantSlider.getValue());
	
	ColorAttribute fog = sceneManager.environment.get(ColorAttribute.class, ColorAttribute.Fog);
	if(fog != null) fog.color.set(ui.fogColor.value);
	
	FogAttribute fogEquation = sceneManager.environment.get(FogAttribute.class, FogAttribute.FogEquation);
	if(fogEquation != null){
		fogEquation.value.set(
				MathUtils.lerp(sceneManager.camera.near, sceneManager.camera.far, (ui.fogEquation.value.x + 1f) / 2f),
				MathUtils.lerp(sceneManager.camera.near, sceneManager.camera.far, (ui.fogEquation.value.y + 1f) / 2f),
				10f * (ui.fogEquation.value.z + 1f) / 2f
				);
	}
	
	skybox.getColor().set(ui.skyBoxColor.value);
	
	DirectionalLight light = sceneManager.getFirstDirectionalLight();
	if(light != null){
		float lum = ui.lightSlider.getValue();
		if(light instanceof DirectionalLightEx){
			DirectionalLightEx lightEx = (DirectionalLightEx)light;
			lightEx.intensity = lum;
			lightEx.updateColor();
		}
		light.direction.set(ui.lightDirectionControl.value).nor();
		
		PBRFloatAttribute shadowBias = sceneManager.environment.get(PBRFloatAttribute.class, PBRFloatAttribute.ShadowBias);
		shadowBias.value = ui.shadowBias.getValue() / 50f;
	}

	sceneManager.render();

	if(ui.outlinesEnabled.isOn()){
		captureDepth();

		outlineShader.begin();
		float size = 1 - ui.outlinesWidth.getValue();
		
		// float depthMin = ui.outlineDepthMin.getValue() * .001f;
		float depthMin = (float)Math.pow(ui.outlineDepthMin.getValue(), 10); // 0.35f
		float depthMax = (float)Math.pow(ui.outlineDepthMax.getValue(), 10); // 0.9f
		
		// TODO use an integer instead and divide w and h
		outlineShader.setUniformf("u_size", Gdx.graphics.getWidth() * size, Gdx.graphics.getHeight() * size);
		outlineShader.setUniformf("u_depth_min", depthMin);
		outlineShader.setUniformf("u_depth_max", depthMax);
		outlineShader.setUniformf("u_inner_color", ui.outlineInnerColor.getValue());
		outlineShader.setUniformf("u_outer_color", ui.outlineOuterColor.getValue());
		
		if(ui.outlineDistFalloffOption.isOn()){
			
			float distanceFalloff = ui.outlineDistFalloff.getValue();
			if(distanceFalloff <= 0){
				distanceFalloff = .001f;
			}
			outlineShader.setUniformf("u_depthRange", sceneManager.camera.far / (sceneManager.camera.near * distanceFalloff));
		}
		
		spriteBatch.enableBlending();
		spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, 1, 1);
		spriteBatch.setShader(outlineShader);
		spriteBatch.begin();
		spriteBatch.draw(depthFbo.getColorBufferTexture(), 0, 0, 1, 1, 0f, 0f, 1f, 1f);
		spriteBatch.end();
		spriteBatch.setShader(null);
	}
	
	renderOverlays();
	
	int shaderCount = 0;
	ShaderProvider shaderProvider = sceneManager.getBatch().getShaderProvider();
	if(shaderProvider instanceof PBRShaderProvider){
		shaderCount = ((PBRShaderProvider) shaderProvider).getShaderCount();
	}
	ui.shaderCount.setText(String.valueOf(shaderCount));
	
	stage.draw();
}
 
Example #26
Source File: IBLStudio.java    From gdx-gltf with Apache License 2.0 votes vote down vote up
abstract public IBLBuilder createBuilder(DirectionalLight sun);