I’m trying to code a shader that uses two cubemaps, one for diffuse and one for vertex deformation. There going to be used on a quad sphere.
My shader skills are yet to be…refined shall we say. I can grasp the basic logic but I still need to get my head around how these things work…
From what I understand about shaders, I managed to combine and modify what I could to get this result…
Shader "Custom/Test"
{
Properties
{
_CubeDiffuse ("Cubemap Diffuse Map", CUBE) = "" {}
_CubeHeight ("Cubemap Height Map", CUBE) = "" {}
_Amount ("Extrusion Amount", Range(-1,1)) = 0.5 //PROBLEM2 - This should act as a multiplier for _CubeHeight rather than a dedicated setting.
}
SubShader
{
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input
{
float3 worldRefl; //PROBLEM 1 - I this this input is causing the diffuse to distort like a reflective cubemap would.
};
float _Amount; //PROBLEM2 - I need this 'Amount' to read the values from _CubeHeight not _Amount.
void vert (inout appdata_full v)
{
v.vertex.xyz += v.normal * _Amount;
}
samplerCUBE _CubeDiffuse;
void surf (Input IN, inout SurfaceOutput o)
{
o.Albedo = texCUBE (_CubeDiffuse, IN.worldRefl).rgb; //PROBEM 1 - It's applied here.
}
ENDCG
}
Fallback "Diffuse"
}
The diffuse part works, but it acts like a reflection cube map and distorts as the object rotates. And adjusting the ‘Amount’ value leads to every vertex moving the exact same amount as expected.
Problem1 - I need to solve the distortion issue.
Problem2 - I need the amount value to read from the greyscale value of the _CubeHeight Texture.
I’ve searched everywhere on the net for something similar and found this one shader which takes care of the diffuse but not the vertex deformation. And it also does not respond to lighting…
Shader "Custom/PlanetShader"
{
Properties
{
_CubeTex("Cubemap", CUBE) = "" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
samplerCUBE _CubeTex;
struct appdata_t
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 vertex : POSITION;
float3 texcoord : TEXCOORD0;
};
v2f vert(appdata_t v)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
OUT.texcoord = v.normal;
return OUT;
}
half4 frag(v2f IN) : COLOR
{
return texCUBE(_CubeTex, IN.texcoord);
}
ENDCG
}
}
FallBack "Diffuse"
}
Any help or guidance would be greatly appreciated.
Ok scratch that - spotted the definitions I was missing and have updated the comment.
– whydoidoitThanks, I'll try this out and let you know how it goes!
– Matthew_ScottNo such luck, I don't think it likes the 'normal' stuff, and it seems to throwing up at line 31. Hmm, it's for an FYP so, I guess I'll just have to work around it and write down other methods.
– Matthew_ScottThere's some platforms that don't support samplers in the vertex shader - that may go double for cube samplers...
– whydoidoitWell I guess you could distort the mesh by playing with the vertices - as it's not really changing right? That would be a performance boost too. So just need to figure out what the equivalent code is of that cube sampler and do it in the ordinary script. Presuming the vertices are evenly spaced it can't be beyond the wit of man to get the right value from the right place I would have thought. Happy to think it through with you.
– whydoidoit