• 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 sriram90 · Oct 21, 2013 at 02:28 PM · runtime

Changing shaders in runtime - ios

Hello guys,

Am facing some serious problems in unity ios when changing the shaders in runtime, I would like to change my material shader from Mobile/Bumped Diffuse to Self-Illumin/Diffuse and Toon/Basic Ouline. These dynamic changes working well in unity-editor, but not it in iOS.

here is my script,

 public Shader sh_mobileBumbedDiffuse = Shader.Find("Mobile/Bumped Diffuse");
 public Shader sh_toonBasicOutline = Shader.Find("Toon/Basic Outline") ;
 public Shader sh_selfIlluminDiffuse = Shader.Find("Self-Illumin/Diffuse");
 
 //Script when changing the shaders
 
 int i_materialsLength = go_bodyTransform.GetComponent<SkinnedMeshRenderer>().materials.Length;
 for(int i=0; i< i_materialsLength; i++)
 {
     if(i != 3)
         go_bodyTransform.GetComponent<SkinnedMeshRenderer>().renderer.materials[i].shader = sh_toonBasicOutline;
     else 
         go_bodyTransform.GetComponent<SkinnedMeshRenderer>().renderer.materials[i].shader = sh_selfIlluminDiffuse;
 }

its changing to toon shader, but outlines are not coming..even i tried to changing the shader script for outline color....Its throwing some errors like , -------- GLSL error: (\305\313<(\305\313<\211P\362

What it does mean ? and how can i resolve it ?

Thanks.

[Updated Shader]

Hi guys finally achieved and its working fine in iOS on runtime, I couldn't able to upload this shader file, so,here's the script.

 Shader "Custom/Outlined Diffuse" 
 { 
    Properties 
    { 
       _Color ("Main Color", Color) = (.5,.5,.5,1) 
       _OutlineColor ("Outline Color", Color) = (0,1,0,1) 
       _Outline ("Outline width", Range (0.002, 0.03)) = 0.01 
       _MainTex ("Base (RGB)", 2D) = "white" { } 
    } 
 
    SubShader 
    { 
       Tags { "RenderType"="Opaque" } 
       //Minor switch 
       UsePass "Toon/Basic/BASE" 
       //UsePass "Diffuse/BASE" 
       Pass 
       { 
          Name "OUTLINE" 
          Tags { "LightMode" = "Always" } 
           
          CGPROGRAM 
 // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
 #pragma exclude_renderers gles
 // Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without semantics (struct appdata members vertex,normal)
 #pragma exclude_renderers d3d11 xbox360
          #pragma vertex vert 
 
          struct appdata { 
              float4 vertex; 
              float3 normal; 
          }; 
 
          struct v2f { 
             float4 pos : POSITION; 
             float4 color : COLOR; 
             float fog : FOGC; 
          }; 
          uniform float _Outline; 
          uniform float4 _OutlineColor; 
 
          v2f vert(appdata v) { 
             v2f o; 
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 
             float3 norm = mul ((float3x3)UNITY_MATRIX_MV, v.normal); 
             norm.x *= UNITY_MATRIX_P[0][0]; 
             norm.y *= UNITY_MATRIX_P[1][1]; 
             o.pos.xy += norm.xy * o.pos.z * _Outline; 
     
             o.fog = o.pos.z; 
             o.color = _OutlineColor; 
             return o; 
          } 
          ENDCG 
           
          Cull Front 
          ZWrite On 
          ColorMask RGB 
          Blend SrcAlpha OneMinusSrcAlpha 
          //? -Note: I don't remember why I put a "?" here 
          SetTexture [_MainTex] { combine primary } 
       } 
    } 
     
    Fallback "Diffuse" 
 }
Comment
Add comment · Show 7
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 sriram90 · Oct 22, 2013 at 04:59 AM 0
Share

Any ideas guys ?

avatar image rutter · Oct 22, 2013 at 05:26 AM 0
Share

$$anonymous$$obile devices have limited capabilities. Unless you want to spend a lot of time researching and handling those limits, I suggest you stick to the recommended mobile shaders. Even then, there are plenty of pitfalls.

avatar image sriram90 · Oct 22, 2013 at 05:39 AM 0
Share

Otherwise is their any possibility to change materials at run time ? I have skinned mesh renderer with 5 materials, i couldn't swap all materials at run time...the first element of material only getting change. Is there any way to swap all materials ?

avatar image Kiloblargh · Oct 26, 2013 at 03:07 PM 0
Share

it's right here- if there's more than one material, you can access the materials[n] array. Switching materials is a better idea than changing the shader of a material anyway.

avatar image sriram90 · Oct 27, 2013 at 06:07 AM 0
Share

Hi @$$anonymous$$iloblargh , You're right. we can get the numbers of material, But we cant interchange material at run time, It'll always change the material for first element of array. Try once :)

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Export objects to a .3DS file at runtime 1 Answer

Model building in Unity with primitives and prefabs - opinions desired. 2 Answers

Tracking instantiated objects 1 Answer

prevLayer < state->GetLayer() printed when playing an animation 1 Answer

Remove object assignment in runtime? 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