Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /nfs/c03/h07/mnt/48256/domains/blog.sokay.net/html/wp-includes/pomo/plural-forms.php on line 210

Warning: Cannot modify header information - headers already sent by (output started at /nfs/c03/h07/mnt/48256/domains/blog.sokay.net/html/wp-includes/pomo/plural-forms.php:210) in /nfs/c03/h07/mnt/48256/domains/blog.sokay.net/html/wp-content/plugins/bad-behavior/bad-behavior/screener.inc.php on line 9

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /nfs/c03/h07/mnt/48256/domains/blog.sokay.net/html/wp-content/plugins/jetpack/_inc/lib/class.media-summary.php on line 77

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /nfs/c03/h07/mnt/48256/domains/blog.sokay.net/html/wp-content/plugins/jetpack/_inc/lib/class.media-summary.php on line 87
Stars and Clouds in Away 3D 4.0 - blog.sokay.net

Shortly after I posted my tutorial on creating a Starfield with Away 3D Lite I tried to see how much I could improve performance with Away 3D 4.0, which was Flash 11 and in Beta stage at the time. I got caught up with finishing Donut Get! and working on Unity stuff and forgot about making a post about the demo. Here’s a post about that Away 3D 4.0 demo I made.

This was inspired by playing Kid Icarus on 3DS. During the flying segments, you’re always flying through clouds which is a great effect. I think they do something similar to this, where the clouds are 3d slices of a cloud which you pass through.

I created the clouds with noise. To get “3D slices” I used a 3d Perlin noise generator by Ron Valstar. The regular Flash noise class lets you generate noise on X and Y axis, this allows you to move in the Z axis to create slices.


View the tutorial demo here: http://blog.sokay.net/stuff/starfield2/

I used Sprite3Ds for the cloud slices, hoping that that would allow me to use a greater amount of cloud slices. In this demo I’m using 12 separate Sprite3D instances. It takes a while to generate the noise at the beginning, I don’t think this noise generator anywhere near as fast as Flash’s internal one unfortunately. I spent a long ass time adjusting the Generator and settled on this result which I felt was looking pretty good, rather than just settling for something that was fast to generate.

Sprite3D displays like a billboard/plane in 3D space that is always pointed towards the camera. The problem with Sprite3D is that it’s displayed as a point in space, and if the point isn’t within the camera viewport, it doesn’t display at all. You can see this issue as you rotate the camera to the side. I tried a list minute fix to change it to planes but I couldn’t get the planes to display. It may have something to do with the scale of the scene, the slices are like 15,000 width. Not spending any time trying to figure it out so good luck with it if you wanna try!

Here is the source of the StarField class, which sets up the Away3D scene:

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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
public class StarField extends Sprite
{

private var m_camera:Camera3D;
private var m_view:View3D;
private var m_scene:Scene3D;

private var starContainer:ObjectContainer3D;
private var starCount:int = 200;
private var stars:Vector.;

private var cloudSliceCount:int = 12;
private var clouds:Vector.;
//private var clouds:Vector.;
private var cloudDistance:int = 10000;

private var cloudTestBitmap:Bitmap;
private var cloudTestBitmapData:BitmapData;
private var genClouds:GenerateClouds;

//you can import an image like this
//[Embed(source = "../assets/star.png" , mimeType="image/png")]
//private var starArt:Class;
private var starArt:BitmapData; // in this tutorial we'll create a bitmapData from scratch

private var rotateSpeed:int = 1;
private var rotateDirection:String = "none";

public function StarField()
{
mouseChildren = false;
mouseEnabled = false;

addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event) : void {

removeEventListener(Event.ADDED_TO_STAGE, init);

/// clouds

genClouds = new GenerateClouds();

cloudTestBitmapData = new BitmapData(50,50,true,0xFFFF0000);
cloudTestBitmap = new Bitmap(cloudTestBitmapData);
addChild(cloudTestBitmap);

////////

initView();
initObjects();

/////////

//genClouds.generateCloudSlice(cloudTestBitmap);

/// ENTERFRAME
addEventListener(Event.ENTER_FRAME, act);

}

private function initView() : void {

// create view
m_view = new View3D();
addChild(m_view);
//m_view.antiAlias = 4;

m_view.backgroundColor = 0x0033CC;

// debug
var stats:AwayStats = new AwayStats(m_view,true);
stats.x = 450;
addChild(stats);

// scene

m_scene = m_view.scene;

// camera

m_camera = m_view.camera;

//set camera location
m_view.camera.y = 0;
m_view.camera.x = 0;
m_view.camera.z = 0;
//m_view.camera.zoom = 10;
//m_view.camera.focus = 30;

m_camera.lens.far = 100000;

}

private function initLights() : void {

}

private function initObjects() : void {

starContainer = new ObjectContainer3D();
m_scene.addChild(starContainer);

//create bitmapData for our material, 4x4 and white! (0xffffff)
starArt = new BitmapData(4,4,false,0xFFFFFF);

// create an Away3D Material with that bitmapData, this is the image used for our particles!

//var starMaterial:BitmapMaterial = new BitmapMaterial(new starArt().bitmapData); // use this if you decide to import an asset
//var starMaterial:BitmapMaterial = new BitmapMaterial(starArt); // BitmapMaterial() is deprecated
var starBitmapTexture:BitmapTexture = new BitmapTexture(starArt);
var starMaterial:TextureMaterial = new TextureMaterial(starBitmapTexture);
var starMaterial2:ColorMaterial = new ColorMaterial(0xFFFFFF);

var cloudBitmapTexture:BitmapTexture = new BitmapTexture(genClouds.getCloudSlice());
var cloudMaterial:TextureMaterial = new TextureMaterial(cloudBitmapTexture);
cloudMaterial.alphaBlending = true;

// create a Vector array to the size of "starCount"
stars = new Vector.(starCount , true);
clouds = new Vector.(cloudSliceCount , true);

// merge
//var merge:Merge = new Merge();

// start filling the Vector with Sprite3D objects!
for (var i:int = 0; i < starCount; i++) {

var starMaterial3:ColorMaterial = new ColorMaterial(0xFFFFFF);

var star:Sprite3D = new Sprite3D(starMaterial3,4,4);

//star.alignmentType = AlignmentType.VIEWPOINT; // I forgot what this does... haha! look it up!

star.x = -400 + (Math.random() * 800);
star.y = -500 + (Math.random() * 800);
star.z = -1000 + (Math.random() * (1000 + 800));

///m_scene.addChild(star);
starContainer.addChild(star);

//merge.apply(starContainer, star);

stars[i] = star;
}

// clouds
for (i = 0; i < cloudSliceCount; i++) {

var cloudBitmapTextureTemp:BitmapTexture = new BitmapTexture(genClouds.getCloudSlice());
var cloudMaterialTemp:TextureMaterial = new TextureMaterial(cloudBitmapTextureTemp);
cloudMaterialTemp.alphaBlending = true;

var cloud:Sprite3D = new Sprite3D(cloudMaterialTemp,15000,13000); // trying Sprite3D, disappear when center isn't in frame :(
//var cloudShape:PlaneGeometry = new PlaneGeometry(15000,13000,1,1); // trying Meshes but the scale may be to huge, they werent displaying
//var cloud:Mesh = new Mesh(cloudShape,cloudMaterialTemp);
//cloudShape = null;

cloud.x = 0;
cloud.y = 0;
cloud.z = -cloudDistance + (((cloudDistance * 2) / (cloudSliceCount - 0)) * i );

m_scene.addChild(cloud);

clouds[i] = cloud;
}

}

// sets the rotation to look in a certain direction

public function lookForward(): void {
m_camera.rotationY = 180;
m_camera.rotationX = 0;
}

public function lookBackward(): void {
m_camera.rotationY = 0;
m_camera.rotationX = 0;
}

public function lookLeft(): void {
m_camera.rotationY = -90;
m_camera.rotationX = 0;
}

public function lookRight(): void {
m_camera.rotationY = 90;
m_camera.rotationX = 0;
}

// set a rotation direction with the keys

public function rotateLeft() : void {
rotateDirection = "left";
}

public function rotateRight() : void {
rotateDirection = "right";
}

public function rotateUp() : void {
rotateDirection = "up";
}

public function rotateDown() : void {
rotateDirection = "down";
}
public function rotateNone() : void {
rotateDirection = "none";
}

//

private function act( e:Event ) : void {

onPreRender();

m_view.render();

}

// this onPreRender function fires every frame, thanks to our nift Away3d template file!

protected function onPreRender():void
{
var starCount:int = stars.length;
for (var i:int = 0; i < starCount; i++) {

var star:Sprite3D = stars[i];

star.z += 20; // stars move forward on Z-axis every frame

if (star.z > 800) {
star.z = -1000; // when stars move past limit of 800, set them back to -1000 so they loop forever!
}

// fade
var starZ:int = star.z;
if (starZ < 0) starZ *= -1;
var facePerc2:Number = starZ / 800;
//trace("fadePerc: " + facePerc);
ColorMaterial(star.material).alpha = 1 - (facePerc2 * 0.5);

}

var cloudCount:int = clouds.length;
for (var j:int = 0; j < cloudCount; j++) {

var cloud:Sprite3D = clouds[j];

cloud.z += 200; // stars move forward on Z-axis every frame

if (cloud.z >= cloudDistance) {

var cloudBitmapTextureTemp:BitmapTexture = new BitmapTexture(genClouds.getCloudSlice());
var cloudMaterialTemp:TextureMaterial = new TextureMaterial(cloudBitmapTextureTemp);
cloudMaterialTemp.alphaBlending = true;
cloud.material = cloudMaterialTemp;

cloud.z = -cloudDistance; // when stars move past limit of 800, set them back to -1000 so they loop forever!
//TextureMaterial(cloud.material).alpha = 1;
}

// fade
var cloudZ:int = cloud.z;
if (cloudZ < 0) cloudZ *= -1;
var facePerc:Number = cloudZ / cloudDistance;
//trace("fadePerc: " + facePerc);
TextureMaterial(cloud.material).alpha = 1 - facePerc;

}

/*
starContainer.z += 20;
if (starContainer.z > 800) {
starContainer.z = -1000;
}
*/


// handle rotations!
if (rotateDirection == "left") {
m_camera.rotationY -= rotateSpeed;
} else if (rotateDirection == "right") {
m_camera.rotationY += rotateSpeed;
}
if (rotateDirection == "up") {
m_camera.rotationX -= rotateSpeed;
} else if (rotateDirection == "down") {
m_camera.rotationX += rotateSpeed;
}

// new cloud slice
//genClouds.generateCloudSlice(cloudTestBitmap);
cloudTestBitmap.bitmapData = genClouds.getCloudSlice();

}

}

And here’s the class I used to generate the slices ‘o’ clouds. I generate 50 128×128 bitmapDatas of Noise, spread out over the Z axis. Then swap these out between the 12 cloud slice Sprite3Ds. When the cloud slice reaches the end of the area, it moves back to the other end and gets a new cloud noise texture.

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
public class GenerateClouds
{
private var bitmapData:BitmapData;

private var baseX:Number = 20;
private var baseY:Number = 20;
private var numOctaves:uint = 8;
private var randomSeed:Number = 13;
private var stitch:Boolean = true;
private var fractalNoise:Boolean = true;
private var channelOptions:uint = 7;
private var grayScale:Boolean = true;
private var offsets:Array;

private var cmf:ColorMatrixFilter;

private var cloudSlices:Vector.;

private var sliceWidth:int = 128;
private var sliceHeight:int = 128;
private var sliceZ:Number = 0;

private var currentSlice:int;
private var totalSlices:int = 50;

public function GenerateClouds()
{

bitmapData = new BitmapData(sliceWidth,sliceHeight,true);

cmf = new ColorMatrixFilter([0,0,0,0,255,
0,0,0,0,255,
0,0,0,0,255,
1,0,0,0,0]);

cmf = new ColorMatrixFilter([0,0,0,0,255,
0,0,0,0,255,
0,0,0,0,255,
0.4,0,0,0,0]);

generateSlices(totalSlices);

/*
var myBitmapDataObject:BitmapData = new BitmapData(640, 480);
//var seed:Number = Math.floor(Math.random()*100);
//myBitmapDataObject.perlinNoise(320, 240, 8, seed, true, true, 7, false, null);
var myBitmap:Bitmap = new Bitmap(myBitmapDataObject);
addChildAt(myBitmap, 0);
*/

}

public function generateSlices( _count:int ) : void {

cloudSlices = new Vector.(_count,true);

Perlin.octaves = 4;
Perlin.falloff = 0.01;

for (var i:uint = 0; i < _count; i++) {

generateCloudSlice(i);

bitmapData.applyFilter(bitmapData, bitmapData.rect, new Point(), cmf);

cloudSlices[i] = bitmapData.clone();
}

}

public function generateCloudSlice( _count:int ) : void {

var bd:BitmapData = bitmapData;

var depthSpeed:Number = 1;

for ( var i:uint = 0; i < bd.width; i++ ) {
for ( var j:uint = 0;j < bd.height; j++ ) {

var size:Number = 10;

var fNoise:Number = Perlin.noise( i/size, j/size, _count * depthSpeed );
var iNoise:int = int( (.5+1.*(fNoise-.5))*255 );
var iColor:uint = iNoise << 16 | iNoise << 8 | iNoise;
var iAlpha:uint = (iColor>>>24);
var iCompsite:uint = iAlpha + iColor;
bd.setPixel(i,j, iColor );

}
}

}

public function getCloudSlice( _number:int = -1) : BitmapData {

if (_number == -1) {
_number = currentSlice++;
if (currentSlice>= totalSlices) {
currentSlice = 0;
}
}

//_bitmap.bitmapData = cloudSlices[_number].clone();
/*
var clone:BitmapData = cloudSlices[_number].clone();
var alpha:BitmapData = new BitmapData(clone.width,clone.height, true, 0xFFFFFFFF);
alpha.fillRect(new Rectangle(0,0,25, 50), 0x00000000);

var bd:BitmapData = new BitmapData(clone.width,clone.height, true)
bd.copyPixels(clone, new Rectangle(0,0,clone.width,clone.height), new Point(0,0), clone, new Point(0,0), false);
*/

//return bd;
return cloudSlices[_number].clone();

}

public function getSlice(_sliceNum:int) : BitmapData {

var bd:BitmapData = new BitmapData(100,1);

bd.copyPixels(bitmapData, new Rectangle(0,0,100,1), new Point(0,0), bitmapData, new Point(0,0), true);

return bd;

}
}

——————

Download the source here:

In the end, the speed wasn’t as fast as I was expecting. I figure it might have something to do with the number of particles and objects I was displaying, and not just the complexity. Don’t get me wrong, it’s a lot faster than what I was using while working on the Soul Calibur site, but not that Unreal Demo speed. Still don’t know what I’m doing wrong… Oh well! haha

(this demo used Away 3D 4.0 beta55)

About the author:
Bryson Whiteman (http://www.sonofbryce.com)
Bryson is the guy behind all of the Sokay creations. Heading artwork and development, he's determined to make sure each game has a "distinctively Sokay" quality to them. He's always looking forward for a chance to experiment with new technologies to explore exciting ways to achieve fun.