···5959scene.add(ambientLight);
606061616262-const atmosphereShader = {
6363- uniforms: {
6464- lightDirection: { value: new THREE.Vector3(1.0, 1.0, 0.0).normalize() }, // Sunlight direction
6565- atmosphereColor: { value: new THREE.Vector3(0.3, 0.6, 1.0) }, // Blue tint for atmosphere
6666-6767- },
6868- vertexShader: `
6969- varying vec3 vNormal;
7070- varying vec3 vViewLightDirection; // Light direction relative to the camera
7171- varying vec3 vViewPosition; // View position
7272-7373- uniform vec3 lightDirection; // Light direction in world space
7474-7575- void main() {
7676- // Transform the vertex normal to world space
7777- vNormal = normalize(normalMatrix * normal);
7878- // Transform the light direction to view space
7979- vViewLightDirection = (viewMatrix * vec4(lightDirection, 0.0)).xyz;
8080- vViewPosition = (modelViewMatrix * vec4(position, 1.0)).xyz;
8181-8282- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
8383- }
8484-`,
8585- fragmentShader: `
8686- varying vec3 vNormal;
8787- varying vec3 vViewLightDirection;
8888- varying vec3 vViewPosition;
8989- uniform vec3 atmosphereColor;
9090-9191- void main() {
9292-9393- // Normalize the normal and the view direction
9494- vec3 viewDirection = normalize(vViewPosition);
9595-9696- // Calculate how much of the surface is perpendicular to the view direction
9797- float viewFactor = dot(normalize(vNormal), -viewDirection);
9898-9999- // Calculate the dot product of the light direction and the surface normal
100100- float lightFactor = dot(normalize(vNormal), normalize(vViewLightDirection));
101101-102102- // Use smoothstep to soften the transition from light to dark side
103103- lightFactor = smoothstep(-0.2, 0.4, lightFactor);
104104-105105- // Ensure a minimum glow, even on the dark side
106106- float minGlow = 0.2; // Adjust this to control how much it glows on the dark side
107107- lightFactor = mix(minGlow, 1.0, lightFactor);
108108-109109- // Adjust the intensity for the atmosphere's glow, including the minimum glow
110110- float dotProduct = dot(normalize(vNormal), vec3(0, 0.0, 1.0));
111111- float intensity = pow(dotProduct, 8.0);
112112- intensity *= lightFactor;
113113-114114- viewFactor = clamp(1.0 - viewFactor, 0.0, 1.0);
115115-116116- // Use smoothstep for a smooth transition on the edges
117117- float atmosphereFactor = smoothstep(0.0, 0.6, viewFactor);
118118-119119- // Output the final color
120120- gl_FragColor = vec4(atmosphereColor, intensity * atmosphereFactor);
121121- // Set the final fragment color with the computed intensity
122122- //gl_FragColor = vec4(0.3, 0.6, 1.0, 1.0) * intensity;
123123- }`,
124124- side: THREE.FrontSide,
125125- transparent: true,
126126- depthWrite: false,
127127-};
128128-129129-// Create the atmosphere material
130130-const atmosphereMaterial = new THREE.ShaderMaterial(atmosphereShader);
131131-132132-// Create the atmosphere geometry
133133-const atmosphereGeometry = new THREE.IcosahedronGeometry(1.2, 20);
134134-const atmosphere = new THREE.Mesh(atmosphereGeometry, atmosphereMaterial);
135135-atmosphere.renderOrder = 1;
136136-scene.add(atmosphere);
137137-138138-139139-140140-// const atmosphereShader = {
141141-// uniforms: {
142142-// lightDirection: { value: new THREE.Vector3(1.0, 1.0, 0.0).normalize() }, // Sunlight direction
143143-// atmosphereColor: { value: new THREE.Vector3(0.3, 0.6, 1.0) }, // Blue tint for atmosphere
144144-// },
145145-// vertexShader: `
146146-// varying vec3 vNormal;
147147-// varying vec3 vViewPosition;
148148-149149-// void main() {
150150-// // Pass the vertex normal to the fragment shader
151151-// vNormal = normalize(normalMatrix * normal);
152152-153153-// // Get the view-space position (relative to the camera)
154154- // vViewPosition = (modelViewMatrix * vec4(position, 1.0)).xyz;
155155-156156-// // Standard vertex position transformation
157157-// gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
158158-// }
159159-// `,
160160-// fragmentShader: `
161161-// varying vec3 vNormal;
162162-// varying vec3 vViewPosition;
163163-164164-// uniform vec3 atmosphereColor;
165165-166166-// void main() {
167167-// // Normalize the normal and the view direction
168168-// vec3 viewDirection = normalize(vViewPosition);
169169-170170-// // Calculate how much of the surface is perpendicular to the view direction
171171-// float viewFactor = dot(normalize(vNormal), -viewDirection);
172172-173173-// // Invert and clamp the viewFactor to ensure a smooth fade on the edges
174174-// viewFactor = clamp(1.0 - viewFactor, 0.0, 1.0);
175175-176176-// // Use smoothstep for a smooth transition on the edges
177177-// float atmosphereFactor = smoothstep(0.2, 0.8, viewFactor);
178178-179179-// // Apply the blue tint more strongly on the edges
180180-// vec3 finalColor = mix(vec3(1.0), atmosphereColor, atmosphereFactor);
181181-182182-// // Output the final color
183183-// gl_FragColor = vec4(finalColor, atmosphereFactor);
184184-// }
185185-// `,
186186-// side: THREE.FrontSide,
187187-// transparent: true,
188188-// depthWrite: false,
189189-// };
190190-191191-// // Create atmosphere material using the shader
192192-// const atmosphereMaterial = new THREE.ShaderMaterial(atmosphereShader);
193193-194194-// // Create your atmosphere geometry and apply the material
195195-// const atmosphereGeometry = new THREE.SphereGeometry(1.3, 32, 32);
196196-// const atmosphereMesh = new THREE.Mesh(atmosphereGeometry, atmosphereMaterial);
197197-// atmosphereMesh.renderOrder = 1;
198198-// scene.add(atmosphereMesh);
199199-200200-// const atmosphereShader = {
201201-// uniforms: {
202202-// lightDirection: { value: new THREE.Vector3(1.0, 1.0, 0.0).normalize() }, // Sunlight direction
203203-// },
204204-// vertexShader: `
205205-// varying vec3 vNormal;
206206-// varying vec3 vViewLightDirection; // Light direction relative to the camera
207207-208208-// uniform vec3 lightDirection; // Light direction in world space
209209-210210-// void main() {
211211-// // Transform the vertex normal to world space
212212-// vNormal = normalize(normalMatrix * normal);
213213-// // Transform the light direction to view space
214214-// vViewLightDirection = (viewMatrix * vec4(lightDirection, 0.0)).xyz;
215215-216216-// gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
217217-// }
218218-// `,
219219-// fragmentShader: `
220220-// varying vec3 vNormal;
221221-// varying vec3 vViewLightDirection;
222222-223223-// void main() {
224224-// // Calculate the dot product of the light direction and the surface normal
225225-// float lightFactor = dot(normalize(vNormal), normalize(vViewLightDirection));
226226-227227-// // Use smoothstep to soften the transition from light to dark side
228228-// lightFactor = smoothstep(0.0, 0.5, lightFactor);
229229-230230-// // Ensure a minimum glow, even on the dark side
231231-// float minGlow = 0.5; // Adjust this to control how much it glows on the dark side
232232-// lightFactor = mix(minGlow, 1.0, lightFactor);
233233-234234-// // Adjust the intensity for the atmosphere's glow, including the minimum glow
235235-// float intensity = pow(0.7 - dot(normalize(vNormal), vec3(0, 0, 1.0)), 2.0);
236236-// intensity *= lightFactor;
237237-238238-// // Calculate an alpha value for fog-like fading effect near the edges
239239-// // This will make the atmosphere more transparent at the edges
240240-// float alpha = lightFactor * 0.6; // Adjust alpha factor for stronger or softer fade
241241-242242-// // Set the final fragment color with the computed intensity and alpha
243243-// gl_FragColor = vec4(0.3, 0.6, 1.0, alpha) * intensity;
244244-// }`,
245245-// side: THREE.BackSide, // Only render the back faces of the atmosphere
246246-// transparent: true, // Enable transparency for the fade effect
247247-// };
248248-249249-// // Create the atmosphere material
250250-// const atmosphereMaterial = new THREE.ShaderMaterial(atmosphereShader);
251251-252252-// // Create the atmosphere geometry
253253-// const atmosphereGeometry = new THREE.IcosahedronGeometry(1, 20);
254254-// const atmosphere = new THREE.Mesh(atmosphereGeometry, atmosphereMaterial);
255255-// const scale = 1.2;
256256-// atmosphere.scale.set(scale, scale, scale);
257257-// scene.add(atmosphere);
258258-25962let total = 0;
26063let lastDelta = 0;
26164renderer.setAnimationLoop((delta) => {
···2707327174 if (!hasPlanet) {
27275 console.log("Creating planet");
273273- createPlanet();
7676+ createPlanet("beach");
27477 hasPlanet = true;
27578 }
27679});
···28386 createPlanet("forest");
28487 } else if (event.key === "3") {
28588 createPlanet("snowForest");
286286- }
287287-288288- if (event.key === "a") {
289289- atmosphere.visible = !atmosphere.visible;
29089 }
29190});
29291···306105 }
307106308107 console.time("planet");
309309- const planet = new Planet({ preset });
108108+ const planet = new Planet({ biome: { preset } });
310109 let mesh = await planet.create();
311110 scene.remove(planetMesh);
312111 scene.add(mesh);
···11+import { ShaderMaterial, Vector3 } from "three";
22+33+const atmosphereShader = {
44+ uniforms: {
55+ lightDirection: { value: new Vector3(1.0, 1.0, 0.0).normalize() }, // Sunlight direction
66+ atmosphereColor: { value: new Vector3(0.3, 0.6, 1.0) }, // Blue tint for atmosphere
77+ },
88+ vertexShader: `
99+varying vec3 vNormal;
1010+varying vec3 vViewLightDirection; // Light direction relative to the camera
1111+varying vec3 vViewPosition; // View position
1212+1313+uniform vec3 lightDirection; // Light direction in world space
1414+1515+void main() {
1616+ // Transform the vertex normal to world space
1717+ vNormal = normalize(normalMatrix * normal);
1818+ // Transform the light direction to view space
1919+ vViewLightDirection = (viewMatrix * vec4(lightDirection, 0.0)).xyz;
2020+ vViewPosition = (modelViewMatrix * vec4(position, 1.0)).xyz;
2121+2222+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
2323+}
2424+`,
2525+ fragmentShader: `
2626+ varying vec3 vNormal;
2727+ varying vec3 vViewLightDirection;
2828+ varying vec3 vViewPosition;
2929+ uniform vec3 atmosphereColor;
3030+3131+ void main() {
3232+3333+ // Normalize the normal and the view direction
3434+ vec3 viewDirection = normalize(vViewPosition);
3535+3636+ // Calculate how much of the surface is perpendicular to the view direction
3737+ float viewFactor = dot(normalize(vNormal), -viewDirection);
3838+3939+ // Calculate the dot product of the light direction and the surface normal
4040+ float lightFactor = dot(normalize(vNormal), normalize(vViewLightDirection));
4141+4242+ // Use smoothstep to soften the transition from light to dark side
4343+ lightFactor = smoothstep(-0.2, 0.4, lightFactor);
4444+4545+ // Ensure a minimum glow, even on the dark side
4646+ float minGlow = 0.2; // Adjust this to control how much it glows on the dark side
4747+ lightFactor = mix(minGlow, 1.0, lightFactor);
4848+4949+ // Adjust the intensity for the atmosphere's glow, including the minimum glow
5050+ float dotProduct = dot(normalize(vNormal), vec3(0, 0.0, 1.0));
5151+ float intensity = pow(dotProduct, 8.0);
5252+ intensity *= lightFactor;
5353+5454+ viewFactor = clamp(1.0 - viewFactor, 0.0, 1.0);
5555+5656+ // Use smoothstep for a smooth transition on the edges
5757+ float atmosphereFactor = smoothstep(0.2, 0.6, viewFactor);
5858+5959+ // Output the final color
6060+ gl_FragColor = vec4(atmosphereColor, intensity * atmosphereFactor);
6161+ // Set the final fragment color with the computed intensity
6262+ //gl_FragColor = vec4(0.3, 0.6, 1.0, 1.0) * intensity;
6363+ }`,
6464+ transparent: true,
6565+ depthWrite: false,
6666+};
6767+6868+const atmosphereMaterial = new ShaderMaterial(atmosphereShader);
6969+7070+export default atmosphereMaterial;