Skip to content
Permalink
Browse files

feat(export): export mateial snapshot

  • Loading branch information...
AmyOrz committed Apr 16, 2019
1 parent af91c1e commit 10fda6d9fc5468c284df87d485b104dd2ba258d0
@@ -47,12 +47,14 @@ type bufferView = {
type basicMaterial = {
name: string,
path: string,
snapshot: imageIndex,
color: array(float),
};

type lightMaterial = {
name: string,
path: string,
snapshot: imageIndex,
diffuseColor: array(float),
diffuseMap: option(textureIndex),
shininess: float,
@@ -176,7 +176,8 @@ let _getTextureIndexFromMap = (textureComponent, textureIndexMap) =>
->Some
};

let _buildMaterialData = (textureIndexMap, (editorState, engineState)) =>
let _buildMaterialData =
(imageIndexMap, textureIndexMap, (editorState, engineState)) =>
MaterialNodeAssetEditorService.findAllMaterialNodes(editorState)
|> WonderCommonlib.ArrayService.reduceOneParam(
(. (basicMaterialArr, lightMaterialArr), node) => {
@@ -187,16 +188,23 @@ let _buildMaterialData = (textureIndexMap, (editorState, engineState)) =>
(editorState, engineState),
);

let {materialComponent, type_}: NodeAssetType.materialNodeData =
let {materialComponent, type_, imageDataIndex}: NodeAssetType.materialNodeData =
MaterialNodeAssetService.getNodeData(node);

let snapshot =
imageIndexMap
|> WonderCommonlib.ImmutableSparseMapService.unsafeGet(
imageDataIndex,
);

switch (type_) {
| BasicMaterial => (
basicMaterialArr
|> ArrayService.push(
{
name,
path,
snapshot,
color:
BasicMaterialEngineService.getColor(
materialComponent,
@@ -213,6 +221,7 @@ let _buildMaterialData = (textureIndexMap, (editorState, engineState)) =>
{
name,
path,
snapshot,
diffuseColor:
LightMaterialEngineService.getLightMaterialDiffuseColor(
materialComponent,
@@ -392,7 +401,11 @@ let buildJsonData = (imageUint8ArrayMap, (editorState, engineState)) => {
let (textureIndexMap, textureArr) =
_buildTextureData(imageIndexMap, (editorState, engineState));
let (basicMaterialArr, lightMaterialArr) =
_buildMaterialData(textureIndexMap, (editorState, engineState));
_buildMaterialData(
imageIndexMap,
textureIndexMap,
(editorState, engineState),
);
let (
engineState,
wdbArr,
@@ -1,6 +1,9 @@
open Js.Typed_array;

let _buildEmptyUint8Array = () => Uint8Array.make([||]);
let _buildDefaultMaterialSnapshotUint8Array = () =>
BufferUtils.convertBase64ToUint8Array(
OperateMaterialLogicService.getDefaultSnapshotBase64(),
);

let _buildImageDataUint8Array = editorState =>
ImageDataMapAssetEditorService.getMap(editorState)
@@ -17,18 +20,7 @@ let _buildImageDataUint8Array = editorState =>
switch (base64) {
| Some(base64) =>
BufferUtils.convertBase64ToUint8Array(base64)
| None =>
ConsoleUtils.error(
LogUtils.buildErrorMessage(
~description={j|image->base64 should exist|j},
~reason="",
~solution={j||j},
~params={j||j},
),
editorState,
);

_buildEmptyUint8Array();
| None => _buildDefaultMaterialSnapshotUint8Array()
}
}
)
@@ -2,12 +2,9 @@ open Js.Typed_array;

open Js.Promise;

let _buildLoadImageStream = (arrayBuffer, mimeType, errorMsg) => {
let blob = Blob.newBlobFromArrayBuffer(arrayBuffer, mimeType);

LoadImageUtils.loadBlobImage(blob |> Blob.createObjectURL, errorMsg)
let _buildLoadImageStream = (blob, blobObjectURL, mimeType, errorMsg) =>
LoadImageUtils.loadBlobImage(blobObjectURL, errorMsg)
|> WonderBsMost.Most.tap(image => Blob.revokeObjectURL(blob));
};

let _getArrayBuffer =
(buffer, bufferView, bufferViews: array(ExportAssetType.bufferView)) => {
@@ -31,11 +28,14 @@ let buildImageData =
imageIndex,
) => {
let arrayBuffer = _getArrayBuffer(buffer, bufferView, bufferViews);
let blob = Blob.newBlobFromArrayBuffer(arrayBuffer, mimeType);
let blobObjectURL = blob |> Blob.createObjectURL;

streamArr
|> ArrayService.push(
_buildLoadImageStream(
arrayBuffer,
blob,
blobObjectURL,
mimeType,
{j|load image error. imageIndex: $imageIndex|j},
)
@@ -45,13 +45,7 @@ let buildImageData =
image;
})
|> WonderBsMost.Most.map(image =>
(
image,
Uint8Array.fromBuffer(arrayBuffer),
imageIndex,
name,
mimeType,
)
(image, blobObjectURL, imageIndex, name, mimeType)
),
);
},
@@ -61,7 +55,7 @@ let buildImageData =
|> WonderBsMost.Most.reduce(
(
(imageMap, imageDataIndexMap, editorState),
(image, uint8Array, imageIndex, name, mimeType),
(image, blobObjectURL, imageIndex, name, mimeType),
) => {
let (editorState, imageDataIndex) =
IndexAssetEditorService.generateImageDataMapIndex(editorState);
@@ -79,7 +73,8 @@ let buildImageData =
imageDataIndex,
ImageDataMapAssetService.buildData(
~base64=None,
~uint8Array=Some(uint8Array),
~uint8Array=None,
~blobObjectURL=Some(blobObjectURL),
~name,
~mimeType,
(),
@@ -229,7 +224,11 @@ let buildTextureData =
);

let _buildMaterialEditorData =
(material, path, type_, (editorState, engineState)) => {
(
(material, path, type_),
(snapshot, imageDataIndexMap),
(editorState, engineState),
) => {
let (editorState, assetNodeId) =
IdAssetEditorService.generateNodeId(editorState);

@@ -239,38 +238,27 @@ let _buildMaterialEditorData =
(editorState, engineState),
);

let (editorState, newImageDataIndex) =
IndexAssetEditorService.generateImageDataMapIndex(editorState);

/*TODO import ASB, create imageDataIndex to store asb's material uint8array */
editorState
|> MaterialNodeAssetEditorService.addMaterialNodeToAssetTree(
parentFolderNode,
MaterialNodeAssetService.buildNode(
~nodeId=assetNodeId,
~type_,
~materialComponent=material,
~imageDataIndex=newImageDataIndex,
),
)
|> ImageDataMapAssetEditorService.setData(
newImageDataIndex,
ImageDataMapAssetService.buildData(
~base64=None,
~uint8Array=None,
~name="material",
~mimeType=ImageUtils.getDefaultMimeType(),
(),
~imageDataIndex=
imageDataIndexMap
|> WonderCommonlib.ImmutableSparseMapService.unsafeGet(snapshot),
),
);
};

let _buildBasicMaterialData = (basicMaterials, (editorState, engineState)) =>
let _buildBasicMaterialData =
(basicMaterials, imageDataIndexMap, (editorState, engineState)) =>
basicMaterials
|> WonderCommonlib.ArrayService.reduceOneParami(
(.
(basicMaterialMap, (editorState, engineState)),
{name, path, color}: ExportAssetType.basicMaterial,
{name, path, snapshot, color}: ExportAssetType.basicMaterial,
materialIndex,
) => {
let (engineState, material) =
@@ -283,9 +271,8 @@ let _buildBasicMaterialData = (basicMaterials, (editorState, engineState)) =>

let editorState =
_buildMaterialEditorData(
material,
path,
MaterialDataAssetType.BasicMaterial,
(material, path, MaterialDataAssetType.BasicMaterial),
(snapshot, imageDataIndexMap),
(editorState, engineState),
);

@@ -305,12 +292,16 @@ let _buildBasicMaterialData = (basicMaterials, (editorState, engineState)) =>
);

let _buildLightMaterialData =
(lightMaterials, textureMap, (editorState, engineState)) =>
(
lightMaterials,
(imageDataIndexMap, textureMap),
(editorState, engineState),
) =>
lightMaterials
|> WonderCommonlib.ArrayService.reduceOneParami(
(.
(lightMaterialMap, (editorState, engineState)),
{name, diffuseColor, diffuseMap, shininess, path}: ExportAssetType.lightMaterial,
{name, diffuseColor, diffuseMap, shininess, snapshot, path}: ExportAssetType.lightMaterial,
materialIndex,
) => {
let (engineState, material) =
@@ -347,9 +338,8 @@ let _buildLightMaterialData =

let editorState =
_buildMaterialEditorData(
material,
path,
MaterialDataAssetType.LightMaterial,
(material, path, MaterialDataAssetType.LightMaterial),
(snapshot, imageDataIndexMap),
(editorState, engineState),
);

@@ -371,15 +361,19 @@ let _buildLightMaterialData =
let buildMaterialData =
(
{basicMaterials, lightMaterials}: ExportAssetType.assets,
textureMap,
(imageDataIndexMap, textureMap),
(editorState, engineState),
) => {
let (basicMaterialMap, (editorState, engineState)) =
_buildBasicMaterialData(basicMaterials, (editorState, engineState));
_buildBasicMaterialData(
basicMaterials,
imageDataIndexMap,
(editorState, engineState),
);
let (lightMaterialMap, (editorState, engineState)) =
_buildLightMaterialData(
lightMaterials,
textureMap,
(imageDataIndexMap, textureMap),
(editorState, engineState),
);

@@ -59,7 +59,7 @@ let importASB = asb => {

BuildAssetDataUtils.buildMaterialData(
asbRecord,
textureMap,
(imageDataIndexMap, textureMap),
(editorState, engineState),
);
})
@@ -6,7 +6,7 @@ return new Blob([arrayBuffer], {type: type_})
|}
];

let createObjectURL = [%raw
let createObjectURL: blob => string = [%raw
blob => {|
return URL.createObject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x4UdKkIB/DvJLqvjtVsrSTPY/h4ja3QULQEqHN0qkZ2fOrDWIXm94y8/wByXZqLqwkr5QtLiEtqIH5Ka6/EVZb0j1iZciwiJYO6U7Ar3oJw2Hm5DSHmVBbbg5kqB3Bry1w2wbW1Mdptp19DzJ23Q4rcD4Vm+Pq7YnkJJQUqPXo4Cmgy1UTHbY4LaM+7PHWxucyl+7YGm2aoY4oAlyJKs9yjd6tIAJ9KJJntH9Lyt9h1rn3c9XLe02ssqbbATuDzekP3qGjtbuIeLb+ELWOz/wAQSmXmNjawqCz3g5n3LlMjsqSkHff1QfUfyQfLzoKXhTsSNx0O3tpWhO5JHkTuKUH1sfyW7Ybe7fk9lU4qbbVlTkZo7LnsEjvGR+o7BSf1JA9pqZbh14trZerVb3WrohW6U86S5yrSfIpUD1BBBBB6ggg1ClXox03ix3FV7xK7OWO5uK55TJSXbTc1e0vtDYpWf7rfpH2hVBbywziJiutMFM9J5kgj1v8Aus5wuISOlA/n09B5B0VUTxXir1Oxptpi8Y9PmdyQgSrJLbuLLg/HlJS4P3TWWUccGXONLRFx3IQ7tygzEtwGevt5lr/wDQWZ8t4n4NthvvO3NpKUJPVT4A6D41XX48OKuVr1lUHE7LOU/huJTFy3Xm3eZi8XEpU1zj8UMIU4lJ9qnVkeQrjJm+v+pOesOQ7jdF2y3Ogpcg299ZW8k+aXHuhIPkQkAfmawvQKUpQbw8JE92j/ACU/SnhInu0f5KfpSlA8JE92j/JT9KeEie7R/kp+lKUDwkT3aP8AJT9KeEie7R/kp+lKUDwkT3aP8lP0pSlB/9k=
";

let getNewMaterilaName = () => "New Material";

let getDefaultName = () => "NoName Material";

0 comments on commit 10fda6d

Please sign in to comment.
You can’t perform that action at this time.