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

Drag drop texture trait #49

Merged
merged 7 commits into from
Oct 29, 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
14 changes: 11 additions & 3 deletions src/components/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import JsonAttributes from "./JsonAttributes"
import { TokenBox } from "./token-box/TokenBox"
import { LanguageContext } from "../context/LanguageContext"
import MenuTitle from "./MenuTitle"
import { setTextureToChildMeshes } from "../library/utils"


export default function Editor({confirmDialog,animationManager, blinkManager, lookatManager, effectManager, jsonSelectionArray}) {
export default function Editor({uploadTextureURL,confirmDialog,animationManager, blinkManager, lookatManager, effectManager, jsonSelectionArray}) {
const {
currentTraitName,
setCurrentTraitName,
Expand All @@ -34,7 +35,9 @@ export default function Editor({confirmDialog,animationManager, blinkManager, lo
manifestSelectionIndex,
moveCamera,
avatar,
setDisplayTraitOption
setDisplayTraitOption,
currentVRM,
setCurrentVRM
} = useContext(SceneContext);

const { isMute } = useContext(AudioContext)
Expand All @@ -47,7 +50,6 @@ export default function Editor({confirmDialog,animationManager, blinkManager, lo
} = useContext(SoundContext)

const [cameraFocused, setCameraFocused] = React.useState(false)
const [currentVRM, setCurrentVRM] = React.useState(null)

// options are selected by random or start
useEffect(() => {
Expand All @@ -61,6 +63,12 @@ export default function Editor({confirmDialog,animationManager, blinkManager, lo
setCurrentTraitName(null)
}, [templateInfo])

useEffect(()=>{
if (uploadTextureURL != null && currentVRM != null){
setTextureToChildMeshes(currentVRM.scene,uploadTextureURL)
}
},[uploadTextureURL])


const selectOption = (option) => {
!isMute && playSound('optionClick');
Expand Down
5 changes: 3 additions & 2 deletions src/components/TraitInformation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { SceneContext } from "../context/SceneContext";
import Slider from "./Slider";
import { cullHiddenMeshes } from "../library/utils";

export default function TraitInformation({currentVRM, animationManager, lookatManager}){
export default function TraitInformation({animationManager, lookatManager}){
const {
displayTraitOption,
avatar
avatar,
currentVRM
} = useContext(SceneContext);

const [cullOutDistance, setCullOutDistance] = useState(0); // set from the values of the trait
Expand Down
4 changes: 4 additions & 0 deletions src/context/SceneContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const SceneProvider = (props) => {
const [currentOptions, setCurrentOptions] = useState([])
const [displayTraitOption, setDisplayTraitOption] = useState(null)

const [currentVRM, setCurrentVRM] = React.useState(null)

const [model, setModel] = useState(new THREE.Object3D())
const [animationManager, setAnimationManager] = useState(null)
const [camera, setCamera] = useState(null)
Expand Down Expand Up @@ -205,6 +207,8 @@ export const SceneProvider = (props) => {
<SceneContext.Provider
value={{
vrmHelperRoot,
currentVRM,
setCurrentVRM,

awaitDisplay,
setAwaitDisplay,
Expand Down
36 changes: 36 additions & 0 deletions src/library/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,42 @@ export async function prepareModel(templateInfo){
return loadModel(trait)
}));
}
export async function setTextureToChildMeshes(scene, textureFile){
console.log(scene);
console.log(textureFile);

const textureLoader = new THREE.TextureLoader();

// Load the image as a texture
const texture = await textureLoader.load(textureFile);
texture.flipY = false;

// Traverse through the child meshes in the scene
scene.traverse((object) => {
if (object instanceof THREE.Mesh) {
const materials = !Array.isArray(object.material) ? [object.material] : object.material
// Assign the texture to the material
for (let i = 0; i < materials.length; i++) {
if (materials[i] instanceof THREE.ShaderMaterial) {
if(!materials[i].name.includes("(Outline)")){
console.log(materials[i].name);
materials[i].uniforms.map.value = texture
materials[i].uniforms.shadeMultiplyTexture.value = texture;
}
}
else{
materials[i].map = texture
}
console.log(materials[i]);
materials[i].needsUpdate = true

}
}
});

// get all mesh children from scene and apply texture to standard material

}

export function getFileNameWithoutExtension(filePath) {
// Get the base file name without the extension
Expand Down
32 changes: 23 additions & 9 deletions src/pages/Appearance.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function Appearance({
setIsChangingWholeAvatar,
toggleDebugMNode,
templateInfo,
setSelectedOptions
setSelectedOptions,
setCurrentVRM,
setDisplayTraitOption,
} = React.useContext(SceneContext)


Expand All @@ -36,14 +38,16 @@ function Appearance({
!isMute && playSound('backNextButton');
resetAvatar()
setViewMode(ViewMode.CREATE)
setDisplayTraitOption(null);
}

const [jsonSelectionArray, setJsonSelectionArray] = React.useState(null)

const [uploadTextureURL, setUploadTextureURL] = React.useState(null)

const next = () => {
!isMute && playSound('backNextButton');
setViewMode(ViewMode.BIO)
setViewMode(ViewMode.BIO);
setDisplayTraitOption(null);
}

const randomize = () => {
Expand Down Expand Up @@ -83,16 +87,25 @@ function Appearance({
// Translate hook
const { t } = useContext(LanguageContext)

const handleAnimationDrop = async (file) => {
const animName = getFileNameWithoutExtension(file.name);
const path = URL.createObjectURL(file);
await animationManager.loadAnimation(path, true, "", animName);
}

const handleImageDrop = (file) => {
const path = URL.createObjectURL(file);
setUploadTextureURL(path);
}

const handleFilesDrop = async(files) => {
const file = files[0];
// Check if the file has the .fbx extension
if (file && file.name.toLowerCase().endsWith('.fbx')) {
const animName = getFileNameWithoutExtension(file.name);
console.log('Dropped .fbx file:', file);
const path = URL.createObjectURL(file);
console.log("path")
await animationManager.loadAnimation(path, true, "", animName);
// Handle the dropped .fbx file
handleAnimationDrop(file);
}
if (file && (file.name.toLowerCase().endsWith('.png') || file.name.toLowerCase().endsWith('.jpg'))) {
handleImageDrop(file);
}

const filesArray = Array.from(files);
Expand Down Expand Up @@ -174,6 +187,7 @@ function Appearance({
effectManager={effectManager}
confirmDialog={confirmDialog}
jsonSelectionArray={jsonSelectionArray}
uploadTextureURL = {uploadTextureURL}
/>
<div className={styles.buttonContainer}>
<CustomButton
Expand Down
Loading