[SOLVED] How to use Lighting.j3md on a Box?

I’m playing with the sample, https://wiki.jmonkeyengine.org/jme3/beginner/hello_material.html
It shows using Lighting.j3md on a sphere,
I changed it onto a Box, but failed, only the front face is visible.
I guess it’s about some “normal” value for the faces are not set.
What is the simplest way to do it?
Thank you.

the code is

	{
		Box cube1Mesh = new Box(1f, 1f, 1f);
//			TangentBinormalGenerator.generate(cube1Mesh); // not work...
		Geometry cube1Geo = new Geometry("My Textured Box", cube1Mesh);
		cube1Geo.setLocalTranslation(new Vector3f(-3f, 1.1f, 0f));

		Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md").clone();
		mat.setBoolean("UseMaterialColors", true);
		mat.setColor("Ambient", new ColorRGBA(0.3f, 0.3f, 1f, 1f));//
		mat.setColor("Diffuse", new ColorRGBA(0.3f, 1f, 0.3f, 1f));
		mat.setColor("Specular", new ColorRGBA(1f, 0.3f, 0.3f, 1f));
		mat.setFloat("Shininess", 32f); // [0,128]

		cube1Geo.setMaterial(mat);

		rootNode.attachChild(cube1Geo);
	}

I don’t use image texture, but only use color by the way.

only the front face is visible.

Lighting.j3md

require light to be visible.

i belive you just use DirectionalLight(that lit only some side), but no AmbientLight(that overall lit)

so i belive your DirectionalLight direction vector is setup to lit only one face so its visible, other faces are “black” same as background is “black” so they are not visible. (you can easly check it by changing background color)

Btw. new games use PBR (PBRLighting.j3md) that additionaly require LightProbe, but its for advanced users, i want to let you know its best ready2use material.

…if you want correct ambient. You can use it just fine without a light probe just for testing… it just doesn’t look as nice as it could.

The reason is just like you said(not like what I guessed)
Everything looks fine now, and I’ll look into PBRLighting.j3md
Thank you for your help!:smiling_face_with_three_hearts:

	@Override
public void simpleInitApp() {
	viewPort.setBackgroundColor(new ColorRGBA(0.5f, 0.5f, 0.5f, 1f));
	{
		Box cube1Mesh = new Box(1f, 1f, 1f);
		Geometry cube1Geo = new Geometry("My Textured Box", cube1Mesh);
		cube1Geo.setLocalTranslation(new Vector3f(0f, 0f, 0f));
		Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md").clone();
		mat.setBoolean("UseMaterialColors", true);
		mat.setColor("Ambient", new ColorRGBA(0.3f, 0.3f, 1f, 1f));
		mat.setColor("Diffuse", new ColorRGBA(0.3f, 1f, 0.3f, 1f));
		mat.setColor("Specular", new ColorRGBA(1f, 0.3f, 0.3f, 1f));
		mat.setFloat("Shininess", 64f); // [0,128]
		cube1Geo.setMaterial(mat);
		rootNode.attachChild(cube1Geo);
	}
	{
		DirectionalLight sun = new DirectionalLight();
		sun.setDirection(new Vector3f(1, 1, -1).normalizeLocal());
		sun.setColor(new ColorRGBA(1f, 1f, 1f, 1f));
		rootNode.addLight(sun);
	}
	{
		DirectionalLight sun = new DirectionalLight();
		sun.setDirection(new Vector3f(1, 1, 1).normalizeLocal());
		sun.setColor(new ColorRGBA(1f, 1f, 1f, 1f));
		rootNode.addLight(sun);
	}
	{
		AmbientLight al = new AmbientLight();
		al.setColor(new ColorRGBA(1f, 1f, 1f, 1f));
		rootNode.addLight(al);
	}
	flyCam.setMoveSpeed(5);
}

np :wink:

im not sure why you use this braces “{” to split code.

Anyway what i want tell is that you dont need 2 DirectionalLights, until its intended and you want them. (you can use directional light vector to just lit multiple faces) 1,1,1 is just pointing one face, even small change like 0.9f,0.9f,0.9f would lit more than 1 face.
You think of it as SUN light direction

Usually its just 1 Directional Light, 1 Ambient Light, X(0+) of Point Lights, X(0+) of Spot lights, etc.

Point Light, Spot lights you think as Local Lights

Ambient Light you think as Ambient(general lit for everything)

Also if you want mix PBR(common known because of metallic reflection abilities) with standard Lightning material, you can, but i suggest add subNode in rootNode for PBR based objects and place light probes in there only.(but generate based on rootNode)

there are also some tutorial videos(you dont need use SDK, but it will be easier for you on start):

For variable name reusing.
and for making sure that local variables not be used accidentally in dozens lines of copy&paste codes.
and also easy for comment out lately.
:grinning:

For variable name reusing.
and for making sure that local variables not be used accidentally in dozens lines of copy&paste codes.
and also easy for comment out lately.

well, thats why you use methods/classes

currently someone could tell “its duplicated code”(only 1 -> -1 change).
adding method like "addDirectionalLight(Vector3f color, Vector3f direction) would make less code here and avoid “{”(well i know method have 2 of them anyway)

its not wrong in this situation tho, but im always serious about code duplications, so i let know :slightly_smiling_face: (because i know in big projects dupplicated code make a lot of issues and increase work speed logarythmic a lot)

Anyway going further, you could use ECS(in JME it have Zay-ES) and even make light as SUN entity :roll_eyes: :innocent:

but lets stay with simple things.

Making duplication is not the case this style ought to be. :grinning:
That’s where I don’t want to use method or class. Because when you see a method call, you don’t know 10 lines of code is written for that, or hundreds lines of code. That gives anxiety.
Some situation you just cannot make a good name for the method describing what it did exactly for the piece of code.
And even it may not likely to be called twice.

Also make sure some local variable not polluting below is also reducing anxiety, when you prototyping.

I firstly also feel uncomfortable about the {} style, but I actually feel it’s a good friend in the 10+ years of java coding.

I will not use it in a big project which has it’s own project-wide coding style, but I’m free and happy to use it in my pet projects.