功能:板件和拉伸实体支持引用材质的<世界坐标>材质贴图平铺方式

pull/1770/MERGE
ChenX 3 years ago
parent 22d12b1f58
commit c861c742e4

@ -3,26 +3,10 @@ import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
import { HostApplicationServices } from "../../ApplicationServices/HostApplicationServices"; import { HostApplicationServices } from "../../ApplicationServices/HostApplicationServices";
import { LoadImageFromUrl } from "../../Loader/ImageLoader"; import { LoadImageFromUrl } from "../../Loader/ImageLoader";
import { GenerateCdnUrl } from "./GenerateCdnUrl"; import { GenerateCdnUrl } from "./GenerateCdnUrl";
import { USE_WORLD_UV, U_MOVE, U_REP, U_RO, U_WORLD_MOVE, U_WORLD_REP, U_WORLD_RO, V_MOVE, V_REP, V_WORLD_MOVE, V_WORLD_REP } from "./USE_WORLD_UV";
export const UE_FBX_LOADER = new FBXLoader(); export const UE_FBX_LOADER = new FBXLoader();
export const USE_WORLD_UV = "USE_WORLD_UV";
export const U_WORLD_REP = "u_w_rep";
export const V_WORLD_REP = "v_w_rep";
export const U_WORLD_MOVE = "u_w_move";
export const V_WORLD_MOVE = "v_w_move";
export const U_WORLD_RO = "v_w_ro";
export const U_REP = "u_rep";
export const V_REP = "v_rep";
export const U_MOVE = "v_move";
export const V_MOVE = "v_move";
export const U_RO = "v_ro";
export async function ParseMaterialImages(url: string): Promise<Map<string, HTMLImageElement>> export async function ParseMaterialImages(url: string): Promise<Map<string, HTMLImageElement>>
{ {
let name = ParseUrlName(url); let name = ParseUrlName(url);
@ -163,7 +147,6 @@ export async function ConverMaterial2(m: MeshPhongMaterial, url: string)
mtl[U_WORLD_MOVE] = data[U_WORLD_MOVE] ?? 0; mtl[U_WORLD_MOVE] = data[U_WORLD_MOVE] ?? 0;
mtl[V_WORLD_MOVE] = data[V_WORLD_MOVE] ?? 0; mtl[V_WORLD_MOVE] = data[V_WORLD_MOVE] ?? 0;
} }
else else
{ {

@ -0,0 +1,17 @@
export const USE_WORLD_UV = "USE_WORLD_UV";
export const U_WORLD_REP = "u_w_rep";
export const V_WORLD_REP = "v_w_rep";
export const U_WORLD_MOVE = "u_w_move";
export const V_WORLD_MOVE = "v_w_move";
export const U_WORLD_RO = "v_w_ro";
export const U_REP = "u_rep";
export const V_REP = "v_rep";
export const U_MOVE = "v_move";
export const V_MOVE = "v_move";
export const U_RO = "v_ro";

@ -18,7 +18,9 @@ import { CADFiler } from "../CADFiler";
import { Contour } from "../Contour"; import { Contour } from "../Contour";
import { DragPointType } from "../Entity/DragPointType"; import { DragPointType } from "../Entity/DragPointType";
import { ExtrudeSolid, ExtureContourCurve } from "../Entity/Extrude"; import { ExtrudeSolid, ExtureContourCurve } from "../Entity/Extrude";
import { GenUVForWorld } from "../Entity/GenUVForWorld";
import { Polyline } from "../Entity/Polyline"; import { Polyline } from "../Entity/Polyline";
import { PhysicalMaterialRecord } from "../PhysicalMaterialRecord";
import { Shape } from "../Shape"; import { Shape } from "../Shape";
import { OBB } from './../../Geometry/OBB/obb'; import { OBB } from './../../Geometry/OBB/obb';
import { Hole } from "./Hole"; import { Hole } from "./Hole";
@ -199,6 +201,16 @@ export class ExtrudeHole extends Hole
}; };
let geo = new ExtrudeGeometry(this.ContourCurve.Shape, extrudeSettings); let geo = new ExtrudeGeometry(this.ContourCurve.Shape, extrudeSettings);
geo.applyMatrix4(this._contourCurve.OCS); geo.applyMatrix4(this._contourCurve.OCS);
let mtl = this.Material?.Object as PhysicalMaterialRecord ?? this.Db?.DefaultMaterial;
if (mtl?.UseWorldUV)
{
let bgeo = new BufferGeometry().fromGeometry(geo);
let gen = new GenUVForWorld();
gen.GenGeoUV(bgeo, mtl.Material);
return bgeo;
}
else
ScaleUV(geo); ScaleUV(geo);
return geo; return geo;
} }

