You are right, my planets are simply orbiting around the sun. That is why I already wondered how to parent DirectionalLight. I tried to change your PG solution to be more like my project in terms of scales ( https://playground.babylonjs.com/#EZCN5Q#8 ). How do I make the planet reflect, so that some parts of the building aren’t black? FYI Here is the constructor of the Planet Class:
// HERE necessary datas are saved
const options = {
biomes: 1,
clouds: true,
mapSize: 1024,
upperColor: new BABYLON.Color3(2, 1, 0),
lowerColor: new BABYLON.Color3(0, .2, 1),
haloColor: new BABYLON.Color3(0, .2, 1),
maxResolution: 64,
seed: .3,
cloudSeed: .55,
lowerClamp: new BABYLON.Vector2(.6, 1),
groundAlbedo: 1.1,
cloudAlbedo: .9,
directNoise: false,
lowerClip: new BABYLON.Vector2(0, 0),
range: new BABYLON.Vector2(.3, .35)
}
const terrain = new BABYLON.DynamicTexture('random', 128, this.scene, false, BABYLON.Texture.NEAREST_SAMPLINGMODE);
const clouds = new BABYLON.DynamicTexture('random', 128, this.scene, false, BABYLON.Texture.NEAREST_SAMPLINGMODE);
let updateRandomSurface = function (random) {
let context = random.getContext();
let imgData = context.getImageData(0, 0, 512, 512);
for (let i = 0; i < 512 * 512 * 4; i++) {
imgData.data[i] = Math.random() * 256 | 0
}
context.putImageData(imgData, 0, 0);
random.update()
};
let noiseTexture;
let cloudTexture;
const cx = (ox > 0) ? ox * Math.cos(0): x;
const cy = (oy > 0) ? oy * Math.cos(0): y;
const cz = (oz > 0) ? oz * Math.sin(0): z;
this.body = BABYLON.MeshBuilder.CreateSphere('Planet', { segments: 64, diameter: 2*this.data['planet_radius']}, this.scene);
this.body.parent = this.mesher.body;
// HERE buildings for planet are created
this.body.position = new BABYLON.Vector3(cx, cy, cz);
this.body.receiveShadows = true;
this.body.isBlocker = true;
this.body.checkCollisions = true;
let shaderMaterial = new BABYLON.ShaderMaterial('shader', this.scene, {
vertex: './assets/textures/planets/planet',
fragment: './assets/textures/planets/planet'
}, {
attributes: ['position', 'normal', 'uv'],
uniforms: ['world', 'worldView', 'worldViewProjection', 'view', 'projection'],
needAlphaBlending: true
});
shaderMaterial.setVector3('cameraPosition', this.camera.position);
shaderMaterial.setVector3('lightPosition', this.light.position);
this.body.material = shaderMaterial;
const planetShell = this.body;
var angle = 0;
this.scene.registerBeforeRender(function () {
const ratio = game.scene.getAnimationRatio();
planetShell.rotation.y += .0001 * ratio;
shaderMaterial.setMatrix('rotation', BABYLON.Matrix.RotationY(angle));
angle -= 4e-4 * ratio;
shaderMaterial.setVector3('options', new BABYLON.Vector3(options.clouds, options.groundAlbedo, options.cloudAlbedo));
});
var generateBiome = function(biome) {
// HERE biome options are changed if not default planet type
if (noiseTexture) {
noiseTexture.dispose();
cloudTexture.dispose()
}
updateRandomSurface(terrain);
updateRandomSurface(clouds);
noiseTexture = new BABYLON.ProceduralTexture('noise', options.mapSize, './assets/textures/noises/noise', game.scene, null, true, true);
noiseTexture.setColor3('upperColor', options.upperColor);
noiseTexture.setColor3('lowerColor', options.lowerColor);
noiseTexture.setFloat('mapSize', options.mapSize);
noiseTexture.setFloat('maxResolution', options.maxResolution);
noiseTexture.setFloat('seed', options.seed);
noiseTexture.setVector2('lowerClamp', options.lowerClamp);
noiseTexture.setTexture('randomSampler', terrain);
noiseTexture.setVector2('range', options.range);
noiseTexture.setVector3('options', new BABYLON.Vector3(options.directNoise ? 1 : 0, options.lowerClip.x, options.lowerClip.y));
shaderMaterial.setTexture('textureSampler', noiseTexture);
cloudTexture = new BABYLON.ProceduralTexture('cloud', options.mapSize, './assets/textures/noises/noise', game.scene, null, true, true);
cloudTexture.setTexture('randomSampler', clouds);
cloudTexture.setFloat('mapSize', options.mapSize);
cloudTexture.setFloat('maxResolution', 256);
cloudTexture.setFloat('seed', options.cloudSeed);
cloudTexture.setVector3('options', new BABYLON.Vector3(1, 0, 1));
shaderMaterial.setTexture('cloudSampler', cloudTexture);
shaderMaterial.setColor3('haloColor', options.haloColor);
};
// Rest should be irrelevant for this case
Edit: Only thing open is how to light up buildings on planet, only way i could make it in my own project is to use createDefaultEnvironment(createSkybox: false), then everything seems perfect, but i would like to know what createDefaultEnvironment does in terms of lighting up meshes, so that I learn do this manually?