ArCore Sceneform:检测图像时播放.mp4视频

2023-12-29

当我找到图像时,我想在其上方放置文本和视频。文本视图放置在场景上,但视频没有放置在场景中,它只是添加到中间的主布局中。 我正在使用组件 VideoView,我不确定这是问题所在

override fun onCreate(savedInstanceState: Bundle?) {
         (....)
        arFragment!!.arSceneView.scene.addOnUpdateListener { this.onUpdateFrame(it) }
        arSceneView = arFragment!!.arSceneView

}

private fun onUpdateFrame(frameTime: FrameTime) {
    val frame = arFragment!!.arSceneView.arFrame

    val augmentedImages = frame.getUpdatedTrackables(AugmentedImage::class.java)

    for (augmentedImage in augmentedImages) {
        if (augmentedImage.trackingState == TrackingState.TRACKING) {

            if (augmentedImage.name.contains("car") && !modelCarAdded) {
                renderView(arFragment!!,
                        augmentedImage.createAnchor(augmentedImage.centerPose))
                modelCarAdded = true
            }
        }
    }

}

text_info只是一个TextView组件,video_youtube是一个RelativeLayout,里面有VideoView。

   private fun renderView(fragment: ArFragment, anchor: Anchor) {
    //WORKING
    ViewRenderable.builder()
            .setView(this, R.layout.text_info)
            .build()
            .thenAccept { renderable ->
                (renderable.view as TextView).text = "Example"
                addNodeToScene(fragment, anchor, renderable, Vector3(0f, 0.2f, 0f))

            }
            .exceptionally { throwable ->
                val builder = AlertDialog.Builder(this)
                builder.setMessage(throwable.message)
                        .setTitle("Error!")
                val dialog = builder.create()
                dialog.show()
                null
            }
    //NOT WORKING
    ViewRenderable.builder()
            .setView(this, R.layout.video_youtube)
            .build()
            .thenAccept { renderable ->
                val view = renderable.view
                videoRenderable = renderable
                val path = "android.resource://" + packageName + "/" + R.raw.googlepixel
                view.video_player.setVideoURI(Uri.parse(path))
                renderable.material.setExternalTexture("videoTexture", texture)
                val videoNode = addNodeToScene(fragment, anchor, renderable, Vector3(0.2f, 0.5f, 0f))
                if (!view.video_player.isPlaying) {
                    view.video_player.start()
                    texture
                            .surfaceTexture
                            .setOnFrameAvailableListener {
                                videoNode.renderable = videoRenderable
                                texture.surfaceTexture.setOnFrameAvailableListener(null)
                            }
                } else {
                    videoNode.renderable = videoRenderable
                }

            }
            .exceptionally { throwable ->
                null
            }
}

private fun addNodeToScene(fragment: ArFragment, anchor: Anchor, renderable: Renderable, vector3: Vector3): Node {
        val anchorNode = AnchorNode(anchor)
        val node = TransformableNode(fragment.transformationSystem)
        node.renderable = renderable
        node.setParent(anchorNode)
        node.localPosition = vector3
        fragment.arSceneView.scene.addChild(anchorNode)
        return node
    }

我尝试使用色度键视频示例,但我不希望视频的白色部分是透明的。而且我不确定是否需要模型 (.sfb) 来显示视频。


我用的是色度键示例 https://github.com/google-ar/sceneform-android-sdk/tree/master/samples/chromakeyvideo作为起点。

首先,我通过添加一个标志来禁用色度过滤来更改用于视频的自定义材质。

material {
    "name" : "Chroma Key Video Material",
    "defines" : [
        "baseColor"
    ],
    "parameters" : [
        {
           // The texture displaying the frames of the video.
           "type" : "samplerExternal",
           "name" : "videoTexture"
        },
        {
            // The color to filter out of the video.
            "type" : "float4",
            "name" : "keyColor"
        },
        {
            "type" : "bool",
            "name" : "disableChromaKey",
        }
    ],
    "requires" : [
        "position",
        "uv0"
    ],
    "shadingModel" : "unlit",
    // Blending is "masked" instead of "transparent" so that the shadows account for the
    // transparent regions of the video instead of just the shape of the mesh.
    "blending" : "masked",
    // Material is double sided so that the video is visible when walking behind it.
    "doubleSided" : true
}

