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 McFreezer · Feb 23, 2015 at 12:57 PM · c#shadermaterialcgruntime-generation

Fragment Shader not applyable at runtime?

I have the following scenrario: A Script generates ShaderCode and it is assigned to a virtual Material. The ShaderCode contains a fragment Shader and is working, if I put the Code into a .shader file and assign it to a created Material file.

If I try to assign the pure Code as a string to a new Material (Material m = new Material(Code)), the compiler produces:

Shader error in 'mcfShaders/Diffuse': Parse error: syntax error at line 15

So something is wrong with the CGPROGRAM. Maybe the Mapping in vertexInput or fragmentInput? This errormessage is not really helping...

 Shader "mcfShaders/Diffuse" {
   Properties {
      _Color ("Main Color", Color) = (1,1,1,1)
   }
   SubShader {
     Tags { "RenderType"="Transparent" "Queue"="Transparent" "LightMode"="ForwardBase" }
 
     LOD 200
     ZWrite On
     Lighting On
     Blend SrcAlpha OneMinusSrcAlpha
     Cull Back
 
     Pass {
       CGPROGRAM
       #pragma exclude_renderers ps3 xbox360 flash
       #pragma fragmentoption ARB_precision_hint_fastest
       #pragma vertex vert addshadow fullforwardshadows
       #pragma fragment frag
 
       uniform float4 _Color;
 
       struct vertInput {
         float4 vertex : POSITION;
         float4 color : COLOR;
       };
 
       struct fragInput {
         float4 pos : SV_POSITION;
         float4 diff : COLOR0;
       };
 
       fragInput vert(vertInput i) {
         fragInput r;
         r.pos = mul(UNITY_MATRIX_MVP, i.vertex);
         r.diff = _Color;
         return r;
       }
 
       half4 frag( fragInput i ) : COLOR {
         return half4(i.diff);
       }
       ENDCG
     }
   }
   Fallback "Diffuse"
 }

Hope someone with knowledge finds this :)

Comment
Add comment · Show 2
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 Graham-Dunnett ♦♦ · Feb 23, 2015 at 12:57 PM 0
Share

I'm not an expert but I assume the Cg compiler isn't part of the run-time.

avatar image Nymisu · Feb 23, 2015 at 02:00 PM 0
Share

Shaders can't really be applied or changed at run-time without expensive, extensive, and difficult swapping. Consider subsituting by instantiating objects ins$$anonymous$$d.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jessespike · Feb 23, 2015 at 02:27 PM

http://docs.unity3d.com/ScriptReference/Material-ctor.html

From that page:

NOTE: Creating materials this way supports only simple shaders (fixed function ones). If you need a surface shader, or vertex/pixel shaders, you'll need to create shader asset in the editor and use that.

Comment
Add comment · Show 3 · 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 McFreezer · Feb 23, 2015 at 03:56 PM 0
Share

Thx for advice. I managed to do a work-around for implementing fragment shaders at runtime. $$anonymous$$y script now writes the code into a file which is loaded by Resources.Load. Then I call the $$anonymous$$aterial constructor with the Name of the shader. Works fine for what I am needing.

avatar image Jessespike · Feb 23, 2015 at 04:20 PM 1
Share

That works? Did you test in a build or only in editor?

avatar image McFreezer · Feb 23, 2015 at 04:25 PM 0
Share

I was a bit preoccupied about the solution. Seems like the file is written correctly, but the shader update happens only at the 2nd start. So i tried to use AssetDatabase, requiring UnityEditor. Still everything happens in Editor, not inside the build. Seems like there is no way to manually reimport an external file without using the Editor. Or is there?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Use multiple materials? 1 Answer

How to randomly change the color of a Shader Graph Material for only a specified GameObject, with C# script? 1 Answer

Create Staining effect 0 Answers

What keyword string should I add to enable/disable when changing shader properties ? 1 Answer

Multiple Cars not working 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