I know I should just start over from a frag/vert shader with the structs aI would but I just love hacking my way to an epiphany, learning through doing, or in my case failing until success!
Come on I think I’m only one epiphany away now!
Shader "Custom/NewGoop" {
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
_Displacement("Displacement", Range( 0, 200 )) = 9.3
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows vertex:vert addshadow
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
float3 noiseFunc;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
float3 _Col;
half _Displacement;
const float2 m = float2( 0.80, 0.60 );
float hash( float2 p )
{
float h = dot(p,float2(127.1,311.7));
return -1.0 + 2.0*frac(sin(h)*43758.5453123);
}
float noise( in float2 p )
{
float2 i = floor( p );
float2 f = frac( p );
float2 u = f*f*(3.0-2.0*f);
return lerp( lerp( hash( i + float2(0.0,0.0) ),
hash( i + float2(1.0,0.0) ), u.x),
lerp( hash( i + float2(0.0,1.0) ),
hash( i + float2(1.0,1.0) ), u.x), u.y);
}
float fbm( float2 p )
{
float f = 0.0;
f += 0.5000*noise( p ); p = m*p*2.02;
f += 0.2500*noise( p ); p = m*p*2.03;
f += 0.1250*noise( p ); p = m*p*2.01;
f += 0.0625*noise( p );
return f/0.9375;
}
float2 fbm2( in float2 p )
{
return float2( fbm(p.xy), fbm(p.yx) );
}
float3 map( float2 p )
{
p *= 2.9;
float f = dot( fbm2( 1.0*(0.5*_Time.y + p + fbm2(-0.5*_Time.y+1.0*(p + fbm2(4.0*p)))) ), float2(1.0,-1.0) );
float bl = smoothstep( -0.8, 0.8, f );
float ti = smoothstep( -1.0, 1.0, fbm(p) );
return lerp( lerp( float3(0.50,0.00,0.00),
float3(1.00,0.5,0.35), ti ),
float3(0.00,0.00,0.02), bl );
}
float3 makeMeNoise(float2 uv)
{
float2 p = 4 * uv;
float e = 0.0016;
float3 colc = map( p ); float gc = dot(colc,float3(0.333, 0.333, 0.333));
float3 cola = map( p + float2(e,0.0) ); float ga = dot(cola,float3(0.333, 0.333, 0.333));
float3 colb = map( p + float2(0.0,e) ); float gb = dot(colb,float3(0.333, 0.333, 0.333));
float3 nor = normalize( float3(ga-gc, e, gb-gc ) );
_Col = colc;
_Col += float3(0.1,0.1,8.3)*1.0*abs(3.0*gc-ga-gb);
_Col *= 0.5+0.2*nor.y*nor.y;
_Col += 0.05*nor.y*nor.y*nor.y;
float2 q = uv;
_Col *= pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.1);
//_Extract = _Col.r;// damnit??
return _Col;
}
float4 getNewVertPosition( float4 p )
{
return p;
}
void vert( inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input,o); // Init output
o.noiseFunc = makeMeNoise(v.vertex); // make noiseFunc
float3 nois = o.noiseFunc;
float d = nois.z * _Displacement;
v.vertex.xyz += v.normal * d;
float4 vertPosition = getNewVertPosition( v.vertex );
// calculate the bitangent (sometimes called binormal) from the cross product of the normal and the tangent
float4 bitangent = float4( cross( v.normal, v.tangent ), 0 );
// how far we want to offset our vert position to calculate the new normal
float vertOffset = 0.01;
float4 v1 = getNewVertPosition( v.vertex + v.tangent * vertOffset );
float4 v2 = getNewVertPosition( v.vertex + bitangent * vertOffset );
// now we can create new tangents and bitangents based on the deformed positions
float4 newTangent = v1 - vertPosition;
float4 newBitangent = v2 - vertPosition;
// recalculate the normal based on the new tangent & bitangent
v.normal = cross( newTangent, newBitangent );
v.vertex = vertPosition;
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
//return float4( col, 1.0 );
_Col *= IN.noiseFunc;
fixed4 c = _Color;
c.rgb = _Col;
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’ve set it up like the custom vertex data example, o.noiseFunc in mine is equivalent to customColour on this page: https://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html
I get down to surf and looks like I’m passing the value in the same way as the example but I’m not getting the colour come through. I suspect it’s 114, as I’m passing v.vertex instead of a uv value.
I’ve just had a crack at the frag/vert one:
Shader"Paintings/Base"{
Properties{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader{
Pass{
CGPROGRAM
#pragmavertexvert
#pragmafragmentfrag
#include "UnityCG.cginc"
struct appdata{
float4vertex : POSITION;
float2 uv : TEXCOORD0;
float4 noiseFunc : TEXCOORD2; //isthisright??
};
struct v2f
{
float2 uv : TEXCOORD0;
float4vertex : SV_POSITION;
float4 screenCoord : TEXCOORD1;
float4 noiseFunc : TEXCOORD2; //isthisright??
};
constfloat2 m = float2( 0.80, 0.60 );
float3 _Col;
half _Displacement;
float hash( float2 p )
{
float h = dot(p,float2(127.1,311.7));
return -1.0 + 2.0*frac(sin(h)*43758.5453123);
}
float noise( infloat2 p )
{
float2 i = floor( p );
float2 f = frac( p );
float2 u = f*f*(3.0-2.0*f);
returnlerp( lerp( hash( i + float2(0.0,0.0) ),
hash( i + float2(1.0,0.0) ), u.x),
lerp( hash( i + float2(0.0,1.0) ),
hash( i + float2(1.0,1.0) ), u.x), u.y);
}
float fbm( float2 p )
{
float f = 0.0;
f += 0.5000*noise( p ); p = m*p*2.02;
f += 0.2500*noise( p ); p = m*p*2.03;
f += 0.1250*noise( p ); p = m*p*2.01;
f += 0.0625*noise( p );
return f/0.9375;
}
float2 fbm2( infloat2 p )
{
returnfloat2( fbm(p.xy), fbm(p.yx) );
}
float3 map( float2 p )
{
p *= 2.9;
float f = dot( fbm2( 1.0*(0.5*_Time.y + p + fbm2(-0.5*_Time.y+1.0*(p + fbm2(4.0*p)))) ), float2(1.0,-1.0) );
float bl = smoothstep( -0.8, 0.8, f );
float ti = smoothstep( -1.0, 1.0, fbm(p) );
returnlerp( lerp( float3(0.50,0.00,0.00),
float3(1.00,0.5,0.35), ti ),
float3(0.00,0.00,0.02), bl );
}
float3 makeMeNoise(float2 uv)
{
float2 p = 4 * uv;
float e = 0.0016;
float3 colc = map( p ); float gc = dot(colc,float3(0.333, 0.333, 0.333));
float3 cola = map( p + float2(e,0.0) ); float ga = dot(cola,float3(0.333, 0.333, 0.333));
float3 colb = map( p + float2(0.0,e) ); float gb = dot(colb,float3(0.333, 0.333, 0.333));
float3 nor = normalize( float3(ga-gc, e, gb-gc ) );
_Col = colc;
_Col += float3(0.1,0.1,8.3)*1.0*abs(3.0*gc-ga-gb);
_Col *= 0.5+0.2*nor.y*nor.y;
_Col += 0.05*nor.y*nor.y*nor.y;
float2 q = uv;
_Col *= pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.1);
return _Col;
}
float4 getNewVertPosition( float4 p )
{
return p;
}
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
o.noiseFunc.xyz = makeMeNoise(o.uv);
float3 nois = o.noiseFunc.xyz;
float d = nois.x;
//v.vertex.xyz+=v.normal*d;
v.vertex.xyz += d;
//v.vertex.z+=_Displacement;
float4 vertPosition = getNewVertPosition( v.vertex );
////calculatethebitangent(sometimescalledbinormal)fromthecrossproductofthenormalandthe tangent
//float4bitangent=float4(cross(v.normal,v.tangent),0);
//
////howfarwewanttooffsetourvertpositiontocalculatethenew normal
//floatvertOffset=0.01;
//
//float4v1=getNewVertPosition(v.vertex+v.tangent*vertOffset);
//float4v2=getNewVertPosition(v.vertex+bitangent*vertOffset);
//
////nowwecancreatenewtangentsandbitangentsbasedonthedeformed positions
//float4newTangent=v1-vertPosition;
//float4newBitangent=v2-vertPosition;
//
////recalculatethenormalbasedonthenewtangent& bitangent
//v.normal=cross(newTangent,newBitangent);
v.vertex = vertPosition;
o.screenCoord.xy = ComputeScreenPos(o.vertex);
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
float2 uv = i.uv;
float3 col = i.noiseFunc;
//float3col=(1,1,1);
returnfloat4(col,1);
}
ENDCG
}
}
}
It compiles, haven’t tested it yet though!