@ -38,6 +38,7 @@ import { DragPointType } from "./DragPointType";
import { Ellipse } from "./Ellipse"; import { Ellipse } from "./Ellipse";
import { Entity } from "./Entity"; import { Entity } from "./Entity";
import { ExtrudeConfig } from "./ExtrudeConfig"; import { ExtrudeConfig } from "./ExtrudeConfig";
import { GenUVForWorld } from './GenUVForWorld';
import { Line } from "./Line"; import { Line } from "./Line";
import { Polyline } from "./Polyline"; import { Polyline } from "./Polyline";
import { Region } from "./Region"; import { Region } from "./Region";
@ -118,6 +119,11 @@ export class ExtrudeSolid extends Entity
let isf_new = Boolean((materialId?.Object as PhysicalMaterialRecord)?.IsFull); let isf_new = Boolean((materialId?.Object as PhysicalMaterialRecord)?.IsFull);
if (isf_old !== isf_new) if (isf_old !== isf_new)
this.Update(); this.Update();
else if ((materialId?.Object as PhysicalMaterialRecord)?.UseWorldUV)
{
if (this._MeshGeometry)
this.GenWorldUV(this._MeshGeometry);
}
} }
get Material() { return super.Material; } get Material() { return super.Material; }
@ -1857,6 +1863,7 @@ export class ExtrudeSolid extends Entity
let bgeo = new BufferGeometry().fromGeometry(geo); let bgeo = new BufferGeometry().fromGeometry(geo);
bgeo["IsMesh"] = true; bgeo["IsMesh"] = true;
this._MeshGeometry = bgeo; this._MeshGeometry = bgeo;
this.GenWorldUV(bgeo);
return bgeo; return bgeo;
} }
@ -1866,6 +1873,7 @@ export class ExtrudeSolid extends Entity
if (grooves.length < MaxDrawGrooveCount) if (grooves.length < MaxDrawGrooveCount)
this._EdgeGeometry = builder.EdgeGeometry; this._EdgeGeometry = builder.EdgeGeometry;
this.UpdateUV(null, null); this.UpdateUV(null, null);
this.GenWorldUV(this._MeshGeometry);
return this._MeshGeometry; return this._MeshGeometry;
} }
@ -1903,13 +1911,9 @@ export class ExtrudeSolid extends Entity
UpdateUV(geo: Geometry, ocs: Matrix4, isRev = false) UpdateUV(geo: Geometry, ocs: Matrix4, isRev = false)
{ {
let mat: PhysicalMaterialRecord; let mtl = (this.Material?.Object as PhysicalMaterialRecord) ?? this.Db?.DefaultMaterial;
if (this.Material && this.Material.Object)
mat = this.Material.Object as PhysicalMaterialRecord;
else
mat = this.Db?.DefaultMaterial;
if (mat && mat.IsFull) if (mtl?.IsFull)
{ {
if (geo) if (geo)
ScaleUV2(geo, ocs, this.width, this.height, isRev); ScaleUV2(geo, ocs, this.width, this.height, isRev);
@ -1922,6 +1926,18 @@ export class ExtrudeSolid extends Entity
ScaleUV(geo); ScaleUV(geo);
} }
} }
GenWorldUV(geo: BufferGeometry)
{
let mtl = (this.Material?.Object as PhysicalMaterialRecord) ?? this.Db?.DefaultMaterial;
if (mtl?.UseWorldUV)
{
let gen = new GenUVForWorld;
gen.GenGeoUV(geo, mtl.Material);
}
}
UpdateBufferGeometryUvs(isRev: boolean) UpdateBufferGeometryUvs(isRev: boolean)
{ {
let uvs = this._MeshGeometry.attributes.uv; let uvs = this._MeshGeometry.attributes.uv;

@ -1,5 +1,5 @@
import { Box3, BufferAttribute, BufferGeometry, Material, Matrix4, Mesh, Vector3 } from "three"; import { Box3, BufferAttribute, BufferGeometry, Material, Matrix4, Mesh, Vector3 } from "three";
import { USE_WORLD_UV, U_WORLD_MOVE, U_WORLD_REP, V_WORLD_MOVE, V_WORLD_REP } from "../../Add-on/testEntity/ParseMaterialImage"; import { USE_WORLD_UV, U_WORLD_MOVE, U_WORLD_REP, V_WORLD_MOVE, V_WORLD_REP } from "../../Add-on/testEntity/USE_WORLD_UV";
import { XAxis, XAxisN, YAxis, YAxisN, ZAxis, ZAxisN } from "../../Geometry/GeUtils"; import { XAxis, XAxisN, YAxis, YAxisN, ZAxis, ZAxisN } from "../../Geometry/GeUtils";
import { Orbit } from "../../Geometry/Orbit"; import { Orbit } from "../../Geometry/Orbit";
@ -103,8 +103,12 @@ export class GenUVForWorld
{ {
let mtl = mesh.material; let mtl = mesh.material;
if (mtl[USE_WORLD_UV]) if (mtl[USE_WORLD_UV])
this.GenGeoUV(mesh.geometry, mtl, 1e-2);
}
}
GenGeoUV(geo: BufferGeometry, mtl: Material, scale = 1e-3)
{ {
let geo = mesh.geometry;
if (!geo.boundingBox) if (!geo.boundingBox)
geo.computeBoundingBox(); geo.computeBoundingBox();
@ -124,12 +128,10 @@ export class GenUVForWorld
this._Box.applyMatrix4(mtx); this._Box.applyMatrix4(mtx);
//@ts-ignore //@ts-ignore
uvs.array[y * 2] = (((this._X.x - (this._Box.min.x + this._Box.max.x) * 0.5)) * 1e-2 + mtl[U_WORLD_MOVE]) * mtl[U_WORLD_REP] + 0.5; uvs.array[y * 2] = (((this._X.x - (this._Box.min.x + this._Box.max.x) * 0.5)) * scale + mtl[U_WORLD_MOVE]) * mtl[U_WORLD_REP] + 0.5;
//@ts-ignore //@ts-ignore
uvs.array[y * 2 + 1] = (((this._X.y - (this._Box.min.y + this._Box.max.y) * 0.5)) * 1e-2 + mtl[V_WORLD_MOVE]) * mtl[V_WORLD_REP] + 0.5; uvs.array[y * 2 + 1] = (((this._X.y - (this._Box.min.y + this._Box.max.y) * 0.5)) * scale + mtl[V_WORLD_MOVE]) * mtl[V_WORLD_REP] + 0.5;
} }
uvs.needsUpdate = true; uvs.needsUpdate = true;
} }
} }
}
}

