- Home /
incorrec number of arguments to numeric type constructor
their are four numbers in the float 4 but it still gives me an error. The error is at test.vertex = float4(p0 - w0, p0 + w0, p1 - w1, p1 + w1);
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/Geom_Grass" {
Properties{
_MainTex("Main Texture", 2D) = "white"{}
_Color("Color", Color) = (1,1,1,1)
_GrassHeight("Grass Height", float) = 3
_GrassWidth("Grass Width", float) = 0.1
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
_Gravity("Gravity", float) = 0
_Length("Length", float) = 3
_Width("Width", float) = .1
_Steps ("Steps", float) = 3
}
SubShader{
Tags{ "RenderType" = "Opaque" "LightMode" = "ForwardBase" }
LOD 200
Cull off
ZWrite off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 normal : NORMAL;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 normal : NORMAL;
float4 worldPosition: TEXCOORD1;
float4 vertex : POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.normal = v.normal;
o.worldPosition = mul(unity_ObjectToWorld, v.vertex).xyzw;
return o;
}
float _Length;
float _Width;
float _Gravity;
float _Steps;
// This struct is the input data from the geometry shader.
// Simply convert the data from the vertex shader to world position
struct geomInput
{
float4 pos : POSITION;
float4 nor : NORMAL;
};
[maxvertexcount(80)] // Don't mind this value for now
void geom(triangle v2f input[3], inout TriangleStream<v2f> stream)
{
// Because access the input data directly tend to make the code a mess, I usually repack everything in clean variables
float4 P1 = input[0].vertex;
float4 P2 = input[1].vertex;
float4 P3 = input[2].vertex;
float4 N1 = input[0].normal;
float4 N2 = input[1].normal;
float4 N3 = input[2].normal;
float4 P = (P1+P2+P3)/3.0f;
float4 N = (N1+N2+N3)/3.0f;
v2f test = (v2f)0;
float4 T = float4(normalize((P2-P1).xyz), 0.0f);
for( int i = 0; i < _Steps; i++ )
{
// Retrieve the normalized time along the strand.
// It will be used to interpolate all the data from the bottom to the top of the strand.
// t0 is the current step of the strand we are drawing.
// t1 is the next step to be drawn. It will be t0 at the next iteration.
// You could store its value for optimization.
float t0 = (float)i / _Steps;
float t1 = (float)(i+1) / _Steps;
// Make our normal bend down with gravity.
// The further we are on the strand, and the longer it is, the more it bends.
// We then normalize this new direction, and scale it by the length at the current iteration of the loop.
float4 p0 = normalize(N - (float4(0, _Length * t0, 0, 0) * _Gravity * t0)) * (_Length * t0);
float4 p1 = normalize(N - (float4(0, _Length * t1, 0, 0) * _Gravity * t1)) * (_Length * t1);
test.normal = input[i].normal;
test.uv = input[i].uv;
// Interpolate the width, and scale the lateral direction vector with it
float4 w0 = T * lerp(_Width, 0, t0);
float4 w1 = T * lerp(_Width, 0, t1);
test.vertex = float4(p0 - w0, p0 + w0, p1 - w1, p1 + w1);
stream.Append(test);
}
}
fixed4 frag(v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
float3 lightdir = float3(1, 1, 0);
// apply fog
float ndotl = dot(i.normal, normalize(lightdir));
return col * ndotl;
}
ENDCG
}
}
Fallback "Diffuse"
}
Answer by Bunny83 · Sep 02, 2017 at 12:01 AM
p0 and w0 are float4 variables. So you actually pass 4 float4 variables to the float4 constructor.
I'm not sure what this line is supposed to do, but currently you basically pass 16 float values to the constructor.
PS: your geometry shader is supposed to generate new triangles. However currently you just push a single vertex during one step. A triangle needs 3 vertices.
Also what's your input geometry looks like? This shader works on a triangle input and for each triangle in the input you have to create some sort of replacement geometry.
Your answer
Follow this Question
Related Questions
How to force the compilation of a shader in Unity? 5 Answers
Shader to a Camera 1 Answer
How do i make particles be under the Player? 0 Answers
Add shadow receiver to 3D text shader. 0 Answers
Access mesh for TextMesh or GuiText 2 Answers