/// <summary> /// Converts a Matrix4x4 to MatrixFloats format /// </summary> /// <param name="matrix">The matrix to be converted</param> /// <returns>The matrix converted into the MatrixFloats format</returns> public static MatrixFloats ToMatrixFloats(Matrix4x4 matrix) { MatrixFloats matrixFloats = new MatrixFloats { a = matrix.GetColumn(0), b = matrix.GetColumn(1), c = matrix.GetColumn(2), d = matrix.GetColumn(3) }; return(matrixFloats); }
/// <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)); }
/// <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 color = (overrideColor ? overridingColor : _lightComponent.color) * _lightComponent.intensity * strength; switch (Type) { case LightType.Directional: { _directionalLightParameters.color = color; _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.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(customDistanceFalloffThreshold, 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.lightPosition = _lightComponent.transform.position; _pointLightParameters.lightRange = _lightComponent.range; _pointLightParameters.distanceFalloffParameters = new Vector2(customDistanceFalloffThreshold, 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); _storePointLightShadowMapMaterial.EnableKeyword("SHADOWS_CUBE"); _storePointLightShadowMapMaterial.EnableKeyword("POINT"); _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> /// 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 VolumeTypeEnum.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 VolumeTypeEnum.Cone: { _volumeData.xPositiveFade = volumeShape.fading.angularConeFade; _volumeData.zPositiveFade = volumeShape.fading.distanceConeFade; } break; case VolumeTypeEnum.Cylinder: { _volumeData.xPositiveFade = volumeShape.fading.widthCylinderFade; _volumeData.yPositiveFade = volumeShape.fading.yPositiveCylinderFade; _volumeData.yNegativeFade = volumeShape.fading.yNegativeCylinderFade; } break; case VolumeTypeEnum.Planar: { _volumeData.yPositiveFade = volumeShape.fading.heightPlaneFade; } break; case VolumeTypeEnum.Sphere: { _volumeData.xPositiveFade = volumeShape.fading.distanceSphereFade; } break; } if (textureMask.enable != _previousTextureMaskState) { RaiseTextureMaskStateChangedEvent(); } if (textureMask.texture != _previousTextureMask) { RaiseTextureMaskChangedEvent(); } if (textureMask.enable) { Matrix4x4 localMatrix = textureMask.transform.Matrix; _volumeData.textureData.transform = MatrixFloats.ToMatrixFloats(textureMask.transform.space == Space.World ? localMatrix * transform.localToWorldMatrix : localMatrix); _volumeData.textureData.wrapMode = textureMask.wrapMode == VolumetricTextureWrapModeEnum.SameAsSource ? (textureMask.texture.wrapMode == TextureWrapMode.Clamp ? 0 : 1) : (int)textureMask.wrapMode; _volumeData.textureData.filterMode = textureMask.filterMode == VolumetricTextureFilterModeEnum.SameAsSource ? (textureMask.texture.filterMode == FilterMode.Point ? 0 : 1) : (int)textureMask.filterMode; _volumeData.textureData.clipOnAlpha = textureMask.clipComputationBasedOnAlpha ? 1 : 0; _volumeData.textureData.clippingThreshold = textureMask.clippingThreshold; } _volumeData.textureData.index = textureMask.textureIndex; _volumeData.noiseData.enable = noiseMask.enable ? 1 : 0; if (noiseMask.enable) { Matrix4x4 localMatrix = noiseMask.transform.Matrix; _volumeData.noiseData.transform = MatrixFloats.ToMatrixFloats(noiseMask.transform.space == Space.World ? localMatrix * transform.localToWorldMatrix : localMatrix); _volumeData.noiseData.speed = noiseMask.speed; _volumeData.noiseData.offset = noiseMask.offset; } _volumeData.injectDensity = density.injectionParameters.enable ? 1 : 0; _volumeData.densityValue = density.injectionParameters.strength; _volumeData.densityTextureLevelsParameters = density.injectionParameters.textureMaskLevelParameters.Data; _volumeData.densityNoiseLevelsParameters = density.injectionParameters.noiseMaskLevelParameters.Data; _volumeData.injectAnisotropy = anisotropy.injectionParameters.enable ? 1 : 0; _volumeData.anisotropyValue = anisotropy.injectionParameters.strength; _volumeData.anisotropyTextureLevelsParameters = anisotropy.injectionParameters.textureMaskLevelParameters.Data; _volumeData.anisotropyNoiseLevelsParameters = anisotropy.injectionParameters.noiseMaskLevelParameters.Data; _volumeData.injectColor = color.injectionParameters.enable ? 1 : 0; _volumeData.colorValue = new Vector3(color.color.r, color.color.g, color.color.b) * color.injectionParameters.strength; _volumeData.colorTextureLevelsParameters = color.injectionParameters.textureMaskLevelParameters.Data; _volumeData.colorNoiseLevelsParameters = color.injectionParameters.noiseMaskLevelParameters.Data; }