-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathChangeColor.ts
38 lines (33 loc) · 1.23 KB
/
ChangeColor.ts
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
// START MARKER Set Random Color
import { Behaviour, serializeable, Renderer } from "@needle-tools/engine";
import { Color } from "three";
export class RandomColor extends Behaviour {
@serializeable()
applyOnStart: boolean = true;
start() {
if (this.applyOnStart)
this.applyRandomColor();
// if materials are not cloned and we change the color they might also change on other objects
const cloneMaterials = true;
if (cloneMaterials) {
const renderer = this.gameObject.getComponent(Renderer);
if (!renderer) {
return;
}
for (let i = 0; i < renderer.sharedMaterials.length; i++) {
renderer.sharedMaterials[i] = renderer.sharedMaterials[i].clone();
}
}
}
applyRandomColor() {
const renderer = this.gameObject.getComponent(Renderer);
if (!renderer) {
console.warn("Can not change color: No renderer on " + this.name);
return;
}
for (let i = 0; i < renderer.sharedMaterials.length; i++) {
renderer.sharedMaterials[i].color = new Color(Math.random(), Math.random(), Math.random());
}
}
}
// END MARKER Set Random Color