0
\$\begingroup\$

I'm trying to apply a texture to the material of a Model but I could not figure out how to do it:

so I made a direct translation of the c code taken from examples:

(original from "Raylib [shaders] example - postprocessing shader" line 77)

model.materials[0].maps[MAP_DIFFUSE].texture = texture;

(my c# translation)

m_LP_SpookyTree = LP_SpookyTree.GetModel;
m_LP_SpookyTree.materials[0].maps[MaterialMapType.MAP_ALBEDO].texture = Raylib.LoadTexture("Ressource/Textures/PlaceHolders/mossy_rock_diff_1k.png");

According to the cs file on the repo, the "materials" definition is an Int pointer. The problem comes from the Intptr, how could I get the material array from it?

thanks for your time.

PS: I've created an issue on Raylib.cs GitHub repo so if I get an answer on one before the other, I'll update accordingly


EDIT 1:

I've tried to change every IntPtr to the object it should actually point to regarding the comments in the source file, compile the lib, and imported it. unfortunately, it broke. so I don't think it's possible.


EDIT 2:

I've found this article talking about IntPtr to objects (vice versa)

https://stackoverflow.com/questions/728105/how-to-convert-an-intptr-back-into-an-object

I've implemented it like so:

public static object pointerToObject(IntPtr pMapping, Type type)
{
    return Marshal.PtrToStructure(pMapping, type);
}

but now I get this error:

System.MissingMethodException : 'No parameterless constructor defined for type 'Raylib_cs.Material[]'.'

PS: The new code:

Material[] materials = (Material[])PointerManipulation.pointerToObject(m_LP_SpookyTree.materials,typeof(Material[]));
MaterialMap[] materialMaps = (MaterialMap[])PointerManipulation.pointerToObject(materials[0].maps, typeof(MaterialMap[]));

materialMaps[(int)Convert.GetTypeCode(MaterialMapType.MAP_ALBEDO)].texture = Raylib.LoadTexture("Ressource/Textures/PlaceHolders/mossy_rock_diff_1k.png");
\$\endgroup\$
0
\$\begingroup\$

I've found a way to do it without strange coding.

I've found in this repo linked on the Raylib.cs readme.md file a translation of the c examples to c#.

Though, I had to do small modifications to the script.

Following are the original script and the modified version that worked for me:

original from https://github.com/ChrisDill/Raylib-cs-Examples/blob/eacde217a26971bb5e72f16d6f56ece6fd7646aa/Examples/models/models_yaw_pitch_roll.cs


            // Model loading
            Model model = LoadModel("resources/plane.obj");      // Load OBJ model

            // Set map diffuse texture
            Material* materials = (Material*)model.materials.ToPointer();
            MaterialMap* maps = (MaterialMap*)materials[0].maps.ToPointer();
            maps[(int)MAP_ALBEDO].texture = LoadTexture("resources/plane_diffuse.png");

mine:

            m_LP_SpookyTree = LP_SpookyTree.GetModel;
            unsafe { 
                Material* materials = (Material*)m_LP_SpookyTree.materials.ToPointer();
                MaterialMap* maps = (MaterialMap*)materials[0].maps.ToPointer();

                Texture2D d_LP_SpookyTree = Raylib.LoadTexture("Ressource/Models/LP_SpookyTree.Diffuse.png");
                maps[(int)MaterialMapType.MAP_ALBEDO].texture = d_LP_SpookyTree;

                Raylib.GenTextureMipmaps(ref maps[(int)MaterialMapType.MAP_ALBEDO].texture);
            }

I Had to add Raylib name before LoadTexture and GenTextureMipmaps because else it would not work since these are static methods from the RayLib cs binder.

So on a technical point, I think, from what I've seen in the code, that to translate an IntPtr we don't need to use strange methods that do not work. Instead, an explicit cast- as followed - suffice to make the pie. You might actually correct me since I'm not a really good programmer.

(Material*)m_LP_SpookyTree.materials.ToPointer();

In comparison here is the c code to get the material array:

m_LP_SpookyTree.materials;

In the cs binder, this returns an IntPtr which must be converted to its correct type.

also, I had to change MAP_ALBEDO to MaterialMapType.MAP_ALBEDO.

you can witness following is the working proof:

enter image description here

Hope it would help Cs users of Raylib

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.