public void OnEnable( ) { Lightmapping.RequestLightsDelegate testDel = (Light [] requests, Unity.Collections.NativeArray <LightDataGI> lightsOutput) => { DirectionalLight dLight = new DirectionalLight( ); PointLight point = new PointLight( ); SpotLight spot = new SpotLight( ); RectangleLight rect = new RectangleLight( ); LightDataGI ld = new LightDataGI( ); for (int i = 0; i < requests.Length; i++) { Light l = requests [i]; switch (l.type) { case UnityEngine.LightType.Directional: LightmapperUtils.Extract(l, ref dLight); ld.Init(ref dLight); break; case UnityEngine.LightType.Point: LightmapperUtils.Extract(l, ref point); ld.Init(ref point); break; case UnityEngine.LightType.Spot: LightmapperUtils.Extract(l, ref spot); ld.Init(ref spot); break; case UnityEngine.LightType.Area: LightmapperUtils.Extract(l, ref rect); ld.Init(ref rect); break; default: ld.InitNoBake(l.GetInstanceID( )); break; } ld.falloff = FalloffType.InverseSquared; lightsOutput [i] = ld; } }; Lightmapping.SetDelegate(testDel); }
// Return true if the light must be added to the baking public static bool LightDataGIExtract(Light l, ref LightDataGI ld) { var add = l.GetComponent <HDAdditionalLightData>(); if (add == null) { add = HDUtils.s_DefaultHDAdditionalLightData; } // TODO: Only take into account the light dimmer when we have real time GI. ld.instanceID = l.GetInstanceID(); ld.color = add.affectDiffuse ? LinearColor.Convert(l.color, l.intensity) : LinearColor.Black(); ld.indirectColor = add.affectDiffuse ? LightmapperUtils.ExtractIndirect(l) : LinearColor.Black(); // Note that the HDRI is correctly integrated in the GlobalIllumination system, we don't need to do anything regarding it. ld.mode = LightmapperUtils.Extract(l.lightmapBakeType); ld.shadow = (byte)(l.shadows != LightShadows.None ? 1 : 0); if (add.lightTypeExtent == LightTypeExtent.Punctual) { // For HDRP we need to divide the analytic light color by PI (HDRP do explicit PI division for Lambert, but built in Unity and the GI don't for punctual lights) // We apply it on both direct and indirect are they are separated, seems that direct is no used if we used mixed mode with indirect or shadowmask bake. ld.color.intensity /= Mathf.PI; ld.indirectColor.intensity /= Mathf.PI; switch (l.type) { case LightType.Directional: ld.orientation.SetLookRotation(l.transform.forward, Vector3.up); ld.position = Vector3.zero; ld.range = 0.0f; ld.coneAngle = 0.0f; ld.innerConeAngle = 0.0f; #if UNITY_EDITOR ld.shape0 = l.shadows != LightShadows.None ? (Mathf.Deg2Rad * l.shadowAngle) : 0.0f; #else ld.shape0 = 0.0f; #endif ld.shape1 = 0.0f; ld.type = UnityEngine.Experimental.GlobalIllumination.LightType.Directional; ld.falloff = FalloffType.Undefined; break; case LightType.Spot: ld.orientation = l.transform.rotation; ld.position = l.transform.position; ld.range = l.range; ld.coneAngle = l.spotAngle * Mathf.Deg2Rad; // coneAngle is the full angle ld.innerConeAngle = l.spotAngle * Mathf.Deg2Rad * add.GetInnerSpotPercent01(); #if UNITY_EDITOR ld.shape0 = l.shadows != LightShadows.None ? l.shadowRadius : 0.0f; #else ld.shape0 = 0.0f; #endif ld.shape1 = 0.0f; ld.type = UnityEngine.Experimental.GlobalIllumination.LightType.Spot; ld.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; /* * switch (add.spotLightShape) * { * case SpotLightShape.Cone: * break; * case SpotLightShape.Pyramid: * break; * case SpotLightShape.Box: * break; * default: * Debug.Assert(false, "Encountered an unknown SpotLightShape."); * break; * } */ break; case LightType.Point: ld.orientation = Quaternion.identity; ld.position = l.transform.position; ld.range = l.range; ld.coneAngle = 0.0f; ld.innerConeAngle = 0.0f; #if UNITY_EDITOR ld.shape0 = l.shadows != LightShadows.None ? l.shadowRadius : 0.0f; #else ld.shape0 = 0.0f; #endif ld.shape1 = 0.0f; ld.type = UnityEngine.Experimental.GlobalIllumination.LightType.Point; ld.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; break; // Note: We don't support this type in HDRP, but ini just in case case LightType.Area: ld.orientation = l.transform.rotation; ld.position = l.transform.position; ld.range = l.range; ld.coneAngle = 0.0f; ld.innerConeAngle = 0.0f; #if UNITY_EDITOR ld.shape0 = l.areaSize.x; ld.shape1 = l.areaSize.y; #else ld.shape0 = 0.0f; ld.shape1 = 0.0f; #endif ld.type = UnityEngine.Experimental.GlobalIllumination.LightType.Rectangle; ld.falloff = FalloffType.Undefined; break; default: Debug.Assert(false, "Encountered an unknown LightType."); break; } } else if (add.lightTypeExtent == LightTypeExtent.Rectangle) { ld.orientation = l.transform.rotation; ld.position = l.transform.position; ld.range = l.range; ld.coneAngle = 0.0f; ld.innerConeAngle = 0.0f; #if UNITY_EDITOR ld.shape0 = l.areaSize.x; ld.shape1 = l.areaSize.y; #else ld.shape0 = 0.0f; ld.shape1 = 0.0f; #endif // TEMP: for now, if we bake a rectangle type this will disable the light for runtime, need to speak with GI team about it! ld.type = UnityEngine.Experimental.GlobalIllumination.LightType.Rectangle; ld.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; } else if (add.lightTypeExtent == LightTypeExtent.Line) { ld.InitNoBake(ld.instanceID); } else { Debug.Assert(false, "Encountered an unknown LightType."); } return(true); }
// Return true if the light must be added to the baking public static bool LightDataGIExtract(Light l, ref LightDataGI ld) { var add = l.GetComponent <HDAdditionalLightData>(); if (add == null) { add = HDUtils.s_DefaultHDAdditionalLightData; } // TODO: Currently color temperature is not handled at runtime, need to expose useColorTemperature publicly Color cct = new Color(1.0f, 1.0f, 1.0f); #if UNITY_EDITOR if (add.useColorTemperature) { cct = Mathf.CorrelatedColorTemperatureToRGB(l.colorTemperature); } #endif // TODO: Only take into account the light dimmer when we have real time GI. ld.instanceID = l.GetInstanceID(); LinearColor directColor, indirectColor; directColor = add.affectDiffuse ? LinearColor.Convert(l.color, l.intensity) : LinearColor.Black(); directColor.red *= cct.r; directColor.green *= cct.g; directColor.blue *= cct.b; indirectColor = add.affectDiffuse ? LightmapperUtils.ExtractIndirect(l) : LinearColor.Black(); indirectColor.red *= cct.r; indirectColor.green *= cct.g; indirectColor.blue *= cct.b; #if UNITY_EDITOR LightMode lightMode = LightmapperUtils.Extract(l.lightmapBakeType); #else LightMode lightMode = LightmapperUtils.Extract(l.bakingOutput.lightmapBakeType); #endif ld.color = directColor; ld.indirectColor = indirectColor; // Note that the HDRI is correctly integrated in the GlobalIllumination system, we don't need to do anything regarding it. // The difference is that `l.lightmapBakeType` is the intent, e.g.you want a mixed light with shadowmask. But then the overlap test might detect more than 4 overlapping volumes and force a light to fallback to baked. // In that case `l.bakingOutput.lightmapBakeType` would be baked, instead of mixed, whereas `l.lightmapBakeType` would still be mixed. But this difference is only relevant in editor builds #if UNITY_EDITOR ld.mode = LightmapperUtils.Extract(l.lightmapBakeType); #else ld.mode = LightmapperUtils.Extract(l.bakingOutput.lightmapBakeType); #endif ld.shadow = (byte)(l.shadows != LightShadows.None ? 1 : 0); if (add.lightTypeExtent == LightTypeExtent.Punctual) { // For HDRP we need to divide the analytic light color by PI (HDRP do explicit PI division for Lambert, but built in Unity and the GI don't for punctual lights) // We apply it on both direct and indirect are they are separated, seems that direct is no used if we used mixed mode with indirect or shadowmask bake. ld.color.intensity /= Mathf.PI; ld.indirectColor.intensity /= Mathf.PI; directColor.intensity /= Mathf.PI; indirectColor.intensity /= Mathf.PI; switch (l.type) { case LightType.Directional: ld.orientation.SetLookRotation(l.transform.forward, Vector3.up); ld.position = Vector3.zero; ld.range = 0.0f; ld.coneAngle = 0.0f; ld.innerConeAngle = 0.0f; #if UNITY_EDITOR ld.shape0 = l.shadows != LightShadows.None ? (Mathf.Deg2Rad * l.shadowAngle) : 0.0f; #else ld.shape0 = 0.0f; #endif ld.shape1 = 0.0f; ld.type = UnityEngine.Experimental.GlobalIllumination.LightType.Directional; ld.falloff = FalloffType.Undefined; break; case LightType.Spot: switch (add.spotLightShape) { case SpotLightShape.Cone: { SpotLight spot; spot.instanceID = l.GetInstanceID(); spot.shadow = l.shadows != LightShadows.None; spot.mode = lightMode; #if UNITY_EDITOR spot.sphereRadius = l.shadows != LightShadows.None ? l.shadowRadius : 0.0f; #else spot.sphereRadius = 0.0f; #endif spot.position = l.transform.position; spot.orientation = l.transform.rotation; spot.color = directColor; spot.indirectColor = indirectColor; spot.range = l.range; spot.coneAngle = l.spotAngle * Mathf.Deg2Rad; spot.innerConeAngle = l.spotAngle * Mathf.Deg2Rad * add.innerSpotPercent01; spot.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; spot.angularFalloff = AngularFalloffType.AnalyticAndInnerAngle; ld.Init(ref spot); ld.shape1 = (float)AngularFalloffType.AnalyticAndInnerAngle; } break; case SpotLightShape.Pyramid: { SpotLightPyramidShape pyramid; pyramid.instanceID = l.GetInstanceID(); pyramid.shadow = l.shadows != LightShadows.None; pyramid.mode = lightMode; pyramid.position = l.transform.position; pyramid.orientation = l.transform.rotation; pyramid.color = directColor; pyramid.indirectColor = indirectColor; pyramid.range = l.range; pyramid.angle = l.spotAngle * Mathf.Deg2Rad; pyramid.aspectRatio = add.aspectRatio; pyramid.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; ld.Init(ref pyramid); } break; case SpotLightShape.Box: { SpotLightBoxShape box; box.instanceID = l.GetInstanceID(); box.shadow = l.shadows != LightShadows.None; box.mode = lightMode; box.position = l.transform.position; box.orientation = l.transform.rotation; box.color = directColor; box.indirectColor = indirectColor; box.range = l.range; box.width = add.shapeWidth; box.height = add.shapeHeight; ld.Init(ref box); } break; default: Debug.Assert(false, "Encountered an unknown SpotLightShape."); break; } break; case LightType.Point: ld.orientation = Quaternion.identity; ld.position = l.transform.position; ld.range = l.range; ld.coneAngle = 0.0f; ld.innerConeAngle = 0.0f; #if UNITY_EDITOR ld.shape0 = l.shadows != LightShadows.None ? l.shadowRadius : 0.0f; #else ld.shape0 = 0.0f; #endif ld.shape1 = 0.0f; ld.type = UnityEngine.Experimental.GlobalIllumination.LightType.Point; ld.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; break; // Note: We don't support this type in HDRP, but ini just in case case LightType.Rectangle: ld.orientation = l.transform.rotation; ld.position = l.transform.position; ld.range = l.range; ld.coneAngle = 0.0f; ld.innerConeAngle = 0.0f; #if UNITY_EDITOR ld.shape0 = l.areaSize.x; ld.shape1 = l.areaSize.y; #else ld.shape0 = 0.0f; ld.shape1 = 0.0f; #endif ld.type = UnityEngine.Experimental.GlobalIllumination.LightType.Rectangle; ld.falloff = FalloffType.Undefined; break; default: Debug.Assert(false, "Encountered an unknown LightType."); break; } } else if (add.lightTypeExtent == LightTypeExtent.Rectangle) { ld.orientation = l.transform.rotation; ld.position = l.transform.position; ld.range = l.range; ld.coneAngle = 0.0f; ld.innerConeAngle = 0.0f; #if UNITY_EDITOR ld.shape0 = l.areaSize.x; ld.shape1 = l.areaSize.y; #else ld.shape0 = 0.0f; ld.shape1 = 0.0f; #endif // TEMP: for now, if we bake a rectangle type this will disable the light for runtime, need to speak with GI team about it! ld.type = UnityEngine.Experimental.GlobalIllumination.LightType.Rectangle; ld.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; } else if (add.lightTypeExtent == LightTypeExtent.Tube) { ld.InitNoBake(ld.instanceID); } else { Debug.Assert(false, "Encountered an unknown LightType."); } return(true); }
// Return true if the light must be added to the baking public static bool LightDataGIExtract(Light light, ref LightDataGI lightDataGI) { var add = light.GetComponent <HDAdditionalLightData>(); if (add == null) { add = HDUtils.s_DefaultHDAdditionalLightData; } Cookie cookie; LightmapperUtils.Extract(light, out cookie); lightDataGI.cookieID = cookie.instanceID; lightDataGI.cookieScale = cookie.scale; Color cct = new Color(1.0f, 1.0f, 1.0f); if (add.useColorTemperature) { cct = Mathf.CorrelatedColorTemperatureToRGB(light.colorTemperature); } #if UNITY_EDITOR LightMode lightMode = LightmapperUtils.Extract(light.lightmapBakeType); #else LightMode lightMode = LightmapperUtils.Extract(light.bakingOutput.lightmapBakeType); #endif float lightDimmer = 1; if (lightMode == LightMode.Realtime && add.affectDiffuse) { lightDimmer = add.lightDimmer; } lightDataGI.instanceID = light.GetInstanceID(); LinearColor directColor, indirectColor; directColor = add.affectDiffuse ? LinearColor.Convert(light.color, light.intensity) : LinearColor.Black(); directColor.red *= cct.r; directColor.green *= cct.g; directColor.blue *= cct.b; directColor.intensity *= lightDimmer; indirectColor = add.affectDiffuse ? LightmapperUtils.ExtractIndirect(light) : LinearColor.Black(); indirectColor.red *= cct.r; indirectColor.green *= cct.g; indirectColor.blue *= cct.b; indirectColor.intensity *= lightDimmer; lightDataGI.color = directColor; lightDataGI.indirectColor = indirectColor; // Note that the HDRI is correctly integrated in the GlobalIllumination system, we don't need to do anything regarding it. // The difference is that `l.lightmapBakeType` is the intent, e.g.you want a mixed light with shadowmask. But then the overlap test might detect more than 4 overlapping volumes and force a light to fallback to baked. // In that case `l.bakingOutput.lightmapBakeType` would be baked, instead of mixed, whereas `l.lightmapBakeType` would still be mixed. But this difference is only relevant in editor builds #if UNITY_EDITOR lightDataGI.mode = LightmapperUtils.Extract(light.lightmapBakeType); #else lightDataGI.mode = LightmapperUtils.Extract(light.bakingOutput.lightmapBakeType); #endif lightDataGI.shadow = (byte)(light.shadows != LightShadows.None ? 1 : 0); HDLightType lightType = add.ComputeLightType(light); if (lightType != HDLightType.Area) { // For HDRP we need to divide the analytic light color by PI (HDRP do explicit PI division for Lambert, but built in Unity and the GI don't for punctual lights) // We apply it on both direct and indirect are they are separated, seems that direct is no used if we used mixed mode with indirect or shadowmask bake. lightDataGI.color.intensity /= Mathf.PI; lightDataGI.indirectColor.intensity /= Mathf.PI; directColor.intensity /= Mathf.PI; indirectColor.intensity /= Mathf.PI; } switch (lightType) { case HDLightType.Directional: lightDataGI.orientation = light.transform.rotation; lightDataGI.position = light.transform.position; lightDataGI.range = 0.0f; lightDataGI.coneAngle = add.shapeWidth; lightDataGI.innerConeAngle = add.shapeHeight; #if UNITY_EDITOR lightDataGI.shape0 = light.shadows != LightShadows.None ? (Mathf.Deg2Rad * light.shadowAngle) : 0.0f; #else lightDataGI.shape0 = 0.0f; #endif lightDataGI.shape1 = 0.0f; lightDataGI.type = UnityEngine.Experimental.GlobalIllumination.LightType.Directional; lightDataGI.falloff = FalloffType.Undefined; lightDataGI.coneAngle = add.shapeWidth; lightDataGI.innerConeAngle = add.shapeHeight; break; case HDLightType.Spot: switch (add.spotLightShape) { case SpotLightShape.Cone: { SpotLight spot; spot.instanceID = light.GetInstanceID(); spot.shadow = light.shadows != LightShadows.None; spot.mode = lightMode; #if UNITY_EDITOR spot.sphereRadius = light.shadows != LightShadows.None ? light.shadowRadius : 0.0f; #else spot.sphereRadius = 0.0f; #endif spot.position = light.transform.position; spot.orientation = light.transform.rotation; spot.color = directColor; spot.indirectColor = indirectColor; spot.range = light.range; spot.coneAngle = light.spotAngle * Mathf.Deg2Rad; spot.innerConeAngle = light.spotAngle * Mathf.Deg2Rad * add.innerSpotPercent01; spot.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; spot.angularFalloff = AngularFalloffType.AnalyticAndInnerAngle; lightDataGI.Init(ref spot, ref cookie); lightDataGI.shape1 = (float)AngularFalloffType.AnalyticAndInnerAngle; if (light.cookie != null) { lightDataGI.cookieID = light.cookie.GetInstanceID(); } else if (add.IESSpot != null) { lightDataGI.cookieID = add.IESSpot.GetInstanceID(); } else { lightDataGI.cookieID = 0; } } break; case SpotLightShape.Pyramid: { SpotLightPyramidShape pyramid; pyramid.instanceID = light.GetInstanceID(); pyramid.shadow = light.shadows != LightShadows.None; pyramid.mode = lightMode; pyramid.position = light.transform.position; pyramid.orientation = light.transform.rotation; pyramid.color = directColor; pyramid.indirectColor = indirectColor; pyramid.range = light.range; pyramid.angle = light.spotAngle * Mathf.Deg2Rad; pyramid.aspectRatio = add.aspectRatio; pyramid.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; lightDataGI.Init(ref pyramid, ref cookie); if (light.cookie != null) { lightDataGI.cookieID = light.cookie.GetInstanceID(); } else if (add.IESSpot != null) { lightDataGI.cookieID = add.IESSpot.GetInstanceID(); } else { lightDataGI.cookieID = 0; } } break; case SpotLightShape.Box: { SpotLightBoxShape box; box.instanceID = light.GetInstanceID(); box.shadow = light.shadows != LightShadows.None; box.mode = lightMode; box.position = light.transform.position; box.orientation = light.transform.rotation; box.color = directColor; box.indirectColor = indirectColor; box.range = light.range; box.width = add.shapeWidth; box.height = add.shapeHeight; lightDataGI.Init(ref box, ref cookie); if (light.cookie != null) { lightDataGI.cookieID = light.cookie.GetInstanceID(); } else if (add.IESSpot != null) { lightDataGI.cookieID = add.IESSpot.GetInstanceID(); } else { lightDataGI.cookieID = 0; } } break; default: Debug.Assert(false, "Encountered an unknown SpotLightShape."); break; } break; case HDLightType.Point: lightDataGI.orientation = light.transform.rotation; lightDataGI.position = light.transform.position; lightDataGI.range = light.range; lightDataGI.coneAngle = 0.0f; lightDataGI.innerConeAngle = 0.0f; #if UNITY_EDITOR lightDataGI.shape0 = light.shadows != LightShadows.None ? light.shadowRadius : 0.0f; #else lightDataGI.shape0 = 0.0f; #endif lightDataGI.shape1 = 0.0f; lightDataGI.type = UnityEngine.Experimental.GlobalIllumination.LightType.Point; lightDataGI.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; break; case HDLightType.Area: switch (add.areaLightShape) { case AreaLightShape.Rectangle: lightDataGI.orientation = light.transform.rotation; lightDataGI.position = light.transform.position; lightDataGI.range = light.range; lightDataGI.coneAngle = 0.0f; lightDataGI.innerConeAngle = 0.0f; lightDataGI.shape0 = add.shapeWidth; lightDataGI.shape1 = add.shapeHeight; // TEMP: for now, if we bake a rectangle type this will disable the light for runtime, need to speak with GI team about it! lightDataGI.type = UnityEngine.Experimental.GlobalIllumination.LightType.Rectangle; lightDataGI.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; if (add.areaLightCookie != null) { lightDataGI.cookieID = add.areaLightCookie.GetInstanceID(); } else if (add.IESSpot != null) { lightDataGI.cookieID = add.IESSpot.GetInstanceID(); } else { lightDataGI.cookieID = 0; } break; case AreaLightShape.Tube: lightDataGI.InitNoBake(lightDataGI.instanceID); break; case AreaLightShape.Disc: lightDataGI.orientation = light.transform.rotation; lightDataGI.position = light.transform.position; lightDataGI.range = light.range; lightDataGI.coneAngle = 0.0f; lightDataGI.innerConeAngle = 0.0f; #if UNITY_EDITOR lightDataGI.shape0 = light.areaSize.x; lightDataGI.shape1 = light.areaSize.y; #else lightDataGI.shape0 = 0.0f; lightDataGI.shape1 = 0.0f; #endif // TEMP: for now, if we bake a rectangle type this will disable the light for runtime, need to speak with GI team about it! lightDataGI.type = UnityEngine.Experimental.GlobalIllumination.LightType.Disc; lightDataGI.falloff = add.applyRangeAttenuation ? FalloffType.InverseSquared : FalloffType.InverseSquaredNoRangeAttenuation; lightDataGI.cookieID = add.areaLightCookie ? add.areaLightCookie.GetInstanceID() : 0; break; default: Debug.Assert(false, "Encountered an unknown AreaLightShape."); break; } break; default: Debug.Assert(false, "Encountered an unknown LightType."); break; } return(true); }