Outline shader with curves and transparency - unity3d

I'm trying to create an outline shader, who has an edited outline.
At the moment it looks like this.
Shader "test/SimpleOutline" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline Width", Range (.002, 1.1)) = .005
}
SubShader {
Tags { "Queue"="Transparent" }
ZWrite off
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input {
float3 viewDir;
float2 uv_MainTex;
};
float _Outline;
float4 _OutlineColor;
void vert (inout appdata_full v) {
v.vertex.xyz += v.normal * _Outline;
}
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o)
{
o.Emission = float4(0, 1, 0,0);
}
ENDCG
ZWrite on
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
};
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
Fallback "Diffuse"
}
I want it like this.
It should be at a distance from the actual object
Transparent so you can see the background.
Completely circle each object and be arched.
Like this horn?
Does anyone have a suggestion on how best to implement this?

Hm, interesting question.
1. How to outline effect in distance from object?
I suggest stencil
Pass1: draw your mesh with no stencil
Pass2: draw mesh with MaskColor 0 with stencil writing some value
Pass3: draw outline mesh in solid color with stencil enabled with bigger "scale" than previous pass
somth like that.
Curving image. Feels like it should be done with techniques like in this video
https://www.youtube.com/watch?v=xH5uUfeB2Go try to apply it in Pass3.
P.S. I'm gonna try this effect myself in next couple of days if I would have time.

Related

Clip the region in Ellipse shape using ShaderLab in Unity

I am trying to draw an Ellipsoid using shaders in Unity
Following the steps
1. I have written a custom shader with the help of some internet tutorial
2. Added the shader on the material
3. Applied that material on the object
Shader "Custom/HoleMaker" {
Properties{
_Position("Position", Vector) = (0,0,0,0)
_Radius("Radius", Range(0,1)) = 0.01
_Color("Color", Color) = (1,1,1,1)
_CutawayColor("Cutaway Color", Color) = (1,1,1,1)
}
SubShader{
Tags { "RenderType" = "Opaque"}
LOD 200
Cull Front
CGPROGRAMtypes
#pragma surface surf MonoColor fullforwardshadows vertex:vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
fixed4 _CutawayColor;
float4 _Position;
float _Radius;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
v.normal *= -1;
}
half4 LightingMonoColor(SurfaceOutput s, half3 lightDir, half atten) {
return _CutawayColor;
}
void surf(Input IN, inout SurfaceOutput o)
{
//spherical clipping
float dist = distance(IN.worldPos.xyz, _Position.xyz);
if (dist < _Radius)
clip(-1);
o.Albedo = _CutawayColor.rgb;
}
ENDCG
}
FallBack "Diffuse"
}
This code is working for making a circle but when I try to use the function clip() to make an Ellipsoid, it is not working. According to the documentation, clip(float4 x) is also available to be used to clip the surface. The variable _Radius makes a perfect sphere in World Space but when I try to use _Radius.x and _Radius.y, the code does not work. Please refer to the image attached herewith.
_Radius.x and _Radius.y break the shader because _Radius is a float. It doesn't have x or y members. How could it possibly have a value for _Radius.y?
Instead, consider adding a Scale property, then scaling the difference between world position and _Position by that amount, before comparing the magnitude of the difference to _radius:
Shader "Custom/HoleMaker" {
Properties{
_Position("Position", Vector) = (0,0,0,0)
_Scale("Scale", Vector) = (1,1,1,0)
_Radius("Radius", Range(0,1)) = 0.01
_Color("Color", Color) = (1,1,1,1)
_CutawayColor("Cutaway Color", Color) = (1,1,1,1)
}
SubShader{
Tags { "RenderType" = "Opaque"}
LOD 200
Cull Front
CGPROGRAMtypes
#pragma surface surf MonoColor fullforwardshadows vertex:vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
fixed4 _CutawayColor;
float4 _Position;
float4 _Scale;
float _Radius;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
v.normal *= -1;
}
half4 LightingMonoColor(SurfaceOutput s, half3 lightDir, half atten) {
return _CutawayColor;
}
void surf(Input IN, inout SurfaceOutput o)
{
//spherical clipping
float dist = length(_Scale.xyz * IN.worldPos.xyz - _Position.xyz);
if (dist < _Radius)
clip(-1);
o.Albedo = _CutawayColor.rgb;
}
ENDCG
}
FallBack "Diffuse"
}

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"
}

How to reflect normals once they have been flipped in unity

