Skip to content

Commit

Permalink
reverse Y for texture
Browse files Browse the repository at this point in the history
  • Loading branch information
memelotsqui committed Oct 29, 2023
1 parent 9ba9d9d commit f96ebd1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
14 changes: 8 additions & 6 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({uploadTexture,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({uploadTexture,confirmDialog,animationManager, bl
manifestSelectionIndex,
moveCamera,
avatar,
setDisplayTraitOption
setDisplayTraitOption,
currentVRM,
setCurrentVRM
} = useContext(SceneContext);

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

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

// options are selected by random or start
useEffect(() => {
Expand All @@ -62,10 +64,10 @@ export default function Editor({uploadTexture,confirmDialog,animationManager, bl
}, [templateInfo])

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


const selectOption = (option) => {
Expand Down
35 changes: 35 additions & 0 deletions src/library/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,41 @@ 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);
console.log(texture)
texture.flipY = false;
texture.needsUpdate = true;

// 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) {
materials[i].uniforms.map = texture
materials[i].uniforms.shadeMultiplyTexture = 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
7 changes: 4 additions & 3 deletions src/pages/Appearance.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function Appearance({
}

const [jsonSelectionArray, setJsonSelectionArray] = React.useState(null)
const [uploadTexture, setUploadTexture] = React.useState(null)
const [uploadTextureURL, setUploadTextureURL] = React.useState(null)

const next = () => {
!isMute && playSound('backNextButton');
Expand Down Expand Up @@ -96,7 +96,8 @@ function Appearance({
}

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

const handleFilesDrop = async(files) => {
Expand Down Expand Up @@ -188,7 +189,7 @@ function Appearance({
effectManager={effectManager}
confirmDialog={confirmDialog}
jsonSelectionArray={jsonSelectionArray}
uploadTexture = {uploadTexture}
uploadTextureURL = {uploadTextureURL}
/>
<div className={styles.buttonContainer}>
<CustomButton
Expand Down

0 comments on commit f96ebd1

Please sign in to comment.