Improve foliage

This commit is contained in:
bmathews 2025-11-15 01:42:32 -08:00
parent 7ba58e6a5b
commit b678b0110c

View file

@ -7,6 +7,7 @@ import {
setupAlphaAsRoughnessTexture, setupAlphaAsRoughnessTexture,
} from "../shaderMaterials"; } from "../shaderMaterials";
import { MeshStandardMaterial } from "three"; import { MeshStandardMaterial } from "three";
import { setupColor } from "../textureUtils";
const FALLBACK_URL = `${BASE_URL}/black.png`; const FALLBACK_URL = `${BASE_URL}/black.png`;
@ -20,19 +21,34 @@ export function useStaticShape(shapeName: string) {
export function ShapeTexture({ export function ShapeTexture({
material, material,
shapeName,
}: { }: {
material?: MeshStandardMaterial; material?: MeshStandardMaterial;
shapeName?: string;
}) { }) {
const url = shapeTextureToUrl(material.name, FALLBACK_URL); const url = shapeTextureToUrl(material.name, FALLBACK_URL);
const texture = useTexture(url, (texture) => const isOrganic = shapeName && /borg|xorg/i.test(shapeName);
setupAlphaAsRoughnessTexture(texture)
);
// Create or reuse shader material that uses alpha channel as roughness const texture = useTexture(url, (texture) => {
const shaderMaterial = createAlphaAsRoughnessMaterial(); if (!isOrganic) {
shaderMaterial.map = texture; setupAlphaAsRoughnessTexture(texture);
}
return setupColor(texture);
});
return <primitive object={shaderMaterial} attach="material" />; // Only use alpha-as-roughness material for borg shapes
if (!isOrganic) {
const shaderMaterial = createAlphaAsRoughnessMaterial();
shaderMaterial.map = texture;
return <primitive object={shaderMaterial} attach="material" />;
}
// For non-borg shapes, use the original GLTF material with updated texture
const clonedMaterial = material.clone();
clonedMaterial.map = texture;
clonedMaterial.transparent = true;
clonedMaterial.alphaTest = 0.9;
return <primitive object={clonedMaterial} attach="material" />;
} }
export function ShapeModel({ shapeName }: { shapeName: string }) { export function ShapeModel({ shapeName }: { shapeName: string }) {
@ -78,11 +94,13 @@ export function ShapeModel({ shapeName }: { shapeName: string }) {
<ShapeTexture <ShapeTexture
key={index} key={index}
material={mat as MeshStandardMaterial} material={mat as MeshStandardMaterial}
shapeName={shapeName}
/> />
)) ))
) : ( ) : (
<ShapeTexture <ShapeTexture
material={node.material as MeshStandardMaterial} material={node.material as MeshStandardMaterial}
shapeName={shapeName}
/> />
)} )}
</Suspense> </Suspense>