Skip to content

Conversation

@navidadelpour
Copy link
Contributor

@navidadelpour navidadelpour commented Jan 12, 2024

Reference Issue

Fixes #4634

Features

  • Adds option: CreateVideoTextureOptions argument to createVideoTexture. Allowing to call it like createVideoTexture(url, {crossOrigin: "anonymous"})

Please let me know if there is any change needed if the solution is accepted (like updating documents, etc).

@google-cla
Copy link

google-cla bot commented Jan 12, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@navidadelpour navidadelpour changed the title feat: add CreateVideoTextureOptions to support crossOrigin videos Add CreateVideoTextureOptions to support crossOrigin videos Jan 12, 2024
@elalish
Copy link
Contributor

elalish commented Jan 12, 2024

Yes, please add this info to the packages/modelviewer.dev/data/docs.json entry for this function. This looks great, thanks!

@navidadelpour
Copy link
Contributor Author

@elalish Thanks for reviewing. Docs updated. Let me know if anything else is needed.

"name": "createVideoTexture(uri, options?)",
"htmlName": "createVideoTexture",
"description": "Returns a Texture object for use with the <code>setTexture</code> method on <code>TextureInfo</code> objects in the Materials API. The video element created is available through <code>texture.source.element</code> for controlling playback.",
"description": "Returns a Texture object for use with the <code>setTexture</code> method on <code>TextureInfo</code> objects in the Materials API. The video element created is available through <code>texture.source.element</code> for controlling playback. for cross origin resources, you can set `options.crossOrigin` to `anonymous`.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this more closely, I wonder if we need a new parameter at all. We already have an attribute withCredentials that is used for all of our image loaders. In three.js it's doing: credentials: this.withCredentials ? 'include' : 'same-origin'. Could we just do video.crossOrigin = this.withCredentials ? 'with-credentials' : 'anonymous' to be consistent?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I understand from your comment:

<model-viewer with-credentials src="https://modelviewer.dev/shared-assets/models/sphere.glb" ... >
videoTexture = modelViewerAnimated.createVideoTexture("https://cdn.glitch.global/68b42b4e-5a36-47f5-ab00-09026a7a73d0/lottie-logo.mp4?v=1705067796906");
// video.crossOrigin would be 'anonymous' as you said

Testing it locally gave me this error:

Access to fetch at 'https://modelviewer.dev/shared-assets/models/sphere.glb' from origin 'http://localhost:8002' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

Which addresses the problem in a scenario that the glb file is on the same origin but the video is not.

Copy link
Contributor Author

@navidadelpour navidadelpour Jan 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elalish For improving the consistency in the code, we can change:

interface CreateVideoTextureOptions {
  withCredentials?: boolean
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, how common do you think it is that different assets are coming from different origins for MV users? I do see your point about generality, but the fact is that we've already got this single with-credentials switch that affects src, createTexture, and lottieLoader, without any complaints yet. I think I'd rather add video to that pattern for now, or else do a complete refactor if it proves to be a user pain point. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how common do you think it is that different assets are coming from different origins for MV users?

I'm not really sure as I'm a newbie in this library, but I believe most users would serve their assets alongside their application, and it would be okay for them. However, it may be concerning for users who have an application on origin A and are using a CMS, which may be on origin B, to serve an image, a video, or a Lottie file.

I think I'd rather add video to that pattern for now, or else do a complete refactor if it proves to be a user pain point.

I think that each resource having its option to be with credentials or not would be a more bulletproof solution. You're right; it may not be a user pain point now, and for the sake of consistency, we can do as you suggested, which covers the case for users that have all assets on a cross-origin source.

I will update the MR ASAP.

@navidadelpour
Copy link
Contributor Author

@elalish Hi again.
I've Updated the code as you suggested and everything is fine. I Also update the example so we can test it. I've built the code into a file named model-viewer-updated-in-pr.min.js and used it inside a file named index-updated-in-pr.html so we can test it. But I couldn't find a way to test it with with-credentials.

Thanks!

Copy link
Contributor

@elalish elalish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Agreed regarding a more general solution - let's see how it goes. Out of curiosity, what is stopping your testing?

@elalish elalish merged commit d206a78 into google:master Jan 16, 2024
@navidadelpour
Copy link
Contributor Author

navidadelpour commented Jan 17, 2024

@elalish I appreciate that 😃🤩 . Actually, I wasn't be able to find a cross origin server that serves both glb and video assets with the right cors headers and credential checking which I can be able to use with-credentials option. my only try was the previous one where we got the

Access to fetch at 'https://modelviewer.dev/shared-assets/models/sphere.glb' from origin 'http://localhost:8002' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

error (which tells us to change the cors configurations on the server that I don't have access to).

Not that I'm thinking maybe there would be some way for me to configure a new server in my localhost and use it as the assets server and configure the cors headers so that it requires credentials for serving assets, making it like a cross origin server...

But I Also would be glad if you had any idea and guide me through how it should be tested.

Thanks for your amazing work!

@elalish
Copy link
Contributor

elalish commented Jan 17, 2024

I see - I figured you would just test with a local server. Any generic file server should work - a Google Cloud bucket or Amazon S3, etc. Thanks!

JL-Vidinoti pushed a commit to vidinoti/model-viewer that referenced this pull request Apr 22, 2024
)

* feat: add CreateVideoTextureOptions to support crossOrigin videos

* fix: update docs related to createVideoTexture options argument

* fix: use already existing withCredentials option in createVideoTexture
@ghost
Copy link

ghost commented May 27, 2024

Helped a lot
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling createVideoTexture with cross origin video url cause CORS error

2 participants