How to make an object to glow in unity3d - unity3d

Hi I'm generating lots of spheres in a 3D world to represent objects and as I'm working with a black background I want the spheres to have a little glow around them, I read that It can be done with shaders but need a little more guindance, for example how to make the shader and where to put it, any help will be appreciated.

There is a video from this link:
https://www.youtube.com/watch?v=nZZ6MDY3JOk
it creates a rim around the object and a glow effect. Here is the shader but you should watch the video for learning purpose (and to give them that one view count for credit):
Shader "Custom/GlowShader"
{
Properties
{
_ColorTint("Color Tint", Color) = (1, 1, 1, 1)
_MainTex("Base (RGB)", 2D) = "white" {}
_BumpMap("Normal Map", 2D) = "bump" {}
_RimColor("Rim Color", Color) = (1, 1, 1, 1)
_RimPower("Rim Power", Range(1.0, 6.0)) = 3.0
}
SubShader {
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float4 color : Color;
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
};
float4 _ColorTint;
sampler2D _MainTex;
sampler2D _BumpMap;
float4 _RimColor;
float _RimPower;
void surf (Input IN, inout SurfaceOutput o)
{
IN.color = _ColorTint;
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * IN.color;
o.Normal = UnpackNormal(tex2D(_BumpMap,IN.uv_BumpMap));
half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow(rim, _RimPower);
}
ENDCG
}
FallBack "Diffuse"
}

Related

Is there a way to repeat texture on Unity 3D object without specifying amount of tiles?

I am trying to make two different objects with the same size of texture / same material, which should be repeating; Without making new material for every new object I create. Below is an example of what I am trying to achieve.
Material A on both objects should look the same, the cube on the right represents how it should look.
This is fixed and working!
I had to make a Unity shader, which uses world space instead of local space (default). This is the code of a shader I used:
Shader "Legacy Shaders/Diffuse - Worldspace" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Scale("Texture Scale", Float) = 1.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
fixed4 _Color;
float _Scale;
struct Input {
float3 worldNormal;
float3 worldPos;
};
void surf (Input IN, inout SurfaceOutput o) {
float2 UV;
fixed4 c;
if (abs(IN.worldNormal.x) > 0.5) {
UV = IN.worldPos.yz; // side
c = tex2D(_MainTex, UV* _Scale); // use WALLSIDE texture
}
else if (abs(IN.worldNormal.z) > 0.5) {
UV = IN.worldPos.xy; // front
c = tex2D(_MainTex, UV* _Scale); // use WALL texture
}
else {
UV = IN.worldPos.xz; // top
c = tex2D(_MainTex, UV* _Scale); // use FLR texture
}
o.Albedo = c.rgb * _Color;
}
ENDCG
}
Fallback "Legacy Shaders/VertexLit"
}
Fixed version:
Fixed
Sources of Knowledge I used:
Video tutorial by PushyPixels
Blocks of code taken from this blog
#derHugo set me on the right track, thank you!!

Does Unity3D custom shader work with its GPU skinning automatically?

Hi I'm writing my own shader in mobile, but was wondering if I will break the GPU skinning feature since it's using vertex shader also? My shader is as below:
Shader "Mobile Custom/Specular Map" {
Properties {
_ShininessColor("Shininess Color", Color) = (1,1,1,1)
_Shininess ("Shininess", Range (0.03, 1)) = 0.078125
_MainTex ("Base (RGB)", 2D) = "white" {}
_SpecMap ("Specular Map", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 250
Cull Back
CGPROGRAM
#pragma surface surf MobileBlinnPhong exclude_path:prepass nolightmap halfasview interpolateview noshadow nofog nometa nolppv noshadowmask
inline fixed4 LightingMobileBlinnPhong (SurfaceOutput s, fixed3 lightDir, fixed3 halfDir, fixed atten) {
fixed diff = max (0, dot (s.Normal, lightDir));
fixed nh = max (0, dot (s.Normal, halfDir));
fixed spec = pow (nh, s.Specular * 128) * s.Gloss;
fixed4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * atten;
UNITY_OPAQUE_ALPHA(c.a);
return c;
}
sampler2D _MainTex;
sampler2D _SpecMap;
uniform float4 _ShininessColor;
half _Shininess;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = tex.rgb;
o.Gloss = tex2D(_SpecMap, IN.uv_MainTex) * _ShininessColor;
o.Alpha = tex.a;
o.Specular = _Shininess;
}
ENDCG
}
FallBack "Mobile/VertexLit"
}
According to here, if using custom shader with instancing, we need to add the instancing shader in our custom shader. Does it apply for GPU skinning too?

In Unity Shaderlab, how can you animate towards a value using lerp and deltaTime

