Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Nov 3, 2023
1 parent f8329b6 commit 3fda232
Showing 1 changed file with 197 additions and 0 deletions.
197 changes: 197 additions & 0 deletions exts/cesium.omniverse/mdl/cesium.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,88 @@ export float4 cesium_imagery_layer_float4(
return imagery_layer.valid ? imagery_layer.value : float4(0.0);
}

export int cesium_feature_id_int(
uniform int set = 0
int feature_id_set_0 = 0,
[[
anno::hidden(),
annotation_not_connectable()
]],
int feature_id_set_1 = 0,
[[
anno::hidden(),
annotation_not_connectable()
]],
int feature_id_set_2 = 0,
[[
anno::hidden(),
annotation_not_connectable()
]],
int feature_id_set_3 = 0,
[[
anno::hidden(),
annotation_not_connectable()
]],
int feature_id_set_4 = 0,
[[
anno::hidden(),
annotation_not_connectable()
]],
int feature_id_set_5 = 0,
[[
anno::hidden(),
annotation_not_connectable()
]],
int feature_id_set_6 = 0,
[[
anno::hidden(),
annotation_not_connectable()
]],
int feature_id_set_7 = 0,
[[
anno::hidden(),
annotation_not_connectable()
]],
int feature_id_set_8 = 0,
[[
anno::hidden(),
annotation_not_connectable()
]],
int feature_id_set_9 = 0
[[
anno::hidden(),
annotation_not_connectable()
]]
)
[[
anno::display_name("Cesium feature ID lookup int"),
anno::description("Returns the feature ID for the given feature ID set. Returns 0 the feature ID set does not exist."),
anno::author("Cesium GS Inc."),
anno::in_group("Cesium")
]]
{
const auto MAX_FEATURE_ID_SETS_COUNT = 10;

int[MAX_FEATURE_ID_SETS_COUNT] feature_id_sets(
feature_id_set_0,
feature_id_set_1,
feature_id_set_2,
feature_id_set_3,
feature_id_set_4,
feature_id_set_5,
feature_id_set_6,
feature_id_set_7,
feature_id_set_8,
feature_id_set_9,
);

if (set >= MAX_FEATURE_ID_SETS_COUNT) {
return 0;
}

return feature_id_set[set];
}

export material cesium_material(
color base_color_factor = color(1.0)
[[
Expand Down Expand Up @@ -220,6 +302,121 @@ float4 compute_base_color(
return base_color;
}

// Copied from gltf/pbr.mdl since it's not exported
::tex::wrap_mode convert_wrap_mode(gltf_wrapping_mode mode)
{
if (mode == clamp_to_edge)
return ::tex::wrap_clamp;
if (mode == mirrored_repeat)
return ::tex::wrap_mirrored_repeat;

return ::tex::wrap_repeat;
}

// Copied from gltf/pbr.mdl since it's not exported
float2 khr_texture_transform_apply(
float2 coord,
float2 offset,
float rotation,
float2 scale
)
{
// MDL expects the texture coordinate origin at the bottom left (gltf at top left)
// Assuming the renderer follows the MDL specification in which case the coordinates
// have been flipped either while loading the glTF geometry or while setting up the state.

// Undo the flipping for the transformation to get into the original glTF texture space.
coord = float2(coord.x, 1.0f - coord.y);

// first scale
coord = coord * scale;
// then rotate
float cos_rotation = ::math::cos(rotation);
float sin_rotation = ::math::sin(rotation);
coord = float2(cos_rotation * coord.x + sin_rotation * coord.y, cos_rotation * coord.y - sin_rotation * coord.x);
// then translate
coord = coord + offset;

// flip back
coord = float2(coord.x, 1.0f - coord.y);
return coord;
}

// Copied from gltf/pbr.mdl, lookup_float4 replaced with texel_float4
gltf_texture_lookup_value texel_fetch(
uniform texture_2d texture = texture_2d()
uniform int tex_coord_index = 0
uniform float2 offset = float2(0.0f, 0.0)
uniform float rotation = 0.0
uniform float2 scale = float2(1.0f, 1.0f)
uniform gltf_wrapping_mode wrap_s = repeat
uniform gltf_wrapping_mode wrap_t = repeat
)
{
gltf_texture_lookup_value tex_ret;
if (!tex::texture_isvalid(texture))
return tex_ret;

float3 tex_coord3 = state::texture_coordinate(tex_coord_index);
float2 tex_coord = khr_texture_transform_apply(
coord: float2(tex_coord3.x, tex_coord3.y),
offset: offset,
rotation: rotation,
scale: scale);

tex_ret.value = tex::texel_float4(
tex: texture,
coord: tex_coord,
wrap_u: convert_wrap_mode(wrap_s),
wrap_v: convert_wrap_mode(wrap_t));
tex_ret.valid = true;
return tex_ret;
}

export int cesium_internal_feature_id_texture_lookup(
// TODO: make constant
int[4] channels = int[](0, 0, 0, 0),
int channel_count = 1,
// gltf_texture_lookup inputs below
uniform texture_2d texture = texture_2d(),
uniform int tex_coord_index = 0,
uniform float2 offset = float2(0.0, 0.0),
uniform float rotation = 0.0,
uniform float2 scale = float2(1.0, 1.0),
uniform gltf_wrapping_mode wrap_s = repeat,
uniform gltf_wrapping_mode wrap_t = repeat
) [[ anno::hidden() ]] {
auto texel_value = texel_fetch(
texture: texture,
tex_coord_index: tex_coord_index,
offset: offset,
rotation: rotation,
scale: scale,
wrap_s: wrap_s,
wrap_t: wrap_t
);

if (!texel_value.valid) {
return 0;
}

// TODO: handle multiple channels and bitshifting stuff
return texel_value[channels[0]];
}

export int cesium_internal_feature_id_vertex_lookup(
uniform string attribute_name = "_FEATURE_ID_0"
) [[ anno::hidden() ]] {
// Even if all feature ids in a triangle are the same value, the primvar interpolation math can yield non integer
// results (e.g. 0.99999999 or 1.00000001), which when accessed as ints (i.e. floored) can cause variance across
// the triangle surface. The fix is to read the primvar as a float, round the value, and cast to int.
return int(round(::scene::data_lookup_float(attribute_name)));
}

export int cesium_internal_feature_id_index_lookup() [[ anno::hidden() ]] {
return int(round(::scene::data_lookup_float("vertexId")));
}

export gltf_texture_lookup_value cesium_internal_texture_lookup(*) [[ anno::hidden() ]] = gltf_texture_lookup();

export gltf_texture_lookup_value cesium_internal_imagery_layer_lookup(
Expand Down

0 comments on commit 3fda232

Please sign in to comment.