Unity3D: My shader doesn't work well in GameView - unity3d

I wrote SurfaceShader with bumped texture.
I apply my material to a sphere. I can see the sphere in Scene View, but I can't see the sphere in Game View.
When I apply other shaders to the sphere, I can see the sphere in GameView.
I found the same question in Unity Community.
I understood I should use not Vertex Lighting but Pixel Lighting.
But I couldn't understand which settings should I change. Should I modify my shader or my settings?
My shader is the following.
Shader "Custom/MyShader2"{
Properties{
_Bump("Bump",2D) = "white"{}
_DiffuseColor("Diffuse Color", Color) = (1.0,1.0,1.0)
_Specular("Specular",Range(1.0,50.0)) = 15.0
_Alpha("Alpha",Range(0,1)) = 1
}
SubShader{
Tags{
"RenderType" = "Opaque"
}
CGPROGRAM
#pragma surface surf Original alpha
struct Input{
float4 color: COLOR;
float2 uv_Bump;
};
float3 _DiffuseColor;
float _Specular;
sampler2D _Bump;
float _Alpha;
half4 LightingOriginal( SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
half diff = max(0,dot(s.Normal,lightDir));
half spec = max(0,dot(s.Normal,normalize(lightDir + viewDir)));
spec = pow(spec, _Specular);
half trans = 1.0 - max(0,dot(s.Normal, viewDir)) + spec;
half4 c;
c. rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec / 2) * (atten * 2);
c.a = trans;
return c;
}
void surf( Input IN, inout SurfaceOutput o) {
o.Albedo = _DiffuseColor * _Alpha;;
o.Normal = UnpackNormal(tex2D(_Bump,IN.uv_Bump));
o.Alpha = _Alpha;
}
ENDCG
}
FallBack "Diffuse"
}
Thanks anyway.

Try to change
Tags { "RenderType" = "Opaque" }
with
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Cull Off ZWrite Off

Related

Vertex displacement doesn't affect anything -- Unity

I'm trying to implement a simulation of an ocean based on this tutorial but I can't get my shader to affect my plane. It stays completly flat whatever I do.
This is my shader :
Shader "Custom/Waves"
{
Properties
{
_Color ("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
_NormalMap ("Normal Map", 2D) = "bump" {}
//Wave Properties
_Direction("Direction", Vector) = (1.0, 0.0, 0.0, 1.0)
_Steepness("Steepness", Range(0.1,1.0)) = 0.5
_Freq("Frequency", Range(1.0,10.0)) = 1.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
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
float _Steepness, _Freq;
float4 _Direction;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void vert(inout appdata_full v){
float3 pos = v.vertex.xyz;
float4 dir = normalize(_Direction);
float defaultWavelength = 2 * UNITY_PI;
float wL = defaultWavelength / _Freq;
float phase = sqrt(9.8/wL);
float disp = wL * (dot(dir, pos) - (phase * _Time.y));
float peak = _Steepness/wL;
pos.x += dir.x * (peak * cos(disp));
pos.y = peak * sin(disp);
pos.z += dir.y * (peak * cos(disp));
v.vertex.xyz = pos;
}
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;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
I thought that it might be the link between the plane, the material and the shader but it doesn't seem to help. I don't have any idea of what I did wrong. I tried with and without GPU instancing and it does not change anything.
You forgot to declare that you want to use a vertex function.
Add this before or after the other pragma
#pragma vertex vert

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

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.

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

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