-
Notifications
You must be signed in to change notification settings - Fork 1
/
main3.dart
263 lines (242 loc) · 7.12 KB
/
main3.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import 'dart:math';
import 'package:arcore_flutter_plugin/arcore_flutter_plugin.dart';
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math_64.dart' as vector;
//do not use this dart. This dart is an example of ar, which cannot be displayed
//at the moment
void main() {
runApp(MaterialApp(
title: "Test Main",
home: RuntimeMaterials(),
));
}
class RuntimeMaterials extends StatefulWidget {
@override
_RuntimeMaterialsState createState() => _RuntimeMaterialsState();
}
class _RuntimeMaterialsState extends State<RuntimeMaterials> {
ArCoreController arCoreController;
ArCoreNode sphereNode;
double metallic = 0.0;
double roughness = 0.4;
double reflectance = 0.5;
Color color = Colors.yellow;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Materials Runtime Change'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.update),
onPressed: () {},
)
],
),
body: Column(
children: <Widget>[
SphereControl(
initialColor: color,
initialMetallicValue: metallic,
initialRoughnessValue: roughness,
initialReflectanceValue: reflectance,
onColorChange: onColorChange,
onMetallicChange: onMetallicChange,
onRoughnessChange: onRoughnessChange,
onReflectanceChange: onReflectanceChange,
),
Expanded(
child: ArCoreView(
onArCoreViewCreated: _onArCoreViewCreated,
),
),
],
),
),
);
}
void _onArCoreViewCreated(ArCoreController controller) {
arCoreController = controller;
_addSphere(arCoreController);
}
void _addSphere(ArCoreController controller) {
final material = ArCoreMaterial(
color: Colors.yellow,
);
final sphere = ArCoreSphere(
materials: [material],
radius: 0.1,
);
sphereNode = ArCoreNode(
shape: sphere,
position: vector.Vector3(0, 0, -1.5),
);
controller.addArCoreNode(sphereNode);
}
onColorChange(Color newColor) {
if (newColor != this.color) {
this.color = newColor;
updateMaterials();
}
}
onMetallicChange(double newMetallic) {
if (newMetallic != this.metallic) {
metallic = newMetallic;
updateMaterials();
}
}
onRoughnessChange(double newRoughness) {
if (newRoughness != this.roughness) {
this.roughness = newRoughness;
updateMaterials();
}
}
onReflectanceChange(double newReflectance) {
if (newReflectance != this.reflectance) {
this.reflectance = newReflectance;
updateMaterials();
}
}
updateMaterials() {
debugPrint("updateMaterials");
if (sphereNode == null) {
return;
}
debugPrint("updateMaterials sphere node not null");
final material = ArCoreMaterial(
color: color,
metallic: metallic,
roughness: roughness,
reflectance: reflectance,
);
sphereNode.shape.materials.value = [material];
}
@override
void dispose() {
arCoreController.dispose();
super.dispose();
}
}
class SphereControl extends StatefulWidget {
final double initialRoughnessValue;
final double initialReflectanceValue;
final double initialMetallicValue;
final Color initialColor;
final ValueChanged<Color> onColorChange;
final ValueChanged<double> onMetallicChange;
final ValueChanged<double> onRoughnessChange;
final ValueChanged<double> onReflectanceChange;
const SphereControl(
{Key key,
this.initialRoughnessValue,
this.initialReflectanceValue,
this.initialMetallicValue,
this.initialColor,
this.onColorChange,
this.onMetallicChange,
this.onRoughnessChange,
this.onReflectanceChange})
: super(key: key);
@override
_SphereControlState createState() => _SphereControlState();
}
class _SphereControlState extends State<SphereControl> {
double metallicValue;
double roughnessValue;
double reflectanceValue;
Color color;
@override
void initState() {
roughnessValue = widget.initialRoughnessValue;
reflectanceValue = widget.initialReflectanceValue;
roughnessValue = widget.initialRoughnessValue;
color = widget.initialColor;
super.initState();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
children: <Widget>[
RaisedButton(
child: Text("Random Color"),
onPressed: () {
final newColor = Colors.accents[Random().nextInt(14)];
widget.onColorChange(newColor);
setState(() {
color = newColor;
});
},
),
Padding(
padding: const EdgeInsets.only(left: 20.0),
child: CircleAvatar(
radius: 20.0,
backgroundColor: color,
),
),
],
),
Row(
children: <Widget>[
Text("Metallic"),
Checkbox(
value: metallicValue == 1.0,
onChanged: (value) {
metallicValue = value ? 1.0 : 0.0;
widget.onMetallicChange(metallicValue);
setState(() {});
},
)
],
),
Row(
children: <Widget>[
Text("Roughness"),
Expanded(
child: Slider(
value: roughnessValue,
divisions: 10,
onChangeEnd: (value) {
roughnessValue = value;
widget.onRoughnessChange(roughnessValue);
},
onChanged: (double value) {
setState(() {
roughnessValue = value;
});
},
),
),
],
),
Row(
children: <Widget>[
Text("Reflectance"),
Expanded(
child: Slider(
value: reflectanceValue,
divisions: 10,
onChangeEnd: (value) {
reflectanceValue = value;
widget.onReflectanceChange(reflectanceValue);
},
onChanged: (double value) {
setState(() {
reflectanceValue = value;
});
},
),
),
],
),
],
),
);
}
}