Avg. Rating 5.0
Tags:



Problem

Stage3D supports stencil tests. How do I use stencils in ActionScript to create a reflection?

Solution

Use the same way as in OpenGL or DirectX - you will need Context3D.setRenderToTexture()

Detailed explanation

This function is typically used to render the scene for dynamic reflections.

Below I`ve described the very general approach to solving this problem but in my opinion it is better to use in some custom< materials

function render(): void {
        //1. Clear buffers
        _myContext3d.clear(/*clear colors*/);
        
        //2. remember current state of projection matrix (Matrix3D) and
        //transformation of your camera (view)
        _cameraCurrentPosition = _myCamera.position.clone();
        _cameraCurrentDirection = _myCamera.direction.clone();


        //3. change camera settings
        //it should "look" from the other side of reflective surface
        _myCamera.position = newCalculatedPosition;
        _myCamera.lookAt(inverseDirection);


        //4. set rendering to texture
        if(!_reflectTexture){
            _myReflectTexture = _myContext3d.createTexture(SIZE, SIZE,
                Context3DTextureFormat.BGRA, true);
            _reflectSurface.texture = _ReflectTexture;
        }
        _myContext3d.setRenrerToTexture(_myReflectTexture, true, 2);
        
        //5. render your scene
        renderScene();

        //6. return previous settings
        _myCamera.position = _cameraCurrentPosition;
        _myCamera.direction = _cameraCurrentDirection;

        _myContext3D.setRenderToBackBuffer();

        //7. Now you can render reflective surface and scene in original way
        renderReflectSurface();
        renderScene();

        _myContext3D.present();
    }

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes