libgdx阴影未显示在地板上

doinxwow  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(314)

我试图添加阴影到我的模型底部,但阴影没有显示在底部。有人能帮忙吗?这就是我尝试使用不推荐的 DirectionalShadowLight :

environment = new Environment();
        environment.set(new ColorAttribute(ColorAttribute.AmbientLight, .4f, .4f, .4f, 1f));
        environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

        shadowLight = new DirectionalShadowLight(1024, 1024, 60, 60, 1f, 300);
        shadowLight.set(0.8f, 0.8f, 0.8f, -1f, -.8f, -.2f);
        environment.add(shadowLight);
        environment.shadowMap = shadowLight;
        shadowBatch = new ModelBatch(new DepthShaderProvider());

        camera = new PerspectiveCamera(67, screenWidth, screenHeight);
        camera.position.set(0, 6, 2);
        camera.direction.set(0, 0, -4).sub(camera.position).nor();
        camera.near = 1;
        camera.far = 300;
        camera.update();

   private void renderSanta(GL20 gl) {
        gl.glEnable(GL20.GL_DEPTH_TEST);
        gl.glEnable(GL20.GL_CULL_FACE);
        // /****************
        shadowLight.begin(Vector3.Zero, camera.direction);
        shadowBatch.begin(shadowLight.getCamera());
        shadowBatch.render(santaModel);
        shadowBatch.end();
        shadowLight.end();
       //********************/
        modelBatch.begin(camera);
        modelBatch.render(santaModel, environment);
        modelBatch.end();
       gl.glDisable(GL20.GL_CULL_FACE);
       gl.glDisable(GL20.GL_DEPTH_TEST);
    }

   private void renderBackground() {
        viewMatrix.setToOrtho2D(0, 0, 480, 800);
        spriteBatch.setProjectionMatrix(viewMatrix);
        spriteBatch.begin();
        spriteBatch.enableBlending();
        spriteBatch.setColor(Color.WHITE);
        spriteBatch.draw(background, 0, 0, 480, 800);
        spriteBatch.draw(hammock, 0, 800 - 500 - 95, 182, 95);
        spriteBatch.disableBlending();
        spriteBatch.end();
    }

@Override
    public void draw(float delta) {
        GL20 gl = Gdx.gl;
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        renderBackground();
        renderSanta(gl);
    }


编辑:
所以就像xoppa评论的那样,我在圣诞老人模型下添加了一个“平面”,但仍然没有显示阴影:

ModelBuilder modelBuilder = new ModelBuilder();
        modelBuilder.begin();
        MeshPartBuilder mpb = modelBuilder.part("parts", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.ColorUnpacked,
                new Material(ColorAttribute.createDiffuse(Color.WHITE)));
        mpb.box(0, -1.5f, 0, 7, 1, 7);
        mpb.setColor(1f, 0f, 1f, 1f);
        model = modelBuilder.end();
        instance = new ModelInstance(model);
        instance.transform.setTranslation(0, -5, -3);

  public void renderModel() {        
        shadowLight.begin(Vector3.Zero, camera.direction);
        shadowBatch.begin(shadowLight.getCamera());
        shadowBatch.render(instance);
        shadowBatch.end();
        shadowLight.end();

        modelBatch.begin(camera);
        modelBatch.render(instance, environment);
        modelBatch.end();
    }

 @Override
        public void draw(float delta) {
            GL20 gl = Gdx.gl;
            gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
            renderBackground();
            renderModel();
            renderSanta(gl);
        }

更新后的输出仍显示阴影

编辑2:多亏了xoppa,我才得以让阴影出现:

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题