So I’m using the Standard Surface Shader, and I’m trying to procedurally generate a smoothness and metallic map while the shader runs. Every time I try to create a line like this:
Texture2D smoothTex = new Texture2D(32, 32);
(For context this is inside of the surf method)
I get an error in the console saying:
" Shader error in ‘Custom/Reveal Shader’: Unexpected identifier “smoothTex”. Expected one of ‘,’ ‘;’ at line 50
Am I just creating the texture wrong, or is it even possible to make a texture inside of the shader?
And if it is possible to create the texture, how would I edit it inside of the shader, based on the world position.
Also, how would I get the width and height of a pre existing texture, like the _MainTex? I assume it would be something like:
_MainTex.width;
or
_MainTex.getWidth();
Any help would be greatly appreciated, and I’ve posted the full code below if you need to see more of it.
Shader "Custom/Reveal Shader"
{
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
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
float3 worldPos : TEXCOORD2;
float3 viewDir;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void vert(inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input, o);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
}
uniform float4 _arrayPosition[1000];
uniform float _arrayStrength[1000];
int _arrayLength;
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
float4 color = float4(0, 0, 0, 0);
Texture2D smoothTex = new Texture2D(32, 32);
for (int i = 0; i < _arrayLength; i++) {
float dist = distance(IN.worldPos, float4(_arrayPosition[i].xyz, 1.0));
if (dist < 10)
{
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
}
o.Metallic = 0;
o.Smoothness = 1;
}
ENDCG
}
FallBack "Diffuse"
}