Files
armorpaint/base/Sources/make_voxel.ts
T

112 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-03-09 14:52:33 +01:00
///if arm_voxels
function make_voxel_run(data: shader_context_t) {
let structure: vertex_struct_t = g4_vertex_struct_create();
g4_vertex_struct_add(structure, "pos", vertex_data_t.I16_4X_NORM);
g4_vertex_struct_add(structure, "nor", vertex_data_t.I16_2X_NORM);
g4_vertex_struct_add(structure, "tex", vertex_data_t.I16_2X_NORM);
let pipe_state: pipeline_t = data._.pipe_state;
pipe_state.input_layout = [structure];
2024-05-03 21:29:39 +02:00
2024-03-09 14:52:33 +01:00
data.vertex_elements = [
2024-05-03 21:29:39 +02:00
{
name: "pos",
data: "short4norm"
},
{
name: "nor",
data: "short2norm"
},
{
name: "tex",
data: "short2norm"
}
2024-03-09 14:52:33 +01:00
];
// ///if arm_skin
// let is_mesh: bool = raw.object.constructor == mesh_object_t;
// let skin: bool = is_mesh && cast(raw.object, mesh_object_t).data.geom.bones != null;
// if (skin) {
// g4_vertex_structure_add(structure, "bone", vertex_data_t.I16_4X_Normalized);
// g4_vertex_structure_add(structure, "weight", vertex_data_t.I16_4X_Normalized);
2024-03-22 14:17:49 +01:00
// array_push(data.raw.vertex_elements, { name: "bone", data: "short4norm" });
// array_push(data.raw.vertex_elements, { name: "weight", data: "short4norm" });
2024-03-09 14:52:33 +01:00
// }
// ///end
pipe_state.vertex_shader = g4_shader_from_source(make_voxel_source(), shader_type_t.VERTEX);
g4_pipeline_compile(pipe_state);
data.constants = [
2024-05-03 21:29:39 +02:00
{
name: "W",
type: "mat4",
link: "_world_matrix"
},
{
name: "N",
type: "mat3",
link: "_normal_matrix"
}
2024-03-09 14:52:33 +01:00
];
data._.constants = [
g4_pipeline_get_const_loc(pipe_state, "W"),
g4_pipeline_get_const_loc(pipe_state, "N")
];
data.texture_units = [
2024-05-03 21:29:39 +02:00
{
name: "texpaint_pack"
},
{
name: "voxels",
image_uniform: true
}
2024-03-09 14:52:33 +01:00
];
data._.tex_units = [
g4_pipeline_get_tex_unit(pipe_state, "texpaint_pack"),
g4_pipeline_get_tex_unit(pipe_state, "voxels")
];
}
function make_voxel_source(): string {
2024-03-14 15:31:22 +01:00
let ds: f32 = make_material_get_displace_strength();
2024-03-09 14:52:33 +01:00
///if krom_direct3d11
2024-03-22 14:17:49 +01:00
return "#define vec3 float3 \
uniform float4x4 W; \
uniform float3x3 N; \
Texture2D<float4> texpaint_pack; \
SamplerState _texpaint_pack_sampler; \
struct SPIRV_Cross_Input { float4 pos : TEXCOORD1; float2 nor : TEXCOORD0; float2 tex : TEXCOORD2; }; \
struct SPIRV_Cross_Output { float4 svpos : SV_POSITION; }; \
SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input) { \
SPIRV_Cross_Output stage_output; \
2024-04-25 22:04:43 +02:00
" + make_material_voxelgi_half_extents() + ")} \
2024-03-22 14:17:49 +01:00
stage_output.svpos.xyz = mul(float4(stage_input.pos.xyz, 1.0), W).xyz / voxelgiHalfExtents.xxx; \
float3 wnormal = normalize(mul(float3(stage_input.nor.xy, stage_input.pos.w), N)); \
float height = texpaint_pack.SampleLevel(_texpaint_pack_sampler, stage_input.tex, 0.0).a; \
stage_output.svpos.xyz += wnormal * height.xxx * float3(" + ds + ", " + ds + ", " + ds + "); \
stage_output.svpos.w = 1.0; \
return stage_output; \
}";
2024-03-09 14:52:33 +01:00
///else
2024-03-22 14:17:49 +01:00
return "#version 450 \
in vec4 pos; \
in vec2 nor; \
in vec2 tex; \
out vec3 voxpositionGeom; \
uniform mat4 W; \
uniform mat3 N; \
uniform sampler2D texpaint_pack; \
void main() { \
2024-04-25 22:04:43 +02:00
" + make_material_voxelgi_half_extents() + ")} \
2024-03-22 14:17:49 +01:00
voxpositionGeom = vec3(W * vec4(pos.xyz, 1.0)) / voxelgiHalfExtents; \
vec3 wnormal = normalize(N * vec3(nor.xy, pos.w)); \
float height = textureLod(texpaint_pack, tex, 0.0).a; \
voxpositionGeom += wnormal * vec3(height) * vec3(" + ds + "); \
}";
2024-03-09 14:52:33 +01:00
///end
}
///end