-
Notifications
You must be signed in to change notification settings - Fork 2
/
A3D4Renderer.as
167 lines (154 loc) · 5.64 KB
/
A3D4Renderer.as
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
/*
* FLINT PARTICLE SYSTEM
* .....................
*
* Author: Richard Lord & Michael Ivanov
* Copyright (c) Richard Lord 2008-2011
* http://flintparticles.org
*
*
* Licence Agreement
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.flintparticles.integration.away3d.v4
{
import away3d.containers.ObjectContainer3D;
import away3d.core.base.Object3D;
import away3d.core.math.Vector3DUtils;
import away3d.entities.Sprite3D;
import away3d.materials.MaterialBase;
import flash.display.BlendMode;
import org.flintparticles.common.particles.Particle;
import org.flintparticles.common.renderers.RendererBase;
import org.flintparticles.common.utils.Maths;
import org.flintparticles.integration.away3d.v4.utils.Convert;
import org.flintparticles.threeD.particles.Particle3D;
import flash.geom.Vector3D;
/**
* Renders the particles in an Away3D4 scene.
*
* <p>To use this renderer, the particles' image properties should be
* Away3D v4(Molehill API based) objects, renderable in an Away3D scene. This renderer
* doesn't update the scene, but copies each particle's properties
* to its image object so next time the Away3D scene is rendered the
* image objects are drawn according to the state of the particle
* system.</p>
*/
public class A3D4Renderer extends RendererBase
{
private var _container:ObjectContainer3D;
/**
* The constructor creates an Away3D renderer for displaying the
* particles in an Away3D scene.
*
* @param container An Away3D object container. The particle display
* objects are created inside this object container.
*/
public function A3D4Renderer( container:ObjectContainer3D )
{
super();
_container = container;
}
/**
* This method copies the particle's state to the associated image object.
*
* <p>This method is called internally by Flint and shouldn't need to be
* called by the user.</p>
*
* @param particles The particles to be rendered.
*/
override protected function renderParticles( particles:Array ):void
{
for each( var p:Particle3D in particles )
{
renderParticle( p );
}
}
protected function renderParticle( particle:Particle3D ):void
{
// N.B. Sprite3D is a subclass of Object3D so we don't need to treat it as a special case
if( particle.image is Object3D )
{
var obj:Object3D = particle.image as Object3D;
obj.x = particle.position.x;
obj.y = particle.position.y;
obj.z = particle.position.z;
obj.scaleX = obj.scaleY = obj.scaleZ = particle.scale;
var rotation:Vector3D = away3d.core.math.Vector3DUtils.quaternion2euler( Convert.QuaternionToA3D( particle.rotation ) );
obj.rotationX = Maths.asDegrees( rotation.x );
obj.rotationY = Maths.asDegrees( rotation.y );
obj.rotationZ = Maths.asDegrees( rotation.z );
if( obj.hasOwnProperty("material") )
{
var material:Object = obj["material"];
(material as MaterialBase).blendMode = "add"; //added - force add blendmode on evverything :)
if( material.hasOwnProperty( "colorTransform" ) )
{
material["colorTransform"] = particle.colorTransform;
}
else
{
if( material.hasOwnProperty( "color" ) )
{
material["color"] = particle.color & 0xFFFFFF;
}
if( material.hasOwnProperty( "alpha" ) )
{
material["alpha"] = particle.alpha;
}
}
}
}
}
/**
* This method is called when a particle is added to an emitter -
* usually because the emitter has just created the particle. The
* method adds the particle's image to the container's display list.
* It is called internally by Flint and need not be called by the user.
*
* @param particle The particle being added to the emitter.
*/
override protected function addParticle( particle:Particle ):void
{
if( particle.image is ObjectContainer3D )
{
_container.addChild( ObjectContainer3D( particle.image ) );
renderParticle( Particle3D( particle ) );
}
}
/**
* This method is called when a particle is removed from an emitter -
* usually because the particle is dying. The method removes the
* particle's image from the container's display list. It is called
* internally by Flint and need not be called by the user.
*
* @param particle The particle being removed from the emitter.
*/
override protected function removeParticle( particle:Particle ):void
{
// We can't clean up the 3d object here because the particle may not be dead.
// We need to handle disposal elsewhere
if( particle.image is ObjectContainer3D )
{
_container.removeChild( ObjectContainer3D( particle.image ) );
}
}
}
}