From 02b0b40b1cf93624b4eaa78bdc5c8863eedda0eb Mon Sep 17 00:00:00 2001 From: Haroon Qureshi Date: Wed, 25 Mar 2026 06:10:11 -0700 Subject: [PATCH] Add reflective materials. A future CL will implement reflective surfaces and make use of these materials. PiperOrigin-RevId: 889200524 Change-Id: Icbe5c0c390f89789ed0f5d5c6058a2ef31105608 --- src/experimental/filament/CMakeLists.txt | 4 ++ .../filament/assets/phong_2d_fade.mat | 2 +- .../filament/assets/phong_2d_reflect.mat | 58 +++++++++++++++++++ .../filament/assets/phong_2d_uv_fade.mat | 2 +- .../filament/assets/phong_2d_uv_reflect.mat | 50 ++++++++++++++++ .../filament/assets/phong_color_reflect.mat | 43 ++++++++++++++ .../filament/assets/phong_cube_fade.mat | 2 +- .../filament/assets/phong_cube_reflect.mat | 57 ++++++++++++++++++ .../filament/filament/object_manager.cc | 4 ++ .../filament/filament/object_manager.h | 4 ++ src/experimental/studio/index.html | 17 ------ 11 files changed, 223 insertions(+), 20 deletions(-) create mode 100644 src/experimental/filament/assets/phong_2d_reflect.mat create mode 100644 src/experimental/filament/assets/phong_2d_uv_reflect.mat create mode 100644 src/experimental/filament/assets/phong_color_reflect.mat create mode 100644 src/experimental/filament/assets/phong_cube_reflect.mat diff --git a/src/experimental/filament/CMakeLists.txt b/src/experimental/filament/CMakeLists.txt index 8d720365..7866dd16 100644 --- a/src/experimental/filament/CMakeLists.txt +++ b/src/experimental/filament/CMakeLists.txt @@ -102,12 +102,16 @@ set(MATERIAL_FILES pbr_packed.mat phong_2d_fade.mat phong_2d.mat + phong_2d_reflect.mat phong_2d_uv_fade.mat phong_2d_uv.mat + phong_2d_uv_reflect.mat phong_color_fade.mat phong_color.mat + phong_color_reflect.mat phong_cube_fade.mat phong_cube.mat + phong_cube_reflect.mat unlit_depth.mat unlit_line.mat unlit_segmentation.mat diff --git a/src/experimental/filament/assets/phong_2d_fade.mat b/src/experimental/filament/assets/phong_2d_fade.mat index b1bf004f..15c18d73 100644 --- a/src/experimental/filament/assets/phong_2d_fade.mat +++ b/src/experimental/filament/assets/phong_2d_fade.mat @@ -13,7 +13,7 @@ // limitations under the License. material { - name : phong_2d, + name : phong_2d_fade, shadingModel : specularGlossiness, culling : none, flipUV : false, diff --git a/src/experimental/filament/assets/phong_2d_reflect.mat b/src/experimental/filament/assets/phong_2d_reflect.mat new file mode 100644 index 00000000..94dd2a0e --- /dev/null +++ b/src/experimental/filament/assets/phong_2d_reflect.mat @@ -0,0 +1,58 @@ +// Copyright 2025 DeepMind Technologies Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +material { + name : phong_2d_reflect, + shadingModel : specularGlossiness, + culling : none, + flipUV : false, + parameters : [ + { type : float4, name : BaseColorFactor }, + { type : float, name : SpecularFactor }, + { type : float, name : GlossinessFactor }, + { type : float, name : EmissiveFactor }, + { type : float, name : Reflectance }, + { type : float3, name : UvScale }, + { type : float3, name : UvOffset }, + { type : sampler2d, name : BaseColor }, + { type : sampler2d, name : Reflection } + ], + variables : [ + vertex_pos + ] +} + +vertex { + void materialVertex(inout MaterialVertexInputs material) { + material.vertex_pos = getPosition(); + } +} + +fragment { + void material(inout MaterialInputs material) { + vec2 uv = variable_vertex_pos.xy * materialParams.UvScale.xy + materialParams.UvOffset.xy; + vec2 screen_uv = gl_FragCoord.xy * getResolution().zw; + + prepareMaterial(material); + material.baseColor = materialParams.BaseColorFactor; + material.baseColor *= texture(materialParams_BaseColor, uv); + material.specularColor = vec3(materialParams.SpecularFactor); + material.glossiness = materialParams.GlossinessFactor; + material.emissive = vec4(materialParams.EmissiveFactor); + + // Apply reflection. + vec4 reflection = texture(materialParams_Reflection, screen_uv); + material.baseColor.rgb = mix(material.baseColor.rgb, reflection.rgb, materialParams.Reflectance); + } +} diff --git a/src/experimental/filament/assets/phong_2d_uv_fade.mat b/src/experimental/filament/assets/phong_2d_uv_fade.mat index 24c2834e..83464418 100644 --- a/src/experimental/filament/assets/phong_2d_uv_fade.mat +++ b/src/experimental/filament/assets/phong_2d_uv_fade.mat @@ -13,7 +13,7 @@ // limitations under the License. material { - name : phong_2d_uv, + name : phong_2d_uv_fade, shadingModel : specularGlossiness, culling : none, flipUV : false, diff --git a/src/experimental/filament/assets/phong_2d_uv_reflect.mat b/src/experimental/filament/assets/phong_2d_uv_reflect.mat new file mode 100644 index 00000000..51ace511 --- /dev/null +++ b/src/experimental/filament/assets/phong_2d_uv_reflect.mat @@ -0,0 +1,50 @@ +// Copyright 2025 DeepMind Technologies Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +material { + name : phong_2d_uv_reflect, + shadingModel : specularGlossiness, + culling : none, + flipUV : false, + parameters : [ + { type : float4, name : BaseColorFactor }, + { type : float, name : SpecularFactor }, + { type : float, name : GlossinessFactor }, + { type : float, name : EmissiveFactor }, + { type : float, name : Reflectance }, + { type : sampler2d, name : BaseColor }, + { type : sampler2d, name : Reflection } + ], + requires : [ + uv0 + ], +} + +fragment { + void material(inout MaterialInputs material) { + vec2 uv = getUV0(); + vec2 screen_uv = gl_FragCoord.xy * getResolution().zw; + + prepareMaterial(material); + material.baseColor = materialParams.BaseColorFactor; + material.baseColor *= texture(materialParams_BaseColor, uv); + material.specularColor = vec3(materialParams.SpecularFactor); + material.glossiness = materialParams.GlossinessFactor; + material.emissive = vec4(materialParams.EmissiveFactor); + + // Apply reflection. + vec4 reflection = texture(materialParams_Reflection, screen_uv); + material.baseColor.rgb = mix(material.baseColor.rgb, reflection.rgb, materialParams.Reflectance); + } +} diff --git a/src/experimental/filament/assets/phong_color_reflect.mat b/src/experimental/filament/assets/phong_color_reflect.mat new file mode 100644 index 00000000..80b7e09d --- /dev/null +++ b/src/experimental/filament/assets/phong_color_reflect.mat @@ -0,0 +1,43 @@ +// Copyright 2025 DeepMind Technologies Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +material { + name : phong_color_reflect, + shadingModel : specularGlossiness, + culling: none, + parameters : [ + { type : float4, name : BaseColorFactor }, + { type : float, name : SpecularFactor }, + { type : float, name : GlossinessFactor }, + { type : float, name : EmissiveFactor }, + { type : float, name : Reflectance }, + { type : sampler2d, name : Reflection } + ] +} + +fragment { + void material(inout MaterialInputs material) { + vec2 screen_uv = gl_FragCoord.xy * getResolution().zw; + + prepareMaterial(material); + material.baseColor = materialParams.BaseColorFactor; + material.specularColor = vec3(materialParams.SpecularFactor); + material.glossiness = materialParams.GlossinessFactor; + material.emissive = vec4(materialParams.EmissiveFactor); + + // Apply reflection. + vec4 reflection = texture(materialParams_Reflection, screen_uv); + material.baseColor.rgb = mix(material.baseColor.rgb, reflection.rgb, materialParams.Reflectance); + } +} diff --git a/src/experimental/filament/assets/phong_cube_fade.mat b/src/experimental/filament/assets/phong_cube_fade.mat index 3b95bec7..dec2b3af 100644 --- a/src/experimental/filament/assets/phong_cube_fade.mat +++ b/src/experimental/filament/assets/phong_cube_fade.mat @@ -13,7 +13,7 @@ // limitations under the License. material { - name : phong_cube, + name : phong_cube_fade, shadingModel : specularGlossiness, culling : none, flipUV : false, diff --git a/src/experimental/filament/assets/phong_cube_reflect.mat b/src/experimental/filament/assets/phong_cube_reflect.mat new file mode 100644 index 00000000..c4f02ac0 --- /dev/null +++ b/src/experimental/filament/assets/phong_cube_reflect.mat @@ -0,0 +1,57 @@ +// Copyright 2025 DeepMind Technologies Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +material { + name : phong_cube_reflect, + shadingModel : specularGlossiness, + culling : none, + flipUV : false, + parameters : [ + { type : float4, name : BaseColorFactor }, + { type : float, name : SpecularFactor }, + { type : float, name : GlossinessFactor }, + { type : float, name : EmissiveFactor }, + { type : float3, name : UvScale }, + { type : float, name : Reflectance }, + { type : samplerCubemap, name : BaseColor }, + { type : sampler2d, name : Reflection } + ], + variables : [ + vertex_pos + ] +} + +vertex { + void materialVertex(inout MaterialVertexInputs material) { + material.vertex_pos = getPosition(); + } +} + +fragment { + void material(inout MaterialInputs material) { + vec3 uv = variable_vertex_pos.xyz * materialParams.UvScale; + vec2 screen_uv = gl_FragCoord.xy * getResolution().zw; + + prepareMaterial(material); + material.baseColor = materialParams.BaseColorFactor; + material.baseColor *= texture(materialParams_BaseColor, uv); + material.specularColor = vec3(materialParams.SpecularFactor); + material.glossiness = materialParams.GlossinessFactor; + material.emissive = vec4(materialParams.EmissiveFactor); + + // Apply reflection. + vec4 reflection = texture(materialParams_Reflection, screen_uv); + material.baseColor.rgb = mix(material.baseColor.rgb, reflection.rgb, materialParams.Reflectance); + } +} diff --git a/src/experimental/filament/filament/object_manager.cc b/src/experimental/filament/filament/object_manager.cc index 28eee711..259816e6 100644 --- a/src/experimental/filament/filament/object_manager.cc +++ b/src/experimental/filament/filament/object_manager.cc @@ -89,12 +89,16 @@ ObjectManager::ObjectManager(const mjModel* model, filament::Engine* engine, materials_[kPbrPacked] = LoadMaterial("pbr_packed.filamat"); materials_[kPhong2d] = LoadMaterial("phong_2d.filamat"); materials_[kPhong2dFade] = LoadMaterial("phong_2d_fade.filamat"); + materials_[kPhong2dReflect] = LoadMaterial("phong_2d_reflect.filamat"); materials_[kPhong2dUv] = LoadMaterial("phong_2d_uv.filamat"); materials_[kPhong2dUvFade] = LoadMaterial("phong_2d_uv_fade.filamat"); + materials_[kPhong2dUvReflect] = LoadMaterial("phong_2d_uv_reflect.filamat"); materials_[kPhongColor] = LoadMaterial("phong_color.filamat"); materials_[kPhongColorFade] = LoadMaterial("phong_color_fade.filamat"); + materials_[kPhongColorReflect] = LoadMaterial("phong_color_reflect.filamat"); materials_[kPhongCube] = LoadMaterial("phong_cube.filamat"); materials_[kPhongCubeFade] = LoadMaterial("phong_cube_fade.filamat"); + materials_[kPhongCubeReflect] = LoadMaterial("phong_cube_reflect.filamat"); materials_[kUnlitSegmentation] = LoadMaterial("unlit_segmentation.filamat"); materials_[kUnlitLine] = LoadMaterial("unlit_line.filamat"); materials_[kUnlitDepth] = LoadMaterial("unlit_depth.filamat"); diff --git a/src/experimental/filament/filament/object_manager.h b/src/experimental/filament/filament/object_manager.h index d5734587..63c8de54 100644 --- a/src/experimental/filament/filament/object_manager.h +++ b/src/experimental/filament/filament/object_manager.h @@ -43,12 +43,16 @@ class ObjectManager { kPbrPacked, kPhong2d, kPhong2dFade, + kPhong2dReflect, kPhong2dUv, kPhong2dUvFade, + kPhong2dUvReflect, kPhongColor, kPhongColorFade, + kPhongColorReflect, kPhongCube, kPhongCubeFade, + kPhongCubeReflect, kUnlitSegmentation, kUnlitDepth, kUnlitLine, diff --git a/src/experimental/studio/index.html b/src/experimental/studio/index.html index 9a98df67..8eb3190f 100644 --- a/src/experimental/studio/index.html +++ b/src/experimental/studio/index.html @@ -48,23 +48,6 @@ onRuntimeInitialized: () => { // Define assets to prefetch. These paths are relative to the wasm_binary/ directory. const assetsToPrefetch = [ - "ibl.ktx", - "pbr.filamat", - "pbr_packed.filamat", - "phong_2d.filamat", - "phong_2d_uv.filamat", - "phong_color.filamat", - "phong_cube.filamat", - "unlit_line.filamat", - "unlit_ui.filamat", - "phong_2d_fade.filamat", - "phong_2d_uv_fade.filamat", - "phong_color_fade.filamat", - "phong_cube_fade.filamat", - "unlit_depth.filamat", - "unlit_segmentation.filamat", - "OpenSans-Regular.ttf", - "fontawesome-webfont.ttf", ]; const assetPromises = assetsToPrefetch.map(async (relativePath) => {