diff --git a/src/alternativa/engine3d/core/Camera3D.as b/src/alternativa/engine3d/core/Camera3D.as index 50de4ca..e69d9ee 100644 --- a/src/alternativa/engine3d/core/Camera3D.as +++ b/src/alternativa/engine3d/core/Camera3D.as @@ -243,9 +243,8 @@ public class Camera3D extends Object3D { renderer.camera = this; // Projection argument calculating calculateProjection(view._width, view._height); - // TODO: clear after shadows rendering // Preparing to rendering - view.prepareToRender(stage3D, context3D); + view.configureContext3D(stage3D, context3D, this); // Transformations calculating if (transformChanged) composeTransforms(); localToGlobalTransform.copy(transform); @@ -274,7 +273,7 @@ public class Camera3D extends Object3D { } else { root.culling = 63; } - // Calculations of conent visibility + // Calculations of content visibility if (root.culling >= 0) root.calculateVisibility(this); // Calculations visibility of children root.calculateChildrenVisibility(this); @@ -357,6 +356,16 @@ public class Camera3D extends Object3D { } raysLength = view.raysLength; + var r:Number = ((view.backgroundColor >> 16) & 0xff)/0xff; + var g:Number = ((view.backgroundColor >> 8) & 0xff)/0xff; + var b:Number = (view.backgroundColor & 0xff)/0xff; + if (view._canvas != null) { + r *= view.backgroundAlpha; + g *= view.backgroundAlpha; + b *= view.backgroundAlpha; + } + context3D.clear(r, g, b, view.backgroundAlpha); + // Check getting in frustum and occluding if (root.culling >= 0 && (root.boundBox == null || occludersLength == 0 || !root.boundBox.checkOcclusion(occluders, occludersLength, root.localToCameraTransform))) { // Check if the ray crossing the bounding box @@ -410,7 +419,6 @@ public class Camera3D extends Object3D { } // Gather the draws for children root.collectChildrenDraws(this, lights, lightsLength, root.useShadow); - // Mouse events prosessing view.processMouseEvents(context3D, this); // Render diff --git a/src/alternativa/engine3d/core/RendererContext3DProperties.as b/src/alternativa/engine3d/core/RendererContext3DProperties.as index 4c2fabe..59a6502 100644 --- a/src/alternativa/engine3d/core/RendererContext3DProperties.as +++ b/src/alternativa/engine3d/core/RendererContext3DProperties.as @@ -8,7 +8,11 @@ package alternativa.engine3d.core { + import alternativa.engine3d.materials.ShaderProgram; + import alternativa.engine3d.resources.Geometry; + import flash.display3D.Program3D; + import flash.utils.Dictionary; /** * @private @@ -18,6 +22,10 @@ package alternativa.engine3d.core { public var isConstrained:Boolean = false; + public var backBufferWidth:int = -1; + public var backBufferHeight:int = -1; + public var backBufferAntiAlias:int = -1; + public var usedBuffers:uint = 0; public var usedTextures:uint = 0; @@ -26,5 +34,11 @@ package alternativa.engine3d.core { public var blendSource:String; public var blendDestination:String; + // View: mouse events + // Key - vertex program of object, value - program. + public var drawDistancePrograms:Dictionary = new Dictionary(); + public var drawColoredRectProgram:ShaderProgram; + public var drawRectGeometry:Geometry; + } } diff --git a/src/alternativa/engine3d/core/View.as b/src/alternativa/engine3d/core/View.as index 67c1367..4d3a503 100644 --- a/src/alternativa/engine3d/core/View.as +++ b/src/alternativa/engine3d/core/View.as @@ -61,10 +61,6 @@ package alternativa.engine3d.core { private static const renderEvent:MouseEvent = new MouseEvent("render"); - private static var properties:Dictionary = new Dictionary(true); - private var cachedContext3D:Context3D; - private var context3DProperties:Context3DViewProperties; - static private var drawDistanceFragment:Linker; static private var drawDistanceVertexProcedure:Procedure; @@ -538,7 +534,7 @@ package alternativa.engine3d.core { /** * @private */ - alternativa3d function prepareToRender(stage3D:Stage3D, context:Context3D):void { + alternativa3d function configureContext3D(stage3D:Stage3D, context3D:Context3D, camera:Camera3D):void { if (_canvas == null) { var vis:Boolean = this.visible; for (var parent:DisplayObject = this.parent; parent != null; parent = parent.parent) { @@ -558,70 +554,55 @@ package alternativa.engine3d.core { createRenderBitmap(); } } - if (context != cachedContext3D) { - // Get properties. - cachedContext3D = context; - context3DProperties = properties[cachedContext3D]; - if (context3DProperties == null) { - context3DProperties = new Context3DViewProperties(); - // Inititalize data for mouse events - var rectGeometry:Geometry = new Geometry(4); - rectGeometry.addVertexStream([VertexAttributes.POSITION, VertexAttributes.POSITION, VertexAttributes.POSITION, VertexAttributes.TEXCOORDS[0], VertexAttributes.TEXCOORDS[0]]); - rectGeometry.setAttributeValues(VertexAttributes.POSITION, Vector.([0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1])); - rectGeometry.setAttributeValues(VertexAttributes.TEXCOORDS[0], Vector.([0, 0, 0, 1, 1, 1, 1, 0])); - rectGeometry.indices = Vector.([0, 1, 3, 2, 3, 1]); - rectGeometry.upload(context); - var vLinker:Linker = new Linker(Context3DProgramType.VERTEX); - vLinker.addProcedure(Procedure.compileFromArray([ - "#a0=a0", - "#c0=c0", - "mul t0.x, a0.x, c0.x", - "mul t0.y, a0.y, c0.y", - "add o0.x, t0.x, c0.z", - "add o0.y, t0.y, c0.w", - "mov o0.z, a0.z", - "mov o0.w, a0.z", - ])); - var fLinker:Linker = new Linker(Context3DProgramType.FRAGMENT); - fLinker.addProcedure(Procedure.compileFromArray([ - "#c0=c0", - "mov o0, c0", - ])); - var coloredRectProgram:ShaderProgram = new ShaderProgram(vLinker, fLinker); - coloredRectProgram.upload(context); + var context3DProperties:RendererContext3DProperties = camera.context3DProperties; + if (context3DProperties.drawRectGeometry == null) { + // Inititalize data for mouse events + var rectGeometry:Geometry = new Geometry(4); + rectGeometry.addVertexStream([VertexAttributes.POSITION, VertexAttributes.POSITION, VertexAttributes.POSITION, VertexAttributes.TEXCOORDS[0], VertexAttributes.TEXCOORDS[0]]); + rectGeometry.setAttributeValues(VertexAttributes.POSITION, Vector.([0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1])); + rectGeometry.setAttributeValues(VertexAttributes.TEXCOORDS[0], Vector.([0, 0, 0, 1, 1, 1, 1, 0])); + rectGeometry.indices = Vector.([0, 1, 3, 2, 3, 1]); + rectGeometry.upload(context3D); + var vLinker:Linker = new Linker(Context3DProgramType.VERTEX); + vLinker.addProcedure(Procedure.compileFromArray([ + "#a0=a0", + "#c0=c0", + "mul t0.x, a0.x, c0.x", + "mul t0.y, a0.y, c0.y", + "add o0.x, t0.x, c0.z", + "add o0.y, t0.y, c0.w", + "mov o0.z, a0.z", + "mov o0.w, a0.z", + ])); + var fLinker:Linker = new Linker(Context3DProgramType.FRAGMENT); + fLinker.addProcedure(Procedure.compileFromArray([ + "#c0=c0", + "mov o0, c0", + ])); + var coloredRectProgram:ShaderProgram = new ShaderProgram(vLinker, fLinker); + coloredRectProgram.upload(context3D); - context3DProperties.drawRectGeometry = rectGeometry; - context3DProperties.drawColoredRectProgram = coloredRectProgram; - properties[cachedContext3D] = context3DProperties; - } + context3DProperties.drawRectGeometry = rectGeometry; + context3DProperties.drawColoredRectProgram = coloredRectProgram; } if (_width != context3DProperties.backBufferWidth || _height != context3DProperties.backBufferHeight || antiAlias != context3DProperties.backBufferAntiAlias) { context3DProperties.backBufferWidth = _width; context3DProperties.backBufferHeight = _height; context3DProperties.backBufferAntiAlias = antiAlias; - context.configureBackBuffer(_width, _height, antiAlias); + context3D.configureBackBuffer(_width, _height, antiAlias); } - var r:Number = ((backgroundColor >> 16) & 0xff)/0xff; - var g:Number = ((backgroundColor >> 8) & 0xff)/0xff; - var b:Number = (backgroundColor & 0xff)/0xff; - if (canvas != null) { - r *= backgroundAlpha; - g *= backgroundAlpha; - b *= backgroundAlpha; - } - context.clear(r, g, b, backgroundAlpha); } /** * @private */ - alternativa3d function processMouseEvents(context:Context3D, camera:Camera3D):void { + alternativa3d function processMouseEvents(context3D:Context3D, camera:Camera3D):void { var i:int; // Mouse events if (eventsLength > 0) { if (surfacesLength > 0) { // Calculating the depth - calculateSurfacesDepths(context, camera, _width, _height); + calculateSurfacesDepths(context3D, camera, _width, _height); // Sorting by decreasing the depth for (i = 0; i < raysLength; i++) { var raySurfaces:Vector. = raysSurfaces[i]; @@ -781,8 +762,8 @@ package alternativa.engine3d.core { context.setVertexBufferAt(6, null); context.setVertexBufferAt(7, null); - var drawRectGeometry:Geometry = context3DProperties.drawRectGeometry; - var drawColoredRectProgram:ShaderProgram = context3DProperties.drawColoredRectProgram; + var drawRectGeometry:Geometry = camera.context3DProperties.drawRectGeometry; + var drawColoredRectProgram:ShaderProgram = camera.context3DProperties.drawColoredRectProgram; // Rectangle var vLinker:Linker, fLinker:Linker; @@ -888,7 +869,7 @@ package alternativa.engine3d.core { var procedure:Procedure = procedures[index]; var object:Object3D = surface.object; // Program - var drawDistanceProgram:ShaderProgram = context3DProperties.drawDistancePrograms[procedure]; + var drawDistanceProgram:ShaderProgram = camera.context3DProperties.drawDistancePrograms[procedure]; if (drawDistanceProgram == null) { // Assembling the vertex shader var vertex:Linker = new Linker(Context3DProgramType.VERTEX); @@ -907,7 +888,7 @@ package alternativa.engine3d.core { drawDistanceProgram = new ShaderProgram(vertex, drawDistanceFragment); drawDistanceProgram.fragmentShader.varyings = drawDistanceProgram.vertexShader.varyings; drawDistanceProgram.upload(context); - context3DProperties.drawDistancePrograms[procedure] = drawDistanceProgram; + camera.context3DProperties.drawDistancePrograms[procedure] = drawDistanceProgram; } var buffer:VertexBuffer3D = geometry.getVertexBuffer(VertexAttributes.POSITION); if (buffer == null) return; @@ -1410,9 +1391,6 @@ package alternativa.engine3d.core { } } -import alternativa.engine3d.materials.ShaderProgram; -import alternativa.engine3d.resources.Geometry; - import flash.display.BitmapData; import flash.display.Sprite; import flash.events.MouseEvent; @@ -1420,7 +1398,6 @@ import flash.geom.ColorTransform; import flash.geom.Matrix; import flash.net.URLRequest; import flash.net.navigateToURL; -import flash.utils.Dictionary; class Logo extends Sprite { @@ -1518,17 +1495,3 @@ class Logo extends Sprite { } } - -class Context3DViewProperties { - public var backBufferWidth:int = -1; - public var backBufferHeight:int = -1; - public var backBufferAntiAlias:int = -1; - - // Mouse events - - // Key - vertex program of object, value - program. - public var drawDistancePrograms:Dictionary = new Dictionary(); - public var drawColoredRectProgram:ShaderProgram; - public var drawRectGeometry:Geometry; - -}