Skip to content
This repository has been archived by the owner on Jul 28, 2020. It is now read-only.

how to set opacity for makeTransparentWithTexture material #196

Closed
michaelvogt opened this issue Jul 15, 2018 · 14 comments
Closed

how to set opacity for makeTransparentWithTexture material #196

michaelvogt opened this issue Jul 15, 2018 · 14 comments

Comments

@michaelvogt
Copy link

Hello.

When I create a material with makeTransparentWithColor, I can set the opacity with the alpha parameter of the color. Is there a way to set the opacity of a material created with makeTransparentWithTexture during runtime? The renderable is a ShapeFactory Cube.

Thanks,
Michael

@claywilkinson
Copy link
Contributor

Unfortunately, there is no way to affect the opacity of that material. The materialFactory generated materials are intended more for simple materials or placeholders during development.

You can create a custom material for your model that you could set a base texture and then pass in a parameter to affect the final color.

I don't have a sample of that handy, but let me know if you have questions.

@michaelvogt
Copy link
Author

Only to make sure I understand correctly: it is not possible to accomplish transparency / translucency with a material from MaterialFactory.makeTransparentWithTexture call? Will this stay like this or are there plans to provide this functionality?

You can create a custom material for your model that you could set a base texture and then pass in a parameter to affect the final color.

As I'm using a ShapeFactory object: Is there a way to create a custom material during runtime?

Or are you suggesting that I should create a simple sfb for example of a plane, create a custom material for it and reuse it throughout the application, instead of using the ShapeFactory?

What I want to accomplish is, that the user can set the translucency of objects with a slider - for example.

@dsternfeld7
Copy link

To clarify, when using makeTransparentWithTexture, the transparency of the material comes from the alpha channel of the texture. My understanding is that what you want is to control the transparency uniformly for the object with a float from 0-1 for use with a slider instead. To do this, you can write a custom material that has a scale factor multiplied against the alpha from the texture.

When writing a custom material, you can add a float parameter called alphaFactor and do this in the fragment block of the material:

material.baseColor = texture(materialParams_texture, getUV0());
material.baseColor.a *= materialParams.alphaFactor;

In regards to creating a custom material at runtime, there is currently no way to load a material outside of an .sfb. However, changing this is on our radar. That leaves two options, you can create a .sfb and use that instead of the ShapeFactory, OR you can create a dummy .sfb, load that as a Renderable, and then do renderable.getMaterial() to get the material and pass it into the ShapeFactory.

Once you have the custom material, you could change the parameter you created at runtime by doing material.setFloat("alphaFactor", alpha);

@michaelvogt
Copy link
Author

Thank you for clarification.

Glad to hear (read) that loading a material outside of an .sfb is on your radar. Can we keep this ticket open as a feature request?

@dsternfeld7
Copy link

No problem! We currently are tracking this feature request through issue #157. We prefer not to keep duplicate issues open, and to have separate issues open for things instead of having one large issue that encapsulates multiple things.

@romainguy
Copy link

The alpha value of baseColor only applies to diffuse lighting. Specular highlights are by definition reflected light and are therefore unaffected by the transparency of the surface. It is however useful to be able to affect both diffuse and specular lighting together for fade effects. This will require a new feature in custom materials.

@dsternfeld7
Copy link

To add to that, in the meantime you can fade an object out entirely by using the unlit shading model in the custom material.

@michaelvogt
Copy link
Author

@dsternfeld7
Yes, agreed. I didn't realize that a feature request already exists.

@romainguy
Is this new feature for custom materials already in consideration?

@romainguy
Copy link

@michaelvogt Yes, I will add this feature

@michaelvogt
Copy link
Author

👍

@romainguy
Copy link

I implemented the new feature internally. It's a new blending mode called "fade".

@michaelvogt
Copy link
Author

@dsternfeld7
I finally had time to try out your suggestion, but failed. Somehow I don't get alpha working at all. I guess I'm missing something simple here.

.sfa
{ bound_relative_root: { x: 0.5, y: 0, z: 0.5, }, materials: [ { name: "unlit_material", parameters: [ { texture: "andy", }, { alphaFactor: 0.5, }, { backColor: [ 0.5, 0.5, 0, 1, ], }, ], source: "sampledata/models/andy.mat", }, ], model: { attributes: [ "Position", "TexCoord", "Orientation", ], collision: {}, file: "sampledata/models/andy.obj", name: "andy", recenter: "root", }, samplers: [ { file: "sampledata/models\\andy.png", name: "andy", pipeline_name: "andy.png", }, ], version: "0.52:1", }

.mat
`material {
name: "Custom material",
parameters: [
{
type: "sampler2d",
name: "texture"
}, {
type: "float",
name: "alphaFactor"
}, {
type: "float4",
name: "backColor"
}
],
requires: [
"position",
"uv0"
],
shadingModel: "unlit",
}

fragment {
void material(inout MaterialInputs material) {
prepareMaterial(material);

    // material.baseColor = materialParams.backColor;
    material.baseColor = texture(materialParams_texture, getUV0());
    material.baseColor.a *= materialParams.alphaFactor;
}

}`

creating the shape:
ModelRenderable.builder() .setSource(this, R.raw.andy) .build() .thenAccept(temp -> { Material material = temp.getMaterial(); material.setFloat("materialParams.alphaFactor", 0.5f); ModelRenderable renderable = ShapeFactory.makeCube(Vector3.one(), Vector3.zero(), material); });

@michaelvogt michaelvogt reopened this Aug 21, 2018
@dsternfeld7
Copy link

In the material block, try adding "blending" : "transparent". There is more information about the different blend modes on this page.

@michaelvogt
Copy link
Author

Yes, works - of course.
Sorry for not finding this myself...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants