«spinning doughnut» by Fredrik Olofsson

on 22 Dec'12 17:23 in graphicanimation3d
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
//req. Canvas3D quark: Quarks.install("Canvas3D");
//works with both cocoa and qt GUI
//see thread https://www.listarc.bham.ac.uk/lists/sc-users/msg06458.html for more examples
//cmd+period to stop

(
	var width = 500, height = 400;
	var w, u;
	
	var segments= 40;
	var minorRadius= 0.667;
	var majorRadius= 1;
	
	w = Window("spinning doughnut", Rect(128, 64, width, height), false)
		.front;
	u = Canvas3D(w, Rect(0, 0, width, height));
	u.add(Canvas3DItem()
		.paths_(
			Array.fill(segments+1, {|i|
				var u= i/segments*2pi;
				Array.fill(segments+1, {|j|
						var v= j/segments*2pi;
						var x= (minorRadius*cos(v)+majorRadius)*cos(u);
						var y= (minorRadius*cos(v)+majorRadius)*sin(u);
						var z= minorRadius*sin(v);
						[x, y, z];
					});
				})
		)
	);
	u.animate(40, {|t|
		u.transforms = [
			Canvas3D.mRotateY(t/52%2pi*sin(t/30)),
			Canvas3D.mRotateX(t/50%2pi),
			Canvas3D.mScale(1, 1, sin(t/32)*2)
		];
	});
	CmdPeriod.doOnce({w.close});
)



//and with trails
(
	var width = 500, height = 400;
	var w, u;
	
	var segments= 40;
	var minorRadius= 2;
	var majorRadius= 1;
	
	w = Window("spinning doughnut tails", Rect(128, 64, width, height), false)
		.front;
	u = Canvas3D(w, Rect(0, 0, width, height));
	u.view.clearOnRefresh= false;
	u.add(Canvas3DItem()
		.paths_(
			Array.fill(segments+1, {|i|
				var u= i/segments*2pi;
				Array.fill(segments+1, {|j|
						var v= j/segments*2pi;
						var x= (minorRadius*cos(v)+majorRadius)*cos(u);
						var y= (minorRadius*cos(v)+majorRadius)*sin(u);
						var z= minorRadius*sin(v);
						[x, y, z];
					});
				})
		)
	);
	u.view.drawFunc= {
		Pen.fillColor= Color.grey(1, 0.1);	//alpha set trail amount
		Pen.fillRect(Rect(0, 0, width, height));
	}<>u.view.drawFunc;
	u.animate(40, {|t|
		u.transforms = [
			Canvas3D.mRotateY(t/22),
			Canvas3D.mRotateX(t/25*sin(t/23)),
			Canvas3D.mScale(1, 1, sin(t/24))
		];
	});
	CmdPeriod.doOnce({w.close});
)



//and with sound input from mic
(
	var width = 500, height = 400;
	var w, u, amp= 0.1;
	
	var segments= 25;
	var minorRadius= 0.5;
	var majorRadius= 1.5;
	
	w = Window("spinning wireframe doughnut", Rect(128, 64, width, height), false)
		.front;
	u = Canvas3D(w, Rect(0, 0, width, height));
	u.add(Canvas3DItem()
		.paths_(
			Array.fill(segments+1, {|i|
				var u= i/segments*2pi;
				Array.fill(segments+1, {|j|
					var v= j/segments*2pi;
					var x= (minorRadius*cos(v)+majorRadius)*cos(u);
					var y= (minorRadius*cos(v)+majorRadius)*sin(u);
					var z= minorRadius*sin(v);
					[x, y, z];
				});
			})
			++
			Array.fill(segments+1, {|i|
				var u= i/segments*2pi;
				Array.fill(segments+1, {|j|
					var v= j/segments*2pi;
					var x= (minorRadius*cos(u)+majorRadius)*cos(v);
					var y= (minorRadius*cos(u)+majorRadius)*sin(v);
					var z= minorRadius*sin(u);
					[x, y, z];
				});
			})
		)
	);
	u.animate(60, {|t|
		u.transforms = [
			Canvas3D.mRotateY(sin(t/42)*sin(t/53)),
			Canvas3D.mRotateX(sin(t/31)*sin(t/64)),
			Canvas3D.mRotateX(sin(t/20)*sin(t/75)),
			Canvas3D.mScale(amp, amp, sin(t/9)*sin(t/86))
		];
	});
	
	//--sound input
	s.waitForBoot{
		OSCFunc({|m| amp= m[3].explin(0.01, 1, 0.667, 1)}, \tr, s.addr);
		{SendTrig.kr(Impulse.kr(60), 0, Amplitude.kr(SoundIn.ar, 0.1, 0.4)); DC.ar(0)}.play;
	};
	
	CmdPeriod.doOnce({w.close});
)
raw 3425 chars (focus & ctrl+a+c to copy)
reception
comments