Changing color values of prefab instances from within a shader
I have a game object tied to a C# script to instantiate a number of cube object (about 100, eventually there will be as many as 5000 cubes) prefabs as a child. The color of the cube objects should change based on the position of a capsule that I can move with a keyboard input. Within the C# script, I passed the position of the capsule to a shader using Material.SetVector(). The plan is to change the color of the cube objects using some metric I define in the shader. I assigned the shader to the child cube prefab as well as the parent game object.
When I run the above setup, I can only see the color of the parent game object change, but not the child cube prefabs. When I look at the inspector, the capsule position only appears to be updating for the parent game object but not the child prefab.
It seems unituitive to make every prefab pass the capsule position to the shader when the parent game object can pass it to the shader, but somehow make the values available for each cube prefab to change. I even tried doing it, but the framerate went down really bad.
I'm wondering how I should address this.
Thanks, Vijay.
C# code here:
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System.Collections.Generic;
public class PrefabInteriorPointDisplay : MonoBehaviour {
Vector3[] skinInteriorPoints;
public float verticesScaling = 0.01f;
private GameObject[] skinPrefabObjects;
Material myCoilPos;
Vector4 ColorValueToPassToShader;
// Use this for initialization
void Start () {
string filepath = Application.dataPath;
string filepath_append = "_interior_points.txt";
string skinfile = filepath + "skin" + filepath_append;
// ParseFileAndObtainInteriorPoints (skinfile, out skinInteriorPoints);
CreatePrefabMesh(out skinPrefabObjects, ref skinInteriorPoints, 1);
}
void ParseFileAndObtainInteriorPoints(string filename, out Vector3[] xyzvals)
{
// ...
}
void CreatePrefabMesh(out GameObject[] myprefabObject, ref Vector3[] interiorPoints, int prefabType)
{
string prefabTypeVal;
if (prefabType == 1) //use Cube
prefabTypeVal = "Cube Prefab";
else if (prefabType == 2) //use Sphere
prefabTypeVal = "Sphere Prefab";
else //use quad as default prefab
prefabTypeVal = "Quad Prefab";
myprefabObject = new GameObject[interiorPoints.Length];
for (int i = 0; i < interiorPoints.Length; i++)
{
myprefabObject[i] = Instantiate(Resources.Load(prefabTypeVal), interiorPoints[i], Quaternion.identity) as GameObject;
myprefabObject [i].transform.parent = gameObject.transform;
}
}
// Update is called once per frame
void Update () {
ColorValueToPassToShader = GameObject.FindGameObjectWithTag ("Coil").transform.position;
Debug.Log ("Color value: " + ColorValueToPassToShader);
myCoilPos.SetVector ("_CoilPos", ColorValueToPassToShader);
}
}
Shader code here:
Shader "vijay/My Colored Cube"
{
Properties{
_Color("Color", Color) = (0.0, 1.0, 1.0, 1.0)
_CoilPos("Coil Position", Vector) = (0.0, 1.0, 1.0, 0.0)
}
Category{
SubShader{
Pass{
LOD 200
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform float4 _CoilPos;
struct VertexInput {
float4 v : POSITION;
float4 color: COLOR;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float4 col : COLOR;
};
VertexOutput vert(VertexInput v) {
VertexOutput o;
float dist = distance(_CoilPos, v.v);
o.pos = mul(UNITY_MATRIX_MVP, v.v);
o.col = mul(float4(_dist, 0.0, 0.0, 0.0), v.v);
return o;
}
float4 frag(VertexOutput o) : COLOR{
return o.col;
}
ENDCG
}
}
}
}
Your answer

Follow this Question
Related Questions
Does adding more material properties slow down a shader? 0 Answers
How do I assign colours to specific vertices on a mesh and interpolate between them? 1 Answer
create Render Textures at runtime 1 Answer
Lag When Instantiating Particle System Prefab On Collision 0 Answers
Mask Shader Problem in Unity 0 Answers