Error:Exception in HostFunction: Wrong object reference. Expected: native object reference at ShadersModule::textureSampler

Hello everyone! I’m very happy I found this community :slight_smile: and I hope one of you can help me get me out of my current coding pickle :see_no_evil:

I’m trying to learn the scripting part of Spark AR by following Sohail Mehra’s first tutorial.

However, it is possible something has changed in the API since the tutorial was published because, although I feel like I followed the instructions closely, I’m currently stuck with errors I do not understand how to fix.

Here is the code (error below):

const Materials = require('Materials');
const Shaders = require('Shaders');
const Textures =  require('Textures');

const Time = require('Time');
const Reactive = require('Reactive');

export const Diagnostics = require('Diagnostics');

function print(a)
{
    Diagnostics.log(a);
}

const material0 = Materials.findFirst('material0');
const cameraTexture = Textures.findFirst('cameraTexture0');

function main()
{
    const uvs = Shaders.vertexAttribute({"variableName" : Shaders.vertexAttribute.TEX_COORDS});

    const ct = Reactive.mul(Time.ms,0.001);
    const curve = Reactive.abs(Reactive.sin(ct));
    const color = Shaders.textureSampler(cameraTexture, uvs);
    const modulationColor = Reactive.pack4(curve,0,0,1);
    const finalColor = Reactive.mul(color, modulationColor);

    const textureSlot = Shaders.DefaultMaterialTextures.DIFFUSE;
    material0.setTexture(mix2, {textureSlot: textureSlot});

}

main();

And the error I’m getting:

Error:Exception in HostFunction: Wrong object reference. Expected: native object reference
    at ShadersModule::textureSampler (native)
    at main (file:///7a6d89b6b47ba717bea40ff1cc728f46.js:865:37)
   {
     "line": 24,
     "column": 41,
     "sourceURL": "script.js"
   }

Line 24 is this one:

const color = Shaders.textureSampler(cameraTexture, uvs);

Can anyone point me to what I’m doing wrong? I’d be very grateful.

PS: I’m using Spark AR Studio Version: 102.0.0.27.234

PPS: By the way, if you have any other resources for learning the scripting API (besides the official documentation), that would be amazing!

Thanks!

Yes, indeed the API changed, although you specifically asked for something other than the docs, taking a look might reveal the problem.

The code throws an error on line 24, but it’s pretty hard to tell where that is, I’d recommend using Gists when asking for script related help. But at first glance, I can see that both the method to fetch Textures & Objects are incorrect since the API used in your code was faced out begging on v82 and completely removed on v101; that may be the source of the error

I have a tutorial that covers the new methods, feel free to check it out here

1 Like

Hi Tomas,

Thanks for your answer! I’d love to follow your scripting tutorial but the link seems broken.

Line 24 is this one:

const color = Shaders.textureSampler(cameraTexture, uvs);

PS: You’re totally right about the doc and I did spend a lot of time looking through it :wink: What I meant by “besides the official doc” is that I’m looking for additional learning material. The doc is great as a reference but it’s nice to have tutorials that make the learning curve a bit smoother.

1 Like

Ooops, liked to the wrong repo :sweat_smile: (that was the draft), I updated the post above but here’s the right link just in case.

1 Like

There don’t seem to be any problems with line 24 as I suspected, most likely you are getting that error because textureSampler is being fed undefined as a texture. (the uvs are fine)

1 Like

Yep, line 24 is just where the runtime notices the problem. The issue is that findFirst returns a Promise and not a texture, and it complains because it expects a texture.

Check out this section in the tutorial to get an understanding of how to use Promises https://github.com/tomaspietravallo/sparkar-fdccc2020#asyncawait

2 Likes

Thanks a lot! I think I’m getting a better understanding of the problem but also realizing that have a lot of learning ahead of me :sweat_smile:

I’m trying to run the practical example included in the Async/await section of Tomas’ tutorial. First VScode complained that it couldn’t find the names “Textures” and “Materials” so I added the following lines:

const Textures = require('Textures');
const Materials = require('Materials');

Now it is throwing a warning:

Possible Unhandled Promise Rejection: Error: Exception in HostFunction: Wrong object reference. Expected: native object reference

And the texture is all white :see_no_evil:

I tried the same on Sohail Mehra’s tutorial:

I’m getting the same warning:
Possible Unhandled Promise Rejection: Error: Exception in HostFunction: Wrong object reference. Expected: native object reference

And my texture just shows white.

One of your Promises is being rejected. You can catch the error and see what it says by adding .catch to it, like this:

const {log} = require('Diagnostics')
//...
const material0 = await Materials.findFirst('material0').catch(err => log(err))

Thanks. I tried and my code looks like this:

It still brings up the same warning.

Possible Unhandled Promise Rejection: Error: Exception in HostFunction: Wrong object reference. Expected: native object reference

I don’t see an error log for the material0 or cameraTexture0 promises.

I added two test logs to isolate the line where the program stops:

log('this prints');
const color = Shaders.textureSampler(cameraTexture0, uvs);
log('this does not print');

What happens if you try logging out cameraTexture0?

It prints this out:

CameraTexture {
  """
    The `CameraTexture` class represents a texture type that contains image data coming in from system camera, or captured photo/video in case of using the effect with "Media Library".
  """
    }

Sorry I’m still unfamiliar with this workflow, but I think you need to get the signal from the texture object.

2 Likes

Thanks Josh! I think I’m getting closer but…

I don’t understand what I am supposed to pass to Shaders.vertexAttribute().

The documentation says:

So I tried Shaders.vertexAttribute(VertexAttribute.TEX_COORDS)

But I get the following error.

Possible Unhandled Promise Rejection: ReferenceError: Property 'VertexAttribute' doesn't exist

How do you set the attribute to TEX_COORDS?

{variableName: Shaders.VertexAttribute.TEX_COORDS} or {variableName: ‘TEX_COORDS’}, either one should work

2 Likes

It worked! Thanks Josh and Tomas :slight_smile:

Here is the final version for reference:

5 Likes