Beispiel #1
0
        /// <summary>
        /// Updates the preview texture for this editor.
        /// </summary>
        /// <param name="regenerateShader">
        /// If true, regenerates the graph's shader, which takes a lot longer.
        /// This should be true whenever the graph itself changes.
        /// </param>
        private void UpdatePreview(bool regenerateShader = true)
        {
            //Update params.
            var oldParams = graphParams;

            graphParams = new GPUGraph.GraphParamCollection(graph);
            graphParams.SetParams(oldParams);

            //Create shader.
            if (regenerateShader || cachedPreviewMat == null)
            {
                string shaderText = graph.GenerateShader("Graph editor temp shader", "rgb", 1.0f);
                Shader shader     = ShaderUtil.CreateShaderAsset(shaderText);

                if (shader == null)
                {
                    Debug.LogError("Shader: " + shaderText);
                }
                else
                {
                    cachedPreviewMat = new Material(shader);
                }
            }

            //Create texture.
            if (previewNoise == null)
            {
                previewNoise = new Texture2D(256, 256, TextureFormat.RGBA32, false);
            }

            //Set params and generate.
            graphParams.SetParams(cachedPreviewMat);
            cachedPreviewMat.SetFloat(GraphUtils.Param_UVz, uvZ);
            GraphUtils.GenerateToTexture(cachedPreviewMat, previewNoise, true);
        }
Beispiel #2
0
        /// <summary>
        /// Properly updates the preview texture for this window.
        /// Returns it in case anybody wants it (returns null if there was a problem loading the graph).
        /// </summary>
        public Texture2D GetPreview(bool regenerateShader)
        {
            //Regenerate shader.
            if (regenerateShader || previewMat == null)
            {
                //Render the gradient ramp to a texture.
                Gradient  gradient = MakeGradient();
                Texture2D myRamp   = new Texture2D(1024, 1, TextureFormat.RGBA32, false);
                myRamp.wrapMode = TextureWrapMode.Clamp;
                Color[] cols = new Color[myRamp.width];
                for (int i = 0; i < cols.Length; ++i)
                {
                    cols[i] = gradient.Evaluate((float)i / (float)(cols.Length - 1));
                }
                myRamp.SetPixels(cols);
                myRamp.Apply(false, true);

                Graph  graph  = new Graph(graphPaths[SelectedGraphIndex]);
                string errMsg = graph.Load();
                if (errMsg.Length > 0)
                {
                    Debug.LogError("Error loading graph " + graphPaths[SelectedGraphIndex] +
                                   ": " + errMsg);
                    return(null);
                }

                Shader shader = ShaderUtil.CreateShaderAsset(graph.GenerateShader(
                                                                 "Graph editor temp shader",
                                                                 "_textureGeneratorWindowGradient"));
                previewMat = new Material(shader);
                previewMat.SetTexture("_textureGeneratorWindowGradient", myRamp);
            }

            //Set parameters.
            gParams.SetParams(previewMat);
            previewMat.SetFloat(GraphUtils.Param_UVz, previewUVz);

            //Generate.
            GeneratePreview(ref previewTex, previewMat);
            return(previewTex);
        }