Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sondre-S · Feb 14, 2015 at 09:44 PM · cameramaterialdisappearsetcolorrealtime reflection

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);
     }
 }
 
 
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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<>();

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sondre-S · Feb 16, 2015 at 06:01 PM 0
Share

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....

avatar image Sondre-S · Feb 16, 2015 at 09:01 PM 0
Share

$$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...

avatar image giulio-pierucci · Feb 17, 2015 at 06:56 AM 0
Share

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?

avatar image Sondre-S · Feb 17, 2015 at 12:07 PM 0
Share

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 :)

avatar image giulio-pierucci · Feb 18, 2015 at 06:25 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges