URP Decals

Is there any example of exporting (URP) Decals?

Original Post on Discord

by user 732122195614105653

There is no example for that at the moment. You could try creating a script matching the urp decal script name tho and seeing if you can use the exported data to feed it into the threejs decals three.js webgl - decal splatter

A good way to get started doing something like that is creating a typescript class with a matching name and then logging console.log(this) in e.g. start and inspecting the exported data

I must be missing something here.

export class DecalProjector extends Behaviour {

    start() {
        console.log(this);
    }
}```
It seems to still generate a C# component in /codegen, and I get a message in the inspector saying "a generated component exists for this type but you are not using it here".

*by user 732122195614105653*

You need to make it a comment: //@dont-generate-component

But even if it exists multiple times it should work (if you use the correct one in Unity!)

I had to manually delete the previously generated version but it works now. Thanks!

by user 732122195614105653

Yes scripts in src/scripts won’t delete c# scripts, that only works when you use npmdefs

Thanks for your support @marcel :cactus: , I managed to add support for URP Decal Projectors.

by user 732122195614105653

Here it is. Please let me know if there’s anything I can do better here. Thanks
DecalProjector.ts

by user 732122195614105653

Hello @Fred@Unity , thanks for sharing. One thing that I would definitely do is to declare the fields that you are actually using in your class. For example you access this.material but its not declared. This works because currently we export everything and its just there but it can be surprising/confusing if we have to figure out which members are required by going through the code and additionally we might only export the data that you declared in your code at some point.

Also I would declare showHelpers in global scope like this: const debug = getParam("debugdecals") for example - that way you can just append ?debugdecals to your URL and enable/disable the decal helpers anytime without having to modify the code

The last thing is maybe the use of var which is kinda outdated. Var has some weird behaviour when it comes to scopes which is why the “modern” way to declare variables that can change is let and variables that can not be assigned anymore is const (const variables can change if they are a reference type of course, like a Vector3 in javascript)

Thanks for the input. So, just to make sure I understand well, the existing component has a m_Material serialized and private field, and a material public accessor.
Should I add a material: Material; to the typeScript class? I tried adding this instead:

m_Material: Material;```
But then accessing `this.m_Material` got me an error as it was undefined.

*by user 732122195614105653*

Yes add

@serializable(Material)
material : Material;

the m_Material private field is not exported as you can see when you do console.log(this) (at least that’s what I assume when you say you get an undefined error, I’m not familiar with the Decal component in URP)

But on the Unity C# component, material is an accessor, not a field.

by user 732122195614105653

We serialize properties as well as fields, depending on visibility or if they’re marked with SerializeField. Not sure why m_Material is not exported then, I would have to look at the class.

This is what it looks like in the C# component:

private Material m_Material = null;
/// <summary>
/// The material used by the decal.
/// </summary>
public Material material
{
    get
    {
        return m_Material;
    }
    set
    {
        m_Material = value;
        OnValidate();
    }
}```

*by user 732122195614105653*

I have tried this script. Its not working
console.log(this.material.map);
const decalMaterial = new THREE.MeshStandardMaterial({
map: this.material.map,
normalScale: new THREE.Vector2(1, 1),
transparent: true,
depthTest: true,
depthWrite: false,
polygonOffset: true,
polygonOffsetFactor: -4,
wireframe: false,
side: THREE.FrontSide,
});
this.material.map is returning ‘null’-

by user 1141289362336784394

Is there any updated code for decal? Please guide.Thanks

by user 1141289362336784394