SceneView / sceneform-android

Sceneform Maintained is an ARCore Android SDK with Google Filament as 3D engine. This is the continuation of the archived Sceneform
https://sceneview.github.io/sceneform-android/
Apache License 2.0
632 stars 147 forks source link

Video Rendering Example #29

Closed aseemiboson closed 3 years ago

aseemiboson commented 3 years ago

Could you please give me an example of video rendering

suryanadiminti99 commented 3 years ago

@aseemiboson did you got any solution ?

aseemiboson commented 3 years ago

@suryanadiminti99 private fun loadVideo() { runOnUiThread { val uri = Uri.parse(arData!!.arContent[0].arProperty[2].propertyValue) val player = MediaPlayer.create(this@MainActivity,uri) val externalTexture = ExternalTexture() player.setSurface(externalTexture.surface) player.setAudioStreamType(AudioManager.STREAM_MUSIC) player.setScreenOnWhilePlaying(true) player.isLooping = true //player.start(); Material.builder().setSource(this@MainActivity,R.raw.augmented_video_material).build().thenAccept { material: Material? -> Log.d(TAG,"loadVideo: $uri") val playerNode = Node() Log.d(TAG,"loadVideo: Aspect Ratio = ${player.videoWidth}x${player.videoHeight}") val gcd = gcd(player.videoWidth.toLong(),player.videoHeight.toLong()) val widthAspect = (player.videoWidth/gcd).toFloat() val heightAspect = (player.videoHeight/gcd).toFloat() val videoScreen = ShapeFactory.makeCube(Vector3(widthAspect,heightAspect,0.0001f),Vector3.zero(),material) videoScreen.isShadowCaster = true videoScreen.material.setExternalTexture("videoTexture",externalTexture) // cube.material.setFloat3("keyColor",CHROMA_KEY_COLOR); playerNode.renderable = videoScreen // (playerNode.renderable as ModelRenderable).material.setFloat4("keyColor",CHROMA_KEY_COLOR); val boundingBox = videoScreen.collisionShape as Box? if (boundingBox != null) { val boundingBoxSize = boundingBox.size val maxExtent = Math.max(boundingBoxSize.x,Math.max(boundingBoxSize.y,boundingBoxSize.z)) val targetSize = 0.2f // Whatever size you want. val scale = targetSize / maxExtent val scaleVector3 = Vector3.one().scaled(scale) playerNode.localScale = scaleVector3 Log.d(TAG,"loadVideo: Scale ${Vector3.one().scaled(scale).toString()}") } cube!!.addChild(playerNode) player.start() } } }

this is currently i am using but this is support in Scenceform 1.16.0 Official SDK, in this repo its crashing, to do chroma keying use croma keying material and put keyColor

suryanadiminti99 commented 3 years ago

@aseemiboson will you please share your material file ? or can we use chromakey material ?

aseemiboson commented 3 years ago

@suryanadiminti99 give me your mail id will sent

suryanadiminti99 commented 3 years ago

@aseemiboson suryanadiminti99@gmail.com

suryanadiminti99 commented 3 years ago

@aseemiboson Thankyou very much.

suryanadiminti99 commented 3 years ago

@aseemiboson It's working in previous scene form module.

suryanadiminti99 commented 3 years ago

@ThomasGorisse Hi , You really did a great job .

This is the code for android users to show video using cube.

`runOnUiThread(new Runnable() { @Override public void run() { // Create Media Player mediaPlayer = MediaPlayer.create(ArActivity.this,Uri.fromFile(new File(videoUrl))); ExternalTexture externalTexture = new ExternalTexture(); mediaPlayer.setSurface(externalTexture.getSurface()); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setScreenOnWhilePlaying(true); mediaPlayer.setLooping(true);

            Material.builder().setSource(ArActivity.this,R.raw.chroma_key_material).build().thenAccept(material -> {

            ModelRenderable renderable = ShapeFactory.makeCube(new Vector3(Float.parseFloat(scalex),0.0001f,Float.parseFloat(scaley)),Vector3.zero(),material);

            renderable.getMaterial().setExternalTexture("videoTexture", externalTexture);
            renderable.getMaterial().setFloat4("keyColor", CHROMA_KEY_COLOR);

            AnchorNode anchorNode = new AnchorNode(anchor);

            // Create a node to render the video and add it to the anchor.
            Node videoNode = new Node();
            anchorNode.setParent(arFragment.getArSceneView().getScene());
            videoNode.setParent(anchorNode);

            // Vector3 vectorScale = new Vector3(Float.parseFloat(scalex), 0.0001f, Float.parseFloat(scaley));

            Vector3 vectorPos = new Vector3(Float.parseFloat(posx), Float.parseFloat(posy),(Float.parseFloat(posz)));

            Vector3 vectorScale = new Vector3(Float.parseFloat(scalex), Float.parseFloat(scaley),(Float.parseFloat(scalez)));

            Quaternion rx = Quaternion.eulerAngles(new Vector3(Float.parseFloat(rotatex), 0.0f, 0.0f));
            Quaternion ry = Quaternion.eulerAngles(new Vector3(0.0f, Float.parseFloat(rotatey), Float.parseFloat(rotatez)));

            // Set Scaling
            //  videoNode.setLocalScale(vectorScale);
            // Set Position
            videoNode.setLocalPosition(vectorPos);
            // Set Rotation
            videoNode.setLocalRotation(Quaternion.multiply(rx,ry));
            // Set Scaling values
            videoNode.setLocalScale(vectorScale);

            // Start playing the video when the first node is placed.
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer.start();

                externalTexture.getSurfaceTexture().setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
                    @Override
                    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
                        videoNode.setRenderable(renderable);
                        externalTexture.getSurfaceTexture().setOnFrameAvailableListener(null);
                    }
                });

            } else {
                videoNode.setRenderable(renderable);
            }

            });

        }
    });`

@ThomasGorisse Please look on this issue. Why it is not working in current repository .

ThomasGorisse commented 3 years ago

The 1.18.8 release solves some Material issues and brings a Video Texture sample Release 1.18.8 - Video Texture + SceneView Background