- Home /
Reflections disappear when changing materials color
I use the Hardsurface shaders by Bruno Rime. I also use a realtime reflection script.
When I change the color of the material (in another script), with Material.SetColor(), the reflections dissappear.
Edit: seems that the refletions disappear even when the script just access the material, even without changing the color...
foreach(Renderer rend in CarBodyRenderers){
foreach(Material matt in rend.materials){
// if(matt.name == carBodyMaterialName){
// matt.SetColor("_Color", carPaint);
}
}
}
// this is the real time reflection script
using UnityEngine;
using System.Collections;
public class RealtimeCubemap : MonoBehaviour {
public int cubemapSize = 128;
public bool oneFacePerFrame = false;
private Camera cam;
private RenderTexture rtex;
private GameObject go;
[ExecuteInEditMode]
void Start() {
// render all six faces at startup
UpdateCubemap(63);
}
void LateUpdate() {
if (oneFacePerFrame) {
int faceToRender = Time.frameCount % 6;
int faceMask = 1 << faceToRender;
UpdateCubemap(faceMask);
} else {
UpdateCubemap(63); // all six faces
}
}
void UpdateCubemap(int faceMask) {
if (!cam) {
go = new GameObject("CubemapCamera");
go.AddComponent(typeof(Camera));
go.hideFlags = HideFlags.HideAndDontSave;
go.transform.position = transform.position;
go.transform.rotation = Quaternion.identity;
cam = go.camera;
cam.farClipPlane = 100; // don't render very far into cubemap
cam.enabled = false;
}
if (!rtex) {
rtex = new RenderTexture(cubemapSize, cubemapSize, 16);
rtex.isCubemap = true;
rtex.hideFlags = HideFlags.HideAndDontSave;
renderer.sharedMaterial.SetTexture ("_Cube", rtex);
}
cam.transform.position = transform.position;
cam.RenderToCubemap(rtex, faceMask);
}
void OnDisable() {
DestroyImmediate (cam);
DestroyImmediate (rtex);
}
}
Answer by Sondre-S · Feb 17, 2015 at 12:11 PM
The realtime refletion script is in the camera object, so I was able to get the reflections and apply them to the instanciated material using gameObject.Find("camera").getComponentInChildren<>();
Answer by giulio-pierucci · Feb 16, 2015 at 12:25 PM
Maybe that when you accessing to material to change color parameter, Unity change the material instance ( you can read "(instance)" append to material name, and changing cubemap to sharedMaterial (renderer.sharedMaterial.SetTexture ("_Cube", rtex)) don't change the cube of material index.
use renderer.material instead
thanks for the reply, but waht you suggested resulted is this error:(you meant changing the reflection script to renderer.material, right?)
"Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.shared$$anonymous$$aterial ins$$anonymous$$d. UnityEngine.Renderer:get_materials() RenderCubemapReflection:UpdateCubemap(Int32) (at Assets/RDScripts/RenderCubemapReflection.cs:61) RenderCubemapReflection:LateUpdate() (at Assets/RDScripts/RenderCubemapReflection.cs:30)"
but I guess your right about what's wrong though, I'm instanciating the material when I'm changing color, but the cube map only works on the original material... still don't know how to solve this though....
$$anonymous$$aybe I should get the cube map from the shared material and apply it tto the instanciated material it in the color changing script...
Sondre.S tell right.
However, "Instantiating material due to calling renderer.material during edit mode", means that you add [ExecuteInEdit$$anonymous$$ode] tag.
Do you really that script run in editor mode?
Didn't really understand what you said there, but I think you meant that it shouldnt run in the editor? I think I found the solution. The realtime refletion script is in the camera object, so by using gameObject.Find("camera").getComponentInChildren(); I was able to get the reflections and apply them to the instanciated material.
Thanks for pointing out that the cube map was applied only to the original material and not the instanciated material :)
Yes, if you tag a script with [ExecuteInEdit$$anonymous$$ode], you shouldn't modify material instances or mesh instances, but sharedmaterial and sharedmesh. (Unity tell us with " This will leak materials into the scene.") Of course, isn't always correct to obtain wanted results.
Your answer
Follow this Question
Related Questions
How does Unity handle alpha sorting? 2 Answers
Help with Texture following Camera!/How to create an effect like this in Unity 3d? 1 Answer
On Cam move: mesh renderer object disapears on ios but not in editor 1 Answer
Changing the global fog (image effect) color from a different script, possible? 2 Answers
How can I make a character disappear from view from one side to the other? 1 Answer