맵 3D: 재질 · 텍스처(디퓨즈 256px) · 두 층 섞기 · 법선 포함 glb, 사이트가 glb 재질로 그림
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -159,8 +159,9 @@ export default function ReplayViewer({ m, replay, sync }: Props) {
|
||||
controls.update();
|
||||
};
|
||||
resetCam.current();
|
||||
scene.add(new THREE.HemisphereLight(0xffffff, 0x223344, 1.6));
|
||||
const sun = new THREE.DirectionalLight(0xffffff, 1.2);
|
||||
// 게임처럼 밝게: 하늘빛 + 해 (맵 텍스처가 어둡게 묻히지 않게)
|
||||
scene.add(new THREE.HemisphereLight(0xffffff, 0x8a8f99, 2.6));
|
||||
const sun = new THREE.DirectionalLight(0xfff4e0, 1.8);
|
||||
sun.position.set(30, 80, 20);
|
||||
scene.add(sun);
|
||||
|
||||
@@ -200,8 +201,8 @@ export default function ReplayViewer({ m, replay, sync }: Props) {
|
||||
.then((info) => {
|
||||
if (disposed) return;
|
||||
if (!info) return setMap3d({ state: 'none' });
|
||||
const mat = mapMaterial(clip, { lo: 0, hi: Math.max(8, (zTop - zBase + 600) * hPerCm) });
|
||||
return loadMap3d(info.url, mat, (p) => !disposed && setMap3d({ state: 'loading', progress: p })).then((obj) => {
|
||||
const fallback = () => mapMaterial(clip, { lo: 0, hi: Math.max(8, (zTop - zBase + 600) * hPerCm) });
|
||||
return loadMap3d(info.url, clip, fallback, (p) => !disposed && setMap3d({ state: 'loading', progress: p })).then((obj) => {
|
||||
if (disposed) return;
|
||||
mapGroup.add(obj);
|
||||
mapGroup.updateMatrixWorld(true);
|
||||
|
||||
@@ -40,22 +40,57 @@ export async function findMap3d(mapUrl: string): Promise<Map3dInfo | null> {
|
||||
return hit ? { url: `/site/maps3d/${encodeURIComponent(idx.dir)}/${encodeURIComponent(hit.file)}`, bytes: hit.bytes, triangles: hit.triangles, version: idx.version } : null;
|
||||
}
|
||||
|
||||
/** glb 를 받아 메시 하나로. onProgress: 0~1 */
|
||||
export function loadMap3d(url: string, material: THREE.Material, onProgress?: (p: number) => void): Promise<THREE.Object3D> {
|
||||
/**
|
||||
* glb 를 받아 장면 하나로. onProgress: 0~1
|
||||
* 재질이 든 glb(텍스처 · 색)는 그 재질에 천장 자르기만 붙이고, 재질이 없는 옛 glb 는 fallback(높이 색) 재질을 쓴다.
|
||||
* 두 층 재질(extras.blend = {texture, tint})은 정점 색 R 로 A 층(기본 텍스처) · B 층을 섞는다.
|
||||
*/
|
||||
export function loadMap3d(url: string, clip: THREE.Plane, fallback: () => THREE.Material, onProgress?: (p: number) => void): Promise<THREE.Object3D> {
|
||||
const loader = new GLTFLoader();
|
||||
loader.setMeshoptDecoder(MeshoptDecoder);
|
||||
return new Promise((resolve, reject) => {
|
||||
loader.load(
|
||||
url,
|
||||
(gltf) => {
|
||||
const hasMats = !!gltf.parser.json.materials?.length;
|
||||
const fb = hasMats ? null : fallback();
|
||||
const pending: Promise<unknown>[] = [];
|
||||
const done = new Map<THREE.Material, THREE.Material>();
|
||||
gltf.scene.traverse((o) => {
|
||||
const mesh = o as THREE.Mesh;
|
||||
if (mesh.isMesh) {
|
||||
mesh.material = material;
|
||||
mesh.frustumCulled = false;
|
||||
if (!mesh.isMesh) return;
|
||||
mesh.frustumCulled = false;
|
||||
if (fb) {
|
||||
mesh.material = fb;
|
||||
return;
|
||||
}
|
||||
const src = mesh.material as THREE.MeshStandardMaterial;
|
||||
const blend = src.userData?.blend as { texture?: number; tint?: number[] } | undefined;
|
||||
// 정점 색은 색칠이 아니라 두 층 섞는 비율이다
|
||||
const col = mesh.geometry.getAttribute('color');
|
||||
if (col) {
|
||||
mesh.geometry.setAttribute('blendCol', col);
|
||||
mesh.geometry.deleteAttribute('color');
|
||||
}
|
||||
let mat = done.get(src);
|
||||
if (!mat) {
|
||||
const m = src.clone() as THREE.MeshStandardMaterial;
|
||||
m.vertexColors = false;
|
||||
// 게임(UE) 좌표를 그대로 쓰고 행렬로 옮기므로 면 방향이 뒤집혀 보일 수 있다 → 양면
|
||||
m.side = THREE.DoubleSide;
|
||||
m.clippingPlanes = [clip];
|
||||
m.clipShadows = true;
|
||||
m.roughness = 1;
|
||||
m.metalness = 0;
|
||||
if (m.map) m.map.anisotropy = 4;
|
||||
if (m.transparent) m.depthWrite = false;
|
||||
if (blend) pending.push(blendLayer(gltf.parser, m, blend));
|
||||
done.set(src, m);
|
||||
mat = m;
|
||||
}
|
||||
mesh.material = mat;
|
||||
});
|
||||
resolve(gltf.scene);
|
||||
void Promise.all(pending).then(() => resolve(gltf.scene), reject);
|
||||
},
|
||||
(e) => onProgress?.(e.total ? e.loaded / e.total : 0),
|
||||
reject,
|
||||
@@ -63,6 +98,39 @@ export function loadMap3d(url: string, material: THREE.Material, onProgress?: (p
|
||||
});
|
||||
}
|
||||
|
||||
/** B 층 텍스처 · 색을 붙이고 셰이더에서 정점 색 R 로 섞는다 */
|
||||
async function blendLayer(parser: { getDependency: (type: string, i: number) => Promise<unknown> }, m: THREE.MeshStandardMaterial, b: { texture?: number; tint?: number[] }) {
|
||||
let texB: THREE.Texture | null = null;
|
||||
if (b.texture != null) {
|
||||
texB = (await parser.getDependency('texture', b.texture)) as THREE.Texture;
|
||||
texB.colorSpace = THREE.SRGBColorSpace;
|
||||
texB.anisotropy = 4;
|
||||
}
|
||||
const tintB = new THREE.Color().setRGB(b.tint?.[0] ?? 1, b.tint?.[1] ?? 1, b.tint?.[2] ?? 1, THREE.SRGBColorSpace);
|
||||
const md = m as unknown as { defines?: Record<string, string> };
|
||||
md.defines = { ...(md.defines ?? {}), GVR_BLEND: '', ...(texB ? { GVR_BLEND_MAP: '' } : {}) };
|
||||
m.onBeforeCompile = (sh) => {
|
||||
sh.uniforms.mapB = { value: texB };
|
||||
sh.uniforms.tintB = { value: tintB };
|
||||
sh.vertexShader = sh.vertexShader
|
||||
.replace('#include <common>', '#include <common>\nattribute vec4 blendCol;\nvarying float vBlend;\nvarying vec2 vUvB;')
|
||||
.replace('#include <uv_vertex>', '#include <uv_vertex>\nvBlend = blendCol.r;\nvUvB = uv;');
|
||||
sh.fragmentShader = sh.fragmentShader
|
||||
.replace('#include <common>', '#include <common>\nuniform sampler2D mapB;\nuniform vec3 tintB;\nvarying float vBlend;\nvarying vec2 vUvB;')
|
||||
.replace(
|
||||
'#include <map_fragment>',
|
||||
`#include <map_fragment>
|
||||
#ifdef GVR_BLEND_MAP
|
||||
vec3 layerB = texture2D(mapB, vUvB).rgb * tintB;
|
||||
#else
|
||||
vec3 layerB = tintB;
|
||||
#endif
|
||||
diffuseColor.rgb = mix(layerB, diffuseColor.rgb, vBlend);`,
|
||||
);
|
||||
};
|
||||
m.needsUpdate = true;
|
||||
}
|
||||
|
||||
/** UE 월드 좌표(cm) → 3D 리플레이 화면 좌표. ReplayViewer 의 toWorld 와 같은 식을 행렬로 */
|
||||
export function worldMatrix(T: { xMultiplier: number; yMultiplier: number; xScalarToAdd: number; yScalarToAdd: number }, S: number, zBase: number, vex: number) {
|
||||
const h = Math.abs(T.xMultiplier) * S * vex;
|
||||
|
||||
Reference in New Issue
Block a user