Vertex displacement doesn't affect anything -- Unity - unity3d

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

Related

Masking 3D object with custom shader in Unity

I'm making a 2D mobile game but I want to use 3D objects in shop section. So, in the shop section, I have a scroll rect and a viewport. I can scroll and the mask component works as expected with 2D objects. But when I add 3D objects, the objects getting out of the mask. I researched the forums and I find a custom shader. When I apply the shader to the objects materials, it partially works. But the depth or shadows are not working properly. I know not much about shaders. You can take a look at this Image. Box on the right is the original one, box on the left is the one with custom shader.
My shader code is here;
Shader "Custom/NewSurfaceShader" {
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
_StencilComp("Stencil Comparison", Float) = 8
_Stencil("Stencil ID", Float) = 0
_StencilOp("Stencil Operation", Float) = 0
_StencilWriteMask("Stencil Write Mask", Float) = 255
_StencilReadMask("Stencil Read Mask", Float) = 255
_ColorMask("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags { "RenderType"="UI""Queue"="Transparent" }
LOD 200
ZTest[unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask[_ColorMask]
Stencil
{
Ref 1
Comp LEqual
Pass Keep
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
CGPROGRAM
#pragma surface surf Standard vertex:vert fullforwardshadows
#pragma target 3.0
#include "UnityUI.cginc"
struct Input
{
float2 uv_MainTex;
float4 worldPosition;
};
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.worldPosition = v.vertex;
}
sampler2D _MainTex;
half _Glossiness;
half _Metallic;
fixed4 _Color;
float4 _ClipRect;
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;
o.Alpha *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
#ifdef UNITY_UI_ALPHACLIP
clip(o.Alpha - 0.001);
#endif
}
ENDCG
}
FallBack "Diffuse"
}

Making a mesh partly semi-transparent and still receive correct shadows

I am creating some game like Minecraft, but not in a first person view, but rather in a top down view.
The problem is, as soon as you dig inside something, the player (obviously) can't be seen anymore. Because of that, I tried to make a shader that cuts a hole in a mesh, so that you can see where you are underground.
I'm really bad with shaders, but somehow I managed to code something like that.
As you can see here, the hole cuts also a hole in the shadow casting, although the actual ceiling should still be there.
This is the code:
Shader "Custom/NewSurfaceShader"
{
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
_Cutoff("Cutoff", Range(0, 1)) = 0.5
_PlayerPosition ("Player Position", Vector) = (0, 0, 0)
}
SubShader
{
Tags { "Queue"="Geometry" }
CGPROGRAM
#pragma surface surf Standard fullforwardshadows addshadow
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
fixed3 _PlayerPosition;
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
if (distance(IN.worldPos.xz, _PlayerPosition.xz) < 7 && IN.worldPos.y > _PlayerPosition.y)
{
clip(-1);
}
}
ENDCG
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
CGPROGRAM
#pragma surface surf Standard addshadow alpha:fade
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
fixed3 _PlayerPosition;
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Metallic = 0;
o.Smoothness = _Glossiness;
if (distance(IN.worldPos.xz, _PlayerPosition.xz) < 7 && IN.worldPos.y > _PlayerPosition.y)
{
o.Albedo *= 0;
o.Alpha = 0.2;
}
else
{
discard;
}
}
ENDCG
}
FallBack Off
}
Is there a way to cast a shadow without that see-through hole? And is there an overall better way to write such a shader somehow?
Would be grateful for any answers, cheers!

Optimizing spherical mask shader for mobile

