Create Renderables

A Renderable is a 3D model and consists of vertices, materials, textures, and more. It can be attached to a Node and rendered as part of a scene. This page describes how to create and modify renderables.

Create from Android widgets

You can create a ViewRenderable from standard android widgets. These are rendered as flat cards in the scene.

To create one:

  1. Create a layout file in res > layout. For example:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/planetInfoCard"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:background="@drawable/rounded_bg"
       android:gravity="center"
       android:orientation="vertical"
       android:padding="6dp"
       android:text="Test"
       android:textAlignment="center" />
    
  2. Build the ViewRenderable

    ViewRenderable.builder()
        .setView(this, R.layout.test_view)
        .build()
        .thenAccept(renderable -> testViewRenderable = renderable);
    

    This version of setView() takes the resource ID of the uninflated layout file. You can also call setView(View) to create a renderable from programmatically created views.

All build() methods in Sceneform return a CompletableFuture. The object is built on a separate thread and the callback function is executed on the main thread.

The size of the renderable is based on the size of the View object. By default, every 10px for the view becomes 1cm for the renderable. Call setPixelsToMeters() to change the conversion ratio.

Changes to the underlying view affect how the renderable is displayed. Nodes with a view renderable attached will dispatch touch events to the view, so you can, for example respond to a button press.

// update button text when the renderable's node is tapped
Button button = (Button) renderable.getView();
button.setOnClickListener((button) -> button.setText("clicked"));

Create from a 3D asset

Sceneform provides tools and plugins for converting 3D assets files (OBJ, FBX, glTF) to Sceneform binary assets (SFB), which can then be built into a ModelRenderable.

For more information, see Import and Preview 3D Assets.

Create at runtime

ShapeFactory and MaterialFactory let your create renderable objects from simple shapes and materials.

Here's how to create a red sphere:

MaterialFactory.makeOpaqueWithColor(this, new Color(android.graphics.Color.RED))
        .thenAccept(
            material -> {
              redSphereRenderable =
                  ShapeFactory.makeSphere(0.1f, new Vector3(0.0f, 0.15f, 0.0f), material); });

Modify renderables at runtime

If multiple nodes use the renderable, then changes to that renderable apply to all nodes. To avoid that behavior, call makeCopy() to create a separate renderable instance. Note this also calls makeCopy() on every material in the renderable.

blueSphereRenderable = redSphereRenderable.makeCopy();
blueSphereRenderable.getMaterial().setFloat3(
                  MaterialFactory.MATERIAL_COLOR, new Color(android.graphics.Color.BLUE));

Send feedback about...