• 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
Question by Liaram · Mar 30, 2018 at 09:24 PM · shadershader programmingshader writing

Need a bit of help with shader. (Diffuse, Specular, Normal, CubeMap-Reflection, 2x Additive)

Right off the bat I know absolutely nothing about shaders, but I've managed to cobble together this legacy shader that almost does what I'm looking for. Maybe one of you shader wizzards out there would be kind enough to lend a hand. Thanks in advance.

What I'm trying to make is a legacy shader that has: Diffuse, Specular, Normal, CubeMap-Reflection and 2x Self-Ilum or Additive. All of which can be colored separately and have the strength values modified.

This Frankenstein attempt almost does the job except the Normal map is only visible in the CubeMap-Reflection unless I crank up the Normal map strength. But that makes the bump effect way too aggressive in the Reflecting parts.

 // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
 
 Shader "Legacy Shaders/Reflective/Bumped Specular AdditiveX2" {
 Properties {
     _Color ("Main Color", Color) = (1,1,1,1)
     _TintColor ("Tint Color", Color) = (1,1,1,1)
     _TintColor2 ("Tint Color2", Color) = (0,0.5,1,1)
     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
     _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
     _Shininess ("Shininess", Range (0.01, 1)) = 0.255
     _SpecPower ("Specular Power", Range (0.01, 20)) = 10
     _BumpPower ("Bump Power", Range (4, 0.1)) = 1
     _MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" {}
     _Cube ("Reflection Cubemap", Cube) = "" {}
     _BumpMap ("Normalmap", 2D) = "bump" {}
     _Illum ("Illumin (RGB)", 2D) = "black" {}
     _Illum2 ("Illumin2 (RGB)", 2D) = "black" {}
 }
      
 SubShader {
     Tags { "RenderType"="Opaque" }
     LOD 400
 CGPROGRAM
 #pragma surface surf BlinnPhong
 #pragma target 3.0
      
 sampler2D _MainTex;
 sampler2D _BumpMap;
 sampler2D _Illum;
 sampler2D _Illum2;
 samplerCUBE _Cube;
      
 float4 _Color;
 fixed4 _TintColor;
 fixed4 _TintColor2;
 float4 _ReflectColor;
 half _Shininess;
 fixed _SpecPower;
 fixed _BumpPower;
      
 struct Input {
     float2 uv_MainTex;
     float2 uv_BumpMap;
     float3 worldRefl;
     INTERNAL_DATA
 };
      
 void surf (Input IN, inout SurfaceOutput o) {
     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
     fixed4 illum = tex2D(_Illum, IN.uv_MainTex);
     fixed4 illum2 = tex2D(_Illum2, IN.uv_MainTex);
     fixed4 c = tex * _Color;
     o.Albedo = c.rgb;
 
     o.Gloss = tex.a * _SpecPower;
     o.Specular = _Shininess;
 
     fixed3 normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
     normal.z = normal.z * _BumpPower;
     o.Normal = normalize(normal);
 
     float3 worldRefl = WorldReflectionVector (IN, o.Normal);
     fixed4 reflcol = texCUBE (_Cube, worldRefl);
     reflcol *= tex.a * _SpecPower;
 
     o.Emission = reflcol.rgb * _ReflectColor.rgb + (((2.0f * (illum.rgb * _TintColor.a)) * _TintColor) + (1.93f * (illum2.rgb * _TintColor2.a) * _TintColor2));
     o.Alpha = reflcol.a * _ReflectColor.a;
 }
 ENDCG
 }
 
 FallBack "Legacy Shaders/Reflective/Bumped Specular"
 }

Comment

People who like this

0 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 Liaram · Apr 03, 2018 at 02:30 PM 0
Share

Anyone? It's only the last bit I need help with. Everything up to line 60 is correct. I just don't know how I'm supposed to add things up at the end there.

avatar image SunnyChow · Apr 12, 2018 at 10:51 AM -1
Share

in this case, i feel your shader is doing vertex based lighting instead of pixel-based lighting. I think BlinnPhong is the problem (it's the lighting function). It may be better if you start your modifying from a shader with standard lighting model. (Right Click: Create/Shader/Standard Surface shader). i don't have to research so i am not sure

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by MadeFromPolygons · Apr 12, 2018 at 09:28 AM

I dont think you need to normalize your normals. That is converting them to 0-1 space. Try removing the line that says:

 o.Normal = normalize(normal);
 
 instead write:
 
 o.Normal = normal;


And see if that helps?

Comment

People who like this

0 Show 0 · 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

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

126 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Plain-Color Voxel Game Lighting 0 Answers

Where do I put this shader code? Getting weird error? 1 Answer

Five colors gradient on sprite 1 Answer

Image effect shader not running 1 Answer

Shader Tiling Texture Overlapping between X and Z Axes 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