fragment {
    vec3 desaturate(vec3 color, float amount) {
        // Convert color to grayscale using Luma formula:
        // https://en.wikipedia.org/wiki/Luma_%28video%29
        vec3 gray = vec3(dot(vec3(0.2126, 0.7152, 0.0722), color));

        return vec3(mix(color, gray, amount));
    }

    void material(inout MaterialInputs material) {
        prepareMaterial(material);

        vec2 uv = getUV0();

        if (!gl_FrontFacing) {
          uv.x = 1.0 - uv.x;
        }

        vec4 color = texture(materialParams_videoTexture, uv).rgba;

        if (!materialParams.disableChromaKey) {
            vec3 keyColor = materialParams.keyColor.rgb;

            float threshold = 0.675;
            float slope = 0.2;

            float distance = abs(length(abs(keyColor - color.rgb)));
            float edge0 = threshold * (1.0 - slope);
            float alpha = smoothstep(edge0, threshold, distance);
            color.rgb = desaturate(color.rgb, 1.0 - (alpha * alpha * alpha));

            material.baseColor.a = alpha;
            material.baseColor.rgb = inverseTonemapSRGB(color.rgb);
            material.baseColor.rgb *= material.baseColor.a;
        } else {
            material.baseColor = color;
        }
    }
}

