How to convert shaders in unity to babylon

This is a shader defined by unityfunction,I try to convert it to babylon

inline float4 TriplanarSampling26( sampler2D topTexMap, float3 worldPos, float3 worldNormal, float falloff, float2 tiling, float3 normalScale, float3 index )
{
float3 projNormal = ( pow( abs( worldNormal ), falloff ) );
projNormal /= ( projNormal.x + projNormal.y + projNormal.z ) + 0.00001;
float3 nsign = sign( worldNormal );
half4 xNorm; half4 yNorm; half4 zNorm;
xNorm = tex2Dlod( topTexMap, float4(tiling * worldPos.zy * float2(  nsign.x, 1.0 ), 0, 0) );
yNorm = tex2Dlod( topTexMap, float4(tiling * worldPos.xz * float2(  nsign.y, 1.0 ), 0, 0) );
zNorm = tex2Dlod( topTexMap, float4(tiling * worldPos.xy * float2( -nsign.z, 1.0 ), 0, 0) );
return xNorm * projNormal.x + yNorm * projNormal.y + zNorm * projNormal.z;
}

I convert float to vec,But I have encountered many problems
My questions:
Question1.
vec3 projNormal = ( pow( abs( worldNormal ), falloff ) );

ERROR:
BJS - [17:27:50]: Error: VERTEX SHADER ERROR: 0:17: ‘pow’ : no matching overloaded function found
ERROR: 0:17: ‘=’ : dimension mismatch
ERROR: 0:17: ‘=’ : cannot convert from ‘const mediump float’ to ‘highp 3-component vector of float’
ERROR: 0:20: ‘half4’ : undeclared identifier
ERROR: 0:20: ‘xNorm’ : syntax error

i changed “falloff” to vec3(falloff),Is this correct?

Question2:
half4 xNorm; half4 yNorm; half4 zNorm;

ERROR:
BJS - [17:32:06]: Error: VERTEX SHADER ERROR: 0:20: ‘half4’ : undeclared identifier
ERROR: 0:20: ‘xNorm’ : syntax error

i changed “half4” to vec4,Is this correct?

Question3:
xNorm = tex2Dlod( topTexMap, vec4(tiling * worldPos.zy * vec2( nsign.x, 1.0 ), 0, 0) );

ERROR:
BJS - [17:37:27]: Error: VERTEX SHADER ERROR: 0:23: ‘tex2Dlod’ : no matching overloaded function found
ERROR: 0:23: ‘=’ : dimension mismatch
ERROR: 0:23: ‘assign’ : cannot convert from ‘const mediump float’ to ‘highp 4-component vector of float’
ERROR: 0:24: ‘tex2Dlod’ : no matching overloaded function found
ERROR: 0:24: ‘=’ : dimension mismatch
ERROR: 0:24: ‘assign’ : cannot convert from ‘const mediump float’ to ‘highp 4-component vector of float’
ERROR: 0:25: ‘tex2Dlod’ : no matching overloaded function found
ERROR: 0:25: ‘=’ : dimension mismatch
ERROR: 0:25: ‘assign’ : cannot convert from ‘const mediump float’ to ‘highp 4-component vector of float’

Which method is used to convert tex2Dlod to babylon?

Question1 pow这个函数在unity是pow(type a,enType b),你可以pow(float3(2,2,2),3),但是webgl不行，webgl是pow(type a,type a)，你需要 pow( abs( worldNormal ), vec3(falloff));

Question2 half4 改成 vec4

Question3 xNorm = texture2D( topTexMap, tiling * worldPos.zy * vec2( nsign.x, 1.0 ) ).r;(可能会有更好的方法)

(Translated by Baidu Translate)
Because there are many differences between the rules of webgl and unity shader, the main reason is that the type of webgl is relatively strict.

Question1 pow is pow (type a, enType b) in unity, you can pow (float3 (2,2,2), 3), but webgl is not. webgl is pow (type a, type a), you need pow (abs (worldNormal), vec3 (falloff));

Question2 half is changed to vec4

Question3 xNorm = texture2D( topTexMap, tiling * worldPos.zy * vec2( nsign.x, 1.0 ) ).r; (There may be a better way)

Note that the webgl float type must have a decimal point. For example, vec3 (1,1,1) will report an error . should be vec3 (1., 1., 1.);

About Question2 @ Evgeni_Popov, Does webgl have any way to sampler texture with lod?
I notice that in Webgpu ocean, bbl use 3 materials to achieve lod sampler, could we sampler texture lod in one material?

The equivalent to tex2Dlod in Babylon is texture2DLodEXT and the lod value should be the 3rd parameter (note that this is not supported in WebGL1, only in WebGL2):

xNorm = texture2DLodEXT(topTexMap, tiling * worldPos.zy * vec2(nsign.x, 1.0), 0).r;
1 Like