How to add a shadow to spawned object via C# script?

I'm trying to add a model to scene via script—using the LoadGeometry method. The model is added perfectly but no shadow has reveived or casted by the model. How to add a shadow to spawned object via script?

I have JSON files, where is described what model where to place in scene. So I prefer to use script solution, not to add the models manually in editor. Any suggestions?

Code: Select all

using System; using CryEngine; using CryEngine.UI; using CryEngine.EntitySystem; using CryEngine.UI.Components; namespace CryEngine.Game { public class MapLoader { public string sceneName; public MapLoader(string sceneName) { this.sceneName = sceneName; this.createGround(); this.createGameObjects(); } void createGround() { var ground = Entity.SpawnWithComponent<MapLoaderObject>( "Ground", Vector3.Zero, Quaternion.CreateRotationZ(0), Vector3.One ); ground.Initialize("Objects/maps/grounds/" + this.sceneName + ".cgf"); } void createGameObjects() { // Actually, here is foreach-loop adding models from JSON data var gameObject = Entity.SpawnWithComponent<MapLoaderObject>( "Model 1", Vector3.Zero, Quaternion.CreateRotationZ(0), Vector3.One ); gameObject.Initialize("Objects/maps/models/model.cgf"); } } public class MapLoaderObject : EntityComponent { public void Initialize(string uri) { Entity.LoadGeometry(0, uri); } } }

Re: How to add a shadow to spawned object via C# script?

Currently this is not yet exposed in the CryEngine.Core namespace. You can however try this with the CryEngine.Common namespace. The CryEngine.Common namespace however is generated by SWIG which means that you have to make sure to properly dispose and manage the objects you use from that namespace.
The code you want to implement for this would probably look like this:

Code: Select all

public static Entity Spawn(string name, Vector3 position, Quaternion rotation, Vector3 scale)
var spawnParams = new SEntitySpawnParams();

spawnParams.sName = name;
spawnParams.pClass = Global.gEnv.pEntitySystem.GetClassRegistry().GetDefaultClass();

spawnParams.vPosition = position;
spawnParams.qRotation = rotation;
spawnParams.vScale = scale;

// Set all required flags of the entity here.
// You can also set the nFlagsExtended here if required. Those will map to EEntityFlagsExtended.
spawnParams.nFlags = (uint)EEntityFlags.ENTITY_FLAG_CASTSHADOW;

var nativeEntity = Global.gEnv.pEntitySystem.SpawnEntity(spawnParams);

return nativeEntity;
Of course if you get the C# source from Github you can add this functionality to CryEngine.Core yourself. This code is based on the existing Spawn function from EntityStatics.cs in the engine and can be added to it. That way you can even create a new Entity from the nativeEntity. In the future the goal is to have all this functionality also available in the CryEngine.Core namespace.

Who is online

Users browsing this forum: No registered users and 3 guests