I have used a shader that is very heavy for mobile performance. Can someone help me to improve it in order to get better performance?
I have noticed that my FPS with this shader goes from 60 to 30.
Shader "Custom/Sphered2"
{Properties
{
[HDR] _Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_ColorStrength("Color Strenth", Range(1,4)) = 1
_EmissionColor ("_EmissionColor Color", Color) = (1,1,1,1)
_EmissionTex ("_EmissionColor (RGB)", 2D) = "white" {}
_EmissionStrength("_EmissionColor Strenth", Range(0,4)) = 1
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
// _position ("World Position", Vector) = (0,0,0,0)
// _Radius ("Sphere Radius", Range(0,100)) = 0
//_Softness("Sphere Softness", Range(0,100)) = 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,_EmissionTex;
struct Input
{
float2 uv_MainTex;
float2 uv_EmissionTex;
float3 worldPos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color,_EmissionColor;
half _ColorStrength,_EmissionStrength;
uniform float4 GLOBALmask_position2;
uniform half GLOBALmask_Radius2;
uniform half GLOBALmask_Softness;
// 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 surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
half grayscale = (c.r + c.g + c.b) * 0.333;
fixed3 cgg = fixed3(grayscale,grayscale,grayscale);
fixed4 e = tex2D (_EmissionTex, IN.uv_EmissionTex) * _EmissionColor *_EmissionStrength;
half d = distance(GLOBALmask_position2,IN.worldPos);
half sum = saturate((d - GLOBALmask_Radius2)/ -GLOBALmask_Softness);
fixed4 lerpColor = lerp(fixed4(cgg,1),c * _ColorStrength,sum);
fixed4 lerpEmission = lerp(fixed4(0,0,0,0),e,sum);
o.Albedo =c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Emission = lerpEmission.rgb;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
I have used it like this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayCastMovePos : MonoBehaviour
{
public Transform playerTransform;
public float radius2,sm;
public Vector4 myPos2;
void Update()
{
myPos2= new Vector4 ( playerTransform.position.x ,
playerTransform.position.y, playerTransform.position.z, 0);
//radius =Mathf .Clamp ( ( myVisualizer.allRythem
[0]+myVisualizer.allRythem [1]+myVisualizer.allRythem [2]) * 170,2,1000);
// radius2 =Mathf .Clamp ( ( myVisualizer.allRythem
[3]+myVisualizer.allRythem [4]+myVisualizer.allRythem [5]) * 150,2,1000);
Shader.SetGlobalVector ("GLOBALmask_position2", myPos2);
Shader.SetGlobalFloat ("GLOBALmask_Radius2", radius2);
Shader.SetGlobalFloat ("GLOBALmask_Softness", sm );
}
}
I am new to shaders, but if there are any suggestions for glowing a sprite, I would be glad to hear it.
If this is for a sprite, then do you really need to use a surface shader? These include tons of overhead, like lightmapping support, shadow casting/receiving, forward light support, etc. I'm sure a vertex/fragment shader would work fine for your purposes. You get emission by simply adding extra color to the final output.
Here is how the body of Unity's sprite shader looks:
https://github.com/TwoTailsGames/Unity-Built-in-Shaders/blob/master/CGIncludes/UnitySprites.cginc

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?

Combining 2 shader scripts in Unity from the Asset Store

