positions | 用于计算探针的世界空间位置的数组。 |
lightProbes | 产生的光照探针所写入的数组。 |
occlusionProbes | 产生的遮挡探针所写入的数组。 |
计算给定世界空间位置处的光照探针和遮挡探针。
如果场景中未烘焙任何探针,则环境探针会写入 lightProbes
数组,而 Vector4
(1,1,1,1) 会写入 occlusionProbes
数组。\
如果 positions
为 null
,则会引发 ArgumentNullException。\
可以通过将 null
传递给函数来省略 lightProbes
或 occlusionProbes
数组,但是无法同时省略两者。如果同时省略这两个数组,则会抛出 ArgumentException。lightProbes
和 occlusionProbes
应一起计算,这样可提高性能。\
对于采用数组作为参数的重载,lightProbes
和 occlusionProbes
必须至少具有与 positions
数组相同数量的元素。\
对于采用列表作为参数的重载,如果给定列表中没有足够空间,则输出列表会调整大小以适应 positions
数组的大小。\
返回的探针可以进一步用于实例化渲染,具体方法是通过 MaterialPropertyBlock.CopySHCoefficientArraysFrom 和 MaterialPropertyBlock.CopyProbeOcclusionArrayFrom 将它们复制到 MaterialPropertyBlock 对象。
using UnityEngine;
// This script uses OnPreCull for the rendering. It is mandatory to put the script to a Camera object. // Make sure light probes are placed and baked in the Scene. // Use Shadowmask mode and mixed lights to see occlusion probes approximating shadowness. [RequireComponent(typeof(Camera))] public class Simple : MonoBehaviour { public Material material;
private Matrix4x4[] transforms; private MaterialPropertyBlock properties; private Mesh cubeMesh;
void Start() { const int kCount = 100;
// Generate 100 random positions var positions = new Vector3[kCount]; for (int i = 0; i < kCount; ++i) positions[i] = new Vector3(Random.Range(-20.0f, 20.0f), Random.Range(-20.0f, 20.0f), Random.Range(-20.0f, 20.0f));
// Calculate probes at these positions var lightprobes = new UnityEngine.Rendering.SphericalHarmonicsL2[kCount]; var occlusionprobes = new Vector4[kCount]; LightProbes.CalculateInterpolatedLightAndOcclusionProbes(positions, lightprobes, occlusionprobes);
// Put them into the MPB properties = new MaterialPropertyBlock(); properties.CopySHCoefficientArraysFrom(lightprobes); properties.CopyProbeOcclusionArrayFrom(occlusionprobes);
// Compute the transforms list transforms = new Matrix4x4[kCount]; for (int i = 0; i < kCount; ++i) transforms[i] = Matrix4x4.Translate(positions[i]);
// Create the cube mesh cubeMesh = GameObject.CreatePrimitive(PrimitiveType.Cube).GetComponent<MeshFilter>().sharedMesh;
// Make sure the material property is assigned if (material == null || !material.enableInstancing) Debug.LogError("material must be assigned with one with instancing enabled."); }
// OnPreCull happens before every culling, which is the perfect timing to inject DrawMesh* function calls. void OnPreCull() { if (material != null && material.enableInstancing) { RenderParams rp = new RenderParams(material) { matProps = properties, lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.CustomProvided, // enable instancing for probes shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On, receiveShadows = true }; Graphics.RenderMeshInstanced(rp, cubeMesh, 0, transforms); } } }
该示例说明如何利用烘焙光照探针来增强实例化渲染的视觉品质。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.