How can a Custom Standard Surface Shader be used to animate a value towards a target value?
For Example:
...
float targetAlpha; //Set by the property block, or a C# script
float currrentAlpha; //Used internally only.
void surf (Input IN, inout SurfaceOutputStandard o)
{
currrentAlpha = lerp(currrentAlpha, targetAlpha, unity_DeltaTime.x);
o.Alpha = currrentAlpha;
}
This code does not work but should demonstrate what the goal is: when targetAlpha is set, the shader will fade towards that value.
There are few ways this can be done. One of them is using built-in shader variables:
Shader "Custom/Test" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_TargetAlpha ("TargetAlpha", Range(0, 1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows alpha:fade
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
half _Alpha;
fixed4 _Color;
half _TargetAlpha;
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = lerp(o.Alpha, _TargetAlpha, clamp(_SinTime.w, 0, 1));
}
ENDCG
}
FallBack "Diffuse"
}
The shader in the answer above still works in Unity 2018.2. However, _SinTime oscillates between -1 and 1, while we want the range to be between 0 and 1. The code clamp(_SinTime.w, 0, 1) clamps onto the desired values, however the alpha stays at zero too long. So we need the absolute value, like so: abs(_SinTime.w).
The modified shader:
Shader "Custom/Test" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_TargetAlpha ("TargetAlpha", Range(0, 1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows alpha:fade
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
half _Alpha;
fixed4 _Color;
half _TargetAlpha;
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = lerp(o.Alpha, _TargetAlpha, abs(_SinTime.w));
}
ENDCG
}
FallBack "Diffuse"
}

Unity Shader: Need help creating texture layers that don't affect other layers

I'm trying to learn how to write shaders in Unity and am having trouble separating the color and alphas values from affecting the other textures(or layers).
In my shader I have 3 textures of letters (The backgrounds are made transparent in the unity texture settings for each).
I want to have a background color then I need 3 texture/layers on top of the background and be able to change the tint and alpha of each layer without affecting the background or any other layer.
also...
When I multiply the Color by the texture in the surf function, The texture tint changes but also does the transparent background. Why is the transparent backgrounds tint changing when it is transparent?
Properties{
_Background("BackroundColor", Color) = (1,1,1,1)
_Color("c1", Color) = (1,1,1,1)
_Color2("c2", Color) = (1,1,1,1)
_Color3("c3", Color) = (1,1,1,1)
_MainTex ("layer1", 2D) = "white" {}
_MainTex2 ("layer2", 2D) = "white" {}
_MainTex3 ("layer3", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque"}
CGPROGRAM
#pragma surface surf Lambert alpha
#pragma target 3.0
struct Input {
float2 uv_MainTex;
float2 uv2_MainTex2;
float2 uv3_MainTex3;
};
sampler2D _MainTex;
sampler2D _MainTex2;
sampler2D _MainTex3;
float4 _Background;
float4 _Color;
float4 _Color2;
float4 _Color3;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = _Background * (tex2D (_MainTex, IN.uv_MainTex).rgb*_Color) * (tex2D (_MainTex2, IN.uv2_MainTex2).rgb*_Color2) * (tex2D (_MainTex3, IN.uv3_MainTex3).rgb*_Color3);
o.Alpha = _Color.a * _Color2.a * _Color3.a;
}
ENDCG
}
Fallback "Diffuse"
Nice thing to begin the shaders with unity ;)
So here is my solution to your problem, you have 2 exposed textures. One is the background (MainTex) and one is one of your letter (overlay) and you can mix the alpha of the overlay with the variable Ratio_
Shader "Custom/Overlay" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Main Texture", 2D) = "white" {}
_Overlay ("Overlay Texture", 2D) = "white" {} // One texture of letter
_Ratio("Ratio", Range(0,1)) = 1
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Blend SrcAlpha OneMinusSrcAlpha
Lighting On
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
sampler2D _Overlay;
float _Ratio;
struct Input {
float2 uv_MainTex;
float2 uv_Overlay;
};
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 background = tex2D (_MainTex, IN.uv_MainTex) * _Color;
fixed4 finalFragment = tex2D (_Overlay, IN.uv_Overlay) * _Color;
o.Albedo = background.rgb * (1 -finalFragment.a*_Ratio) + finalFragment.rgb * _Ratio;
o.Alpha = background.a;
}
ENDCG
}FallBack "Diffuse"
}
Hope this will help :D

Unity 3D multiple UV sets

I have created an object that has the texture and an AO , they are on different UV sets in maya(with Layered Texture) and in maya the mash looks ok.
How do i achive the same effect in Unity3D?
I can not make unity use the 2nd UV set.
You'll need to write a shader that does this. Here's a very minimal example, but you'd probably need to have a more elaborate setup for things specular, etc.
Shader "Custom/twotex" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_AoTex ("AO (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
sampler2D _AoTex;
struct Input {
float2 uv_MainTex : TEXCOORD0;
float2 uv_AoTex : TEXCOORD1;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex.xy);
half4 ao = tex2D (_AoTex, IN.uv_AoTex.xy);
o.Albedo = c.rgb * ao.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}