Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by vijaykiran · Aug 12, 2016 at 02:36 PM · shader writingprefab changing at runtime

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

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

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


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