@ -1,4 +1,5 @@
import { Color, MeshPhysicalMaterial } from "three"; import { Color, MeshPhysicalMaterial } from "three";
import { USE_WORLD_UV, U_WORLD_MOVE, U_WORLD_REP, U_WORLD_RO, V_WORLD_MOVE, V_WORLD_REP } from "../Add-on/testEntity/USE_WORLD_UV";
import { HostApplicationServices } from "../ApplicationServices/HostApplicationServices"; import { HostApplicationServices } from "../ApplicationServices/HostApplicationServices";
import { AutoRecord } from "./AutoRecord"; import { AutoRecord } from "./AutoRecord";
import { Factory } from "./CADFactory"; import { Factory } from "./CADFactory";
@ -80,6 +81,14 @@ export class PhysicalMaterialRecord extends MaterialTableRecord
@AutoRecord sharpen = 1;//锐化 默认1 范围0-20 默认折叠 @AutoRecord sharpen = 1;//锐化 默认1 范围0-20 默认折叠
get UseWorldUV() { return this.UVType === UVType.WorldUV; }
set UseWorldUV(b: boolean) { this.UVType = b ? UVType.WorldUV : UVType.LocalUV; }
@AutoRecord UWroldRep = 1;
@AutoRecord VWroldRep = 1;
@AutoRecord UWroldRo = 0;
@AutoRecord UWorldMove = 0;
@AutoRecord VWorldMove = 0;
@AutoRecord depthTest: boolean = true;//深度测试(默认true)(弃用(不在UI上显示) @AutoRecord depthTest: boolean = true;//深度测试(默认true)(弃用(不在UI上显示)
@ -91,6 +100,18 @@ export class PhysicalMaterialRecord extends MaterialTableRecord
private material = new MeshPhysicalMaterial({}); private material = new MeshPhysicalMaterial({});
async Update() async Update()
{ {
this.material[USE_WORLD_UV] = this.UseWorldUV;
if (this.material[USE_WORLD_UV])
{
this.material[U_WORLD_REP] = this.UWroldRep;
this.material[V_WORLD_REP] = this.VWroldRep;
this.material[U_WORLD_RO] = this.UWroldRo;
this.material[U_WORLD_MOVE] = this.UWorldMove;
this.material[V_WORLD_MOVE] = this.VWorldMove;
}
if (!this.material.color) if (!this.material.color)
this.material.color = new Color(this.color); this.material.color = new Color(this.color);
else else
@ -266,6 +287,15 @@ export class PhysicalMaterialRecord extends MaterialTableRecord
this.type = file.Read(); this.type = file.Read();
if (ver > 7) if (ver > 7)
this.ref = file.Read(); this.ref = file.Read();
if (ver > 8)
{
this.UWroldRep = file.Read();
this.VWroldRep = file.Read();
this.UWroldRo = file.Read();
this.UWorldMove = file.Read();
this.VWorldMove = file.Read();
}
} }
this.Update(); this.Update();
@ -274,7 +304,7 @@ export class PhysicalMaterialRecord extends MaterialTableRecord
WriteFile(file: CADFiler) WriteFile(file: CADFiler)
{ {
super.WriteFile(file); super.WriteFile(file);
file.Write(8); file.Write(9);
file.Write(this.color); file.Write(this.color);
file.Write(this.transparent); file.Write(this.transparent);
file.Write(this.matalness); file.Write(this.matalness);
@ -335,6 +365,13 @@ export class PhysicalMaterialRecord extends MaterialTableRecord
//ver8 //ver8
file.Write(this.ref); file.Write(this.ref);
//ver9
file.Write(this.UWroldRep);
file.Write(this.VWroldRep);
file.Write(this.UWroldRo);
file.Write(this.UWorldMove);
file.Write(this.VWorldMove);
} }
//#endregion //#endregion
} }

