Hi guys, I have this script here which when activated changes the shader of all objects to be transparent. The problem though is that they stay transparent even after the script is deactivated. Is there an easy way to return them to their original shader after the script is deactivated?
Here is the script:
function Update () {
var hits : RaycastHit[];
hits = Physics.RaycastAll (transform.position, transform.forward, 9999);
// Change the material of all hit colliders
// to use a transparent Shader
for (var i = 0;i < hits.Length; i++) {
var hit : RaycastHit = hits*;*
var renderer = hit.collider.renderer;
if (renderer && this.GetComponentInChildren(aimmode).inScope == true) {
renderer.material.shader = Shader.Find(“Transparent/Diffuse”);
renderer.material.color.a = .9;
}
else if (renderer && this.GetComponentInChildren(aimmode).inScope == false) {
return;
}
}
}
3 Answers
3
Are you doing this shader replacement every Update? It’s very time consumming! I would separate in two funcitons: one to make objects transparent, and other to restore the original shaders:
var hits: RaycastHit[];
var shaders: Shader[];
function SetTransparent(){ // set objects to transparent
hits = Physics.RaycastAll (transform.position, transform.forward, 9999);
shaders = new Shader[hits.Length]; // create array of old shaders
var newShader = Shader.Find("Transparent/Diffuse"); // find new shader only once
for (var i = 0; i < hits.Length; i++) {
var hit : RaycastHit = hits*;*
*var renderer = hit.collider.renderer;*
*if (renderer && this.GetComponentInChildren(aimmode).inScope == true) {*
_shaders *= renderer.material.shader; // save original shader*_
_*renderer.material.shader = newShader;*_
_*renderer.material.color.a = .9;*_
_*}*_
_*else {*_
_shaders *= null; // ensure that non replaced shaders are null*_
_*if (renderer && this.GetComponentInChildren(aimmode).inScope == false) return;*_
_*}*_
_*}*_
_*}*_
_*function RestoreShaders(){ // restore all shaders*_
_*for (var j = 0; j < hits.Length; j++){*_
_*if (shaders[j]){ // restore shader, if modified*_
_*var hit : RaycastHit = hits[j];*_
_*var renderer = hit.collider.renderer;*_
_*renderer.material.shader = shaders[j];*_
_*}*_
_*}*_
_*}*_
_**_
Thanks aldonaletto. I am using your script now and the transparency part works but the RestoreShaders function isn’t being called. I used a debug.log to test it.
I would like the RestoreShaders function to be called after this part:
else {
shaders *= null; // ensure that non replaced shaders are null*
if (renderer && this.GetComponentInChildren(aimmode).inScope == false) return;
Ok here is the revised script:
var shadersRestored = true;
var hits: RaycastHit[];
var shaders: Shader[];
function Update(){
if(this.GetComponentInChildren(aimmode).inScope == true && shadersRestored == true)
SetTransparent();
else if(this.GetComponentInChildren(aimmode).inScope == false && shadersRestored == false)
RestoreShaders();
}
function SetTransparent(){ // set objects to transparent
hits = Physics.RaycastAll (transform.position, transform.forward, 9999);
shaders = new Shader[hits.Length]; // create array of old shaders
var newShader = Shader.Find("Transparent/Diffuse"); // find new shader only once
for (var i = 0; i < hits.Length; i++) {
var hit : RaycastHit = hits*;*
var renderer = hit.collider.renderer;
if (renderer) {
shaders = renderer.material.shader; // save original shader
renderer.material.shader = newShader;
renderer.material.color.a = .9;
shadersRestored = false;
Debug.Log(“Shaders NOT Restored!”);
}
else {
shaders = null; // ensure that non replaced shaders are null
}
}
}
function RestoreShaders(){ // restore all shaders
for (var j = 0; j < hits.Length; j++){
if (shaders[j]) { // restore shader, if modified
var hit : RaycastHit = hits[j];
var renderer = hit.collider.renderer;
renderer.material.shader = shaders[j];
shadersRestored = true;
Debug.Log(“Shaders Restored!”);
}
}
}
Now at this line: for (var j = 0; j < hits.Length; j++){ I am getting an error saying “Object reference not set to an instance of an object.” Can you go into a little more detail as to what I need to do? Also I know its not good to keep these in the Update function I will move it once I get this working.
That's correct. I have this script attached to a weapon so whenever the weapon is in scope I want SetTransparent to be called and when they leave scope I want RestoreShaders to be called.
– ObviouslyInsane