docs.unity3d.com
Version: 

    Bake Ambient Occlusion

    API functions:

    • algo.bakeMaps
    • material.exportImage
    • algo.BakeMap
    • algo.BakingMethod
    • material.filterAO
    • material.fillUnusedPixels

    Ambient occlusion is a rendering technique increasing the visual quality of your scene. To limit the computational cost in your real time application, we can precompute the self occlusion of an object, and store these values in a dedicated texture. Using algo.BakeMap, Pixyz SDK allow you to perform such operation in your pipeline, in a fast way (GPU accelerated).

    Code example

    Bake AO and export it to a png

    Create a sphere and a torus, then precompute the ambiant occlusion information from the 2 objects on the sphere. Then we store this information in a png texture.

    sphere = scene.createSphere(20, 64, 64, True)
    torus = scene.createTorus(40, 15, 64, 64)
    
    
    # Samples used per pixel. Increasing this value will increase the quality. But it will have an important impact on the computational cost.
    # We recommend keeping this value between 8 and 64.
    samples = 32
    # Your AO texture resolution
    resolution = 512
    
    # No need to have occurrences in the scene to bake maps.
    # scene.setParent(sphere, scene.getRoot())
    # scene.setParent(torus, scene.getRoot())
    
    # Occurrences that will receive the light information.
    destination = [sphere]
    # Occurrences that will be used to compute light information.
    source = [sphere, torus]
    
    # Default color of your AO texture.
    ao_default_color = [0, 0, 0, 0]
    
    properties = [scene.PropertyValue("SamplesPerPixel", f"{samples}"),
                    scene.PropertyValue("DefaultColor", f"{ao_default_color}")]
    
    # We bake 2 maps at the same time to take advantage of keeping internal structures during the computation.
    # The normal map will be used for the denoising step.
    maps_to_bake = [algo.BakeMap(algo.MapType.ComputeAO, properties), algo.BakeMap(algo.MapType.Normal, [])]
    
    baked_maps = algo.bakeMaps(destination, source, maps_to_bake, 0, resolution, 1, True, "_AO",
                                [], -1, algo.BakingMethod.RayOnly, 0.1, False, 0.01, [])
    
    # We denoise the AO map using the computed AO and Normal maps.
    # This optimisation allow us to have a smooth lighting result without having to increase the samplePerPixel value (which is expensive).
    filtered_AO_map = material.filterAO(baked_maps[0], baked_maps[1], 40, 0.4, 0.01, 4, True, 0.01)
    # We fill the empty pixels in our texture to prevent bleeding in later stage of your rendering engine (mipmaping...)
    filled_AO_map = material.fillUnusedPixels(filtered_AO_map, ao_default_color)
    
    export_path = "./" + str(scene.getOccurrenceName(destination[0])) + ".png"
    material.exportImage(filled_AO_map, export_path)
    
    Version 2024.3.0.10
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX.