Jump to content

[SOLVED] - Problem with Exported Shadow


Recommended Posts

I think there may be a problem with the "lifecyle" of a exported shadow generator.

If i create the shadow generator with the exporter (good bias and filter settings) the shadows show up IF the Y position is high enough off the ground. But if close to ground ... no shadows.

If i create the shadows in the client scene.executeWhenReady the shadows are perfect... If i Console.log both shadows generators (the one created from unity exporter and the one manually created) they are the same. Its weird... Maybe something to do with "Where In The Scene Lifecycle" the shadow generator is being created of exported and the shadow generator info is in the .babyone scene file.

I am attaching a test .babylon file exported from unity... The shadow generator part of the .babylon looks fine. but even in sandbox. With none of my babylon enhancements, the .babylon on load... Show the sphere above the ground... Shadows enabled and physics enabled... The balls starts to drop but when it gets close to ground, the shadow disappears. Its gotta be something in the way way is reading the shadow generator information and instantiates the BABYLON.ShadowGenerator... or maybe WHEN is instantiates the BABYLON.ShadowGenerator.


Anyways take a look a the MiniGame.babylon file in SANDBOX


Link to comment
Share on other sites

BTW... Notice the physics state information in the .babylon file. So physics in working in the sandbox for meshes defined in the scene with unity and without having to do ANY client side setPhysicsState on every mesh, because they had the babylon physics state script attached to them in unity...

So you can just drag that one .babylon file to sandbox with no EXTRA client side javascript to enable the physics

Very Kool:)

Link to comment
Share on other sites

My Manual Code (Which works great) is in C#. I have create a C# web application framework fro creating html5 apps using C# and compiled with DuoCode/Roslyn Base Transpilers.. but there nothing wrong with code. Works great. If you take just the raw .babylon file, add some shadow generator stuff in and upload it to sandbox... Take one off the mesh and hover above ground... drop sphere with physics or something.. You will see the shadow disappear. Check out the .babylon file. Just normal file with this shadowGeneratorSection (Which works fine if i manually create shadow generator) with this at end:

"shadowGenerators" : [
			"mapSize" : 1024,
			"bias" : 0.0005,
			"lightId" : "5fd822aa-70bf-48da-8da1-3021a5fe381c",
			"useVarianceShadowMap" : false,
			"usePoissonSampling" : true,
			"useBlurVarianceShadowMap" : false,
			"blurScale" : 0,
			"blurBoxOffset" : 0,
			"renderList" : [
			"forceBackFacesOnly" : false


If you wanna see what parts of the API i am manually using... Here is my C# GameView Controller Class:

using System;
using System.Threading.Tasks;

using DuoCode.Dom;
using DuoCode.Runtime;

namespace BxGame.Duo
	public class GameView : ViewController
		public BABYLON.Engine GameEngine { get; set; }
		public HTMLCanvasElement GameCanvas { get; set; }
		public BABYLON.Color3 GameColor { get; set; }

		public GameView() 
			this.GameColor = new BABYLON.Color3(0.0, 255.0, 0.0);

		protected override void OnInitialize(object parameter)
			this.element.style.backgroundColor = this.GameColor.toRgbString();

			// Initialize content view
			this.GameCanvas = Root.create<HTMLCanvasElement>("canvas");
			this.GameCanvas.style.width = "100%";
			this.GameCanvas.style.height = "100%";
			this.GameCanvas.style.touchAction = "none";
			this.GameEngine = new BABYLON.Engine(this.GameCanvas, true);
			this.GameEngine.clear(this.GameColor, true, true);

			// Initialize resize window event
			Global.window.onresize += (UIEvent arg1) => {
				return true;

			// Initialize main game scene
			BABYLON.SceneLoader.Load("Scenes/Test1/", "MiniGame.babylon", this.GameEngine, (scene) => {
				var sunlight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
				var skybox = BABYLON.Mesh.CreateBox("skyBox", 400.0, scene);
				var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
				skyboxMaterial.backFaceCulling = false;
				skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("Scenes/Test1/TropicalSunnyDay", scene);
				skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
				skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
				skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
				skybox.material = skyboxMaterial;

				// Wait for textures and shaders to be ready
				scene.clearColor = this.GameColor;
				scene.executeWhenReady(() => {

					Trace.Info("Physics Enabled: {0}", scene.isPhysicsEnabled());
					BABYLON.DirectionalLight light = scene.getLightByName("Directional Light").As<BABYLON.DirectionalLight>();
					light.intensity = light.intensity * 1.5;

					BABYLON.AbstractMesh cube = scene.getMeshByName("Cube");
					BABYLON.AbstractMesh sphere = scene.getMeshByName("Sphere");
					BABYLON.AbstractMesh capsule = scene.getMeshByName("Capsule");
					BABYLON.AbstractMesh ground = scene.getMeshByName("Ground");

					//var shadows = light.getShadowGenerator();
					var shadows = new BABYLON.ShadowGenerator(2048, light);
					shadows.usePoissonSampling = true;

					cube.receiveShadows = true;
					sphere.receiveShadows = true;
					capsule.receiveShadows = true;
					ground.receiveShadows = true;
					shadows.getShadowMap().renderList.push(sphere.As<dynamic>(), cube.As<dynamic>(), capsule.As<dynamic>());

					//UnityMetaData data = (sphere.metadata != null) ? sphere.metadata.As<UnityMetaData>() : null;
					//Trace.Info("===> Mesh Metadata: {0} - {1} - {2} - {3}", data.tagName, data.layerIndex, data.layerName, data.parentId);

					// Once the scene is loaded, just register a render loop to render it
					this.GameEngine.runRenderLoop(() => {

						//var ratio = 1.0;
						//cube.rotation.x += 0.025 * ratio;
						//cube.rotation.y += 0.025 * ratio;


		protected override void OnDispose()


I am attaching the .babylon file


Link to comment
Share on other sites

I don't know if you know about DuoCode. But i created almost exact matching Typescript API. Actually i created a TypeScript Syntax Tree Parser that actual uses the TypeScript Compiler API type COMPLETLY Parser Typescript declaration file and generate and XML file containing all the necessary info to create C# DuoCode BINDINGS to the Underlying JavaScript. The whole thing is on my github: My GitHub.

And the Main BABYLON C# Plugin is up on nuget: Install-Package DuoCode.Plugins.Babylon

If you know about .d.cs declaration files... Attached is the one i created to MIRROR the original TypeScript for BabylonJS 2.5... Take a look :)


Link to comment
Share on other sites

5 hours ago, Deltakosh said:

So this should not be related to lifecyle (I guess).

Can you confirm that you are using the same blurring function and the same bias?

Can you also share a playground where you create the generator manually?

Did you take a look at the .babylon file in sandbox. Can you see why the shadows disappear. Is there something wrong with the .babylon file

Link to comment
Share on other sites

Just to be sure... i added shadows.blurScale = 0... Here is the console.log(shadows)... Everything matches whats in the .babylon file... Shadows still disappear on sandbox:


_bias: 0.00005

_blurBoxOffset: 0

_cachedDefines: "#define FULLFLOAT↵#define NUM_BONE_INFLUENCERS 0"

_cachedDirection: Vector3 {x: -0.32139380162218123, y: -0.7660444418664807, z: 0.5566704028097004, toString: function, getClassName: function, …}

_cachedPosition: Vector3 {x: 0, y: 0, z: 0, toString: function, getClassName: function, …}

_currentFaceIndex: 0

_currentFaceIndexCache: 0

_currentRenderID: 1916

_darkness: 0

_effect: Effect {_isReady: true, _compilationError: "", _valueCache: Object, _engine: Engine, name: "shadowMap", …}

_filter: 2

_light: DirectionalLight {state: "", metadata: Object, animations: [], _ranges: {}, _childrenFlag: -1, …}

_lightDirection: Vector3 {x: -0.32139380162218123, y: -0.7660444418664807, z: 0.5566704028097004, toString: function, getClassName: function, …}

_mapSize: 2048

_projectionMatrix: Matrix {m: Float32Array, isIdentity: function, determinant: function, toArray: function, asArray: function, …}

_scene: Scene {autoClear: true, clearColor: Color3, ambientColor: Color3, forceWireframe: false, forcePointsCloud: false, …}

_shadowMap: RenderTargetTexture {hasAlpha: false, getAlphaFromRGB: false, level: 1, coordinatesIndex: 0, coordinatesMode: 4, …}

_transformMatrix: Matrix {m: Float32Array, isIdentity: function, determinant: function, toArray: function, asArray: function, …}

_transparencyShadow: false

_useFullFloat: true

_viewMatrix: Matrix {m: Float32Array, isIdentity: function, determinant: function, toArray: function, asArray: function, …}

_worldViewProjection: Matrix {m: Float32Array, isIdentity: function, determinant: function, toArray: function, asArray: function, …}

blurScale: 0

forceBackFacesOnly: false


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...