private void LoadLightData() { List <Light> allLights = new List <Light>(FindObjectsOfType <Light>()); LightData[] lightData; if (allLights.Count > 0) { lightData = new LightData[allLights.Count]; for (int i = 0; i < allLights.Count; i++) { Light l = allLights[i]; Vector3 radiance = new Vector3(l.color.r, l.color.g, l.color.b) * l.intensity * 2; radiance = Vector3.Scale(radiance, radiance); lightData[i] = new LightData() { position = l.type == LightType.Directional ? l.transform.forward : l.transform.position, isDirectional = l.type == LightType.Directional ? (byte)1 : (byte)0, radiance = radiance }; } rayMarchingShader.SetInt("numLights", lightData.Length); } else { lightData = new LightData[1]; rayMarchingShader.SetInt("numLights", 0); } ComputeBuffer lightBuffer = new ComputeBuffer(lightData.Length, LightData.GetSize()); lightBuffer.SetData(lightData); rayMarchingShader.SetBuffer(0, "lights", lightBuffer); buffersToDispose.Add(lightBuffer); }
void SetupLights() { List <Light> lights = new List <Light>(FindObjectsOfType <Light>()); LightData[] lightData = new LightData[lights.Count]; for (int i = 0; i < lights.Count; ++i) { var light = lights[i]; Vector3 intensity = new Vector3(light.color.r, light.color.g, light.color.b) * light.intensity; lightData[i] = new LightData() { position = light.type == LightType.Directional ? light.transform.forward : light.transform.position, intensity = intensity, isDirectional = light.type == LightType.Directional ? 1:0 }; } ComputeBuffer lightBuffer = new ComputeBuffer(lightData.Length, LightData.GetSize()); lightBuffer.SetData(lightData); raymarcherShader.SetBuffer(0, "lights", lightBuffer); raymarcherShader.SetInt("numLights", lightData.Length); buffers.Add(lightBuffer); }
void CreateScene() { List <Shape> allShapes = new List <Shape> (FindObjectsOfType <Shape> ()); allShapes.Sort((a, b) => a.operation.CompareTo(b.operation)); List <Shape> orderedShapes = new List <Shape> (); for (int i = 0; i < allShapes.Count; i++) { // Add top-level shapes (those without a parent) if (allShapes[i].transform.parent == null) { Transform parentShape = allShapes[i].transform; orderedShapes.Add(allShapes[i]); allShapes[i].numChildren = parentShape.childCount; // Add all children of the shape (nested children not supported currently) for (int j = 0; j < parentShape.childCount; j++) { if (parentShape.GetChild(j).GetComponent <Shape> () != null) { orderedShapes.Add(parentShape.GetChild(j).GetComponent <Shape> ()); orderedShapes[orderedShapes.Count - 1].numChildren = 0; } } } } ShapeData[] shapeData = new ShapeData[orderedShapes.Count]; for (int i = 0; i < orderedShapes.Count; i++) { var s = orderedShapes[i]; Vector3 col = new Vector3(s.colour.r, s.colour.g, s.colour.b); shapeData[i] = new ShapeData() { position = s.Position, scale = s.Scale, colour = col, shapeType = (int)s.shapeType, operation = (int)s.operation, blendStrength = s.blendStrength * 3, numChildren = s.numChildren }; } int kernelHandle = raymarching.FindKernel("CSMain"); ComputeBuffer shapeBuffer = new ComputeBuffer(shapeData.Length, ShapeData.GetSize()); shapeBuffer.SetData(shapeData); raymarching.SetBuffer(kernelHandle, "shapes", shapeBuffer); raymarching.SetInt("numShapes", shapeData.Length); buffersToDispose.Add(shapeBuffer); // do lights LightData[] lightData = new LightData[lightSources.Length]; // get positions of lights for (int i = 0; i < lightSources.Length; i++) { lightData[i] = new LightData() { position = lightSources[i].transform.position, pointLight = lightSources[i].type != LightType.Directional ? 1 : 0, scale = Vector3.one }; } ComputeBuffer lightBuffer = new ComputeBuffer(lightData.Length, LightData.GetSize()); lightBuffer.SetData(lightData); raymarching.SetBuffer(kernelHandle, "lights", lightBuffer); raymarching.SetInt("numLights", lightData.Length); buffersToDispose.Add(lightBuffer); // do depth texture // Get (update only if necessary, check for null) // RenderTexture depth = Shader.GetGlobalTexture( "_CameraDepthTexture" ) as RenderTexture; // Texture gBuffer2 = Shader.GetGlobalTexture( "_CameraGBufferTexture2" ); // Set // int kernelHandle = raymarching.FindKernel("CSMain"); // raymarching.SetTextureFromGlobal(kernelHandle, Shader.PropertyToID("_CameraDepthTexture"), Shader.PropertyToID("_CameraDepthTexture")); // raymarching.SetTexture( 0, "_CameraGBufferTexture2", gBuffer2 ); // raymarching.SetTextureFromGlobal(0, "_DepthTexture", "_CameraDepthTexture"); }