How can I create a transparent panel for shadows in sceneform?

374 Views Asked by At

I looked everywhere but found no answer. The idea is to create a node with a transparent panel that receives only shadows. I tried with

MaterialFactory.makeTransparentWithColor(context, new Color(255,255,255,0.2f))
.thenAccept(material -> {
            Vector3 size = new Vector3(5f,0.0001f,5f);
            Vector3 center = new Vector3(0f,-0.5f,0f);
            ModelRenderable floorRenderable = ShapeFactory.makeCube(size,center,material);
            floorNode.setRenderable(floorRenderable);
        })

and

MaterialFactory.makeTransparentWithTexture(context, texture)
        .thenAccept(material -> {
            Vector3 size = new Vector3(5f,0.0001f,5f);
            Vector3 center = new Vector3(0f,-0.5f,0f);
            ModelRenderable floorRenderable = ShapeFactory.makeCube(size,center,material);
            floorNode.setRenderable(floorRenderable);
        })

but I got a not completely transparent object.

thanks

1

There are 1 best solutions below

0
On

I was able to get this to work with @RomainGuy's help.

First, create the `.mat file

material {
    name : GroundShadow,
    blending : transparent,
    shadingModel : unlit,
    shadowMultiplier : true
}

fragment {
    void material(inout MaterialInputs material) {
        prepareMaterial(material);
        material.baseColor = vec4(0.0, 0.0, 0.0, 0.5); // change 0.5 to change the strength of the shadow
    }
}

Adjust vec4's fourth parameter to change the strength of the shadow.

Compile the .mat file with a version of Filament's matc that Sceneform uses (in my case, it was Filament 1.45). I wasn't sure how to find out so I just experimented with various version of Filament here: https://github.com/google/filament/releases/.

I ended up needing this file: https://github.com/google/filament/releases/download/v1.4.5/filament-20200127-mac.tgz.

Place the resulting matc file in your res/raw package, and use it like so. If the matc file is incompatible, your app will crash. That's how I found out which matc was the correct one, through trial and error:

val footprintSelectionVisualizer = FootprintSelectionVisualizer()
val transformationSystem = TransformationSystem(displayMetrics, footprintSelectionVisualizer)

Material.builder()
    .setSource(context, R.raw.sceneform_plane_shadow_material)
    .thenAccept { material ->

                    val size = Vector3(1f, 0.00001f, 1f)
                    val center = Vector3(0f, 0f, 0f)
                    val floorRenderable: ModelRenderable = ShapeFactory.makeCube(size, center, material)
                    floorRenderable.isShadowReceiver = true
                    floorRenderable.isShadowCaster = false
                    footprintSelectionVisualizer.footprintRenderable = floorRenderable

                    node.select()
                }