@ -1,9 +1,10 @@
import { Intent } from "@blueprintjs/core"; import { Intent } from "@blueprintjs/core";
import { action, observable } from "mobx"; import { action, observable } from "mobx";
import { Color, MathUtils, Matrix4, MirroredRepeatWrapping, Object3D, Vector3 } from "three"; import { Color, MathUtils, Matrix4, Object3D, RepeatWrapping, Vector3 } from "three";
import { begin } from "xaop"; import { begin } from "xaop";
import { GenerateCdnUrl } from "../../../../Add-on/testEntity/GenerateCdnUrl"; import { GenerateCdnUrl } from "../../../../Add-on/testEntity/GenerateCdnUrl";
import { ParseUrlName } from "../../../../Add-on/testEntity/ParseMaterialImage"; import { ParseUrlName } from "../../../../Add-on/testEntity/ParseMaterialImage";
import { U_MOVE, U_REP, U_RO, U_WORLD_MOVE, U_WORLD_REP, U_WORLD_RO, V_MOVE, V_REP, V_WORLD_MOVE, V_WORLD_REP } from "../../../../Add-on/testEntity/USE_WORLD_UV";
import { app } from "../../../../ApplicationServices/Application"; import { app } from "../../../../ApplicationServices/Application";
import { CommandNames } from "../../../../Common/CommandNames"; import { CommandNames } from "../../../../Common/CommandNames";
import { UserCollection } from "../../../../Common/HostUrl"; import { UserCollection } from "../../../../Common/HostUrl";
@ -236,18 +237,44 @@ export default class ResourceStore
let texturePath = data.baseColor_texturePath as string; let texturePath = data.baseColor_texturePath as string;
if (!texturePath.includes("CT")) if (!texturePath.includes("CT"))
{ {
let t = app.Database.TextureTable.GetAt(texturePath) as TextureTableRecord; let t = new TextureTableRecord();
if (!t)
{
t = new TextureTableRecord();
t.Name = texturePath;
t.imageUrl = texturePath; t.imageUrl = texturePath;
t.WrapS = MirroredRepeatWrapping; t.WrapS = RepeatWrapping;
t.WrapT = MirroredRepeatWrapping; t.WrapT = RepeatWrapping;
app.Database.TextureTable.Add(t);
//世界坐标系UV
mtl.UseWorldUV = !(data.uv_enable && data.uv_type === 0);
if (mtl.UseWorldUV)
{
mtl.UWroldRep = data[U_WORLD_REP] ?? 1;
mtl.VWroldRep = data[V_WORLD_REP] ?? 1;
mtl.UWroldRo = data[U_WORLD_RO] ?? 0;
mtl.UWorldMove = data[U_WORLD_MOVE] ?? 0;
mtl.UWorldMove = data[V_WORLD_MOVE] ?? 0;
}
else
{
if (data[U_REP])
{
t.repeatX = 1 / data[U_REP];
t.repeatY = 1 / data[V_REP];
t.rotation = data[U_RO] ?? 0;
t.moveX = data[U_MOVE] ?? 0;
t.moveY = data[V_MOVE] ?? 0;
}
}
t.Name = `${t.imageUrl}!${t.repeatX}!${t.repeatY}!${t.rotation}!${t.moveX}!${t.moveY}`;;
let ot = app.Database.TextureTable.GetAt(t.Name) as TextureTableRecord;
if (ot) t = ot;
else
{
t.Update(); t.Update();
app.Database.TextureTable.Add(t);
} }
mtl.map = t.objectId; mtl.map = t.objectId;
mtl.useMap = true; mtl.useMap = true;
} }

Loading…
Cancel
Save