Skip to content

Commit

Permalink
added shading to planet
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmyers8 committed Feb 8, 2024
1 parent 7ae374a commit 1367851
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ update()
<p align="center">
<img src="./public/readmeImg/output.png" />
<img src="./public/readmeImg/output1.png" />
<img src="./public/readmeImg/p26.png" />
<img src="./public/readmeImg/p27.png" />
</p>

## Gallery
Expand Down
Binary file added public/readmeImg/p26.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/readmeImg/p27.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/lib/PlanetTech/engine/quad.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function checkDivisible(w, h, ws, hs) {
var wp = p.position.clone()//todo
var cnt = this.quadTreeconfig.config.cnt.clone()
p.worldToLocal(cnt)
var textureNodeN = NODE.texture(texture_[0][i],NODE.uv()).mul(2).sub(1)
var textureNodeN = NODE.texture(texture_[0][i],NODE.uv())
var textureNodeD = NODE.texture(texture_[1][i],NODE.uv()).r
p.material.colorNode = textureNodeN
const displace = textureNodeD.mul(displacementScale).mul(NODE.positionLocal.sub(cnt).normalize())
Expand Down
102 changes: 101 additions & 1 deletion src/lib/WorldSpace/Shaders/atmosphereScattering.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,102 @@ const postFragmentShader =
${screenToWorldBlock}
vec2 ray_sphere_intersect(
vec3 start, // starting position of the ray
vec3 dir, // the direction of the ray
float radius // and the sphere radius
) {
// ray-sphere intersection that assumes
// the sphere is centered at the origin.
// No intersection when result.x > result.y
float a = dot(dir, dir);
float b = 2.0 * dot(dir, start);
float c = dot(start, start) - (radius * radius);
float d = (b*b) - 4.0*a*c;
if (d < 0.0) return vec2(1e5,-1e5);
return vec2(
(-b - sqrt(d))/(2.0*a),
(-b + sqrt(d))/(2.0*a)
);
}
vec3 skylight(vec3 sample_pos, vec3 surface_normal, vec3 light_dir, vec3 background_col) {
surface_normal = normalize(mix(surface_normal, light_dir, 0.6));
Atmospheres currentAtmospheres = atmospheres[0];
return calculate_scattering(
sample_pos, // the position of the camera
surface_normal, // the camera vector (ray direction of this pixel)
3.0 * currentAtmospheres.ATMOSPHERE_RADIUS, // max dist, since nothing will stop the ray here, just use some arbitrary value
background_col, // scene color, just the background color here
light_dir, // light direction
currentAtmospheres.ulight_intensity, // light intensity, 40 looks nice
currentAtmospheres.uray_light_color,
currentAtmospheres.umie_light_color,
currentAtmospheres.PLANET_CENTER, // position of the planet
currentAtmospheres.PLANET_RADIUS, // radius of the planet in meters
currentAtmospheres.ATMOSPHERE_RADIUS, // radius of the atmosphere in meters
currentAtmospheres.RAY_BETA, // Rayleigh scattering coefficient
currentAtmospheres.MIE_BETA, // Mie scattering coefficient
currentAtmospheres.ABSORPTION_BETA, // Absorbtion coefficient
currentAtmospheres.AMBIENT_BETA, // ambient scattering, turned off for now. This causes the air to glow a bit when no light reaches it
currentAtmospheres.G,
currentAtmospheres.HEIGHT_RAY,
currentAtmospheres.HEIGHT_MIE,
currentAtmospheres.HEIGHT_ABSORPTION,
currentAtmospheres.ABSORPTION_FALLOFF,
currentAtmospheres.PRIMARY_STEPS,
currentAtmospheres.LIGHT_STEPS
);
}
vec4 render_scene(vec3 pos, vec3 dir, vec3 light_dir, vec3 addColor, vec3 PLANET_POS, float PLANET_RADIUS) {
// the color to use, w is the scene depth
vec4 color = vec4(addColor, 1e25);
// add a sun, if the angle between the ray direction and the light direction is small enough, color the pixels white
//color.xyz = vec3(dot(dir, light_dir) > 0.9998 ? 3.0 : 0.0);
// get where the ray intersects the planet
vec2 planet_intersect = ray_sphere_intersect(pos - PLANET_POS, dir, PLANET_RADIUS);
// if the ray hit the planet, set the max distance to that ray
if (0.0 < planet_intersect.y) {
color.w = max(planet_intersect.x, 0.0);
// sample position, where the pixel is
vec3 sample_pos = pos + (dir * planet_intersect.x) - PLANET_POS;
// and the surface normal
vec3 surface_normal = normalize(sample_pos);
// get the color of the sphere
color.xyz = addColor;
// get wether this point is shadowed, + how much light scatters towards the camera according to the lommel-seelinger law
vec3 N = surface_normal;
vec3 V = -dir;
vec3 L = light_dir;
float dotNV = max(1e-6, dot(N, V));
float dotNL = max(1e-6, dot(N, L));
float shadow = dotNL / (dotNL + dotNV);
// apply the shadow
color.xyz *= shadow + .025;
// apply skylight
//color.xyz += clamp(skylight(sample_pos, surface_normal, light_dir, vec3(0.0)) * addColor, 0.0, 1.0);
}
return color;
}
void mainImage(const in vec4 inputColor, const in vec2 uv, const in float depth, out vec4 outputColor) {
float d = texture2D(depthBuffer, uv).x;
Expand All @@ -248,6 +344,7 @@ const postFragmentShader =
Atmospheres currentAtmospheres = atmospheres[0];
vec3 lightDirection = normalize(currentAtmospheres.lightDir);;
addColor = render_scene(rayOrigin, rayDirection, lightDirection,addColor,currentAtmospheres.PLANET_CENTER,currentAtmospheres.PLANET_RADIUS).rgb;
col += calculate_scattering(
rayOrigin,
Expand All @@ -273,7 +370,10 @@ const postFragmentShader =
currentAtmospheres.PRIMARY_STEPS,
currentAtmospheres.LIGHT_STEPS
);
outputColor = vec4(col*2., 1.0);
col = 1.0 - exp(-col);
outputColor = vec4(col*1.5, 1.0);
}
`;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/examples/dynamicTileTextureManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ let N = await Promise.all([
])
let moon = new Moon({
size: 10000,
polyCount: 100,
polyCount: 50,
quadTreeDimensions: 1,
levels: 4,
radius: 80000,
displacmentScale: 80.5,
displacmentScale: 65.5,
lodDistanceOffset: 12.4,
material: new NODE.MeshBasicNodeMaterial()
})
Expand Down
23 changes: 5 additions & 18 deletions src/lib/viewGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,39 +118,26 @@ class ViewGL {
PLANET_RADIUS: this.celestialBodie.metaData().radius,
ATMOSPHERE_RADIUS: 81000,
lightDir: new THREE.Vector3(0,0,1),
ulight_intensity: new THREE.Vector3(9.0,9.0,9.0),
ulight_intensity: new THREE.Vector3(8.0,8.0,8.0),
uray_light_color: new THREE.Vector3(1.0,1.0,1.0),
umie_light_color: new THREE.Vector3(1.0,1.0,1.0),
umie_light_color: new THREE.Vector3(5.0,5.0,5.0),
RAY_BETA: new THREE.Vector3(5.5e-6, 13.0e-6, 22.4e-6).multiplyScalar(25.5),
MIE_BETA: new THREE.Vector3(21e-6, 21e-6, 21e-6).multiplyScalar(25.5),
AMBIENT_BETA: new THREE.Vector3(0.0),
ABSORPTION_BETA: new THREE.Vector3(2.04e-5, 4.97e-5, 1.95e-6).multiplyScalar(79.5),
HEIGHT_RAY: 8e3/80.5,
HEIGHT_MIE: 1.2e3/80.5,
HEIGHT_RAY: 8e3/85.5,
HEIGHT_MIE: 1.2e3/85.5,
HEIGHT_ABSORPTION: 30e3/79.5,
ABSORPTION_FALLOFF: 4e3/79.5,
PRIMARY_STEPS: 12,
LIGHT_STEPS: 4,
G: 0.00007,
G: 0.0000007,
})
this.space.setAtmosphere()
this.space.addEffects([new SMAAEffect()])

const geometry = new THREE.SphereGeometry( this.celestialBodie.metaData().radius, 220, 220 );
const material = new THREE.MeshPhongMaterial( { color: 'red' } );
const sphere = new THREE.Mesh( geometry, material );

sphere.position.copy(this.celestialBodie.metaData().cnt.clone())


let light = new THREE.DirectionalLight(0xffffff,1.5);
light.position.set(0, 0, 1);
this.rend.scene_.add(light);

const alight = new THREE.AmbientLight( 0x404040 ); // soft white light
this.rend.scene_.add( alight );
this.rend.scene_.add( this.celestialBodie.sphere );
//this.rend.scene_.add(sphere)
}


Expand Down

0 comments on commit 1367851

Please sign in to comment.