How can I set a value (via code) on a material using the Unity GLTF PBR shader?

Hi again,

In my script I have accessed the renderers sharedMaterial. I am then trying to set the Surface Inputs > Base Color Map > Offset values via a script but I dont see any option to do so. I have tried via the .setvalues() method, and directly, but neither have a mapping for the value I want to set.

Is there a specific way of doing this? I can see the Base Color Map property is exposed in the inspector but I cant seem to access and change it via a script.

Hi, nostalgicbear! I’m your AI-powered assistant here to help. Although I can’t answer your question directly, I can assist with queries about Needle Engine, three.js, and basic Unity or Blender help. Please provide more info, such as your OS and the versions of Needle Engine and Unity you’re using, and feel free to ask your question!

2 replies left - GPT 4O
thread_WJWjtqmletPyq4zclOQ2B9j9
Reply to this message to continue the conversation

Via a script, how can I set the Base Color Map offset value of a material using the UnityGLTF/PBRGraph shader?

OS : Windows
Needle version : 3.45.2-pre
Unity version : 2022.3.12f1

Hi, nostalgicbear! Here’s how you can set the Base Color Map offset value of a material using the Unity GLTF/PBRGraph shader via a script:

  1. First, access the Renderer component.
  2. Get the shared material from the renderer.
  3. Use Material.SetTextureOffset to set the Base Color Map offset.

Here’s a code snippet:

using UnityEngine;

public class UpdateMaterial : MonoBehaviour
{
    public Renderer targetRenderer;

    void Start()
    {
        // Ensure the renderer is assigned
        if (targetRenderer != null)
        {
            // Get the shared material
            Material material = targetRenderer.sharedMaterial;

            // Set the offset for the Base Color Map
            Vector2 offset = new Vector2(0.1f, 0.2f); // Replace with your values
            material.SetTextureOffset("_BaseColorMap", offset);
        }
    }
}

Replace 0.1f and 0.2f with your desired offset values. Note that the property name _BaseColorMap should match the shader’s property exactly (see the documentation). If you have further questions, feel free to ask!

1 replies left - GPT 4O
thread_WJWjtqmletPyq4zclOQ2B9j9
Reply to this message to continue the conversation

How do I do it via typescript using the classes and properties exposed on the material through Needle Engine?

Hi, nostalgicbear! To set the Base Color Map offset value of a material using Needle Engine with TypeScript, you can create a custom component and use the material’s properties exposed by the engine.

Here’s a sample code snippet:

import { Material } from 'needle-tools';

class SetBaseColorMapOffset {
    private material: Material;

    constructor(material: Material) {
        this.material = material;
    }

    setOffset(offset: { x: number, y: number }) {
        this.material.setTextureOffset("_BaseColorMap", offset);
    }
}

// Usage example
const myMaterial = ...; // Access your material appropriately
const colorMapOffset = new SetBaseColorMapOffset(myMaterial);
colorMapOffset.setOffset({ x: 0.1, y: 0.2 }); // Set your desired offset values

Make sure to adjust the property names in accordance with the specific shader you’re using, as it may vary (see the documentation). Let me know if you have more questions!

0 replies left - GPT 4O
thread_WJWjtqmletPyq4zclOQ2B9j9
Reply to this message to continue the conversation

Hi

at runtime (in the browser/Needle Engine) your material will be a three.js Material - so you need to access the MeshStandardMaterial map property - so you want to modify the Texture offset and repeat properties

The sharedMaterials array is what gives you access to all those textures that you assigned in Unity

1 Like

Thank you as always @marcel

1 Like

This topic was automatically closed 13 days after the last reply. New replies are no longer allowed.