问题
I am creating a 3d scene currently a box and rect, and trying to enable lighting.
When i create a PointLight and add it to Environment everything turns to black color?
all i want to do is create a 3d scene and enable point light, like a sun or rays coming from a point and shading the objects.
Code:
environment = new Environment();
environment.add(new PointLight().set(1f, 1f, 1f, 0, 0, 20f, 100f));
modelBatch=new ModelBatch();
..
square=new ModelBuilder().createBox(300,300,300,new Material(ColorAttribute.createDiffuse(Color.GREEN)),
VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
squareinst=new ModelInstance(square);
squareinst.transform.setTranslation(-500,0,0);
--
sprites.get(0).setRotationY(sprites.get(0).getRotationY() + 1f);
sprites.get(1).setRotationY(sprites.get(1).getRotationY() - 1f);
squareinst.transform.rotate(1,0,0,1);
modelBatch.begin(camera);
for(Sprite3D sp:sprites)// has 3d rect models
sp.draw(modelBatch,environment);
modelBatch.render(squareinst,environment);
modelBatch.end();
PointLight turning everything black
Without using environment or lights
as per my investigation, here if pointlight is not working then everything should be black as currently, because the environment needs light, it works fine with Directional light (only the backface of rect is black even after rotations, i don't know why)
libgdx version 1.6.1 - android studio i checked it on both android device and desktop
please i really need to get this PointLight working, i don't know if it will take a custom shader, if so please guide me to some links because i am not experienced in shaders. I also read about PointLight not working on some device or not working in opengl 2.0 enabled, but i am not sure.
I tried a lot of thing and values. I know about Ambient Light but that has no use to my case. Directional light also has limited usage (can be used as a fallback if this doesn't work).
Edit:
Its working now, check the answer below:
- if you are using big camera size or big model size, please try adding more zeros to the pointlight intensity until the light is visible.
回答1:
Here is a very simple example that shows a point light being rotated around a sphere:
public class PointLightTest extends ApplicationAdapter {
ModelBatch modelBatch;
Environment environment;
PerspectiveCamera camera;
CameraInputController camController;
PointLight pointLight;
Model model;
ModelInstance instance;
@Override
public void create () {
modelBatch = new ModelBatch();
camera = new PerspectiveCamera();
camera.position.set(5f, 5f, 5f);
camera.lookAt(0f, 0f, 0f);
camController = new CameraInputController(camera);
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.0f));
environment.add(pointLight = new PointLight().set(0.8f, 0.8f, 0.8f, 2f, 0f, 0f, 10f));
ModelBuilder mb = new ModelBuilder();
model = mb.createSphere(1f, 1f, 1f, 20, 10, new Material(ColorAttribute.createDiffuse(Color.GREEN)), Usage.Position | Usage.Normal);
instance = new ModelInstance(model);
Gdx.input.setInputProcessor(camController);
}
@Override
public void resize (int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
@Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
camController.update();
pointLight.position.rotate(Vector3.Z, Gdx.graphics.getDeltaTime() * 90f);
modelBatch.begin(camera);
modelBatch.render(instance, environment);
modelBatch.end();
}
@Override
public void dispose () {
model.dispose();
modelBatch.dispose();
}
}
Note that the light needs to be outside the model and within the range for it to light the model. Try what happens when you gradually move the light away from the model or towards the model. The Renderable in that other example was used to visualize this location of the light.
来源:https://stackoverflow.com/questions/34256785/libgdx-3d-point-light-shows-black-box-rect-pointlight-not-working