How to cover a cube with a (tiled) texture by code

Posted By: Nobby

How to cover a cube with a (tiled) texture by code - 09/30/18 13:17

I want to cover primitives like a cube as well as more complex meshes with one or more textures (skins) from external bitmap files (e.g. 128x128 pixel / 32 Bit color / tga format) by code NOT with an editor. Any ideas?

I tried different things like this but w/o success:

ent = ent_create(CUBE_MDL, vector(-100, 0, 0), NULL); //create primitive
ent_clone(ent); //clone primitive
set(ent, SHADOW | CAST); //enable shadow if lighted by spots
BMAP* skin = bmap_create("Brick_08.tga");
ent_setskin(ent, skin, 1);
Posted By: Superku

Re: How to cover a cube with a (tiled) texture by code - 09/30/18 19:04

You could use tri-planar texturing, something like the following (untested but should/ could work):

Code:
const float4x4 matWorldViewProj;
const float4x4 matWorld;
const float4 vecSunDir;
float4 vecSkill41;

texture entSkin1;

sampler ColorMapSampler = sampler_state 
{ 
   Texture = <entSkin1>; 
   AddressU  = Wrap; 
   AddressV  = Wrap; 
}; 
    
void DiffuseVS( 
   in float4 InPos: POSITION, 
   in float3 InNormal: NORMAL, 
   in float2 InTex: TEXCOORD0, 
   out float4 OutPos: POSITION, 
   out float3 OutTex: TEXCOORD0, 
   out float3 OutNormal: TEXCOORD1,
   out float3 OutNormal2: TEXCOORD2) 
{ 
   OutPos = mul(InPos, matWorldViewProj); 
   OutNormal = mul(InNormal, matWorld);
   OutNormal2 = InNormal;
   OutTex = InPos.xyz*vecSkill41.x+vecSkill41.yzw;
} 
    
float4 DiffusePS( 
   in float3 InTex: TEXCOORD0, 
   in float3 InNormal: TEXCOORD1,
   in float3 InNormal2: TEXCOORD2): COLOR 
{ 
	InNormal = normalize(InNormal);
   
   float Diffuse = saturate(dot(-vecSunDir, InNormal)*2)*0.5+0.5; 
   
   float4 Color = tex2D(ColorMapSampler, InTex.xy)*InNormal2.z*InNormal2.z; 
   Color += tex2D(ColorMapSampler, InTex.xz+0.333)*InNormal2.y*InNormal2.y; 
   Color += tex2D(ColorMapSampler, InTex.yz+0.667)*InNormal2.x*InNormal2.x; 
   
   float4 final = Color*Diffuse;
  
   return final; 
} 
 

technique DiffuseTechnique 
{ 
   pass P0 
   { 
      VertexShader = compile vs_2_0 DiffuseVS(); 
      PixelShader  = compile ps_2_0 DiffusePS(); 
   } 
}



entity.skill41 is the scaling of the texture, try for example
entity.skill41 = floatd(1.0,16.0); // for a cube with a width of 16 quants

You can add an offset such as
entity.skill42 = floatv(0.5);
entity.skill43 = floatv(0.5);
entity.skill44 = floatv(0.5);
which might result in better tiling/ fitting textures.
© 2024 lite-C Forums