Help with detail ambient texture material plugin

Hi all, im trying to create something similar to detail maps

but basically a detail ambient texture that tiles separately from the ambient texture. with limited documentation on material plugins my thought was to just copy the detail map code and base my detail ambient texture plugin off of that.

however i don’t actually see where the detail map stuff is being calculated in the fragment shader in the above code?

for my use case the fragment shader would just multiply the ambient texture with the detail ambient texture eg:

ambientTexture = ambientTexture * detailAmbientTexture;

i looked at the texture samplers code in docs but its still not clear how to approach this. any help or links to resources would be appreciated.

cc @Evgeni_Popov

The shader code for detail maps is embedded directly into the standard and PBR shader code, so we don’t need to implement the getCustomCode function.

In your case, you will have to implement this function and do something like:

getCustomCode(shaderType) {
    return shaderType === "vertex" ? null : {
        "!reflectivityOutParams reflectivityOut;": `
            aoOut.ambientOcclusionColor.rgb *= vec3(0.5);
            reflectivityOutParams reflectivityOut;
        `,
    };
}

You will need to replace vec3(0.5) by your own code, of course.

We don’t have a predefined replacement point (like CUSTOM_FRAGMENT_BEGIN, CUSTOM_FRAGMENT_DEFINITIONS, CUSTOM_FRAGMENT_UPDATE_ALPHA, etc) for the ambient value, so I had to find a place in the sources where the code could be injected. It turns out that the ambient value is calculated before the reflectivityOutParams reflectivityOut; declaration, hence the regexp I used.

2 Likes

@Evgeni_Popov
so I went ahead and implemented this and it works in the playground but the shader does not compile in my own app. I create and assigned the material in the same way I do in the PG example, the setup is identical. Also note that if I cut paste the simplified getCustomCode from the shader block above in the thread it works.

In the above shader code do you see anything bad or wrong im doing in terms of not following best practices…or initializing ubo’s.? ( we can assume all shaders are PBRMaterial NOT StandardMaterial as we never use StandardMaterial. )

here is what the error log is giving me:

Offending line [730] in fragment code: vec2 AOuv = vAlbedoUV * detailAOScale;

Your code looks ok to me. You could get a micro gain by using a single vec2 to pass ao scale and intensity instead of using two separate floats.

Regarding the error, we would need the whole log (or a live repro), as there is probably some context + the full shader code.

1 Like

here is the logs.

Searching for “error:” gives me…
logger.ts:103 BJS - [22:45:57]: Error: FRAGMENT SHADER ERROR: 0:730: ‘vAlbedoUV’ : undeclared identifier
ERROR: 0:730: ‘=’ : dimension mismatch
ERROR: 0:730: ‘=’ : cannot convert from ‘highp float’ to ‘highp 2-component vector of float’

vAlbedoUV is alread declared in the fragment shader I thought. Its used here for example…

Do i need to explicitly declare it in CUSTOM_FRAGMENT_DEFINITIONS?

FYI this is how I initialize the material plugin in my own code . Note, the mesh is an imported GLTF mesh that is imported in a previous step. The mesh is completely valid and is importing properly.

      // apply detail AO plugin
       mesh.material = new PBRMaterial("pbr",scene);
mesh.material.detailaoplugin = new DetailAOMaterialPlugin(mesh.material);
      mesh.material.detailaoplugin.texture  =  pbr.ambientTexture;
      //mesh.material.detailaoplugin.texture  =  pbr.ambientTexture;
      //mesh.material.detailaoplugin.detailAOScale = 10;
     // mesh.material.detailaoplugin.detailAOIntensity = 0.0;
      mesh.material.detailaoplugin.isEnabled = true;

We have to request access to get the files from the Google drive.

Anyway, I think your problem is that you use vAlbedoUV in your plugin but don’t have an albedo texture in your material: vAlbedoUV is only defined if material.albedoTexture is not empty.

1 Like

ohhh let me double check if this has an albedo texture.
anyhow I did update permissions on that drive link apologies.

yep that was it. the material in question did not have an Albedo Texture. I ended up using vAmbientUV as I was using the base ambient texture and fixed my issue.