Clip the region in Ellipse shape using ShaderLab in Unity - unity3d

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

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!!

Unity: Apply shader to object to curve its texture

I'm making a endless runner game in Unity, and I want to add a curved shader to it like in this tutorial https://youtu.be/6_e_GoWlZOo
But when I add that material to a object it replaces the old one. And when I add it as second one in the Mesh Renderer it doesn't affect the first one.
https://drive.google.com/open?id=1-kP4A0Xh3P9xVjrwjTiTPlh_WcHnBA1d
This is the shader code:
Shader "PPP/BendWorld"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Curvature("Curvature", Float) = 0.001
}
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert vertex:vert addshadow
uniform sampler2D _MainTex;
uniform float _Curvature;
struct Input
{
float uv_MainTex;
};
void vert(inout appdata_full v)
{
float4 worldSpace = mul(unity_ObjectToWorld, v.vertex);
worldSpace.xyz -= _WorldSpaceCameraPos.xyz;
worldSpace = float4(0.0f, (worldSpace.x * worldSpace.x) * -_Curvature, 0.0f, 0.0f);
v.vertex += mul(unity_WorldToObject, worldSpace);
}
void surf(Input IN, inout SurfaceOutput o)
{
half4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Mobile/Diffuse"
}
I want to curve the object and have a texture on it, is there any way to do this?
Regarding the texture issue: The texture looks broken because you have defined your uv_MainTex as a float instead of a float2. UV coordinates are two-dimensional - if you define it as a scalar, it will only use the U axis of the UVs.

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

Shader and OpenGL ES in Unity

I have a shader that "cuts" the 3D model depending on the position of plane(s) (it sets the side above the panel to transprent).
The shader works very well in editor. However, it does work on a mobile device lets call it A) on Android, but it does not work on a other device (lets call it B) also on Android.
Device A: Open GL ES 2.0 build 1.12#2701748 | colors are good, "cutting" is good
Device B: Open GL ES 2.0 build 16.05001 | colors are lost (pink), cutting does not work
Can someone give me a clue about how to find out what is making the shader not working?
Here is the shader code:
Shader "CrossSection/GenericThreePlanesBSP" {
Properties{
_Color("Color", Color) = (1,1,1,1)
_CrossColor("Cross Section Color", Color) = (1,1,1,1)
_MainTex("Albedo (RGB)", 2D) = "white" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
_PlaneNormal("Plane1Normal",Vector) = (0,1,0,0)
_PlanePosition("Plane1Position",Vector) = (0,0,0,1)
}
SubShader{
Tags{ "RenderType" = "Transparent" }
//LOD 200
Cull Back
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float3 worldPos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
fixed4 _CrossColor;
fixed3 _PlaneNormal;
fixed3 _PlanePosition;
float _RealArraySize;
float _MaxArraySize = 100;
float4 _Positions[100];
float4 _Normals[100];
bool checkVisability(fixed3 worldPos)
{
for(int i = 0 ; i <= _RealArraySize - 1 ; i++)
{
// Will check if the point's position is above the plane
// If true --> Hide the point
if (dot(worldPos - _Positions[i], _Normals[i]) > 0)
{
return false;
}
}
return true;
}
void surf(Input IN, inout SurfaceOutputStandard o) {
if (checkVisability(IN.worldPos))discard;
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
Cull Front
CGPROGRAM
#pragma surface surf NoLighting noambient
struct Input {
half2 uv_MainTex;
float3 worldPos;
};
sampler2D _MainTex;
fixed4 _Color;
fixed4 _CrossColor;
fixed3 _PlaneNormal;
fixed3 _PlanePosition;
float _RealArraySize;
float _MaxArraySize = 100;
float4 _Positions[100];
float4 _Normals[100];
bool checkVisability(fixed3 worldPos)
{
for(int i = 0 ; i <= _RealArraySize - 1 ; i++)
{
// Will check if the point's position is above the plane
// If true --> Hide the point
if (dot(worldPos - _Positions[i], _Normals[i]) > 0)
{
return false;
}
}
return true;
}
fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
{
fixed4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
void surf(Input IN, inout SurfaceOutput o)
{
if (checkVisability(IN.worldPos))discard;
o.Albedo = _CrossColor;
}
ENDCG
}
//FallBack "Diffuse"
}
Edit: SystemInfo.graphicsShaderLevel returns 30 for both devices.

Transparent Shadow Shader Unity5

I use the following shader in unity 4.6.2, but unfortunately it doesn't work in unity5. I have an object in an AR scene (vuforia 4) and I want to show the shadows on a plane which is under the object. That plane should be transparent and only show the shadows, like in the picture.
this is the shader which works in unity 4.6.2
Shader "TransparentShadowShader" {
Properties
{
_ShadowColor ("Shadow Color", Color) = (0,0,0,1)
}
Category {
Blend SrcAlpha OneMinusSrcAlpha
Lighting Off
Zwrite Off
LOD 200
SubShader
{
Tags { "RenderType"="Transparent" }
CGPROGRAM
#pragma surface surf Custom
struct Input {
float2 pos : POSITION;
};
uniform float4 _ShadowColor;
void surf(Input IN, inout SurfaceOutput o)
{
//Pass through shadow colour to lighting model
o.Albedo = _ShadowColor.rgb;
o.Alpha = _ShadowColor.a;
}
half4 LightingCustom(SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
half4 c;
//Inverse illumination - atten accounts for shadowing
c.rgb = s.Albedo.rgb * 1.0f-atten;
c.a = s.Alpha * 1.0f-atten;
return c;
}
ENDCG
}
}
Fallback "VertexLit", 2
}
this is what I am getting in unity5, using this shader