Jump to content
Unity Insider Forum

VoxelEngine Problem: VoxelDaten zum Shader


b4nj0

Recommended Posts

Hallo liebe community.

 

Ich bin relativ neu in der unity engine unterwegs und noch viel neuer in der shaderentwicklung.

 

Ich arbeite momentan an einer VoxelEngine bei der die Welt durch verschiedene Voxel Repräsentiert wird (Gras, Stein, etc. wie bei minecraft halt) und erzeuge pro chunk ein geglättetes Mesh.

 

Dem Algorithmus geschuldet stehen für das Mesh keine UV maps zu verfügung und ich bin daher auf Triplanares texturieren gekommen.

 

Ich kann jetzt für 1 chunk jeweils 3 texturen festlegen (1x für oben+unten, 1 für vorne+hinten, 1 für links+rechts)

 

Ich möchte aber für jeden Würfel im chunk die 3 texturen aus einem texturatlas wählen.

 

Ich habe es hinbekommen den shader soweit zu modifizieren, dass ich für den gesamten chunk die 3 texturen aus einem texturatlas wählen kann, jedoch nicht für jeden würfel einzeln.

 

Ich finde einfach keine möglichkeit die daten aus meinem 3dim. VoxelArray(unity script) welche festlegen wo welche würfeltypen sind in den shader zu bekommen, um dementsprechend die offsets für den texturatlas zu wählen.

 

hier erstmal mein shader:

 

Shader "TriplanarTutorial/Triplanar_Final"
{
Properties
{
 _DiffuseMap ("Diffuse Map ", 2D)  = "white" {}
 _DiffuseMap2 ("Diffuse Map ", 2D)  = "white" {}
 _DiffuseMap3 ("Diffuse Map ", 2D)  = "white" {}
 _TextureScale ("Texture Scale",float) = 1
 _TextureOffset ("Texture Offset",float) = 1
 _TriplanarBlendSharpness ("Blend Sharpness",float) = 1
}
SubShader
{
 Tags { "RenderType"="Opaque" }
 LOD 200
 CGPROGRAM
 #pragma target 3.0
 #pragma surface surf Lambert
 sampler2D _DiffuseMap;
 sampler2D _DiffuseMap2;
 sampler2D _DiffuseMap3;
 float _TextureScale;
 float _TriplanarBlendSharpness;
 float _TextureOffset;

 struct Input
 {
  float3 worldPos;
  float3 worldNormal;
  float2 uv_MainTex;
 };


 void surf (Input IN, inout SurfaceOutput o)
 {

  float2 offset1 = float2(0.0f,0.5f);
  float2 offset2 = float2(0.5f,0.5f);
  // Find our UVs for each axis based on world position of the fragment.
  half2 yUV = IN.worldPos.xz / _TextureScale;
  half2 xUV = IN.worldPos.zy / _TextureScale;
  half2 zUV = IN.worldPos.xy / _TextureScale;

  // Now do texture samples from our diffuse map with each of the 3 UV set's we've just made.
  half3 yDiff = tex2D (_DiffuseMap, frac(yUV ) * 0.25f + offset1);
  half3 xDiff = tex2D (_DiffuseMap2, frac(xUV) * 0.25f  + offset2);
  half3 zDiff = tex2D (_DiffuseMap3,  frac(zUV) * 0.25f + offset2);
  // Get the absolute value of the world normal.
  // Put the blend weights to the power of BlendSharpness, the higher the value,
		// the sharper the transition between the planar maps will be.
  half3 blendWeights = pow (abs(IN.worldNormal), _TriplanarBlendSharpness);
  // Divide our blend mask by the sum of it's components, this will make x+y+z=1
  blendWeights = blendWeights / (blendWeights.x + blendWeights.y + blendWeights.z);
  // Finally, blend together all three samples based on the blend mask.
  //o.Albedo = xDiff * blendWeights.x + yDiff * blendWeights.y + zDiff * blendWeights.z;
  o.Albedo = tex2D(_DiffuseMap , IN.uv_MainTex).rgb;
 }

 ENDCG
}
}

 

 

hier habe ich die beiden test offsets für den textur atlas einfach hard gecodet

 

float2 offset1 = float2(0.0f,0.5f);

float2 offset2 = float2(0.5f,0.5f);

 

 

Ich würde die aber gerne in abhängigkeit des Voxeltyps an der stelle von IN.worldPos in der eigendlichen VoxelWelt

Link zu diesem Kommentar
Auf anderen Seiten teilen

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Lädt...
×
×
  • Neu erstellen...