/// <summary> /// Packs the data /// </summary> private void PackData() { _volumeData.transform = MatrixFloats.ToMatrixFloats(transform.worldToLocalMatrix); _volumeData.shape = (int)volumeShape.shape; _volumeData.falloffExponent = volumeShape.fading.falloffExponent; switch (volumeShape.shape) { case VolumeType.Box: { _volumeData.xPositiveFade = volumeShape.fading.xPositiveCubeFade; _volumeData.xNegativeFade = volumeShape.fading.xNegativeCubeFade; _volumeData.yPositiveFade = volumeShape.fading.yPositiveCubeFade; _volumeData.yNegativeFade = volumeShape.fading.yNegativeCubeFade; _volumeData.zPositiveFade = volumeShape.fading.zPositiveCubeFade; _volumeData.zNegativeFade = volumeShape.fading.zNegativeCubeFade; } break; case VolumeType.Cone: { _volumeData.xPositiveFade = volumeShape.fading.angularConeFade; _volumeData.zPositiveFade = volumeShape.fading.distanceConeFade; } break; case VolumeType.Cylinder: { _volumeData.xPositiveFade = volumeShape.fading.widthCylinderFade; _volumeData.yPositiveFade = volumeShape.fading.yPositiveCylinderFade; _volumeData.yNegativeFade = volumeShape.fading.yNegativeCylinderFade; } break; case VolumeType.Sphere: { _volumeData.xPositiveFade = volumeShape.fading.distanceSphereFade; } break; } _volumeData.useAsLightProbesProxyVolume = useAsLightProbesProxyVolume ? 1 : 0; _volumeData.lightProbesMultiplier = lightProbesMultiplier * Mathf.PI; if (UsesTexture2DMasking) { Matrix4x4 localMatrix = texture2DMask.transform.Matrix.inverse; _volumeData.texture2DMaskData.transform = MatrixFloats.ToMatrixFloats(noiseMask.transform.space == Space.Self ? localMatrix * transform.worldToLocalMatrix : localMatrix); } _volumeData.texture2DMaskData.index = texture2DMask.textureIndex; if (UsesTexture3DMasking) { Matrix4x4 localMatrix = texture3DMask.transform.Matrix.inverse; _volumeData.texture3DMaskData.transform = MatrixFloats.ToMatrixFloats(noiseMask.transform.space == Space.Self ? localMatrix * transform.worldToLocalMatrix : localMatrix); } _volumeData.texture3DMaskData.index = texture3DMask.textureIndex; _volumeData.noiseData.enable = noiseMask.enable ? 1 : 0; if (noiseMask.enable) { Matrix4x4 localMatrix = noiseMask.transform.Matrix.inverse; _volumeData.noiseData.transform = MatrixFloats.ToMatrixFloats(noiseMask.transform.space == Space.Self ? localMatrix * transform.worldToLocalMatrix : localMatrix); _volumeData.noiseData.speed = noiseMask.speed; } _volumeData.injectDensity = densityInjection.enable ? 1 : 0; _volumeData.densityValue = densityInjection.strength; _volumeData.densityNoiseLevelsParameters = densityInjection.useNoiseMask ? (densityInjection.useNoiseMaskLevels ? densityInjection.noiseMaskLevelParameters.Data : LevelsParameters.Default.Data) : LevelsParameters.One.Data; _volumeData.densityTexture2DMaskLevelsParameters = densityInjection.useTexture2DMask ? (densityInjection.useTexture2DMaskLevels ? densityInjection.texture2DMaskLevelParameters.Data : LevelsParameters.Default.Data) : LevelsParameters.One.Data; _volumeData.densityTexture3DMaskLevelsParameters = densityInjection.useTexture3DMask ? (densityInjection.useTexture3DMaskLevels ? densityInjection.texture3DMaskLevelParameters.Data : LevelsParameters.Default.Data) : LevelsParameters.One.Data; _volumeData.injectScattering = scatteringInjection.enable ? 1 : 0; _volumeData.scatteringValue = scatteringInjection.strength; _volumeData.scatteringNoiseLevelsParameters = scatteringInjection.useNoiseMask ? (scatteringInjection.useNoiseMaskLevels ? scatteringInjection.noiseMaskLevelParameters.Data : LevelsParameters.Default.Data) : LevelsParameters.One.Data; _volumeData.scatteringTexture2DMaskLevelsParameters = scatteringInjection.useTexture2DMask ? (scatteringInjection.useTexture2DMaskLevels ? scatteringInjection.texture2DMaskLevelParameters.Data : LevelsParameters.Default.Data) : LevelsParameters.One.Data; _volumeData.scatteringTexture3DMaskLevelsParameters = scatteringInjection.useTexture3DMask ? (scatteringInjection.useTexture3DMaskLevels ? scatteringInjection.texture3DMaskLevelParameters.Data : LevelsParameters.Default.Data) : LevelsParameters.One.Data; _volumeData.injectColor = lightInjection.injectionParameters.enable ? 1 : 0; _volumeData.colorValue = new Vector3(lightInjection.color.r, lightInjection.color.g, lightInjection.color.b) * lightInjection.injectionParameters.strength; _volumeData.colorNoiseLevelsParameters = lightInjection.injectionParameters.useNoiseMask ? (lightInjection.injectionParameters.useNoiseMaskLevels ? lightInjection.injectionParameters.noiseMaskLevelParameters.Data : LevelsParameters.Default.Data) : LevelsParameters.One.Data; _volumeData.colorTexture2DMaskLevelsParameters = lightInjection.injectionParameters.useTexture2DMask ? (lightInjection.injectionParameters.useTexture2DMaskLevels ? lightInjection.injectionParameters.texture2DMaskLevelParameters.Data : LevelsParameters.Default.Data) : LevelsParameters.One.Data; _volumeData.colorTexture3DMaskLevelsParameters = lightInjection.injectionParameters.useTexture3DMask ? (lightInjection.injectionParameters.useTexture3DMaskLevels ? lightInjection.injectionParameters.texture3DMaskLevelParameters.Data : LevelsParameters.Default.Data) : LevelsParameters.One.Data; }
/// <summary> /// Collects the light's data so it will be packed into a common compute buffer by the lights manager and sent to the compute shader /// </summary> /// <param name="camera"></param> private void PackParameters(Camera camera) { Vector4 colorTemperature = (LightHelpers.IsColorTemperatureAvailable && useColorTemperatureTint) ? (Vector4)Mathf.CorrelatedColorTemperatureToRGB(LightComponent.colorTemperature) : Vector4.one; Vector4 color = (overrideColor ? overridingColor : LightComponent.color * colorTemperature) * LightComponent.intensity * strength; int useDefaultScattering = useScattering == BooleanChoice.Default ? 1 : 0; float scatteringOverride = useScattering == BooleanChoice.False ? -2 : (overrideScattering ? 1.0f - overridingScattering : -1); switch (Type) { case LightType.Directional: { _directionalLightParameters.color = color; _directionalLightParameters.useDefaultScattering = useDefaultScattering; _directionalLightParameters.scatteringOverride = scatteringOverride; _directionalLightParameters.lightPosition = LightComponent.transform.position; _directionalLightParameters.lightDirection = LightComponent.transform.forward; Matrix4x4 lightToWorldMatrix = Matrix4x4.TRS(LightComponent.transform.position, LightComponent.transform.rotation, Vector3.one); _directionalLightParameters.worldToLightMatrix = MatrixFloats.ToMatrixFloats(lightToWorldMatrix.inverse); _directionalLightParameters.lightToWorldMatrix = MatrixFloats.ToMatrixFloats(lightToWorldMatrix); _directionalLightParameters.shadowMapIndex = CastsShadows ? _shadowMapIndex : -1; _directionalLightParameters.cookieMapIndex = -1; if (CastsCookie) { _directionalLightParameters.cookieMapIndex = _cookieMapIndex; _directionalLightParameters.cookieParameters.x = LightComponent.cookieSize; _directionalLightParameters.cookieParameters.y = LightComponent.cookie.wrapMode == TextureWrapMode.Repeat ? 0 : 1; } _directionalLightParameters.enableOutOfPhaseColor = enableOutOfPhaseColor ? 1 : 0; _directionalLightParameters.outOfPhaseColor = (Vector4)outOfPhaseColor * outOfPhaseColorStrength; } break; case LightType.Spot: { _spotLightParameters.color = color; _spotLightParameters.useDefaultScattering = useDefaultScattering; _spotLightParameters.scatteringOverride = scatteringOverride; _spotLightParameters.lightPosition = LightComponent.transform.position; _spotLightParameters.lightDirection = LightComponent.transform.forward; _spotLightParameters.lightRange = LightComponent.range; _spotLightParameters.lightCosHalfAngle = Mathf.Cos(LightComponent.spotAngle * 0.5f * Mathf.Deg2Rad); _spotLightParameters.angularFalloffParameters = new Vector2(customAngularFalloffThreshold, customAngularFalloffPower); _spotLightParameters.distanceFalloffParameters = new Vector2(Mathf.Min(customDistanceFalloffThreshold, 0.999999f), customDistanceFalloffPower); _spotLightParameters.shadowMapIndex = -1; if (CastsShadows) { Matrix4x4 worldToLight = Matrix4x4.TRS(LightComponent.transform.position, LightComponent.transform.rotation, Vector3.one).inverse; Matrix4x4 proj = Matrix4x4.Perspective(LightComponent.spotAngle, 1, LightComponent.shadowNearPlane, LightComponent.range); Matrix4x4 clip = Matrix4x4.TRS(Vector3.one * 0.5f, Quaternion.identity, Vector3.one * 0.5f); Matrix4x4 m = clip * proj; m[0, 2] *= -1; m[1, 2] *= -1; m[2, 2] *= -1; m[3, 2] *= -1; Matrix4x4 worldToShadow = m * worldToLight; _spotLightParameters.worldToShadowMatrix = MatrixFloats.ToMatrixFloats(worldToShadow); _spotLightParameters.shadowMapIndex = _shadowMapIndex; _spotLightParameters.shadowStrength = 1.0f - LightComponent.shadowStrength; } _spotLightParameters.cookieMapIndex = -1; if (CastsCookie) { _spotLightParameters.cookieMapIndex = _cookieMapIndex; _spotLightParameters.cookieParameters.x = customCookieDistanceFalloffStartThreshold; _spotLightParameters.cookieParameters.y = customCookieDistanceFalloffEndThreshold; _spotLightParameters.cookieParameters.z = customCookieDistanceFalloffPower; } } break; case LightType.Point: { _pointLightParameters.color = color; _pointLightParameters.useDefaultScattering = useDefaultScattering; _pointLightParameters.scatteringOverride = scatteringOverride; _pointLightParameters.lightPosition = LightComponent.transform.position; _pointLightParameters.lightRange = LightComponent.range; _pointLightParameters.distanceFalloffParameters = new Vector2(Mathf.Min(customDistanceFalloffThreshold, 0.999999f), customDistanceFalloffPower); _pointLightParameters.shadowMapIndex = -1; if (CastsShadows) { Matrix4x4 worldMatrix = Matrix4x4.TRS(camera.transform.position, transform.rotation, Vector3.one * camera.nearClipPlane * 2); _storePointLightShadowMapMaterial.SetMatrix("_WorldViewProj", GL.GetGPUProjectionMatrix(camera.projectionMatrix, true) * camera.worldToCameraMatrix * worldMatrix); _copyShadowmapCommandBuffer.SetGlobalTexture("_ShadowMapTexture", BuiltinRenderTextureType.CurrentActive); _copyShadowmapCommandBuffer.SetRenderTarget(shadowMapRenderTexture); _copyShadowmapCommandBuffer.DrawMesh(_storePointLightShadowMapMesh, worldMatrix, _storePointLightShadowMapMaterial, 0); Matrix4x4 worldToShadow = Matrix4x4.TRS(LightComponent.transform.position, LightComponent.transform.rotation, Vector3.one * LightComponent.range).inverse; _pointLightParameters.worldToShadowMatrix = MatrixFloats.ToMatrixFloats(worldToShadow); #if UNITY_2017_3_OR_NEWER _pointLightParameters.lightProjectionParameters = new Vector2(LightComponent.range / (LightComponent.shadowNearPlane - LightComponent.range), (LightComponent.shadowNearPlane * LightComponent.range) / (LightComponent.shadowNearPlane - LightComponent.range)); // From UnityShaderVariables.cginc:114 #endif _pointLightParameters.shadowMapIndex = _shadowMapIndex; _pointLightParameters.shadowStrength = 1.0f - LightComponent.shadowStrength; } _pointLightParameters.cookieMapIndex = -1; if (CastsCookie) { _pointLightParameters.cookieMapIndex = _cookieMapIndex; _pointLightParameters.cookieParameters.x = customCookieDistanceFalloffStartThreshold; _pointLightParameters.cookieParameters.y = customCookieDistanceFalloffEndThreshold; _pointLightParameters.cookieParameters.z = customCookieDistanceFalloffPower; } } break; } }
/// <summary> /// Converts the matrix to the MatrixFloats format /// </summary> /// <returns>The converted MatrixFloats</returns> public static MatrixFloats ToAuraMatrixFloats(this Matrix4x4 matrix) { return(MatrixFloats.ToMatrixFloats(matrix)); }