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 /
avatar image
0
Question by Hirnfroster · Aug 10, 2016 at 03:14 PM · shadermaterial

Accessing property of material with custom shader

Hey guys. I'm working on a mechanic to highlight objects when they are interactable in a 3D game. For this I'm currently using this shader: http://wiki.unity3d.com/index.php/Silhouette-Outlined_Diffuse

I had the idea that in every frame I would do a Raycast from the center of the screen. If it hits an object with the interactable-script(a script from me) attached to it and it is in range to interact, then I'd display "Interact" on the screen to tell the user that there is an interaction available. This mechanic works until here. Since I want to "highlight" it a little more I added this shader. Whenever I would display that "Interact Message" thing, I'd like to set the outline width(Property of the shader) to a value to "activate" the outlining. If I'm not in range or the Ray doesn't hit the object, the outline width is then 0.

I didn't write the shader and I'm fairly new to shaders. The real question is: How can I manipulate the "Outline width" of this material via script? I tried something with material.SetFloat("unknownNameOfProperty", float); I also read on the internet that I have to use activateKeyword("unknownNameOfProperty"); For the string that I passed in these functions I tried many things like "Outline width", "outline width", "_Outline_Width" and many many more. I just can't figure out how to do this.

If you guys could help me, I'd be really thankful :) Answers preferebly in C# but JavaScript would be okay I guess. And if you have another, maybe more efficient, method for this problem please tell me :D

Thank you in advance :D

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Yujingping · Aug 10, 2016 at 03:40 PM

Hello, here is my example code.

 using UnityEngine;
 using System.Collections;
 
 public class CharacterOnInteraction : MonoBehaviour
 {
     public void SetOutLineWidth (float width)
     {
         material.SetFloat ("_Outline", width);
     }
 }

What troubles you is the fact that "Outline Width" is the DESCRIPTION of the variable _Outline, and thus calling it doesn't make any sense. "_Outline" is what should be used.

You may use material.SetFloat ("varName", value); to change the UNIFORM Variables in your shader. You can also set other variable types such as Color (Official Document Here) and vector and so on. You may simply check the online manual for more information. All relevant info about Material

Please feel free for further questions and I am willing to be of help.

BTW honestly speaking, the shader that you have utilized doesn't react appropriately with models that have sharp normal direction changes (Such as Cube). Hence I recommend you to take a reference at my solution here.

GL & HF!

Comment
Add comment · Show 2 · 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 Yujingping · Aug 10, 2016 at 03:43 PM 1
Share

Well it is actually not very correct to say "uniform variables" since even if you don't declare them as "uniform" they could still be controlled from extern environments. Anyway I think you get the idea.

avatar image Hirnfroster · Aug 10, 2016 at 06:05 PM 0
Share

Thank you Yujingping! That really helped me out! Big thanks from me to you :D I'll probably take your "solution way" ins$$anonymous$$d of $$anonymous$$e. :D

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

Material doesn't have a color property '_Color' 4 Answers

One outline material for multiple objects? 2 Answers

Changing material color´s alpha in self iluminated shader 1 Answer

Lens Flare & Auto Hide 0 Answers

Pixel snap for material made with Unlit Shader Graph? 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