I have material with Shader in unity.
How to get a value in code?
For example, i need change value EdgeWidth in code, how can i do it?
According to the Unity documentation, you need to find your material or your shader, in your script, and you have a lot of functions to change the value of a material, like .SetFloat().
Renderer rend = GetComponent<Renderer> ();
rend.material.SetFloat("EdgeWidth", -1.18f);
Related
I'm trying to create a shader that tiles a texture on an object, But I am Running into an issue. When trying to tile with this Shader Graph, It looks good from the front but looks wrong from the side. this is a 3x3x1 object and all of the squares should be the same size. Thanks in advance
I think it's because you're trying to input a Vector3 into a UV input node, which only takes a Vector2, so it's not using the Z component, which is the dimension that isn't working for you.
I haven't looked too deeply into it, but maybe this script will let you accomplish tiling in all three dimensions? https://github.com/Dsphar/Cube_Texture_Auto_Repeat_Unity/blob/master/ReCalcCubeTexture.cs
I have a model that I have to use in game. I imported the model and extract materials as: 'clothes', 'phong1' and 'skin'
But its applying 'phong1' material to all over the model and nothing changes when i change the color on 'clothes' or 'skin' material.
Weirdness on the model
Do you guys have any idea to how to fix that? Im sharing original .fbx and texture to be more identifying.
https://easyupload.io/lsowue
Check the attached MeshRenderer or SkinnedMeshRenderer and check if the materials are passed in right order.
I have a plane mesh (a custom trail) that uses an advanced shader in URP and I need it to detect overlaps and change it's color to red.
The application is 3d and the trail is always drawn at y zero. The user needs to have visual feedback when the trails overlap so they can fix their route.
I am trying to do it using a Stencil Shader but stencil for URP doesn't work like in built-in renderer.
This question has exactly what I need to do, but the shader doesn't work in URP. Whatever shader I try to do multiple passes of stencil it just renders the first pass and never detects the other passes.
In URP I was able to use stencil only with one writer shader and one reader shader and Replacing the stencil buffer (adding doesn't work).
Since it can't detect the two different passes I can't even do it using doubled objects with writer and readers and my reader having a pass for the advanced shader and one for the flat red color.
Remember, I need a bunch of plane trails to detect overlapping and self overlapping.
Can anyone make it work or suggest another approach? (I must finish this at my job)
Found two ways of doing it.
After some hours of searching I found this thread where Invertex says that:
URP doesn't automatically run all shader passes for performance reasons.
You could make a second material that has the other pass enabled and add it to the object's material list. It would effectively be the same.
So I made a Shader with:
Tags {
"RenderType" = "Opaque"
"Queue" = "Geometry-1"
}
Pass
{
Stencil {
Ref 0
Comp Equal
Pass IncrSat
Fail IncrSat
}
}
//here the code that renders when not overlapping
And another shader with:
//this one is in "Queue"="Geometry"
Stencil {
Ref 1
Comp Less
}
//here the code that renders when overlapping
Then I place both shaders in my prefab. Since it has only one submesh, it renders both materials in it.
The method above is an ugly and costly workaround, so I kept searching on the problem
The solution I was after was in this thread. We can use "LightMode" tags like "DepthOnly", "Meta", "SRPDefaultUnlit", "UniversalForward", etc... to bypass the passes in URP. They will render in a specific order, no matter in which order they are on your file. You are limited to a few but it solves it in my case.
Since "SRPDefaultUnlit" will render first for all objects and then "UniversalForward" will render for all objects, I can use this to my advantage.
Inside the first pass:
Tags
{
"LightMode" = "SRPDefaultUnlit"
}
Stencil
{
Ref 0
Comp Equal
Pass IncrSat
Fail IncrSat
}
//here the code that renders when not overlapping
Inside the second pass:
Tags
{
"LightMode" = "UniversalForward"
}
Stencil
{
Ref 1
Comp Less
}
//here the code that renders when overlapping
NOTE: If you're using the Built in Render the LightMode will not work, you'll have to use the default one and change the passes order in code to get the desired effect. You'll put the overlay one after the base pass.
This seem to be the final solution for me but feel free to contribute as you may help other people in the future.
Here is my code:
root_level1.GetComponent<Renderer>().material.EnableKeyword ("_NORMALMAP");
Texture tex = Resources.Load("book-cover-1-glitched-23-11-2020-8-35-56-pm") as Texture;
root_level1.GetComponent<Renderer>().material.mainTexture = tex;
To change material texture in runtime you should change it by accessing shader properties. First of all you gonna check these properties in shader code or shader properties in unity inspector. If you want to check shader code, your goal is "properties" block:
Or you can select shader in Unity Editor and check Inspector Window:
So to change Base RGB Texture we should change property "_MainTex" and to change Color we have to change property "_Color".
I your example its gonna be like this:
root_level1.GetComponent<Renderer>().material.SetTexture("_MainTex", tex);
I am trying to save a scalar signal value for future use as a texture with the Spark AR software.
Basically, using the camera texture on a material is scripted like this:
const texture = Textures.get('CameraTexture').signal;
const material = Materials.get('defaultMaterial0');
material.setTexture(texture, {textureSlotName: "diffuseTexture"})
It is not clear to me what class is the texture const, but I am guessing this is a ScalarSignal.
That means that it is a stream, not a value. How would I go about snapshotting that value? lastValue does not work, and when trying valueOf(), I get
JavaScript error: Exception in native code while calling a function: valueOf() called on a Signal. This probably means that you are trying to perform an arithmetic operation on a signal like +, -, *, etc. Use functions .add, .sub(), etc on the signal instead or .subscribeWithSnapshot() on an EventSource to get the signal's current value on a callback.
And subscribeWithSnapshot() is not a function..
If anyone has any idea how I could do this, or the specific reason why I can't, help would be appreciated!
Thanks!
You can't. Currently in SparkAr is not allowed to save a texture. And I think the data type should be a texture buffer (eventually...)
EDIT: Also, the camera texture signal it's not a scalarSignal but a ShaderSignal.
you can check this:
Diagnostics.log(Textures.get('CameraTexture').signal);