然后在 .sfa 文件中将 `disableChromaKey' 设置为 false:

 materials: [
    {
      name: 'DefaultMaterial',
      parameters: [
        {
          videoTexture: {
            external_path: 'MISSING_PATH',
          },
        },
        {
          keyColor: [
            0,
            0,
            0,
            0,
          ],
        },
        {
            disableChromaKey : true,
        }
      ],
      source: 'sampledata/models/chroma_key_video_material.mat',
    },
  ],

然后,我根据命中测试中的锚点放置视频节点,并在其上方放置一个 ViewRenderable 节点作为文本。

   private Node createVideoDisplay(final AnchorNode parent, Vector3 localPosition, String title) {
        // Create a node to render the video and add it to the anchor.
        Node videoNode = new Node();
        videoNode.setParent(parent);
        videoNode.setLocalPosition(localPosition);

        // Set the scale of the node so that the aspect ratio of the video is correct.
        float videoWidth = mediaPlayer.getVideoWidth();
        float videoHeight = mediaPlayer.getVideoHeight();
        videoNode.setLocalScale(
                new Vector3(
                        VIDEO_HEIGHT_METERS * (videoWidth / videoHeight),
                        VIDEO_HEIGHT_METERS, 1.0f));

        // Place the text above the video
        final float videoNodeHeight = VIDEO_HEIGHT_METERS+ localPosition.y;
        ViewRenderable.builder().setView(this,R.layout.video_title)
                .build().thenAccept(viewRenderable -> {
                   Node titleNode =  new Node();
                   titleNode.setLocalPosition(new Vector3(0,videoNodeHeight,0));
                   titleNode.setParent(parent);
                   titleNode.setRenderable(viewRenderable);
            ((TextView)viewRenderable.getView().findViewById(R.id.video_text))
                           .setText(title);
        });

        return videoNode;
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ArCore Sceneform:检测图像时播放.mp4视频 的相关文章

随机推荐

  • MOTODEV Studio 与 SDK 安装有关的问题

    我以前有 Java 编程经验 并且还接受过大学 Java 培训 因此我希望使用 Android 进行一些 Java 应用程序开发 为此 我从 Motorola 下载并安装了 MOTODEV studio 它部分基于 Eclipse 不过默认
  • 使用 Redirect 与 RewriteRule 的速度影响

    我很想知道 Apache 上 htaccess 规则中的 RewriteRules 和 Redirect 之间的速度是否有任何差异 在我看来 RewriteRules 通常可以是复杂的正则表达式 我认为与 Redirect 相比 它有开销
  • URI 太长时 Chrome 崩溃

    我正在为我的 HTML5 游戏制作一个导出功能 我当前的保存方法是游戏数据的粗略序列化 然后 this is Javascript var gameData abc this is actually a HUGE string of ove
  • 使用 PHP 从 Youtube 视频 URL 获取 Youtube 视频缩略图

    假设我有一个 YouTube 视频网址www youtube com watch v B4CRkpBGQzU feature youtube gdata par1 1 par2 2 我想获取视频缩略图 gt i3 ytimg com vi
  • Android Espresso IdlingResources 和片段/活动转换

    我有一个托管片段 F1 的活动 单击按钮后 F1 被另一个片段 F2 替换 当按下后退按钮时 应用程序通过一个按钮从 F2 返回到 F1退出过渡动画片 我的 Espresso 测试用例大致如下所示 Test public void pres
  • Django 和 MySQL unicode 错误

    我正在得到一个Incorrect string value Exception Value Incorrect string value xEA xB0 x95 xED x95 x98 for column object repr at r
  • 如何将存储过程的多个输出抓取到临时表中

    我的存储过程返回两个输出 我想在另一个存储过程中使用其中一个 因此尝试获取临时表中的第二个输出 但由于两个输出的结构不同 因此我总是得到 列名称或提供的值的数量与表定义不匹配 即使我更改输出的顺序 第一个输出第二个 第二个输出第一个 它也不
  • 如何在R中保存foreach循环的输出

    我在保存数据输出后遇到问题foreach loop 这是读取我的数据并处理它的函数 readFiles lt function x data lt read table filelist skip grep Begin Data Text
  • Java 中最终字符串的串联是如何完成的?

    当我编译这个片段时 public class InternTest public static void main String strings final String str1 str final String str2 ing Str
  • Netbeans 中的 Git 控制台

    我刚刚安装了 Netbeans 8 我想开始使用 Git 有很多按钮 菜单等 但在哪里可以找到 Git 控制台 我使用Windows 如果您想要控制台 您可能最好直接安装它 IDE 通常只提供菜单集成选项 Git 的网站有 Windows
  • Symfony2主义mysql IN查询

    我有一系列产品 ID 我必须像这样进行查询 SELECT FROM products WHERE pid IN 1 2 8 4 etc 我的 id 位于变量 pids 中 qb em gt createQueryBuilder query
  • 如何使用 Amazon AWS Elastic Beanstalk 安装 PHP 扩展?

    我们在 EC2 实例上的 PHP 应用程序中使用 aws elastic beanstalk 由于我们选择了负载平衡 它会不断地更改实例 我想知道如果我们安装 PHP 插件 它会受到实例更改的影响还是在新实例中也可用 提出这个问题是因为我们
  • macOS 上 CLion 中的 std::unordered_map

    我怎样才能看到元素std unordered map在 CLion 调试器中 有一些recipe https blog jetbrains com clion 2015 05 debug clion 怎么看std map元素 但它不适用于s
  • 添加两个 NSDate

    我有两个 NSDate 日期和时间 我想以这样的方式添加它们 以便我从日期中获取日期 从时间中获取时间 关于我如何做到这一点有什么建议吗 Cheers Nik 如果我没听错的话NSDates dateByAddingTimeInterval
  • 在 Symfony 1.4 中重命名“web”文件夹

    我想在 symfony 1 4 中将 web 文件夹重命名为 html 不幸的是 搜索这方面的文档却一无所获 除了在 1 0 中如何实现这一点之外 这似乎不起作用 首先 您不必重命名它 您可以只创建一个符号链接 除非您运行的是 Window
  • 使用 MySQL 和 ejabberd 进行高效的外部排班

    Question 请注意 这个问题的解决方案就在下面 使用 Eugen 的视图思想 我正在为 PHP MySQL 用户驱动的网站编写一个聊天模块 允许两个用户交朋友 并选择 eJabberd 作为聊天系统 我已经使用 PHP 守护程序成功设
  • 透明外壳。如何将很长的 JavaScript 拆分为多行?

    我有一个 JS 压缩文件 长约 14k 并且在一行中 这会在 Clear Case 上造成一些问题 导致无法办理登机手续 有没有办法修复clearcase 如果我想将JS文件拆分为多行 插入换行符时需要注意什么吗 显然我不会分割字符串或数字
  • schema.org 中的产品类别?

    用作参考 https support google com webmasters answer 146750 hl en https support google com webmasters answer 146750 hl en 您会注
  • 如何加密/解密 XML 文件?

    我正在尝试加密 解密 XML 文件 我找到了这个加密示例 但我不知道如何解密 任何想法 谢谢 Load this XML file System Xml XmlDocument myDoc new System Xml XmlDocumen
  • ArCore Sceneform:检测图像时播放.mp4视频

    当我找到图像时 我想在其上方放置文本和视频 文本视图放置在场景上 但视频没有放置在场景中 它只是添加到中间的主布局中 我正在使用组件 VideoView 我不确定这是问题所在 override fun onCreate savedInsta