Im a student animation and learning to write c# scripts on my own. Im trying to combine 2 shader scripts from Unity into one: one scripts is a shading effect that lets objects fade due to a range of a gizmo.
You can find it in the Asset store here for details:
World Space Fading Effect
https://assetstore.unity.com/packages/vfx/shaders/world-space-fading-transitions-98207
And the other script adds a cutout from the alpha in the maintex and ignore the lighting settings of the object.
Fading script:
Shader "Fading/Surface/DissolveGlowEdited" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
//_Noise ("noise", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
//_spread ("dissolveSpread", Range(0,1)) = 1.0
_GlowIntensity("Glow Intensity", Range(0.0, 5.0)) = 1
_GlowScale("Glow Size", Range(0.0, 5.0)) = 1.0
_Glow("Glow Color", Color) = (1, 0, 0, 1)
_GlowEnd("Glow End Color", Color) = (1, 1, 0, 1)
_GlowColFac("Glow Colorshift", Range(0.01, 2.0)) = 0.5
_SectionColor ("Section Color", Color) = (1,0,0,1)
[Toggle] _inverse("inverse", Float) = 0
[Toggle] _doubleSided ("doubleSided", Float) = 1
[Toggle(RETRACT_BACKFACES)] _retractBackfaces("retractBackfaces", Float) = 0
[Toggle(DISSOLVE_GLOW)] _glowdissolve("glowdissolve", Float) = 1
}
SubShader {
Tags { "RenderType"="Clipping" }
LOD 200
// ------------------------------------------------------------------
Cull off
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard addshadow vertex:vert
#pragma multi_compile __ FADE_PLANE FADE_SPHERE
#pragma shader_feature RETRACT_BACKFACES
#pragma shader_feature DISSOLVE_GLOW
#include "CGIncludes/section_clipping_CS.cginc"
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.5
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float3 worldPos;
float myface : VFACE;
};
half _BackfaceExtrusion;
void vert (inout appdata_full v) {
#if RETRACT_BACKFACES
float3 viewDir = ObjSpaceViewDir(v.vertex);
float dotProduct = dot(v.normal, viewDir);
if(dotProduct<0) {
float3 worldPos = mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1)).xyz;
float3 worldNorm = UnityObjectToWorldNormal(v.normal);
worldPos -= worldNorm * _BackfaceExtrusion;
v.vertex.xyz = mul(unity_WorldToObject, float4(worldPos, 1)).xyz;
}
#endif
}
half _Glossiness;
half _Metallic;
fixed4 _Color;
fixed4 _SectionColor;
fixed _doubleSided;
fixed4 _Glow;
fixed4 _GlowEnd;
half _GlowScale;
half _GlowColFac;
half _GlowIntensity;
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 glowCol = fixed4(0,0,0,0);
//if(IN.myface<0&&_doubleSided==0) discard;
#if (FADE_PLANE || FADE_SPHERE)&& DISSOLVE_GLOW
if(IN.myface<0&&_doubleSided==0) discard; else PLANE_CLIP(IN.worldPos);
float4 fade = PLANE_FADE(IN.worldPos);
//Combine texture factor with geometry coefficient from vertex routine.
half dFinal = fade.a;
//Shift the computed raw alpha value based on the scale factor of the glow.
//Scale the shifted value based on effect intensity.
half dPredict = (_GlowScale - dFinal) * _GlowIntensity;
//Change colour interpolation by adding in another factor controlling the gradient.
half dPredictCol = (_GlowScale * _GlowColFac - dFinal) * _GlowIntensity;
//Calculate and clamp glow colour.
glowCol = dPredict * lerp(_Glow, _GlowEnd, clamp(dPredictCol, 0.0f, 1.0f));
glowCol = clamp(glowCol, 0.0f, 1.0f);
#endif
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
if(IN.myface>0)
{
#if (FADE_PLANE || FADE_SPHERE)&& DISSOLVE_GLOW
glowCol = clamp(glowCol, 0.0f, 1.0f);
o.Emission = glowCol;
#endif
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
else
{
#if (FADE_PLANE || FADE_SPHERE)&& DISSOLVE_GLOW
glowCol.rgb = lerp(glowCol.rgb, 0.5*o.Albedo, dFinal);
glowCol = clamp(glowCol, 0.0f, 1.0f);
if(dFinal>1) glowCol.rgb = 0.5*o.Albedo;
o.Emission = glowCol;
#else
o.Emission = 0.5*o.Albedo;
#endif
o.Albedo = float3(0,0,0);
}
}
ENDCG
}
FallBack "Diffuse"
}
Flat shade / cutout script:
Shader "Unlit/Transparent Color CutoutTrans" {
Properties {
_Color ("Color Tint", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutoff"}
Pass {
Alphatest Greater [_Cutoff]
Lighting Off
SetTexture [_MainTex] {
constantColor [_Color]
combine texture * constant DOUBLE
}
}
}
}
The problem I have is if I combine it, it still read one of the scripts at the time, and not the two of them.
Does someone knows why and how it can be fixed? Many thanks!
If all what you want is to use the first shader but with alpha cutoff then according to the optional parameters section in the documentation you can do the following:
In the properties section in the 1st shader add:
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
Then, edit the line:
#pragma surface surf Standard addshadow vertex:vert
to be:
#pragma surface surf Standard addshadow vertex:vert alphatest:_Cutoff