OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
9 #include "SkAnimTimer.h" | 9 #include "SkAnimTimer.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkGradientShader.h" | 11 #include "SkGradientShader.h" |
12 #include "SkPatchUtils.h" | 12 #include "SkPatchUtils.h" |
13 #include "SkPerlinNoiseShader.h" | 13 #include "SkPerlinNoiseShader2/SkPerlinNoiseShader2.h" |
14 #include "SkComposeShader.h" | 14 #include "SkComposeShader.h" |
15 | 15 |
16 static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) { | 16 static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) { |
17 //draw control points | 17 //draw control points |
18 SkPaint paint; | 18 SkPaint paint; |
19 SkPoint bottom[SkPatchUtils::kNumPtsCubic]; | 19 SkPoint bottom[SkPatchUtils::kNumPtsCubic]; |
20 SkPatchUtils::getBottomCubic(cubics, bottom); | 20 SkPatchUtils::getBottomCubic(cubics, bottom); |
21 SkPoint top[SkPatchUtils::kNumPtsCubic]; | 21 SkPoint top[SkPatchUtils::kNumPtsCubic]; |
22 SkPatchUtils::getTopCubic(cubics, top); | 22 SkPatchUtils::getTopCubic(cubics, top); |
23 SkPoint left[SkPatchUtils::kNumPtsCubic]; | 23 SkPoint left[SkPatchUtils::kNumPtsCubic]; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 //bottom points | 87 //bottom points |
88 fPts[6].set(300, 300); | 88 fPts[6].set(300, 300); |
89 fPts[7].set(250, 250); | 89 fPts[7].set(250, 250); |
90 //left points | 90 //left points |
91 fPts[8].set(150, 350); | 91 fPts[8].set(150, 350); |
92 fPts[9].set(100, 300); | 92 fPts[9].set(100, 300); |
93 fPts[10].set(50, 250); | 93 fPts[10].set(50, 250); |
94 fPts[11].set(150, 150); | 94 fPts[11].set(150, 150); |
95 | 95 |
96 const SkColor colors[SkPatchUtils::kNumCorners] = { | 96 const SkColor colors[SkPatchUtils::kNumCorners] = { |
97 SK_ColorBLUE, SK_ColorYELLOW | 97 0xFF5555FF, 0xFF8888FF, 0xFFCCCCFF |
98 }; | 98 }; |
99 | 99 const SkPoint points[2] = { SkPoint::Make(0.0f, 0.0f), |
100 fShader0 = SkGradientShader::CreateRadial(SkPoint::Make(128.0f, 128.0f), | 100 SkPoint::Make(100.0f, 100.0f) }; |
101 180.0f, | 101 fShader0 = SkGradientShader::CreateLinear(points, |
102 colors, | 102 colors, |
103 NULL, | 103 NULL, |
104 2, | 104 3, |
105 SkShader::kMirror_TileMode, | 105 SkShader::kMirror_TileMode, |
106 0, | 106 0, |
107 NULL); | 107 NULL); |
108 fShader1 = SkPerlinNoiseShader::CreateTurbulence(fXFreq, fYFreq, 2, fSee
d, NULL); | |
109 fShaderCompose = new SkComposeShader(fShader0, fShader1); | |
110 } | 108 } |
111 | 109 |
112 virtual ~PerlinPatchView() { | 110 virtual ~PerlinPatchView() { |
113 SkSafeUnref(fShader0); | 111 SkSafeUnref(fShader0); |
114 SkSafeUnref(fShader1); | |
115 SkSafeUnref(fShaderCompose); | |
116 } | 112 } |
117 protected: | 113 protected: |
118 // overrides from SkEventSink | 114 // overrides from SkEventSink |
119 bool onQuery(SkEvent* evt) override { | 115 bool onQuery(SkEvent* evt) override { |
120 if (SampleCode::TitleQ(*evt)) { | 116 if (SampleCode::TitleQ(*evt)) { |
121 SampleCode::TitleR(evt, "PerlinPatch"); | 117 SampleCode::TitleR(evt, "PerlinPatch"); |
122 return true; | 118 return true; |
123 } | 119 } |
124 return this->INHERITED::onQuery(evt); | 120 return this->INHERITED::onQuery(evt); |
125 } | 121 } |
126 | 122 |
127 bool onAnimate(const SkAnimTimer& timer) override { | 123 bool onAnimate(const SkAnimTimer& timer) override { |
| 124 fSeed += 0.005f; |
128 return true; | 125 return true; |
129 } | 126 } |
130 | 127 |
131 | 128 |
132 void onDrawContent(SkCanvas* canvas) override { | 129 void onDrawContent(SkCanvas* canvas) override { |
133 if (!canvas->getTotalMatrix().invert(&fInvMatrix)) { | 130 if (!canvas->getTotalMatrix().invert(&fInvMatrix)) { |
134 return; | 131 return; |
135 } | 132 } |
136 | 133 |
137 SkPaint paint; | 134 SkPaint paint; |
138 | 135 |
139 SkScalar texWidth = fTexScale * TexWidth; | 136 SkScalar texWidth = fTexScale * TexWidth; |
140 SkScalar texHeight = fTexScale * TexHeight; | 137 SkScalar texHeight = fTexScale * TexHeight; |
141 const SkPoint texCoords[SkPatchUtils::kNumCorners] = { | 138 const SkPoint texCoords[SkPatchUtils::kNumCorners] = { |
142 { fTexX - texWidth, fTexY - texHeight}, | 139 { fTexX - texWidth, fTexY - texHeight}, |
143 { fTexX + texWidth, fTexY - texHeight}, | 140 { fTexX + texWidth, fTexY - texHeight}, |
144 { fTexX + texWidth, fTexY + texHeight}, | 141 { fTexX + texWidth, fTexY + texHeight}, |
145 { fTexX - texWidth, fTexY + texHeight}} | 142 { fTexX - texWidth, fTexY + texHeight}} |
146 ; | 143 ; |
147 | 144 |
148 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kSrc_Mode))
; | 145 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kSrc_Mode))
; |
149 | 146 |
| 147 SkScalar scaleFreq = 2.0; |
| 148 fShader1 = SkPerlinNoiseShader2::CreateImprovedNoise(fXFreq/scaleFreq, f
YFreq/scaleFreq, 4, |
| 149 fSeed); |
| 150 fShaderCompose = new SkComposeShader(fShader0, fShader1); |
| 151 |
150 paint.setShader(fShaderCompose); | 152 paint.setShader(fShaderCompose); |
151 canvas->drawPatch(fPts, nullptr, texCoords, xfer, paint); | 153 canvas->drawPatch(fPts, nullptr, texCoords, xfer, paint); |
152 | 154 |
153 draw_control_points(canvas, fPts); | 155 draw_control_points(canvas, fPts); |
| 156 SkSafeUnref(fShader1); |
| 157 SkSafeUnref(fShaderCompose); |
154 } | 158 } |
155 | 159 |
156 class PtClick : public Click { | 160 class PtClick : public Click { |
157 public: | 161 public: |
158 int fIndex; | 162 int fIndex; |
159 PtClick(SkView* view, int index) : Click(view), fIndex(index) {} | 163 PtClick(SkView* view, int index) : Click(view), fIndex(index) {} |
160 }; | 164 }; |
161 | 165 |
162 static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) { | 166 static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) { |
163 return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5); | 167 return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 this->inval(nullptr); | 203 this->inval(nullptr); |
200 return true; | 204 return true; |
201 } | 205 } |
202 | 206 |
203 private: | 207 private: |
204 typedef SampleView INHERITED; | 208 typedef SampleView INHERITED; |
205 }; | 209 }; |
206 | 210 |
207 DEF_SAMPLE( return new PerlinPatchView(); ) | 211 DEF_SAMPLE( return new PerlinPatchView(); ) |
208 | 212 |
OLD | NEW |