1
+ import React , { useState , useCallback } from "react"
2
+ import Layout from "@theme/Layout"
3
+
4
+ export default function MaterialOverrides ( ) {
5
+ const [ message , setMessage ] = useState ( "" ) ;
6
+
7
+ const handleDrop = useCallback ( async ( event ) => {
8
+ event . preventDefault ( ) ;
9
+ const file = event . dataTransfer . files [ 0 ] ;
10
+ if ( ! file ) return ;
11
+
12
+ if ( ! file . name . endsWith ( ".json" ) ) {
13
+ setMessage ( "Please drop a valid .json file." ) ;
14
+ return ;
15
+ }
16
+
17
+ const text = await file . text ( ) ;
18
+ try {
19
+ const jsonData = JSON . parse ( text ) ;
20
+ const modified = appendOverrides ( jsonData ) ;
21
+ const materialName = jsonData . Material . Instance [ 0 ] . Name . replace ( "mi" , "material" ) ;
22
+ const blob = new Blob ( [ JSON . stringify ( modified , null , 2 ) ] , { type : "application/json" } ) ;
23
+
24
+ const a = document . createElement ( "a" ) ;
25
+ a . href = URL . createObjectURL ( blob ) ;
26
+ a . download = `${ materialName } .json` ;
27
+ a . click ( ) ;
28
+
29
+ setMessage ( `Generated ${ materialName } .json` ) ;
30
+ } catch ( err ) {
31
+ setMessage ( "Failed to process file: " + err . message ) ;
32
+ console . error ( err ) ;
33
+ }
34
+ } , [ ] ) ;
35
+
36
+ const handleDragOver = ( event ) => {
37
+ event . preventDefault ( ) ;
38
+ } ;
39
+
40
+ return (
41
+ < Layout
42
+ title = "Material overrides"
43
+ description = "Automatically populate overrides section in a material.json file"
44
+ >
45
+ < main >
46
+ < div className = "container margin-vert--lg" >
47
+ < div
48
+ onDrop = { handleDrop }
49
+ onDragOver = { handleDragOver }
50
+ style = { {
51
+ border : "3px dashed var(--ifm-navbar-link-color)" ,
52
+ padding : "8rem" ,
53
+ textAlign : "center" ,
54
+ borderRadius : "1rem" ,
55
+ backgroundColor : "var(--ifm-navbar-background-color)" ,
56
+ color : "var(--ifm-navbar-link-color)"
57
+ } }
58
+ >
59
+ < h2 > Drop your material.json file here</ h2 >
60
+ < p > and overrides will be automatically added into it.</ p >
61
+ { message && < p > < strong > { message } </ strong > </ p > }
62
+ </ div >
63
+ < p style = { { textAlign : "center" , marginTop : "2rem" } } >
64
+ Adapted from code originally created by{ " " }
65
+ < a
66
+ href = "https://github.com/scrungofan"
67
+ target = "_blank"
68
+ rel = "noopener noreferrer"
69
+ style = { { fontWeight : "bold" , textDecoration : "none" } }
70
+ >
71
+ Invalid
72
+ </ a >
73
+ </ p >
74
+
75
+ </ div >
76
+ </ main >
77
+ </ Layout >
78
+ ) ;
79
+ }
80
+
81
+ // start of addoverrides.js - created by Invalid
82
+ function appendOverrides ( json ) {
83
+ const material = json . Material . Instance [ 0 ] . Binder [ 0 ] ;
84
+
85
+ const overrides = {
86
+ Overrides : {
87
+ Texture : { } ,
88
+ Color : { }
89
+ }
90
+ } ;
91
+
92
+ material [ 'Float Value' ] . forEach ( floatVal => {
93
+ if ( floatVal . Enabled && ! floatVal . Name . includes ( "gm_mTransform" ) ) {
94
+ overrides . Overrides [ floatVal . Name ] = floatVal . Value ;
95
+ }
96
+ } ) ;
97
+
98
+ material . Texture . forEach ( texture => {
99
+ if ( texture . Enabled ) {
100
+ overrides . Overrides . Texture [ texture . Name . replace ( "map" , "" ) ] = texture [ 'Texture Id' ] ;
101
+ }
102
+ } ) ;
103
+
104
+ material . Color . forEach ( color => {
105
+ if ( color . Enabled ) {
106
+ overrides . Overrides . Color [ color . Name ] = color . Value ;
107
+ }
108
+ } ) ;
109
+
110
+ return {
111
+ ...json ,
112
+ ...overrides
113
+ } ;
114
+ }
115
+ // end of addoverrides.js
0 commit comments