I am trying to play a 360 video within a sphere. I am not an expert at unity or 3D modeling but I've managed to get the 360 video to invert its normals and play on the inside of the sphere.
The problem is the video has been flipped by the inversion (right is left and left is right). I need to correct this and despite the last three hours of searching I cannot find a solution that I understand. This is the code I've been using as a custom shader to achieve the inversion:
Shader "Custom/Flip Normals" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType" = "Opaque" }
Cull Off
CGPROGRAM
#pragma surface surf Lambert vertex:vert
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float4 color : COLOR;
};
void vert(inout appdata_full v) {
v.normal.xyz = v.normal * -1;
}
void surf (Input IN, inout SurfaceOutput o) {
fixed3 result = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = result.rgb;
o.Alpha = 1;
}
ENDCG
}
Fallback "Diffuse"
}
Note: messing with the camera or the video file itself are not ideal options.

Have 3d text shader be non-emissive?

In Unity, how can I stop my 3d texts from "glowing" in the dark?
I'm using below shader code, for instance, but the texts turn up much brighter/ self-emissive than other things. Changing to "Lighting On" gets rid of the glow, but will then also get rid of the material color, and turn up black.
How to solve this? Many thanks!
Shader "GUI/3D Text Shader - Cull Back" {
Properties {
_MainTex ("Font Texture", 2D) = "white" {}
_Color ("Text Color", Color) = (1,1,1,1)
}
SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Lighting Off Cull Back ZWrite Off Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass {
Color [_Color]
SetTexture [_MainTex] {
combine primary, texture * primary
}
}
}
Copied from comments, added for context:
I suspect the emissive glow is just the material's unlit color contrasting against a dark/variably lit scene? A lit shader could fix such an issue, however Unity's TextMesh component appears to not generate mesh normals and thus the shader's lighting calculations will corrupt (if at all be compiled). It explains why Lighting On outputs black. Without access to TextMesh internals and confined to the limitations of fixed-function shaders, a custom shader may be your only option.
The following is a basic Surface Shader using the Lambert lightning model. Under the hood it will compile to a standard vertex/fragment shader with Unity's lighting calculations automagically injected. It's been written as a drop-in replacement, so simply add it to your project and drag and drop it onto your material:
Shader "GUI/3D Text Shader - Lit"
{
Properties
{
_Color ("Text Color", Color) = (1,1,1,1)
_MainTex ("Font Texture", 2D) = "white" {}
}
SubShader
{
Tags {"RenderType"="Transparent" "Queue"="Transparent"}
Cull Back
CGPROGRAM
#pragma surface SS_Main Lambert vertex:VS_Main alpha:blend
#pragma target 3.0
uniform fixed4 _Color;
uniform sampler2D _MainTex;
struct Output
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 texcoord1 : TEXCOORD1;
float4 texcoord2 : TEXCOORD2;
};
struct Input
{
float2 uv_MainTex;
};
void VS_Main (inout Output o)
{
// Generate normals for lighting.
o.normal = float3(0, 0, -1);
}
void SS_Main (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = _Color.rgb;
o.Alpha = c.a;
}
ENDCG
}
}

unity custom shader not receiving Shadow

Shader "Custom/1_displayImageOnSlide" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_CutoffX("CutoffX", Range(0.0,1.0)) = 1
_CutoffY("CutoffY", Range(0.0,1.0)) = 1
}
Category {
Cull off
Blend SrcAlpha OneMinusSrcAlpha
Lighting Off
Fog { Mode Off }
SubShader {
// Tags {"LighMode"="ForwardBase"}
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
//UsePass "VertexLit/SHADOWCOLLECTOR"
//UsePass "VertexLit/SHADOWCASTER"
CGPROGRAM
#pragma surface surf Unlit
//#pragma surface surf Lambert
// Inside CGPROGRAM block.
half4 LightingUnlit (SurfaceOutput s, half3 lightDir, half atten) {
half4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float4 color : Color;
};
fixed _CutoffX;
fixed _CutoffY;
void surf (Input IN, inout SurfaceOutput o) {
half4 tex = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = IN.color.rgb * tex.rgb;
o.Alpha = IN.uv_MainTex.x > _CutoffX ? 0 : IN.uv_MainTex.y > _CutoffY ? 0 : IN.color.a*tex.a;
}
ENDCG
}//sub Shader End
}//Categoy End
FallBack "VertexLit" // Use VertexLit's shadow caster/receiver passes.
}
This is the code of my shader file and it is applied to a cube object. The problem is, shader is casting correctly by cube but cube is not receiving shader. Whats wrong in it? I don't have enough knowledge about shader programming so it is requested to share you answer with description