make_voxel.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ///if arm_voxels
  2. function make_voxel_run(data: shader_context_t) {
  3. let structure: vertex_struct_t = g4_vertex_struct_create();
  4. g4_vertex_struct_add(structure, "pos", vertex_data_t.I16_4X_NORM);
  5. g4_vertex_struct_add(structure, "nor", vertex_data_t.I16_2X_NORM);
  6. g4_vertex_struct_add(structure, "tex", vertex_data_t.I16_2X_NORM);
  7. let pipe_state: pipeline_t = data._.pipe_state;
  8. pipe_state.input_layout = [structure];
  9. data.vertex_elements = [
  10. {name: "pos", data: "short4norm"},
  11. {name: "nor", data: "short2norm"},
  12. {name: "tex", data: "short2norm"}
  13. ];
  14. // ///if arm_skin
  15. // let is_mesh: bool = raw.object.constructor == mesh_object_t;
  16. // let skin: bool = is_mesh && cast(raw.object, mesh_object_t).data.geom.bones != null;
  17. // if (skin) {
  18. // g4_vertex_structure_add(structure, "bone", vertex_data_t.I16_4X_Normalized);
  19. // g4_vertex_structure_add(structure, "weight", vertex_data_t.I16_4X_Normalized);
  20. // array_push(data.raw.vertex_elements, { name: "bone", data: "short4norm" });
  21. // array_push(data.raw.vertex_elements, { name: "weight", data: "short4norm" });
  22. // }
  23. // ///end
  24. pipe_state.vertex_shader = g4_shader_from_source(make_voxel_source(), shader_type_t.VERTEX);
  25. g4_pipeline_compile(pipe_state);
  26. data.constants = [
  27. { name: "W", type: "mat4", link: "_world_matrix" },
  28. { name: "N", type: "mat3", link: "_normal_matrix" }
  29. ];
  30. data._.constants = [
  31. g4_pipeline_get_const_loc(pipe_state, "W"),
  32. g4_pipeline_get_const_loc(pipe_state, "N")
  33. ];
  34. data.texture_units = [
  35. { name: "texpaint_pack" },
  36. { name: "voxels", image_uniform: true }
  37. ];
  38. data._.tex_units = [
  39. g4_pipeline_get_tex_unit(pipe_state, "texpaint_pack"),
  40. g4_pipeline_get_tex_unit(pipe_state, "voxels")
  41. ];
  42. }
  43. function make_voxel_source(): string {
  44. let ds: f32 = make_material_get_displace_strength();
  45. ///if krom_direct3d11
  46. return "#define vec3 float3 \
  47. uniform float4x4 W; \
  48. uniform float3x3 N; \
  49. Texture2D<float4> texpaint_pack; \
  50. SamplerState _texpaint_pack_sampler; \
  51. struct SPIRV_Cross_Input { float4 pos : TEXCOORD1; float2 nor : TEXCOORD0; float2 tex : TEXCOORD2; }; \
  52. struct SPIRV_Cross_Output { float4 svpos : SV_POSITION; }; \
  53. SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input) { \
  54. SPIRV_Cross_Output stage_output; \
  55. " + make_material_voxelgi_half_extents() + ")} \
  56. stage_output.svpos.xyz = mul(float4(stage_input.pos.xyz, 1.0), W).xyz / voxelgiHalfExtents.xxx; \
  57. float3 wnormal = normalize(mul(float3(stage_input.nor.xy, stage_input.pos.w), N)); \
  58. float height = texpaint_pack.SampleLevel(_texpaint_pack_sampler, stage_input.tex, 0.0).a; \
  59. stage_output.svpos.xyz += wnormal * height.xxx * float3(" + ds + ", " + ds + ", " + ds + "); \
  60. stage_output.svpos.w = 1.0; \
  61. return stage_output; \
  62. }";
  63. ///else
  64. return "#version 450 \
  65. in vec4 pos; \
  66. in vec2 nor; \
  67. in vec2 tex; \
  68. out vec3 voxpositionGeom; \
  69. uniform mat4 W; \
  70. uniform mat3 N; \
  71. uniform sampler2D texpaint_pack; \
  72. void main() { \
  73. " + make_material_voxelgi_half_extents() + ")} \
  74. voxpositionGeom = vec3(W * vec4(pos.xyz, 1.0)) / voxelgiHalfExtents; \
  75. vec3 wnormal = normalize(N * vec3(nor.xy, pos.w)); \
  76. float height = textureLod(texpaint_pack, tex, 0.0).a; \
  77. voxpositionGeom += wnormal * vec3(height) * vec3(" + ds + "); \
  78. }";
  79. ///end
  80. }
  81. ///end