Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load vrm files drag drop #56

Merged
merged 7 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/components/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import MenuTitle from "./MenuTitle"
import { setTextureToChildMeshes } from "../library/utils"


export default function Editor({uploadTextureURL,confirmDialog,animationManager, blinkManager, lookatManager, effectManager, jsonSelectionArray}) {
export default function Editor({uploadTextureURL, uploadVRMURL,confirmDialog,animationManager, blinkManager, lookatManager, effectManager, jsonSelectionArray}) {
const {
currentTraitName,
setCurrentTraitName,
Expand Down Expand Up @@ -123,7 +123,14 @@ export default function Editor({uploadTextureURL,confirmDialog,animationManager,
</div>
</div>
</div>
<Selector confirmDialog = {confirmDialog} animationManager={animationManager} templateInfo={templateInfo} blinkManager = {blinkManager} lookatManager = {lookatManager} effectManager = {effectManager}/>
<Selector
confirmDialog = {confirmDialog}
animationManager={animationManager}
templateInfo={templateInfo}
blinkManager = {blinkManager}
lookatManager = {lookatManager}
effectManager = {effectManager}
uploadVRMURL = {uploadVRMURL}/>
<JsonAttributes jsonSelectionArray={jsonSelectionArray}/>
<TraitInformation currentVRM={currentVRM} animationManager={animationManager} lookatManager={lookatManager}/>
</Fragment>
Expand Down
62 changes: 27 additions & 35 deletions src/components/Selector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
THREE.Mesh.prototype.raycast = acceleratedRaycast;

export default function Selector({confirmDialog, templateInfo, animationManager, blinkManager, lookatManager, effectManager}) {
export default function Selector({confirmDialog, uploadVRMURL, templateInfo, animationManager, blinkManager, lookatManager, effectManager}) {
const {
avatar,
setAvatar,
Expand Down Expand Up @@ -60,7 +60,6 @@ export default function Selector({confirmDialog, templateInfo, animationManager,
const [, setLoadPercentage] = useState(1)
const [restrictions, setRestrictions] = useState(null)
const [currentTrait, setCurrentTrait] = useState(new Map());
const [vrm1Warn, setVrm1Warn1] = useState(true);

const updateCurrentTraitMap = (k,v) => {
setCurrentTrait(currentTrait.set(k,v));
Expand All @@ -71,9 +70,17 @@ export default function Selector({confirmDialog, templateInfo, animationManager,

useEffect(() => {
setRestrictions(getRestrictions());

},[templateInfo])

useEffect(()=>{
if (uploadVRMURL != null){
if (uploadVRMURL != ""){
console.log(uploadVRMURL);
loadCustom(uploadVRMURL);
}
}
},[uploadVRMURL])

const getRestrictions = () => {

const traitRestrictions = templateInfo.traitRestrictions
Expand Down Expand Up @@ -167,21 +174,19 @@ export default function Selector({confirmDialog, templateInfo, animationManager,

},[selectedOptions])

const loadCustom = (file) => {
const url = URL.createObjectURL(file);
const option = {

item:{
id:"custom_" + currentTraitName,
name:"Custom " + currentTraitName,
directory:url
},
trait:templateInfo.traits.find((t) => t.name === currentTraitName)
}
effectManager.setTransitionEffect('switch_item');
loadOptions([option], false, false, false).then((loadedData)=>{
URL.revokeObjectURL(url);
if (loadedData[0].models[0]?.userData?.gltfExtensions?.VRMC_vrm){
const loadCustom = (url) => {
if (currentTraitName){
const option = {
item:{
id:"custom_" + currentTraitName,
name:"Custom " + currentTraitName,
directory:url
},
trait:templateInfo.traits.find((t) => t.name === currentTraitName)
}
effectManager.setTransitionEffect('switch_item');
loadOptions([option], false, false, false).then((loadedData)=>{
URL.revokeObjectURL(url);
let newAvatar = {};
loadedData.map((data)=>{
newAvatar = {...newAvatar, ...itemAssign(data)}
Expand All @@ -193,24 +198,10 @@ export default function Selector({confirmDialog, templateInfo, animationManager,
}
}, effectManager.transitionTime);
setAvatar(finalAvatar)
}
else{

console.log("Only vrm1 file supported")
}
})
}

const promptUpload = async () => {
if (vrm1Warn){
confirmDialog("Supports only VRM1 files", (val)=>{
setVrm1Warn1(!val);
if (val)
uploadTrait()
})
}
else{
uploadTrait();
console.log("Please select a trait first");
}
}

Expand All @@ -222,7 +213,8 @@ export default function Selector({confirmDialog, templateInfo, animationManager,
input.onchange = e => {
var file = e.target.files[0];
if (file.name.endsWith(".vrm")){
loadCustom(file)
const url = URL.createObjectURL(file);
loadCustom(url)
}
}
input.click();
Expand Down Expand Up @@ -827,7 +819,7 @@ export default function Selector({confirmDialog, templateInfo, animationManager,

<div
className={styles["uploadButton"]}
onClick={() => {promptUpload()}}>
onClick={uploadTrait}>
<div>
Upload </div>
</div>
Expand Down
9 changes: 9 additions & 0 deletions src/pages/Appearance.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function Appearance({

const [jsonSelectionArray, setJsonSelectionArray] = React.useState(null)
const [uploadTextureURL, setUploadTextureURL] = React.useState(null)
const [uploadVRMURL, setUploadVRMURL] = React.useState(null)

const next = () => {
!isMute && playSound('backNextButton');
Expand Down Expand Up @@ -97,6 +98,10 @@ function Appearance({
const path = URL.createObjectURL(file);
setUploadTextureURL(path);
}
const handleVRMDrop = (file) =>{
const path = URL.createObjectURL(file);
setUploadVRMURL(path);
}

const handleFilesDrop = async(files) => {
const file = files[0];
Expand All @@ -107,6 +112,9 @@ function Appearance({
if (file && (file.name.toLowerCase().endsWith('.png') || file.name.toLowerCase().endsWith('.jpg'))) {
handleImageDrop(file);
}
if (file && file.name.toLowerCase().endsWith('.vrm')) {
handleVRMDrop(file);
}

const filesArray = Array.from(files);
const jsonDataArray = [];
Expand Down Expand Up @@ -188,6 +196,7 @@ function Appearance({
confirmDialog={confirmDialog}
jsonSelectionArray={jsonSelectionArray}
uploadTextureURL = {uploadTextureURL}
uploadVRMURL = {uploadVRMURL}
/>
<div className={styles.buttonContainer}>
<CustomButton
Expand Down
Loading