Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
9045cb79c2 | |||
b9f14813fd | |||
8a693f005b | |||
c5f97495e8 | |||
e369ebe87b | |||
e14068f507 | |||
7f45d5c9db | |||
13312f1ddf | |||
3aebd9ed95 | |||
3d27bccdea | |||
1a04bb8d20 |
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "material-editor",
|
||||
"private": true,
|
||||
"version": "1.0.23",
|
||||
"version": "1.0.34",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
|
@ -1,12 +1,13 @@
|
||||
<template>
|
||||
<div vertical class="material-adjuster">
|
||||
<div class="adjust-section">
|
||||
<!-- 新增模式下,隐藏材质名输入框 -->
|
||||
<div v-if="ignoreTexture" class="adjust-section">
|
||||
<h3>操作</h3>
|
||||
<fieldset v-if="debugMode" style="margin: 1em 0;">
|
||||
<legend>DEBUG</legend>
|
||||
<label>上传路径ID</label>
|
||||
<input v-model="materialInfo.dirId" type="text" placeholder="材质路径ID" />
|
||||
<label>配置JSON</label>
|
||||
<label>纹理(BASE64)</label>
|
||||
<input v-model="materialInfo.inputText" type="text" />
|
||||
<button class="btn-success" style="min-width: 110px;" @click="loadData">加载</button>
|
||||
</fieldset>
|
||||
@ -16,7 +17,8 @@
|
||||
<CfFlex gap="1em" v-if="debugMode">
|
||||
<button class="btn-success" style="min-width: 110px;" @click="HandleUpload">保存</button>
|
||||
<button class="btn-danger" style="min-width: 110px;" @click="HandleCancel">取消</button>
|
||||
<button v-if="debugMode" class="btn-primary" style="min-width: 110px;" @click="HandleGenerateLogo">预览缩略图</button>
|
||||
<button v-if="debugMode" class="btn-primary" style="min-width: 110px;"
|
||||
@click="HandleGenerateLogo">预览缩略图</button>
|
||||
</CfFlex>
|
||||
</div>
|
||||
|
||||
@ -38,27 +40,28 @@
|
||||
<h3>材质调节</h3>
|
||||
|
||||
<label>金属度</label>
|
||||
<CfFlex gap="1em">
|
||||
<input v-model="Material.matalness" @input="UpdateMaterial" type="range" min="0" max="1" step="0.01" />
|
||||
<span>{{ Material.matalness }}</span>
|
||||
<CfFlex gap="1em" class="input-range">
|
||||
<input v-model="metallic" type="range" min="0" max="1" step="0.01" />
|
||||
<input v-model="metallic" type="number" @change="ClampNumericValue" min="0" max="1" step="0.01" />
|
||||
</CfFlex>
|
||||
|
||||
<label>粗糙度</label>
|
||||
<CfFlex gap="1em">
|
||||
<input v-model="Material.roughness" @input="UpdateMaterial" type="range" min="0" max="1" step="0.01" />
|
||||
<span>{{ Material.roughness }}</span>
|
||||
<CfFlex gap="1em" class="input-range">
|
||||
<input v-model="roughness" type="range" min="0" max="1" step="0.01" />
|
||||
<input v-model="roughness" type="number" @change="ClampNumericValue" min="0" max="1" step="0.01" />
|
||||
</CfFlex>
|
||||
|
||||
<label>法线强度</label>
|
||||
<CfFlex gap="1em">
|
||||
<input v-model="Material.bumpScale" @input="UpdateMaterial" type="range" min="0" max="1" step="0.01" />
|
||||
<span>{{ Material.bumpScale }}</span>
|
||||
<CfFlex gap="1em" class="input-range">
|
||||
<input v-model="normalScale" type="range" min="0" max="1" step="0.01" />
|
||||
<input v-model="normalScale" type="number" @change="ClampNumericValue" min="0" max="1" step="0.01" />
|
||||
</CfFlex>
|
||||
|
||||
<label>高光</label>
|
||||
<CfFlex gap="1em">
|
||||
<input v-model="Material.specular" @input="UpdateMaterial" type="range" min="0" max="1" step="0.01" />
|
||||
<span>{{ Material.specular }}</span>
|
||||
<CfFlex gap="1em" class="input-range">
|
||||
<input v-model="emissiveIntensity" type="range" min="0" max="1" step="0.01" />
|
||||
<input v-model="emissiveIntensity" type="number" @change="ClampNumericValue" min="0" max="1"
|
||||
step="0.01" />
|
||||
</CfFlex>
|
||||
</div>
|
||||
|
||||
@ -69,16 +72,16 @@
|
||||
|
||||
<label>平铺U</label>
|
||||
<select v-model="textureAdjustment.wrapS">
|
||||
<option value="0">镜像平铺</option>
|
||||
<option value="1">平铺</option>
|
||||
<option value="2">展开</option>
|
||||
<option :value="MirroredRepeatWrapping">镜像平铺</option>
|
||||
<option :value="RepeatWrapping">平铺</option>
|
||||
<option :value="ClampToEdgeWrapping">展开</option>
|
||||
</select>
|
||||
|
||||
<label>平铺V</label>
|
||||
<select v-model="textureAdjustment.wrapT">
|
||||
<option value="0">镜像平铺</option>
|
||||
<option value="1">平铺</option>
|
||||
<option value="2">展开</option>
|
||||
<option :value="MirroredRepeatWrapping">镜像平铺</option>
|
||||
<option :value="RepeatWrapping">平铺</option>
|
||||
<option :value="ClampToEdgeWrapping">展开</option>
|
||||
</select>
|
||||
|
||||
<label>旋转</label>
|
||||
@ -117,6 +120,8 @@ import { IsNullOrWhitespace } from "../helpers/helper.string";
|
||||
import { FromDeflateBase64, ToDeflatedBase64 } from "../helpers/helper.material";
|
||||
import { storeToRefs } from "pinia";
|
||||
import { DownloadFile } from "../helpers/helper.web";
|
||||
import MathHelper from "../helpers/MathHelper";
|
||||
import { ClampToEdgeWrapping, MirroredRepeatWrapping, RepeatWrapping, Wrapping } from "three";
|
||||
|
||||
export interface MaterialRequest {
|
||||
/** 材质名 */
|
||||
@ -132,7 +137,7 @@ const { Material, CurrGeometry, Geometries } = storeToRefs(scene);
|
||||
|
||||
const props = defineProps<{
|
||||
name?: string;
|
||||
textureSrcList?: string[];
|
||||
textures?: { name: string, src: string }[];
|
||||
/** 忽略纹理参数,提交时直接输出场景内的材质,用于某些在组件外对场景进行编辑的特殊情况(例如材质编辑模式) */
|
||||
ignoreTexture?: boolean;
|
||||
}>();
|
||||
@ -142,17 +147,50 @@ const emits = defineEmits<{
|
||||
}>();
|
||||
|
||||
const debugMode = ref(false);
|
||||
const _textureSrc = ref(props.textureSrcList);
|
||||
const Textures = computed(() => props.textures || []);
|
||||
const debugTextureSrc = ref("");
|
||||
const textureAdjustment = ref<TextureAdjustment>({
|
||||
wrapS: 0,
|
||||
wrapT: 0,
|
||||
wrapS: MirroredRepeatWrapping,
|
||||
wrapT: MirroredRepeatWrapping,
|
||||
rotation: 0,
|
||||
repeatX: 1,
|
||||
repeatY: 1,
|
||||
moveX: 0,
|
||||
moveY: 0
|
||||
});
|
||||
|
||||
const metallic = computed({
|
||||
get: () => Material.value.matalness,
|
||||
set: (val) => {
|
||||
Material.value.matalness = MathHelper.clamp(val, 0, 1);
|
||||
UpdateMaterial();
|
||||
}
|
||||
});
|
||||
|
||||
const roughness = computed({
|
||||
get: () => Material.value.roughness,
|
||||
set: (val) => {
|
||||
Material.value.roughness = MathHelper.clamp(val, 0, 1);
|
||||
UpdateMaterial();
|
||||
}
|
||||
});
|
||||
|
||||
const normalScale = computed({
|
||||
get: () => Material.value.bumpScale,
|
||||
set: (val) => {
|
||||
Material.value.bumpScale = MathHelper.clamp(val, 0, 1);
|
||||
UpdateMaterial();
|
||||
}
|
||||
});
|
||||
|
||||
const emissiveIntensity = computed({
|
||||
get: () => Material.value.specular,
|
||||
set: (val) => {
|
||||
Material.value.specular = MathHelper.clamp(val, 0, 1);
|
||||
UpdateMaterial();
|
||||
}
|
||||
});
|
||||
|
||||
const uploading = ref(false);
|
||||
// const model = reactive({
|
||||
// metallic: Material.value.matalness,
|
||||
@ -162,33 +200,33 @@ const uploading = ref(false);
|
||||
// });
|
||||
const materialInfo = reactive({
|
||||
dirId: DirectoryId.MaterialDir, // 正常来说是2
|
||||
materialName: props.name || '材质',
|
||||
materialName: props.name || Textures.value[0]?.name || '材质',
|
||||
inputText: '',
|
||||
});
|
||||
|
||||
watch(() => props.textureSrcList, async (val) => {
|
||||
_textureSrc.value = val;
|
||||
await scene.ChangeTextureFromUrlAsync(_textureSrc.value[0]);
|
||||
watch(Textures, async (val) => {
|
||||
if (val.length == 0 || props.ignoreTexture) return;
|
||||
await scene.ChangeTextureFromUrlAsync(val[0].src);
|
||||
});
|
||||
|
||||
watch(() => props.name, () => {
|
||||
materialInfo.materialName = props.name || '材质';
|
||||
materialInfo.materialName = props.name || Textures.value[0]?.name || '材质';
|
||||
});
|
||||
|
||||
watch(textureAdjustment, async (val) => {
|
||||
UpdateTexture();
|
||||
scene.UpdateTexture();
|
||||
}, { deep: true });
|
||||
|
||||
// 监听纹理更新
|
||||
watch(() => scene.CurrTexture, (val) => {
|
||||
textureAdjustment.value = {
|
||||
wrapS: val.wrapS,
|
||||
wrapT: val.wrapT,
|
||||
wrapS: val.WrapS,
|
||||
wrapT: val.WrapT,
|
||||
rotation: val.rotation,
|
||||
repeatX: val.repeat.x,
|
||||
repeatY: val.repeat.y,
|
||||
moveX: val.offset.x,
|
||||
moveY: val.offset.y
|
||||
repeatX: val.repeatX,
|
||||
repeatY: val.repeatY,
|
||||
moveX: val.moveX,
|
||||
moveY: val.moveY
|
||||
}
|
||||
})
|
||||
|
||||
@ -207,24 +245,9 @@ async function UpdateMaterial() {
|
||||
await scene.UpdateMaterialAsync();
|
||||
}
|
||||
|
||||
function UpdateTexture() {
|
||||
const texture = scene.CurrTexture;
|
||||
const val = textureAdjustment.value;
|
||||
texture.wrapS = val.wrapS;
|
||||
texture.wrapT = val.wrapT;
|
||||
texture.anisotropy = 16;
|
||||
texture.rotation = val.rotation;
|
||||
texture.repeat.set(val.repeatX, val.repeatY);
|
||||
texture.offset.set(val.moveX, val.moveY);
|
||||
texture.needsUpdate = true;
|
||||
|
||||
scene.Update();
|
||||
}
|
||||
|
||||
async function loadData() {
|
||||
if (!materialInfo.inputText) return;
|
||||
const json = JSON.parse(materialInfo.inputText);
|
||||
const cadFile = FromDeflateBase64(json.file);
|
||||
const cadFile = FromDeflateBase64(materialInfo.inputText);
|
||||
scene.ImportMaterialAsync(cadFile)
|
||||
}
|
||||
|
||||
@ -239,7 +262,9 @@ async function HandleUpload() {
|
||||
uploading.value = true;
|
||||
const result = [];
|
||||
if (props.ignoreTexture) {
|
||||
// 直接将材质序列化
|
||||
// 注意更改材质内部名称
|
||||
Material.value.Name = materialInfo.materialName;
|
||||
// 将材质序列化
|
||||
const mat = {
|
||||
name: materialInfo.materialName,
|
||||
logo: await scene.GenerateMaterialLogoAsync(),
|
||||
@ -250,20 +275,23 @@ async function HandleUpload() {
|
||||
}
|
||||
else {
|
||||
// 遍历纹理链接列表,更改纹理后将材质序列化,然后还原场景
|
||||
let idx = 0;
|
||||
for (const src of _textureSrc.value) {
|
||||
await scene.ChangeTextureFromUrlAsync(src);
|
||||
for (const texture of Textures.value) {
|
||||
// 注意更改材质内部名称
|
||||
Material.value.Name = texture.name;
|
||||
// 更新纹理
|
||||
await scene.ChangeTextureFromUrlAsync(texture.src);
|
||||
const mat = {
|
||||
name: materialInfo.materialName + ++idx,
|
||||
name: texture.name,
|
||||
logo: await scene.GenerateMaterialLogoAsync(),
|
||||
// jsonString -> Deflate -> BinaryString -> Base64
|
||||
file: ToDeflatedBase64(await scene.SerializeMaterialAsync())
|
||||
};
|
||||
result.push(mat);
|
||||
}
|
||||
// 还原场景纹理
|
||||
await scene.ChangeTextureFromUrlAsync(Textures.value[0].src);
|
||||
}
|
||||
|
||||
await scene.ChangeTextureFromUrlAsync(_textureSrc.value[0]);
|
||||
emits('submit', result);
|
||||
return result;
|
||||
} finally {
|
||||
@ -280,6 +308,16 @@ async function HandleGenerateLogo() {
|
||||
DownloadFile("logo.png", blob);
|
||||
}
|
||||
|
||||
function ClampNumericValue(e: InputEvent) {
|
||||
const elm = e.target as HTMLInputElement;
|
||||
const min = +elm.min;
|
||||
const max = +elm.max;
|
||||
|
||||
if (isNaN(min) || isNaN(max)) return;
|
||||
|
||||
elm.valueAsNumber = MathHelper.clamp(elm.valueAsNumber, min, max);
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
Upload: HandleUpload,
|
||||
Cancel: HandleCancel
|
||||
@ -322,26 +360,6 @@ select
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
input[type="range"]
|
||||
{
|
||||
width: 220px;
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
input[type="text"]
|
||||
{
|
||||
width: 220px;
|
||||
padding: 4px;
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
input[type="number"]
|
||||
{
|
||||
width: 220px;
|
||||
padding: 4px;
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
.btn-success
|
||||
{
|
||||
background-color: #4caf50;
|
||||
@ -386,4 +404,40 @@ input[type="number"]
|
||||
{
|
||||
background-color: #0a7ed2;
|
||||
}
|
||||
|
||||
.input-range
|
||||
{
|
||||
max-width: 300px;
|
||||
|
||||
input[type="range"]
|
||||
{
|
||||
flex: 14;
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
input[type="text"]
|
||||
{
|
||||
flex: 2;
|
||||
padding: 4px;
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
input[type="number"]
|
||||
{
|
||||
flex: 2;
|
||||
min-width: 0;
|
||||
padding: 4px;
|
||||
margin: 0.5rem 0;
|
||||
appearance: textfield;
|
||||
-moz-appearance: textfield;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
input::-webkit-outer-spin-button,
|
||||
input::-webkit-inner-spin-button
|
||||
{
|
||||
-webkit-appearance: none;
|
||||
margin: 0;
|
||||
}
|
||||
}
|
||||
</style>
|
@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<CfFlex class="material-view">
|
||||
<div ref="container" class="material-view-container" />
|
||||
<MaterialAdjuster ref="adjuster" class="material-view-sider" :name="matName" :textureSrcList="textureSrc" :ignore-texture="editMode"
|
||||
<MaterialAdjuster ref="adjuster" class="material-view-sider" :name="matName" :textures="textures" :ignore-texture="editMode"
|
||||
@cancel="config.cancelCallback" @submit="config.submitCallback" />
|
||||
</CfFlex>
|
||||
</template>
|
||||
@ -19,7 +19,7 @@ const eventbus = useEvent();
|
||||
const container = useTemplateRef('container');
|
||||
const adjusterRef = useTemplateRef('adjuster');
|
||||
const config = GetConfig();
|
||||
const textureSrc = ref<string[]>(Array.from(config.textureSrc));
|
||||
const textures = ref<{name: string, src: string}[]>(Array.from(config.textures));
|
||||
const matName = ref<string>();
|
||||
const editMode = ref(false);
|
||||
|
||||
@ -45,7 +45,7 @@ function HandleUpload() {
|
||||
}
|
||||
|
||||
function HandleChangeTexture() {
|
||||
textureSrc.value = Array.from(config.textureSrc);
|
||||
textures.value = Array.from(config.textures);
|
||||
}
|
||||
|
||||
async function HandleUpdateConfig() {
|
||||
@ -56,9 +56,9 @@ async function HandleUpdateConfig() {
|
||||
editMode.value = true;
|
||||
}
|
||||
else { editMode.value = false; }
|
||||
if (config.textureSrc) {
|
||||
textureSrc.value = Array.from(config.textureSrc);
|
||||
await scene.ChangeTextureFromUrlAsync(textureSrc.value[0]);
|
||||
if (config.textures && config.textures.length > 0) {
|
||||
textures.value = Array.from(config.textures);
|
||||
await scene.ChangeTextureFromUrlAsync(textures.value[0].src); // 这一行是保证首次Mount组件时纹理能够立刻刷新
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@ import type { MaterialRequest } from "../components/MaterialAdjuster.vue"
|
||||
|
||||
let _libOutputConfig = {
|
||||
host:'https://tapi.cfcad.cn:7779',
|
||||
textureSrc: [],
|
||||
textures: [],
|
||||
file: undefined,
|
||||
submitCallback: undefined,
|
||||
cancelCallback: undefined,
|
||||
@ -24,7 +24,12 @@ export type LibOutputConfig = {
|
||||
*/
|
||||
host: string,
|
||||
/** 材质贴图链接列表,用与批量提交,场景会只会载入第一个链接作为纹理预览,但是导出提交时会为所有链接创建材质 */
|
||||
textureSrc: Array<string>,
|
||||
textures: Array<{
|
||||
/** 材质名 */
|
||||
name: string;
|
||||
/** 纹理链接 */
|
||||
src: string;
|
||||
}>,
|
||||
/** 更新模型,对材质进行编辑时赋值 */
|
||||
updateModel?: {
|
||||
/** 材质名 */
|
||||
|
@ -3,22 +3,23 @@ import { computed, ref } from "vue";
|
||||
import { MaterialEditor } from "../common/MaterialEditor";
|
||||
import { Database, PhysicalMaterialRecord, TextureTableRecord } from "webcad_ue4_api";
|
||||
import { LoadImageFromUrl } from "../helpers/helper.imageLoader";
|
||||
import { Texture } from "three";
|
||||
import { ClampToEdgeWrapping, MirroredRepeatWrapping, RepeatWrapping, Texture } from "three";
|
||||
import { materialRenderer } from "../common/MaterialRenderer";
|
||||
import { MaterialIn, MaterialOut } from "../common/MaterialSerializer";
|
||||
import { GetConfig } from "../lib/libOutputConfig";
|
||||
import { AsyncDelay } from "../helpers/helper.async";
|
||||
|
||||
const sceneSetup = () => {
|
||||
let _editor: MaterialEditor | undefined;
|
||||
let _database: Database | undefined;
|
||||
const _material = ref<PhysicalMaterialRecord>(new PhysicalMaterialRecord());
|
||||
const _currGeometry = ref<string>('球');
|
||||
const _currTexture = ref<Texture>();
|
||||
const _currTexture = ref<TextureTableRecord>();
|
||||
const CurrGeometry = computed({
|
||||
get: () => _currGeometry.value,
|
||||
set: (val: string) => ChangeGeometry(val)
|
||||
})
|
||||
const CurrTexture = computed<Texture>(() => _currTexture.value);
|
||||
const CurrTexture = computed<TextureTableRecord>(() => _currTexture.value);
|
||||
const Geometries = ref<string[]>([]);
|
||||
const Material = computed(() => _material.value);
|
||||
const CurrentShowObject = computed(() => _editor.ShowObject);
|
||||
@ -82,36 +83,57 @@ const sceneSetup = () => {
|
||||
Update();
|
||||
}
|
||||
|
||||
|
||||
/** 这是自定义材质更新方法,WebCAD库中的TextureTableRecord.TextureUpdate方法在导出的时候被删除了,所以需要自己实现 */
|
||||
function UpdateTexture() {
|
||||
const record = _currTexture.value;
|
||||
const texture = record['texture'] as Texture;
|
||||
texture.wrapS = record.WrapS;
|
||||
texture.wrapT = record.WrapT;
|
||||
texture.anisotropy = 16;
|
||||
texture.rotation = record.rotation;
|
||||
texture.repeat.set(record.repeatX, record.repeatY);
|
||||
texture.offset.set(record.moveX, record.moveY);
|
||||
texture.needsUpdate = true;
|
||||
Update();
|
||||
}
|
||||
|
||||
async function ChangeTextureFromUrlAsync(url?: string) {
|
||||
// 关联贴图
|
||||
const db = Material.value.Db;
|
||||
if (!db) return; // 材质未初始化
|
||||
// 材质未初始化
|
||||
if (!db) {
|
||||
console.warn("Material has not been initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果url为空,则保留原有材质中的纹理(修改模式),否则重新实例化纹理对象
|
||||
let record = Material.value.map?.Object as TextureTableRecord;
|
||||
if (!record) {
|
||||
// record = db.TextureTable.Symbols.values().next().value;
|
||||
// if(!record){
|
||||
if (url) {
|
||||
record = new TextureTableRecord();
|
||||
record.Name = db.TextureTable.AllocateName();
|
||||
db.TextureTable.Add(record);
|
||||
// 替换map
|
||||
Material.value.map = record.Id;
|
||||
// Material.value.map = img ? record.Id : undefined;
|
||||
// }
|
||||
}
|
||||
|
||||
// record.objectId = new ObjectId(undefined, record);
|
||||
_currTexture.value = record['texture'] as Texture;
|
||||
if (url) {
|
||||
record.imageUrl = url;
|
||||
_currTexture.value.image = undefined;
|
||||
}
|
||||
|
||||
// 设置Store
|
||||
if (!_currTexture.value.image) {
|
||||
const img = await LoadImageFromUrl(GetConfig().host + '/' + record.imageUrl);
|
||||
_currTexture.value.image = img;
|
||||
_currTexture.value.needsUpdate = true;
|
||||
_currTexture.value = record;
|
||||
const texture = record['texture'] as Texture;
|
||||
if (url) {
|
||||
record.imageUrl = url;
|
||||
texture.image = undefined;
|
||||
}
|
||||
|
||||
if (!texture.image) {
|
||||
console.warn('Load Image: ', GetConfig().host + '/' + record.imageUrl);
|
||||
const img = await LoadImageFromUrl(GetConfig().host + '/' + record.imageUrl);
|
||||
texture.image = img;
|
||||
}
|
||||
|
||||
UpdateTexture();
|
||||
await record.Update();
|
||||
await AsyncDelay(10);
|
||||
await UpdateMaterialAsync();
|
||||
}
|
||||
|
||||
@ -153,6 +175,7 @@ const sceneSetup = () => {
|
||||
Material,
|
||||
Initial,
|
||||
Update,
|
||||
UpdateTexture,
|
||||
UpdateMaterialAsync,
|
||||
ChangeTextureFromUrlAsync,
|
||||
SerializeMaterialAsync,
|
||||
|
Loading…
Reference in New Issue
Block a user